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); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 231 | static int expr_constant(expr_ty); |
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 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1575 | /* These macros allows to check only for errors and not emmit bytecode |
| 1576 | * while visiting nodes. |
| 1577 | */ |
| 1578 | |
| 1579 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1580 | c->c_do_not_emit_bytecode++; |
| 1581 | |
| 1582 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1583 | c->c_do_not_emit_bytecode--; \ |
| 1584 | } |
| 1585 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1586 | /* Search if variable annotations are present statically in a block. */ |
| 1587 | |
| 1588 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1589 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1590 | { |
| 1591 | int i, j, res = 0; |
| 1592 | stmt_ty st; |
| 1593 | |
| 1594 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1595 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1596 | switch (st->kind) { |
| 1597 | case AnnAssign_kind: |
| 1598 | return 1; |
| 1599 | case For_kind: |
| 1600 | res = find_ann(st->v.For.body) || |
| 1601 | find_ann(st->v.For.orelse); |
| 1602 | break; |
| 1603 | case AsyncFor_kind: |
| 1604 | res = find_ann(st->v.AsyncFor.body) || |
| 1605 | find_ann(st->v.AsyncFor.orelse); |
| 1606 | break; |
| 1607 | case While_kind: |
| 1608 | res = find_ann(st->v.While.body) || |
| 1609 | find_ann(st->v.While.orelse); |
| 1610 | break; |
| 1611 | case If_kind: |
| 1612 | res = find_ann(st->v.If.body) || |
| 1613 | find_ann(st->v.If.orelse); |
| 1614 | break; |
| 1615 | case With_kind: |
| 1616 | res = find_ann(st->v.With.body); |
| 1617 | break; |
| 1618 | case AsyncWith_kind: |
| 1619 | res = find_ann(st->v.AsyncWith.body); |
| 1620 | break; |
| 1621 | case Try_kind: |
| 1622 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1623 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1624 | st->v.Try.handlers, j); |
| 1625 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1626 | return 1; |
| 1627 | } |
| 1628 | } |
| 1629 | res = find_ann(st->v.Try.body) || |
| 1630 | find_ann(st->v.Try.finalbody) || |
| 1631 | find_ann(st->v.Try.orelse); |
| 1632 | break; |
| 1633 | default: |
| 1634 | res = 0; |
| 1635 | } |
| 1636 | if (res) { |
| 1637 | break; |
| 1638 | } |
| 1639 | } |
| 1640 | return res; |
| 1641 | } |
| 1642 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1643 | /* |
| 1644 | * Frame block handling functions |
| 1645 | */ |
| 1646 | |
| 1647 | static int |
| 1648 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1649 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1650 | { |
| 1651 | struct fblockinfo *f; |
| 1652 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1653 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1654 | } |
| 1655 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1656 | f->fb_type = t; |
| 1657 | f->fb_block = b; |
| 1658 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1659 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1660 | return 1; |
| 1661 | } |
| 1662 | |
| 1663 | static void |
| 1664 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1665 | { |
| 1666 | struct compiler_unit *u = c->u; |
| 1667 | assert(u->u_nfblocks > 0); |
| 1668 | u->u_nfblocks--; |
| 1669 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1670 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1671 | } |
| 1672 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1673 | static int |
| 1674 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1675 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1676 | ADDOP(c, DUP_TOP); |
| 1677 | ADDOP(c, DUP_TOP); |
| 1678 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1679 | return 1; |
| 1680 | } |
| 1681 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1682 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1683 | * popping the blocks will be restored afterwards, unless another |
| 1684 | * return, break or continue is found. In which case, the TOS will |
| 1685 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1686 | */ |
| 1687 | static int |
| 1688 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1689 | int preserve_tos) |
| 1690 | { |
| 1691 | switch (info->fb_type) { |
| 1692 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1693 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1694 | return 1; |
| 1695 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1696 | case FOR_LOOP: |
| 1697 | /* Pop the iterator */ |
| 1698 | if (preserve_tos) { |
| 1699 | ADDOP(c, ROT_TWO); |
| 1700 | } |
| 1701 | ADDOP(c, POP_TOP); |
| 1702 | return 1; |
| 1703 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1704 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1705 | ADDOP(c, POP_BLOCK); |
| 1706 | return 1; |
| 1707 | |
| 1708 | case FINALLY_TRY: |
| 1709 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1710 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1711 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1712 | return 0; |
| 1713 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1714 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1715 | /* Emit the finally block, restoring the line number when done */ |
| 1716 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1717 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1718 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1719 | if (preserve_tos) { |
| 1720 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1721 | } |
| 1722 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1723 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1724 | case FINALLY_END: |
| 1725 | if (preserve_tos) { |
| 1726 | ADDOP(c, ROT_FOUR); |
| 1727 | } |
| 1728 | ADDOP(c, POP_TOP); |
| 1729 | ADDOP(c, POP_TOP); |
| 1730 | ADDOP(c, POP_TOP); |
| 1731 | if (preserve_tos) { |
| 1732 | ADDOP(c, ROT_FOUR); |
| 1733 | } |
| 1734 | ADDOP(c, POP_EXCEPT); |
| 1735 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1736 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1737 | case WITH: |
| 1738 | case ASYNC_WITH: |
| 1739 | ADDOP(c, POP_BLOCK); |
| 1740 | if (preserve_tos) { |
| 1741 | ADDOP(c, ROT_TWO); |
| 1742 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1743 | if(!compiler_call_exit_with_nones(c)) { |
| 1744 | return 0; |
| 1745 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1746 | if (info->fb_type == ASYNC_WITH) { |
| 1747 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1748 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1749 | ADDOP(c, YIELD_FROM); |
| 1750 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1751 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1752 | return 1; |
| 1753 | |
| 1754 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1755 | if (info->fb_datum) { |
| 1756 | ADDOP(c, POP_BLOCK); |
| 1757 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1758 | if (preserve_tos) { |
| 1759 | ADDOP(c, ROT_FOUR); |
| 1760 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1761 | ADDOP(c, POP_EXCEPT); |
| 1762 | if (info->fb_datum) { |
| 1763 | ADDOP_LOAD_CONST(c, Py_None); |
| 1764 | compiler_nameop(c, info->fb_datum, Store); |
| 1765 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1766 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1767 | return 1; |
| 1768 | |
| 1769 | case POP_VALUE: |
| 1770 | if (preserve_tos) { |
| 1771 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1772 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1773 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1774 | return 1; |
| 1775 | } |
| 1776 | Py_UNREACHABLE(); |
| 1777 | } |
| 1778 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1779 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1780 | static int |
| 1781 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1782 | if (c->u->u_nfblocks == 0) { |
| 1783 | return 1; |
| 1784 | } |
| 1785 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1786 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1787 | *loop = top; |
| 1788 | return 1; |
| 1789 | } |
| 1790 | struct fblockinfo copy = *top; |
| 1791 | c->u->u_nfblocks--; |
| 1792 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1793 | return 0; |
| 1794 | } |
| 1795 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1796 | return 0; |
| 1797 | } |
| 1798 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1799 | c->u->u_nfblocks++; |
| 1800 | return 1; |
| 1801 | } |
| 1802 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1803 | /* Compile a sequence of statements, checking for a docstring |
| 1804 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1805 | |
| 1806 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1807 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1808 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1809 | int i = 0; |
| 1810 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1811 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1812 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1813 | /* Set current line number to the line number of first statement. |
| 1814 | This way line number for SETUP_ANNOTATIONS will always |
| 1815 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1816 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1817 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1818 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1819 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1820 | } |
| 1821 | /* Every annotated class and module should have __annotations__. */ |
| 1822 | if (find_ann(stmts)) { |
| 1823 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1824 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1825 | if (!asdl_seq_LEN(stmts)) |
| 1826 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1827 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1828 | if (c->c_optimize < 2) { |
| 1829 | docstring = _PyAST_GetDocString(stmts); |
| 1830 | if (docstring) { |
| 1831 | i = 1; |
| 1832 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1833 | assert(st->kind == Expr_kind); |
| 1834 | VISIT(c, expr, st->v.Expr.value); |
| 1835 | if (!compiler_nameop(c, __doc__, Store)) |
| 1836 | return 0; |
| 1837 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1839 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1840 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | static PyCodeObject * |
| 1845 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1846 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | PyCodeObject *co; |
| 1848 | int addNone = 1; |
| 1849 | static PyObject *module; |
| 1850 | if (!module) { |
| 1851 | module = PyUnicode_InternFromString("<module>"); |
| 1852 | if (!module) |
| 1853 | return NULL; |
| 1854 | } |
| 1855 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1856 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | return NULL; |
| 1858 | switch (mod->kind) { |
| 1859 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1860 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | compiler_exit_scope(c); |
| 1862 | return 0; |
| 1863 | } |
| 1864 | break; |
| 1865 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1866 | if (find_ann(mod->v.Interactive.body)) { |
| 1867 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1868 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1870 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | case Expression_kind: |
| 1873 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1874 | addNone = 0; |
| 1875 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | default: |
| 1877 | PyErr_Format(PyExc_SystemError, |
| 1878 | "module kind %d should not be possible", |
| 1879 | mod->kind); |
| 1880 | return 0; |
| 1881 | } |
| 1882 | co = assemble(c, addNone); |
| 1883 | compiler_exit_scope(c); |
| 1884 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1887 | /* The test for LOCAL must come before the test for FREE in order to |
| 1888 | handle classes where name is both local and free. The local var is |
| 1889 | a method and the free var is a free var referenced within a method. |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1890 | */ |
| 1891 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1892 | static int |
| 1893 | get_ref_type(struct compiler *c, PyObject *name) |
| 1894 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1895 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1896 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1897 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1898 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1899 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1900 | if (scope == 0) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1901 | _Py_FatalErrorFormat(__func__, |
| 1902 | "unknown scope for %.100s in %.100s(%s)\n" |
| 1903 | "symbols: %s\nlocals: %s\nglobals: %s", |
| 1904 | PyUnicode_AsUTF8(name), |
| 1905 | PyUnicode_AsUTF8(c->u->u_name), |
| 1906 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1907 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1908 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1909 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1910 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | static int |
| 1916 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1917 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1918 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1919 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1920 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1921 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1922 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1926 | 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] | 1927 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1928 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1929 | if (qualname == NULL) |
| 1930 | qualname = co->co_name; |
| 1931 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1932 | if (free) { |
| 1933 | for (i = 0; i < free; ++i) { |
| 1934 | /* Bypass com_addop_varname because it will generate |
| 1935 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1936 | */ |
| 1937 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1938 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1939 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1940 | /* Special case: If a class contains a method with a |
| 1941 | free variable that has the same name as a method, |
| 1942 | the name will be considered free *and* local in the |
| 1943 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1944 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1945 | */ |
| 1946 | reftype = get_ref_type(c, name); |
| 1947 | if (reftype == CELL) |
| 1948 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1949 | else /* (reftype == FREE) */ |
| 1950 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1951 | if (arg == -1) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1952 | _Py_FatalErrorFormat(__func__, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1953 | "lookup %s in %s %d %d\n" |
| 1954 | "freevars of %s: %s\n", |
| 1955 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1956 | PyUnicode_AsUTF8(c->u->u_name), |
| 1957 | reftype, arg, |
| 1958 | PyUnicode_AsUTF8(co->co_name), |
| 1959 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1960 | } |
| 1961 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1963 | flags |= 0x08; |
| 1964 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1966 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1967 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1968 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1973 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1974 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 | if (!decos) |
| 1978 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1981 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1982 | } |
| 1983 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1987 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 1988 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1989 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1990 | /* Push a dict of keyword-only default values. |
| 1991 | |
| 1992 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1993 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1994 | int i; |
| 1995 | PyObject *keys = NULL; |
| 1996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1998 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1999 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 2000 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 2001 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2002 | if (!mangled) { |
| 2003 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2005 | if (keys == NULL) { |
| 2006 | keys = PyList_New(1); |
| 2007 | if (keys == NULL) { |
| 2008 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2009 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2010 | } |
| 2011 | PyList_SET_ITEM(keys, 0, mangled); |
| 2012 | } |
| 2013 | else { |
| 2014 | int res = PyList_Append(keys, mangled); |
| 2015 | Py_DECREF(mangled); |
| 2016 | if (res == -1) { |
| 2017 | goto error; |
| 2018 | } |
| 2019 | } |
| 2020 | if (!compiler_visit_expr(c, default_)) { |
| 2021 | goto error; |
| 2022 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | } |
| 2024 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2025 | if (keys != NULL) { |
| 2026 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2027 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2028 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2029 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2030 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2031 | assert(default_count > 0); |
| 2032 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2033 | } |
| 2034 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2035 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2036 | } |
| 2037 | |
| 2038 | error: |
| 2039 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2040 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2044 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2045 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2046 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2047 | return 1; |
| 2048 | } |
| 2049 | |
| 2050 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2051 | compiler_visit_argannotation(struct compiler *c, identifier id, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2052 | expr_ty annotation, Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2053 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | if (annotation) { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2055 | PyObject *mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2056 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2057 | return 0; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2058 | |
| 2059 | ADDOP_LOAD_CONST(c, mangled); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2060 | Py_DECREF(mangled); |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2061 | VISIT(c, annexpr, annotation); |
| 2062 | *annotations_len += 2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2064 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2068 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2069 | Py_ssize_t *annotations_len) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2070 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2071 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2073 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2074 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2075 | c, |
| 2076 | arg->arg, |
| 2077 | arg->annotation, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2078 | annotations_len)) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2079 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2080 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2081 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
| 2084 | static int |
| 2085 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2086 | expr_ty returns) |
| 2087 | { |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2088 | /* Push arg annotation names and values. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2089 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2090 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2091 | 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] | 2092 | */ |
| 2093 | static identifier return_str; |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2094 | Py_ssize_t annotations_len = 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2095 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2096 | if (!compiler_visit_argannotations(c, args->args, &annotations_len)) |
| 2097 | return 0; |
| 2098 | if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len)) |
| 2099 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2100 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2101 | !compiler_visit_argannotation(c, args->vararg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2102 | args->vararg->annotation, &annotations_len)) |
| 2103 | return 0; |
| 2104 | if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len)) |
| 2105 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2106 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2107 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2108 | args->kwarg->annotation, &annotations_len)) |
| 2109 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | if (!return_str) { |
| 2112 | return_str = PyUnicode_InternFromString("return"); |
| 2113 | if (!return_str) |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2114 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | } |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2116 | if (!compiler_visit_argannotation(c, return_str, returns, &annotations_len)) { |
| 2117 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2120 | if (annotations_len) { |
| 2121 | ADDOP_I(c, BUILD_TUPLE, annotations_len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2122 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2124 | |
Yurii Karabas | 7301979 | 2020-11-25 12:43:18 +0200 | [diff] [blame] | 2125 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2129 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2130 | { |
| 2131 | VISIT_SEQ(c, expr, args->defaults); |
| 2132 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2133 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2136 | static Py_ssize_t |
| 2137 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2138 | { |
| 2139 | Py_ssize_t funcflags = 0; |
| 2140 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2141 | if (!compiler_visit_defaults(c, args)) |
| 2142 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2143 | funcflags |= 0x01; |
| 2144 | } |
| 2145 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2146 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2147 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2148 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2149 | return -1; |
| 2150 | } |
| 2151 | else if (res > 0) { |
| 2152 | funcflags |= 0x02; |
| 2153 | } |
| 2154 | } |
| 2155 | return funcflags; |
| 2156 | } |
| 2157 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2158 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2159 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2160 | { |
| 2161 | |
| 2162 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2163 | compiler_error(c, "cannot assign to __debug__"); |
| 2164 | return 1; |
| 2165 | } |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | static int |
| 2170 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2171 | { |
| 2172 | if (arg != NULL) { |
| 2173 | if (forbidden_name(c, arg->arg, Store)) |
| 2174 | return 0; |
| 2175 | } |
| 2176 | return 1; |
| 2177 | } |
| 2178 | |
| 2179 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2180 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2181 | { |
| 2182 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2183 | 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] | 2184 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2185 | return 0; |
| 2186 | } |
| 2187 | } |
| 2188 | return 1; |
| 2189 | } |
| 2190 | |
| 2191 | static int |
| 2192 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2193 | { |
| 2194 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2195 | return 0; |
| 2196 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2197 | return 0; |
| 2198 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2199 | return 0; |
| 2200 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2201 | return 0; |
| 2202 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2203 | return 0; |
| 2204 | return 1; |
| 2205 | } |
| 2206 | |
| 2207 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2208 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2210 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2211 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2212 | arguments_ty args; |
| 2213 | expr_ty returns; |
| 2214 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2215 | asdl_expr_seq* decos; |
| 2216 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2217 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2218 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2219 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2220 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2221 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2222 | if (is_async) { |
| 2223 | assert(s->kind == AsyncFunctionDef_kind); |
| 2224 | |
| 2225 | args = s->v.AsyncFunctionDef.args; |
| 2226 | returns = s->v.AsyncFunctionDef.returns; |
| 2227 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2228 | name = s->v.AsyncFunctionDef.name; |
| 2229 | body = s->v.AsyncFunctionDef.body; |
| 2230 | |
| 2231 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2232 | } else { |
| 2233 | assert(s->kind == FunctionDef_kind); |
| 2234 | |
| 2235 | args = s->v.FunctionDef.args; |
| 2236 | returns = s->v.FunctionDef.returns; |
| 2237 | decos = s->v.FunctionDef.decorator_list; |
| 2238 | name = s->v.FunctionDef.name; |
| 2239 | body = s->v.FunctionDef.body; |
| 2240 | |
| 2241 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2242 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2243 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2244 | if (!compiler_check_debug_args(c, args)) |
| 2245 | return 0; |
| 2246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | if (!compiler_decorators(c, decos)) |
| 2248 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2249 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2250 | firstlineno = s->lineno; |
| 2251 | if (asdl_seq_LEN(decos)) { |
| 2252 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2253 | } |
| 2254 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2255 | funcflags = compiler_default_arguments(c, args); |
| 2256 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2258 | } |
| 2259 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2260 | annotations = compiler_visit_annotations(c, args, returns); |
| 2261 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2262 | return 0; |
| 2263 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2264 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2265 | funcflags |= 0x04; |
| 2266 | } |
| 2267 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2268 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2269 | return 0; |
| 2270 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2271 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2272 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2273 | if (c->c_optimize < 2) { |
| 2274 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2275 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2276 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | compiler_exit_scope(c); |
| 2278 | return 0; |
| 2279 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2282 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2284 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2285 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2286 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2287 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2288 | qualname = c->u->u_qualname; |
| 2289 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2291 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2292 | Py_XDECREF(qualname); |
| 2293 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2295 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2296 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2297 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2298 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2301 | /* decorators */ |
| 2302 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2303 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2304 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2305 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2306 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | static int |
| 2310 | compiler_class(struct compiler *c, stmt_ty s) |
| 2311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | PyCodeObject *co; |
| 2313 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2314 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2315 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2316 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | if (!compiler_decorators(c, decos)) |
| 2318 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2319 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2320 | firstlineno = s->lineno; |
| 2321 | if (asdl_seq_LEN(decos)) { |
| 2322 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2323 | } |
| 2324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2325 | /* ultimately generate code for: |
| 2326 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2327 | where: |
| 2328 | <func> is a function/closure created from the class body; |
| 2329 | it has a single argument (__locals__) where the dict |
| 2330 | (or MutableSequence) representing the locals is passed |
| 2331 | <name> is the class name |
| 2332 | <bases> is the positional arguments and *varargs argument |
| 2333 | <keywords> is the keyword arguments and **kwds argument |
| 2334 | This borrows from compiler_call. |
| 2335 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2338 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2339 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | return 0; |
| 2341 | /* this block represents what we do in the new scope */ |
| 2342 | { |
| 2343 | /* use the class name for name mangling */ |
| 2344 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2345 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | /* load (global) __name__ ... */ |
| 2347 | str = PyUnicode_InternFromString("__name__"); |
| 2348 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2349 | Py_XDECREF(str); |
| 2350 | compiler_exit_scope(c); |
| 2351 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2352 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 | Py_DECREF(str); |
| 2354 | /* ... and store it as __module__ */ |
| 2355 | str = PyUnicode_InternFromString("__module__"); |
| 2356 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2357 | Py_XDECREF(str); |
| 2358 | compiler_exit_scope(c); |
| 2359 | return 0; |
| 2360 | } |
| 2361 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2362 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2363 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2364 | str = PyUnicode_InternFromString("__qualname__"); |
| 2365 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2366 | Py_XDECREF(str); |
| 2367 | compiler_exit_scope(c); |
| 2368 | return 0; |
| 2369 | } |
| 2370 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2372 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | compiler_exit_scope(c); |
| 2374 | return 0; |
| 2375 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2376 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2377 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2378 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2379 | str = PyUnicode_InternFromString("__class__"); |
| 2380 | if (str == NULL) { |
| 2381 | compiler_exit_scope(c); |
| 2382 | return 0; |
| 2383 | } |
| 2384 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2385 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2386 | if (i < 0) { |
| 2387 | compiler_exit_scope(c); |
| 2388 | return 0; |
| 2389 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2390 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2392 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2393 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2394 | str = PyUnicode_InternFromString("__classcell__"); |
| 2395 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2396 | Py_XDECREF(str); |
| 2397 | compiler_exit_scope(c); |
| 2398 | return 0; |
| 2399 | } |
| 2400 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2402 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2403 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2404 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2405 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2406 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2407 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | /* create the code object */ |
| 2409 | co = assemble(c, 1); |
| 2410 | } |
| 2411 | /* leave the new scope */ |
| 2412 | compiler_exit_scope(c); |
| 2413 | if (co == NULL) |
| 2414 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | /* 2. load the 'build_class' function */ |
| 2417 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2418 | |
| 2419 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2420 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | Py_DECREF(co); |
| 2422 | |
| 2423 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2424 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | |
| 2426 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2427 | 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] | 2428 | return 0; |
| 2429 | |
| 2430 | /* 6. apply decorators */ |
| 2431 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2432 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2433 | } |
| 2434 | |
| 2435 | /* 7. store into <name> */ |
| 2436 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2437 | return 0; |
| 2438 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2439 | } |
| 2440 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2441 | /* Return 0 if the expression is a constant value except named singletons. |
| 2442 | Return 1 otherwise. */ |
| 2443 | static int |
| 2444 | check_is_arg(expr_ty e) |
| 2445 | { |
| 2446 | if (e->kind != Constant_kind) { |
| 2447 | return 1; |
| 2448 | } |
| 2449 | PyObject *value = e->v.Constant.value; |
| 2450 | return (value == Py_None |
| 2451 | || value == Py_False |
| 2452 | || value == Py_True |
| 2453 | || value == Py_Ellipsis); |
| 2454 | } |
| 2455 | |
| 2456 | /* Check operands of identity chacks ("is" and "is not"). |
| 2457 | Emit a warning if any operand is a constant except named singletons. |
| 2458 | Return 0 on error. |
| 2459 | */ |
| 2460 | static int |
| 2461 | check_compare(struct compiler *c, expr_ty e) |
| 2462 | { |
| 2463 | Py_ssize_t i, n; |
| 2464 | int left = check_is_arg(e->v.Compare.left); |
| 2465 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2466 | for (i = 0; i < n; i++) { |
| 2467 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2468 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2469 | if (op == Is || op == IsNot) { |
| 2470 | if (!right || !left) { |
| 2471 | const char *msg = (op == Is) |
| 2472 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2473 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2474 | return compiler_warn(c, msg); |
| 2475 | } |
| 2476 | } |
| 2477 | left = right; |
| 2478 | } |
| 2479 | return 1; |
| 2480 | } |
| 2481 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2482 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2483 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2484 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2485 | switch (op) { |
| 2486 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2487 | cmp = Py_EQ; |
| 2488 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2489 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2490 | cmp = Py_NE; |
| 2491 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2492 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2493 | cmp = Py_LT; |
| 2494 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2495 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2496 | cmp = Py_LE; |
| 2497 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2498 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2499 | cmp = Py_GT; |
| 2500 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2501 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2502 | cmp = Py_GE; |
| 2503 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2504 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2505 | ADDOP_I(c, IS_OP, 0); |
| 2506 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2507 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2508 | ADDOP_I(c, IS_OP, 1); |
| 2509 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2510 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2511 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2512 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2513 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2514 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2515 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2516 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2517 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2518 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2519 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2520 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2521 | } |
| 2522 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2523 | |
| 2524 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2525 | static int |
| 2526 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2527 | { |
| 2528 | switch (e->kind) { |
| 2529 | case UnaryOp_kind: |
| 2530 | if (e->v.UnaryOp.op == Not) |
| 2531 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2532 | /* fallback to general implementation */ |
| 2533 | break; |
| 2534 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2535 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2536 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2537 | assert(n >= 0); |
| 2538 | int cond2 = e->v.BoolOp.op == Or; |
| 2539 | basicblock *next2 = next; |
| 2540 | if (!cond2 != !cond) { |
| 2541 | next2 = compiler_new_block(c); |
| 2542 | if (next2 == NULL) |
| 2543 | return 0; |
| 2544 | } |
| 2545 | for (i = 0; i < n; ++i) { |
| 2546 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2547 | return 0; |
| 2548 | } |
| 2549 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2550 | return 0; |
| 2551 | if (next2 != next) |
| 2552 | compiler_use_next_block(c, next2); |
| 2553 | return 1; |
| 2554 | } |
| 2555 | case IfExp_kind: { |
| 2556 | basicblock *end, *next2; |
| 2557 | end = compiler_new_block(c); |
| 2558 | if (end == NULL) |
| 2559 | return 0; |
| 2560 | next2 = compiler_new_block(c); |
| 2561 | if (next2 == NULL) |
| 2562 | return 0; |
| 2563 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2564 | return 0; |
| 2565 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2566 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2567 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2568 | compiler_use_next_block(c, next2); |
| 2569 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2570 | return 0; |
| 2571 | compiler_use_next_block(c, end); |
| 2572 | return 1; |
| 2573 | } |
| 2574 | case Compare_kind: { |
| 2575 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2576 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2577 | if (!check_compare(c, e)) { |
| 2578 | return 0; |
| 2579 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2580 | basicblock *cleanup = compiler_new_block(c); |
| 2581 | if (cleanup == NULL) |
| 2582 | return 0; |
| 2583 | VISIT(c, expr, e->v.Compare.left); |
| 2584 | for (i = 0; i < n; i++) { |
| 2585 | VISIT(c, expr, |
| 2586 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2587 | ADDOP(c, DUP_TOP); |
| 2588 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2589 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2590 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2591 | NEXT_BLOCK(c); |
| 2592 | } |
| 2593 | 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] | 2594 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2595 | 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] | 2596 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2597 | basicblock *end = compiler_new_block(c); |
| 2598 | if (end == NULL) |
| 2599 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2600 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2601 | compiler_use_next_block(c, cleanup); |
| 2602 | ADDOP(c, POP_TOP); |
| 2603 | if (!cond) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2604 | ADDOP_JUMP(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2605 | } |
| 2606 | compiler_use_next_block(c, end); |
| 2607 | return 1; |
| 2608 | } |
| 2609 | /* fallback to general implementation */ |
| 2610 | break; |
| 2611 | } |
| 2612 | default: |
| 2613 | /* fallback to general implementation */ |
| 2614 | break; |
| 2615 | } |
| 2616 | |
| 2617 | /* general implementation */ |
| 2618 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2619 | 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] | 2620 | NEXT_BLOCK(c); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2621 | return 1; |
| 2622 | } |
| 2623 | |
| 2624 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2625 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2626 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | basicblock *end, *next; |
| 2628 | |
| 2629 | assert(e->kind == IfExp_kind); |
| 2630 | end = compiler_new_block(c); |
| 2631 | if (end == NULL) |
| 2632 | return 0; |
| 2633 | next = compiler_new_block(c); |
| 2634 | if (next == NULL) |
| 2635 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2636 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2637 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2639 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | compiler_use_next_block(c, next); |
| 2641 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2642 | compiler_use_next_block(c, end); |
| 2643 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
| 2646 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2648 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2650 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2652 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | arguments_ty args = e->v.Lambda.args; |
| 2654 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2655 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2656 | if (!compiler_check_debug_args(c, args)) |
| 2657 | return 0; |
| 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | if (!name) { |
| 2660 | name = PyUnicode_InternFromString("<lambda>"); |
| 2661 | if (!name) |
| 2662 | return 0; |
| 2663 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2664 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2665 | funcflags = compiler_default_arguments(c, args); |
| 2666 | if (funcflags == -1) { |
| 2667 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2668 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2669 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2670 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2671 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | /* Make None the first constant, so the lambda can't have a |
| 2675 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2676 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2680 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2682 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2683 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2684 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2685 | } |
| 2686 | else { |
| 2687 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2688 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2690 | qualname = c->u->u_qualname; |
| 2691 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2693 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2695 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2696 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2697 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | Py_DECREF(co); |
| 2699 | |
| 2700 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | compiler_if(struct compiler *c, stmt_ty s) |
| 2705 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | basicblock *end, *next; |
| 2707 | int constant; |
| 2708 | assert(s->kind == If_kind); |
| 2709 | end = compiler_new_block(c); |
| 2710 | if (end == NULL) |
| 2711 | return 0; |
| 2712 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2713 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2714 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2715 | * constant = 1: "if 1", "if 2", ... |
| 2716 | * constant = -1: rest */ |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2717 | if (constant == 0) { |
| 2718 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | VISIT_SEQ(c, stmt, s->v.If.body); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2720 | END_DO_NOT_EMIT_BYTECODE |
| 2721 | if (s->v.If.orelse) { |
| 2722 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2723 | } |
| 2724 | } else if (constant == 1) { |
| 2725 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2726 | if (s->v.If.orelse) { |
| 2727 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2728 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2729 | END_DO_NOT_EMIT_BYTECODE |
| 2730 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2732 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | next = compiler_new_block(c); |
| 2734 | if (next == NULL) |
| 2735 | return 0; |
| 2736 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2737 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | next = end; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2739 | } |
| 2740 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2741 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2742 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2744 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2745 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | compiler_use_next_block(c, next); |
| 2747 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2748 | } |
| 2749 | } |
| 2750 | compiler_use_next_block(c, end); |
| 2751 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2752 | } |
| 2753 | |
| 2754 | static int |
| 2755 | compiler_for(struct compiler *c, stmt_ty s) |
| 2756 | { |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2757 | basicblock *start, *body, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2759 | start = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2760 | body = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | cleanup = compiler_new_block(c); |
| 2762 | end = compiler_new_block(c); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2763 | if (start == NULL || body == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2765 | } |
| 2766 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2768 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | VISIT(c, expr, s->v.For.iter); |
| 2770 | ADDOP(c, GET_ITER); |
| 2771 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2772 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 2773 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | VISIT(c, expr, s->v.For.target); |
| 2775 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2776 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2778 | |
| 2779 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2782 | compiler_use_next_block(c, end); |
| 2783 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2786 | |
| 2787 | static int |
| 2788 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2789 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2790 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2791 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2792 | c->u->u_ste->ste_coroutine = 1; |
| 2793 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2794 | return compiler_error(c, "'async for' outside async function"); |
| 2795 | } |
| 2796 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2797 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2798 | except = compiler_new_block(c); |
| 2799 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2800 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2801 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2802 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2803 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2804 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2805 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2806 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2807 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2808 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2809 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2810 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2811 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2812 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2813 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2814 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2815 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2816 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2817 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2818 | /* Success block for __anext__ */ |
| 2819 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2820 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2821 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2822 | |
| 2823 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2824 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2825 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2826 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2827 | |
| 2828 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2829 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2830 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2831 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2832 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2833 | |
| 2834 | compiler_use_next_block(c, end); |
| 2835 | |
| 2836 | return 1; |
| 2837 | } |
| 2838 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2839 | static int |
| 2840 | compiler_while(struct compiler *c, stmt_ty s) |
| 2841 | { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2842 | basicblock *loop, *body, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2843 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | if (constant == 0) { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2846 | BEGIN_DO_NOT_EMIT_BYTECODE |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2847 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2848 | // inside a while loop so it can correctly evaluate syntax |
| 2849 | // errors. |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2850 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) { |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2851 | return 0; |
| 2852 | } |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2853 | VISIT_SEQ(c, stmt, s->v.While.body); |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2854 | // Remove the dummy block now that is not needed. |
| 2855 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2856 | END_DO_NOT_EMIT_BYTECODE |
| 2857 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2859 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | return 1; |
| 2861 | } |
| 2862 | loop = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2863 | body = compiler_new_block(c); |
| 2864 | anchor = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2865 | end = compiler_new_block(c); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2866 | if (loop == NULL || body == NULL || anchor == NULL || end == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | compiler_use_next_block(c, loop); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2870 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2873 | if (constant == -1) { |
| 2874 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) { |
| 2875 | return 0; |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | compiler_use_next_block(c, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2881 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2882 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2883 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2884 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2885 | compiler_use_next_block(c, anchor); |
| 2886 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2888 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2889 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2895 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2896 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2897 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2898 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2899 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2900 | return compiler_error(c, "'return' outside function"); |
| 2901 | if (s->v.Return.value != NULL && |
| 2902 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2903 | { |
| 2904 | return compiler_error( |
| 2905 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2907 | if (preserve_tos) { |
| 2908 | VISIT(c, expr, s->v.Return.value); |
| 2909 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2910 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2911 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2912 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2913 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2914 | } |
| 2915 | else if (!preserve_tos) { |
| 2916 | VISIT(c, expr, s->v.Return.value); |
| 2917 | } |
| 2918 | ADDOP(c, RETURN_VALUE); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2919 | NEXT_BLOCK(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2924 | static int |
| 2925 | compiler_break(struct compiler *c) |
| 2926 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2927 | struct fblockinfo *loop = NULL; |
| 2928 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2929 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2930 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2931 | if (loop == NULL) { |
| 2932 | return compiler_error(c, "'break' outside loop"); |
| 2933 | } |
| 2934 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2935 | return 0; |
| 2936 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2937 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2938 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2939 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | static int |
| 2943 | compiler_continue(struct compiler *c) |
| 2944 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2945 | struct fblockinfo *loop = NULL; |
| 2946 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2947 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2948 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2949 | if (loop == NULL) { |
| 2950 | return compiler_error(c, "'continue' not properly in loop"); |
| 2951 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2952 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 2953 | NEXT_BLOCK(c) |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2954 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2958 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | |
| 2960 | SETUP_FINALLY L |
| 2961 | <code for body> |
| 2962 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2963 | <code for finalbody> |
| 2964 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2965 | L: |
| 2966 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2967 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2968 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2969 | The special instructions use the block stack. Each block |
| 2970 | stack entry contains the instruction that created it (here |
| 2971 | SETUP_FINALLY), the level of the value stack at the time the |
| 2972 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | Pushes the current value stack level and the label |
| 2976 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2977 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2978 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2980 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2981 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2982 | exceptions are pushed onto the value stack (and the exception |
| 2983 | condition is cleared), and the interpreter jumps to the label |
| 2984 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | */ |
| 2986 | |
| 2987 | static int |
| 2988 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2989 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2990 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2992 | body = compiler_new_block(c); |
| 2993 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2994 | exit = compiler_new_block(c); |
| 2995 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2997 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2998 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2999 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3000 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3001 | 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] | 3002 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3003 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3004 | if (!compiler_try_except(c, s)) |
| 3005 | return 0; |
| 3006 | } |
| 3007 | else { |
| 3008 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3009 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3011 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3012 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3013 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3014 | /* `finally` block */ |
| 3015 | compiler_use_next_block(c, end); |
| 3016 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3017 | return 0; |
| 3018 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3019 | compiler_pop_fblock(c, FINALLY_END, end); |
| 3020 | ADDOP(c, RERAISE); |
| 3021 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3022 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3026 | 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] | 3027 | (The contents of the value stack is shown in [], with the top |
| 3028 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3029 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | |
| 3031 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3032 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | [] <code for S> |
| 3034 | [] POP_BLOCK |
| 3035 | [] JUMP_FORWARD L0 |
| 3036 | |
| 3037 | [tb, val, exc] L1: DUP ) |
| 3038 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3039 | [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] | 3040 | [tb, val, exc] POP |
| 3041 | [tb, val] <assign to V1> (or POP if no V1) |
| 3042 | [tb] POP |
| 3043 | [] <code for S1> |
| 3044 | JUMP_FORWARD L0 |
| 3045 | |
| 3046 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3047 | .............................etc....................... |
| 3048 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3049 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | |
| 3051 | [] L0: <next statement> |
| 3052 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3053 | Of course, parts are not generated if Vi or Ei is not present. |
| 3054 | */ |
| 3055 | static int |
| 3056 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3057 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3059 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | body = compiler_new_block(c); |
| 3062 | except = compiler_new_block(c); |
| 3063 | orelse = compiler_new_block(c); |
| 3064 | end = compiler_new_block(c); |
| 3065 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3066 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3067 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3069 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3071 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | ADDOP(c, POP_BLOCK); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3073 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3074 | ADDOP_JUMP(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3075 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3077 | /* Runtime will push a block here, so we need to account for that */ |
| 3078 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3079 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | for (i = 0; i < n; i++) { |
| 3081 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3082 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3084 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3085 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | except = compiler_new_block(c); |
| 3087 | if (except == NULL) |
| 3088 | return 0; |
| 3089 | if (handler->v.ExceptHandler.type) { |
| 3090 | ADDOP(c, DUP_TOP); |
| 3091 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3092 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3093 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | } |
| 3095 | ADDOP(c, POP_TOP); |
| 3096 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3097 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3098 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3099 | cleanup_end = compiler_new_block(c); |
| 3100 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3101 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3102 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3103 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3104 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3105 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3106 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3108 | /* |
| 3109 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3110 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3111 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3112 | try: |
| 3113 | # body |
| 3114 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3115 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3116 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3117 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3119 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3120 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3121 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3122 | 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] | 3123 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3125 | /* second # body */ |
| 3126 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3127 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3128 | ADDOP(c, POP_BLOCK); |
| 3129 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3130 | /* name = None; del name; # Mark as artificial */ |
| 3131 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3132 | ADDOP_LOAD_CONST(c, Py_None); |
| 3133 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3134 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3135 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3137 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3138 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3140 | /* name = None; del name; # Mark as artificial */ |
| 3141 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3142 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3143 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3144 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3146 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | } |
| 3148 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3149 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3151 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3152 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3153 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3155 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3156 | ADDOP(c, POP_TOP); |
| 3157 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3158 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3159 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3161 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3162 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3163 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | compiler_use_next_block(c, except); |
| 3166 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3167 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3168 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3170 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | compiler_use_next_block(c, end); |
| 3172 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3176 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3177 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3178 | return compiler_try_finally(c, s); |
| 3179 | else |
| 3180 | return compiler_try_except(c, s); |
| 3181 | } |
| 3182 | |
| 3183 | |
| 3184 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3186 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3187 | /* The IMPORT_NAME opcode was already generated. This function |
| 3188 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | 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] | 3191 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3193 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3194 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3195 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3196 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3197 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3198 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3199 | while (1) { |
| 3200 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3201 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3202 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3203 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3204 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3205 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3207 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3208 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3209 | if (dot == -1) { |
| 3210 | break; |
| 3211 | } |
| 3212 | ADDOP(c, ROT_TWO); |
| 3213 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3215 | if (!compiler_nameop(c, asname, Store)) { |
| 3216 | return 0; |
| 3217 | } |
| 3218 | ADDOP(c, POP_TOP); |
| 3219 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | } |
| 3221 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | static int |
| 3225 | compiler_import(struct compiler *c, stmt_ty s) |
| 3226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | /* The Import node stores a module name like a.b.c as a single |
| 3228 | string. This is convenient for all cases except |
| 3229 | import a.b.c as d |
| 3230 | where we need to parse that string to extract the individual |
| 3231 | module names. |
| 3232 | XXX Perhaps change the representation to make this case simpler? |
| 3233 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3234 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3235 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3236 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | for (i = 0; i < n; i++) { |
| 3238 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3239 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3241 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3242 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | if (alias->asname) { |
| 3246 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3247 | if (!r) |
| 3248 | return r; |
| 3249 | } |
| 3250 | else { |
| 3251 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3252 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3253 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3254 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3255 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3256 | if (tmp == NULL) |
| 3257 | return 0; |
| 3258 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3260 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | Py_DECREF(tmp); |
| 3262 | } |
| 3263 | if (!r) |
| 3264 | return r; |
| 3265 | } |
| 3266 | } |
| 3267 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | static int |
| 3271 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3272 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3273 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3274 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | if (!empty_string) { |
| 3278 | empty_string = PyUnicode_FromString(""); |
| 3279 | if (!empty_string) |
| 3280 | return 0; |
| 3281 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3282 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3283 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3284 | |
| 3285 | names = PyTuple_New(n); |
| 3286 | if (!names) |
| 3287 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3289 | /* build up the names */ |
| 3290 | for (i = 0; i < n; i++) { |
| 3291 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3292 | Py_INCREF(alias->name); |
| 3293 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3294 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3296 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3297 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | Py_DECREF(names); |
| 3299 | return compiler_error(c, "from __future__ imports must occur " |
| 3300 | "at the beginning of the file"); |
| 3301 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3302 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3304 | if (s->v.ImportFrom.module) { |
| 3305 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3306 | } |
| 3307 | else { |
| 3308 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3309 | } |
| 3310 | for (i = 0; i < n; i++) { |
| 3311 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3312 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3313 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3314 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3315 | assert(n == 1); |
| 3316 | ADDOP(c, IMPORT_STAR); |
| 3317 | return 1; |
| 3318 | } |
| 3319 | |
| 3320 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3321 | store_name = alias->name; |
| 3322 | if (alias->asname) |
| 3323 | store_name = alias->asname; |
| 3324 | |
| 3325 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | return 0; |
| 3327 | } |
| 3328 | } |
| 3329 | /* remove imported module */ |
| 3330 | ADDOP(c, POP_TOP); |
| 3331 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | static int |
| 3335 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3336 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3338 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3339 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3341 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3342 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3343 | { |
| 3344 | if (!compiler_warn(c, "assertion is always true, " |
| 3345 | "perhaps remove parentheses?")) |
| 3346 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3347 | return 0; |
| 3348 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3350 | end = compiler_new_block(c); |
| 3351 | if (end == NULL) |
| 3352 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3353 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3354 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3355 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | if (s->v.Assert.msg) { |
| 3357 | VISIT(c, expr, s->v.Assert.msg); |
| 3358 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3359 | } |
| 3360 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3361 | compiler_use_next_block(c, end); |
| 3362 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3366 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3367 | { |
| 3368 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3369 | VISIT(c, expr, value); |
| 3370 | ADDOP(c, PRINT_EXPR); |
| 3371 | return 1; |
| 3372 | } |
| 3373 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3374 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3375 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3376 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3377 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3378 | } |
| 3379 | |
| 3380 | VISIT(c, expr, value); |
| 3381 | ADDOP(c, POP_TOP); |
| 3382 | return 1; |
| 3383 | } |
| 3384 | |
| 3385 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3386 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3387 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3388 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3391 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3393 | switch (s->kind) { |
| 3394 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3395 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | case ClassDef_kind: |
| 3397 | return compiler_class(c, s); |
| 3398 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3399 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | case Delete_kind: |
| 3401 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3402 | break; |
| 3403 | case Assign_kind: |
| 3404 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3405 | VISIT(c, expr, s->v.Assign.value); |
| 3406 | for (i = 0; i < n; i++) { |
| 3407 | if (i < n - 1) |
| 3408 | ADDOP(c, DUP_TOP); |
| 3409 | VISIT(c, expr, |
| 3410 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3411 | } |
| 3412 | break; |
| 3413 | case AugAssign_kind: |
| 3414 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3415 | case AnnAssign_kind: |
| 3416 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | case For_kind: |
| 3418 | return compiler_for(c, s); |
| 3419 | case While_kind: |
| 3420 | return compiler_while(c, s); |
| 3421 | case If_kind: |
| 3422 | return compiler_if(c, s); |
| 3423 | case Raise_kind: |
| 3424 | n = 0; |
| 3425 | if (s->v.Raise.exc) { |
| 3426 | VISIT(c, expr, s->v.Raise.exc); |
| 3427 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3428 | if (s->v.Raise.cause) { |
| 3429 | VISIT(c, expr, s->v.Raise.cause); |
| 3430 | n++; |
| 3431 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3433 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 3434 | NEXT_BLOCK(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3436 | case Try_kind: |
| 3437 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3438 | case Assert_kind: |
| 3439 | return compiler_assert(c, s); |
| 3440 | case Import_kind: |
| 3441 | return compiler_import(c, s); |
| 3442 | case ImportFrom_kind: |
| 3443 | return compiler_from_import(c, s); |
| 3444 | case Global_kind: |
| 3445 | case Nonlocal_kind: |
| 3446 | break; |
| 3447 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3448 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3450 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | break; |
| 3452 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3453 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3454 | case Continue_kind: |
| 3455 | return compiler_continue(c); |
| 3456 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3457 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3458 | case AsyncFunctionDef_kind: |
| 3459 | return compiler_function(c, s, 1); |
| 3460 | case AsyncWith_kind: |
| 3461 | return compiler_async_with(c, s, 0); |
| 3462 | case AsyncFor_kind: |
| 3463 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3464 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | static int |
| 3470 | unaryop(unaryop_ty op) |
| 3471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | switch (op) { |
| 3473 | case Invert: |
| 3474 | return UNARY_INVERT; |
| 3475 | case Not: |
| 3476 | return UNARY_NOT; |
| 3477 | case UAdd: |
| 3478 | return UNARY_POSITIVE; |
| 3479 | case USub: |
| 3480 | return UNARY_NEGATIVE; |
| 3481 | default: |
| 3482 | PyErr_Format(PyExc_SystemError, |
| 3483 | "unary op %d should not be possible", op); |
| 3484 | return 0; |
| 3485 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3489 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | switch (op) { |
| 3492 | case Add: |
| 3493 | return BINARY_ADD; |
| 3494 | case Sub: |
| 3495 | return BINARY_SUBTRACT; |
| 3496 | case Mult: |
| 3497 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3498 | case MatMult: |
| 3499 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | case Div: |
| 3501 | return BINARY_TRUE_DIVIDE; |
| 3502 | case Mod: |
| 3503 | return BINARY_MODULO; |
| 3504 | case Pow: |
| 3505 | return BINARY_POWER; |
| 3506 | case LShift: |
| 3507 | return BINARY_LSHIFT; |
| 3508 | case RShift: |
| 3509 | return BINARY_RSHIFT; |
| 3510 | case BitOr: |
| 3511 | return BINARY_OR; |
| 3512 | case BitXor: |
| 3513 | return BINARY_XOR; |
| 3514 | case BitAnd: |
| 3515 | return BINARY_AND; |
| 3516 | case FloorDiv: |
| 3517 | return BINARY_FLOOR_DIVIDE; |
| 3518 | default: |
| 3519 | PyErr_Format(PyExc_SystemError, |
| 3520 | "binary op %d should not be possible", op); |
| 3521 | return 0; |
| 3522 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3526 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3527 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | switch (op) { |
| 3529 | case Add: |
| 3530 | return INPLACE_ADD; |
| 3531 | case Sub: |
| 3532 | return INPLACE_SUBTRACT; |
| 3533 | case Mult: |
| 3534 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3535 | case MatMult: |
| 3536 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3537 | case Div: |
| 3538 | return INPLACE_TRUE_DIVIDE; |
| 3539 | case Mod: |
| 3540 | return INPLACE_MODULO; |
| 3541 | case Pow: |
| 3542 | return INPLACE_POWER; |
| 3543 | case LShift: |
| 3544 | return INPLACE_LSHIFT; |
| 3545 | case RShift: |
| 3546 | return INPLACE_RSHIFT; |
| 3547 | case BitOr: |
| 3548 | return INPLACE_OR; |
| 3549 | case BitXor: |
| 3550 | return INPLACE_XOR; |
| 3551 | case BitAnd: |
| 3552 | return INPLACE_AND; |
| 3553 | case FloorDiv: |
| 3554 | return INPLACE_FLOOR_DIVIDE; |
| 3555 | default: |
| 3556 | PyErr_Format(PyExc_SystemError, |
| 3557 | "inplace binary op %d should not be possible", op); |
| 3558 | return 0; |
| 3559 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3560 | } |
| 3561 | |
| 3562 | static int |
| 3563 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3564 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3565 | int op, scope; |
| 3566 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
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 | PyObject *dict = c->u->u_names; |
| 3570 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3571 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3572 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3573 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3574 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3575 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3576 | if (forbidden_name(c, name, ctx)) |
| 3577 | return 0; |
| 3578 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3579 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3580 | if (!mangled) |
| 3581 | return 0; |
| 3582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | op = 0; |
| 3584 | optype = OP_NAME; |
| 3585 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3586 | switch (scope) { |
| 3587 | case FREE: |
| 3588 | dict = c->u->u_freevars; |
| 3589 | optype = OP_DEREF; |
| 3590 | break; |
| 3591 | case CELL: |
| 3592 | dict = c->u->u_cellvars; |
| 3593 | optype = OP_DEREF; |
| 3594 | break; |
| 3595 | case LOCAL: |
| 3596 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3597 | optype = OP_FAST; |
| 3598 | break; |
| 3599 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3600 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3601 | optype = OP_GLOBAL; |
| 3602 | break; |
| 3603 | case GLOBAL_EXPLICIT: |
| 3604 | optype = OP_GLOBAL; |
| 3605 | break; |
| 3606 | default: |
| 3607 | /* scope can be 0 */ |
| 3608 | break; |
| 3609 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3611 | /* 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] | 3612 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3614 | switch (optype) { |
| 3615 | case OP_DEREF: |
| 3616 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3617 | case Load: |
| 3618 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3619 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3620 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3621 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | } |
| 3623 | break; |
| 3624 | case OP_FAST: |
| 3625 | switch (ctx) { |
| 3626 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3627 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3630 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | return 1; |
| 3632 | case OP_GLOBAL: |
| 3633 | switch (ctx) { |
| 3634 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3635 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3636 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | } |
| 3638 | break; |
| 3639 | case OP_NAME: |
| 3640 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3641 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3642 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3644 | } |
| 3645 | break; |
| 3646 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3649 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3650 | Py_DECREF(mangled); |
| 3651 | if (arg < 0) |
| 3652 | return 0; |
| 3653 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | static int |
| 3657 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3659 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3660 | int jumpi; |
| 3661 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3662 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | assert(e->kind == BoolOp_kind); |
| 3665 | if (e->v.BoolOp.op == And) |
| 3666 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3667 | else |
| 3668 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3669 | end = compiler_new_block(c); |
| 3670 | if (end == NULL) |
| 3671 | return 0; |
| 3672 | s = e->v.BoolOp.values; |
| 3673 | n = asdl_seq_LEN(s) - 1; |
| 3674 | assert(n >= 0); |
| 3675 | for (i = 0; i < n; ++i) { |
| 3676 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3677 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3678 | basicblock *next = compiler_new_block(c); |
| 3679 | if (next == NULL) { |
| 3680 | return 0; |
| 3681 | } |
| 3682 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3683 | } |
| 3684 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3685 | compiler_use_next_block(c, end); |
| 3686 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3687 | } |
| 3688 | |
| 3689 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3690 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3691 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3692 | { |
| 3693 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3694 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3695 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3696 | PyObject *folded = PyTuple_New(n); |
| 3697 | if (folded == NULL) { |
| 3698 | return 0; |
| 3699 | } |
| 3700 | PyObject *val; |
| 3701 | for (i = 0; i < n; i++) { |
| 3702 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3703 | Py_INCREF(val); |
| 3704 | PyTuple_SET_ITEM(folded, i, val); |
| 3705 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3706 | if (tuple) { |
| 3707 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3708 | } else { |
| 3709 | if (add == SET_ADD) { |
| 3710 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3711 | if (folded == NULL) { |
| 3712 | return 0; |
| 3713 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3714 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3715 | ADDOP_I(c, build, pushed); |
| 3716 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3717 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3718 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3719 | return 1; |
| 3720 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3721 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3722 | for (i = 0; i < n; i++) { |
| 3723 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3724 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3725 | seen_star = 1; |
| 3726 | } |
| 3727 | } |
| 3728 | if (seen_star) { |
| 3729 | seen_star = 0; |
| 3730 | for (i = 0; i < n; i++) { |
| 3731 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3732 | if (elt->kind == Starred_kind) { |
| 3733 | if (seen_star == 0) { |
| 3734 | ADDOP_I(c, build, i+pushed); |
| 3735 | seen_star = 1; |
| 3736 | } |
| 3737 | VISIT(c, expr, elt->v.Starred.value); |
| 3738 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3739 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3740 | else { |
| 3741 | VISIT(c, expr, elt); |
| 3742 | if (seen_star) { |
| 3743 | ADDOP_I(c, add, 1); |
| 3744 | } |
| 3745 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3746 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3747 | assert(seen_star); |
| 3748 | if (tuple) { |
| 3749 | ADDOP(c, LIST_TO_TUPLE); |
| 3750 | } |
| 3751 | } |
| 3752 | else { |
| 3753 | for (i = 0; i < n; i++) { |
| 3754 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3755 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3756 | } |
| 3757 | if (tuple) { |
| 3758 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3759 | } else { |
| 3760 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3761 | } |
| 3762 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3763 | return 1; |
| 3764 | } |
| 3765 | |
| 3766 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3767 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3768 | { |
| 3769 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3770 | Py_ssize_t i; |
| 3771 | int seen_star = 0; |
| 3772 | for (i = 0; i < n; i++) { |
| 3773 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3774 | if (elt->kind == Starred_kind && !seen_star) { |
| 3775 | if ((i >= (1 << 8)) || |
| 3776 | (n-i-1 >= (INT_MAX >> 8))) |
| 3777 | return compiler_error(c, |
| 3778 | "too many expressions in " |
| 3779 | "star-unpacking assignment"); |
| 3780 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3781 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3782 | } |
| 3783 | else if (elt->kind == Starred_kind) { |
| 3784 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3785 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3786 | } |
| 3787 | } |
| 3788 | if (!seen_star) { |
| 3789 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3790 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3791 | for (i = 0; i < n; i++) { |
| 3792 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3793 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3794 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3795 | return 1; |
| 3796 | } |
| 3797 | |
| 3798 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3799 | compiler_list(struct compiler *c, expr_ty e) |
| 3800 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3801 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3802 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3803 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3804 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3805 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3806 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3807 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3809 | else |
| 3810 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3811 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | static int |
| 3815 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3816 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3817 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3818 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3819 | return assignment_helper(c, elts); |
| 3820 | } |
| 3821 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3822 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3823 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3824 | } |
| 3825 | else |
| 3826 | VISIT_SEQ(c, expr, elts); |
| 3827 | return 1; |
| 3828 | } |
| 3829 | |
| 3830 | static int |
| 3831 | compiler_set(struct compiler *c, expr_ty e) |
| 3832 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3833 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3834 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3835 | } |
| 3836 | |
| 3837 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3838 | 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] | 3839 | { |
| 3840 | Py_ssize_t i; |
| 3841 | for (i = begin; i < end; i++) { |
| 3842 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3843 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3844 | return 0; |
| 3845 | } |
| 3846 | return 1; |
| 3847 | } |
| 3848 | |
| 3849 | static int |
| 3850 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3851 | { |
| 3852 | Py_ssize_t i, n = end - begin; |
| 3853 | PyObject *keys, *key; |
| 3854 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3855 | for (i = begin; i < end; i++) { |
| 3856 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3857 | } |
| 3858 | keys = PyTuple_New(n); |
| 3859 | if (keys == NULL) { |
| 3860 | return 0; |
| 3861 | } |
| 3862 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3863 | 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] | 3864 | Py_INCREF(key); |
| 3865 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3866 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3867 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3868 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3869 | } |
| 3870 | else { |
| 3871 | for (i = begin; i < end; i++) { |
| 3872 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3873 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3874 | } |
| 3875 | ADDOP_I(c, BUILD_MAP, n); |
| 3876 | } |
| 3877 | return 1; |
| 3878 | } |
| 3879 | |
| 3880 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3881 | compiler_dict(struct compiler *c, expr_ty e) |
| 3882 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3883 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3884 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3885 | int is_unpacking = 0; |
| 3886 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3887 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3888 | elements = 0; |
| 3889 | for (i = 0; i < n; i++) { |
| 3890 | 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] | 3891 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3892 | if (elements) { |
| 3893 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3894 | return 0; |
| 3895 | } |
| 3896 | if (have_dict) { |
| 3897 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3898 | } |
| 3899 | have_dict = 1; |
| 3900 | elements = 0; |
| 3901 | } |
| 3902 | if (have_dict == 0) { |
| 3903 | ADDOP_I(c, BUILD_MAP, 0); |
| 3904 | have_dict = 1; |
| 3905 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3906 | 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] | 3907 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3908 | } |
| 3909 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3910 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3911 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3912 | return 0; |
| 3913 | } |
| 3914 | if (have_dict) { |
| 3915 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3916 | } |
| 3917 | have_dict = 1; |
| 3918 | elements = 0; |
| 3919 | } |
| 3920 | else { |
| 3921 | elements++; |
| 3922 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3925 | if (elements) { |
| 3926 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3927 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3928 | } |
| 3929 | if (have_dict) { |
| 3930 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3931 | } |
| 3932 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3933 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3934 | if (!have_dict) { |
| 3935 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3936 | } |
| 3937 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3938 | } |
| 3939 | |
| 3940 | static int |
| 3941 | compiler_compare(struct compiler *c, expr_ty e) |
| 3942 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3943 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3944 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3945 | if (!check_compare(c, e)) { |
| 3946 | return 0; |
| 3947 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3948 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3949 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3950 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3951 | if (n == 0) { |
| 3952 | 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] | 3953 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3954 | } |
| 3955 | else { |
| 3956 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | if (cleanup == NULL) |
| 3958 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3959 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3960 | VISIT(c, expr, |
| 3961 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3962 | ADDOP(c, DUP_TOP); |
| 3963 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3964 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3965 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3966 | NEXT_BLOCK(c); |
| 3967 | } |
| 3968 | 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] | 3969 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3970 | basicblock *end = compiler_new_block(c); |
| 3971 | if (end == NULL) |
| 3972 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3973 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3974 | compiler_use_next_block(c, cleanup); |
| 3975 | ADDOP(c, ROT_TWO); |
| 3976 | ADDOP(c, POP_TOP); |
| 3977 | compiler_use_next_block(c, end); |
| 3978 | } |
| 3979 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3982 | static PyTypeObject * |
| 3983 | infer_type(expr_ty e) |
| 3984 | { |
| 3985 | switch (e->kind) { |
| 3986 | case Tuple_kind: |
| 3987 | return &PyTuple_Type; |
| 3988 | case List_kind: |
| 3989 | case ListComp_kind: |
| 3990 | return &PyList_Type; |
| 3991 | case Dict_kind: |
| 3992 | case DictComp_kind: |
| 3993 | return &PyDict_Type; |
| 3994 | case Set_kind: |
| 3995 | case SetComp_kind: |
| 3996 | return &PySet_Type; |
| 3997 | case GeneratorExp_kind: |
| 3998 | return &PyGen_Type; |
| 3999 | case Lambda_kind: |
| 4000 | return &PyFunction_Type; |
| 4001 | case JoinedStr_kind: |
| 4002 | case FormattedValue_kind: |
| 4003 | return &PyUnicode_Type; |
| 4004 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4005 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4006 | default: |
| 4007 | return NULL; |
| 4008 | } |
| 4009 | } |
| 4010 | |
| 4011 | static int |
| 4012 | check_caller(struct compiler *c, expr_ty e) |
| 4013 | { |
| 4014 | switch (e->kind) { |
| 4015 | case Constant_kind: |
| 4016 | case Tuple_kind: |
| 4017 | case List_kind: |
| 4018 | case ListComp_kind: |
| 4019 | case Dict_kind: |
| 4020 | case DictComp_kind: |
| 4021 | case Set_kind: |
| 4022 | case SetComp_kind: |
| 4023 | case GeneratorExp_kind: |
| 4024 | case JoinedStr_kind: |
| 4025 | case FormattedValue_kind: |
| 4026 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4027 | "perhaps you missed a comma?", |
| 4028 | infer_type(e)->tp_name); |
| 4029 | default: |
| 4030 | return 1; |
| 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | static int |
| 4035 | check_subscripter(struct compiler *c, expr_ty e) |
| 4036 | { |
| 4037 | PyObject *v; |
| 4038 | |
| 4039 | switch (e->kind) { |
| 4040 | case Constant_kind: |
| 4041 | v = e->v.Constant.value; |
| 4042 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4043 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4044 | PyAnySet_Check(v))) |
| 4045 | { |
| 4046 | return 1; |
| 4047 | } |
| 4048 | /* fall through */ |
| 4049 | case Set_kind: |
| 4050 | case SetComp_kind: |
| 4051 | case GeneratorExp_kind: |
| 4052 | case Lambda_kind: |
| 4053 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4054 | "perhaps you missed a comma?", |
| 4055 | infer_type(e)->tp_name); |
| 4056 | default: |
| 4057 | return 1; |
| 4058 | } |
| 4059 | } |
| 4060 | |
| 4061 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4062 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4063 | { |
| 4064 | PyObject *v; |
| 4065 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4066 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4067 | if (index_type == NULL |
| 4068 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4069 | || index_type == &PySlice_Type) { |
| 4070 | return 1; |
| 4071 | } |
| 4072 | |
| 4073 | switch (e->kind) { |
| 4074 | case Constant_kind: |
| 4075 | v = e->v.Constant.value; |
| 4076 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4077 | return 1; |
| 4078 | } |
| 4079 | /* fall through */ |
| 4080 | case Tuple_kind: |
| 4081 | case List_kind: |
| 4082 | case ListComp_kind: |
| 4083 | case JoinedStr_kind: |
| 4084 | case FormattedValue_kind: |
| 4085 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4086 | "not %.200s; " |
| 4087 | "perhaps you missed a comma?", |
| 4088 | infer_type(e)->tp_name, |
| 4089 | index_type->tp_name); |
| 4090 | default: |
| 4091 | return 1; |
| 4092 | } |
| 4093 | } |
| 4094 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4095 | // 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] | 4096 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4097 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4098 | { |
| 4099 | Py_ssize_t argsl, i; |
| 4100 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4101 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4102 | |
| 4103 | /* Check that the call node is an attribute access, and that |
| 4104 | the call doesn't have keyword parameters. */ |
| 4105 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4106 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4107 | return -1; |
| 4108 | |
| 4109 | /* Check that there are no *varargs types of arguments. */ |
| 4110 | argsl = asdl_seq_LEN(args); |
| 4111 | for (i = 0; i < argsl; i++) { |
| 4112 | expr_ty elt = asdl_seq_GET(args, i); |
| 4113 | if (elt->kind == Starred_kind) { |
| 4114 | return -1; |
| 4115 | } |
| 4116 | } |
| 4117 | |
| 4118 | /* Alright, we can optimize the code. */ |
| 4119 | VISIT(c, expr, meth->v.Attribute.value); |
| 4120 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4121 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4122 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4123 | return 1; |
| 4124 | } |
| 4125 | |
| 4126 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4127 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4128 | { |
| 4129 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4130 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4131 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4132 | if (key->arg == NULL) { |
| 4133 | continue; |
| 4134 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4135 | if (forbidden_name(c, key->arg, Store)) { |
| 4136 | return -1; |
| 4137 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4138 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4139 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4140 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
| 4141 | PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg); |
| 4142 | if (msg == NULL) { |
| 4143 | return -1; |
| 4144 | } |
| 4145 | c->u->u_col_offset = other->col_offset; |
| 4146 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
| 4147 | Py_DECREF(msg); |
| 4148 | return -1; |
| 4149 | } |
| 4150 | } |
| 4151 | } |
| 4152 | return 0; |
| 4153 | } |
| 4154 | |
| 4155 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4156 | compiler_call(struct compiler *c, expr_ty e) |
| 4157 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4158 | int ret = maybe_optimize_method_call(c, e); |
| 4159 | if (ret >= 0) { |
| 4160 | return ret; |
| 4161 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4162 | if (!check_caller(c, e->v.Call.func)) { |
| 4163 | return 0; |
| 4164 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4165 | VISIT(c, expr, e->v.Call.func); |
| 4166 | return compiler_call_helper(c, 0, |
| 4167 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4168 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4169 | } |
| 4170 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4171 | static int |
| 4172 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4173 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4174 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4175 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4176 | 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] | 4177 | return 1; |
| 4178 | } |
| 4179 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4180 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4181 | static int |
| 4182 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4183 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4184 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4185 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4186 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4187 | Convert the conversion char to 3 bits: |
| 4188 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4189 | !s : 001 0x1 FVC_STR |
| 4190 | !r : 010 0x2 FVC_REPR |
| 4191 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4192 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4193 | next bit is whether or not we have a format spec: |
| 4194 | yes : 100 0x4 |
| 4195 | no : 000 0x0 |
| 4196 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4197 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4198 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4199 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4200 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4201 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4202 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4203 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4204 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4205 | case 's': oparg = FVC_STR; break; |
| 4206 | case 'r': oparg = FVC_REPR; break; |
| 4207 | case 'a': oparg = FVC_ASCII; break; |
| 4208 | case -1: oparg = FVC_NONE; break; |
| 4209 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4210 | PyErr_Format(PyExc_SystemError, |
| 4211 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4212 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4213 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4214 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4215 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4216 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4217 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4218 | } |
| 4219 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4220 | /* And push our opcode and oparg */ |
| 4221 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4222 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4223 | return 1; |
| 4224 | } |
| 4225 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4226 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4227 | 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] | 4228 | { |
| 4229 | Py_ssize_t i, n = end - begin; |
| 4230 | keyword_ty kw; |
| 4231 | PyObject *keys, *key; |
| 4232 | assert(n > 0); |
| 4233 | if (n > 1) { |
| 4234 | for (i = begin; i < end; i++) { |
| 4235 | kw = asdl_seq_GET(keywords, i); |
| 4236 | VISIT(c, expr, kw->value); |
| 4237 | } |
| 4238 | keys = PyTuple_New(n); |
| 4239 | if (keys == NULL) { |
| 4240 | return 0; |
| 4241 | } |
| 4242 | for (i = begin; i < end; i++) { |
| 4243 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4244 | Py_INCREF(key); |
| 4245 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4246 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4247 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4248 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4249 | } |
| 4250 | else { |
| 4251 | /* a for loop only executes once */ |
| 4252 | for (i = begin; i < end; i++) { |
| 4253 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4254 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4255 | VISIT(c, expr, kw->value); |
| 4256 | } |
| 4257 | ADDOP_I(c, BUILD_MAP, n); |
| 4258 | } |
| 4259 | return 1; |
| 4260 | } |
| 4261 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4262 | /* shared code between compiler_call and compiler_class */ |
| 4263 | static int |
| 4264 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4265 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4266 | asdl_expr_seq *args, |
| 4267 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4268 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4269 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4270 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4271 | if (validate_keywords(c, keywords) == -1) { |
| 4272 | return 0; |
| 4273 | } |
| 4274 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4275 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4276 | nkwelts = asdl_seq_LEN(keywords); |
| 4277 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4278 | for (i = 0; i < nelts; i++) { |
| 4279 | expr_ty elt = asdl_seq_GET(args, i); |
| 4280 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4281 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4282 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4283 | } |
| 4284 | for (i = 0; i < nkwelts; i++) { |
| 4285 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4286 | if (kw->arg == NULL) { |
| 4287 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4288 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4290 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4291 | /* No * or ** args, so can use faster calling sequence */ |
| 4292 | for (i = 0; i < nelts; i++) { |
| 4293 | expr_ty elt = asdl_seq_GET(args, i); |
| 4294 | assert(elt->kind != Starred_kind); |
| 4295 | VISIT(c, expr, elt); |
| 4296 | } |
| 4297 | if (nkwelts) { |
| 4298 | PyObject *names; |
| 4299 | VISIT_SEQ(c, keyword, keywords); |
| 4300 | names = PyTuple_New(nkwelts); |
| 4301 | if (names == NULL) { |
| 4302 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4303 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4304 | for (i = 0; i < nkwelts; i++) { |
| 4305 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4306 | Py_INCREF(kw->arg); |
| 4307 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4308 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4309 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4310 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4311 | return 1; |
| 4312 | } |
| 4313 | else { |
| 4314 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4315 | return 1; |
| 4316 | } |
| 4317 | |
| 4318 | ex_call: |
| 4319 | |
| 4320 | /* Do positional arguments. */ |
| 4321 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4322 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4323 | } |
| 4324 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4325 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4326 | return 0; |
| 4327 | } |
| 4328 | /* Then keyword arguments */ |
| 4329 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4330 | /* Has a new dict been pushed */ |
| 4331 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4332 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4333 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4334 | for (i = 0; i < nkwelts; i++) { |
| 4335 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4336 | if (kw->arg == NULL) { |
| 4337 | /* A keyword argument unpacking. */ |
| 4338 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4339 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4340 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4341 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4342 | if (have_dict) { |
| 4343 | ADDOP_I(c, DICT_MERGE, 1); |
| 4344 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4345 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4346 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4347 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4348 | if (!have_dict) { |
| 4349 | ADDOP_I(c, BUILD_MAP, 0); |
| 4350 | have_dict = 1; |
| 4351 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4352 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4353 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4354 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4355 | else { |
| 4356 | nseen++; |
| 4357 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4358 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4359 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4360 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4361 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4362 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4363 | } |
| 4364 | if (have_dict) { |
| 4365 | ADDOP_I(c, DICT_MERGE, 1); |
| 4366 | } |
| 4367 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4368 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4369 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4371 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4372 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4373 | } |
| 4374 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4375 | |
| 4376 | /* List and set comprehensions and generator expressions work by creating a |
| 4377 | nested function to perform the actual iteration. This means that the |
| 4378 | iteration variables don't leak into the current scope. |
| 4379 | The defined function is called immediately following its definition, with the |
| 4380 | result of that call being the result of the expression. |
| 4381 | The LC/SC version returns the populated container, while the GE version is |
| 4382 | flagged in symtable.c as a generator, so it returns the generator object |
| 4383 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4384 | |
| 4385 | Possible cleanups: |
| 4386 | - iterate over the generator sequence instead of using recursion |
| 4387 | */ |
| 4388 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4389 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4390 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4391 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4392 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4393 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4395 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4396 | comprehension_ty gen; |
| 4397 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4398 | if (gen->is_async) { |
| 4399 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4400 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4401 | } else { |
| 4402 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4403 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | static int |
| 4408 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4409 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4410 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4411 | expr_ty elt, expr_ty val, int type) |
| 4412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | /* generate code for the iterator, then each of the ifs, |
| 4414 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4415 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | comprehension_ty gen; |
| 4417 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4418 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | start = compiler_new_block(c); |
| 4421 | skip = compiler_new_block(c); |
| 4422 | if_cleanup = compiler_new_block(c); |
| 4423 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4426 | anchor == NULL) |
| 4427 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4428 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4429 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4431 | if (gen_index == 0) { |
| 4432 | /* Receive outermost iter as an implicit argument */ |
| 4433 | c->u->u_argcount = 1; |
| 4434 | ADDOP_I(c, LOAD_FAST, 0); |
| 4435 | } |
| 4436 | else { |
| 4437 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4438 | /* Fast path for the temporary variable assignment idiom: |
| 4439 | for y in [f(x)] |
| 4440 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4441 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4442 | switch (gen->iter->kind) { |
| 4443 | case List_kind: |
| 4444 | elts = gen->iter->v.List.elts; |
| 4445 | break; |
| 4446 | case Tuple_kind: |
| 4447 | elts = gen->iter->v.Tuple.elts; |
| 4448 | break; |
| 4449 | default: |
| 4450 | elts = NULL; |
| 4451 | } |
| 4452 | if (asdl_seq_LEN(elts) == 1) { |
| 4453 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4454 | if (elt->kind != Starred_kind) { |
| 4455 | VISIT(c, expr, elt); |
| 4456 | start = NULL; |
| 4457 | } |
| 4458 | } |
| 4459 | if (start) { |
| 4460 | VISIT(c, expr, gen->iter); |
| 4461 | ADDOP(c, GET_ITER); |
| 4462 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4464 | if (start) { |
| 4465 | depth++; |
| 4466 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4467 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4468 | NEXT_BLOCK(c); |
| 4469 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4470 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | /* XXX this needs to be cleaned up...a lot! */ |
| 4473 | n = asdl_seq_LEN(gen->ifs); |
| 4474 | for (i = 0; i < n; i++) { |
| 4475 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4476 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4477 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | NEXT_BLOCK(c); |
| 4479 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4480 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4482 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4483 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | elt, val, type)) |
| 4485 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | /* only append after the last for generator */ |
| 4488 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4489 | /* comprehension specific code */ |
| 4490 | switch (type) { |
| 4491 | case COMP_GENEXP: |
| 4492 | VISIT(c, expr, elt); |
| 4493 | ADDOP(c, YIELD_VALUE); |
| 4494 | ADDOP(c, POP_TOP); |
| 4495 | break; |
| 4496 | case COMP_LISTCOMP: |
| 4497 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4498 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | break; |
| 4500 | case COMP_SETCOMP: |
| 4501 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4502 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | break; |
| 4504 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4505 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4508 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4509 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | break; |
| 4511 | default: |
| 4512 | return 0; |
| 4513 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4515 | compiler_use_next_block(c, skip); |
| 4516 | } |
| 4517 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4518 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4519 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4520 | compiler_use_next_block(c, anchor); |
| 4521 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4522 | |
| 4523 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4527 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4528 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4529 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4530 | expr_ty elt, expr_ty val, int type) |
| 4531 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4532 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4533 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4534 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4535 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4536 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4537 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4538 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4539 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4540 | return 0; |
| 4541 | } |
| 4542 | |
| 4543 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4544 | |
| 4545 | if (gen_index == 0) { |
| 4546 | /* Receive outermost iter as an implicit argument */ |
| 4547 | c->u->u_argcount = 1; |
| 4548 | ADDOP_I(c, LOAD_FAST, 0); |
| 4549 | } |
| 4550 | else { |
| 4551 | /* Sub-iter - calculate on the fly */ |
| 4552 | VISIT(c, expr, gen->iter); |
| 4553 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4556 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4557 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4558 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4560 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4561 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4562 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4563 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4564 | |
| 4565 | n = asdl_seq_LEN(gen->ifs); |
| 4566 | for (i = 0; i < n; i++) { |
| 4567 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4568 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4569 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4570 | NEXT_BLOCK(c); |
| 4571 | } |
| 4572 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4573 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4574 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4575 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4576 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4577 | elt, val, type)) |
| 4578 | return 0; |
| 4579 | |
| 4580 | /* only append after the last for generator */ |
| 4581 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4582 | /* comprehension specific code */ |
| 4583 | switch (type) { |
| 4584 | case COMP_GENEXP: |
| 4585 | VISIT(c, expr, elt); |
| 4586 | ADDOP(c, YIELD_VALUE); |
| 4587 | ADDOP(c, POP_TOP); |
| 4588 | break; |
| 4589 | case COMP_LISTCOMP: |
| 4590 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4591 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4592 | break; |
| 4593 | case COMP_SETCOMP: |
| 4594 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4595 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4596 | break; |
| 4597 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4598 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4599 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4600 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4601 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4602 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4603 | break; |
| 4604 | default: |
| 4605 | return 0; |
| 4606 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4607 | } |
| 4608 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4609 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4610 | |
| 4611 | compiler_use_next_block(c, except); |
| 4612 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4613 | |
| 4614 | return 1; |
| 4615 | } |
| 4616 | |
| 4617 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4618 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4619 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4620 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4623 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4624 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4625 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4626 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4627 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4628 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4629 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4630 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4631 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4632 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4633 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4635 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4639 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4640 | 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] | 4641 | compiler_error(c, "asynchronous comprehension outside of " |
| 4642 | "an asynchronous function"); |
| 4643 | goto error_in_scope; |
| 4644 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | if (type != COMP_GENEXP) { |
| 4647 | int op; |
| 4648 | switch (type) { |
| 4649 | case COMP_LISTCOMP: |
| 4650 | op = BUILD_LIST; |
| 4651 | break; |
| 4652 | case COMP_SETCOMP: |
| 4653 | op = BUILD_SET; |
| 4654 | break; |
| 4655 | case COMP_DICTCOMP: |
| 4656 | op = BUILD_MAP; |
| 4657 | break; |
| 4658 | default: |
| 4659 | PyErr_Format(PyExc_SystemError, |
| 4660 | "unknown comprehension type %d", type); |
| 4661 | goto error_in_scope; |
| 4662 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | ADDOP_I(c, op, 0); |
| 4665 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4666 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4667 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4668 | val, type)) |
| 4669 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4671 | if (type != COMP_GENEXP) { |
| 4672 | ADDOP(c, RETURN_VALUE); |
| 4673 | } |
| 4674 | |
| 4675 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4676 | qualname = c->u->u_qualname; |
| 4677 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4679 | if (top_level_await && is_async_generator){ |
| 4680 | c->u->u_ste->ste_coroutine = 1; |
| 4681 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4682 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | goto error; |
| 4684 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4685 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4686 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4687 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | Py_DECREF(co); |
| 4689 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4690 | VISIT(c, expr, outermost->iter); |
| 4691 | |
| 4692 | if (outermost->is_async) { |
| 4693 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4694 | } else { |
| 4695 | ADDOP(c, GET_ITER); |
| 4696 | } |
| 4697 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4698 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4699 | |
| 4700 | if (is_async_generator && type != COMP_GENEXP) { |
| 4701 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4702 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4703 | ADDOP(c, YIELD_FROM); |
| 4704 | } |
| 4705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4707 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4708 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4709 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4710 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | Py_XDECREF(co); |
| 4712 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4713 | } |
| 4714 | |
| 4715 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4716 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | static identifier name; |
| 4719 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4720 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | if (!name) |
| 4722 | return 0; |
| 4723 | } |
| 4724 | assert(e->kind == GeneratorExp_kind); |
| 4725 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4726 | e->v.GeneratorExp.generators, |
| 4727 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4728 | } |
| 4729 | |
| 4730 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4731 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | static identifier name; |
| 4734 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4735 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | if (!name) |
| 4737 | return 0; |
| 4738 | } |
| 4739 | assert(e->kind == ListComp_kind); |
| 4740 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4741 | e->v.ListComp.generators, |
| 4742 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4743 | } |
| 4744 | |
| 4745 | static int |
| 4746 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4747 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4748 | static identifier name; |
| 4749 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4750 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4751 | if (!name) |
| 4752 | return 0; |
| 4753 | } |
| 4754 | assert(e->kind == SetComp_kind); |
| 4755 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4756 | e->v.SetComp.generators, |
| 4757 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4758 | } |
| 4759 | |
| 4760 | |
| 4761 | static int |
| 4762 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4763 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4764 | static identifier name; |
| 4765 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4766 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | if (!name) |
| 4768 | return 0; |
| 4769 | } |
| 4770 | assert(e->kind == DictComp_kind); |
| 4771 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4772 | e->v.DictComp.generators, |
| 4773 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | |
| 4777 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4778 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4779 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | VISIT(c, expr, k->value); |
| 4781 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4784 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4785 | whether they are true or false. |
| 4786 | |
| 4787 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4788 | */ |
| 4789 | |
| 4790 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4791 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4792 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4793 | if (e->kind == Constant_kind) { |
| 4794 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4795 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4796 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4799 | static int |
| 4800 | compiler_with_except_finish(struct compiler *c) { |
| 4801 | basicblock *exit; |
| 4802 | exit = compiler_new_block(c); |
| 4803 | if (exit == NULL) |
| 4804 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4805 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 4806 | NEXT_BLOCK(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4807 | ADDOP(c, RERAISE); |
| 4808 | compiler_use_next_block(c, exit); |
| 4809 | ADDOP(c, POP_TOP); |
| 4810 | ADDOP(c, POP_TOP); |
| 4811 | ADDOP(c, POP_TOP); |
| 4812 | ADDOP(c, POP_EXCEPT); |
| 4813 | ADDOP(c, POP_TOP); |
| 4814 | return 1; |
| 4815 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4816 | |
| 4817 | /* |
| 4818 | Implements the async with statement. |
| 4819 | |
| 4820 | The semantics outlined in that PEP are as follows: |
| 4821 | |
| 4822 | async with EXPR as VAR: |
| 4823 | BLOCK |
| 4824 | |
| 4825 | It is implemented roughly as: |
| 4826 | |
| 4827 | context = EXPR |
| 4828 | exit = context.__aexit__ # not calling it |
| 4829 | value = await context.__aenter__() |
| 4830 | try: |
| 4831 | VAR = value # if VAR present in the syntax |
| 4832 | BLOCK |
| 4833 | finally: |
| 4834 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4835 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4836 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4837 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4838 | if not (await exit(*exc)): |
| 4839 | raise |
| 4840 | */ |
| 4841 | static int |
| 4842 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4843 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4844 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4845 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4846 | |
| 4847 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4848 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4849 | c->u->u_ste->ste_coroutine = 1; |
| 4850 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4851 | return compiler_error(c, "'async with' outside async function"); |
| 4852 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4853 | |
| 4854 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4855 | final = compiler_new_block(c); |
| 4856 | exit = compiler_new_block(c); |
| 4857 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4858 | return 0; |
| 4859 | |
| 4860 | /* Evaluate EXPR */ |
| 4861 | VISIT(c, expr, item->context_expr); |
| 4862 | |
| 4863 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4864 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4865 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4866 | ADDOP(c, YIELD_FROM); |
| 4867 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4868 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4869 | |
| 4870 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4871 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4872 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4873 | return 0; |
| 4874 | } |
| 4875 | |
| 4876 | if (item->optional_vars) { |
| 4877 | VISIT(c, expr, item->optional_vars); |
| 4878 | } |
| 4879 | else { |
| 4880 | /* Discard result from context.__aenter__() */ |
| 4881 | ADDOP(c, POP_TOP); |
| 4882 | } |
| 4883 | |
| 4884 | pos++; |
| 4885 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4886 | /* BLOCK code */ |
| 4887 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4888 | else if (!compiler_async_with(c, s, pos)) |
| 4889 | return 0; |
| 4890 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4891 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4892 | ADDOP(c, POP_BLOCK); |
| 4893 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4894 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4895 | /* For successful outcome: |
| 4896 | * call __exit__(None, None, None) |
| 4897 | */ |
| 4898 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4899 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4900 | ADDOP(c, GET_AWAITABLE); |
| 4901 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4902 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4903 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4904 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4905 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4906 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4907 | |
| 4908 | /* For exceptional outcome: */ |
| 4909 | compiler_use_next_block(c, final); |
| 4910 | |
| 4911 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4912 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4913 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4914 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4915 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4916 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4917 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4918 | return 1; |
| 4919 | } |
| 4920 | |
| 4921 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4922 | /* |
| 4923 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4924 | with EXPR as VAR: |
| 4925 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4926 | is implemented as: |
| 4927 | <code for EXPR> |
| 4928 | SETUP_WITH E |
| 4929 | <code to store to VAR> or POP_TOP |
| 4930 | <code for BLOCK> |
| 4931 | LOAD_CONST (None, None, None) |
| 4932 | CALL_FUNCTION_EX 0 |
| 4933 | JUMP_FORWARD EXIT |
| 4934 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4935 | POP_JUMP_IF_TRUE T: |
| 4936 | RERAISE |
| 4937 | T: POP_TOP * 3 (remove exception from stack) |
| 4938 | POP_EXCEPT |
| 4939 | POP_TOP |
| 4940 | 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 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4943 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4944 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4945 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4946 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4947 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4948 | |
| 4949 | assert(s->kind == With_kind); |
| 4950 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4951 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4952 | final = compiler_new_block(c); |
| 4953 | exit = compiler_new_block(c); |
| 4954 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4955 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4956 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4957 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4958 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4959 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4960 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4961 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4962 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4963 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4964 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4965 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4966 | } |
| 4967 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4968 | if (item->optional_vars) { |
| 4969 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4970 | } |
| 4971 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4972 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4973 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4974 | } |
| 4975 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4976 | pos++; |
| 4977 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4978 | /* BLOCK code */ |
| 4979 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4980 | else if (!compiler_with(c, s, pos)) |
| 4981 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4982 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4983 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4984 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4985 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4986 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4987 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4988 | /* For successful outcome: |
| 4989 | * call __exit__(None, None, None) |
| 4990 | */ |
| 4991 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4992 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4993 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4994 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4995 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4996 | /* For exceptional outcome: */ |
| 4997 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4998 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4999 | ADDOP(c, WITH_EXCEPT_START); |
| 5000 | compiler_with_except_finish(c); |
| 5001 | |
| 5002 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 5003 | return 1; |
| 5004 | } |
| 5005 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5006 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5007 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5008 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5010 | case NamedExpr_kind: |
| 5011 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5012 | ADDOP(c, DUP_TOP); |
| 5013 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5014 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | case BoolOp_kind: |
| 5016 | return compiler_boolop(c, e); |
| 5017 | case BinOp_kind: |
| 5018 | VISIT(c, expr, e->v.BinOp.left); |
| 5019 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5020 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | break; |
| 5022 | case UnaryOp_kind: |
| 5023 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5024 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5025 | break; |
| 5026 | case Lambda_kind: |
| 5027 | return compiler_lambda(c, e); |
| 5028 | case IfExp_kind: |
| 5029 | return compiler_ifexp(c, e); |
| 5030 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5031 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5033 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5034 | case GeneratorExp_kind: |
| 5035 | return compiler_genexp(c, e); |
| 5036 | case ListComp_kind: |
| 5037 | return compiler_listcomp(c, e); |
| 5038 | case SetComp_kind: |
| 5039 | return compiler_setcomp(c, e); |
| 5040 | case DictComp_kind: |
| 5041 | return compiler_dictcomp(c, e); |
| 5042 | case Yield_kind: |
| 5043 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5044 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5045 | if (e->v.Yield.value) { |
| 5046 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5047 | } |
| 5048 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5049 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5050 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5051 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5052 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5053 | case YieldFrom_kind: |
| 5054 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5055 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5056 | |
| 5057 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5058 | return compiler_error(c, "'yield from' inside async function"); |
| 5059 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5060 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5061 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5062 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5063 | ADDOP(c, YIELD_FROM); |
| 5064 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5065 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5066 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5067 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5068 | return compiler_error(c, "'await' outside function"); |
| 5069 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5070 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5071 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5072 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5073 | return compiler_error(c, "'await' outside async function"); |
| 5074 | } |
| 5075 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5076 | |
| 5077 | VISIT(c, expr, e->v.Await.value); |
| 5078 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5079 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5080 | ADDOP(c, YIELD_FROM); |
| 5081 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | case Compare_kind: |
| 5083 | return compiler_compare(c, e); |
| 5084 | case Call_kind: |
| 5085 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5086 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5087 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5088 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5089 | case JoinedStr_kind: |
| 5090 | return compiler_joined_str(c, e); |
| 5091 | case FormattedValue_kind: |
| 5092 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | /* The following exprs can be assignment targets. */ |
| 5094 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5095 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5097 | case Load: |
| 5098 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5099 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5100 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5101 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5102 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5103 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5104 | break; |
| 5105 | case Del: |
| 5106 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5107 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5108 | } |
| 5109 | break; |
| 5110 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5111 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5112 | case Starred_kind: |
| 5113 | switch (e->v.Starred.ctx) { |
| 5114 | case Store: |
| 5115 | /* In all legitimate cases, the Starred node was already replaced |
| 5116 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5117 | return compiler_error(c, |
| 5118 | "starred assignment target must be in a list or tuple"); |
| 5119 | default: |
| 5120 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5121 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5122 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5123 | break; |
| 5124 | case Slice_kind: |
| 5125 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5126 | case Name_kind: |
| 5127 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5128 | /* child nodes of List and Tuple will have expr_context set */ |
| 5129 | case List_kind: |
| 5130 | return compiler_list(c, e); |
| 5131 | case Tuple_kind: |
| 5132 | return compiler_tuple(c, e); |
| 5133 | } |
| 5134 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5138 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5139 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5140 | int old_lineno = c->u->u_lineno; |
| 5141 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5142 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5143 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5144 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5145 | c->u->u_col_offset = old_col_offset; |
| 5146 | return res; |
| 5147 | } |
| 5148 | |
| 5149 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5150 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5151 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5152 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5153 | expr_ty e = s->v.AugAssign.target; |
| 5154 | |
| 5155 | int old_lineno = c->u->u_lineno; |
| 5156 | int old_col_offset = c->u->u_col_offset; |
| 5157 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5159 | switch (e->kind) { |
| 5160 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5161 | VISIT(c, expr, e->v.Attribute.value); |
| 5162 | ADDOP(c, DUP_TOP); |
| 5163 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5166 | VISIT(c, expr, e->v.Subscript.value); |
| 5167 | VISIT(c, expr, e->v.Subscript.slice); |
| 5168 | ADDOP(c, DUP_TOP_TWO); |
| 5169 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5170 | break; |
| 5171 | case Name_kind: |
| 5172 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5173 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5174 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5175 | default: |
| 5176 | PyErr_Format(PyExc_SystemError, |
| 5177 | "invalid node type (%d) for augmented assignment", |
| 5178 | e->kind); |
| 5179 | return 0; |
| 5180 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5181 | |
| 5182 | c->u->u_lineno = old_lineno; |
| 5183 | c->u->u_col_offset = old_col_offset; |
| 5184 | |
| 5185 | VISIT(c, expr, s->v.AugAssign.value); |
| 5186 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5187 | |
| 5188 | SET_LOC(c, e); |
| 5189 | |
| 5190 | switch (e->kind) { |
| 5191 | case Attribute_kind: |
| 5192 | ADDOP(c, ROT_TWO); |
| 5193 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5194 | break; |
| 5195 | case Subscript_kind: |
| 5196 | ADDOP(c, ROT_THREE); |
| 5197 | ADDOP(c, STORE_SUBSCR); |
| 5198 | break; |
| 5199 | case Name_kind: |
| 5200 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5201 | default: |
| 5202 | Py_UNREACHABLE(); |
| 5203 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
| 5207 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5208 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5209 | { |
| 5210 | VISIT(c, expr, e); |
| 5211 | ADDOP(c, POP_TOP); |
| 5212 | return 1; |
| 5213 | } |
| 5214 | |
| 5215 | static int |
| 5216 | check_annotation(struct compiler *c, stmt_ty s) |
| 5217 | { |
| 5218 | /* Annotations are only evaluated in a module or class. */ |
| 5219 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5220 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5221 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5222 | } |
| 5223 | return 1; |
| 5224 | } |
| 5225 | |
| 5226 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5227 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5228 | { |
| 5229 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5230 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5231 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5232 | 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] | 5233 | return 0; |
| 5234 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5235 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5236 | return 0; |
| 5237 | } |
| 5238 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5239 | return 0; |
| 5240 | } |
| 5241 | return 1; |
| 5242 | case Tuple_kind: { |
| 5243 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5244 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5245 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5246 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5247 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5248 | return 0; |
| 5249 | } |
| 5250 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5251 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5252 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5253 | default: |
| 5254 | return check_ann_expr(c, e); |
| 5255 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
| 5258 | static int |
| 5259 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5260 | { |
| 5261 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5262 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5263 | |
| 5264 | assert(s->kind == AnnAssign_kind); |
| 5265 | |
| 5266 | /* We perform the actual assignment first. */ |
| 5267 | if (s->v.AnnAssign.value) { |
| 5268 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5269 | VISIT(c, expr, targ); |
| 5270 | } |
| 5271 | switch (targ->kind) { |
| 5272 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5273 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5274 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5275 | /* If we have a simple name in a module or class, store annotation. */ |
| 5276 | if (s->v.AnnAssign.simple && |
| 5277 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5278 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5279 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5280 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5281 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5282 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5283 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5284 | } |
| 5285 | break; |
| 5286 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5287 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5288 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5289 | if (!s->v.AnnAssign.value && |
| 5290 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5291 | return 0; |
| 5292 | } |
| 5293 | break; |
| 5294 | case Subscript_kind: |
| 5295 | if (!s->v.AnnAssign.value && |
| 5296 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5297 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5298 | return 0; |
| 5299 | } |
| 5300 | break; |
| 5301 | default: |
| 5302 | PyErr_Format(PyExc_SystemError, |
| 5303 | "invalid node type (%d) for annotated assignment", |
| 5304 | targ->kind); |
| 5305 | return 0; |
| 5306 | } |
| 5307 | /* Annotation is evaluated last. */ |
| 5308 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5309 | return 0; |
| 5310 | } |
| 5311 | return 1; |
| 5312 | } |
| 5313 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5314 | /* Raises a SyntaxError and returns 0. |
| 5315 | If something goes wrong, a different exception may be raised. |
| 5316 | */ |
| 5317 | |
| 5318 | static int |
| 5319 | compiler_error(struct compiler *c, const char *errstr) |
| 5320 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5321 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5322 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5323 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5324 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5325 | if (!loc) { |
| 5326 | Py_INCREF(Py_None); |
| 5327 | loc = Py_None; |
| 5328 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5329 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5330 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5331 | if (!u) |
| 5332 | goto exit; |
| 5333 | v = Py_BuildValue("(zO)", errstr, u); |
| 5334 | if (!v) |
| 5335 | goto exit; |
| 5336 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5337 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5338 | Py_DECREF(loc); |
| 5339 | Py_XDECREF(u); |
| 5340 | Py_XDECREF(v); |
| 5341 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5342 | } |
| 5343 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5344 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5345 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5346 | and returns 0. |
| 5347 | */ |
| 5348 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5349 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5350 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5351 | va_list vargs; |
| 5352 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5353 | va_start(vargs, format); |
| 5354 | #else |
| 5355 | va_start(vargs); |
| 5356 | #endif |
| 5357 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5358 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5359 | if (msg == NULL) { |
| 5360 | return 0; |
| 5361 | } |
| 5362 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5363 | c->u->u_lineno, NULL, NULL) < 0) |
| 5364 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5365 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5366 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5367 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5368 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5369 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5370 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5371 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5372 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5373 | return 0; |
| 5374 | } |
| 5375 | Py_DECREF(msg); |
| 5376 | return 1; |
| 5377 | } |
| 5378 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5379 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5380 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5381 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5382 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5383 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5384 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5385 | if (ctx == Load) { |
| 5386 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5387 | return 0; |
| 5388 | } |
| 5389 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5390 | return 0; |
| 5391 | } |
| 5392 | } |
| 5393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5395 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5396 | case Store: op = STORE_SUBSCR; break; |
| 5397 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5399 | assert(op); |
| 5400 | VISIT(c, expr, e->v.Subscript.value); |
| 5401 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5402 | ADDOP(c, op); |
| 5403 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5407 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | int n = 2; |
| 5410 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5412 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5413 | if (s->v.Slice.lower) { |
| 5414 | VISIT(c, expr, s->v.Slice.lower); |
| 5415 | } |
| 5416 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5417 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5419 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | if (s->v.Slice.upper) { |
| 5421 | VISIT(c, expr, s->v.Slice.upper); |
| 5422 | } |
| 5423 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5424 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | if (s->v.Slice.step) { |
| 5428 | n++; |
| 5429 | VISIT(c, expr, s->v.Slice.step); |
| 5430 | } |
| 5431 | ADDOP_I(c, BUILD_SLICE, n); |
| 5432 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5433 | } |
| 5434 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5435 | /* End of the compiler section, beginning of the assembler section */ |
| 5436 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5437 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5438 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5439 | |
| 5440 | XXX must handle implicit jumps from one block to next |
| 5441 | */ |
| 5442 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5443 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5444 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5445 | int a_offset; /* offset into bytecode */ |
| 5446 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5447 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5448 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5449 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 5450 | int a_lineno; /* lineno of last emitted instruction */ |
| 5451 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5452 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5453 | }; |
| 5454 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5455 | Py_LOCAL_INLINE(void) |
| 5456 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5457 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5458 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5459 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5460 | assert(b->b_startdepth < 0); |
| 5461 | b->b_startdepth = depth; |
| 5462 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5463 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
| 5466 | /* Find the flow path that needs the largest stack. We assume that |
| 5467 | * cycles in the flow graph have no net effect on the stack depth. |
| 5468 | */ |
| 5469 | static int |
| 5470 | stackdepth(struct compiler *c) |
| 5471 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5472 | basicblock *b, *entryblock = NULL; |
| 5473 | basicblock **stack, **sp; |
| 5474 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5475 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5476 | b->b_startdepth = INT_MIN; |
| 5477 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5478 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | } |
| 5480 | if (!entryblock) |
| 5481 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5482 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5483 | if (!stack) { |
| 5484 | PyErr_NoMemory(); |
| 5485 | return -1; |
| 5486 | } |
| 5487 | |
| 5488 | sp = stack; |
| 5489 | stackdepth_push(&sp, entryblock, 0); |
| 5490 | while (sp != stack) { |
| 5491 | b = *--sp; |
| 5492 | int depth = b->b_startdepth; |
| 5493 | assert(depth >= 0); |
| 5494 | basicblock *next = b->b_next; |
| 5495 | for (int i = 0; i < b->b_iused; i++) { |
| 5496 | struct instr *instr = &b->b_instr[i]; |
| 5497 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5498 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 5499 | _Py_FatalErrorFormat(__func__, |
| 5500 | "opcode = %d", instr->i_opcode); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5501 | } |
| 5502 | int new_depth = depth + effect; |
| 5503 | if (new_depth > maxdepth) { |
| 5504 | maxdepth = new_depth; |
| 5505 | } |
| 5506 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5507 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5508 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5509 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5510 | int target_depth = depth + effect; |
| 5511 | if (target_depth > maxdepth) { |
| 5512 | maxdepth = target_depth; |
| 5513 | } |
| 5514 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5515 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5516 | } |
| 5517 | depth = new_depth; |
| 5518 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5519 | instr->i_opcode == JUMP_FORWARD || |
| 5520 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5521 | instr->i_opcode == RAISE_VARARGS || |
| 5522 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5523 | { |
| 5524 | /* remaining code is dead */ |
| 5525 | next = NULL; |
| 5526 | break; |
| 5527 | } |
| 5528 | } |
| 5529 | if (next != NULL) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 5530 | assert(b->b_nofallthrough == 0); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5531 | stackdepth_push(&sp, next, depth); |
| 5532 | } |
| 5533 | } |
| 5534 | PyObject_Free(stack); |
| 5535 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5536 | } |
| 5537 | |
| 5538 | static int |
| 5539 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5542 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5543 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5545 | if (a->a_bytecode == NULL) { |
| 5546 | goto error; |
| 5547 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5549 | if (a->a_lnotab == NULL) { |
| 5550 | goto error; |
| 5551 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5552 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5554 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5557 | error: |
| 5558 | Py_XDECREF(a->a_bytecode); |
| 5559 | Py_XDECREF(a->a_lnotab); |
| 5560 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
| 5563 | static void |
| 5564 | assemble_free(struct assembler *a) |
| 5565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | Py_XDECREF(a->a_bytecode); |
| 5567 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5568 | } |
| 5569 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5570 | static int |
| 5571 | blocksize(basicblock *b) |
| 5572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5573 | int i; |
| 5574 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5577 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5581 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5582 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5583 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5584 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | if (a->a_lnotab_off + 2 >= len) { |
| 5586 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5587 | return 0; |
| 5588 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5589 | unsigned char *lnotab = (unsigned char *) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5590 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5593 | *lnotab++ = bdelta; |
| 5594 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5596 | } |
| 5597 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5598 | /* Appends a range to the end of the line number table. See |
| 5599 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 5600 | |
| 5601 | static int |
| 5602 | assemble_line_range(struct assembler *a) |
| 5603 | { |
| 5604 | int ldelta, bdelta; |
| 5605 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 5606 | if (bdelta == 0) { |
| 5607 | return 1; |
| 5608 | } |
| 5609 | if (a->a_lineno < 0) { |
| 5610 | ldelta = -128; |
| 5611 | } |
| 5612 | else { |
| 5613 | ldelta = a->a_lineno - a->a_prevlineno; |
| 5614 | a->a_prevlineno = a->a_lineno; |
| 5615 | while (ldelta > 127) { |
| 5616 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 5617 | return 0; |
| 5618 | } |
| 5619 | ldelta -= 127; |
| 5620 | } |
| 5621 | while (ldelta < -127) { |
| 5622 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 5623 | return 0; |
| 5624 | } |
| 5625 | ldelta += 127; |
| 5626 | } |
| 5627 | } |
| 5628 | assert(-128 <= ldelta && ldelta < 128); |
| 5629 | while (bdelta > 254) { |
| 5630 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 5631 | return 0; |
| 5632 | } |
| 5633 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 5634 | bdelta -= 254; |
| 5635 | } |
| 5636 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 5637 | return 0; |
| 5638 | } |
| 5639 | a->a_lineno_start = a->a_offset; |
| 5640 | return 1; |
| 5641 | } |
| 5642 | |
| 5643 | static int |
| 5644 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 5645 | { |
| 5646 | if (i->i_lineno == a->a_lineno) { |
| 5647 | return 1; |
| 5648 | } |
| 5649 | if (!assemble_line_range(a)) { |
| 5650 | return 0; |
| 5651 | } |
| 5652 | a->a_lineno = i->i_lineno; |
| 5653 | return 1; |
| 5654 | } |
| 5655 | |
| 5656 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5657 | /* assemble_emit() |
| 5658 | Extend the bytecode with a new instruction. |
| 5659 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5660 | */ |
| 5661 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5662 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5663 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5664 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5665 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5666 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5667 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5668 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5669 | arg = i->i_oparg; |
| 5670 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5671 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5672 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5673 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | if (len > PY_SSIZE_T_MAX / 2) |
| 5675 | return 0; |
| 5676 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5677 | return 0; |
| 5678 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5679 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5681 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5682 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5683 | } |
| 5684 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5685 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5686 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5688 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5689 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | /* Compute the size of each block and fixup jump args. |
| 5693 | Replace block pointer with position in bytecode. */ |
| 5694 | do { |
| 5695 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5696 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 | bsize = blocksize(b); |
| 5698 | b->b_offset = totsize; |
| 5699 | totsize += bsize; |
| 5700 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5701 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5703 | bsize = b->b_offset; |
| 5704 | for (i = 0; i < b->b_iused; i++) { |
| 5705 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5706 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | /* Relative jumps are computed relative to |
| 5708 | the instruction pointer after fetching |
| 5709 | the jump instruction. |
| 5710 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5711 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5712 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5713 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5714 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5715 | instr->i_oparg -= bsize; |
| 5716 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5717 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5718 | if (instrsize(instr->i_oparg) != isize) { |
| 5719 | extended_arg_recompile = 1; |
| 5720 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5721 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5722 | } |
| 5723 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5725 | /* XXX: This is an awful hack that could hurt performance, but |
| 5726 | on the bright side it should work until we come up |
| 5727 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5729 | The issue is that in the first loop blocksize() is called |
| 5730 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5731 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5732 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5734 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5735 | The only EXTENDED_ARGs that could be popping up are |
| 5736 | ones in jump instructions. So this should converge |
| 5737 | fairly quickly. |
| 5738 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5739 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5740 | } |
| 5741 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5742 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5743 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5745 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5746 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5748 | tuple = PyTuple_New(size); |
| 5749 | if (tuple == NULL) |
| 5750 | return NULL; |
| 5751 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5752 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5753 | Py_INCREF(k); |
| 5754 | assert((i - offset) < size); |
| 5755 | assert((i - offset) >= 0); |
| 5756 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5757 | } |
| 5758 | return tuple; |
| 5759 | } |
| 5760 | |
| 5761 | static PyObject * |
| 5762 | consts_dict_keys_inorder(PyObject *dict) |
| 5763 | { |
| 5764 | PyObject *consts, *k, *v; |
| 5765 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5766 | |
| 5767 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5768 | if (consts == NULL) |
| 5769 | return NULL; |
| 5770 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5771 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5772 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5773 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5774 | * the object we want is always second. */ |
| 5775 | if (PyTuple_CheckExact(k)) { |
| 5776 | k = PyTuple_GET_ITEM(k, 1); |
| 5777 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5778 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5779 | assert(i < size); |
| 5780 | assert(i >= 0); |
| 5781 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5782 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5783 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5786 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5787 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5789 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5790 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5791 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5792 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 | if (ste->ste_nested) |
| 5794 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5795 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5796 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5797 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5798 | flags |= CO_COROUTINE; |
| 5799 | if (ste->ste_generator && ste->ste_coroutine) |
| 5800 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5801 | if (ste->ste_varargs) |
| 5802 | flags |= CO_VARARGS; |
| 5803 | if (ste->ste_varkeywords) |
| 5804 | flags |= CO_VARKEYWORDS; |
| 5805 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5807 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5808 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5809 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5810 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5811 | ste->ste_coroutine && |
| 5812 | !ste->ste_generator) { |
| 5813 | flags |= CO_COROUTINE; |
| 5814 | } |
| 5815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5816 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5819 | // Merge *tuple* with constant cache. |
| 5820 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5821 | static int |
| 5822 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5823 | { |
| 5824 | assert(PyTuple_CheckExact(*tuple)); |
| 5825 | |
| 5826 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5827 | if (key == NULL) { |
| 5828 | return 0; |
| 5829 | } |
| 5830 | |
| 5831 | // t is borrowed reference |
| 5832 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5833 | Py_DECREF(key); |
| 5834 | if (t == NULL) { |
| 5835 | return 0; |
| 5836 | } |
| 5837 | if (t == key) { // tuple is new constant. |
| 5838 | return 1; |
| 5839 | } |
| 5840 | |
| 5841 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5842 | Py_INCREF(u); |
| 5843 | Py_DECREF(*tuple); |
| 5844 | *tuple = u; |
| 5845 | return 1; |
| 5846 | } |
| 5847 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5848 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5849 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5850 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5851 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | PyObject *names = NULL; |
| 5853 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5854 | PyObject *name = NULL; |
| 5855 | PyObject *freevars = NULL; |
| 5856 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5857 | Py_ssize_t nlocals; |
| 5858 | int nlocals_int; |
| 5859 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5860 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5862 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5863 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5864 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5865 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5866 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5867 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5868 | if (!cellvars) |
| 5869 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5870 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5871 | if (!freevars) |
| 5872 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5873 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5874 | if (!merge_const_tuple(c, &names) || |
| 5875 | !merge_const_tuple(c, &varnames) || |
| 5876 | !merge_const_tuple(c, &cellvars) || |
| 5877 | !merge_const_tuple(c, &freevars)) |
| 5878 | { |
| 5879 | goto error; |
| 5880 | } |
| 5881 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5882 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5883 | assert(nlocals < INT_MAX); |
| 5884 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5886 | flags = compute_code_flags(c); |
| 5887 | if (flags < 0) |
| 5888 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5889 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5890 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5891 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5892 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5893 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5894 | if (!merge_const_tuple(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5895 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5896 | goto error; |
| 5897 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5898 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5899 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5900 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5901 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5902 | maxdepth = stackdepth(c); |
| 5903 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5904 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5905 | goto error; |
| 5906 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5907 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5908 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5909 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5910 | varnames, freevars, cellvars, c->c_filename, |
| 5911 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5912 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5913 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5914 | Py_XDECREF(names); |
| 5915 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5916 | Py_XDECREF(name); |
| 5917 | Py_XDECREF(freevars); |
| 5918 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5920 | } |
| 5921 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5922 | |
| 5923 | /* For debugging purposes only */ |
| 5924 | #if 0 |
| 5925 | static void |
| 5926 | dump_instr(const struct instr *i) |
| 5927 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5928 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 5929 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5930 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5932 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5933 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5934 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5935 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5937 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5938 | } |
| 5939 | |
| 5940 | static void |
| 5941 | dump_basicblock(const basicblock *b) |
| 5942 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5943 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5944 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 5945 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5946 | if (b->b_instr) { |
| 5947 | int i; |
| 5948 | for (i = 0; i < b->b_iused; i++) { |
| 5949 | fprintf(stderr, " [%02d] ", i); |
| 5950 | dump_instr(b->b_instr + i); |
| 5951 | } |
| 5952 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5953 | } |
| 5954 | #endif |
| 5955 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5956 | |
| 5957 | static int |
| 5958 | normalize_basic_block(basicblock *bb); |
| 5959 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5960 | static int |
| 5961 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 5962 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5963 | static int |
| 5964 | ensure_exits_have_lineno(struct compiler *c); |
| 5965 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5966 | static PyCodeObject * |
| 5967 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | basicblock *b, *entryblock; |
| 5970 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5971 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5972 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5973 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5975 | /* Make sure every block that falls off the end returns None. |
| 5976 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5977 | block ends with a jump or return b_next shouldn't set. |
| 5978 | */ |
| 5979 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5980 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5981 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5982 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5983 | ADDOP(c, RETURN_VALUE); |
| 5984 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5985 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 5986 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5987 | if (normalize_basic_block(b)) { |
| 5988 | goto error; |
| 5989 | } |
| 5990 | } |
| 5991 | |
| 5992 | if (ensure_exits_have_lineno(c)) { |
| 5993 | goto error; |
| 5994 | } |
| 5995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5996 | nblocks = 0; |
| 5997 | entryblock = NULL; |
| 5998 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5999 | nblocks++; |
| 6000 | entryblock = b; |
| 6001 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6003 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6004 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 6005 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6006 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6007 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6008 | c->u->u_firstlineno = 1; |
| 6009 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6010 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6011 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6012 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6013 | a.a_entry = entryblock; |
| 6014 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6015 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6016 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6017 | if (consts == NULL) { |
| 6018 | goto error; |
| 6019 | } |
| 6020 | if (optimize_cfg(&a, consts)) { |
| 6021 | goto error; |
| 6022 | } |
| 6023 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6024 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6025 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6026 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6027 | /* Emit code. */ |
| 6028 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6029 | for (j = 0; j < b->b_iused; j++) |
| 6030 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6031 | goto error; |
| 6032 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6033 | if (!assemble_line_range(&a)) { |
| 6034 | return 0; |
| 6035 | } |
| 6036 | /* Emit sentinel at end of line number table */ |
| 6037 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6038 | goto error; |
| 6039 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6041 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6042 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6043 | 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] | 6044 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6045 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6046 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6047 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6048 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6049 | assemble_free(&a); |
| 6050 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6051 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6052 | |
| 6053 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6054 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6055 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6056 | PyArena *arena) |
| 6057 | { |
| 6058 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6059 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6060 | |
| 6061 | |
| 6062 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6063 | with LOAD_CONST (c1, c2, ... cn). |
| 6064 | The consts table must still be in list form so that the |
| 6065 | new constant (c1, c2, ... cn) can be appended. |
| 6066 | Called with codestr pointing to the first LOAD_CONST. |
| 6067 | */ |
| 6068 | static int |
| 6069 | fold_tuple_on_constants(struct instr *inst, |
| 6070 | int n, PyObject *consts) |
| 6071 | { |
| 6072 | /* Pre-conditions */ |
| 6073 | assert(PyList_CheckExact(consts)); |
| 6074 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6075 | assert(inst[n].i_oparg == n); |
| 6076 | |
| 6077 | for (int i = 0; i < n; i++) { |
| 6078 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6079 | return 0; |
| 6080 | } |
| 6081 | } |
| 6082 | |
| 6083 | /* Buildup new tuple of constants */ |
| 6084 | PyObject *newconst = PyTuple_New(n); |
| 6085 | if (newconst == NULL) { |
| 6086 | return -1; |
| 6087 | } |
| 6088 | for (int i = 0; i < n; i++) { |
| 6089 | int arg = inst[i].i_oparg; |
| 6090 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6091 | Py_INCREF(constant); |
| 6092 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6093 | } |
| 6094 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6095 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6096 | Py_DECREF(newconst); |
| 6097 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6098 | return -1; |
| 6099 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6100 | if (PyList_Append(consts, newconst)) { |
| 6101 | Py_DECREF(newconst); |
| 6102 | return -1; |
| 6103 | } |
| 6104 | Py_DECREF(newconst); |
| 6105 | for (int i = 0; i < n; i++) { |
| 6106 | inst[i].i_opcode = NOP; |
| 6107 | } |
| 6108 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6109 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6110 | return 0; |
| 6111 | } |
| 6112 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6113 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6114 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6115 | |
| 6116 | /* Optimization */ |
| 6117 | static int |
| 6118 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6119 | { |
| 6120 | assert(PyList_CheckExact(consts)); |
| 6121 | struct instr nop; |
| 6122 | nop.i_opcode = NOP; |
| 6123 | struct instr *target; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6124 | for (int i = 0; i < bb->b_iused; i++) { |
| 6125 | struct instr *inst = &bb->b_instr[i]; |
| 6126 | int oparg = inst->i_oparg; |
| 6127 | 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] | 6128 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6129 | /* Skip over empty basic blocks. */ |
| 6130 | while (inst->i_target->b_iused == 0) { |
| 6131 | inst->i_target = inst->i_target->b_next; |
| 6132 | } |
| 6133 | target = &inst->i_target->b_instr[0]; |
| 6134 | } |
| 6135 | else { |
| 6136 | target = &nop; |
| 6137 | } |
| 6138 | switch (inst->i_opcode) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6139 | /* Remove LOAD_CONST const; conditional jump */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6140 | case LOAD_CONST: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6141 | { |
| 6142 | PyObject* cnt; |
| 6143 | int is_true; |
| 6144 | int jump_if_true; |
| 6145 | switch(nextop) { |
| 6146 | case POP_JUMP_IF_FALSE: |
| 6147 | case POP_JUMP_IF_TRUE: |
| 6148 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6149 | is_true = PyObject_IsTrue(cnt); |
| 6150 | if (is_true == -1) { |
| 6151 | goto error; |
| 6152 | } |
| 6153 | inst->i_opcode = NOP; |
| 6154 | jump_if_true = nextop == POP_JUMP_IF_TRUE; |
| 6155 | if (is_true == jump_if_true) { |
| 6156 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6157 | bb->b_nofallthrough = 1; |
| 6158 | } |
| 6159 | else { |
| 6160 | bb->b_instr[i+1].i_opcode = NOP; |
| 6161 | } |
| 6162 | break; |
| 6163 | case JUMP_IF_FALSE_OR_POP: |
| 6164 | case JUMP_IF_TRUE_OR_POP: |
| 6165 | cnt = PyList_GET_ITEM(consts, oparg); |
| 6166 | is_true = PyObject_IsTrue(cnt); |
| 6167 | if (is_true == -1) { |
| 6168 | goto error; |
| 6169 | } |
| 6170 | jump_if_true = nextop == JUMP_IF_TRUE_OR_POP; |
| 6171 | if (is_true == jump_if_true) { |
| 6172 | bb->b_instr[i+1].i_opcode = JUMP_ABSOLUTE; |
| 6173 | bb->b_nofallthrough = 1; |
| 6174 | } |
| 6175 | else { |
| 6176 | inst->i_opcode = NOP; |
| 6177 | bb->b_instr[i+1].i_opcode = NOP; |
| 6178 | } |
| 6179 | break; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6180 | } |
| 6181 | break; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6182 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6183 | |
| 6184 | /* Try to fold tuples of constants. |
| 6185 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6186 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6187 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6188 | case BUILD_TUPLE: |
| 6189 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6190 | switch(oparg) { |
| 6191 | case 1: |
| 6192 | inst->i_opcode = NOP; |
| 6193 | bb->b_instr[i+1].i_opcode = NOP; |
| 6194 | break; |
| 6195 | case 2: |
| 6196 | inst->i_opcode = ROT_TWO; |
| 6197 | bb->b_instr[i+1].i_opcode = NOP; |
| 6198 | break; |
| 6199 | case 3: |
| 6200 | inst->i_opcode = ROT_THREE; |
| 6201 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6202 | } |
| 6203 | break; |
| 6204 | } |
| 6205 | if (i >= oparg) { |
| 6206 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6207 | goto error; |
| 6208 | } |
| 6209 | } |
| 6210 | break; |
| 6211 | |
| 6212 | /* Simplify conditional jump to conditional jump where the |
| 6213 | result of the first test implies the success of a similar |
| 6214 | test or the failure of the opposite test. |
| 6215 | Arises in code like: |
| 6216 | "a and b or c" |
| 6217 | "(a and b) and c" |
| 6218 | "(a or b) or c" |
| 6219 | "(a or b) and c" |
| 6220 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6221 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6222 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6223 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6224 | where y+1 is the instruction following the second test. |
| 6225 | */ |
| 6226 | case JUMP_IF_FALSE_OR_POP: |
| 6227 | switch(target->i_opcode) { |
| 6228 | case POP_JUMP_IF_FALSE: |
| 6229 | *inst = *target; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6230 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6231 | break; |
| 6232 | case JUMP_ABSOLUTE: |
| 6233 | case JUMP_FORWARD: |
| 6234 | case JUMP_IF_FALSE_OR_POP: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6235 | if (inst->i_target != target->i_target) { |
| 6236 | inst->i_target = target->i_target; |
| 6237 | --i; |
| 6238 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6239 | break; |
| 6240 | case JUMP_IF_TRUE_OR_POP: |
| 6241 | assert (inst->i_target->b_iused == 1); |
| 6242 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6243 | inst->i_target = inst->i_target->b_next; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6244 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6245 | break; |
| 6246 | } |
| 6247 | break; |
| 6248 | |
| 6249 | case JUMP_IF_TRUE_OR_POP: |
| 6250 | switch(target->i_opcode) { |
| 6251 | case POP_JUMP_IF_TRUE: |
| 6252 | *inst = *target; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6253 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6254 | break; |
| 6255 | case JUMP_ABSOLUTE: |
| 6256 | case JUMP_FORWARD: |
| 6257 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6258 | if (inst->i_target != target->i_target) { |
| 6259 | inst->i_target = target->i_target; |
| 6260 | --i; |
| 6261 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6262 | break; |
| 6263 | case JUMP_IF_FALSE_OR_POP: |
| 6264 | assert (inst->i_target->b_iused == 1); |
| 6265 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 6266 | inst->i_target = inst->i_target->b_next; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6267 | --i; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6268 | break; |
| 6269 | } |
| 6270 | break; |
| 6271 | |
| 6272 | case POP_JUMP_IF_FALSE: |
| 6273 | switch(target->i_opcode) { |
| 6274 | case JUMP_ABSOLUTE: |
| 6275 | case JUMP_FORWARD: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6276 | if (inst->i_target != target->i_target) { |
| 6277 | inst->i_target = target->i_target; |
| 6278 | --i; |
| 6279 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6280 | break; |
| 6281 | } |
| 6282 | break; |
| 6283 | |
| 6284 | case POP_JUMP_IF_TRUE: |
| 6285 | switch(target->i_opcode) { |
| 6286 | case JUMP_ABSOLUTE: |
| 6287 | case JUMP_FORWARD: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6288 | if (inst->i_target != target->i_target) { |
| 6289 | inst->i_target = target->i_target; |
| 6290 | --i; |
| 6291 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6292 | break; |
| 6293 | } |
| 6294 | break; |
| 6295 | |
| 6296 | case JUMP_ABSOLUTE: |
| 6297 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6298 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6299 | switch(target->i_opcode) { |
| 6300 | case JUMP_FORWARD: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6301 | if (inst->i_target != target->i_target) { |
| 6302 | inst->i_target = target->i_target; |
| 6303 | --i; |
| 6304 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6305 | break; |
| 6306 | case JUMP_ABSOLUTE: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6307 | if (inst->i_target != target->i_target) { |
| 6308 | inst->i_target = target->i_target; |
| 6309 | inst->i_opcode = target->i_opcode; |
| 6310 | --i; |
| 6311 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6312 | break; |
| 6313 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6314 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 6315 | basicblock *to_copy = inst->i_target; |
| 6316 | *inst = to_copy->b_instr[0]; |
| 6317 | for (i = 1; i < to_copy->b_iused; i++) { |
| 6318 | int index = compiler_next_instr(bb); |
| 6319 | if (index < 0) { |
| 6320 | return -1; |
| 6321 | } |
| 6322 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 6323 | } |
| 6324 | bb->b_exit = 1; |
| 6325 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6326 | break; |
| 6327 | } |
| 6328 | } |
| 6329 | return 0; |
| 6330 | error: |
| 6331 | return -1; |
| 6332 | } |
| 6333 | |
| 6334 | |
| 6335 | static void |
| 6336 | clean_basic_block(basicblock *bb) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6337 | /* Remove NOPs. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6338 | int dest = 0; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6339 | int prev_lineno = -1; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6340 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6341 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6342 | if (bb->b_instr[src].i_opcode == NOP) { |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6343 | /* Eliminate no-op if it doesn't have a line number */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6344 | if (lineno < 0) { |
| 6345 | continue; |
| 6346 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6347 | /* or, if the previous instruction had the same line number. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6348 | if (prev_lineno == lineno) { |
| 6349 | continue; |
| 6350 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6351 | /* 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] | 6352 | if (src < bb->b_iused - 1) { |
| 6353 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 6354 | if (next_lineno < 0 || next_lineno == lineno) { |
| 6355 | bb->b_instr[src+1].i_lineno = lineno; |
| 6356 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6357 | } |
| 6358 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6359 | else { |
| 6360 | basicblock* next = bb->b_next; |
| 6361 | while (next && next->b_iused == 0) { |
| 6362 | next = next->b_next; |
| 6363 | } |
| 6364 | /* or if last instruction in BB and next BB has same line number */ |
| 6365 | if (next) { |
| 6366 | if (lineno == next->b_instr[0].i_lineno) { |
| 6367 | continue; |
| 6368 | } |
| 6369 | } |
| 6370 | } |
| 6371 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6372 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6373 | if (dest != src) { |
| 6374 | bb->b_instr[dest] = bb->b_instr[src]; |
| 6375 | } |
| 6376 | dest++; |
| 6377 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6378 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6379 | assert(dest <= bb->b_iused); |
| 6380 | bb->b_iused = dest; |
| 6381 | } |
| 6382 | |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6383 | static int |
| 6384 | normalize_basic_block(basicblock *bb) { |
| 6385 | /* Mark blocks as exit and/or nofallthrough. |
| 6386 | Raise SystemError if CFG is malformed. */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6387 | for (int i = 0; i < bb->b_iused; i++) { |
| 6388 | switch(bb->b_instr[i].i_opcode) { |
| 6389 | case RETURN_VALUE: |
| 6390 | case RAISE_VARARGS: |
| 6391 | case RERAISE: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6392 | bb->b_exit = 1; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6393 | bb->b_nofallthrough = 1; |
| 6394 | break; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6395 | case JUMP_ABSOLUTE: |
| 6396 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6397 | bb->b_nofallthrough = 1; |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6398 | /* fall through */ |
| 6399 | case POP_JUMP_IF_FALSE: |
| 6400 | case POP_JUMP_IF_TRUE: |
| 6401 | case JUMP_IF_FALSE_OR_POP: |
| 6402 | case JUMP_IF_TRUE_OR_POP: |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6403 | case FOR_ITER: |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6404 | if (i != bb->b_iused-1) { |
| 6405 | PyErr_SetString(PyExc_SystemError, "malformed control flow graph."); |
| 6406 | return -1; |
| 6407 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6408 | /* Skip over empty basic blocks. */ |
| 6409 | while (bb->b_instr[i].i_target->b_iused == 0) { |
| 6410 | bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next; |
| 6411 | } |
| 6412 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6413 | } |
| 6414 | } |
Mark Shannon | 266b462 | 2020-11-17 19:30:14 +0000 | [diff] [blame] | 6415 | return 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6416 | } |
| 6417 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6418 | static int |
| 6419 | mark_reachable(struct assembler *a) { |
| 6420 | basicblock **stack, **sp; |
| 6421 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 6422 | if (stack == NULL) { |
| 6423 | return -1; |
| 6424 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6425 | a->a_entry->b_reachable = 1; |
| 6426 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6427 | while (sp > stack) { |
| 6428 | basicblock *b = *(--sp); |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6429 | 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] | 6430 | b->b_next->b_reachable = 1; |
| 6431 | *sp++ = b->b_next; |
| 6432 | } |
| 6433 | for (int i = 0; i < b->b_iused; i++) { |
| 6434 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6435 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6436 | target = b->b_instr[i].i_target; |
| 6437 | if (target->b_reachable == 0) { |
| 6438 | target->b_reachable = 1; |
| 6439 | *sp++ = target; |
| 6440 | } |
| 6441 | } |
| 6442 | } |
| 6443 | } |
| 6444 | PyObject_Free(stack); |
| 6445 | return 0; |
| 6446 | } |
| 6447 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6448 | /* If an instruction has no line number, but it's predecessor in the BB does, |
| 6449 | * then copy the line number. This reduces the size of the line number table, |
| 6450 | * but has no impact on the generated line number events. |
| 6451 | */ |
| 6452 | static void |
| 6453 | minimize_lineno_table(struct assembler *a) { |
| 6454 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6455 | int prev_lineno = -1; |
| 6456 | for (int i = 0; i < b->b_iused; i++) { |
| 6457 | if (b->b_instr[i].i_lineno < 0) { |
| 6458 | b->b_instr[i].i_lineno = prev_lineno; |
| 6459 | } |
| 6460 | else { |
| 6461 | prev_lineno = b->b_instr[i].i_lineno; |
| 6462 | } |
| 6463 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6464 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6465 | } |
| 6466 | } |
| 6467 | |
| 6468 | /* Perform optimizations on a control flow graph. |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6469 | The consts object should still be in list form to allow new constants |
| 6470 | to be appended. |
| 6471 | |
| 6472 | All transformations keep the code size the same or smaller. |
| 6473 | For those that reduce size, the gaps are initially filled with |
| 6474 | NOPs. Later those NOPs are removed. |
| 6475 | */ |
| 6476 | |
| 6477 | static int |
| 6478 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 6479 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6480 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6481 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6482 | return -1; |
| 6483 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6484 | clean_basic_block(b); |
| 6485 | assert(b->b_reachable == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6486 | } |
| 6487 | if (mark_reachable(a)) { |
| 6488 | return -1; |
| 6489 | } |
| 6490 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6491 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6492 | if (b->b_reachable == 0) { |
| 6493 | b->b_iused = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6494 | } |
| 6495 | } |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6496 | minimize_lineno_table(a); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6497 | return 0; |
| 6498 | } |
| 6499 | |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6500 | static inline int |
| 6501 | is_exit_without_lineno(basicblock *b) { |
| 6502 | return b->b_exit && b->b_instr[0].i_lineno < 0; |
| 6503 | } |
| 6504 | |
| 6505 | /* PEP 626 mandates that the f_lineno of a frame is correct |
| 6506 | * after a frame terminates. It would be prohibitively expensive |
| 6507 | * to continuously update the f_lineno field at runtime, |
| 6508 | * so we make sure that all exiting instruction (raises and returns) |
| 6509 | * have a valid line number, allowing us to compute f_lineno lazily. |
| 6510 | * We can do this by duplicating the exit blocks without line number |
| 6511 | * so that none have more than one predecessor. We can then safely |
| 6512 | * copy the line number from the sole predecessor block. |
| 6513 | */ |
| 6514 | static int |
| 6515 | ensure_exits_have_lineno(struct compiler *c) |
| 6516 | { |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 6517 | basicblock *entry = NULL; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6518 | /* Copy all exit blocks without line number that are targets of a jump. |
| 6519 | */ |
| 6520 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6521 | if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) { |
| 6522 | switch (b->b_instr[b->b_iused-1].i_opcode) { |
| 6523 | /* Note: Only actual jumps, not exception handlers */ |
| 6524 | case SETUP_ASYNC_WITH: |
| 6525 | case SETUP_WITH: |
| 6526 | case SETUP_FINALLY: |
| 6527 | continue; |
| 6528 | } |
| 6529 | basicblock *target = b->b_instr[b->b_iused-1].i_target; |
| 6530 | if (is_exit_without_lineno(target)) { |
| 6531 | basicblock *new_target = compiler_copy_block(c, target); |
| 6532 | if (new_target == NULL) { |
| 6533 | return -1; |
| 6534 | } |
| 6535 | new_target->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 6536 | b->b_instr[b->b_iused-1].i_target = new_target; |
| 6537 | } |
| 6538 | } |
Mark Shannon | eaccc12 | 2020-12-04 15:22:12 +0000 | [diff] [blame] | 6539 | entry = b; |
| 6540 | } |
| 6541 | assert(entry != NULL); |
| 6542 | if (is_exit_without_lineno(entry)) { |
| 6543 | entry->b_instr[0].i_lineno = c->u->u_firstlineno; |
Mark Shannon | 5977a79 | 2020-12-02 13:31:40 +0000 | [diff] [blame] | 6544 | } |
| 6545 | /* Any remaining reachable exit blocks without line number can only be reached by |
| 6546 | * fall through, and thus can only have a single predecessor */ |
| 6547 | for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6548 | if (!b->b_nofallthrough && b->b_next && b->b_iused > 0) { |
| 6549 | if (is_exit_without_lineno(b->b_next)) { |
| 6550 | assert(b->b_next->b_iused > 0); |
| 6551 | b->b_next->b_instr[0].i_lineno = b->b_instr[b->b_iused-1].i_lineno; |
| 6552 | } |
| 6553 | } |
| 6554 | } |
| 6555 | return 0; |
| 6556 | } |
| 6557 | |
| 6558 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6559 | /* Retained for API compatibility. |
| 6560 | * Optimization is now done in optimize_cfg */ |
| 6561 | |
| 6562 | PyObject * |
| 6563 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 6564 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 6565 | { |
| 6566 | Py_INCREF(code); |
| 6567 | return code; |
| 6568 | } |
| 6569 | |