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