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 | |
| 815 | /* Returns the offset of the next instruction in the current block's |
| 816 | b_instr array. Resizes the b_instr as necessary. |
| 817 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 818 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 819 | |
| 820 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 821 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 823 | assert(b != NULL); |
| 824 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 825 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 826 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | if (b->b_instr == NULL) { |
| 828 | PyErr_NoMemory(); |
| 829 | return -1; |
| 830 | } |
| 831 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | } |
| 833 | else if (b->b_iused == b->b_ialloc) { |
| 834 | struct instr *tmp; |
| 835 | size_t oldsize, newsize; |
| 836 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 837 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 838 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 839 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 840 | PyErr_NoMemory(); |
| 841 | return -1; |
| 842 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 844 | if (newsize == 0) { |
| 845 | PyErr_NoMemory(); |
| 846 | return -1; |
| 847 | } |
| 848 | b->b_ialloc <<= 1; |
| 849 | tmp = (struct instr *)PyObject_Realloc( |
| 850 | (void *)b->b_instr, newsize); |
| 851 | if (tmp == NULL) { |
| 852 | PyErr_NoMemory(); |
| 853 | return -1; |
| 854 | } |
| 855 | b->b_instr = tmp; |
| 856 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 857 | } |
| 858 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 861 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 862 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 863 | The line number is reset in the following cases: |
| 864 | - when entering a new scope |
| 865 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 866 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 867 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 868 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 869 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 870 | #define SET_LOC(c, x) \ |
| 871 | (c)->u->u_lineno = (x)->lineno; \ |
| 872 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 874 | /* Return the stack effect of opcode with argument oparg. |
| 875 | |
| 876 | Some opcodes have different stack effect when jump to the target and |
| 877 | when not jump. The 'jump' parameter specifies the case: |
| 878 | |
| 879 | * 0 -- when not jump |
| 880 | * 1 -- when jump |
| 881 | * -1 -- maximal |
| 882 | */ |
| 883 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 884 | WITH_CLEANUP_FINISH deterministic. */ |
| 885 | static int |
| 886 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 889 | case NOP: |
| 890 | case EXTENDED_ARG: |
| 891 | return 0; |
| 892 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 893 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | case POP_TOP: |
| 895 | return -1; |
| 896 | case ROT_TWO: |
| 897 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 898 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | return 0; |
| 900 | case DUP_TOP: |
| 901 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 902 | case DUP_TOP_TWO: |
| 903 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 904 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 905 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | case UNARY_POSITIVE: |
| 907 | case UNARY_NEGATIVE: |
| 908 | case UNARY_NOT: |
| 909 | case UNARY_INVERT: |
| 910 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 912 | case SET_ADD: |
| 913 | case LIST_APPEND: |
| 914 | return -1; |
| 915 | case MAP_ADD: |
| 916 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 917 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 918 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | case BINARY_POWER: |
| 920 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 921 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | case BINARY_MODULO: |
| 923 | case BINARY_ADD: |
| 924 | case BINARY_SUBTRACT: |
| 925 | case BINARY_SUBSCR: |
| 926 | case BINARY_FLOOR_DIVIDE: |
| 927 | case BINARY_TRUE_DIVIDE: |
| 928 | return -1; |
| 929 | case INPLACE_FLOOR_DIVIDE: |
| 930 | case INPLACE_TRUE_DIVIDE: |
| 931 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | case INPLACE_ADD: |
| 934 | case INPLACE_SUBTRACT: |
| 935 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 936 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 937 | case INPLACE_MODULO: |
| 938 | return -1; |
| 939 | case STORE_SUBSCR: |
| 940 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | case DELETE_SUBSCR: |
| 942 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | case BINARY_LSHIFT: |
| 945 | case BINARY_RSHIFT: |
| 946 | case BINARY_AND: |
| 947 | case BINARY_XOR: |
| 948 | case BINARY_OR: |
| 949 | return -1; |
| 950 | case INPLACE_POWER: |
| 951 | return -1; |
| 952 | case GET_ITER: |
| 953 | return 0; |
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 PRINT_EXPR: |
| 956 | return -1; |
| 957 | case LOAD_BUILD_CLASS: |
| 958 | return 1; |
| 959 | case INPLACE_LSHIFT: |
| 960 | case INPLACE_RSHIFT: |
| 961 | case INPLACE_AND: |
| 962 | case INPLACE_XOR: |
| 963 | case INPLACE_OR: |
| 964 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 967 | /* 1 in the normal flow. |
| 968 | * Restore the stack position and push 6 values before jumping to |
| 969 | * the handler if an exception be raised. */ |
| 970 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | case RETURN_VALUE: |
| 972 | return -1; |
| 973 | case IMPORT_STAR: |
| 974 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 975 | case SETUP_ANNOTATIONS: |
| 976 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | case YIELD_VALUE: |
| 978 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 979 | case YIELD_FROM: |
| 980 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | case POP_BLOCK: |
| 982 | return 0; |
| 983 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 984 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | case STORE_NAME: |
| 987 | return -1; |
| 988 | case DELETE_NAME: |
| 989 | return 0; |
| 990 | case UNPACK_SEQUENCE: |
| 991 | return oparg-1; |
| 992 | case UNPACK_EX: |
| 993 | return (oparg&0xFF) + (oparg>>8); |
| 994 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 995 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 996 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | case STORE_ATTR: |
| 999 | return -2; |
| 1000 | case DELETE_ATTR: |
| 1001 | return -1; |
| 1002 | case STORE_GLOBAL: |
| 1003 | return -1; |
| 1004 | case DELETE_GLOBAL: |
| 1005 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | case LOAD_CONST: |
| 1007 | return 1; |
| 1008 | case LOAD_NAME: |
| 1009 | return 1; |
| 1010 | case BUILD_TUPLE: |
| 1011 | case BUILD_LIST: |
| 1012 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1013 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | return 1-oparg; |
| 1015 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1016 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1017 | case BUILD_CONST_KEY_MAP: |
| 1018 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | case LOAD_ATTR: |
| 1020 | return 0; |
| 1021 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1022 | case IS_OP: |
| 1023 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1025 | case JUMP_IF_NOT_EXC_MATCH: |
| 1026 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case IMPORT_NAME: |
| 1028 | return -1; |
| 1029 | case IMPORT_FROM: |
| 1030 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1031 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1032 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | case JUMP_ABSOLUTE: |
| 1035 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1036 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1037 | case JUMP_IF_TRUE_OR_POP: |
| 1038 | case JUMP_IF_FALSE_OR_POP: |
| 1039 | return jump ? 0 : -1; |
| 1040 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | case POP_JUMP_IF_FALSE: |
| 1042 | case POP_JUMP_IF_TRUE: |
| 1043 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | case LOAD_GLOBAL: |
| 1046 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1047 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1048 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1050 | /* 0 in the normal flow. |
| 1051 | * Restore the stack position and push 6 values before jumping to |
| 1052 | * the handler if an exception be raised. */ |
| 1053 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1054 | case RERAISE: |
| 1055 | return -3; |
| 1056 | |
| 1057 | case WITH_EXCEPT_START: |
| 1058 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | case LOAD_FAST: |
| 1061 | return 1; |
| 1062 | case STORE_FAST: |
| 1063 | return -1; |
| 1064 | case DELETE_FAST: |
| 1065 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | case RAISE_VARARGS: |
| 1068 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1072 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1073 | case CALL_METHOD: |
| 1074 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1076 | return -oparg-1; |
| 1077 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1078 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1079 | case MAKE_FUNCTION: |
| 1080 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1081 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | case BUILD_SLICE: |
| 1083 | if (oparg == 3) |
| 1084 | return -2; |
| 1085 | else |
| 1086 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1087 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1088 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | case LOAD_CLOSURE: |
| 1090 | return 1; |
| 1091 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1092 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | return 1; |
| 1094 | case STORE_DEREF: |
| 1095 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1096 | case DELETE_DEREF: |
| 1097 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1098 | |
| 1099 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1100 | case GET_AWAITABLE: |
| 1101 | return 0; |
| 1102 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1103 | /* 0 in the normal flow. |
| 1104 | * Restore the stack position to the position before the result |
| 1105 | * of __aenter__ and push 6 values before jumping to the handler |
| 1106 | * if an exception be raised. */ |
| 1107 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1108 | case BEFORE_ASYNC_WITH: |
| 1109 | return 1; |
| 1110 | case GET_AITER: |
| 1111 | return 0; |
| 1112 | case GET_ANEXT: |
| 1113 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1114 | case GET_YIELD_FROM_ITER: |
| 1115 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1116 | case END_ASYNC_FOR: |
| 1117 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1118 | case FORMAT_VALUE: |
| 1119 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1120 | else 1->1. */ |
| 1121 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1122 | case LOAD_METHOD: |
| 1123 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1124 | case LOAD_ASSERTION_ERROR: |
| 1125 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1126 | case LIST_TO_TUPLE: |
| 1127 | return 0; |
| 1128 | case LIST_EXTEND: |
| 1129 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1130 | case DICT_MERGE: |
| 1131 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1132 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1134 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1136 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1139 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1140 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1141 | { |
| 1142 | return stack_effect(opcode, oparg, jump); |
| 1143 | } |
| 1144 | |
| 1145 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1146 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1147 | { |
| 1148 | return stack_effect(opcode, oparg, -1); |
| 1149 | } |
| 1150 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1151 | /* Add an opcode with no argument. |
| 1152 | Returns 0 on failure, 1 on success. |
| 1153 | */ |
| 1154 | |
| 1155 | static int |
| 1156 | compiler_addop(struct compiler *c, int opcode) |
| 1157 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | basicblock *b; |
| 1159 | struct instr *i; |
| 1160 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1161 | assert(!HAS_ARG(opcode)); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1162 | if (c->c_do_not_emit_bytecode) { |
| 1163 | return 1; |
| 1164 | } |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1165 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1166 | if (off < 0) |
| 1167 | return 0; |
| 1168 | b = c->u->u_curblock; |
| 1169 | i = &b->b_instr[off]; |
| 1170 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1171 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | if (opcode == RETURN_VALUE) |
| 1173 | b->b_return = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1174 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1178 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1179 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1180 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1181 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1183 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1184 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1186 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1188 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1189 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1190 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1192 | return -1; |
| 1193 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1194 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1195 | Py_DECREF(v); |
| 1196 | return -1; |
| 1197 | } |
| 1198 | Py_DECREF(v); |
| 1199 | } |
| 1200 | else |
| 1201 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1202 | return arg; |
| 1203 | } |
| 1204 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1205 | // Merge const *o* recursively and return constant key object. |
| 1206 | static PyObject* |
| 1207 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1208 | { |
| 1209 | // None and Ellipsis are singleton, and key is the singleton. |
| 1210 | // No need to merge object and key. |
| 1211 | if (o == Py_None || o == Py_Ellipsis) { |
| 1212 | Py_INCREF(o); |
| 1213 | return o; |
| 1214 | } |
| 1215 | |
| 1216 | PyObject *key = _PyCode_ConstantKey(o); |
| 1217 | if (key == NULL) { |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
| 1221 | // t is borrowed reference |
| 1222 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1223 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1224 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1225 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1226 | Py_DECREF(key); |
| 1227 | return t; |
| 1228 | } |
| 1229 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1230 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1231 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1232 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1233 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1234 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1235 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1236 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1237 | PyObject *u = merge_consts_recursive(c, item); |
| 1238 | if (u == NULL) { |
| 1239 | Py_DECREF(key); |
| 1240 | return NULL; |
| 1241 | } |
| 1242 | |
| 1243 | // See _PyCode_ConstantKey() |
| 1244 | PyObject *v; // borrowed |
| 1245 | if (PyTuple_CheckExact(u)) { |
| 1246 | v = PyTuple_GET_ITEM(u, 1); |
| 1247 | } |
| 1248 | else { |
| 1249 | v = u; |
| 1250 | } |
| 1251 | if (v != item) { |
| 1252 | Py_INCREF(v); |
| 1253 | PyTuple_SET_ITEM(o, i, v); |
| 1254 | Py_DECREF(item); |
| 1255 | } |
| 1256 | |
| 1257 | Py_DECREF(u); |
| 1258 | } |
| 1259 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1260 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1261 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1262 | // constant keys. |
| 1263 | // See _PyCode_ConstantKey() for detail. |
| 1264 | assert(PyTuple_CheckExact(key)); |
| 1265 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1266 | |
| 1267 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1268 | if (len == 0) { // empty frozenset should not be re-created. |
| 1269 | return key; |
| 1270 | } |
| 1271 | PyObject *tuple = PyTuple_New(len); |
| 1272 | if (tuple == NULL) { |
| 1273 | Py_DECREF(key); |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | Py_ssize_t i = 0, pos = 0; |
| 1277 | PyObject *item; |
| 1278 | Py_hash_t hash; |
| 1279 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1280 | PyObject *k = merge_consts_recursive(c, item); |
| 1281 | if (k == NULL) { |
| 1282 | Py_DECREF(tuple); |
| 1283 | Py_DECREF(key); |
| 1284 | return NULL; |
| 1285 | } |
| 1286 | PyObject *u; |
| 1287 | if (PyTuple_CheckExact(k)) { |
| 1288 | u = PyTuple_GET_ITEM(k, 1); |
| 1289 | Py_INCREF(u); |
| 1290 | Py_DECREF(k); |
| 1291 | } |
| 1292 | else { |
| 1293 | u = k; |
| 1294 | } |
| 1295 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1296 | i++; |
| 1297 | } |
| 1298 | |
| 1299 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1300 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1301 | PyObject *new = PyFrozenSet_New(tuple); |
| 1302 | Py_DECREF(tuple); |
| 1303 | if (new == NULL) { |
| 1304 | Py_DECREF(key); |
| 1305 | return NULL; |
| 1306 | } |
| 1307 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1308 | Py_DECREF(o); |
| 1309 | PyTuple_SET_ITEM(key, 1, new); |
| 1310 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1311 | |
| 1312 | return key; |
| 1313 | } |
| 1314 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1315 | static Py_ssize_t |
| 1316 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1317 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1318 | if (c->c_do_not_emit_bytecode) { |
| 1319 | return 0; |
| 1320 | } |
| 1321 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1322 | PyObject *key = merge_consts_recursive(c, o); |
| 1323 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1324 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1325 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1326 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1327 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1328 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1333 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1334 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1335 | if (c->c_do_not_emit_bytecode) { |
| 1336 | return 1; |
| 1337 | } |
| 1338 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1339 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1340 | if (arg < 0) |
| 1341 | return 0; |
| 1342 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1343 | } |
| 1344 | |
| 1345 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1346 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1348 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1349 | if (c->c_do_not_emit_bytecode) { |
| 1350 | return 1; |
| 1351 | } |
| 1352 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1353 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1354 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1355 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | return compiler_addop_i(c, opcode, arg); |
| 1357 | } |
| 1358 | |
| 1359 | static int |
| 1360 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1362 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1363 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1364 | |
| 1365 | if (c->c_do_not_emit_bytecode) { |
| 1366 | return 1; |
| 1367 | } |
| 1368 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1369 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1370 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1371 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1372 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1373 | Py_DECREF(mangled); |
| 1374 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1375 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1376 | return compiler_addop_i(c, opcode, arg); |
| 1377 | } |
| 1378 | |
| 1379 | /* Add an opcode with an integer argument. |
| 1380 | Returns 0 on failure, 1 on success. |
| 1381 | */ |
| 1382 | |
| 1383 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1384 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1385 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | struct instr *i; |
| 1387 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1388 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1389 | if (c->c_do_not_emit_bytecode) { |
| 1390 | return 1; |
| 1391 | } |
| 1392 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1393 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1394 | it in the C code (like Python/ceval.c). |
| 1395 | |
| 1396 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1397 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1398 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1399 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1400 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1401 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1402 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1403 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | if (off < 0) |
| 1405 | return 0; |
| 1406 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1407 | i->i_opcode = opcode; |
| 1408 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1409 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1414 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | struct instr *i; |
| 1417 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1419 | if (c->c_do_not_emit_bytecode) { |
| 1420 | return 1; |
| 1421 | } |
| 1422 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1423 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | assert(b != NULL); |
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]; |
| 1429 | i->i_opcode = opcode; |
| 1430 | i->i_target = b; |
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 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1435 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1436 | to the new block. |
| 1437 | |
| 1438 | The returns inside this macro make it impossible to decref objects |
| 1439 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1441 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | if (compiler_next_block((C)) == NULL) \ |
| 1443 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | if (!compiler_addop((C), (OP))) \ |
| 1448 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1451 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | if (!compiler_addop((C), (OP))) { \ |
| 1453 | compiler_exit_scope(c); \ |
| 1454 | return 0; \ |
| 1455 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1458 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1459 | if (!compiler_addop_load_const((C), (O))) \ |
| 1460 | return 0; \ |
| 1461 | } |
| 1462 | |
| 1463 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1464 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1465 | PyObject *__new_const = (O); \ |
| 1466 | if (__new_const == NULL) { \ |
| 1467 | return 0; \ |
| 1468 | } \ |
| 1469 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1470 | Py_DECREF(__new_const); \ |
| 1471 | return 0; \ |
| 1472 | } \ |
| 1473 | Py_DECREF(__new_const); \ |
| 1474 | } |
| 1475 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1476 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1478 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1481 | /* Same as ADDOP_O, but steals a reference. */ |
| 1482 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1483 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1484 | Py_DECREF((O)); \ |
| 1485 | return 0; \ |
| 1486 | } \ |
| 1487 | Py_DECREF((O)); \ |
| 1488 | } |
| 1489 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1492 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1497 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1500 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1501 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1505 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1506 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1507 | return 0; \ |
| 1508 | } |
| 1509 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1510 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1511 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1512 | */ |
| 1513 | |
| 1514 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1516 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1519 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1521 | compiler_exit_scope(c); \ |
| 1522 | return 0; \ |
| 1523 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1526 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1528 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1533 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1535 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1536 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1537 | return 0; \ |
| 1538 | } \ |
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_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1542 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1543 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1545 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1546 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1547 | compiler_exit_scope(c); \ |
| 1548 | return 0; \ |
| 1549 | } \ |
| 1550 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1553 | /* These macros allows to check only for errors and not emmit bytecode |
| 1554 | * while visiting nodes. |
| 1555 | */ |
| 1556 | |
| 1557 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1558 | c->c_do_not_emit_bytecode++; |
| 1559 | |
| 1560 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1561 | c->c_do_not_emit_bytecode--; \ |
| 1562 | } |
| 1563 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1564 | /* Search if variable annotations are present statically in a block. */ |
| 1565 | |
| 1566 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1567 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1568 | { |
| 1569 | int i, j, res = 0; |
| 1570 | stmt_ty st; |
| 1571 | |
| 1572 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1573 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1574 | switch (st->kind) { |
| 1575 | case AnnAssign_kind: |
| 1576 | return 1; |
| 1577 | case For_kind: |
| 1578 | res = find_ann(st->v.For.body) || |
| 1579 | find_ann(st->v.For.orelse); |
| 1580 | break; |
| 1581 | case AsyncFor_kind: |
| 1582 | res = find_ann(st->v.AsyncFor.body) || |
| 1583 | find_ann(st->v.AsyncFor.orelse); |
| 1584 | break; |
| 1585 | case While_kind: |
| 1586 | res = find_ann(st->v.While.body) || |
| 1587 | find_ann(st->v.While.orelse); |
| 1588 | break; |
| 1589 | case If_kind: |
| 1590 | res = find_ann(st->v.If.body) || |
| 1591 | find_ann(st->v.If.orelse); |
| 1592 | break; |
| 1593 | case With_kind: |
| 1594 | res = find_ann(st->v.With.body); |
| 1595 | break; |
| 1596 | case AsyncWith_kind: |
| 1597 | res = find_ann(st->v.AsyncWith.body); |
| 1598 | break; |
| 1599 | case Try_kind: |
| 1600 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1601 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1602 | st->v.Try.handlers, j); |
| 1603 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1604 | return 1; |
| 1605 | } |
| 1606 | } |
| 1607 | res = find_ann(st->v.Try.body) || |
| 1608 | find_ann(st->v.Try.finalbody) || |
| 1609 | find_ann(st->v.Try.orelse); |
| 1610 | break; |
| 1611 | default: |
| 1612 | res = 0; |
| 1613 | } |
| 1614 | if (res) { |
| 1615 | break; |
| 1616 | } |
| 1617 | } |
| 1618 | return res; |
| 1619 | } |
| 1620 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1621 | /* |
| 1622 | * Frame block handling functions |
| 1623 | */ |
| 1624 | |
| 1625 | static int |
| 1626 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1627 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1628 | { |
| 1629 | struct fblockinfo *f; |
| 1630 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1631 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1632 | } |
| 1633 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1634 | f->fb_type = t; |
| 1635 | f->fb_block = b; |
| 1636 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1637 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1638 | return 1; |
| 1639 | } |
| 1640 | |
| 1641 | static void |
| 1642 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1643 | { |
| 1644 | struct compiler_unit *u = c->u; |
| 1645 | assert(u->u_nfblocks > 0); |
| 1646 | u->u_nfblocks--; |
| 1647 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1648 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1649 | } |
| 1650 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1651 | static int |
| 1652 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1653 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1654 | ADDOP(c, DUP_TOP); |
| 1655 | ADDOP(c, DUP_TOP); |
| 1656 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1657 | return 1; |
| 1658 | } |
| 1659 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1660 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1661 | * popping the blocks will be restored afterwards, unless another |
| 1662 | * return, break or continue is found. In which case, the TOS will |
| 1663 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1664 | */ |
| 1665 | static int |
| 1666 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1667 | int preserve_tos) |
| 1668 | { |
| 1669 | switch (info->fb_type) { |
| 1670 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1671 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1672 | return 1; |
| 1673 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1674 | case FOR_LOOP: |
| 1675 | /* Pop the iterator */ |
| 1676 | if (preserve_tos) { |
| 1677 | ADDOP(c, ROT_TWO); |
| 1678 | } |
| 1679 | ADDOP(c, POP_TOP); |
| 1680 | return 1; |
| 1681 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1682 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1683 | ADDOP(c, POP_BLOCK); |
| 1684 | return 1; |
| 1685 | |
| 1686 | case FINALLY_TRY: |
| 1687 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1688 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1689 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1690 | return 0; |
| 1691 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1692 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1693 | /* Emit the finally block, restoring the line number when done */ |
| 1694 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1695 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1696 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1697 | if (preserve_tos) { |
| 1698 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1699 | } |
| 1700 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1701 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1702 | case FINALLY_END: |
| 1703 | if (preserve_tos) { |
| 1704 | ADDOP(c, ROT_FOUR); |
| 1705 | } |
| 1706 | ADDOP(c, POP_TOP); |
| 1707 | ADDOP(c, POP_TOP); |
| 1708 | ADDOP(c, POP_TOP); |
| 1709 | if (preserve_tos) { |
| 1710 | ADDOP(c, ROT_FOUR); |
| 1711 | } |
| 1712 | ADDOP(c, POP_EXCEPT); |
| 1713 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1714 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1715 | case WITH: |
| 1716 | case ASYNC_WITH: |
| 1717 | ADDOP(c, POP_BLOCK); |
| 1718 | if (preserve_tos) { |
| 1719 | ADDOP(c, ROT_TWO); |
| 1720 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1721 | if(!compiler_call_exit_with_nones(c)) { |
| 1722 | return 0; |
| 1723 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1724 | if (info->fb_type == ASYNC_WITH) { |
| 1725 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1726 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1727 | ADDOP(c, YIELD_FROM); |
| 1728 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1729 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1730 | return 1; |
| 1731 | |
| 1732 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1733 | if (info->fb_datum) { |
| 1734 | ADDOP(c, POP_BLOCK); |
| 1735 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1736 | if (preserve_tos) { |
| 1737 | ADDOP(c, ROT_FOUR); |
| 1738 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1739 | ADDOP(c, POP_EXCEPT); |
| 1740 | if (info->fb_datum) { |
| 1741 | ADDOP_LOAD_CONST(c, Py_None); |
| 1742 | compiler_nameop(c, info->fb_datum, Store); |
| 1743 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1744 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1745 | return 1; |
| 1746 | |
| 1747 | case POP_VALUE: |
| 1748 | if (preserve_tos) { |
| 1749 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 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 | Py_UNREACHABLE(); |
| 1755 | } |
| 1756 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1757 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1758 | static int |
| 1759 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1760 | if (c->u->u_nfblocks == 0) { |
| 1761 | return 1; |
| 1762 | } |
| 1763 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1764 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1765 | *loop = top; |
| 1766 | return 1; |
| 1767 | } |
| 1768 | struct fblockinfo copy = *top; |
| 1769 | c->u->u_nfblocks--; |
| 1770 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1771 | return 0; |
| 1772 | } |
| 1773 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1774 | return 0; |
| 1775 | } |
| 1776 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1777 | c->u->u_nfblocks++; |
| 1778 | return 1; |
| 1779 | } |
| 1780 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1781 | /* Compile a sequence of statements, checking for a docstring |
| 1782 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1783 | |
| 1784 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1785 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1786 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1787 | int i = 0; |
| 1788 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1789 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1790 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1791 | /* Set current line number to the line number of first statement. |
| 1792 | This way line number for SETUP_ANNOTATIONS will always |
| 1793 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1794 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1795 | 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] | 1796 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1797 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1798 | } |
| 1799 | /* Every annotated class and module should have __annotations__. */ |
| 1800 | if (find_ann(stmts)) { |
| 1801 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1802 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1803 | if (!asdl_seq_LEN(stmts)) |
| 1804 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1805 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1806 | if (c->c_optimize < 2) { |
| 1807 | docstring = _PyAST_GetDocString(stmts); |
| 1808 | if (docstring) { |
| 1809 | i = 1; |
| 1810 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1811 | assert(st->kind == Expr_kind); |
| 1812 | VISIT(c, expr, st->v.Expr.value); |
| 1813 | if (!compiler_nameop(c, __doc__, Store)) |
| 1814 | return 0; |
| 1815 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1817 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1818 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | static PyCodeObject * |
| 1823 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1824 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1825 | PyCodeObject *co; |
| 1826 | int addNone = 1; |
| 1827 | static PyObject *module; |
| 1828 | if (!module) { |
| 1829 | module = PyUnicode_InternFromString("<module>"); |
| 1830 | if (!module) |
| 1831 | return NULL; |
| 1832 | } |
| 1833 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 1834 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 1)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | return NULL; |
| 1836 | switch (mod->kind) { |
| 1837 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1838 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | compiler_exit_scope(c); |
| 1840 | return 0; |
| 1841 | } |
| 1842 | break; |
| 1843 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1844 | if (find_ann(mod->v.Interactive.body)) { |
| 1845 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1846 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1848 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | break; |
| 1850 | case Expression_kind: |
| 1851 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1852 | addNone = 0; |
| 1853 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | default: |
| 1855 | PyErr_Format(PyExc_SystemError, |
| 1856 | "module kind %d should not be possible", |
| 1857 | mod->kind); |
| 1858 | return 0; |
| 1859 | } |
| 1860 | co = assemble(c, addNone); |
| 1861 | compiler_exit_scope(c); |
| 1862 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | /* The test for LOCAL must come before the test for FREE in order to |
| 1866 | handle classes where name is both local and free. The local var is |
| 1867 | 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] | 1868 | */ |
| 1869 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1870 | static int |
| 1871 | get_ref_type(struct compiler *c, PyObject *name) |
| 1872 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1873 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1874 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1875 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1876 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1877 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | if (scope == 0) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1879 | _Py_FatalErrorFormat(__func__, |
| 1880 | "unknown scope for %.100s in %.100s(%s)\n" |
| 1881 | "symbols: %s\nlocals: %s\nglobals: %s", |
| 1882 | PyUnicode_AsUTF8(name), |
| 1883 | PyUnicode_AsUTF8(c->u->u_name), |
| 1884 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1885 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1886 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1887 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | static int |
| 1894 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1895 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1896 | PyObject *v; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1897 | v = PyDict_GetItemWithError(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1898 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1899 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1900 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1904 | 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] | 1905 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1906 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1907 | if (qualname == NULL) |
| 1908 | qualname = co->co_name; |
| 1909 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1910 | if (free) { |
| 1911 | for (i = 0; i < free; ++i) { |
| 1912 | /* Bypass com_addop_varname because it will generate |
| 1913 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1914 | */ |
| 1915 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1916 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1917 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1918 | /* Special case: If a class contains a method with a |
| 1919 | free variable that has the same name as a method, |
| 1920 | the name will be considered free *and* local in the |
| 1921 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1922 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1923 | */ |
| 1924 | reftype = get_ref_type(c, name); |
| 1925 | if (reftype == CELL) |
| 1926 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1927 | else /* (reftype == FREE) */ |
| 1928 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1929 | if (arg == -1) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1930 | _Py_FatalErrorFormat(__func__, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1931 | "lookup %s in %s %d %d\n" |
| 1932 | "freevars of %s: %s\n", |
| 1933 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1934 | PyUnicode_AsUTF8(c->u->u_name), |
| 1935 | reftype, arg, |
| 1936 | PyUnicode_AsUTF8(co->co_name), |
| 1937 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1938 | } |
| 1939 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1940 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1941 | flags |= 0x08; |
| 1942 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1943 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1944 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1945 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1946 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1951 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | if (!decos) |
| 1956 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1958 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1959 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1960 | } |
| 1961 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1965 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 1966 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1967 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1968 | /* Push a dict of keyword-only default values. |
| 1969 | |
| 1970 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1971 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1972 | int i; |
| 1973 | PyObject *keys = NULL; |
| 1974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1976 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1977 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1978 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1979 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1980 | if (!mangled) { |
| 1981 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1983 | if (keys == NULL) { |
| 1984 | keys = PyList_New(1); |
| 1985 | if (keys == NULL) { |
| 1986 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1987 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1988 | } |
| 1989 | PyList_SET_ITEM(keys, 0, mangled); |
| 1990 | } |
| 1991 | else { |
| 1992 | int res = PyList_Append(keys, mangled); |
| 1993 | Py_DECREF(mangled); |
| 1994 | if (res == -1) { |
| 1995 | goto error; |
| 1996 | } |
| 1997 | } |
| 1998 | if (!compiler_visit_expr(c, default_)) { |
| 1999 | goto error; |
| 2000 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | } |
| 2002 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2003 | if (keys != NULL) { |
| 2004 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2005 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2006 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2007 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2008 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2009 | assert(default_count > 0); |
| 2010 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2011 | } |
| 2012 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2013 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | error: |
| 2017 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2018 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2022 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2023 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2024 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2025 | return 1; |
| 2026 | } |
| 2027 | |
| 2028 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2029 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 2030 | expr_ty annotation, PyObject *names) |
| 2031 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2033 | PyObject *mangled; |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 2034 | VISIT(c, annexpr, annotation); |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2035 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2036 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2037 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2038 | if (PyList_Append(names, mangled) < 0) { |
| 2039 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2040 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2041 | } |
| 2042 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2043 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2044 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
| 2047 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2048 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2049 | PyObject *names) |
| 2050 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2051 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2053 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2054 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | c, |
| 2056 | arg->arg, |
| 2057 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2058 | names)) |
| 2059 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2061 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | static int |
| 2065 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2066 | expr_ty returns) |
| 2067 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2068 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2069 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2070 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2071 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2072 | */ |
| 2073 | static identifier return_str; |
| 2074 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2075 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | names = PyList_New(0); |
| 2077 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2078 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2079 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2080 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2081 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 2082 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 2083 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2084 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2085 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2086 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2087 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2088 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2090 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2091 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2092 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2093 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | if (!return_str) { |
| 2096 | return_str = PyUnicode_InternFromString("return"); |
| 2097 | if (!return_str) |
| 2098 | goto error; |
| 2099 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2100 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2101 | goto error; |
| 2102 | } |
| 2103 | |
| 2104 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2106 | PyObject *keytuple = PyList_AsTuple(names); |
| 2107 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2108 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2109 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2110 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2111 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2112 | else { |
| 2113 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2114 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2115 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2116 | |
| 2117 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2119 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2123 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2124 | { |
| 2125 | VISIT_SEQ(c, expr, args->defaults); |
| 2126 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2127 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2130 | static Py_ssize_t |
| 2131 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2132 | { |
| 2133 | Py_ssize_t funcflags = 0; |
| 2134 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2135 | if (!compiler_visit_defaults(c, args)) |
| 2136 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2137 | funcflags |= 0x01; |
| 2138 | } |
| 2139 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2140 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2141 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2142 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2143 | return -1; |
| 2144 | } |
| 2145 | else if (res > 0) { |
| 2146 | funcflags |= 0x02; |
| 2147 | } |
| 2148 | } |
| 2149 | return funcflags; |
| 2150 | } |
| 2151 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2152 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2153 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2154 | { |
| 2155 | |
| 2156 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2157 | compiler_error(c, "cannot assign to __debug__"); |
| 2158 | return 1; |
| 2159 | } |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | static int |
| 2164 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2165 | { |
| 2166 | if (arg != NULL) { |
| 2167 | if (forbidden_name(c, arg->arg, Store)) |
| 2168 | return 0; |
| 2169 | } |
| 2170 | return 1; |
| 2171 | } |
| 2172 | |
| 2173 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2174 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2175 | { |
| 2176 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2177 | 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] | 2178 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2179 | return 0; |
| 2180 | } |
| 2181 | } |
| 2182 | return 1; |
| 2183 | } |
| 2184 | |
| 2185 | static int |
| 2186 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2187 | { |
| 2188 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2189 | return 0; |
| 2190 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2191 | return 0; |
| 2192 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2193 | return 0; |
| 2194 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2195 | return 0; |
| 2196 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2197 | return 0; |
| 2198 | return 1; |
| 2199 | } |
| 2200 | |
| 2201 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2202 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2204 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2205 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2206 | arguments_ty args; |
| 2207 | expr_ty returns; |
| 2208 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2209 | asdl_expr_seq* decos; |
| 2210 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2211 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2212 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2213 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2214 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2215 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2216 | if (is_async) { |
| 2217 | assert(s->kind == AsyncFunctionDef_kind); |
| 2218 | |
| 2219 | args = s->v.AsyncFunctionDef.args; |
| 2220 | returns = s->v.AsyncFunctionDef.returns; |
| 2221 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2222 | name = s->v.AsyncFunctionDef.name; |
| 2223 | body = s->v.AsyncFunctionDef.body; |
| 2224 | |
| 2225 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2226 | } else { |
| 2227 | assert(s->kind == FunctionDef_kind); |
| 2228 | |
| 2229 | args = s->v.FunctionDef.args; |
| 2230 | returns = s->v.FunctionDef.returns; |
| 2231 | decos = s->v.FunctionDef.decorator_list; |
| 2232 | name = s->v.FunctionDef.name; |
| 2233 | body = s->v.FunctionDef.body; |
| 2234 | |
| 2235 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2236 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2237 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2238 | if (!compiler_check_debug_args(c, args)) |
| 2239 | return 0; |
| 2240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2241 | if (!compiler_decorators(c, decos)) |
| 2242 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2243 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2244 | firstlineno = s->lineno; |
| 2245 | if (asdl_seq_LEN(decos)) { |
| 2246 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2247 | } |
| 2248 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2249 | funcflags = compiler_default_arguments(c, args); |
| 2250 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2252 | } |
| 2253 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2254 | annotations = compiler_visit_annotations(c, args, returns); |
| 2255 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2256 | return 0; |
| 2257 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2258 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2259 | funcflags |= 0x04; |
| 2260 | } |
| 2261 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2262 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2263 | return 0; |
| 2264 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2265 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2266 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2267 | if (c->c_optimize < 2) { |
| 2268 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2269 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2270 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | compiler_exit_scope(c); |
| 2272 | return 0; |
| 2273 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2276 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2278 | for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) { |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 2279 | VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2280 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2282 | qualname = c->u->u_qualname; |
| 2283 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2285 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2286 | Py_XDECREF(qualname); |
| 2287 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2289 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2290 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2291 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2292 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2294 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* decorators */ |
| 2296 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2297 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2298 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2299 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2300 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | static int |
| 2304 | compiler_class(struct compiler *c, stmt_ty s) |
| 2305 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | PyCodeObject *co; |
| 2307 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2308 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2309 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2311 | if (!compiler_decorators(c, decos)) |
| 2312 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2313 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2314 | firstlineno = s->lineno; |
| 2315 | if (asdl_seq_LEN(decos)) { |
| 2316 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2317 | } |
| 2318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2319 | /* ultimately generate code for: |
| 2320 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2321 | where: |
| 2322 | <func> is a function/closure created from the class body; |
| 2323 | it has a single argument (__locals__) where the dict |
| 2324 | (or MutableSequence) representing the locals is passed |
| 2325 | <name> is the class name |
| 2326 | <bases> is the positional arguments and *varargs argument |
| 2327 | <keywords> is the keyword arguments and **kwds argument |
| 2328 | This borrows from compiler_call. |
| 2329 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2331 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2332 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2333 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | return 0; |
| 2335 | /* this block represents what we do in the new scope */ |
| 2336 | { |
| 2337 | /* use the class name for name mangling */ |
| 2338 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2339 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | /* load (global) __name__ ... */ |
| 2341 | str = PyUnicode_InternFromString("__name__"); |
| 2342 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2343 | Py_XDECREF(str); |
| 2344 | compiler_exit_scope(c); |
| 2345 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2346 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | Py_DECREF(str); |
| 2348 | /* ... and store it as __module__ */ |
| 2349 | str = PyUnicode_InternFromString("__module__"); |
| 2350 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2351 | Py_XDECREF(str); |
| 2352 | compiler_exit_scope(c); |
| 2353 | return 0; |
| 2354 | } |
| 2355 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2356 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2357 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2358 | str = PyUnicode_InternFromString("__qualname__"); |
| 2359 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2360 | Py_XDECREF(str); |
| 2361 | compiler_exit_scope(c); |
| 2362 | return 0; |
| 2363 | } |
| 2364 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2366 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | compiler_exit_scope(c); |
| 2368 | return 0; |
| 2369 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2370 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2371 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2372 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2373 | str = PyUnicode_InternFromString("__class__"); |
| 2374 | if (str == NULL) { |
| 2375 | compiler_exit_scope(c); |
| 2376 | return 0; |
| 2377 | } |
| 2378 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2379 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2380 | if (i < 0) { |
| 2381 | compiler_exit_scope(c); |
| 2382 | return 0; |
| 2383 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2384 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2386 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2387 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2388 | str = PyUnicode_InternFromString("__classcell__"); |
| 2389 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2390 | Py_XDECREF(str); |
| 2391 | compiler_exit_scope(c); |
| 2392 | return 0; |
| 2393 | } |
| 2394 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2396 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2397 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2398 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2399 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2400 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2401 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2402 | /* create the code object */ |
| 2403 | co = assemble(c, 1); |
| 2404 | } |
| 2405 | /* leave the new scope */ |
| 2406 | compiler_exit_scope(c); |
| 2407 | if (co == NULL) |
| 2408 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | /* 2. load the 'build_class' function */ |
| 2411 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2412 | |
| 2413 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2414 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2415 | Py_DECREF(co); |
| 2416 | |
| 2417 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2418 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2419 | |
| 2420 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2421 | 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] | 2422 | return 0; |
| 2423 | |
| 2424 | /* 6. apply decorators */ |
| 2425 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2426 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2427 | } |
| 2428 | |
| 2429 | /* 7. store into <name> */ |
| 2430 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2431 | return 0; |
| 2432 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2435 | /* Return 0 if the expression is a constant value except named singletons. |
| 2436 | Return 1 otherwise. */ |
| 2437 | static int |
| 2438 | check_is_arg(expr_ty e) |
| 2439 | { |
| 2440 | if (e->kind != Constant_kind) { |
| 2441 | return 1; |
| 2442 | } |
| 2443 | PyObject *value = e->v.Constant.value; |
| 2444 | return (value == Py_None |
| 2445 | || value == Py_False |
| 2446 | || value == Py_True |
| 2447 | || value == Py_Ellipsis); |
| 2448 | } |
| 2449 | |
| 2450 | /* Check operands of identity chacks ("is" and "is not"). |
| 2451 | Emit a warning if any operand is a constant except named singletons. |
| 2452 | Return 0 on error. |
| 2453 | */ |
| 2454 | static int |
| 2455 | check_compare(struct compiler *c, expr_ty e) |
| 2456 | { |
| 2457 | Py_ssize_t i, n; |
| 2458 | int left = check_is_arg(e->v.Compare.left); |
| 2459 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2460 | for (i = 0; i < n; i++) { |
| 2461 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2462 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2463 | if (op == Is || op == IsNot) { |
| 2464 | if (!right || !left) { |
| 2465 | const char *msg = (op == Is) |
| 2466 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2467 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2468 | return compiler_warn(c, msg); |
| 2469 | } |
| 2470 | } |
| 2471 | left = right; |
| 2472 | } |
| 2473 | return 1; |
| 2474 | } |
| 2475 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2476 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2477 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2478 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2479 | switch (op) { |
| 2480 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2481 | cmp = Py_EQ; |
| 2482 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2483 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2484 | cmp = Py_NE; |
| 2485 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2486 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2487 | cmp = Py_LT; |
| 2488 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2489 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2490 | cmp = Py_LE; |
| 2491 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2492 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2493 | cmp = Py_GT; |
| 2494 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2495 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2496 | cmp = Py_GE; |
| 2497 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2498 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2499 | ADDOP_I(c, IS_OP, 0); |
| 2500 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2501 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2502 | ADDOP_I(c, IS_OP, 1); |
| 2503 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2504 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2505 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2506 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2507 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2508 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2509 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2510 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2511 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2512 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2513 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2514 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2515 | } |
| 2516 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2517 | |
| 2518 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2519 | static int |
| 2520 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2521 | { |
| 2522 | switch (e->kind) { |
| 2523 | case UnaryOp_kind: |
| 2524 | if (e->v.UnaryOp.op == Not) |
| 2525 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2526 | /* fallback to general implementation */ |
| 2527 | break; |
| 2528 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2529 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2530 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2531 | assert(n >= 0); |
| 2532 | int cond2 = e->v.BoolOp.op == Or; |
| 2533 | basicblock *next2 = next; |
| 2534 | if (!cond2 != !cond) { |
| 2535 | next2 = compiler_new_block(c); |
| 2536 | if (next2 == NULL) |
| 2537 | return 0; |
| 2538 | } |
| 2539 | for (i = 0; i < n; ++i) { |
| 2540 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2541 | return 0; |
| 2542 | } |
| 2543 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2544 | return 0; |
| 2545 | if (next2 != next) |
| 2546 | compiler_use_next_block(c, next2); |
| 2547 | return 1; |
| 2548 | } |
| 2549 | case IfExp_kind: { |
| 2550 | basicblock *end, *next2; |
| 2551 | end = compiler_new_block(c); |
| 2552 | if (end == NULL) |
| 2553 | return 0; |
| 2554 | next2 = compiler_new_block(c); |
| 2555 | if (next2 == NULL) |
| 2556 | return 0; |
| 2557 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2558 | return 0; |
| 2559 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2560 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2561 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2562 | compiler_use_next_block(c, next2); |
| 2563 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2564 | return 0; |
| 2565 | compiler_use_next_block(c, end); |
| 2566 | return 1; |
| 2567 | } |
| 2568 | case Compare_kind: { |
| 2569 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2570 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2571 | if (!check_compare(c, e)) { |
| 2572 | return 0; |
| 2573 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2574 | basicblock *cleanup = compiler_new_block(c); |
| 2575 | if (cleanup == NULL) |
| 2576 | return 0; |
| 2577 | VISIT(c, expr, e->v.Compare.left); |
| 2578 | for (i = 0; i < n; i++) { |
| 2579 | VISIT(c, expr, |
| 2580 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2581 | ADDOP(c, DUP_TOP); |
| 2582 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2583 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2584 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2585 | NEXT_BLOCK(c); |
| 2586 | } |
| 2587 | 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] | 2588 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2589 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2590 | basicblock *end = compiler_new_block(c); |
| 2591 | if (end == NULL) |
| 2592 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2593 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2594 | compiler_use_next_block(c, cleanup); |
| 2595 | ADDOP(c, POP_TOP); |
| 2596 | if (!cond) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2597 | ADDOP_JUMP(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2598 | } |
| 2599 | compiler_use_next_block(c, end); |
| 2600 | return 1; |
| 2601 | } |
| 2602 | /* fallback to general implementation */ |
| 2603 | break; |
| 2604 | } |
| 2605 | default: |
| 2606 | /* fallback to general implementation */ |
| 2607 | break; |
| 2608 | } |
| 2609 | |
| 2610 | /* general implementation */ |
| 2611 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2612 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2613 | return 1; |
| 2614 | } |
| 2615 | |
| 2616 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2617 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2619 | basicblock *end, *next; |
| 2620 | |
| 2621 | assert(e->kind == IfExp_kind); |
| 2622 | end = compiler_new_block(c); |
| 2623 | if (end == NULL) |
| 2624 | return 0; |
| 2625 | next = compiler_new_block(c); |
| 2626 | if (next == NULL) |
| 2627 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2628 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2629 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2630 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2631 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | compiler_use_next_block(c, next); |
| 2633 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2634 | compiler_use_next_block(c, end); |
| 2635 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2639 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2641 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2642 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2644 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2645 | arguments_ty args = e->v.Lambda.args; |
| 2646 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2648 | if (!compiler_check_debug_args(c, args)) |
| 2649 | return 0; |
| 2650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | if (!name) { |
| 2652 | name = PyUnicode_InternFromString("<lambda>"); |
| 2653 | if (!name) |
| 2654 | return 0; |
| 2655 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2656 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2657 | funcflags = compiler_default_arguments(c, args); |
| 2658 | if (funcflags == -1) { |
| 2659 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2661 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2662 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2663 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | /* Make None the first constant, so the lambda can't have a |
| 2667 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2668 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2670 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2671 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2672 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2674 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2675 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2676 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | } |
| 2678 | else { |
| 2679 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2680 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2682 | qualname = c->u->u_qualname; |
| 2683 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2685 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2687 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2688 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2689 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | Py_DECREF(co); |
| 2691 | |
| 2692 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2696 | compiler_if(struct compiler *c, stmt_ty s) |
| 2697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2698 | basicblock *end, *next; |
| 2699 | int constant; |
| 2700 | assert(s->kind == If_kind); |
| 2701 | end = compiler_new_block(c); |
| 2702 | if (end == NULL) |
| 2703 | return 0; |
| 2704 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2705 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2706 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | * constant = 1: "if 1", "if 2", ... |
| 2708 | * constant = -1: rest */ |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2709 | if (constant == 0) { |
| 2710 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | VISIT_SEQ(c, stmt, s->v.If.body); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2712 | END_DO_NOT_EMIT_BYTECODE |
| 2713 | if (s->v.If.orelse) { |
| 2714 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2715 | } |
| 2716 | } else if (constant == 1) { |
| 2717 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2718 | if (s->v.If.orelse) { |
| 2719 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2720 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2721 | END_DO_NOT_EMIT_BYTECODE |
| 2722 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2724 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | next = compiler_new_block(c); |
| 2726 | if (next == NULL) |
| 2727 | return 0; |
| 2728 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2729 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | next = end; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2731 | } |
| 2732 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2733 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2734 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2735 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2736 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2737 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | compiler_use_next_block(c, next); |
| 2739 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2740 | } |
| 2741 | } |
| 2742 | compiler_use_next_block(c, end); |
| 2743 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
| 2746 | static int |
| 2747 | compiler_for(struct compiler *c, stmt_ty s) |
| 2748 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2749 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | start = compiler_new_block(c); |
| 2752 | cleanup = compiler_new_block(c); |
| 2753 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2754 | if (start == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2756 | } |
| 2757 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2759 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | VISIT(c, expr, s->v.For.iter); |
| 2761 | ADDOP(c, GET_ITER); |
| 2762 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2763 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | VISIT(c, expr, s->v.For.target); |
| 2765 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2766 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2767 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2768 | |
| 2769 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2772 | compiler_use_next_block(c, end); |
| 2773 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2776 | |
| 2777 | static int |
| 2778 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2779 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2780 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2781 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2782 | c->u->u_ste->ste_coroutine = 1; |
| 2783 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2784 | return compiler_error(c, "'async for' outside async function"); |
| 2785 | } |
| 2786 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2787 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2788 | except = compiler_new_block(c); |
| 2789 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2790 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2791 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2792 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2793 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2794 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2795 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2796 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2797 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2798 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2799 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2800 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2801 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2802 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2803 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2804 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2805 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2806 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2807 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2808 | /* Success block for __anext__ */ |
| 2809 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2810 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2811 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2812 | |
| 2813 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2814 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2815 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2816 | compiler_use_next_block(c, except); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 2817 | |
| 2818 | c->u->u_lineno = -1; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2819 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2820 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2821 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2822 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2823 | |
| 2824 | compiler_use_next_block(c, end); |
| 2825 | |
| 2826 | return 1; |
| 2827 | } |
| 2828 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2829 | static int |
| 2830 | compiler_while(struct compiler *c, stmt_ty s) |
| 2831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2833 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | if (constant == 0) { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2836 | BEGIN_DO_NOT_EMIT_BYTECODE |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2837 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2838 | // inside a while loop so it can correctly evaluate syntax |
| 2839 | // errors. |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2840 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) { |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2841 | return 0; |
| 2842 | } |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2843 | VISIT_SEQ(c, stmt, s->v.While.body); |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2844 | // Remove the dummy block now that is not needed. |
| 2845 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2846 | END_DO_NOT_EMIT_BYTECODE |
| 2847 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2849 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2850 | return 1; |
| 2851 | } |
| 2852 | loop = compiler_new_block(c); |
| 2853 | end = compiler_new_block(c); |
| 2854 | if (constant == -1) { |
| 2855 | anchor = compiler_new_block(c); |
| 2856 | if (anchor == NULL) |
| 2857 | return 0; |
| 2858 | } |
| 2859 | if (loop == NULL || end == NULL) |
| 2860 | return 0; |
| 2861 | if (s->v.While.orelse) { |
| 2862 | orelse = compiler_new_block(c); |
| 2863 | if (orelse == NULL) |
| 2864 | return 0; |
| 2865 | } |
| 2866 | else |
| 2867 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +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 | fee5526 | 2019-11-21 09:11:43 +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; |
| 2872 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2873 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2874 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | } |
| 2876 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2877 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | /* XXX should the two POP instructions be in a separate block |
| 2880 | if there is no else clause ? |
| 2881 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2882 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2883 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2885 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2888 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 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); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2920 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2923 | static int |
| 2924 | compiler_break(struct compiler *c) |
| 2925 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2926 | struct fblockinfo *loop = NULL; |
| 2927 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2928 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2929 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2930 | if (loop == NULL) { |
| 2931 | return compiler_error(c, "'break' outside loop"); |
| 2932 | } |
| 2933 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2934 | return 0; |
| 2935 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2936 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2937 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2938 | } |
| 2939 | |
| 2940 | static int |
| 2941 | compiler_continue(struct compiler *c) |
| 2942 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2943 | struct fblockinfo *loop = NULL; |
| 2944 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2945 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2946 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2947 | if (loop == NULL) { |
| 2948 | return compiler_error(c, "'continue' not properly in loop"); |
| 2949 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2950 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2951 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2955 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | |
| 2957 | SETUP_FINALLY L |
| 2958 | <code for body> |
| 2959 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2960 | <code for finalbody> |
| 2961 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2962 | L: |
| 2963 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2964 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2966 | The special instructions use the block stack. Each block |
| 2967 | stack entry contains the instruction that created it (here |
| 2968 | SETUP_FINALLY), the level of the value stack at the time the |
| 2969 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2971 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | Pushes the current value stack level and the label |
| 2973 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2975 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2977 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2978 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2979 | exceptions are pushed onto the value stack (and the exception |
| 2980 | condition is cleared), and the interpreter jumps to the label |
| 2981 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2982 | */ |
| 2983 | |
| 2984 | static int |
| 2985 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2986 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2987 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2989 | body = compiler_new_block(c); |
| 2990 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2991 | exit = compiler_new_block(c); |
| 2992 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2994 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2995 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2996 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2998 | 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] | 2999 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3000 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3001 | if (!compiler_try_except(c, s)) |
| 3002 | return 0; |
| 3003 | } |
| 3004 | else { |
| 3005 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3006 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3008 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3009 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3010 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3011 | /* `finally` block */ |
| 3012 | compiler_use_next_block(c, end); |
| 3013 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3014 | return 0; |
| 3015 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3016 | compiler_pop_fblock(c, FINALLY_END, end); |
| 3017 | ADDOP(c, RERAISE); |
| 3018 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3023 | 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] | 3024 | (The contents of the value stack is shown in [], with the top |
| 3025 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3026 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3027 | |
| 3028 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3029 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | [] <code for S> |
| 3031 | [] POP_BLOCK |
| 3032 | [] JUMP_FORWARD L0 |
| 3033 | |
| 3034 | [tb, val, exc] L1: DUP ) |
| 3035 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3036 | [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] | 3037 | [tb, val, exc] POP |
| 3038 | [tb, val] <assign to V1> (or POP if no V1) |
| 3039 | [tb] POP |
| 3040 | [] <code for S1> |
| 3041 | JUMP_FORWARD L0 |
| 3042 | |
| 3043 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3044 | .............................etc....................... |
| 3045 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3046 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | |
| 3048 | [] L0: <next statement> |
| 3049 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | Of course, parts are not generated if Vi or Ei is not present. |
| 3051 | */ |
| 3052 | static int |
| 3053 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3054 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3055 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3056 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | body = compiler_new_block(c); |
| 3059 | except = compiler_new_block(c); |
| 3060 | orelse = compiler_new_block(c); |
| 3061 | end = compiler_new_block(c); |
| 3062 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3063 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3064 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3065 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3066 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3068 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | ADDOP(c, POP_BLOCK); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3070 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3071 | ADDOP_JUMP(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3072 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3074 | /* Runtime will push a block here, so we need to account for that */ |
| 3075 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3076 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | for (i = 0; i < n; i++) { |
| 3078 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3079 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3081 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3082 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | except = compiler_new_block(c); |
| 3084 | if (except == NULL) |
| 3085 | return 0; |
| 3086 | if (handler->v.ExceptHandler.type) { |
| 3087 | ADDOP(c, DUP_TOP); |
| 3088 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3089 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | } |
| 3091 | ADDOP(c, POP_TOP); |
| 3092 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3093 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3094 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3095 | cleanup_end = compiler_new_block(c); |
| 3096 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3097 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3098 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3099 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3100 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3101 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3102 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3104 | /* |
| 3105 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3106 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3107 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3108 | try: |
| 3109 | # body |
| 3110 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3111 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3112 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3113 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3115 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3116 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3117 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3118 | 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] | 3119 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3121 | /* second # body */ |
| 3122 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3123 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3124 | ADDOP(c, POP_BLOCK); |
| 3125 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3126 | /* name = None; del name; # Mark as artificial */ |
| 3127 | c->u->u_lineno = -1; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3128 | ADDOP_LOAD_CONST(c, Py_None); |
| 3129 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3130 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3131 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3133 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3134 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3136 | /* name = None; del name; # Mark as artificial */ |
| 3137 | c->u->u_lineno = -1; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3138 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3139 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3140 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3141 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3142 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | } |
| 3144 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3145 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3147 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3148 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3149 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3151 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3152 | ADDOP(c, POP_TOP); |
| 3153 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3154 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3155 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3157 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3158 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3159 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3161 | compiler_use_next_block(c, except); |
| 3162 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3163 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3164 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3166 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3167 | compiler_use_next_block(c, end); |
| 3168 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3169 | } |
| 3170 | |
| 3171 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3172 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3173 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3174 | return compiler_try_finally(c, s); |
| 3175 | else |
| 3176 | return compiler_try_except(c, s); |
| 3177 | } |
| 3178 | |
| 3179 | |
| 3180 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3182 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | /* The IMPORT_NAME opcode was already generated. This function |
| 3184 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | 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] | 3187 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3189 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3190 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3191 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3192 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3193 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3195 | while (1) { |
| 3196 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3198 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3199 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3200 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3201 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3203 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3204 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3205 | if (dot == -1) { |
| 3206 | break; |
| 3207 | } |
| 3208 | ADDOP(c, ROT_TWO); |
| 3209 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3211 | if (!compiler_nameop(c, asname, Store)) { |
| 3212 | return 0; |
| 3213 | } |
| 3214 | ADDOP(c, POP_TOP); |
| 3215 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | } |
| 3217 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | static int |
| 3221 | compiler_import(struct compiler *c, stmt_ty s) |
| 3222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | /* The Import node stores a module name like a.b.c as a single |
| 3224 | string. This is convenient for all cases except |
| 3225 | import a.b.c as d |
| 3226 | where we need to parse that string to extract the individual |
| 3227 | module names. |
| 3228 | XXX Perhaps change the representation to make this case simpler? |
| 3229 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3230 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3231 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3232 | PyObject *zero = _PyLong_GetZero(); // borrowed reference |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | for (i = 0; i < n; i++) { |
| 3234 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3235 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3236 | |
Victor Stinner | c9bc290 | 2020-10-27 02:24:34 +0100 | [diff] [blame] | 3237 | ADDOP_LOAD_CONST(c, zero); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3238 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3241 | if (alias->asname) { |
| 3242 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3243 | if (!r) |
| 3244 | return r; |
| 3245 | } |
| 3246 | else { |
| 3247 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3248 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3249 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3250 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3251 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3252 | if (tmp == NULL) |
| 3253 | return 0; |
| 3254 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3256 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | Py_DECREF(tmp); |
| 3258 | } |
| 3259 | if (!r) |
| 3260 | return r; |
| 3261 | } |
| 3262 | } |
| 3263 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | static int |
| 3267 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3268 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3269 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3270 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3273 | if (!empty_string) { |
| 3274 | empty_string = PyUnicode_FromString(""); |
| 3275 | if (!empty_string) |
| 3276 | return 0; |
| 3277 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3278 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3279 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3280 | |
| 3281 | names = PyTuple_New(n); |
| 3282 | if (!names) |
| 3283 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | /* build up the names */ |
| 3286 | for (i = 0; i < n; i++) { |
| 3287 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3288 | Py_INCREF(alias->name); |
| 3289 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3290 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3292 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3293 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | Py_DECREF(names); |
| 3295 | return compiler_error(c, "from __future__ imports must occur " |
| 3296 | "at the beginning of the file"); |
| 3297 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3298 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3300 | if (s->v.ImportFrom.module) { |
| 3301 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3302 | } |
| 3303 | else { |
| 3304 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3305 | } |
| 3306 | for (i = 0; i < n; i++) { |
| 3307 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3308 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3309 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3310 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3311 | assert(n == 1); |
| 3312 | ADDOP(c, IMPORT_STAR); |
| 3313 | return 1; |
| 3314 | } |
| 3315 | |
| 3316 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3317 | store_name = alias->name; |
| 3318 | if (alias->asname) |
| 3319 | store_name = alias->asname; |
| 3320 | |
| 3321 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | return 0; |
| 3323 | } |
| 3324 | } |
| 3325 | /* remove imported module */ |
| 3326 | ADDOP(c, POP_TOP); |
| 3327 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | static int |
| 3331 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3334 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3335 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3338 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3339 | { |
| 3340 | if (!compiler_warn(c, "assertion is always true, " |
| 3341 | "perhaps remove parentheses?")) |
| 3342 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3343 | return 0; |
| 3344 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3345 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | end = compiler_new_block(c); |
| 3347 | if (end == NULL) |
| 3348 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3349 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3350 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3351 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3352 | if (s->v.Assert.msg) { |
| 3353 | VISIT(c, expr, s->v.Assert.msg); |
| 3354 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3355 | } |
| 3356 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3357 | compiler_use_next_block(c, end); |
| 3358 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3362 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3363 | { |
| 3364 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3365 | VISIT(c, expr, value); |
| 3366 | ADDOP(c, PRINT_EXPR); |
| 3367 | return 1; |
| 3368 | } |
| 3369 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3370 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3371 | /* ignore constant statement */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3372 | ADDOP(c, NOP); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3373 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | VISIT(c, expr, value); |
| 3377 | ADDOP(c, POP_TOP); |
| 3378 | return 1; |
| 3379 | } |
| 3380 | |
| 3381 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3382 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3383 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3384 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3387 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | switch (s->kind) { |
| 3390 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3391 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | case ClassDef_kind: |
| 3393 | return compiler_class(c, s); |
| 3394 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3395 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | case Delete_kind: |
| 3397 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3398 | break; |
| 3399 | case Assign_kind: |
| 3400 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3401 | VISIT(c, expr, s->v.Assign.value); |
| 3402 | for (i = 0; i < n; i++) { |
| 3403 | if (i < n - 1) |
| 3404 | ADDOP(c, DUP_TOP); |
| 3405 | VISIT(c, expr, |
| 3406 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3407 | } |
| 3408 | break; |
| 3409 | case AugAssign_kind: |
| 3410 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3411 | case AnnAssign_kind: |
| 3412 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3413 | case For_kind: |
| 3414 | return compiler_for(c, s); |
| 3415 | case While_kind: |
| 3416 | return compiler_while(c, s); |
| 3417 | case If_kind: |
| 3418 | return compiler_if(c, s); |
| 3419 | case Raise_kind: |
| 3420 | n = 0; |
| 3421 | if (s->v.Raise.exc) { |
| 3422 | VISIT(c, expr, s->v.Raise.exc); |
| 3423 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3424 | if (s->v.Raise.cause) { |
| 3425 | VISIT(c, expr, s->v.Raise.cause); |
| 3426 | n++; |
| 3427 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3429 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3431 | case Try_kind: |
| 3432 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | case Assert_kind: |
| 3434 | return compiler_assert(c, s); |
| 3435 | case Import_kind: |
| 3436 | return compiler_import(c, s); |
| 3437 | case ImportFrom_kind: |
| 3438 | return compiler_from_import(c, s); |
| 3439 | case Global_kind: |
| 3440 | case Nonlocal_kind: |
| 3441 | break; |
| 3442 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3443 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | case Pass_kind: |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 3445 | ADDOP(c, NOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | break; |
| 3447 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3448 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | case Continue_kind: |
| 3450 | return compiler_continue(c); |
| 3451 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3452 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3453 | case AsyncFunctionDef_kind: |
| 3454 | return compiler_function(c, s, 1); |
| 3455 | case AsyncWith_kind: |
| 3456 | return compiler_async_with(c, s, 0); |
| 3457 | case AsyncFor_kind: |
| 3458 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | static int |
| 3465 | unaryop(unaryop_ty op) |
| 3466 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3467 | switch (op) { |
| 3468 | case Invert: |
| 3469 | return UNARY_INVERT; |
| 3470 | case Not: |
| 3471 | return UNARY_NOT; |
| 3472 | case UAdd: |
| 3473 | return UNARY_POSITIVE; |
| 3474 | case USub: |
| 3475 | return UNARY_NEGATIVE; |
| 3476 | default: |
| 3477 | PyErr_Format(PyExc_SystemError, |
| 3478 | "unary op %d should not be possible", op); |
| 3479 | return 0; |
| 3480 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
| 3483 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3484 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3485 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | switch (op) { |
| 3487 | case Add: |
| 3488 | return BINARY_ADD; |
| 3489 | case Sub: |
| 3490 | return BINARY_SUBTRACT; |
| 3491 | case Mult: |
| 3492 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3493 | case MatMult: |
| 3494 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | case Div: |
| 3496 | return BINARY_TRUE_DIVIDE; |
| 3497 | case Mod: |
| 3498 | return BINARY_MODULO; |
| 3499 | case Pow: |
| 3500 | return BINARY_POWER; |
| 3501 | case LShift: |
| 3502 | return BINARY_LSHIFT; |
| 3503 | case RShift: |
| 3504 | return BINARY_RSHIFT; |
| 3505 | case BitOr: |
| 3506 | return BINARY_OR; |
| 3507 | case BitXor: |
| 3508 | return BINARY_XOR; |
| 3509 | case BitAnd: |
| 3510 | return BINARY_AND; |
| 3511 | case FloorDiv: |
| 3512 | return BINARY_FLOOR_DIVIDE; |
| 3513 | default: |
| 3514 | PyErr_Format(PyExc_SystemError, |
| 3515 | "binary op %d should not be possible", op); |
| 3516 | return 0; |
| 3517 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3521 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | switch (op) { |
| 3524 | case Add: |
| 3525 | return INPLACE_ADD; |
| 3526 | case Sub: |
| 3527 | return INPLACE_SUBTRACT; |
| 3528 | case Mult: |
| 3529 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3530 | case MatMult: |
| 3531 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3532 | case Div: |
| 3533 | return INPLACE_TRUE_DIVIDE; |
| 3534 | case Mod: |
| 3535 | return INPLACE_MODULO; |
| 3536 | case Pow: |
| 3537 | return INPLACE_POWER; |
| 3538 | case LShift: |
| 3539 | return INPLACE_LSHIFT; |
| 3540 | case RShift: |
| 3541 | return INPLACE_RSHIFT; |
| 3542 | case BitOr: |
| 3543 | return INPLACE_OR; |
| 3544 | case BitXor: |
| 3545 | return INPLACE_XOR; |
| 3546 | case BitAnd: |
| 3547 | return INPLACE_AND; |
| 3548 | case FloorDiv: |
| 3549 | return INPLACE_FLOOR_DIVIDE; |
| 3550 | default: |
| 3551 | PyErr_Format(PyExc_SystemError, |
| 3552 | "inplace binary op %d should not be possible", op); |
| 3553 | return 0; |
| 3554 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | static int |
| 3558 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3559 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3560 | int op, scope; |
| 3561 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | PyObject *dict = c->u->u_names; |
| 3565 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3566 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3567 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3568 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3569 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3570 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3571 | if (forbidden_name(c, name, ctx)) |
| 3572 | return 0; |
| 3573 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3574 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3575 | if (!mangled) |
| 3576 | return 0; |
| 3577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | op = 0; |
| 3579 | optype = OP_NAME; |
| 3580 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3581 | switch (scope) { |
| 3582 | case FREE: |
| 3583 | dict = c->u->u_freevars; |
| 3584 | optype = OP_DEREF; |
| 3585 | break; |
| 3586 | case CELL: |
| 3587 | dict = c->u->u_cellvars; |
| 3588 | optype = OP_DEREF; |
| 3589 | break; |
| 3590 | case LOCAL: |
| 3591 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3592 | optype = OP_FAST; |
| 3593 | break; |
| 3594 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3595 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3596 | optype = OP_GLOBAL; |
| 3597 | break; |
| 3598 | case GLOBAL_EXPLICIT: |
| 3599 | optype = OP_GLOBAL; |
| 3600 | break; |
| 3601 | default: |
| 3602 | /* scope can be 0 */ |
| 3603 | break; |
| 3604 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | /* 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] | 3607 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | switch (optype) { |
| 3610 | case OP_DEREF: |
| 3611 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3612 | case Load: |
| 3613 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3614 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3615 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3616 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | } |
| 3618 | break; |
| 3619 | case OP_FAST: |
| 3620 | switch (ctx) { |
| 3621 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3622 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3623 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3624 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3625 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | return 1; |
| 3627 | case OP_GLOBAL: |
| 3628 | switch (ctx) { |
| 3629 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3630 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | } |
| 3633 | break; |
| 3634 | case OP_NAME: |
| 3635 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3636 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3637 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3638 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3639 | } |
| 3640 | break; |
| 3641 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3643 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3644 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | Py_DECREF(mangled); |
| 3646 | if (arg < 0) |
| 3647 | return 0; |
| 3648 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | static int |
| 3652 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3654 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3655 | int jumpi; |
| 3656 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3657 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3659 | assert(e->kind == BoolOp_kind); |
| 3660 | if (e->v.BoolOp.op == And) |
| 3661 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3662 | else |
| 3663 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3664 | end = compiler_new_block(c); |
| 3665 | if (end == NULL) |
| 3666 | return 0; |
| 3667 | s = e->v.BoolOp.values; |
| 3668 | n = asdl_seq_LEN(s) - 1; |
| 3669 | assert(n >= 0); |
| 3670 | for (i = 0; i < n; ++i) { |
| 3671 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3672 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3673 | basicblock *next = compiler_new_block(c); |
| 3674 | if (next == NULL) { |
| 3675 | return 0; |
| 3676 | } |
| 3677 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3678 | } |
| 3679 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3680 | compiler_use_next_block(c, end); |
| 3681 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
| 3684 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3685 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3686 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3687 | { |
| 3688 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3689 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3690 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3691 | PyObject *folded = PyTuple_New(n); |
| 3692 | if (folded == NULL) { |
| 3693 | return 0; |
| 3694 | } |
| 3695 | PyObject *val; |
| 3696 | for (i = 0; i < n; i++) { |
| 3697 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3698 | Py_INCREF(val); |
| 3699 | PyTuple_SET_ITEM(folded, i, val); |
| 3700 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3701 | if (tuple) { |
| 3702 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3703 | } else { |
| 3704 | if (add == SET_ADD) { |
| 3705 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3706 | if (folded == NULL) { |
| 3707 | return 0; |
| 3708 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3709 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3710 | ADDOP_I(c, build, pushed); |
| 3711 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3712 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3713 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3714 | return 1; |
| 3715 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3716 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3717 | for (i = 0; i < n; i++) { |
| 3718 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3719 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3720 | seen_star = 1; |
| 3721 | } |
| 3722 | } |
| 3723 | if (seen_star) { |
| 3724 | seen_star = 0; |
| 3725 | for (i = 0; i < n; i++) { |
| 3726 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3727 | if (elt->kind == Starred_kind) { |
| 3728 | if (seen_star == 0) { |
| 3729 | ADDOP_I(c, build, i+pushed); |
| 3730 | seen_star = 1; |
| 3731 | } |
| 3732 | VISIT(c, expr, elt->v.Starred.value); |
| 3733 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3734 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3735 | else { |
| 3736 | VISIT(c, expr, elt); |
| 3737 | if (seen_star) { |
| 3738 | ADDOP_I(c, add, 1); |
| 3739 | } |
| 3740 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3741 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3742 | assert(seen_star); |
| 3743 | if (tuple) { |
| 3744 | ADDOP(c, LIST_TO_TUPLE); |
| 3745 | } |
| 3746 | } |
| 3747 | else { |
| 3748 | for (i = 0; i < n; i++) { |
| 3749 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3750 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3751 | } |
| 3752 | if (tuple) { |
| 3753 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3754 | } else { |
| 3755 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3756 | } |
| 3757 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3758 | return 1; |
| 3759 | } |
| 3760 | |
| 3761 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3762 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3763 | { |
| 3764 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3765 | Py_ssize_t i; |
| 3766 | int seen_star = 0; |
| 3767 | for (i = 0; i < n; i++) { |
| 3768 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3769 | if (elt->kind == Starred_kind && !seen_star) { |
| 3770 | if ((i >= (1 << 8)) || |
| 3771 | (n-i-1 >= (INT_MAX >> 8))) |
| 3772 | return compiler_error(c, |
| 3773 | "too many expressions in " |
| 3774 | "star-unpacking assignment"); |
| 3775 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3776 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3777 | } |
| 3778 | else if (elt->kind == Starred_kind) { |
| 3779 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3780 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3781 | } |
| 3782 | } |
| 3783 | if (!seen_star) { |
| 3784 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3785 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3786 | for (i = 0; i < n; i++) { |
| 3787 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3788 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3789 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3790 | return 1; |
| 3791 | } |
| 3792 | |
| 3793 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3794 | compiler_list(struct compiler *c, expr_ty e) |
| 3795 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3796 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3797 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3798 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3799 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3800 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3801 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3802 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3804 | else |
| 3805 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3806 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | static int |
| 3810 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3811 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3812 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3813 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3814 | return assignment_helper(c, elts); |
| 3815 | } |
| 3816 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3817 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3818 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3819 | } |
| 3820 | else |
| 3821 | VISIT_SEQ(c, expr, elts); |
| 3822 | return 1; |
| 3823 | } |
| 3824 | |
| 3825 | static int |
| 3826 | compiler_set(struct compiler *c, expr_ty e) |
| 3827 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3828 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3829 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3833 | 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] | 3834 | { |
| 3835 | Py_ssize_t i; |
| 3836 | for (i = begin; i < end; i++) { |
| 3837 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3838 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3839 | return 0; |
| 3840 | } |
| 3841 | return 1; |
| 3842 | } |
| 3843 | |
| 3844 | static int |
| 3845 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3846 | { |
| 3847 | Py_ssize_t i, n = end - begin; |
| 3848 | PyObject *keys, *key; |
| 3849 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3850 | for (i = begin; i < end; i++) { |
| 3851 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3852 | } |
| 3853 | keys = PyTuple_New(n); |
| 3854 | if (keys == NULL) { |
| 3855 | return 0; |
| 3856 | } |
| 3857 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3858 | 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] | 3859 | Py_INCREF(key); |
| 3860 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3861 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3862 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3863 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3864 | } |
| 3865 | else { |
| 3866 | for (i = begin; i < end; i++) { |
| 3867 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3868 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3869 | } |
| 3870 | ADDOP_I(c, BUILD_MAP, n); |
| 3871 | } |
| 3872 | return 1; |
| 3873 | } |
| 3874 | |
| 3875 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3876 | compiler_dict(struct compiler *c, expr_ty e) |
| 3877 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3878 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3879 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3880 | int is_unpacking = 0; |
| 3881 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3882 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3883 | elements = 0; |
| 3884 | for (i = 0; i < n; i++) { |
| 3885 | 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] | 3886 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3887 | if (elements) { |
| 3888 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3889 | return 0; |
| 3890 | } |
| 3891 | if (have_dict) { |
| 3892 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3893 | } |
| 3894 | have_dict = 1; |
| 3895 | elements = 0; |
| 3896 | } |
| 3897 | if (have_dict == 0) { |
| 3898 | ADDOP_I(c, BUILD_MAP, 0); |
| 3899 | have_dict = 1; |
| 3900 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3901 | 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] | 3902 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3903 | } |
| 3904 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3905 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3906 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3907 | return 0; |
| 3908 | } |
| 3909 | if (have_dict) { |
| 3910 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3911 | } |
| 3912 | have_dict = 1; |
| 3913 | elements = 0; |
| 3914 | } |
| 3915 | else { |
| 3916 | elements++; |
| 3917 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | } |
| 3919 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3920 | if (elements) { |
| 3921 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3922 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3923 | } |
| 3924 | if (have_dict) { |
| 3925 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3926 | } |
| 3927 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3928 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3929 | if (!have_dict) { |
| 3930 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3931 | } |
| 3932 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
| 3935 | static int |
| 3936 | compiler_compare(struct compiler *c, expr_ty e) |
| 3937 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3938 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3939 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3940 | if (!check_compare(c, e)) { |
| 3941 | return 0; |
| 3942 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3943 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3944 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3945 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3946 | if (n == 0) { |
| 3947 | 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] | 3948 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3949 | } |
| 3950 | else { |
| 3951 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | if (cleanup == NULL) |
| 3953 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3954 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3955 | VISIT(c, expr, |
| 3956 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3957 | ADDOP(c, DUP_TOP); |
| 3958 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3959 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3960 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3961 | NEXT_BLOCK(c); |
| 3962 | } |
| 3963 | 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] | 3964 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3965 | basicblock *end = compiler_new_block(c); |
| 3966 | if (end == NULL) |
| 3967 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3968 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | compiler_use_next_block(c, cleanup); |
| 3970 | ADDOP(c, ROT_TWO); |
| 3971 | ADDOP(c, POP_TOP); |
| 3972 | compiler_use_next_block(c, end); |
| 3973 | } |
| 3974 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3977 | static PyTypeObject * |
| 3978 | infer_type(expr_ty e) |
| 3979 | { |
| 3980 | switch (e->kind) { |
| 3981 | case Tuple_kind: |
| 3982 | return &PyTuple_Type; |
| 3983 | case List_kind: |
| 3984 | case ListComp_kind: |
| 3985 | return &PyList_Type; |
| 3986 | case Dict_kind: |
| 3987 | case DictComp_kind: |
| 3988 | return &PyDict_Type; |
| 3989 | case Set_kind: |
| 3990 | case SetComp_kind: |
| 3991 | return &PySet_Type; |
| 3992 | case GeneratorExp_kind: |
| 3993 | return &PyGen_Type; |
| 3994 | case Lambda_kind: |
| 3995 | return &PyFunction_Type; |
| 3996 | case JoinedStr_kind: |
| 3997 | case FormattedValue_kind: |
| 3998 | return &PyUnicode_Type; |
| 3999 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 4000 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4001 | default: |
| 4002 | return NULL; |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | static int |
| 4007 | check_caller(struct compiler *c, expr_ty e) |
| 4008 | { |
| 4009 | switch (e->kind) { |
| 4010 | case Constant_kind: |
| 4011 | case Tuple_kind: |
| 4012 | case List_kind: |
| 4013 | case ListComp_kind: |
| 4014 | case Dict_kind: |
| 4015 | case DictComp_kind: |
| 4016 | case Set_kind: |
| 4017 | case SetComp_kind: |
| 4018 | case GeneratorExp_kind: |
| 4019 | case JoinedStr_kind: |
| 4020 | case FormattedValue_kind: |
| 4021 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4022 | "perhaps you missed a comma?", |
| 4023 | infer_type(e)->tp_name); |
| 4024 | default: |
| 4025 | return 1; |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | static int |
| 4030 | check_subscripter(struct compiler *c, expr_ty e) |
| 4031 | { |
| 4032 | PyObject *v; |
| 4033 | |
| 4034 | switch (e->kind) { |
| 4035 | case Constant_kind: |
| 4036 | v = e->v.Constant.value; |
| 4037 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4038 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4039 | PyAnySet_Check(v))) |
| 4040 | { |
| 4041 | return 1; |
| 4042 | } |
| 4043 | /* fall through */ |
| 4044 | case Set_kind: |
| 4045 | case SetComp_kind: |
| 4046 | case GeneratorExp_kind: |
| 4047 | case Lambda_kind: |
| 4048 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4049 | "perhaps you missed a comma?", |
| 4050 | infer_type(e)->tp_name); |
| 4051 | default: |
| 4052 | return 1; |
| 4053 | } |
| 4054 | } |
| 4055 | |
| 4056 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4057 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4058 | { |
| 4059 | PyObject *v; |
| 4060 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4061 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4062 | if (index_type == NULL |
| 4063 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4064 | || index_type == &PySlice_Type) { |
| 4065 | return 1; |
| 4066 | } |
| 4067 | |
| 4068 | switch (e->kind) { |
| 4069 | case Constant_kind: |
| 4070 | v = e->v.Constant.value; |
| 4071 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4072 | return 1; |
| 4073 | } |
| 4074 | /* fall through */ |
| 4075 | case Tuple_kind: |
| 4076 | case List_kind: |
| 4077 | case ListComp_kind: |
| 4078 | case JoinedStr_kind: |
| 4079 | case FormattedValue_kind: |
| 4080 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4081 | "not %.200s; " |
| 4082 | "perhaps you missed a comma?", |
| 4083 | infer_type(e)->tp_name, |
| 4084 | index_type->tp_name); |
| 4085 | default: |
| 4086 | return 1; |
| 4087 | } |
| 4088 | } |
| 4089 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4090 | // 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] | 4091 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4092 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4093 | { |
| 4094 | Py_ssize_t argsl, i; |
| 4095 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4096 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4097 | |
| 4098 | /* Check that the call node is an attribute access, and that |
| 4099 | the call doesn't have keyword parameters. */ |
| 4100 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4101 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4102 | return -1; |
| 4103 | |
| 4104 | /* Check that there are no *varargs types of arguments. */ |
| 4105 | argsl = asdl_seq_LEN(args); |
| 4106 | for (i = 0; i < argsl; i++) { |
| 4107 | expr_ty elt = asdl_seq_GET(args, i); |
| 4108 | if (elt->kind == Starred_kind) { |
| 4109 | return -1; |
| 4110 | } |
| 4111 | } |
| 4112 | |
| 4113 | /* Alright, we can optimize the code. */ |
| 4114 | VISIT(c, expr, meth->v.Attribute.value); |
| 4115 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4116 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4117 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4118 | return 1; |
| 4119 | } |
| 4120 | |
| 4121 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4122 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4123 | { |
| 4124 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4125 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4126 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4127 | if (key->arg == NULL) { |
| 4128 | continue; |
| 4129 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4130 | if (forbidden_name(c, key->arg, Store)) { |
| 4131 | return -1; |
| 4132 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4133 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4134 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4135 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
| 4136 | PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg); |
| 4137 | if (msg == NULL) { |
| 4138 | return -1; |
| 4139 | } |
| 4140 | c->u->u_col_offset = other->col_offset; |
| 4141 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
| 4142 | Py_DECREF(msg); |
| 4143 | return -1; |
| 4144 | } |
| 4145 | } |
| 4146 | } |
| 4147 | return 0; |
| 4148 | } |
| 4149 | |
| 4150 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4151 | compiler_call(struct compiler *c, expr_ty e) |
| 4152 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4153 | int ret = maybe_optimize_method_call(c, e); |
| 4154 | if (ret >= 0) { |
| 4155 | return ret; |
| 4156 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4157 | if (!check_caller(c, e->v.Call.func)) { |
| 4158 | return 0; |
| 4159 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4160 | VISIT(c, expr, e->v.Call.func); |
| 4161 | return compiler_call_helper(c, 0, |
| 4162 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4163 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4166 | static int |
| 4167 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4168 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4169 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4170 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4171 | 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] | 4172 | return 1; |
| 4173 | } |
| 4174 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4175 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4176 | static int |
| 4177 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4178 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4179 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4180 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4181 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4182 | Convert the conversion char to 3 bits: |
| 4183 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4184 | !s : 001 0x1 FVC_STR |
| 4185 | !r : 010 0x2 FVC_REPR |
| 4186 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4187 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4188 | next bit is whether or not we have a format spec: |
| 4189 | yes : 100 0x4 |
| 4190 | no : 000 0x0 |
| 4191 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4192 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4193 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4194 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4195 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4196 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4197 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4198 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4199 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4200 | case 's': oparg = FVC_STR; break; |
| 4201 | case 'r': oparg = FVC_REPR; break; |
| 4202 | case 'a': oparg = FVC_ASCII; break; |
| 4203 | case -1: oparg = FVC_NONE; break; |
| 4204 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4205 | PyErr_Format(PyExc_SystemError, |
| 4206 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4207 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4208 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4209 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4210 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4211 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4212 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4213 | } |
| 4214 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4215 | /* And push our opcode and oparg */ |
| 4216 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4217 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4218 | return 1; |
| 4219 | } |
| 4220 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4221 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4222 | 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] | 4223 | { |
| 4224 | Py_ssize_t i, n = end - begin; |
| 4225 | keyword_ty kw; |
| 4226 | PyObject *keys, *key; |
| 4227 | assert(n > 0); |
| 4228 | if (n > 1) { |
| 4229 | for (i = begin; i < end; i++) { |
| 4230 | kw = asdl_seq_GET(keywords, i); |
| 4231 | VISIT(c, expr, kw->value); |
| 4232 | } |
| 4233 | keys = PyTuple_New(n); |
| 4234 | if (keys == NULL) { |
| 4235 | return 0; |
| 4236 | } |
| 4237 | for (i = begin; i < end; i++) { |
| 4238 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4239 | Py_INCREF(key); |
| 4240 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4241 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4242 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4243 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4244 | } |
| 4245 | else { |
| 4246 | /* a for loop only executes once */ |
| 4247 | for (i = begin; i < end; i++) { |
| 4248 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4249 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4250 | VISIT(c, expr, kw->value); |
| 4251 | } |
| 4252 | ADDOP_I(c, BUILD_MAP, n); |
| 4253 | } |
| 4254 | return 1; |
| 4255 | } |
| 4256 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4257 | /* shared code between compiler_call and compiler_class */ |
| 4258 | static int |
| 4259 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4260 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4261 | asdl_expr_seq *args, |
| 4262 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4263 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4264 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4265 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4266 | if (validate_keywords(c, keywords) == -1) { |
| 4267 | return 0; |
| 4268 | } |
| 4269 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4270 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4271 | nkwelts = asdl_seq_LEN(keywords); |
| 4272 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4273 | for (i = 0; i < nelts; i++) { |
| 4274 | expr_ty elt = asdl_seq_GET(args, i); |
| 4275 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4276 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4277 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4278 | } |
| 4279 | for (i = 0; i < nkwelts; i++) { |
| 4280 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4281 | if (kw->arg == NULL) { |
| 4282 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4283 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4284 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4285 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4286 | /* No * or ** args, so can use faster calling sequence */ |
| 4287 | for (i = 0; i < nelts; i++) { |
| 4288 | expr_ty elt = asdl_seq_GET(args, i); |
| 4289 | assert(elt->kind != Starred_kind); |
| 4290 | VISIT(c, expr, elt); |
| 4291 | } |
| 4292 | if (nkwelts) { |
| 4293 | PyObject *names; |
| 4294 | VISIT_SEQ(c, keyword, keywords); |
| 4295 | names = PyTuple_New(nkwelts); |
| 4296 | if (names == NULL) { |
| 4297 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4298 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4299 | for (i = 0; i < nkwelts; i++) { |
| 4300 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4301 | Py_INCREF(kw->arg); |
| 4302 | PyTuple_SET_ITEM(names, i, kw->arg); |
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 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4305 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4306 | return 1; |
| 4307 | } |
| 4308 | else { |
| 4309 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4310 | return 1; |
| 4311 | } |
| 4312 | |
| 4313 | ex_call: |
| 4314 | |
| 4315 | /* Do positional arguments. */ |
| 4316 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4317 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4318 | } |
| 4319 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4320 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4321 | return 0; |
| 4322 | } |
| 4323 | /* Then keyword arguments */ |
| 4324 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4325 | /* Has a new dict been pushed */ |
| 4326 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4327 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4328 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4329 | for (i = 0; i < nkwelts; i++) { |
| 4330 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4331 | if (kw->arg == NULL) { |
| 4332 | /* A keyword argument unpacking. */ |
| 4333 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4334 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4335 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4336 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4337 | if (have_dict) { |
| 4338 | ADDOP_I(c, DICT_MERGE, 1); |
| 4339 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4340 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4341 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4342 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4343 | if (!have_dict) { |
| 4344 | ADDOP_I(c, BUILD_MAP, 0); |
| 4345 | have_dict = 1; |
| 4346 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4347 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4348 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4349 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4350 | else { |
| 4351 | nseen++; |
| 4352 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4353 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4354 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4355 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4356 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4357 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4358 | } |
| 4359 | if (have_dict) { |
| 4360 | ADDOP_I(c, DICT_MERGE, 1); |
| 4361 | } |
| 4362 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4363 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4364 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4365 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4366 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4367 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4370 | |
| 4371 | /* List and set comprehensions and generator expressions work by creating a |
| 4372 | nested function to perform the actual iteration. This means that the |
| 4373 | iteration variables don't leak into the current scope. |
| 4374 | The defined function is called immediately following its definition, with the |
| 4375 | result of that call being the result of the expression. |
| 4376 | The LC/SC version returns the populated container, while the GE version is |
| 4377 | flagged in symtable.c as a generator, so it returns the generator object |
| 4378 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4379 | |
| 4380 | Possible cleanups: |
| 4381 | - iterate over the generator sequence instead of using recursion |
| 4382 | */ |
| 4383 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4384 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4385 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4386 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4387 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4388 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4390 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4391 | comprehension_ty gen; |
| 4392 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4393 | if (gen->is_async) { |
| 4394 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4395 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4396 | } else { |
| 4397 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4398 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | static int |
| 4403 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4404 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4405 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4406 | expr_ty elt, expr_ty val, int type) |
| 4407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | /* generate code for the iterator, then each of the ifs, |
| 4409 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | comprehension_ty gen; |
| 4412 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4413 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | start = compiler_new_block(c); |
| 4416 | skip = compiler_new_block(c); |
| 4417 | if_cleanup = compiler_new_block(c); |
| 4418 | anchor = compiler_new_block(c); |
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 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4421 | anchor == NULL) |
| 4422 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4424 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | if (gen_index == 0) { |
| 4427 | /* Receive outermost iter as an implicit argument */ |
| 4428 | c->u->u_argcount = 1; |
| 4429 | ADDOP_I(c, LOAD_FAST, 0); |
| 4430 | } |
| 4431 | else { |
| 4432 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4433 | /* Fast path for the temporary variable assignment idiom: |
| 4434 | for y in [f(x)] |
| 4435 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4436 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4437 | switch (gen->iter->kind) { |
| 4438 | case List_kind: |
| 4439 | elts = gen->iter->v.List.elts; |
| 4440 | break; |
| 4441 | case Tuple_kind: |
| 4442 | elts = gen->iter->v.Tuple.elts; |
| 4443 | break; |
| 4444 | default: |
| 4445 | elts = NULL; |
| 4446 | } |
| 4447 | if (asdl_seq_LEN(elts) == 1) { |
| 4448 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4449 | if (elt->kind != Starred_kind) { |
| 4450 | VISIT(c, expr, elt); |
| 4451 | start = NULL; |
| 4452 | } |
| 4453 | } |
| 4454 | if (start) { |
| 4455 | VISIT(c, expr, gen->iter); |
| 4456 | ADDOP(c, GET_ITER); |
| 4457 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4458 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4459 | if (start) { |
| 4460 | depth++; |
| 4461 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4462 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4463 | NEXT_BLOCK(c); |
| 4464 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4467 | /* XXX this needs to be cleaned up...a lot! */ |
| 4468 | n = asdl_seq_LEN(gen->ifs); |
| 4469 | for (i = 0; i < n; i++) { |
| 4470 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4471 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4472 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4473 | NEXT_BLOCK(c); |
| 4474 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4475 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4477 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4478 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | elt, val, type)) |
| 4480 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | /* only append after the last for generator */ |
| 4483 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4484 | /* comprehension specific code */ |
| 4485 | switch (type) { |
| 4486 | case COMP_GENEXP: |
| 4487 | VISIT(c, expr, elt); |
| 4488 | ADDOP(c, YIELD_VALUE); |
| 4489 | ADDOP(c, POP_TOP); |
| 4490 | break; |
| 4491 | case COMP_LISTCOMP: |
| 4492 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4493 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | break; |
| 4495 | case COMP_SETCOMP: |
| 4496 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4497 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4498 | break; |
| 4499 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4500 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4501 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4502 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4503 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4504 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | break; |
| 4506 | default: |
| 4507 | return 0; |
| 4508 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | compiler_use_next_block(c, skip); |
| 4511 | } |
| 4512 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4513 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4514 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4515 | compiler_use_next_block(c, anchor); |
| 4516 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4517 | |
| 4518 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
| 4521 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4522 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4523 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4524 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4525 | expr_ty elt, expr_ty val, int type) |
| 4526 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4527 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4528 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4529 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4530 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4531 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4532 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4533 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4534 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4535 | return 0; |
| 4536 | } |
| 4537 | |
| 4538 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4539 | |
| 4540 | if (gen_index == 0) { |
| 4541 | /* Receive outermost iter as an implicit argument */ |
| 4542 | c->u->u_argcount = 1; |
| 4543 | ADDOP_I(c, LOAD_FAST, 0); |
| 4544 | } |
| 4545 | else { |
| 4546 | /* Sub-iter - calculate on the fly */ |
| 4547 | VISIT(c, expr, gen->iter); |
| 4548 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4549 | } |
| 4550 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4551 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4552 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4553 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4554 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4555 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4556 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4557 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4558 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | |
| 4560 | n = asdl_seq_LEN(gen->ifs); |
| 4561 | for (i = 0; i < n; i++) { |
| 4562 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4563 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4564 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4565 | NEXT_BLOCK(c); |
| 4566 | } |
| 4567 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4568 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4569 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4570 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4571 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4572 | elt, val, type)) |
| 4573 | return 0; |
| 4574 | |
| 4575 | /* only append after the last for generator */ |
| 4576 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4577 | /* comprehension specific code */ |
| 4578 | switch (type) { |
| 4579 | case COMP_GENEXP: |
| 4580 | VISIT(c, expr, elt); |
| 4581 | ADDOP(c, YIELD_VALUE); |
| 4582 | ADDOP(c, POP_TOP); |
| 4583 | break; |
| 4584 | case COMP_LISTCOMP: |
| 4585 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4586 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4587 | break; |
| 4588 | case COMP_SETCOMP: |
| 4589 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4590 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4591 | break; |
| 4592 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4593 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4594 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4595 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4596 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4597 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4598 | break; |
| 4599 | default: |
| 4600 | return 0; |
| 4601 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4602 | } |
| 4603 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4604 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4605 | |
| 4606 | compiler_use_next_block(c, except); |
| 4607 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4608 | |
| 4609 | return 1; |
| 4610 | } |
| 4611 | |
| 4612 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4613 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4614 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4615 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4618 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4619 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4620 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4621 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4622 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4623 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4624 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4625 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4626 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4627 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4628 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
| 4633 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4634 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4635 | 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] | 4636 | compiler_error(c, "asynchronous comprehension outside of " |
| 4637 | "an asynchronous function"); |
| 4638 | goto error_in_scope; |
| 4639 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | if (type != COMP_GENEXP) { |
| 4642 | int op; |
| 4643 | switch (type) { |
| 4644 | case COMP_LISTCOMP: |
| 4645 | op = BUILD_LIST; |
| 4646 | break; |
| 4647 | case COMP_SETCOMP: |
| 4648 | op = BUILD_SET; |
| 4649 | break; |
| 4650 | case COMP_DICTCOMP: |
| 4651 | op = BUILD_MAP; |
| 4652 | break; |
| 4653 | default: |
| 4654 | PyErr_Format(PyExc_SystemError, |
| 4655 | "unknown comprehension type %d", type); |
| 4656 | goto error_in_scope; |
| 4657 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | ADDOP_I(c, op, 0); |
| 4660 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4661 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4662 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | val, type)) |
| 4664 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4666 | if (type != COMP_GENEXP) { |
| 4667 | ADDOP(c, RETURN_VALUE); |
| 4668 | } |
| 4669 | |
| 4670 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4671 | qualname = c->u->u_qualname; |
| 4672 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4674 | if (top_level_await && is_async_generator){ |
| 4675 | c->u->u_ste->ste_coroutine = 1; |
| 4676 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4677 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4678 | goto error; |
| 4679 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4680 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4681 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4682 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4683 | Py_DECREF(co); |
| 4684 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4685 | VISIT(c, expr, outermost->iter); |
| 4686 | |
| 4687 | if (outermost->is_async) { |
| 4688 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4689 | } else { |
| 4690 | ADDOP(c, GET_ITER); |
| 4691 | } |
| 4692 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4694 | |
| 4695 | if (is_async_generator && type != COMP_GENEXP) { |
| 4696 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4697 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4698 | ADDOP(c, YIELD_FROM); |
| 4699 | } |
| 4700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4701 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4702 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4704 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4705 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4706 | Py_XDECREF(co); |
| 4707 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4711 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4713 | static identifier name; |
| 4714 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4715 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4716 | if (!name) |
| 4717 | return 0; |
| 4718 | } |
| 4719 | assert(e->kind == GeneratorExp_kind); |
| 4720 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4721 | e->v.GeneratorExp.generators, |
| 4722 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4723 | } |
| 4724 | |
| 4725 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4726 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4727 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4728 | static identifier name; |
| 4729 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4730 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4731 | if (!name) |
| 4732 | return 0; |
| 4733 | } |
| 4734 | assert(e->kind == ListComp_kind); |
| 4735 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4736 | e->v.ListComp.generators, |
| 4737 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | static int |
| 4741 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4742 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4743 | static identifier name; |
| 4744 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4745 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | if (!name) |
| 4747 | return 0; |
| 4748 | } |
| 4749 | assert(e->kind == SetComp_kind); |
| 4750 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4751 | e->v.SetComp.generators, |
| 4752 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4753 | } |
| 4754 | |
| 4755 | |
| 4756 | static int |
| 4757 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | static identifier name; |
| 4760 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4761 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | if (!name) |
| 4763 | return 0; |
| 4764 | } |
| 4765 | assert(e->kind == DictComp_kind); |
| 4766 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4767 | e->v.DictComp.generators, |
| 4768 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4773 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4775 | VISIT(c, expr, k->value); |
| 4776 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4779 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4780 | whether they are true or false. |
| 4781 | |
| 4782 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4783 | */ |
| 4784 | |
| 4785 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4786 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4787 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4788 | if (e->kind == Constant_kind) { |
| 4789 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4790 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4791 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4792 | } |
| 4793 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4794 | static int |
| 4795 | compiler_with_except_finish(struct compiler *c) { |
| 4796 | basicblock *exit; |
| 4797 | exit = compiler_new_block(c); |
| 4798 | if (exit == NULL) |
| 4799 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4800 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4801 | ADDOP(c, RERAISE); |
| 4802 | compiler_use_next_block(c, exit); |
| 4803 | ADDOP(c, POP_TOP); |
| 4804 | ADDOP(c, POP_TOP); |
| 4805 | ADDOP(c, POP_TOP); |
| 4806 | ADDOP(c, POP_EXCEPT); |
| 4807 | ADDOP(c, POP_TOP); |
| 4808 | return 1; |
| 4809 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4810 | |
| 4811 | /* |
| 4812 | Implements the async with statement. |
| 4813 | |
| 4814 | The semantics outlined in that PEP are as follows: |
| 4815 | |
| 4816 | async with EXPR as VAR: |
| 4817 | BLOCK |
| 4818 | |
| 4819 | It is implemented roughly as: |
| 4820 | |
| 4821 | context = EXPR |
| 4822 | exit = context.__aexit__ # not calling it |
| 4823 | value = await context.__aenter__() |
| 4824 | try: |
| 4825 | VAR = value # if VAR present in the syntax |
| 4826 | BLOCK |
| 4827 | finally: |
| 4828 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4829 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4830 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4831 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4832 | if not (await exit(*exc)): |
| 4833 | raise |
| 4834 | */ |
| 4835 | static int |
| 4836 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4837 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4838 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4839 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4840 | |
| 4841 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4842 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4843 | c->u->u_ste->ste_coroutine = 1; |
| 4844 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4845 | return compiler_error(c, "'async with' outside async function"); |
| 4846 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4847 | |
| 4848 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4849 | final = compiler_new_block(c); |
| 4850 | exit = compiler_new_block(c); |
| 4851 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4852 | return 0; |
| 4853 | |
| 4854 | /* Evaluate EXPR */ |
| 4855 | VISIT(c, expr, item->context_expr); |
| 4856 | |
| 4857 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4858 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4859 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4860 | ADDOP(c, YIELD_FROM); |
| 4861 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4862 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4863 | |
| 4864 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4865 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4866 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4867 | return 0; |
| 4868 | } |
| 4869 | |
| 4870 | if (item->optional_vars) { |
| 4871 | VISIT(c, expr, item->optional_vars); |
| 4872 | } |
| 4873 | else { |
| 4874 | /* Discard result from context.__aenter__() */ |
| 4875 | ADDOP(c, POP_TOP); |
| 4876 | } |
| 4877 | |
| 4878 | pos++; |
| 4879 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4880 | /* BLOCK code */ |
| 4881 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4882 | else if (!compiler_async_with(c, s, pos)) |
| 4883 | return 0; |
| 4884 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4885 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4886 | ADDOP(c, POP_BLOCK); |
| 4887 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4888 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4889 | /* For successful outcome: |
| 4890 | * call __exit__(None, None, None) |
| 4891 | */ |
| 4892 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4893 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4894 | ADDOP(c, GET_AWAITABLE); |
| 4895 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4896 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4897 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4898 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4899 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4900 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4901 | |
| 4902 | /* For exceptional outcome: */ |
| 4903 | compiler_use_next_block(c, final); |
| 4904 | |
| 4905 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4906 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4907 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4908 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4909 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4910 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4911 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4912 | return 1; |
| 4913 | } |
| 4914 | |
| 4915 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4916 | /* |
| 4917 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4918 | with EXPR as VAR: |
| 4919 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4920 | is implemented as: |
| 4921 | <code for EXPR> |
| 4922 | SETUP_WITH E |
| 4923 | <code to store to VAR> or POP_TOP |
| 4924 | <code for BLOCK> |
| 4925 | LOAD_CONST (None, None, None) |
| 4926 | CALL_FUNCTION_EX 0 |
| 4927 | JUMP_FORWARD EXIT |
| 4928 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4929 | POP_JUMP_IF_TRUE T: |
| 4930 | RERAISE |
| 4931 | T: POP_TOP * 3 (remove exception from stack) |
| 4932 | POP_EXCEPT |
| 4933 | POP_TOP |
| 4934 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4935 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4936 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4937 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4938 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4939 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4940 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4941 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4942 | |
| 4943 | assert(s->kind == With_kind); |
| 4944 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4945 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4946 | final = compiler_new_block(c); |
| 4947 | exit = compiler_new_block(c); |
| 4948 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4949 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4950 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4951 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4952 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4953 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4954 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4955 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4956 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4957 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4958 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4959 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4960 | } |
| 4961 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4962 | if (item->optional_vars) { |
| 4963 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4964 | } |
| 4965 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4966 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4967 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4968 | } |
| 4969 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4970 | pos++; |
| 4971 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4972 | /* BLOCK code */ |
| 4973 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4974 | else if (!compiler_with(c, s, pos)) |
| 4975 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4976 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4977 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4978 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4979 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4980 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4981 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4982 | /* For successful outcome: |
| 4983 | * call __exit__(None, None, None) |
| 4984 | */ |
| 4985 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4986 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4987 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4988 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4989 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4990 | /* For exceptional outcome: */ |
| 4991 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4992 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4993 | ADDOP(c, WITH_EXCEPT_START); |
| 4994 | compiler_with_except_finish(c); |
| 4995 | |
| 4996 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4997 | return 1; |
| 4998 | } |
| 4999 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5000 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5001 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5003 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 5004 | case NamedExpr_kind: |
| 5005 | VISIT(c, expr, e->v.NamedExpr.value); |
| 5006 | ADDOP(c, DUP_TOP); |
| 5007 | VISIT(c, expr, e->v.NamedExpr.target); |
| 5008 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | case BoolOp_kind: |
| 5010 | return compiler_boolop(c, e); |
| 5011 | case BinOp_kind: |
| 5012 | VISIT(c, expr, e->v.BinOp.left); |
| 5013 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5014 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | break; |
| 5016 | case UnaryOp_kind: |
| 5017 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5018 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5019 | break; |
| 5020 | case Lambda_kind: |
| 5021 | return compiler_lambda(c, e); |
| 5022 | case IfExp_kind: |
| 5023 | return compiler_ifexp(c, e); |
| 5024 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5025 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5026 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5027 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | case GeneratorExp_kind: |
| 5029 | return compiler_genexp(c, e); |
| 5030 | case ListComp_kind: |
| 5031 | return compiler_listcomp(c, e); |
| 5032 | case SetComp_kind: |
| 5033 | return compiler_setcomp(c, e); |
| 5034 | case DictComp_kind: |
| 5035 | return compiler_dictcomp(c, e); |
| 5036 | case Yield_kind: |
| 5037 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5038 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5039 | if (e->v.Yield.value) { |
| 5040 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | } |
| 5042 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5043 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5044 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5045 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5046 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5047 | case YieldFrom_kind: |
| 5048 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5049 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5050 | |
| 5051 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5052 | return compiler_error(c, "'yield from' inside async function"); |
| 5053 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5054 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5055 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5056 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5057 | ADDOP(c, YIELD_FROM); |
| 5058 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5059 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5060 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5061 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5062 | return compiler_error(c, "'await' outside function"); |
| 5063 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5064 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5065 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5066 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5067 | return compiler_error(c, "'await' outside async function"); |
| 5068 | } |
| 5069 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5070 | |
| 5071 | VISIT(c, expr, e->v.Await.value); |
| 5072 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5073 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5074 | ADDOP(c, YIELD_FROM); |
| 5075 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5076 | case Compare_kind: |
| 5077 | return compiler_compare(c, e); |
| 5078 | case Call_kind: |
| 5079 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5080 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5081 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5082 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5083 | case JoinedStr_kind: |
| 5084 | return compiler_joined_str(c, e); |
| 5085 | case FormattedValue_kind: |
| 5086 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | /* The following exprs can be assignment targets. */ |
| 5088 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5089 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5090 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5091 | case Load: |
| 5092 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5093 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5095 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5096 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5097 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5098 | break; |
| 5099 | case Del: |
| 5100 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5101 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | } |
| 5103 | break; |
| 5104 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5105 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | case Starred_kind: |
| 5107 | switch (e->v.Starred.ctx) { |
| 5108 | case Store: |
| 5109 | /* In all legitimate cases, the Starred node was already replaced |
| 5110 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5111 | return compiler_error(c, |
| 5112 | "starred assignment target must be in a list or tuple"); |
| 5113 | default: |
| 5114 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5115 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5116 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5117 | break; |
| 5118 | case Slice_kind: |
| 5119 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5120 | case Name_kind: |
| 5121 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5122 | /* child nodes of List and Tuple will have expr_context set */ |
| 5123 | case List_kind: |
| 5124 | return compiler_list(c, e); |
| 5125 | case Tuple_kind: |
| 5126 | return compiler_tuple(c, e); |
| 5127 | } |
| 5128 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5132 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5133 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5134 | int old_lineno = c->u->u_lineno; |
| 5135 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5136 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5137 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5138 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5139 | c->u->u_col_offset = old_col_offset; |
| 5140 | return res; |
| 5141 | } |
| 5142 | |
| 5143 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5144 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5146 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5147 | expr_ty e = s->v.AugAssign.target; |
| 5148 | |
| 5149 | int old_lineno = c->u->u_lineno; |
| 5150 | int old_col_offset = c->u->u_col_offset; |
| 5151 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5153 | switch (e->kind) { |
| 5154 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5155 | VISIT(c, expr, e->v.Attribute.value); |
| 5156 | ADDOP(c, DUP_TOP); |
| 5157 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5158 | break; |
| 5159 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5160 | VISIT(c, expr, e->v.Subscript.value); |
| 5161 | VISIT(c, expr, e->v.Subscript.slice); |
| 5162 | ADDOP(c, DUP_TOP_TWO); |
| 5163 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | case Name_kind: |
| 5166 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5167 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5168 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5169 | default: |
| 5170 | PyErr_Format(PyExc_SystemError, |
| 5171 | "invalid node type (%d) for augmented assignment", |
| 5172 | e->kind); |
| 5173 | return 0; |
| 5174 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5175 | |
| 5176 | c->u->u_lineno = old_lineno; |
| 5177 | c->u->u_col_offset = old_col_offset; |
| 5178 | |
| 5179 | VISIT(c, expr, s->v.AugAssign.value); |
| 5180 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5181 | |
| 5182 | SET_LOC(c, e); |
| 5183 | |
| 5184 | switch (e->kind) { |
| 5185 | case Attribute_kind: |
| 5186 | ADDOP(c, ROT_TWO); |
| 5187 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5188 | break; |
| 5189 | case Subscript_kind: |
| 5190 | ADDOP(c, ROT_THREE); |
| 5191 | ADDOP(c, STORE_SUBSCR); |
| 5192 | break; |
| 5193 | case Name_kind: |
| 5194 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5195 | default: |
| 5196 | Py_UNREACHABLE(); |
| 5197 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5198 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5202 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5203 | { |
| 5204 | VISIT(c, expr, e); |
| 5205 | ADDOP(c, POP_TOP); |
| 5206 | return 1; |
| 5207 | } |
| 5208 | |
| 5209 | static int |
| 5210 | check_annotation(struct compiler *c, stmt_ty s) |
| 5211 | { |
| 5212 | /* Annotations are only evaluated in a module or class. */ |
| 5213 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5214 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5215 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5216 | } |
| 5217 | return 1; |
| 5218 | } |
| 5219 | |
| 5220 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5221 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5222 | { |
| 5223 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5224 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5225 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5226 | 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] | 5227 | return 0; |
| 5228 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5229 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5230 | return 0; |
| 5231 | } |
| 5232 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5233 | return 0; |
| 5234 | } |
| 5235 | return 1; |
| 5236 | case Tuple_kind: { |
| 5237 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5238 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5239 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5240 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5241 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5242 | return 0; |
| 5243 | } |
| 5244 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5245 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5246 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5247 | default: |
| 5248 | return check_ann_expr(c, e); |
| 5249 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | static int |
| 5253 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5254 | { |
| 5255 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5256 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5257 | |
| 5258 | assert(s->kind == AnnAssign_kind); |
| 5259 | |
| 5260 | /* We perform the actual assignment first. */ |
| 5261 | if (s->v.AnnAssign.value) { |
| 5262 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5263 | VISIT(c, expr, targ); |
| 5264 | } |
| 5265 | switch (targ->kind) { |
| 5266 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5267 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5268 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5269 | /* If we have a simple name in a module or class, store annotation. */ |
| 5270 | if (s->v.AnnAssign.simple && |
| 5271 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5272 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5273 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5274 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5275 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5276 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5277 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5278 | } |
| 5279 | break; |
| 5280 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5281 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5282 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5283 | if (!s->v.AnnAssign.value && |
| 5284 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5285 | return 0; |
| 5286 | } |
| 5287 | break; |
| 5288 | case Subscript_kind: |
| 5289 | if (!s->v.AnnAssign.value && |
| 5290 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5291 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5292 | return 0; |
| 5293 | } |
| 5294 | break; |
| 5295 | default: |
| 5296 | PyErr_Format(PyExc_SystemError, |
| 5297 | "invalid node type (%d) for annotated assignment", |
| 5298 | targ->kind); |
| 5299 | return 0; |
| 5300 | } |
| 5301 | /* Annotation is evaluated last. */ |
| 5302 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5303 | return 0; |
| 5304 | } |
| 5305 | return 1; |
| 5306 | } |
| 5307 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5308 | /* Raises a SyntaxError and returns 0. |
| 5309 | If something goes wrong, a different exception may be raised. |
| 5310 | */ |
| 5311 | |
| 5312 | static int |
| 5313 | compiler_error(struct compiler *c, const char *errstr) |
| 5314 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5315 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5316 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5317 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5318 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5319 | if (!loc) { |
| 5320 | Py_INCREF(Py_None); |
| 5321 | loc = Py_None; |
| 5322 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5323 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5324 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5325 | if (!u) |
| 5326 | goto exit; |
| 5327 | v = Py_BuildValue("(zO)", errstr, u); |
| 5328 | if (!v) |
| 5329 | goto exit; |
| 5330 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5331 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5332 | Py_DECREF(loc); |
| 5333 | Py_XDECREF(u); |
| 5334 | Py_XDECREF(v); |
| 5335 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5338 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5339 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5340 | and returns 0. |
| 5341 | */ |
| 5342 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5343 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5344 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5345 | va_list vargs; |
| 5346 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5347 | va_start(vargs, format); |
| 5348 | #else |
| 5349 | va_start(vargs); |
| 5350 | #endif |
| 5351 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5352 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5353 | if (msg == NULL) { |
| 5354 | return 0; |
| 5355 | } |
| 5356 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5357 | c->u->u_lineno, NULL, NULL) < 0) |
| 5358 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5359 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5360 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5361 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5362 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5363 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5364 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5365 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5366 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5367 | return 0; |
| 5368 | } |
| 5369 | Py_DECREF(msg); |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5373 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5374 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5375 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5376 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5377 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5378 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5379 | if (ctx == Load) { |
| 5380 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5381 | return 0; |
| 5382 | } |
| 5383 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5384 | return 0; |
| 5385 | } |
| 5386 | } |
| 5387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5388 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5389 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | case Store: op = STORE_SUBSCR; break; |
| 5391 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5393 | assert(op); |
| 5394 | VISIT(c, expr, e->v.Subscript.value); |
| 5395 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5396 | ADDOP(c, op); |
| 5397 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
| 5400 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5401 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | int n = 2; |
| 5404 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5406 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5407 | if (s->v.Slice.lower) { |
| 5408 | VISIT(c, expr, s->v.Slice.lower); |
| 5409 | } |
| 5410 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5411 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5412 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5413 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | if (s->v.Slice.upper) { |
| 5415 | VISIT(c, expr, s->v.Slice.upper); |
| 5416 | } |
| 5417 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5418 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | if (s->v.Slice.step) { |
| 5422 | n++; |
| 5423 | VISIT(c, expr, s->v.Slice.step); |
| 5424 | } |
| 5425 | ADDOP_I(c, BUILD_SLICE, n); |
| 5426 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5427 | } |
| 5428 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5429 | /* End of the compiler section, beginning of the assembler section */ |
| 5430 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5431 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5432 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5433 | |
| 5434 | XXX must handle implicit jumps from one block to next |
| 5435 | */ |
| 5436 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5437 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5438 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5439 | int a_offset; /* offset into bytecode */ |
| 5440 | int a_nblocks; /* number of reachable blocks */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5441 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5442 | int a_lnotab_off; /* offset into lnotab */ |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5443 | int a_prevlineno; /* lineno of last emitted line in line table */ |
| 5444 | int a_lineno; /* lineno of last emitted instruction */ |
| 5445 | int a_lineno_start; /* bytecode start offset of current lineno */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5446 | basicblock *a_entry; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5447 | }; |
| 5448 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5449 | Py_LOCAL_INLINE(void) |
| 5450 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5451 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5452 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5453 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5454 | assert(b->b_startdepth < 0); |
| 5455 | b->b_startdepth = depth; |
| 5456 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5457 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5458 | } |
| 5459 | |
| 5460 | /* Find the flow path that needs the largest stack. We assume that |
| 5461 | * cycles in the flow graph have no net effect on the stack depth. |
| 5462 | */ |
| 5463 | static int |
| 5464 | stackdepth(struct compiler *c) |
| 5465 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5466 | basicblock *b, *entryblock = NULL; |
| 5467 | basicblock **stack, **sp; |
| 5468 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5470 | b->b_startdepth = INT_MIN; |
| 5471 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5472 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5473 | } |
| 5474 | if (!entryblock) |
| 5475 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5476 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5477 | if (!stack) { |
| 5478 | PyErr_NoMemory(); |
| 5479 | return -1; |
| 5480 | } |
| 5481 | |
| 5482 | sp = stack; |
| 5483 | stackdepth_push(&sp, entryblock, 0); |
| 5484 | while (sp != stack) { |
| 5485 | b = *--sp; |
| 5486 | int depth = b->b_startdepth; |
| 5487 | assert(depth >= 0); |
| 5488 | basicblock *next = b->b_next; |
| 5489 | for (int i = 0; i < b->b_iused; i++) { |
| 5490 | struct instr *instr = &b->b_instr[i]; |
| 5491 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5492 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 5493 | _Py_FatalErrorFormat(__func__, |
| 5494 | "opcode = %d", instr->i_opcode); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5495 | } |
| 5496 | int new_depth = depth + effect; |
| 5497 | if (new_depth > maxdepth) { |
| 5498 | maxdepth = new_depth; |
| 5499 | } |
| 5500 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5501 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5502 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5503 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5504 | int target_depth = depth + effect; |
| 5505 | if (target_depth > maxdepth) { |
| 5506 | maxdepth = target_depth; |
| 5507 | } |
| 5508 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5509 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5510 | } |
| 5511 | depth = new_depth; |
| 5512 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5513 | instr->i_opcode == JUMP_FORWARD || |
| 5514 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5515 | instr->i_opcode == RAISE_VARARGS || |
| 5516 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5517 | { |
| 5518 | /* remaining code is dead */ |
| 5519 | next = NULL; |
| 5520 | break; |
| 5521 | } |
| 5522 | } |
| 5523 | if (next != NULL) { |
| 5524 | stackdepth_push(&sp, next, depth); |
| 5525 | } |
| 5526 | } |
| 5527 | PyObject_Free(stack); |
| 5528 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5529 | } |
| 5530 | |
| 5531 | static int |
| 5532 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5534 | memset(a, 0, sizeof(struct assembler)); |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5535 | a->a_prevlineno = a->a_lineno = firstlineno; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5536 | a->a_lnotab = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5537 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5538 | if (a->a_bytecode == NULL) { |
| 5539 | goto error; |
| 5540 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5541 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5542 | if (a->a_lnotab == NULL) { |
| 5543 | goto error; |
| 5544 | } |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5545 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5546 | PyErr_NoMemory(); |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5547 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5549 | return 1; |
Mark Shannon | fd009e6 | 2020-11-13 12:53:53 +0000 | [diff] [blame] | 5550 | error: |
| 5551 | Py_XDECREF(a->a_bytecode); |
| 5552 | Py_XDECREF(a->a_lnotab); |
| 5553 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5554 | } |
| 5555 | |
| 5556 | static void |
| 5557 | assemble_free(struct assembler *a) |
| 5558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5559 | Py_XDECREF(a->a_bytecode); |
| 5560 | Py_XDECREF(a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5563 | static int |
| 5564 | blocksize(basicblock *b) |
| 5565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | int i; |
| 5567 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5570 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5571 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5574 | static int |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5575 | assemble_emit_linetable_pair(struct assembler *a, int bdelta, int ldelta) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5576 | { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5577 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_lnotab); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | if (a->a_lnotab_off + 2 >= len) { |
| 5579 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5580 | return 0; |
| 5581 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5582 | unsigned char *lnotab = (unsigned char *) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5583 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | a->a_lnotab_off += 2; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5586 | *lnotab++ = bdelta; |
| 5587 | *lnotab++ = ldelta; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5588 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5589 | } |
| 5590 | |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5591 | /* Appends a range to the end of the line number table. See |
| 5592 | * Objects/lnotab_notes.txt for the description of the line number table. */ |
| 5593 | |
| 5594 | static int |
| 5595 | assemble_line_range(struct assembler *a) |
| 5596 | { |
| 5597 | int ldelta, bdelta; |
| 5598 | bdelta = (a->a_offset - a->a_lineno_start) * 2; |
| 5599 | if (bdelta == 0) { |
| 5600 | return 1; |
| 5601 | } |
| 5602 | if (a->a_lineno < 0) { |
| 5603 | ldelta = -128; |
| 5604 | } |
| 5605 | else { |
| 5606 | ldelta = a->a_lineno - a->a_prevlineno; |
| 5607 | a->a_prevlineno = a->a_lineno; |
| 5608 | while (ldelta > 127) { |
| 5609 | if (!assemble_emit_linetable_pair(a, 0, 127)) { |
| 5610 | return 0; |
| 5611 | } |
| 5612 | ldelta -= 127; |
| 5613 | } |
| 5614 | while (ldelta < -127) { |
| 5615 | if (!assemble_emit_linetable_pair(a, 0, -127)) { |
| 5616 | return 0; |
| 5617 | } |
| 5618 | ldelta += 127; |
| 5619 | } |
| 5620 | } |
| 5621 | assert(-128 <= ldelta && ldelta < 128); |
| 5622 | while (bdelta > 254) { |
| 5623 | if (!assemble_emit_linetable_pair(a, 254, ldelta)) { |
| 5624 | return 0; |
| 5625 | } |
| 5626 | ldelta = a->a_lineno < 0 ? -128 : 0; |
| 5627 | bdelta -= 254; |
| 5628 | } |
| 5629 | if (!assemble_emit_linetable_pair(a, bdelta, ldelta)) { |
| 5630 | return 0; |
| 5631 | } |
| 5632 | a->a_lineno_start = a->a_offset; |
| 5633 | return 1; |
| 5634 | } |
| 5635 | |
| 5636 | static int |
| 5637 | assemble_lnotab(struct assembler *a, struct instr *i) |
| 5638 | { |
| 5639 | if (i->i_lineno == a->a_lineno) { |
| 5640 | return 1; |
| 5641 | } |
| 5642 | if (!assemble_line_range(a)) { |
| 5643 | return 0; |
| 5644 | } |
| 5645 | a->a_lineno = i->i_lineno; |
| 5646 | return 1; |
| 5647 | } |
| 5648 | |
| 5649 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5650 | /* assemble_emit() |
| 5651 | Extend the bytecode with a new instruction. |
| 5652 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5653 | */ |
| 5654 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5655 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5656 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5657 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5658 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5660 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5661 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5662 | arg = i->i_oparg; |
| 5663 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5664 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5665 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5666 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5667 | if (len > PY_SSIZE_T_MAX / 2) |
| 5668 | return 0; |
| 5669 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5670 | return 0; |
| 5671 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5672 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5673 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5674 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5675 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5676 | } |
| 5677 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5678 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5679 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5682 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5684 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5685 | /* Compute the size of each block and fixup jump args. |
| 5686 | Replace block pointer with position in bytecode. */ |
| 5687 | do { |
| 5688 | totsize = 0; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5689 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | bsize = blocksize(b); |
| 5691 | b->b_offset = totsize; |
| 5692 | totsize += bsize; |
| 5693 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5694 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5695 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5696 | bsize = b->b_offset; |
| 5697 | for (i = 0; i < b->b_iused; i++) { |
| 5698 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5699 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5700 | /* Relative jumps are computed relative to |
| 5701 | the instruction pointer after fetching |
| 5702 | the jump instruction. |
| 5703 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5704 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5705 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5707 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5708 | instr->i_oparg -= bsize; |
| 5709 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5710 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5711 | if (instrsize(instr->i_oparg) != isize) { |
| 5712 | extended_arg_recompile = 1; |
| 5713 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5714 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | } |
| 5716 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | /* XXX: This is an awful hack that could hurt performance, but |
| 5719 | on the bright side it should work until we come up |
| 5720 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5721 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5722 | The issue is that in the first loop blocksize() is called |
| 5723 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5724 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5725 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5727 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5728 | The only EXTENDED_ARGs that could be popping up are |
| 5729 | ones in jump instructions. So this should converge |
| 5730 | fairly quickly. |
| 5731 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5732 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5733 | } |
| 5734 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5735 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5736 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5738 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5739 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5741 | tuple = PyTuple_New(size); |
| 5742 | if (tuple == NULL) |
| 5743 | return NULL; |
| 5744 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5745 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5746 | Py_INCREF(k); |
| 5747 | assert((i - offset) < size); |
| 5748 | assert((i - offset) >= 0); |
| 5749 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5750 | } |
| 5751 | return tuple; |
| 5752 | } |
| 5753 | |
| 5754 | static PyObject * |
| 5755 | consts_dict_keys_inorder(PyObject *dict) |
| 5756 | { |
| 5757 | PyObject *consts, *k, *v; |
| 5758 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5759 | |
| 5760 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5761 | if (consts == NULL) |
| 5762 | return NULL; |
| 5763 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5764 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5765 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5766 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5767 | * the object we want is always second. */ |
| 5768 | if (PyTuple_CheckExact(k)) { |
| 5769 | k = PyTuple_GET_ITEM(k, 1); |
| 5770 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5771 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5772 | assert(i < size); |
| 5773 | assert(i >= 0); |
| 5774 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5775 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5776 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5777 | } |
| 5778 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5779 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5780 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5781 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5782 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5783 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5784 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5785 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5786 | if (ste->ste_nested) |
| 5787 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5788 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5789 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5790 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5791 | flags |= CO_COROUTINE; |
| 5792 | if (ste->ste_generator && ste->ste_coroutine) |
| 5793 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5794 | if (ste->ste_varargs) |
| 5795 | flags |= CO_VARARGS; |
| 5796 | if (ste->ste_varkeywords) |
| 5797 | flags |= CO_VARKEYWORDS; |
| 5798 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5800 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5801 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5802 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5803 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5804 | ste->ste_coroutine && |
| 5805 | !ste->ste_generator) { |
| 5806 | flags |= CO_COROUTINE; |
| 5807 | } |
| 5808 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5809 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5812 | // Merge *tuple* with constant cache. |
| 5813 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5814 | static int |
| 5815 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5816 | { |
| 5817 | assert(PyTuple_CheckExact(*tuple)); |
| 5818 | |
| 5819 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5820 | if (key == NULL) { |
| 5821 | return 0; |
| 5822 | } |
| 5823 | |
| 5824 | // t is borrowed reference |
| 5825 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5826 | Py_DECREF(key); |
| 5827 | if (t == NULL) { |
| 5828 | return 0; |
| 5829 | } |
| 5830 | if (t == key) { // tuple is new constant. |
| 5831 | return 1; |
| 5832 | } |
| 5833 | |
| 5834 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5835 | Py_INCREF(u); |
| 5836 | Py_DECREF(*tuple); |
| 5837 | *tuple = u; |
| 5838 | return 1; |
| 5839 | } |
| 5840 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5841 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5842 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5843 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5845 | PyObject *names = NULL; |
| 5846 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5847 | PyObject *name = NULL; |
| 5848 | PyObject *freevars = NULL; |
| 5849 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5850 | Py_ssize_t nlocals; |
| 5851 | int nlocals_int; |
| 5852 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5853 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5855 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5856 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5857 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5858 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5859 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5860 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5861 | if (!cellvars) |
| 5862 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5863 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5864 | if (!freevars) |
| 5865 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5866 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5867 | if (!merge_const_tuple(c, &names) || |
| 5868 | !merge_const_tuple(c, &varnames) || |
| 5869 | !merge_const_tuple(c, &cellvars) || |
| 5870 | !merge_const_tuple(c, &freevars)) |
| 5871 | { |
| 5872 | goto error; |
| 5873 | } |
| 5874 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5875 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5876 | assert(nlocals < INT_MAX); |
| 5877 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5879 | flags = compute_code_flags(c); |
| 5880 | if (flags < 0) |
| 5881 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5882 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5883 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5884 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5886 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5887 | if (!merge_const_tuple(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5888 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5889 | goto error; |
| 5890 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5891 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5892 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5893 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5894 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5895 | maxdepth = stackdepth(c); |
| 5896 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5897 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5898 | goto error; |
| 5899 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5900 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5901 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5902 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5903 | varnames, freevars, cellvars, c->c_filename, |
| 5904 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5905 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5906 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5907 | Py_XDECREF(names); |
| 5908 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5909 | Py_XDECREF(name); |
| 5910 | Py_XDECREF(freevars); |
| 5911 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5912 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5915 | |
| 5916 | /* For debugging purposes only */ |
| 5917 | #if 0 |
| 5918 | static void |
| 5919 | dump_instr(const struct instr *i) |
| 5920 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5921 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 5922 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5923 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5925 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5926 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5927 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5928 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5929 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5930 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5931 | } |
| 5932 | |
| 5933 | static void |
| 5934 | dump_basicblock(const basicblock *b) |
| 5935 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5937 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 5938 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5939 | if (b->b_instr) { |
| 5940 | int i; |
| 5941 | for (i = 0; i < b->b_iused; i++) { |
| 5942 | fprintf(stderr, " [%02d] ", i); |
| 5943 | dump_instr(b->b_instr + i); |
| 5944 | } |
| 5945 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5946 | } |
| 5947 | #endif |
| 5948 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5949 | static int |
| 5950 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 5951 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5952 | static PyCodeObject * |
| 5953 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5954 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5955 | basicblock *b, *entryblock; |
| 5956 | struct assembler a; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5957 | int j, nblocks; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5958 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5959 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5961 | /* Make sure every block that falls off the end returns None. |
| 5962 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5963 | block ends with a jump or return b_next shouldn't set. |
| 5964 | */ |
| 5965 | if (!c->u->u_curblock->b_return) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5966 | c->u->u_lineno = -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5968 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | ADDOP(c, RETURN_VALUE); |
| 5970 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5972 | nblocks = 0; |
| 5973 | entryblock = NULL; |
| 5974 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5975 | nblocks++; |
| 5976 | entryblock = b; |
| 5977 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5979 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5980 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5981 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5982 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 5983 | else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5984 | c->u->u_firstlineno = 1; |
| 5985 | } |
| 5986 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5987 | goto error; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 5988 | a.a_entry = entryblock; |
| 5989 | a.a_nblocks = nblocks; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5990 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5991 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 5992 | if (consts == NULL) { |
| 5993 | goto error; |
| 5994 | } |
| 5995 | if (optimize_cfg(&a, consts)) { |
| 5996 | goto error; |
| 5997 | } |
| 5998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5999 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6000 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6001 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6002 | /* Emit code. */ |
| 6003 | for(b = entryblock; b != NULL; b = b->b_next) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6004 | for (j = 0; j < b->b_iused; j++) |
| 6005 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6006 | goto error; |
| 6007 | } |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6008 | if (!assemble_line_range(&a)) { |
| 6009 | return 0; |
| 6010 | } |
| 6011 | /* Emit sentinel at end of line number table */ |
| 6012 | if (!assemble_emit_linetable_pair(&a, 255, -128)) { |
| 6013 | goto error; |
| 6014 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6016 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6017 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6018 | 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] | 6019 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6020 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6021 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6022 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6023 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6024 | assemble_free(&a); |
| 6025 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6026 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6027 | |
| 6028 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6029 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6030 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6031 | PyArena *arena) |
| 6032 | { |
| 6033 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6034 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6035 | |
| 6036 | |
| 6037 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6038 | with LOAD_CONST (c1, c2, ... cn). |
| 6039 | The consts table must still be in list form so that the |
| 6040 | new constant (c1, c2, ... cn) can be appended. |
| 6041 | Called with codestr pointing to the first LOAD_CONST. |
| 6042 | */ |
| 6043 | static int |
| 6044 | fold_tuple_on_constants(struct instr *inst, |
| 6045 | int n, PyObject *consts) |
| 6046 | { |
| 6047 | /* Pre-conditions */ |
| 6048 | assert(PyList_CheckExact(consts)); |
| 6049 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6050 | assert(inst[n].i_oparg == n); |
| 6051 | |
| 6052 | for (int i = 0; i < n; i++) { |
| 6053 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6054 | return 0; |
| 6055 | } |
| 6056 | } |
| 6057 | |
| 6058 | /* Buildup new tuple of constants */ |
| 6059 | PyObject *newconst = PyTuple_New(n); |
| 6060 | if (newconst == NULL) { |
| 6061 | return -1; |
| 6062 | } |
| 6063 | for (int i = 0; i < n; i++) { |
| 6064 | int arg = inst[i].i_oparg; |
| 6065 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6066 | Py_INCREF(constant); |
| 6067 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6068 | } |
| 6069 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6070 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6071 | Py_DECREF(newconst); |
| 6072 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6073 | return -1; |
| 6074 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6075 | if (PyList_Append(consts, newconst)) { |
| 6076 | Py_DECREF(newconst); |
| 6077 | return -1; |
| 6078 | } |
| 6079 | Py_DECREF(newconst); |
| 6080 | for (int i = 0; i < n; i++) { |
| 6081 | inst[i].i_opcode = NOP; |
| 6082 | } |
| 6083 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6084 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6085 | return 0; |
| 6086 | } |
| 6087 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6088 | /* Maximum size of basic block that should be copied in optimizer */ |
| 6089 | #define MAX_COPY_SIZE 4 |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6090 | |
| 6091 | /* Optimization */ |
| 6092 | static int |
| 6093 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6094 | { |
| 6095 | assert(PyList_CheckExact(consts)); |
| 6096 | struct instr nop; |
| 6097 | nop.i_opcode = NOP; |
| 6098 | struct instr *target; |
| 6099 | int lineno; |
| 6100 | for (int i = 0; i < bb->b_iused; i++) { |
| 6101 | struct instr *inst = &bb->b_instr[i]; |
| 6102 | int oparg = inst->i_oparg; |
| 6103 | 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] | 6104 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6105 | /* Skip over empty basic blocks. */ |
| 6106 | while (inst->i_target->b_iused == 0) { |
| 6107 | inst->i_target = inst->i_target->b_next; |
| 6108 | } |
| 6109 | target = &inst->i_target->b_instr[0]; |
| 6110 | } |
| 6111 | else { |
| 6112 | target = &nop; |
| 6113 | } |
| 6114 | switch (inst->i_opcode) { |
| 6115 | /* Skip over LOAD_CONST trueconst |
| 6116 | POP_JUMP_IF_FALSE xx. This improves |
| 6117 | "while 1" performance. */ |
| 6118 | case LOAD_CONST: |
| 6119 | if (nextop != POP_JUMP_IF_FALSE) { |
| 6120 | break; |
| 6121 | } |
| 6122 | PyObject* cnt = PyList_GET_ITEM(consts, oparg); |
| 6123 | int is_true = PyObject_IsTrue(cnt); |
| 6124 | if (is_true == -1) { |
| 6125 | goto error; |
| 6126 | } |
| 6127 | if (is_true == 1) { |
| 6128 | inst->i_opcode = NOP; |
| 6129 | bb->b_instr[i+1].i_opcode = NOP; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6130 | } |
| 6131 | break; |
| 6132 | |
| 6133 | /* Try to fold tuples of constants. |
| 6134 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6135 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6136 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6137 | case BUILD_TUPLE: |
| 6138 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6139 | switch(oparg) { |
| 6140 | case 1: |
| 6141 | inst->i_opcode = NOP; |
| 6142 | bb->b_instr[i+1].i_opcode = NOP; |
| 6143 | break; |
| 6144 | case 2: |
| 6145 | inst->i_opcode = ROT_TWO; |
| 6146 | bb->b_instr[i+1].i_opcode = NOP; |
| 6147 | break; |
| 6148 | case 3: |
| 6149 | inst->i_opcode = ROT_THREE; |
| 6150 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6151 | } |
| 6152 | break; |
| 6153 | } |
| 6154 | if (i >= oparg) { |
| 6155 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6156 | goto error; |
| 6157 | } |
| 6158 | } |
| 6159 | break; |
| 6160 | |
| 6161 | /* Simplify conditional jump to conditional jump where the |
| 6162 | result of the first test implies the success of a similar |
| 6163 | test or the failure of the opposite test. |
| 6164 | Arises in code like: |
| 6165 | "a and b or c" |
| 6166 | "(a and b) and c" |
| 6167 | "(a or b) or c" |
| 6168 | "(a or b) and c" |
| 6169 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6170 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6171 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6172 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6173 | where y+1 is the instruction following the second test. |
| 6174 | */ |
| 6175 | case JUMP_IF_FALSE_OR_POP: |
| 6176 | switch(target->i_opcode) { |
| 6177 | case POP_JUMP_IF_FALSE: |
| 6178 | *inst = *target; |
| 6179 | break; |
| 6180 | case JUMP_ABSOLUTE: |
| 6181 | case JUMP_FORWARD: |
| 6182 | case JUMP_IF_FALSE_OR_POP: |
| 6183 | inst->i_target = target->i_target; |
| 6184 | break; |
| 6185 | case JUMP_IF_TRUE_OR_POP: |
| 6186 | assert (inst->i_target->b_iused == 1); |
| 6187 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6188 | inst->i_target = inst->i_target->b_next; |
| 6189 | break; |
| 6190 | } |
| 6191 | break; |
| 6192 | |
| 6193 | case JUMP_IF_TRUE_OR_POP: |
| 6194 | switch(target->i_opcode) { |
| 6195 | case POP_JUMP_IF_TRUE: |
| 6196 | *inst = *target; |
| 6197 | break; |
| 6198 | case JUMP_ABSOLUTE: |
| 6199 | case JUMP_FORWARD: |
| 6200 | case JUMP_IF_TRUE_OR_POP: |
| 6201 | inst->i_target = target->i_target; |
| 6202 | break; |
| 6203 | case JUMP_IF_FALSE_OR_POP: |
| 6204 | assert (inst->i_target->b_iused == 1); |
| 6205 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 6206 | inst->i_target = inst->i_target->b_next; |
| 6207 | break; |
| 6208 | } |
| 6209 | break; |
| 6210 | |
| 6211 | case POP_JUMP_IF_FALSE: |
| 6212 | switch(target->i_opcode) { |
| 6213 | case JUMP_ABSOLUTE: |
| 6214 | case JUMP_FORWARD: |
| 6215 | inst->i_target = target->i_target; |
| 6216 | break; |
| 6217 | } |
| 6218 | break; |
| 6219 | |
| 6220 | case POP_JUMP_IF_TRUE: |
| 6221 | switch(target->i_opcode) { |
| 6222 | case JUMP_ABSOLUTE: |
| 6223 | case JUMP_FORWARD: |
| 6224 | inst->i_target = target->i_target; |
| 6225 | break; |
| 6226 | } |
| 6227 | break; |
| 6228 | |
| 6229 | case JUMP_ABSOLUTE: |
| 6230 | case JUMP_FORWARD: |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6231 | assert (i == bb->b_iused-1); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6232 | switch(target->i_opcode) { |
| 6233 | case JUMP_FORWARD: |
| 6234 | inst->i_target = target->i_target; |
| 6235 | break; |
| 6236 | case JUMP_ABSOLUTE: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6237 | lineno = inst->i_lineno; |
| 6238 | *inst = *target; |
| 6239 | inst->i_lineno = lineno; |
| 6240 | break; |
| 6241 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6242 | if (inst->i_target->b_exit && inst->i_target->b_iused <= MAX_COPY_SIZE) { |
| 6243 | basicblock *to_copy = inst->i_target; |
| 6244 | *inst = to_copy->b_instr[0]; |
| 6245 | for (i = 1; i < to_copy->b_iused; i++) { |
| 6246 | int index = compiler_next_instr(bb); |
| 6247 | if (index < 0) { |
| 6248 | return -1; |
| 6249 | } |
| 6250 | bb->b_instr[index] = to_copy->b_instr[i]; |
| 6251 | } |
| 6252 | bb->b_exit = 1; |
| 6253 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6254 | break; |
| 6255 | } |
| 6256 | } |
| 6257 | return 0; |
| 6258 | error: |
| 6259 | return -1; |
| 6260 | } |
| 6261 | |
| 6262 | |
| 6263 | static void |
| 6264 | clean_basic_block(basicblock *bb) { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6265 | /* Remove NOPs. */ |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6266 | int dest = 0; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6267 | int prev_lineno = -1; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6268 | for (int src = 0; src < bb->b_iused; src++) { |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6269 | int lineno = bb->b_instr[src].i_lineno; |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6270 | if (bb->b_instr[src].i_opcode == NOP) { |
| 6271 | /* Eliminate no-op if it doesn't have a line number, or |
| 6272 | * if the next instruction has same line number or no line number, or |
| 6273 | * if the previous instruction had the same line number. */ |
| 6274 | if (lineno < 0) { |
| 6275 | continue; |
| 6276 | } |
| 6277 | if (prev_lineno == lineno) { |
| 6278 | continue; |
| 6279 | } |
| 6280 | if (src < bb->b_iused - 1) { |
| 6281 | int next_lineno = bb->b_instr[src+1].i_lineno; |
| 6282 | if (next_lineno < 0 || next_lineno == lineno) { |
| 6283 | bb->b_instr[src+1].i_lineno = lineno; |
| 6284 | continue; |
Mark Shannon | 877df85 | 2020-11-12 09:43:29 +0000 | [diff] [blame] | 6285 | } |
| 6286 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6287 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6288 | if (dest != src) { |
| 6289 | bb->b_instr[dest] = bb->b_instr[src]; |
| 6290 | } |
| 6291 | dest++; |
| 6292 | prev_lineno = lineno; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6293 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6294 | assert(dest <= bb->b_iused); |
| 6295 | bb->b_iused = dest; |
| 6296 | } |
| 6297 | |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6298 | static void |
| 6299 | normalise_basic_block(basicblock *bb) { |
| 6300 | /* Remove any code following a return or re-raise, |
| 6301 | and mark those blocks as exit and/or nofallthrough. */ |
| 6302 | for (int i = 0; i < bb->b_iused; i++) { |
| 6303 | switch(bb->b_instr[i].i_opcode) { |
| 6304 | case RETURN_VALUE: |
| 6305 | case RAISE_VARARGS: |
| 6306 | case RERAISE: |
| 6307 | bb->b_iused = i+1; |
| 6308 | bb->b_exit = 1; |
| 6309 | bb->b_nofallthrough = 1; |
| 6310 | return; |
| 6311 | case JUMP_ABSOLUTE: |
| 6312 | case JUMP_FORWARD: |
| 6313 | bb->b_iused = i+1; |
| 6314 | bb->b_nofallthrough = 1; |
| 6315 | return; |
| 6316 | } |
| 6317 | } |
| 6318 | } |
| 6319 | |
| 6320 | |
| 6321 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6322 | static int |
| 6323 | mark_reachable(struct assembler *a) { |
| 6324 | basicblock **stack, **sp; |
| 6325 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 6326 | if (stack == NULL) { |
| 6327 | return -1; |
| 6328 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6329 | a->a_entry->b_reachable = 1; |
| 6330 | *sp++ = a->a_entry; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6331 | while (sp > stack) { |
| 6332 | basicblock *b = *(--sp); |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6333 | 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] | 6334 | b->b_next->b_reachable = 1; |
| 6335 | *sp++ = b->b_next; |
| 6336 | } |
| 6337 | for (int i = 0; i < b->b_iused; i++) { |
| 6338 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6339 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6340 | target = b->b_instr[i].i_target; |
| 6341 | if (target->b_reachable == 0) { |
| 6342 | target->b_reachable = 1; |
| 6343 | *sp++ = target; |
| 6344 | } |
| 6345 | } |
| 6346 | } |
| 6347 | } |
| 6348 | PyObject_Free(stack); |
| 6349 | return 0; |
| 6350 | } |
| 6351 | |
| 6352 | |
| 6353 | /* Perform basic peephole optimizations on a control flow graph. |
| 6354 | The consts object should still be in list form to allow new constants |
| 6355 | to be appended. |
| 6356 | |
| 6357 | All transformations keep the code size the same or smaller. |
| 6358 | For those that reduce size, the gaps are initially filled with |
| 6359 | NOPs. Later those NOPs are removed. |
| 6360 | */ |
| 6361 | |
| 6362 | static int |
| 6363 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 6364 | { |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6365 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6366 | normalise_basic_block(b); |
| 6367 | } |
| 6368 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6369 | if (optimize_basic_block(b, consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6370 | return -1; |
| 6371 | } |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6372 | clean_basic_block(b); |
| 6373 | assert(b->b_reachable == 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6374 | } |
| 6375 | if (mark_reachable(a)) { |
| 6376 | return -1; |
| 6377 | } |
| 6378 | /* Delete unreachable instructions */ |
Mark Shannon | cc75ab7 | 2020-11-12 19:49:33 +0000 | [diff] [blame] | 6379 | for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { |
| 6380 | if (b->b_reachable == 0) { |
| 6381 | b->b_iused = 0; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6382 | } |
| 6383 | } |
| 6384 | return 0; |
| 6385 | } |
| 6386 | |
| 6387 | /* Retained for API compatibility. |
| 6388 | * Optimization is now done in optimize_cfg */ |
| 6389 | |
| 6390 | PyObject * |
| 6391 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 6392 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 6393 | { |
| 6394 | Py_INCREF(code); |
| 6395 | return code; |
| 6396 | } |
| 6397 | |