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" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 26 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 27 | #include "ast.h" |
| 28 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 29 | #include "symtable.h" |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 30 | #define NEED_OPCODE_JUMP_TABLES |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 32 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 33 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 34 | #define DEFAULT_BLOCK_SIZE 16 |
| 35 | #define DEFAULT_BLOCKS 8 |
| 36 | #define DEFAULT_CODE_SIZE 128 |
| 37 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 38 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 39 | #define COMP_GENEXP 0 |
| 40 | #define COMP_LISTCOMP 1 |
| 41 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 42 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 43 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 44 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 45 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 46 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 47 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 48 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | unsigned char i_opcode; |
| 50 | int i_oparg; |
| 51 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 52 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 55 | #define LOG_BITS_PER_INT 5 |
| 56 | #define MASK_LOW_LOG_BITS 31 |
| 57 | |
| 58 | static inline int |
| 59 | is_bit_set_in_table(uint32_t *table, int bitindex) { |
| 60 | /* Is the relevant bit set in the relevant word? */ |
| 61 | /* 256 bits fit into 8 32-bits words. |
| 62 | * Word is indexed by (bitindex>>ln(size of int in bits)). |
| 63 | * Bit within word is the low bits of bitindex. |
| 64 | */ |
| 65 | uint32_t word = table[bitindex >> LOG_BITS_PER_INT]; |
| 66 | return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1; |
| 67 | } |
| 68 | |
| 69 | static inline int |
| 70 | is_relative_jump(struct instr *i) |
| 71 | { |
| 72 | return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode); |
| 73 | } |
| 74 | |
| 75 | static inline int |
| 76 | is_jump(struct instr *i) |
| 77 | { |
| 78 | return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode); |
| 79 | } |
| 80 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 81 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 82 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 83 | reverse order that the block are allocated. b_list points to the next |
| 84 | 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] | 85 | struct basicblock_ *b_list; |
| 86 | /* number of instructions used */ |
| 87 | int b_iused; |
| 88 | /* length of instruction array (b_instr) */ |
| 89 | int b_ialloc; |
| 90 | /* pointer to an array of instructions, initially NULL */ |
| 91 | struct instr *b_instr; |
| 92 | /* If b_next is non-NULL, it is a pointer to the next |
| 93 | block reached by normal control flow. */ |
| 94 | struct basicblock_ *b_next; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 96 | unsigned b_return : 1; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 97 | unsigned b_reachable : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 99 | int b_startdepth; |
| 100 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 101 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 102 | } basicblock; |
| 103 | |
| 104 | /* fblockinfo tracks the current frame block. |
| 105 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 106 | A frame block is used to handle loops, try/except, and try/finally. |
| 107 | It's called a frame block to distinguish it from a basic block in the |
| 108 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | */ |
| 110 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 111 | enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END, |
| 112 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 113 | |
| 114 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | enum fblocktype fb_type; |
| 116 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 117 | /* (optional) type-specific exit or cleanup block */ |
| 118 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 119 | /* (optional) additional information required for unwinding */ |
| 120 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 123 | enum { |
| 124 | COMPILER_SCOPE_MODULE, |
| 125 | COMPILER_SCOPE_CLASS, |
| 126 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 127 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 128 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 129 | COMPILER_SCOPE_COMPREHENSION, |
| 130 | }; |
| 131 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 132 | /* The following items change on entry and exit of code blocks. |
| 133 | They must be saved and restored when returning to a block. |
| 134 | */ |
| 135 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 139 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 140 | int u_scope_type; |
| 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | /* The following fields are dicts that map objects to |
| 143 | the index of them in co_XXX. The index is used as |
| 144 | the argument for opcodes that refer to those collections. |
| 145 | */ |
| 146 | PyObject *u_consts; /* all constants */ |
| 147 | PyObject *u_names; /* all names */ |
| 148 | PyObject *u_varnames; /* local variables */ |
| 149 | PyObject *u_cellvars; /* cell variables */ |
| 150 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 151 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 154 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 155 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 156 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | /* Pointer to the most recently allocated block. By following b_list |
| 158 | members, you can reach all early allocated blocks. */ |
| 159 | basicblock *u_blocks; |
| 160 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | int u_nfblocks; |
| 163 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | int u_firstlineno; /* the first lineno of the block */ |
| 166 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 167 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 171 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 172 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 173 | 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] | 174 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 175 | |
| 176 | Note that we don't track recursion levels during compilation - the |
| 177 | task of detecting and rejecting excessive levels of nesting is |
| 178 | handled by the symbol analysis pass. |
| 179 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 180 | */ |
| 181 | |
| 182 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 183 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | struct symtable *c_st; |
| 185 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 186 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 188 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | int c_interactive; /* true if in interactive mode */ |
| 190 | int c_nestlevel; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 191 | int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode |
| 192 | if this value is different from zero. |
| 193 | This can be used to temporarily visit |
| 194 | nodes without emitting bytecode to |
| 195 | check only errors. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 197 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 198 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | struct compiler_unit *u; /* compiler state for current block */ |
| 200 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 201 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 204 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 205 | static void compiler_free(struct compiler *); |
| 206 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 207 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 208 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 209 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 210 | static int compiler_addop_j(struct compiler *, int, basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 211 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 212 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 213 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 214 | |
| 215 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 216 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 217 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 218 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 219 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 220 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 221 | static int compiler_subscript(struct compiler *, expr_ty); |
| 222 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 223 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 224 | static int inplace_binop(operator_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 225 | 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] | 226 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 227 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 228 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 229 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 230 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 231 | static int compiler_call_helper(struct compiler *c, int n, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 232 | asdl_expr_seq *args, |
| 233 | asdl_keyword_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 234 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 235 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 236 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 237 | static int compiler_sync_comprehension_generator( |
| 238 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 239 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 240 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 241 | expr_ty elt, expr_ty val, int type); |
| 242 | |
| 243 | static int compiler_async_comprehension_generator( |
| 244 | struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 245 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 246 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 247 | expr_ty elt, expr_ty val, int type); |
| 248 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 249 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 250 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 251 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 252 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 253 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 254 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 255 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 256 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | /* Name mangling: __private becomes _classname__private. |
| 258 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 259 | PyObject *result; |
| 260 | size_t nlen, plen, ipriv; |
| 261 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 262 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 263 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 264 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | Py_INCREF(ident); |
| 266 | return ident; |
| 267 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 268 | nlen = PyUnicode_GET_LENGTH(ident); |
| 269 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | The only time a name with a dot can occur is when |
| 273 | we are compiling an import statement that has a |
| 274 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | TODO(jhylton): Decide whether we want to support |
| 277 | mangling of the module name, e.g. __M.X. |
| 278 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 279 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 280 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 281 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | Py_INCREF(ident); |
| 283 | return ident; /* Don't mangle __whatever__ */ |
| 284 | } |
| 285 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 286 | ipriv = 0; |
| 287 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 288 | ipriv++; |
| 289 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | Py_INCREF(ident); |
| 291 | return ident; /* Don't mangle if class is just underscores */ |
| 292 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 293 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 294 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 295 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 296 | PyErr_SetString(PyExc_OverflowError, |
| 297 | "private identifier too large to be mangled"); |
| 298 | return NULL; |
| 299 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 300 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 301 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 302 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 303 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 304 | |
| 305 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 306 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 307 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 308 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 309 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 310 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 311 | Py_DECREF(result); |
| 312 | return NULL; |
| 313 | } |
| 314 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 315 | Py_DECREF(result); |
| 316 | return NULL; |
| 317 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 318 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 319 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 322 | static int |
| 323 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 324 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 326 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 327 | c->c_const_cache = PyDict_New(); |
| 328 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | c->c_stack = PyList_New(0); |
| 333 | if (!c->c_stack) { |
| 334 | Py_CLEAR(c->c_const_cache); |
| 335 | return 0; |
| 336 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 342 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 343 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | struct compiler c; |
| 346 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 347 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | int merged; |
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 | if (!__doc__) { |
| 351 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 352 | if (!__doc__) |
| 353 | return NULL; |
| 354 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 355 | if (!__annotations__) { |
| 356 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 357 | if (!__annotations__) |
| 358 | return NULL; |
| 359 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | if (!compiler_init(&c)) |
| 361 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 362 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | c.c_filename = filename; |
| 364 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 365 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (c.c_future == NULL) |
| 367 | goto finally; |
| 368 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | flags = &local_flags; |
| 370 | } |
| 371 | merged = c.c_future->ff_features | flags->cf_flags; |
| 372 | c.c_future->ff_features = merged; |
| 373 | flags->cf_flags = merged; |
| 374 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 375 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 376 | c.c_nestlevel = 0; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 377 | c.c_do_not_emit_bytecode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 378 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 379 | _PyASTOptimizeState state; |
| 380 | state.optimize = c.c_optimize; |
| 381 | state.ff_features = merged; |
| 382 | |
| 383 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 384 | goto finally; |
| 385 | } |
| 386 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 387 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | if (c.c_st == NULL) { |
| 389 | if (!PyErr_Occurred()) |
| 390 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 391 | goto finally; |
| 392 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 395 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 396 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 397 | compiler_free(&c); |
| 398 | assert(co || PyErr_Occurred()); |
| 399 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 403 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 404 | int optimize, PyArena *arena) |
| 405 | { |
| 406 | PyObject *filename; |
| 407 | PyCodeObject *co; |
| 408 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 409 | if (filename == NULL) |
| 410 | return NULL; |
| 411 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 412 | Py_DECREF(filename); |
| 413 | return co; |
| 414 | |
| 415 | } |
| 416 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 417 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 418 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 419 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 420 | if (c->c_st) |
| 421 | PySymtable_Free(c->c_st); |
| 422 | if (c->c_future) |
| 423 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 424 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 425 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 429 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 430 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 431 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | Py_ssize_t i, n; |
| 433 | PyObject *v, *k; |
| 434 | PyObject *dict = PyDict_New(); |
| 435 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | n = PyList_Size(list); |
| 438 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 439 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | if (!v) { |
| 441 | Py_DECREF(dict); |
| 442 | return NULL; |
| 443 | } |
| 444 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 445 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | Py_DECREF(v); |
| 447 | Py_DECREF(dict); |
| 448 | return NULL; |
| 449 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 450 | Py_DECREF(v); |
| 451 | } |
| 452 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* Return new dict containing names from src that match scope(s). |
| 456 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 457 | 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] | 458 | 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] | 459 | values are integers, starting at offset and increasing by one for |
| 460 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 461 | */ |
| 462 | |
| 463 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 464 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 465 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 466 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 468 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 469 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 470 | assert(offset >= 0); |
| 471 | if (dest == NULL) |
| 472 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 473 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 474 | /* Sort the keys so that we have a deterministic order on the indexes |
| 475 | saved in the returned dictionary. These indexes are used as indexes |
| 476 | into the free and cell var storage. Therefore if they aren't |
| 477 | deterministic, then the generated bytecode is not deterministic. |
| 478 | */ |
| 479 | sorted_keys = PyDict_Keys(src); |
| 480 | if (sorted_keys == NULL) |
| 481 | return NULL; |
| 482 | if (PyList_Sort(sorted_keys) != 0) { |
| 483 | Py_DECREF(sorted_keys); |
| 484 | return NULL; |
| 485 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 486 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 487 | |
| 488 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | /* XXX this should probably be a macro in symtable.h */ |
| 490 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 491 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 492 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | assert(PyLong_Check(v)); |
| 494 | vi = PyLong_AS_LONG(v); |
| 495 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 498 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 500 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | Py_DECREF(dest); |
| 502 | return NULL; |
| 503 | } |
| 504 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 505 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 506 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | Py_DECREF(item); |
| 508 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | return NULL; |
| 510 | } |
| 511 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | } |
| 513 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 514 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 515 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 518 | static void |
| 519 | compiler_unit_check(struct compiler_unit *u) |
| 520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | basicblock *block; |
| 522 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 523 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 524 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 525 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | if (block->b_instr != NULL) { |
| 527 | assert(block->b_ialloc > 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 528 | assert(block->b_iused >= 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | assert(block->b_ialloc >= block->b_iused); |
| 530 | } |
| 531 | else { |
| 532 | assert (block->b_iused == 0); |
| 533 | assert (block->b_ialloc == 0); |
| 534 | } |
| 535 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | static void |
| 539 | compiler_unit_free(struct compiler_unit *u) |
| 540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 541 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | compiler_unit_check(u); |
| 544 | b = u->u_blocks; |
| 545 | while (b != NULL) { |
| 546 | if (b->b_instr) |
| 547 | PyObject_Free((void *)b->b_instr); |
| 548 | next = b->b_list; |
| 549 | PyObject_Free((void *)b); |
| 550 | b = next; |
| 551 | } |
| 552 | Py_CLEAR(u->u_ste); |
| 553 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 554 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | Py_CLEAR(u->u_consts); |
| 556 | Py_CLEAR(u->u_names); |
| 557 | Py_CLEAR(u->u_varnames); |
| 558 | Py_CLEAR(u->u_freevars); |
| 559 | Py_CLEAR(u->u_cellvars); |
| 560 | Py_CLEAR(u->u_private); |
| 561 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 565 | compiler_enter_scope(struct compiler *c, identifier name, |
| 566 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 569 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 570 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 571 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | struct compiler_unit)); |
| 573 | if (!u) { |
| 574 | PyErr_NoMemory(); |
| 575 | return 0; |
| 576 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 577 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 579 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 580 | u->u_kwonlyargcount = 0; |
| 581 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 582 | if (!u->u_ste) { |
| 583 | compiler_unit_free(u); |
| 584 | return 0; |
| 585 | } |
| 586 | Py_INCREF(name); |
| 587 | u->u_name = name; |
| 588 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 589 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 590 | if (!u->u_varnames || !u->u_cellvars) { |
| 591 | compiler_unit_free(u); |
| 592 | return 0; |
| 593 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 594 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 595 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 596 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 597 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 598 | int res; |
| 599 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 600 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 601 | name = _PyUnicode_FromId(&PyId___class__); |
| 602 | if (!name) { |
| 603 | compiler_unit_free(u); |
| 604 | return 0; |
| 605 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 606 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 607 | if (res < 0) { |
| 608 | compiler_unit_free(u); |
| 609 | return 0; |
| 610 | } |
| 611 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | 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] | 614 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | if (!u->u_freevars) { |
| 616 | compiler_unit_free(u); |
| 617 | return 0; |
| 618 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | u->u_blocks = NULL; |
| 621 | u->u_nfblocks = 0; |
| 622 | u->u_firstlineno = lineno; |
| 623 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 624 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | u->u_consts = PyDict_New(); |
| 626 | if (!u->u_consts) { |
| 627 | compiler_unit_free(u); |
| 628 | return 0; |
| 629 | } |
| 630 | u->u_names = PyDict_New(); |
| 631 | if (!u->u_names) { |
| 632 | compiler_unit_free(u); |
| 633 | return 0; |
| 634 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | /* Push the old compiler_unit on the stack. */ |
| 639 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 640 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 642 | Py_XDECREF(capsule); |
| 643 | compiler_unit_free(u); |
| 644 | return 0; |
| 645 | } |
| 646 | Py_DECREF(capsule); |
| 647 | u->u_private = c->u->u_private; |
| 648 | Py_XINCREF(u->u_private); |
| 649 | } |
| 650 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 653 | |
| 654 | block = compiler_new_block(c); |
| 655 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 657 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 659 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 660 | if (!compiler_set_qualname(c)) |
| 661 | return 0; |
| 662 | } |
| 663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 667 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | compiler_exit_scope(struct compiler *c) |
| 669 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 670 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | c->c_nestlevel--; |
| 674 | compiler_unit_free(c->u); |
| 675 | /* Restore c->u to the parent unit. */ |
| 676 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 677 | if (n >= 0) { |
| 678 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 679 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | assert(c->u); |
| 681 | /* we are deleting from a list so this really shouldn't fail */ |
| 682 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 683 | Py_FatalError("compiler_exit_scope()"); |
| 684 | compiler_unit_check(c->u); |
| 685 | } |
| 686 | else |
| 687 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 688 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 691 | static int |
| 692 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 693 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 694 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 695 | _Py_static_string(dot_locals, ".<locals>"); |
| 696 | Py_ssize_t stack_size; |
| 697 | struct compiler_unit *u = c->u; |
| 698 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 699 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 700 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 701 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 702 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 703 | if (stack_size > 1) { |
| 704 | int scope, force_global = 0; |
| 705 | struct compiler_unit *parent; |
| 706 | PyObject *mangled, *capsule; |
| 707 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 708 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 709 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 710 | assert(parent); |
| 711 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 712 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 713 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 714 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 715 | assert(u->u_name); |
| 716 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 717 | if (!mangled) |
| 718 | return 0; |
| 719 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 720 | Py_DECREF(mangled); |
| 721 | assert(scope != GLOBAL_IMPLICIT); |
| 722 | if (scope == GLOBAL_EXPLICIT) |
| 723 | force_global = 1; |
| 724 | } |
| 725 | |
| 726 | if (!force_global) { |
| 727 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 728 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 729 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 730 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 731 | if (dot_locals_str == NULL) |
| 732 | return 0; |
| 733 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 734 | if (base == NULL) |
| 735 | return 0; |
| 736 | } |
| 737 | else { |
| 738 | Py_INCREF(parent->u_qualname); |
| 739 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 740 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 741 | } |
| 742 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 743 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 744 | if (base != NULL) { |
| 745 | dot_str = _PyUnicode_FromId(&dot); |
| 746 | if (dot_str == NULL) { |
| 747 | Py_DECREF(base); |
| 748 | return 0; |
| 749 | } |
| 750 | name = PyUnicode_Concat(base, dot_str); |
| 751 | Py_DECREF(base); |
| 752 | if (name == NULL) |
| 753 | return 0; |
| 754 | PyUnicode_Append(&name, u->u_name); |
| 755 | if (name == NULL) |
| 756 | return 0; |
| 757 | } |
| 758 | else { |
| 759 | Py_INCREF(u->u_name); |
| 760 | name = u->u_name; |
| 761 | } |
| 762 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 763 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 764 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 765 | } |
| 766 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 767 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | /* Allocate a new block and return a pointer to it. |
| 769 | Returns NULL on error. |
| 770 | */ |
| 771 | |
| 772 | static basicblock * |
| 773 | compiler_new_block(struct compiler *c) |
| 774 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | basicblock *b; |
| 776 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 779 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 780 | if (b == NULL) { |
| 781 | PyErr_NoMemory(); |
| 782 | return NULL; |
| 783 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | /* Extend the singly linked list of blocks with new block. */ |
| 785 | b->b_list = u->u_blocks; |
| 786 | u->u_blocks = b; |
| 787 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | compiler_next_block(struct compiler *c) |
| 792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | basicblock *block = compiler_new_block(c); |
| 794 | if (block == NULL) |
| 795 | return NULL; |
| 796 | c->u->u_curblock->b_next = block; |
| 797 | c->u->u_curblock = block; |
| 798 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | static basicblock * |
| 802 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 803 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 804 | assert(block != NULL); |
| 805 | c->u->u_curblock->b_next = block; |
| 806 | c->u->u_curblock = block; |
| 807 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* Returns the offset of the next instruction in the current block's |
| 811 | b_instr array. Resizes the b_instr as necessary. |
| 812 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 813 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 814 | |
| 815 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 816 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 817 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | assert(b != NULL); |
| 819 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 820 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 821 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | if (b->b_instr == NULL) { |
| 823 | PyErr_NoMemory(); |
| 824 | return -1; |
| 825 | } |
| 826 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 827 | } |
| 828 | else if (b->b_iused == b->b_ialloc) { |
| 829 | struct instr *tmp; |
| 830 | size_t oldsize, newsize; |
| 831 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 832 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 833 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 834 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 835 | PyErr_NoMemory(); |
| 836 | return -1; |
| 837 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | if (newsize == 0) { |
| 840 | PyErr_NoMemory(); |
| 841 | return -1; |
| 842 | } |
| 843 | b->b_ialloc <<= 1; |
| 844 | tmp = (struct instr *)PyObject_Realloc( |
| 845 | (void *)b->b_instr, newsize); |
| 846 | if (tmp == NULL) { |
| 847 | PyErr_NoMemory(); |
| 848 | return -1; |
| 849 | } |
| 850 | b->b_instr = tmp; |
| 851 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 852 | } |
| 853 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 856 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 857 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 858 | The line number is reset in the following cases: |
| 859 | - when entering a new scope |
| 860 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 861 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 862 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 863 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 864 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 865 | #define SET_LOC(c, x) \ |
| 866 | (c)->u->u_lineno = (x)->lineno; \ |
| 867 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 868 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 869 | /* Return the stack effect of opcode with argument oparg. |
| 870 | |
| 871 | Some opcodes have different stack effect when jump to the target and |
| 872 | when not jump. The 'jump' parameter specifies the case: |
| 873 | |
| 874 | * 0 -- when not jump |
| 875 | * 1 -- when jump |
| 876 | * -1 -- maximal |
| 877 | */ |
| 878 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 879 | WITH_CLEANUP_FINISH deterministic. */ |
| 880 | static int |
| 881 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 882 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 884 | case NOP: |
| 885 | case EXTENDED_ARG: |
| 886 | return 0; |
| 887 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 888 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 889 | case POP_TOP: |
| 890 | return -1; |
| 891 | case ROT_TWO: |
| 892 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 893 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | return 0; |
| 895 | case DUP_TOP: |
| 896 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 897 | case DUP_TOP_TWO: |
| 898 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 899 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 900 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 901 | case UNARY_POSITIVE: |
| 902 | case UNARY_NEGATIVE: |
| 903 | case UNARY_NOT: |
| 904 | case UNARY_INVERT: |
| 905 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | case SET_ADD: |
| 908 | case LIST_APPEND: |
| 909 | return -1; |
| 910 | case MAP_ADD: |
| 911 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 912 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 913 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | case BINARY_POWER: |
| 915 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 916 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | case BINARY_MODULO: |
| 918 | case BINARY_ADD: |
| 919 | case BINARY_SUBTRACT: |
| 920 | case BINARY_SUBSCR: |
| 921 | case BINARY_FLOOR_DIVIDE: |
| 922 | case BINARY_TRUE_DIVIDE: |
| 923 | return -1; |
| 924 | case INPLACE_FLOOR_DIVIDE: |
| 925 | case INPLACE_TRUE_DIVIDE: |
| 926 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | case INPLACE_ADD: |
| 929 | case INPLACE_SUBTRACT: |
| 930 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 931 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | case INPLACE_MODULO: |
| 933 | return -1; |
| 934 | case STORE_SUBSCR: |
| 935 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | case DELETE_SUBSCR: |
| 937 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | case BINARY_LSHIFT: |
| 940 | case BINARY_RSHIFT: |
| 941 | case BINARY_AND: |
| 942 | case BINARY_XOR: |
| 943 | case BINARY_OR: |
| 944 | return -1; |
| 945 | case INPLACE_POWER: |
| 946 | return -1; |
| 947 | case GET_ITER: |
| 948 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | case PRINT_EXPR: |
| 951 | return -1; |
| 952 | case LOAD_BUILD_CLASS: |
| 953 | return 1; |
| 954 | case INPLACE_LSHIFT: |
| 955 | case INPLACE_RSHIFT: |
| 956 | case INPLACE_AND: |
| 957 | case INPLACE_XOR: |
| 958 | case INPLACE_OR: |
| 959 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 962 | /* 1 in the normal flow. |
| 963 | * Restore the stack position and push 6 values before jumping to |
| 964 | * the handler if an exception be raised. */ |
| 965 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | case RETURN_VALUE: |
| 967 | return -1; |
| 968 | case IMPORT_STAR: |
| 969 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 970 | case SETUP_ANNOTATIONS: |
| 971 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | case YIELD_VALUE: |
| 973 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 974 | case YIELD_FROM: |
| 975 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 976 | case POP_BLOCK: |
| 977 | return 0; |
| 978 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 979 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | case STORE_NAME: |
| 982 | return -1; |
| 983 | case DELETE_NAME: |
| 984 | return 0; |
| 985 | case UNPACK_SEQUENCE: |
| 986 | return oparg-1; |
| 987 | case UNPACK_EX: |
| 988 | return (oparg&0xFF) + (oparg>>8); |
| 989 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 990 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 991 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | case STORE_ATTR: |
| 994 | return -2; |
| 995 | case DELETE_ATTR: |
| 996 | return -1; |
| 997 | case STORE_GLOBAL: |
| 998 | return -1; |
| 999 | case DELETE_GLOBAL: |
| 1000 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1001 | case LOAD_CONST: |
| 1002 | return 1; |
| 1003 | case LOAD_NAME: |
| 1004 | return 1; |
| 1005 | case BUILD_TUPLE: |
| 1006 | case BUILD_LIST: |
| 1007 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1008 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | return 1-oparg; |
| 1010 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1011 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1012 | case BUILD_CONST_KEY_MAP: |
| 1013 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | case LOAD_ATTR: |
| 1015 | return 0; |
| 1016 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1017 | case IS_OP: |
| 1018 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1020 | case JUMP_IF_NOT_EXC_MATCH: |
| 1021 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | case IMPORT_NAME: |
| 1023 | return -1; |
| 1024 | case IMPORT_FROM: |
| 1025 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1026 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1027 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | case JUMP_ABSOLUTE: |
| 1030 | return 0; |
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 | case JUMP_IF_TRUE_OR_POP: |
| 1033 | case JUMP_IF_FALSE_OR_POP: |
| 1034 | return jump ? 0 : -1; |
| 1035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1036 | case POP_JUMP_IF_FALSE: |
| 1037 | case POP_JUMP_IF_TRUE: |
| 1038 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | case LOAD_GLOBAL: |
| 1041 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1042 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1043 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1045 | /* 0 in the normal flow. |
| 1046 | * Restore the stack position and push 6 values before jumping to |
| 1047 | * the handler if an exception be raised. */ |
| 1048 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1049 | case RERAISE: |
| 1050 | return -3; |
| 1051 | |
| 1052 | case WITH_EXCEPT_START: |
| 1053 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | case LOAD_FAST: |
| 1056 | return 1; |
| 1057 | case STORE_FAST: |
| 1058 | return -1; |
| 1059 | case DELETE_FAST: |
| 1060 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | case RAISE_VARARGS: |
| 1063 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1064 | |
| 1065 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1066 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1067 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1068 | case CALL_METHOD: |
| 1069 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1071 | return -oparg-1; |
| 1072 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1073 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1074 | case MAKE_FUNCTION: |
| 1075 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1076 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | case BUILD_SLICE: |
| 1078 | if (oparg == 3) |
| 1079 | return -2; |
| 1080 | else |
| 1081 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1082 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1083 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | case LOAD_CLOSURE: |
| 1085 | return 1; |
| 1086 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1087 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | return 1; |
| 1089 | case STORE_DEREF: |
| 1090 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1091 | case DELETE_DEREF: |
| 1092 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1093 | |
| 1094 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1095 | case GET_AWAITABLE: |
| 1096 | return 0; |
| 1097 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1098 | /* 0 in the normal flow. |
| 1099 | * Restore the stack position to the position before the result |
| 1100 | * of __aenter__ and push 6 values before jumping to the handler |
| 1101 | * if an exception be raised. */ |
| 1102 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1103 | case BEFORE_ASYNC_WITH: |
| 1104 | return 1; |
| 1105 | case GET_AITER: |
| 1106 | return 0; |
| 1107 | case GET_ANEXT: |
| 1108 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1109 | case GET_YIELD_FROM_ITER: |
| 1110 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1111 | case END_ASYNC_FOR: |
| 1112 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1113 | case FORMAT_VALUE: |
| 1114 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1115 | else 1->1. */ |
| 1116 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1117 | case LOAD_METHOD: |
| 1118 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1119 | case LOAD_ASSERTION_ERROR: |
| 1120 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1121 | case LIST_TO_TUPLE: |
| 1122 | return 0; |
| 1123 | case LIST_EXTEND: |
| 1124 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1125 | case DICT_MERGE: |
| 1126 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1127 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1129 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1131 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1134 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1135 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1136 | { |
| 1137 | return stack_effect(opcode, oparg, jump); |
| 1138 | } |
| 1139 | |
| 1140 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1141 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1142 | { |
| 1143 | return stack_effect(opcode, oparg, -1); |
| 1144 | } |
| 1145 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | /* Add an opcode with no argument. |
| 1147 | Returns 0 on failure, 1 on success. |
| 1148 | */ |
| 1149 | |
| 1150 | static int |
| 1151 | compiler_addop(struct compiler *c, int opcode) |
| 1152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | basicblock *b; |
| 1154 | struct instr *i; |
| 1155 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1156 | assert(!HAS_ARG(opcode)); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1157 | if (c->c_do_not_emit_bytecode) { |
| 1158 | return 1; |
| 1159 | } |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1160 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | if (off < 0) |
| 1162 | return 0; |
| 1163 | b = c->u->u_curblock; |
| 1164 | i = &b->b_instr[off]; |
| 1165 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1166 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | if (opcode == RETURN_VALUE) |
| 1168 | b->b_return = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1169 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1173 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1174 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1175 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1176 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1179 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1181 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1183 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1184 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1185 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | return -1; |
| 1188 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1189 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | Py_DECREF(v); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | Py_DECREF(v); |
| 1194 | } |
| 1195 | else |
| 1196 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1197 | return arg; |
| 1198 | } |
| 1199 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1200 | // Merge const *o* recursively and return constant key object. |
| 1201 | static PyObject* |
| 1202 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1203 | { |
| 1204 | // None and Ellipsis are singleton, and key is the singleton. |
| 1205 | // No need to merge object and key. |
| 1206 | if (o == Py_None || o == Py_Ellipsis) { |
| 1207 | Py_INCREF(o); |
| 1208 | return o; |
| 1209 | } |
| 1210 | |
| 1211 | PyObject *key = _PyCode_ConstantKey(o); |
| 1212 | if (key == NULL) { |
| 1213 | return NULL; |
| 1214 | } |
| 1215 | |
| 1216 | // t is borrowed reference |
| 1217 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1218 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1219 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1220 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1221 | Py_DECREF(key); |
| 1222 | return t; |
| 1223 | } |
| 1224 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1225 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1226 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1227 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1228 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1229 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1230 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1231 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1232 | PyObject *u = merge_consts_recursive(c, item); |
| 1233 | if (u == NULL) { |
| 1234 | Py_DECREF(key); |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | |
| 1238 | // See _PyCode_ConstantKey() |
| 1239 | PyObject *v; // borrowed |
| 1240 | if (PyTuple_CheckExact(u)) { |
| 1241 | v = PyTuple_GET_ITEM(u, 1); |
| 1242 | } |
| 1243 | else { |
| 1244 | v = u; |
| 1245 | } |
| 1246 | if (v != item) { |
| 1247 | Py_INCREF(v); |
| 1248 | PyTuple_SET_ITEM(o, i, v); |
| 1249 | Py_DECREF(item); |
| 1250 | } |
| 1251 | |
| 1252 | Py_DECREF(u); |
| 1253 | } |
| 1254 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1255 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1256 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1257 | // constant keys. |
| 1258 | // See _PyCode_ConstantKey() for detail. |
| 1259 | assert(PyTuple_CheckExact(key)); |
| 1260 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1261 | |
| 1262 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1263 | if (len == 0) { // empty frozenset should not be re-created. |
| 1264 | return key; |
| 1265 | } |
| 1266 | PyObject *tuple = PyTuple_New(len); |
| 1267 | if (tuple == NULL) { |
| 1268 | Py_DECREF(key); |
| 1269 | return NULL; |
| 1270 | } |
| 1271 | Py_ssize_t i = 0, pos = 0; |
| 1272 | PyObject *item; |
| 1273 | Py_hash_t hash; |
| 1274 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1275 | PyObject *k = merge_consts_recursive(c, item); |
| 1276 | if (k == NULL) { |
| 1277 | Py_DECREF(tuple); |
| 1278 | Py_DECREF(key); |
| 1279 | return NULL; |
| 1280 | } |
| 1281 | PyObject *u; |
| 1282 | if (PyTuple_CheckExact(k)) { |
| 1283 | u = PyTuple_GET_ITEM(k, 1); |
| 1284 | Py_INCREF(u); |
| 1285 | Py_DECREF(k); |
| 1286 | } |
| 1287 | else { |
| 1288 | u = k; |
| 1289 | } |
| 1290 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1291 | i++; |
| 1292 | } |
| 1293 | |
| 1294 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1295 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1296 | PyObject *new = PyFrozenSet_New(tuple); |
| 1297 | Py_DECREF(tuple); |
| 1298 | if (new == NULL) { |
| 1299 | Py_DECREF(key); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1303 | Py_DECREF(o); |
| 1304 | PyTuple_SET_ITEM(key, 1, new); |
| 1305 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1306 | |
| 1307 | return key; |
| 1308 | } |
| 1309 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1310 | static Py_ssize_t |
| 1311 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1312 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1313 | if (c->c_do_not_emit_bytecode) { |
| 1314 | return 0; |
| 1315 | } |
| 1316 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1317 | PyObject *key = merge_consts_recursive(c, o); |
| 1318 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1319 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1320 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1321 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1322 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1323 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1324 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1328 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1329 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1330 | if (c->c_do_not_emit_bytecode) { |
| 1331 | return 1; |
| 1332 | } |
| 1333 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1334 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1335 | if (arg < 0) |
| 1336 | return 0; |
| 1337 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1338 | } |
| 1339 | |
| 1340 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1341 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1342 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1343 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1344 | if (c->c_do_not_emit_bytecode) { |
| 1345 | return 1; |
| 1346 | } |
| 1347 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1348 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1349 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1350 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1351 | return compiler_addop_i(c, opcode, arg); |
| 1352 | } |
| 1353 | |
| 1354 | static int |
| 1355 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1357 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1358 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1359 | |
| 1360 | if (c->c_do_not_emit_bytecode) { |
| 1361 | return 1; |
| 1362 | } |
| 1363 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1364 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1365 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1366 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1367 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1368 | Py_DECREF(mangled); |
| 1369 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1370 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1371 | return compiler_addop_i(c, opcode, arg); |
| 1372 | } |
| 1373 | |
| 1374 | /* Add an opcode with an integer argument. |
| 1375 | Returns 0 on failure, 1 on success. |
| 1376 | */ |
| 1377 | |
| 1378 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1379 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1380 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1381 | struct instr *i; |
| 1382 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1383 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1384 | if (c->c_do_not_emit_bytecode) { |
| 1385 | return 1; |
| 1386 | } |
| 1387 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1388 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1389 | it in the C code (like Python/ceval.c). |
| 1390 | |
| 1391 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1392 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1393 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1394 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1395 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1396 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1397 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1398 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | if (off < 0) |
| 1400 | return 0; |
| 1401 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1402 | i->i_opcode = opcode; |
| 1403 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1404 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1405 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | static int |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1409 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | struct instr *i; |
| 1412 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1413 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1414 | if (c->c_do_not_emit_bytecode) { |
| 1415 | return 1; |
| 1416 | } |
| 1417 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1418 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | assert(b != NULL); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1420 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | if (off < 0) |
| 1422 | return 0; |
| 1423 | i = &c->u->u_curblock->b_instr[off]; |
| 1424 | i->i_opcode = opcode; |
| 1425 | i->i_target = b; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1426 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1430 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1431 | to the new block. |
| 1432 | |
| 1433 | The returns inside this macro make it impossible to decref objects |
| 1434 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1435 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1436 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | if (compiler_next_block((C)) == NULL) \ |
| 1438 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | if (!compiler_addop((C), (OP))) \ |
| 1443 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1446 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | if (!compiler_addop((C), (OP))) { \ |
| 1448 | compiler_exit_scope(c); \ |
| 1449 | return 0; \ |
| 1450 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1453 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1454 | if (!compiler_addop_load_const((C), (O))) \ |
| 1455 | return 0; \ |
| 1456 | } |
| 1457 | |
| 1458 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1459 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1460 | PyObject *__new_const = (O); \ |
| 1461 | if (__new_const == NULL) { \ |
| 1462 | return 0; \ |
| 1463 | } \ |
| 1464 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1465 | Py_DECREF(__new_const); \ |
| 1466 | return 0; \ |
| 1467 | } \ |
| 1468 | Py_DECREF(__new_const); \ |
| 1469 | } |
| 1470 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1471 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1473 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1476 | /* Same as ADDOP_O, but steals a reference. */ |
| 1477 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1478 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1479 | Py_DECREF((O)); \ |
| 1480 | return 0; \ |
| 1481 | } \ |
| 1482 | Py_DECREF((O)); \ |
| 1483 | } |
| 1484 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1485 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1487 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1492 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 1495 | #define ADDOP_JUMP(C, OP, O) { \ |
| 1496 | if (!compiler_addop_j((C), (OP), (O))) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1500 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1501 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1502 | return 0; \ |
| 1503 | } |
| 1504 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1505 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1506 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1507 | */ |
| 1508 | |
| 1509 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1511 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1514 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1515 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1516 | compiler_exit_scope(c); \ |
| 1517 | return 0; \ |
| 1518 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1521 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1523 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
| 1526 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1528 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1530 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1531 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1532 | return 0; \ |
| 1533 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1536 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1537 | int _i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1538 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1539 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1540 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1541 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1542 | compiler_exit_scope(c); \ |
| 1543 | return 0; \ |
| 1544 | } \ |
| 1545 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1548 | /* These macros allows to check only for errors and not emmit bytecode |
| 1549 | * while visiting nodes. |
| 1550 | */ |
| 1551 | |
| 1552 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1553 | c->c_do_not_emit_bytecode++; |
| 1554 | |
| 1555 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1556 | c->c_do_not_emit_bytecode--; \ |
| 1557 | } |
| 1558 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1559 | /* Search if variable annotations are present statically in a block. */ |
| 1560 | |
| 1561 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1562 | find_ann(asdl_stmt_seq *stmts) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1563 | { |
| 1564 | int i, j, res = 0; |
| 1565 | stmt_ty st; |
| 1566 | |
| 1567 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1568 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1569 | switch (st->kind) { |
| 1570 | case AnnAssign_kind: |
| 1571 | return 1; |
| 1572 | case For_kind: |
| 1573 | res = find_ann(st->v.For.body) || |
| 1574 | find_ann(st->v.For.orelse); |
| 1575 | break; |
| 1576 | case AsyncFor_kind: |
| 1577 | res = find_ann(st->v.AsyncFor.body) || |
| 1578 | find_ann(st->v.AsyncFor.orelse); |
| 1579 | break; |
| 1580 | case While_kind: |
| 1581 | res = find_ann(st->v.While.body) || |
| 1582 | find_ann(st->v.While.orelse); |
| 1583 | break; |
| 1584 | case If_kind: |
| 1585 | res = find_ann(st->v.If.body) || |
| 1586 | find_ann(st->v.If.orelse); |
| 1587 | break; |
| 1588 | case With_kind: |
| 1589 | res = find_ann(st->v.With.body); |
| 1590 | break; |
| 1591 | case AsyncWith_kind: |
| 1592 | res = find_ann(st->v.AsyncWith.body); |
| 1593 | break; |
| 1594 | case Try_kind: |
| 1595 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1596 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1597 | st->v.Try.handlers, j); |
| 1598 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1599 | return 1; |
| 1600 | } |
| 1601 | } |
| 1602 | res = find_ann(st->v.Try.body) || |
| 1603 | find_ann(st->v.Try.finalbody) || |
| 1604 | find_ann(st->v.Try.orelse); |
| 1605 | break; |
| 1606 | default: |
| 1607 | res = 0; |
| 1608 | } |
| 1609 | if (res) { |
| 1610 | break; |
| 1611 | } |
| 1612 | } |
| 1613 | return res; |
| 1614 | } |
| 1615 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1616 | /* |
| 1617 | * Frame block handling functions |
| 1618 | */ |
| 1619 | |
| 1620 | static int |
| 1621 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1622 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1623 | { |
| 1624 | struct fblockinfo *f; |
| 1625 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1626 | return compiler_error(c, "too many statically nested blocks"); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1627 | } |
| 1628 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1629 | f->fb_type = t; |
| 1630 | f->fb_block = b; |
| 1631 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1632 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1633 | return 1; |
| 1634 | } |
| 1635 | |
| 1636 | static void |
| 1637 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1638 | { |
| 1639 | struct compiler_unit *u = c->u; |
| 1640 | assert(u->u_nfblocks > 0); |
| 1641 | u->u_nfblocks--; |
| 1642 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1643 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1644 | } |
| 1645 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1646 | static int |
| 1647 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1648 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1649 | ADDOP(c, DUP_TOP); |
| 1650 | ADDOP(c, DUP_TOP); |
| 1651 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1652 | return 1; |
| 1653 | } |
| 1654 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1655 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1656 | * popping the blocks will be restored afterwards, unless another |
| 1657 | * return, break or continue is found. In which case, the TOS will |
| 1658 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1659 | */ |
| 1660 | static int |
| 1661 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1662 | int preserve_tos) |
| 1663 | { |
| 1664 | switch (info->fb_type) { |
| 1665 | case WHILE_LOOP: |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1666 | case EXCEPTION_HANDLER: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1667 | return 1; |
| 1668 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1669 | case FOR_LOOP: |
| 1670 | /* Pop the iterator */ |
| 1671 | if (preserve_tos) { |
| 1672 | ADDOP(c, ROT_TWO); |
| 1673 | } |
| 1674 | ADDOP(c, POP_TOP); |
| 1675 | return 1; |
| 1676 | |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 1677 | case TRY_EXCEPT: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1678 | ADDOP(c, POP_BLOCK); |
| 1679 | return 1; |
| 1680 | |
| 1681 | case FINALLY_TRY: |
| 1682 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1683 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1684 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1685 | return 0; |
| 1686 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1687 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1688 | /* Emit the finally block, restoring the line number when done */ |
| 1689 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1690 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1691 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1692 | if (preserve_tos) { |
| 1693 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1694 | } |
| 1695 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1696 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1697 | case FINALLY_END: |
| 1698 | if (preserve_tos) { |
| 1699 | ADDOP(c, ROT_FOUR); |
| 1700 | } |
| 1701 | ADDOP(c, POP_TOP); |
| 1702 | ADDOP(c, POP_TOP); |
| 1703 | ADDOP(c, POP_TOP); |
| 1704 | if (preserve_tos) { |
| 1705 | ADDOP(c, ROT_FOUR); |
| 1706 | } |
| 1707 | ADDOP(c, POP_EXCEPT); |
| 1708 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1709 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1710 | case WITH: |
| 1711 | case ASYNC_WITH: |
| 1712 | ADDOP(c, POP_BLOCK); |
| 1713 | if (preserve_tos) { |
| 1714 | ADDOP(c, ROT_TWO); |
| 1715 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1716 | if(!compiler_call_exit_with_nones(c)) { |
| 1717 | return 0; |
| 1718 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1719 | if (info->fb_type == ASYNC_WITH) { |
| 1720 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1721 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1722 | ADDOP(c, YIELD_FROM); |
| 1723 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1724 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1725 | return 1; |
| 1726 | |
| 1727 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1728 | if (info->fb_datum) { |
| 1729 | ADDOP(c, POP_BLOCK); |
| 1730 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1731 | if (preserve_tos) { |
| 1732 | ADDOP(c, ROT_FOUR); |
| 1733 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1734 | ADDOP(c, POP_EXCEPT); |
| 1735 | if (info->fb_datum) { |
| 1736 | ADDOP_LOAD_CONST(c, Py_None); |
| 1737 | compiler_nameop(c, info->fb_datum, Store); |
| 1738 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1739 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1740 | return 1; |
| 1741 | |
| 1742 | case POP_VALUE: |
| 1743 | if (preserve_tos) { |
| 1744 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1745 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1746 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1747 | return 1; |
| 1748 | } |
| 1749 | Py_UNREACHABLE(); |
| 1750 | } |
| 1751 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1752 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1753 | static int |
| 1754 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1755 | if (c->u->u_nfblocks == 0) { |
| 1756 | return 1; |
| 1757 | } |
| 1758 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1759 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1760 | *loop = top; |
| 1761 | return 1; |
| 1762 | } |
| 1763 | struct fblockinfo copy = *top; |
| 1764 | c->u->u_nfblocks--; |
| 1765 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1766 | return 0; |
| 1767 | } |
| 1768 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1769 | return 0; |
| 1770 | } |
| 1771 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1772 | c->u->u_nfblocks++; |
| 1773 | return 1; |
| 1774 | } |
| 1775 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1776 | /* Compile a sequence of statements, checking for a docstring |
| 1777 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1778 | |
| 1779 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1780 | compiler_body(struct compiler *c, asdl_stmt_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1781 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1782 | int i = 0; |
| 1783 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1784 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1785 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1786 | /* Set current line number to the line number of first statement. |
| 1787 | This way line number for SETUP_ANNOTATIONS will always |
| 1788 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1789 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1790 | 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] | 1791 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1792 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1793 | } |
| 1794 | /* Every annotated class and module should have __annotations__. */ |
| 1795 | if (find_ann(stmts)) { |
| 1796 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1797 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1798 | if (!asdl_seq_LEN(stmts)) |
| 1799 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1800 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1801 | if (c->c_optimize < 2) { |
| 1802 | docstring = _PyAST_GetDocString(stmts); |
| 1803 | if (docstring) { |
| 1804 | i = 1; |
| 1805 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1806 | assert(st->kind == Expr_kind); |
| 1807 | VISIT(c, expr, st->v.Expr.value); |
| 1808 | if (!compiler_nameop(c, __doc__, Store)) |
| 1809 | return 0; |
| 1810 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1812 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1813 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1814 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | static PyCodeObject * |
| 1818 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1819 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | PyCodeObject *co; |
| 1821 | int addNone = 1; |
| 1822 | static PyObject *module; |
| 1823 | if (!module) { |
| 1824 | module = PyUnicode_InternFromString("<module>"); |
| 1825 | if (!module) |
| 1826 | return NULL; |
| 1827 | } |
| 1828 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1829 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1830 | return NULL; |
| 1831 | switch (mod->kind) { |
| 1832 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1833 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | compiler_exit_scope(c); |
| 1835 | return 0; |
| 1836 | } |
| 1837 | break; |
| 1838 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1839 | if (find_ann(mod->v.Interactive.body)) { |
| 1840 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1841 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | c->c_interactive = 1; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1843 | VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | break; |
| 1845 | case Expression_kind: |
| 1846 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1847 | addNone = 0; |
| 1848 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | default: |
| 1850 | PyErr_Format(PyExc_SystemError, |
| 1851 | "module kind %d should not be possible", |
| 1852 | mod->kind); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | co = assemble(c, addNone); |
| 1856 | compiler_exit_scope(c); |
| 1857 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1860 | /* The test for LOCAL must come before the test for FREE in order to |
| 1861 | handle classes where name is both local and free. The local var is |
| 1862 | 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] | 1863 | */ |
| 1864 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | static int |
| 1866 | get_ref_type(struct compiler *c, PyObject *name) |
| 1867 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1868 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1869 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1870 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1871 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1872 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | if (scope == 0) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1874 | _Py_FatalErrorFormat(__func__, |
| 1875 | "unknown scope for %.100s in %.100s(%s)\n" |
| 1876 | "symbols: %s\nlocals: %s\nglobals: %s", |
| 1877 | PyUnicode_AsUTF8(name), |
| 1878 | PyUnicode_AsUTF8(c->u->u_name), |
| 1879 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1880 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1881 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1882 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | static int |
| 1889 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1890 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1891 | PyObject *v; |
| 1892 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1893 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1894 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1895 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1899 | 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] | 1900 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1901 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1902 | if (qualname == NULL) |
| 1903 | qualname = co->co_name; |
| 1904 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1905 | if (free) { |
| 1906 | for (i = 0; i < free; ++i) { |
| 1907 | /* Bypass com_addop_varname because it will generate |
| 1908 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1909 | */ |
| 1910 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1911 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1912 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1913 | /* Special case: If a class contains a method with a |
| 1914 | free variable that has the same name as a method, |
| 1915 | the name will be considered free *and* local in the |
| 1916 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1917 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1918 | */ |
| 1919 | reftype = get_ref_type(c, name); |
| 1920 | if (reftype == CELL) |
| 1921 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1922 | else /* (reftype == FREE) */ |
| 1923 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1924 | if (arg == -1) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1925 | _Py_FatalErrorFormat(__func__, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1926 | "lookup %s in %s %d %d\n" |
| 1927 | "freevars of %s: %s\n", |
| 1928 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1929 | PyUnicode_AsUTF8(c->u->u_name), |
| 1930 | reftype, arg, |
| 1931 | PyUnicode_AsUTF8(co->co_name), |
| 1932 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1933 | } |
| 1934 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1936 | flags |= 0x08; |
| 1937 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1939 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1940 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1941 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1946 | compiler_decorators(struct compiler *c, asdl_expr_seq* decos) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1947 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | if (!decos) |
| 1951 | return 1; |
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 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1954 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1955 | } |
| 1956 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1960 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs, |
| 1961 | asdl_expr_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1962 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1963 | /* Push a dict of keyword-only default values. |
| 1964 | |
| 1965 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1966 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1967 | int i; |
| 1968 | PyObject *keys = NULL; |
| 1969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1971 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1972 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1973 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1974 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1975 | if (!mangled) { |
| 1976 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1977 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1978 | if (keys == NULL) { |
| 1979 | keys = PyList_New(1); |
| 1980 | if (keys == NULL) { |
| 1981 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1982 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1983 | } |
| 1984 | PyList_SET_ITEM(keys, 0, mangled); |
| 1985 | } |
| 1986 | else { |
| 1987 | int res = PyList_Append(keys, mangled); |
| 1988 | Py_DECREF(mangled); |
| 1989 | if (res == -1) { |
| 1990 | goto error; |
| 1991 | } |
| 1992 | } |
| 1993 | if (!compiler_visit_expr(c, default_)) { |
| 1994 | goto error; |
| 1995 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1996 | } |
| 1997 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1998 | if (keys != NULL) { |
| 1999 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2000 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2001 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2002 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2003 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2004 | assert(default_count > 0); |
| 2005 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2006 | } |
| 2007 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2008 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | error: |
| 2012 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2013 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2017 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2018 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2019 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2020 | return 1; |
| 2021 | } |
| 2022 | |
| 2023 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2024 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 2025 | expr_ty annotation, PyObject *names) |
| 2026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2028 | PyObject *mangled; |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 2029 | VISIT(c, annexpr, annotation); |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2030 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2031 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2032 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2033 | if (PyList_Append(names, mangled) < 0) { |
| 2034 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2035 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2036 | } |
| 2037 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2038 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2039 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2043 | compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args, |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2044 | PyObject *names) |
| 2045 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2046 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2048 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2049 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | c, |
| 2051 | arg->arg, |
| 2052 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2053 | names)) |
| 2054 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2056 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | static int |
| 2060 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2061 | expr_ty returns) |
| 2062 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2063 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2064 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2065 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2066 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | */ |
| 2068 | static identifier return_str; |
| 2069 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2070 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2071 | names = PyList_New(0); |
| 2072 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2073 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2074 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2075 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2076 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 2077 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 2078 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2079 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2080 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2081 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2083 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2085 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2086 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2087 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | if (!return_str) { |
| 2091 | return_str = PyUnicode_InternFromString("return"); |
| 2092 | if (!return_str) |
| 2093 | goto error; |
| 2094 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2095 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | goto error; |
| 2097 | } |
| 2098 | |
| 2099 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2101 | PyObject *keytuple = PyList_AsTuple(names); |
| 2102 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2103 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2104 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2105 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2107 | else { |
| 2108 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2109 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2110 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2111 | |
| 2112 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2113 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2114 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2118 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2119 | { |
| 2120 | VISIT_SEQ(c, expr, args->defaults); |
| 2121 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2122 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2125 | static Py_ssize_t |
| 2126 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2127 | { |
| 2128 | Py_ssize_t funcflags = 0; |
| 2129 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2130 | if (!compiler_visit_defaults(c, args)) |
| 2131 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2132 | funcflags |= 0x01; |
| 2133 | } |
| 2134 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2135 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2136 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2137 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2138 | return -1; |
| 2139 | } |
| 2140 | else if (res > 0) { |
| 2141 | funcflags |= 0x02; |
| 2142 | } |
| 2143 | } |
| 2144 | return funcflags; |
| 2145 | } |
| 2146 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2147 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2148 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2149 | { |
| 2150 | |
| 2151 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2152 | compiler_error(c, "cannot assign to __debug__"); |
| 2153 | return 1; |
| 2154 | } |
| 2155 | return 0; |
| 2156 | } |
| 2157 | |
| 2158 | static int |
| 2159 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2160 | { |
| 2161 | if (arg != NULL) { |
| 2162 | if (forbidden_name(c, arg->arg, Store)) |
| 2163 | return 0; |
| 2164 | } |
| 2165 | return 1; |
| 2166 | } |
| 2167 | |
| 2168 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2169 | compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args) |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2170 | { |
| 2171 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2172 | for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2173 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2174 | return 0; |
| 2175 | } |
| 2176 | } |
| 2177 | return 1; |
| 2178 | } |
| 2179 | |
| 2180 | static int |
| 2181 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2182 | { |
| 2183 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2184 | return 0; |
| 2185 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2186 | return 0; |
| 2187 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2188 | return 0; |
| 2189 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2190 | return 0; |
| 2191 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2192 | return 0; |
| 2193 | return 1; |
| 2194 | } |
| 2195 | |
| 2196 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2197 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2199 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2200 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2201 | arguments_ty args; |
| 2202 | expr_ty returns; |
| 2203 | identifier name; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2204 | asdl_expr_seq* decos; |
| 2205 | asdl_stmt_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2206 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2207 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2208 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2209 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2210 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2211 | if (is_async) { |
| 2212 | assert(s->kind == AsyncFunctionDef_kind); |
| 2213 | |
| 2214 | args = s->v.AsyncFunctionDef.args; |
| 2215 | returns = s->v.AsyncFunctionDef.returns; |
| 2216 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2217 | name = s->v.AsyncFunctionDef.name; |
| 2218 | body = s->v.AsyncFunctionDef.body; |
| 2219 | |
| 2220 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2221 | } else { |
| 2222 | assert(s->kind == FunctionDef_kind); |
| 2223 | |
| 2224 | args = s->v.FunctionDef.args; |
| 2225 | returns = s->v.FunctionDef.returns; |
| 2226 | decos = s->v.FunctionDef.decorator_list; |
| 2227 | name = s->v.FunctionDef.name; |
| 2228 | body = s->v.FunctionDef.body; |
| 2229 | |
| 2230 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2231 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2232 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2233 | if (!compiler_check_debug_args(c, args)) |
| 2234 | return 0; |
| 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | if (!compiler_decorators(c, decos)) |
| 2237 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2238 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2239 | firstlineno = s->lineno; |
| 2240 | if (asdl_seq_LEN(decos)) { |
| 2241 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2242 | } |
| 2243 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2244 | funcflags = compiler_default_arguments(c, args); |
| 2245 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2247 | } |
| 2248 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2249 | annotations = compiler_visit_annotations(c, args, returns); |
| 2250 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2251 | return 0; |
| 2252 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2253 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2254 | funcflags |= 0x04; |
| 2255 | } |
| 2256 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2257 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2258 | return 0; |
| 2259 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2260 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2261 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2262 | if (c->c_optimize < 2) { |
| 2263 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2264 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2265 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | compiler_exit_scope(c); |
| 2267 | return 0; |
| 2268 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2271 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2272 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2273 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2275 | qualname = c->u->u_qualname; |
| 2276 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2278 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2279 | Py_XDECREF(qualname); |
| 2280 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2281 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2282 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2283 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2284 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2285 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | /* decorators */ |
| 2289 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2290 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2291 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2292 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2293 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | static int |
| 2297 | compiler_class(struct compiler *c, stmt_ty s) |
| 2298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | PyCodeObject *co; |
| 2300 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2301 | int i, firstlineno; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2302 | asdl_expr_seq *decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2304 | if (!compiler_decorators(c, decos)) |
| 2305 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2306 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2307 | firstlineno = s->lineno; |
| 2308 | if (asdl_seq_LEN(decos)) { |
| 2309 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2310 | } |
| 2311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | /* ultimately generate code for: |
| 2313 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2314 | where: |
| 2315 | <func> is a function/closure created from the class body; |
| 2316 | it has a single argument (__locals__) where the dict |
| 2317 | (or MutableSequence) representing the locals is passed |
| 2318 | <name> is the class name |
| 2319 | <bases> is the positional arguments and *varargs argument |
| 2320 | <keywords> is the keyword arguments and **kwds argument |
| 2321 | This borrows from compiler_call. |
| 2322 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2325 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2326 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2327 | return 0; |
| 2328 | /* this block represents what we do in the new scope */ |
| 2329 | { |
| 2330 | /* use the class name for name mangling */ |
| 2331 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2332 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | /* load (global) __name__ ... */ |
| 2334 | str = PyUnicode_InternFromString("__name__"); |
| 2335 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2336 | Py_XDECREF(str); |
| 2337 | compiler_exit_scope(c); |
| 2338 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2339 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | Py_DECREF(str); |
| 2341 | /* ... and store it as __module__ */ |
| 2342 | str = PyUnicode_InternFromString("__module__"); |
| 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); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2349 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2350 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2351 | str = PyUnicode_InternFromString("__qualname__"); |
| 2352 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2353 | Py_XDECREF(str); |
| 2354 | compiler_exit_scope(c); |
| 2355 | return 0; |
| 2356 | } |
| 2357 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2358 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2359 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2360 | compiler_exit_scope(c); |
| 2361 | return 0; |
| 2362 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2363 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2364 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2365 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2366 | str = PyUnicode_InternFromString("__class__"); |
| 2367 | if (str == NULL) { |
| 2368 | compiler_exit_scope(c); |
| 2369 | return 0; |
| 2370 | } |
| 2371 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2372 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2373 | if (i < 0) { |
| 2374 | compiler_exit_scope(c); |
| 2375 | return 0; |
| 2376 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2377 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2380 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2381 | str = PyUnicode_InternFromString("__classcell__"); |
| 2382 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2383 | Py_XDECREF(str); |
| 2384 | compiler_exit_scope(c); |
| 2385 | return 0; |
| 2386 | } |
| 2387 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2388 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2389 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2390 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2391 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2392 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2393 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2394 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2395 | /* create the code object */ |
| 2396 | co = assemble(c, 1); |
| 2397 | } |
| 2398 | /* leave the new scope */ |
| 2399 | compiler_exit_scope(c); |
| 2400 | if (co == NULL) |
| 2401 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | /* 2. load the 'build_class' function */ |
| 2404 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2405 | |
| 2406 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2407 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2408 | Py_DECREF(co); |
| 2409 | |
| 2410 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2411 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | |
| 2413 | /* 5. generate the rest of the code for the call */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2414 | 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] | 2415 | return 0; |
| 2416 | |
| 2417 | /* 6. apply decorators */ |
| 2418 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2419 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2420 | } |
| 2421 | |
| 2422 | /* 7. store into <name> */ |
| 2423 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2424 | return 0; |
| 2425 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2428 | /* Return 0 if the expression is a constant value except named singletons. |
| 2429 | Return 1 otherwise. */ |
| 2430 | static int |
| 2431 | check_is_arg(expr_ty e) |
| 2432 | { |
| 2433 | if (e->kind != Constant_kind) { |
| 2434 | return 1; |
| 2435 | } |
| 2436 | PyObject *value = e->v.Constant.value; |
| 2437 | return (value == Py_None |
| 2438 | || value == Py_False |
| 2439 | || value == Py_True |
| 2440 | || value == Py_Ellipsis); |
| 2441 | } |
| 2442 | |
| 2443 | /* Check operands of identity chacks ("is" and "is not"). |
| 2444 | Emit a warning if any operand is a constant except named singletons. |
| 2445 | Return 0 on error. |
| 2446 | */ |
| 2447 | static int |
| 2448 | check_compare(struct compiler *c, expr_ty e) |
| 2449 | { |
| 2450 | Py_ssize_t i, n; |
| 2451 | int left = check_is_arg(e->v.Compare.left); |
| 2452 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2453 | for (i = 0; i < n; i++) { |
| 2454 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2455 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2456 | if (op == Is || op == IsNot) { |
| 2457 | if (!right || !left) { |
| 2458 | const char *msg = (op == Is) |
| 2459 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2460 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2461 | return compiler_warn(c, msg); |
| 2462 | } |
| 2463 | } |
| 2464 | left = right; |
| 2465 | } |
| 2466 | return 1; |
| 2467 | } |
| 2468 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2469 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2470 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2471 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2472 | switch (op) { |
| 2473 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2474 | cmp = Py_EQ; |
| 2475 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2476 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2477 | cmp = Py_NE; |
| 2478 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2479 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2480 | cmp = Py_LT; |
| 2481 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2482 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2483 | cmp = Py_LE; |
| 2484 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2485 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2486 | cmp = Py_GT; |
| 2487 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2488 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2489 | cmp = Py_GE; |
| 2490 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2491 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2492 | ADDOP_I(c, IS_OP, 0); |
| 2493 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2494 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2495 | ADDOP_I(c, IS_OP, 1); |
| 2496 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2497 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2498 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2499 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2500 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2501 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2502 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2503 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2504 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2505 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2506 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2507 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2508 | } |
| 2509 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2510 | |
| 2511 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2512 | static int |
| 2513 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2514 | { |
| 2515 | switch (e->kind) { |
| 2516 | case UnaryOp_kind: |
| 2517 | if (e->v.UnaryOp.op == Not) |
| 2518 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2519 | /* fallback to general implementation */ |
| 2520 | break; |
| 2521 | case BoolOp_kind: { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 2522 | asdl_expr_seq *s = e->v.BoolOp.values; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2523 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2524 | assert(n >= 0); |
| 2525 | int cond2 = e->v.BoolOp.op == Or; |
| 2526 | basicblock *next2 = next; |
| 2527 | if (!cond2 != !cond) { |
| 2528 | next2 = compiler_new_block(c); |
| 2529 | if (next2 == NULL) |
| 2530 | return 0; |
| 2531 | } |
| 2532 | for (i = 0; i < n; ++i) { |
| 2533 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2534 | return 0; |
| 2535 | } |
| 2536 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2537 | return 0; |
| 2538 | if (next2 != next) |
| 2539 | compiler_use_next_block(c, next2); |
| 2540 | return 1; |
| 2541 | } |
| 2542 | case IfExp_kind: { |
| 2543 | basicblock *end, *next2; |
| 2544 | end = compiler_new_block(c); |
| 2545 | if (end == NULL) |
| 2546 | return 0; |
| 2547 | next2 = compiler_new_block(c); |
| 2548 | if (next2 == NULL) |
| 2549 | return 0; |
| 2550 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2551 | return 0; |
| 2552 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2553 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2554 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2555 | compiler_use_next_block(c, next2); |
| 2556 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2557 | return 0; |
| 2558 | compiler_use_next_block(c, end); |
| 2559 | return 1; |
| 2560 | } |
| 2561 | case Compare_kind: { |
| 2562 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2563 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2564 | if (!check_compare(c, e)) { |
| 2565 | return 0; |
| 2566 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2567 | basicblock *cleanup = compiler_new_block(c); |
| 2568 | if (cleanup == NULL) |
| 2569 | return 0; |
| 2570 | VISIT(c, expr, e->v.Compare.left); |
| 2571 | for (i = 0; i < n; i++) { |
| 2572 | VISIT(c, expr, |
| 2573 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2574 | ADDOP(c, DUP_TOP); |
| 2575 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2576 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2577 | ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2578 | NEXT_BLOCK(c); |
| 2579 | } |
| 2580 | 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] | 2581 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2582 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2583 | basicblock *end = compiler_new_block(c); |
| 2584 | if (end == NULL) |
| 2585 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2586 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2587 | compiler_use_next_block(c, cleanup); |
| 2588 | ADDOP(c, POP_TOP); |
| 2589 | if (!cond) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2590 | ADDOP_JUMP(c, JUMP_FORWARD, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2591 | } |
| 2592 | compiler_use_next_block(c, end); |
| 2593 | return 1; |
| 2594 | } |
| 2595 | /* fallback to general implementation */ |
| 2596 | break; |
| 2597 | } |
| 2598 | default: |
| 2599 | /* fallback to general implementation */ |
| 2600 | break; |
| 2601 | } |
| 2602 | |
| 2603 | /* general implementation */ |
| 2604 | VISIT(c, expr, e); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2605 | ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2606 | return 1; |
| 2607 | } |
| 2608 | |
| 2609 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2610 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | basicblock *end, *next; |
| 2613 | |
| 2614 | assert(e->kind == IfExp_kind); |
| 2615 | end = compiler_new_block(c); |
| 2616 | if (end == NULL) |
| 2617 | return 0; |
| 2618 | next = compiler_new_block(c); |
| 2619 | if (next == NULL) |
| 2620 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2621 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2622 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2623 | VISIT(c, expr, e->v.IfExp.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2624 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | compiler_use_next_block(c, next); |
| 2626 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2627 | compiler_use_next_block(c, end); |
| 2628 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2632 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2635 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2637 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | arguments_ty args = e->v.Lambda.args; |
| 2639 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2640 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2641 | if (!compiler_check_debug_args(c, args)) |
| 2642 | return 0; |
| 2643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | if (!name) { |
| 2645 | name = PyUnicode_InternFromString("<lambda>"); |
| 2646 | if (!name) |
| 2647 | return 0; |
| 2648 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2649 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2650 | funcflags = compiler_default_arguments(c, args); |
| 2651 | if (funcflags == -1) { |
| 2652 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2654 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2655 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2656 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | /* Make None the first constant, so the lambda can't have a |
| 2660 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2661 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2665 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2666 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2667 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2668 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2669 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | } |
| 2671 | else { |
| 2672 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2673 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2675 | qualname = c->u->u_qualname; |
| 2676 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2678 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2680 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2681 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2682 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | Py_DECREF(co); |
| 2684 | |
| 2685 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
| 2688 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2689 | compiler_if(struct compiler *c, stmt_ty s) |
| 2690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2691 | basicblock *end, *next; |
| 2692 | int constant; |
| 2693 | assert(s->kind == If_kind); |
| 2694 | end = compiler_new_block(c); |
| 2695 | if (end == NULL) |
| 2696 | return 0; |
| 2697 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2698 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2699 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2700 | * constant = 1: "if 1", "if 2", ... |
| 2701 | * constant = -1: rest */ |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2702 | if (constant == 0) { |
| 2703 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2704 | VISIT_SEQ(c, stmt, s->v.If.body); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2705 | END_DO_NOT_EMIT_BYTECODE |
| 2706 | if (s->v.If.orelse) { |
| 2707 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2708 | } |
| 2709 | } else if (constant == 1) { |
| 2710 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2711 | if (s->v.If.orelse) { |
| 2712 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2713 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2714 | END_DO_NOT_EMIT_BYTECODE |
| 2715 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2717 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | next = compiler_new_block(c); |
| 2719 | if (next == NULL) |
| 2720 | return 0; |
| 2721 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2722 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | next = end; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2724 | } |
| 2725 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2726 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2727 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2729 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2730 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | compiler_use_next_block(c, next); |
| 2732 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2733 | } |
| 2734 | } |
| 2735 | compiler_use_next_block(c, end); |
| 2736 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | static int |
| 2740 | compiler_for(struct compiler *c, stmt_ty s) |
| 2741 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | start = compiler_new_block(c); |
| 2745 | cleanup = compiler_new_block(c); |
| 2746 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2747 | if (start == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2749 | } |
| 2750 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2751 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2752 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2753 | VISIT(c, expr, s->v.For.iter); |
| 2754 | ADDOP(c, GET_ITER); |
| 2755 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2756 | ADDOP_JUMP(c, FOR_ITER, cleanup); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | VISIT(c, expr, s->v.For.target); |
| 2758 | VISIT_SEQ(c, stmt, s->v.For.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2759 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2761 | |
| 2762 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2764 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2765 | compiler_use_next_block(c, end); |
| 2766 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2769 | |
| 2770 | static int |
| 2771 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2772 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2773 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2774 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2775 | c->u->u_ste->ste_coroutine = 1; |
| 2776 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2777 | return compiler_error(c, "'async for' outside async function"); |
| 2778 | } |
| 2779 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2780 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2781 | except = compiler_new_block(c); |
| 2782 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2783 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2784 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2785 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2786 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2787 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2788 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2789 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2790 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2791 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2792 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2793 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2794 | /* SETUP_FINALLY to guard the __anext__ call */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2795 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2796 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2797 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2798 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2799 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2800 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2801 | /* Success block for __anext__ */ |
| 2802 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2803 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2804 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2805 | |
| 2806 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2807 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2808 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2809 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2810 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2811 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2812 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2813 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2814 | |
| 2815 | compiler_use_next_block(c, end); |
| 2816 | |
| 2817 | return 1; |
| 2818 | } |
| 2819 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2820 | static int |
| 2821 | compiler_while(struct compiler *c, stmt_ty s) |
| 2822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2824 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | if (constant == 0) { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2827 | BEGIN_DO_NOT_EMIT_BYTECODE |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2828 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2829 | // inside a while loop so it can correctly evaluate syntax |
| 2830 | // errors. |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2831 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) { |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2832 | return 0; |
| 2833 | } |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2834 | VISIT_SEQ(c, stmt, s->v.While.body); |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2835 | // Remove the dummy block now that is not needed. |
| 2836 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2837 | END_DO_NOT_EMIT_BYTECODE |
| 2838 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2840 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | return 1; |
| 2842 | } |
| 2843 | loop = compiler_new_block(c); |
| 2844 | end = compiler_new_block(c); |
| 2845 | if (constant == -1) { |
| 2846 | anchor = compiler_new_block(c); |
| 2847 | if (anchor == NULL) |
| 2848 | return 0; |
| 2849 | } |
| 2850 | if (loop == NULL || end == NULL) |
| 2851 | return 0; |
| 2852 | if (s->v.While.orelse) { |
| 2853 | orelse = compiler_new_block(c); |
| 2854 | if (orelse == NULL) |
| 2855 | return 0; |
| 2856 | } |
| 2857 | else |
| 2858 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | compiler_use_next_block(c, loop); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2861 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | return 0; |
| 2863 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2864 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2865 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2866 | } |
| 2867 | VISIT_SEQ(c, stmt, s->v.While.body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2868 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | /* XXX should the two POP instructions be in a separate block |
| 2871 | if there is no else clause ? |
| 2872 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2873 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2874 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2875 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2876 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2879 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2880 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2882 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2886 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2887 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2888 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2889 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2890 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2891 | return compiler_error(c, "'return' outside function"); |
| 2892 | if (s->v.Return.value != NULL && |
| 2893 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2894 | { |
| 2895 | return compiler_error( |
| 2896 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2898 | if (preserve_tos) { |
| 2899 | VISIT(c, expr, s->v.Return.value); |
| 2900 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2901 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2902 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2903 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2904 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2905 | } |
| 2906 | else if (!preserve_tos) { |
| 2907 | VISIT(c, expr, s->v.Return.value); |
| 2908 | } |
| 2909 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2912 | } |
| 2913 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2914 | static int |
| 2915 | compiler_break(struct compiler *c) |
| 2916 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2917 | struct fblockinfo *loop = NULL; |
| 2918 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2919 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2920 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2921 | if (loop == NULL) { |
| 2922 | return compiler_error(c, "'break' outside loop"); |
| 2923 | } |
| 2924 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2925 | return 0; |
| 2926 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2927 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2928 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | static int |
| 2932 | compiler_continue(struct compiler *c) |
| 2933 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2934 | struct fblockinfo *loop = NULL; |
| 2935 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2936 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2937 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2938 | if (loop == NULL) { |
| 2939 | return compiler_error(c, "'continue' not properly in loop"); |
| 2940 | } |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2941 | ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2942 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
| 2945 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2946 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | |
| 2948 | SETUP_FINALLY L |
| 2949 | <code for body> |
| 2950 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2951 | <code for finalbody> |
| 2952 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2953 | L: |
| 2954 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2955 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2957 | The special instructions use the block stack. Each block |
| 2958 | stack entry contains the instruction that created it (here |
| 2959 | SETUP_FINALLY), the level of the value stack at the time the |
| 2960 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2962 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | Pushes the current value stack level and the label |
| 2964 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2965 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2966 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2967 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2968 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2969 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2970 | exceptions are pushed onto the value stack (and the exception |
| 2971 | condition is cleared), and the interpreter jumps to the label |
| 2972 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2973 | */ |
| 2974 | |
| 2975 | static int |
| 2976 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2977 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2978 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | body = compiler_new_block(c); |
| 2981 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2982 | exit = compiler_new_block(c); |
| 2983 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2986 | /* `try` block */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 2987 | ADDOP_JUMP(c, SETUP_FINALLY, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2989 | 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] | 2990 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2991 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2992 | if (!compiler_try_except(c, s)) |
| 2993 | return 0; |
| 2994 | } |
| 2995 | else { |
| 2996 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2997 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2999 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3000 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3001 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3002 | /* `finally` block */ |
| 3003 | compiler_use_next_block(c, end); |
| 3004 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3005 | return 0; |
| 3006 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3007 | compiler_pop_fblock(c, FINALLY_END, end); |
| 3008 | ADDOP(c, RERAISE); |
| 3009 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3011 | } |
| 3012 | |
| 3013 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3014 | 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] | 3015 | (The contents of the value stack is shown in [], with the top |
| 3016 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3017 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | |
| 3019 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3020 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | [] <code for S> |
| 3022 | [] POP_BLOCK |
| 3023 | [] JUMP_FORWARD L0 |
| 3024 | |
| 3025 | [tb, val, exc] L1: DUP ) |
| 3026 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3027 | [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] | 3028 | [tb, val, exc] POP |
| 3029 | [tb, val] <assign to V1> (or POP if no V1) |
| 3030 | [tb] POP |
| 3031 | [] <code for S1> |
| 3032 | JUMP_FORWARD L0 |
| 3033 | |
| 3034 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3035 | .............................etc....................... |
| 3036 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3037 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | |
| 3039 | [] L0: <next statement> |
| 3040 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3041 | Of course, parts are not generated if Vi or Ei is not present. |
| 3042 | */ |
| 3043 | static int |
| 3044 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3045 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3047 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3049 | body = compiler_new_block(c); |
| 3050 | except = compiler_new_block(c); |
| 3051 | orelse = compiler_new_block(c); |
| 3052 | end = compiler_new_block(c); |
| 3053 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3054 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3055 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | compiler_use_next_block(c, body); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3057 | if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3059 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3060 | ADDOP(c, POP_BLOCK); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3061 | compiler_pop_fblock(c, TRY_EXCEPT, body); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3062 | ADDOP_JUMP(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3063 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | compiler_use_next_block(c, except); |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3065 | /* Runtime will push a block here, so we need to account for that */ |
| 3066 | if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL)) |
| 3067 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | for (i = 0; i < n; i++) { |
| 3069 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3070 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3072 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3073 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | except = compiler_new_block(c); |
| 3075 | if (except == NULL) |
| 3076 | return 0; |
| 3077 | if (handler->v.ExceptHandler.type) { |
| 3078 | ADDOP(c, DUP_TOP); |
| 3079 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3080 | ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | } |
| 3082 | ADDOP(c, POP_TOP); |
| 3083 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3084 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3085 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3086 | cleanup_end = compiler_new_block(c); |
| 3087 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3088 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3089 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3090 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3091 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3092 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3093 | ADDOP(c, POP_TOP); |
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 | /* |
| 3096 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3097 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3098 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3099 | try: |
| 3100 | # body |
| 3101 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3102 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3103 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3104 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3106 | /* second try: */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3107 | ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3108 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3109 | 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] | 3110 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3112 | /* second # body */ |
| 3113 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3114 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3115 | ADDOP(c, POP_BLOCK); |
| 3116 | ADDOP(c, POP_EXCEPT); |
| 3117 | /* name = None; del name */ |
| 3118 | ADDOP_LOAD_CONST(c, Py_None); |
| 3119 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3120 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3121 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3122 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3123 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3124 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3126 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3127 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3128 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3129 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3130 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3131 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | } |
| 3133 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3134 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3136 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3137 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3138 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3140 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3141 | ADDOP(c, POP_TOP); |
| 3142 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3143 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3144 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3146 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3147 | ADDOP(c, POP_EXCEPT); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3148 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | compiler_use_next_block(c, except); |
| 3151 | } |
Mark Shannon | 02d126a | 2020-09-25 14:04:19 +0100 | [diff] [blame] | 3152 | compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3153 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3154 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3155 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | compiler_use_next_block(c, end); |
| 3157 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3161 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3162 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3163 | return compiler_try_finally(c, s); |
| 3164 | else |
| 3165 | return compiler_try_except(c, s); |
| 3166 | } |
| 3167 | |
| 3168 | |
| 3169 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3170 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3171 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | /* The IMPORT_NAME opcode was already generated. This function |
| 3173 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | 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] | 3176 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3178 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3179 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3180 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3181 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3182 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3184 | while (1) { |
| 3185 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3187 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3188 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3189 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3190 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3191 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3192 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3193 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3194 | if (dot == -1) { |
| 3195 | break; |
| 3196 | } |
| 3197 | ADDOP(c, ROT_TWO); |
| 3198 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3200 | if (!compiler_nameop(c, asname, Store)) { |
| 3201 | return 0; |
| 3202 | } |
| 3203 | ADDOP(c, POP_TOP); |
| 3204 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | } |
| 3206 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | static int |
| 3210 | compiler_import(struct compiler *c, stmt_ty s) |
| 3211 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | /* The Import node stores a module name like a.b.c as a single |
| 3213 | string. This is convenient for all cases except |
| 3214 | import a.b.c as d |
| 3215 | where we need to parse that string to extract the individual |
| 3216 | module names. |
| 3217 | XXX Perhaps change the representation to make this case simpler? |
| 3218 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3219 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3220 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | for (i = 0; i < n; i++) { |
| 3222 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3223 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3224 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3225 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3226 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3229 | if (alias->asname) { |
| 3230 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3231 | if (!r) |
| 3232 | return r; |
| 3233 | } |
| 3234 | else { |
| 3235 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3236 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3237 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3238 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3239 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3240 | if (tmp == NULL) |
| 3241 | return 0; |
| 3242 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3244 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | Py_DECREF(tmp); |
| 3246 | } |
| 3247 | if (!r) |
| 3248 | return r; |
| 3249 | } |
| 3250 | } |
| 3251 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | static int |
| 3255 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3256 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3257 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3258 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3259 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3261 | if (!empty_string) { |
| 3262 | empty_string = PyUnicode_FromString(""); |
| 3263 | if (!empty_string) |
| 3264 | return 0; |
| 3265 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3266 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3267 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3268 | |
| 3269 | names = PyTuple_New(n); |
| 3270 | if (!names) |
| 3271 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3273 | /* build up the names */ |
| 3274 | for (i = 0; i < n; i++) { |
| 3275 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3276 | Py_INCREF(alias->name); |
| 3277 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3278 | } |
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->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3281 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | Py_DECREF(names); |
| 3283 | return compiler_error(c, "from __future__ imports must occur " |
| 3284 | "at the beginning of the file"); |
| 3285 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3286 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | if (s->v.ImportFrom.module) { |
| 3289 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3290 | } |
| 3291 | else { |
| 3292 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3293 | } |
| 3294 | for (i = 0; i < n; i++) { |
| 3295 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3296 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3297 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3298 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3299 | assert(n == 1); |
| 3300 | ADDOP(c, IMPORT_STAR); |
| 3301 | return 1; |
| 3302 | } |
| 3303 | |
| 3304 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3305 | store_name = alias->name; |
| 3306 | if (alias->asname) |
| 3307 | store_name = alias->asname; |
| 3308 | |
| 3309 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | return 0; |
| 3311 | } |
| 3312 | } |
| 3313 | /* remove imported module */ |
| 3314 | ADDOP(c, POP_TOP); |
| 3315 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | static int |
| 3319 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3322 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3323 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3324 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3326 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3327 | { |
| 3328 | if (!compiler_warn(c, "assertion is always true, " |
| 3329 | "perhaps remove parentheses?")) |
| 3330 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3331 | return 0; |
| 3332 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | end = compiler_new_block(c); |
| 3335 | if (end == NULL) |
| 3336 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3337 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3338 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3339 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | if (s->v.Assert.msg) { |
| 3341 | VISIT(c, expr, s->v.Assert.msg); |
| 3342 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3343 | } |
| 3344 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3345 | compiler_use_next_block(c, end); |
| 3346 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3350 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3351 | { |
| 3352 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3353 | VISIT(c, expr, value); |
| 3354 | ADDOP(c, PRINT_EXPR); |
| 3355 | return 1; |
| 3356 | } |
| 3357 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3358 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3359 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3360 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3361 | } |
| 3362 | |
| 3363 | VISIT(c, expr, value); |
| 3364 | ADDOP(c, POP_TOP); |
| 3365 | return 1; |
| 3366 | } |
| 3367 | |
| 3368 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3369 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3370 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3371 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3374 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3376 | switch (s->kind) { |
| 3377 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3378 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | case ClassDef_kind: |
| 3380 | return compiler_class(c, s); |
| 3381 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3382 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3383 | case Delete_kind: |
| 3384 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3385 | break; |
| 3386 | case Assign_kind: |
| 3387 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3388 | VISIT(c, expr, s->v.Assign.value); |
| 3389 | for (i = 0; i < n; i++) { |
| 3390 | if (i < n - 1) |
| 3391 | ADDOP(c, DUP_TOP); |
| 3392 | VISIT(c, expr, |
| 3393 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3394 | } |
| 3395 | break; |
| 3396 | case AugAssign_kind: |
| 3397 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3398 | case AnnAssign_kind: |
| 3399 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | case For_kind: |
| 3401 | return compiler_for(c, s); |
| 3402 | case While_kind: |
| 3403 | return compiler_while(c, s); |
| 3404 | case If_kind: |
| 3405 | return compiler_if(c, s); |
| 3406 | case Raise_kind: |
| 3407 | n = 0; |
| 3408 | if (s->v.Raise.exc) { |
| 3409 | VISIT(c, expr, s->v.Raise.exc); |
| 3410 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3411 | if (s->v.Raise.cause) { |
| 3412 | VISIT(c, expr, s->v.Raise.cause); |
| 3413 | n++; |
| 3414 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3415 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3416 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3417 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3418 | case Try_kind: |
| 3419 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | case Assert_kind: |
| 3421 | return compiler_assert(c, s); |
| 3422 | case Import_kind: |
| 3423 | return compiler_import(c, s); |
| 3424 | case ImportFrom_kind: |
| 3425 | return compiler_from_import(c, s); |
| 3426 | case Global_kind: |
| 3427 | case Nonlocal_kind: |
| 3428 | break; |
| 3429 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3430 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | case Pass_kind: |
| 3432 | break; |
| 3433 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3434 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | case Continue_kind: |
| 3436 | return compiler_continue(c); |
| 3437 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3438 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3439 | case AsyncFunctionDef_kind: |
| 3440 | return compiler_function(c, s, 1); |
| 3441 | case AsyncWith_kind: |
| 3442 | return compiler_async_with(c, s, 0); |
| 3443 | case AsyncFor_kind: |
| 3444 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3445 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3447 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
| 3450 | static int |
| 3451 | unaryop(unaryop_ty op) |
| 3452 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | switch (op) { |
| 3454 | case Invert: |
| 3455 | return UNARY_INVERT; |
| 3456 | case Not: |
| 3457 | return UNARY_NOT; |
| 3458 | case UAdd: |
| 3459 | return UNARY_POSITIVE; |
| 3460 | case USub: |
| 3461 | return UNARY_NEGATIVE; |
| 3462 | default: |
| 3463 | PyErr_Format(PyExc_SystemError, |
| 3464 | "unary op %d should not be possible", op); |
| 3465 | return 0; |
| 3466 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3467 | } |
| 3468 | |
| 3469 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3470 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3471 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3472 | switch (op) { |
| 3473 | case Add: |
| 3474 | return BINARY_ADD; |
| 3475 | case Sub: |
| 3476 | return BINARY_SUBTRACT; |
| 3477 | case Mult: |
| 3478 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3479 | case MatMult: |
| 3480 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3481 | case Div: |
| 3482 | return BINARY_TRUE_DIVIDE; |
| 3483 | case Mod: |
| 3484 | return BINARY_MODULO; |
| 3485 | case Pow: |
| 3486 | return BINARY_POWER; |
| 3487 | case LShift: |
| 3488 | return BINARY_LSHIFT; |
| 3489 | case RShift: |
| 3490 | return BINARY_RSHIFT; |
| 3491 | case BitOr: |
| 3492 | return BINARY_OR; |
| 3493 | case BitXor: |
| 3494 | return BINARY_XOR; |
| 3495 | case BitAnd: |
| 3496 | return BINARY_AND; |
| 3497 | case FloorDiv: |
| 3498 | return BINARY_FLOOR_DIVIDE; |
| 3499 | default: |
| 3500 | PyErr_Format(PyExc_SystemError, |
| 3501 | "binary op %d should not be possible", op); |
| 3502 | return 0; |
| 3503 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3507 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | switch (op) { |
| 3510 | case Add: |
| 3511 | return INPLACE_ADD; |
| 3512 | case Sub: |
| 3513 | return INPLACE_SUBTRACT; |
| 3514 | case Mult: |
| 3515 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3516 | case MatMult: |
| 3517 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3518 | case Div: |
| 3519 | return INPLACE_TRUE_DIVIDE; |
| 3520 | case Mod: |
| 3521 | return INPLACE_MODULO; |
| 3522 | case Pow: |
| 3523 | return INPLACE_POWER; |
| 3524 | case LShift: |
| 3525 | return INPLACE_LSHIFT; |
| 3526 | case RShift: |
| 3527 | return INPLACE_RSHIFT; |
| 3528 | case BitOr: |
| 3529 | return INPLACE_OR; |
| 3530 | case BitXor: |
| 3531 | return INPLACE_XOR; |
| 3532 | case BitAnd: |
| 3533 | return INPLACE_AND; |
| 3534 | case FloorDiv: |
| 3535 | return INPLACE_FLOOR_DIVIDE; |
| 3536 | default: |
| 3537 | PyErr_Format(PyExc_SystemError, |
| 3538 | "inplace binary op %d should not be possible", op); |
| 3539 | return 0; |
| 3540 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | static int |
| 3544 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3545 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3546 | int op, scope; |
| 3547 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3549 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3550 | PyObject *dict = c->u->u_names; |
| 3551 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3552 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3553 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3554 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3555 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3556 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3557 | if (forbidden_name(c, name, ctx)) |
| 3558 | return 0; |
| 3559 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3560 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3561 | if (!mangled) |
| 3562 | return 0; |
| 3563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | op = 0; |
| 3565 | optype = OP_NAME; |
| 3566 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3567 | switch (scope) { |
| 3568 | case FREE: |
| 3569 | dict = c->u->u_freevars; |
| 3570 | optype = OP_DEREF; |
| 3571 | break; |
| 3572 | case CELL: |
| 3573 | dict = c->u->u_cellvars; |
| 3574 | optype = OP_DEREF; |
| 3575 | break; |
| 3576 | case LOCAL: |
| 3577 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3578 | optype = OP_FAST; |
| 3579 | break; |
| 3580 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3581 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | optype = OP_GLOBAL; |
| 3583 | break; |
| 3584 | case GLOBAL_EXPLICIT: |
| 3585 | optype = OP_GLOBAL; |
| 3586 | break; |
| 3587 | default: |
| 3588 | /* scope can be 0 */ |
| 3589 | break; |
| 3590 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | /* 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] | 3593 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3595 | switch (optype) { |
| 3596 | case OP_DEREF: |
| 3597 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3598 | case Load: |
| 3599 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3600 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3601 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3602 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | } |
| 3604 | break; |
| 3605 | case OP_FAST: |
| 3606 | switch (ctx) { |
| 3607 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3608 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3610 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3611 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3612 | return 1; |
| 3613 | case OP_GLOBAL: |
| 3614 | switch (ctx) { |
| 3615 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3616 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | } |
| 3619 | break; |
| 3620 | case OP_NAME: |
| 3621 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3622 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3623 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3624 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3625 | } |
| 3626 | break; |
| 3627 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3630 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | Py_DECREF(mangled); |
| 3632 | if (arg < 0) |
| 3633 | return 0; |
| 3634 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | static int |
| 3638 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3640 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3641 | int jumpi; |
| 3642 | Py_ssize_t i, n; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3643 | asdl_expr_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3645 | assert(e->kind == BoolOp_kind); |
| 3646 | if (e->v.BoolOp.op == And) |
| 3647 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3648 | else |
| 3649 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3650 | end = compiler_new_block(c); |
| 3651 | if (end == NULL) |
| 3652 | return 0; |
| 3653 | s = e->v.BoolOp.values; |
| 3654 | n = asdl_seq_LEN(s) - 1; |
| 3655 | assert(n >= 0); |
| 3656 | for (i = 0; i < n; ++i) { |
| 3657 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3658 | ADDOP_JUMP(c, jumpi, end); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 3659 | basicblock *next = compiler_new_block(c); |
| 3660 | if (next == NULL) { |
| 3661 | return 0; |
| 3662 | } |
| 3663 | compiler_use_next_block(c, next); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3664 | } |
| 3665 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3666 | compiler_use_next_block(c, end); |
| 3667 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3671 | starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3672 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3673 | { |
| 3674 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3675 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3676 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3677 | PyObject *folded = PyTuple_New(n); |
| 3678 | if (folded == NULL) { |
| 3679 | return 0; |
| 3680 | } |
| 3681 | PyObject *val; |
| 3682 | for (i = 0; i < n; i++) { |
| 3683 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3684 | Py_INCREF(val); |
| 3685 | PyTuple_SET_ITEM(folded, i, val); |
| 3686 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3687 | if (tuple) { |
| 3688 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3689 | } else { |
| 3690 | if (add == SET_ADD) { |
| 3691 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3692 | if (folded == NULL) { |
| 3693 | return 0; |
| 3694 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3695 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3696 | ADDOP_I(c, build, pushed); |
| 3697 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3698 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3699 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3700 | return 1; |
| 3701 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3702 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3703 | for (i = 0; i < n; i++) { |
| 3704 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3705 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3706 | seen_star = 1; |
| 3707 | } |
| 3708 | } |
| 3709 | if (seen_star) { |
| 3710 | seen_star = 0; |
| 3711 | for (i = 0; i < n; i++) { |
| 3712 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3713 | if (elt->kind == Starred_kind) { |
| 3714 | if (seen_star == 0) { |
| 3715 | ADDOP_I(c, build, i+pushed); |
| 3716 | seen_star = 1; |
| 3717 | } |
| 3718 | VISIT(c, expr, elt->v.Starred.value); |
| 3719 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3720 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3721 | else { |
| 3722 | VISIT(c, expr, elt); |
| 3723 | if (seen_star) { |
| 3724 | ADDOP_I(c, add, 1); |
| 3725 | } |
| 3726 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3727 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3728 | assert(seen_star); |
| 3729 | if (tuple) { |
| 3730 | ADDOP(c, LIST_TO_TUPLE); |
| 3731 | } |
| 3732 | } |
| 3733 | else { |
| 3734 | for (i = 0; i < n; i++) { |
| 3735 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3736 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3737 | } |
| 3738 | if (tuple) { |
| 3739 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3740 | } else { |
| 3741 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3742 | } |
| 3743 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3744 | return 1; |
| 3745 | } |
| 3746 | |
| 3747 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3748 | assignment_helper(struct compiler *c, asdl_expr_seq *elts) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3749 | { |
| 3750 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3751 | Py_ssize_t i; |
| 3752 | int seen_star = 0; |
| 3753 | for (i = 0; i < n; i++) { |
| 3754 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3755 | if (elt->kind == Starred_kind && !seen_star) { |
| 3756 | if ((i >= (1 << 8)) || |
| 3757 | (n-i-1 >= (INT_MAX >> 8))) |
| 3758 | return compiler_error(c, |
| 3759 | "too many expressions in " |
| 3760 | "star-unpacking assignment"); |
| 3761 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3762 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3763 | } |
| 3764 | else if (elt->kind == Starred_kind) { |
| 3765 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3766 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3767 | } |
| 3768 | } |
| 3769 | if (!seen_star) { |
| 3770 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3771 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3772 | for (i = 0; i < n; i++) { |
| 3773 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3774 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3775 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3776 | return 1; |
| 3777 | } |
| 3778 | |
| 3779 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3780 | compiler_list(struct compiler *c, expr_ty e) |
| 3781 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3782 | asdl_expr_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3783 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3784 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3785 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3786 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3787 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3788 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3789 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3790 | else |
| 3791 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3792 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3793 | } |
| 3794 | |
| 3795 | static int |
| 3796 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3797 | { |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3798 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3799 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3800 | return assignment_helper(c, elts); |
| 3801 | } |
| 3802 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3803 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3804 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3805 | } |
| 3806 | else |
| 3807 | VISIT_SEQ(c, expr, elts); |
| 3808 | return 1; |
| 3809 | } |
| 3810 | |
| 3811 | static int |
| 3812 | compiler_set(struct compiler *c, expr_ty e) |
| 3813 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3814 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3815 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 3819 | 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] | 3820 | { |
| 3821 | Py_ssize_t i; |
| 3822 | for (i = begin; i < end; i++) { |
| 3823 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3824 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3825 | return 0; |
| 3826 | } |
| 3827 | return 1; |
| 3828 | } |
| 3829 | |
| 3830 | static int |
| 3831 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3832 | { |
| 3833 | Py_ssize_t i, n = end - begin; |
| 3834 | PyObject *keys, *key; |
| 3835 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3836 | for (i = begin; i < end; i++) { |
| 3837 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3838 | } |
| 3839 | keys = PyTuple_New(n); |
| 3840 | if (keys == NULL) { |
| 3841 | return 0; |
| 3842 | } |
| 3843 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3844 | 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] | 3845 | Py_INCREF(key); |
| 3846 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3847 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3848 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3849 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3850 | } |
| 3851 | else { |
| 3852 | for (i = begin; i < end; i++) { |
| 3853 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3854 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3855 | } |
| 3856 | ADDOP_I(c, BUILD_MAP, n); |
| 3857 | } |
| 3858 | return 1; |
| 3859 | } |
| 3860 | |
| 3861 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3862 | compiler_dict(struct compiler *c, expr_ty e) |
| 3863 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3864 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3865 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3866 | int is_unpacking = 0; |
| 3867 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3868 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3869 | elements = 0; |
| 3870 | for (i = 0; i < n; i++) { |
| 3871 | 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] | 3872 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3873 | if (elements) { |
| 3874 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3875 | return 0; |
| 3876 | } |
| 3877 | if (have_dict) { |
| 3878 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3879 | } |
| 3880 | have_dict = 1; |
| 3881 | elements = 0; |
| 3882 | } |
| 3883 | if (have_dict == 0) { |
| 3884 | ADDOP_I(c, BUILD_MAP, 0); |
| 3885 | have_dict = 1; |
| 3886 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3887 | 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] | 3888 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3889 | } |
| 3890 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3891 | if (elements == 0xFFFF) { |
Pablo Galindo | c51db0e | 2020-08-13 09:48:41 +0100 | [diff] [blame] | 3892 | if (!compiler_subdict(c, e, i - elements, i + 1)) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3893 | return 0; |
| 3894 | } |
| 3895 | if (have_dict) { |
| 3896 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3897 | } |
| 3898 | have_dict = 1; |
| 3899 | elements = 0; |
| 3900 | } |
| 3901 | else { |
| 3902 | elements++; |
| 3903 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3904 | } |
| 3905 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3906 | if (elements) { |
| 3907 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3908 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3909 | } |
| 3910 | if (have_dict) { |
| 3911 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3912 | } |
| 3913 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3914 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3915 | if (!have_dict) { |
| 3916 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3917 | } |
| 3918 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
| 3921 | static int |
| 3922 | compiler_compare(struct compiler *c, expr_ty e) |
| 3923 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3924 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3925 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3926 | if (!check_compare(c, e)) { |
| 3927 | return 0; |
| 3928 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3929 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3930 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3931 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3932 | if (n == 0) { |
| 3933 | 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] | 3934 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3935 | } |
| 3936 | else { |
| 3937 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | if (cleanup == NULL) |
| 3939 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3940 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3941 | VISIT(c, expr, |
| 3942 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3943 | ADDOP(c, DUP_TOP); |
| 3944 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3945 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3946 | ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3947 | NEXT_BLOCK(c); |
| 3948 | } |
| 3949 | 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] | 3950 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3951 | basicblock *end = compiler_new_block(c); |
| 3952 | if (end == NULL) |
| 3953 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 3954 | ADDOP_JUMP(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3955 | compiler_use_next_block(c, cleanup); |
| 3956 | ADDOP(c, ROT_TWO); |
| 3957 | ADDOP(c, POP_TOP); |
| 3958 | compiler_use_next_block(c, end); |
| 3959 | } |
| 3960 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3963 | static PyTypeObject * |
| 3964 | infer_type(expr_ty e) |
| 3965 | { |
| 3966 | switch (e->kind) { |
| 3967 | case Tuple_kind: |
| 3968 | return &PyTuple_Type; |
| 3969 | case List_kind: |
| 3970 | case ListComp_kind: |
| 3971 | return &PyList_Type; |
| 3972 | case Dict_kind: |
| 3973 | case DictComp_kind: |
| 3974 | return &PyDict_Type; |
| 3975 | case Set_kind: |
| 3976 | case SetComp_kind: |
| 3977 | return &PySet_Type; |
| 3978 | case GeneratorExp_kind: |
| 3979 | return &PyGen_Type; |
| 3980 | case Lambda_kind: |
| 3981 | return &PyFunction_Type; |
| 3982 | case JoinedStr_kind: |
| 3983 | case FormattedValue_kind: |
| 3984 | return &PyUnicode_Type; |
| 3985 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3986 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3987 | default: |
| 3988 | return NULL; |
| 3989 | } |
| 3990 | } |
| 3991 | |
| 3992 | static int |
| 3993 | check_caller(struct compiler *c, expr_ty e) |
| 3994 | { |
| 3995 | switch (e->kind) { |
| 3996 | case Constant_kind: |
| 3997 | case Tuple_kind: |
| 3998 | case List_kind: |
| 3999 | case ListComp_kind: |
| 4000 | case Dict_kind: |
| 4001 | case DictComp_kind: |
| 4002 | case Set_kind: |
| 4003 | case SetComp_kind: |
| 4004 | case GeneratorExp_kind: |
| 4005 | case JoinedStr_kind: |
| 4006 | case FormattedValue_kind: |
| 4007 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4008 | "perhaps you missed a comma?", |
| 4009 | infer_type(e)->tp_name); |
| 4010 | default: |
| 4011 | return 1; |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | static int |
| 4016 | check_subscripter(struct compiler *c, expr_ty e) |
| 4017 | { |
| 4018 | PyObject *v; |
| 4019 | |
| 4020 | switch (e->kind) { |
| 4021 | case Constant_kind: |
| 4022 | v = e->v.Constant.value; |
| 4023 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4024 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4025 | PyAnySet_Check(v))) |
| 4026 | { |
| 4027 | return 1; |
| 4028 | } |
| 4029 | /* fall through */ |
| 4030 | case Set_kind: |
| 4031 | case SetComp_kind: |
| 4032 | case GeneratorExp_kind: |
| 4033 | case Lambda_kind: |
| 4034 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4035 | "perhaps you missed a comma?", |
| 4036 | infer_type(e)->tp_name); |
| 4037 | default: |
| 4038 | return 1; |
| 4039 | } |
| 4040 | } |
| 4041 | |
| 4042 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4043 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4044 | { |
| 4045 | PyObject *v; |
| 4046 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4047 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4048 | if (index_type == NULL |
| 4049 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4050 | || index_type == &PySlice_Type) { |
| 4051 | return 1; |
| 4052 | } |
| 4053 | |
| 4054 | switch (e->kind) { |
| 4055 | case Constant_kind: |
| 4056 | v = e->v.Constant.value; |
| 4057 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4058 | return 1; |
| 4059 | } |
| 4060 | /* fall through */ |
| 4061 | case Tuple_kind: |
| 4062 | case List_kind: |
| 4063 | case ListComp_kind: |
| 4064 | case JoinedStr_kind: |
| 4065 | case FormattedValue_kind: |
| 4066 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4067 | "not %.200s; " |
| 4068 | "perhaps you missed a comma?", |
| 4069 | infer_type(e)->tp_name, |
| 4070 | index_type->tp_name); |
| 4071 | default: |
| 4072 | return 1; |
| 4073 | } |
| 4074 | } |
| 4075 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4076 | // 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] | 4077 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4078 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4079 | { |
| 4080 | Py_ssize_t argsl, i; |
| 4081 | expr_ty meth = e->v.Call.func; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4082 | asdl_expr_seq *args = e->v.Call.args; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4083 | |
| 4084 | /* Check that the call node is an attribute access, and that |
| 4085 | the call doesn't have keyword parameters. */ |
| 4086 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4087 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4088 | return -1; |
| 4089 | |
| 4090 | /* Check that there are no *varargs types of arguments. */ |
| 4091 | argsl = asdl_seq_LEN(args); |
| 4092 | for (i = 0; i < argsl; i++) { |
| 4093 | expr_ty elt = asdl_seq_GET(args, i); |
| 4094 | if (elt->kind == Starred_kind) { |
| 4095 | return -1; |
| 4096 | } |
| 4097 | } |
| 4098 | |
| 4099 | /* Alright, we can optimize the code. */ |
| 4100 | VISIT(c, expr, meth->v.Attribute.value); |
| 4101 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4102 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4103 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4104 | return 1; |
| 4105 | } |
| 4106 | |
| 4107 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4108 | validate_keywords(struct compiler *c, asdl_keyword_seq *keywords) |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4109 | { |
| 4110 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4111 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4112 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4113 | if (key->arg == NULL) { |
| 4114 | continue; |
| 4115 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4116 | if (forbidden_name(c, key->arg, Store)) { |
| 4117 | return -1; |
| 4118 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4119 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4120 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4121 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
| 4122 | PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg); |
| 4123 | if (msg == NULL) { |
| 4124 | return -1; |
| 4125 | } |
| 4126 | c->u->u_col_offset = other->col_offset; |
| 4127 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
| 4128 | Py_DECREF(msg); |
| 4129 | return -1; |
| 4130 | } |
| 4131 | } |
| 4132 | } |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
| 4136 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4137 | compiler_call(struct compiler *c, expr_ty e) |
| 4138 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4139 | int ret = maybe_optimize_method_call(c, e); |
| 4140 | if (ret >= 0) { |
| 4141 | return ret; |
| 4142 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4143 | if (!check_caller(c, e->v.Call.func)) { |
| 4144 | return 0; |
| 4145 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4146 | VISIT(c, expr, e->v.Call.func); |
| 4147 | return compiler_call_helper(c, 0, |
| 4148 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4149 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4152 | static int |
| 4153 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4154 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4155 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4156 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4157 | 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] | 4158 | return 1; |
| 4159 | } |
| 4160 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4161 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4162 | static int |
| 4163 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4164 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4165 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4166 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4167 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4168 | Convert the conversion char to 3 bits: |
| 4169 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4170 | !s : 001 0x1 FVC_STR |
| 4171 | !r : 010 0x2 FVC_REPR |
| 4172 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4173 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4174 | next bit is whether or not we have a format spec: |
| 4175 | yes : 100 0x4 |
| 4176 | no : 000 0x0 |
| 4177 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4178 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4179 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4180 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4181 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4182 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4183 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4184 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4185 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4186 | case 's': oparg = FVC_STR; break; |
| 4187 | case 'r': oparg = FVC_REPR; break; |
| 4188 | case 'a': oparg = FVC_ASCII; break; |
| 4189 | case -1: oparg = FVC_NONE; break; |
| 4190 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4191 | PyErr_Format(PyExc_SystemError, |
| 4192 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4193 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4194 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4195 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4196 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4197 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4198 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4199 | } |
| 4200 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4201 | /* And push our opcode and oparg */ |
| 4202 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4203 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4204 | return 1; |
| 4205 | } |
| 4206 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4207 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4208 | 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] | 4209 | { |
| 4210 | Py_ssize_t i, n = end - begin; |
| 4211 | keyword_ty kw; |
| 4212 | PyObject *keys, *key; |
| 4213 | assert(n > 0); |
| 4214 | if (n > 1) { |
| 4215 | for (i = begin; i < end; i++) { |
| 4216 | kw = asdl_seq_GET(keywords, i); |
| 4217 | VISIT(c, expr, kw->value); |
| 4218 | } |
| 4219 | keys = PyTuple_New(n); |
| 4220 | if (keys == NULL) { |
| 4221 | return 0; |
| 4222 | } |
| 4223 | for (i = begin; i < end; i++) { |
| 4224 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4225 | Py_INCREF(key); |
| 4226 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4227 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4228 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4229 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4230 | } |
| 4231 | else { |
| 4232 | /* a for loop only executes once */ |
| 4233 | for (i = begin; i < end; i++) { |
| 4234 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4235 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4236 | VISIT(c, expr, kw->value); |
| 4237 | } |
| 4238 | ADDOP_I(c, BUILD_MAP, n); |
| 4239 | } |
| 4240 | return 1; |
| 4241 | } |
| 4242 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4243 | /* shared code between compiler_call and compiler_class */ |
| 4244 | static int |
| 4245 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4246 | int n, /* Args already pushed */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4247 | asdl_expr_seq *args, |
| 4248 | asdl_keyword_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4249 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4250 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4251 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4252 | if (validate_keywords(c, keywords) == -1) { |
| 4253 | return 0; |
| 4254 | } |
| 4255 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4256 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4257 | nkwelts = asdl_seq_LEN(keywords); |
| 4258 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4259 | for (i = 0; i < nelts; i++) { |
| 4260 | expr_ty elt = asdl_seq_GET(args, i); |
| 4261 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4262 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4263 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4264 | } |
| 4265 | for (i = 0; i < nkwelts; i++) { |
| 4266 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4267 | if (kw->arg == NULL) { |
| 4268 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4269 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4270 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4271 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4272 | /* No * or ** args, so can use faster calling sequence */ |
| 4273 | for (i = 0; i < nelts; i++) { |
| 4274 | expr_ty elt = asdl_seq_GET(args, i); |
| 4275 | assert(elt->kind != Starred_kind); |
| 4276 | VISIT(c, expr, elt); |
| 4277 | } |
| 4278 | if (nkwelts) { |
| 4279 | PyObject *names; |
| 4280 | VISIT_SEQ(c, keyword, keywords); |
| 4281 | names = PyTuple_New(nkwelts); |
| 4282 | if (names == NULL) { |
| 4283 | return 0; |
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 | for (i = 0; i < nkwelts; i++) { |
| 4286 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4287 | Py_INCREF(kw->arg); |
| 4288 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4289 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4290 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4291 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4292 | return 1; |
| 4293 | } |
| 4294 | else { |
| 4295 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4296 | return 1; |
| 4297 | } |
| 4298 | |
| 4299 | ex_call: |
| 4300 | |
| 4301 | /* Do positional arguments. */ |
| 4302 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4303 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4304 | } |
| 4305 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4306 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4307 | return 0; |
| 4308 | } |
| 4309 | /* Then keyword arguments */ |
| 4310 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4311 | /* Has a new dict been pushed */ |
| 4312 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4313 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4314 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4315 | for (i = 0; i < nkwelts; i++) { |
| 4316 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4317 | if (kw->arg == NULL) { |
| 4318 | /* A keyword argument unpacking. */ |
| 4319 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4320 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4321 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4322 | } |
Mark Shannon | db64f12 | 2020-06-01 10:42:42 +0100 | [diff] [blame] | 4323 | if (have_dict) { |
| 4324 | ADDOP_I(c, DICT_MERGE, 1); |
| 4325 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4326 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4327 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4328 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4329 | if (!have_dict) { |
| 4330 | ADDOP_I(c, BUILD_MAP, 0); |
| 4331 | have_dict = 1; |
| 4332 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4333 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4334 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4335 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4336 | else { |
| 4337 | nseen++; |
| 4338 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4339 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4340 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4341 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4342 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4343 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4344 | } |
| 4345 | if (have_dict) { |
| 4346 | ADDOP_I(c, DICT_MERGE, 1); |
| 4347 | } |
| 4348 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4349 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4350 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4351 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4352 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4353 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4354 | } |
| 4355 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4356 | |
| 4357 | /* List and set comprehensions and generator expressions work by creating a |
| 4358 | nested function to perform the actual iteration. This means that the |
| 4359 | iteration variables don't leak into the current scope. |
| 4360 | The defined function is called immediately following its definition, with the |
| 4361 | result of that call being the result of the expression. |
| 4362 | The LC/SC version returns the populated container, while the GE version is |
| 4363 | flagged in symtable.c as a generator, so it returns the generator object |
| 4364 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4365 | |
| 4366 | Possible cleanups: |
| 4367 | - iterate over the generator sequence instead of using recursion |
| 4368 | */ |
| 4369 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4370 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4371 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | compiler_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4373 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4374 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4376 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4377 | comprehension_ty gen; |
| 4378 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4379 | if (gen->is_async) { |
| 4380 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4381 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4382 | } else { |
| 4383 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4384 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4385 | } |
| 4386 | } |
| 4387 | |
| 4388 | static int |
| 4389 | compiler_sync_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4390 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4391 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4392 | expr_ty elt, expr_ty val, int type) |
| 4393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | /* generate code for the iterator, then each of the ifs, |
| 4395 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4397 | comprehension_ty gen; |
| 4398 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4399 | Py_ssize_t i, n; |
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 | start = compiler_new_block(c); |
| 4402 | skip = compiler_new_block(c); |
| 4403 | if_cleanup = compiler_new_block(c); |
| 4404 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4406 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4407 | anchor == NULL) |
| 4408 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4412 | if (gen_index == 0) { |
| 4413 | /* Receive outermost iter as an implicit argument */ |
| 4414 | c->u->u_argcount = 1; |
| 4415 | ADDOP_I(c, LOAD_FAST, 0); |
| 4416 | } |
| 4417 | else { |
| 4418 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4419 | /* Fast path for the temporary variable assignment idiom: |
| 4420 | for y in [f(x)] |
| 4421 | */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4422 | asdl_expr_seq *elts; |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4423 | switch (gen->iter->kind) { |
| 4424 | case List_kind: |
| 4425 | elts = gen->iter->v.List.elts; |
| 4426 | break; |
| 4427 | case Tuple_kind: |
| 4428 | elts = gen->iter->v.Tuple.elts; |
| 4429 | break; |
| 4430 | default: |
| 4431 | elts = NULL; |
| 4432 | } |
| 4433 | if (asdl_seq_LEN(elts) == 1) { |
| 4434 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4435 | if (elt->kind != Starred_kind) { |
| 4436 | VISIT(c, expr, elt); |
| 4437 | start = NULL; |
| 4438 | } |
| 4439 | } |
| 4440 | if (start) { |
| 4441 | VISIT(c, expr, gen->iter); |
| 4442 | ADDOP(c, GET_ITER); |
| 4443 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4445 | if (start) { |
| 4446 | depth++; |
| 4447 | compiler_use_next_block(c, start); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4448 | ADDOP_JUMP(c, FOR_ITER, anchor); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4449 | NEXT_BLOCK(c); |
| 4450 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4453 | /* XXX this needs to be cleaned up...a lot! */ |
| 4454 | n = asdl_seq_LEN(gen->ifs); |
| 4455 | for (i = 0; i < n; i++) { |
| 4456 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4457 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4458 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4459 | NEXT_BLOCK(c); |
| 4460 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4461 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4462 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4463 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4464 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | elt, val, type)) |
| 4466 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4467 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | /* only append after the last for generator */ |
| 4469 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4470 | /* comprehension specific code */ |
| 4471 | switch (type) { |
| 4472 | case COMP_GENEXP: |
| 4473 | VISIT(c, expr, elt); |
| 4474 | ADDOP(c, YIELD_VALUE); |
| 4475 | ADDOP(c, POP_TOP); |
| 4476 | break; |
| 4477 | case COMP_LISTCOMP: |
| 4478 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4479 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4480 | break; |
| 4481 | case COMP_SETCOMP: |
| 4482 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4483 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | break; |
| 4485 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4486 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4488 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4489 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4490 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | break; |
| 4492 | default: |
| 4493 | return 0; |
| 4494 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | compiler_use_next_block(c, skip); |
| 4497 | } |
| 4498 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4499 | if (start) { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4500 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4501 | compiler_use_next_block(c, anchor); |
| 4502 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | |
| 4504 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4505 | } |
| 4506 | |
| 4507 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4508 | compiler_async_comprehension_generator(struct compiler *c, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4509 | asdl_comprehension_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4510 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4511 | expr_ty elt, expr_ty val, int type) |
| 4512 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4513 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4514 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4515 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4516 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4517 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4518 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4519 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4520 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4521 | return 0; |
| 4522 | } |
| 4523 | |
| 4524 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4525 | |
| 4526 | if (gen_index == 0) { |
| 4527 | /* Receive outermost iter as an implicit argument */ |
| 4528 | c->u->u_argcount = 1; |
| 4529 | ADDOP_I(c, LOAD_FAST, 0); |
| 4530 | } |
| 4531 | else { |
| 4532 | /* Sub-iter - calculate on the fly */ |
| 4533 | VISIT(c, expr, gen->iter); |
| 4534 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4535 | } |
| 4536 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4537 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4538 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4539 | ADDOP_JUMP(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4540 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4541 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4542 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4543 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4544 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4545 | |
| 4546 | n = asdl_seq_LEN(gen->ifs); |
| 4547 | for (i = 0; i < n; i++) { |
| 4548 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4549 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4550 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4551 | NEXT_BLOCK(c); |
| 4552 | } |
| 4553 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4554 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4555 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4556 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4557 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4558 | elt, val, type)) |
| 4559 | return 0; |
| 4560 | |
| 4561 | /* only append after the last for generator */ |
| 4562 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4563 | /* comprehension specific code */ |
| 4564 | switch (type) { |
| 4565 | case COMP_GENEXP: |
| 4566 | VISIT(c, expr, elt); |
| 4567 | ADDOP(c, YIELD_VALUE); |
| 4568 | ADDOP(c, POP_TOP); |
| 4569 | break; |
| 4570 | case COMP_LISTCOMP: |
| 4571 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4572 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4573 | break; |
| 4574 | case COMP_SETCOMP: |
| 4575 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4576 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4577 | break; |
| 4578 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4579 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4580 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4581 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4582 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4583 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4584 | break; |
| 4585 | default: |
| 4586 | return 0; |
| 4587 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4588 | } |
| 4589 | compiler_use_next_block(c, if_cleanup); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4590 | ADDOP_JUMP(c, JUMP_ABSOLUTE, start); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4591 | |
| 4592 | compiler_use_next_block(c, except); |
| 4593 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4594 | |
| 4595 | return 1; |
| 4596 | } |
| 4597 | |
| 4598 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4599 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 4600 | identifier name, asdl_comprehension_seq *generators, expr_ty elt, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4601 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4602 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4604 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4605 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4606 | int is_async_generator = 0; |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4607 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4608 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4609 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4610 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4611 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4612 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4613 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4614 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4620 | |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4621 | 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] | 4622 | compiler_error(c, "asynchronous comprehension outside of " |
| 4623 | "an asynchronous function"); |
| 4624 | goto error_in_scope; |
| 4625 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4627 | if (type != COMP_GENEXP) { |
| 4628 | int op; |
| 4629 | switch (type) { |
| 4630 | case COMP_LISTCOMP: |
| 4631 | op = BUILD_LIST; |
| 4632 | break; |
| 4633 | case COMP_SETCOMP: |
| 4634 | op = BUILD_SET; |
| 4635 | break; |
| 4636 | case COMP_DICTCOMP: |
| 4637 | op = BUILD_MAP; |
| 4638 | break; |
| 4639 | default: |
| 4640 | PyErr_Format(PyExc_SystemError, |
| 4641 | "unknown comprehension type %d", type); |
| 4642 | goto error_in_scope; |
| 4643 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4645 | ADDOP_I(c, op, 0); |
| 4646 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4647 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4648 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4649 | val, type)) |
| 4650 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4652 | if (type != COMP_GENEXP) { |
| 4653 | ADDOP(c, RETURN_VALUE); |
| 4654 | } |
| 4655 | |
| 4656 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4657 | qualname = c->u->u_qualname; |
| 4658 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | compiler_exit_scope(c); |
Matthias Bussonnier | bd46174 | 2020-07-06 14:26:52 -0700 | [diff] [blame] | 4660 | if (top_level_await && is_async_generator){ |
| 4661 | c->u->u_ste->ste_coroutine = 1; |
| 4662 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4663 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4664 | goto error; |
| 4665 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4666 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4667 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4668 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4669 | Py_DECREF(co); |
| 4670 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4671 | VISIT(c, expr, outermost->iter); |
| 4672 | |
| 4673 | if (outermost->is_async) { |
| 4674 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4675 | } else { |
| 4676 | ADDOP(c, GET_ITER); |
| 4677 | } |
| 4678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4679 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4680 | |
| 4681 | if (is_async_generator && type != COMP_GENEXP) { |
| 4682 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4683 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4684 | ADDOP(c, YIELD_FROM); |
| 4685 | } |
| 4686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4687 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4688 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4689 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4690 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4691 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4692 | Py_XDECREF(co); |
| 4693 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4697 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4698 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4699 | static identifier name; |
| 4700 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4701 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4702 | if (!name) |
| 4703 | return 0; |
| 4704 | } |
| 4705 | assert(e->kind == GeneratorExp_kind); |
| 4706 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4707 | e->v.GeneratorExp.generators, |
| 4708 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4712 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4713 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4714 | static identifier name; |
| 4715 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4716 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4717 | if (!name) |
| 4718 | return 0; |
| 4719 | } |
| 4720 | assert(e->kind == ListComp_kind); |
| 4721 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4722 | e->v.ListComp.generators, |
| 4723 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4724 | } |
| 4725 | |
| 4726 | static int |
| 4727 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4729 | static identifier name; |
| 4730 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4731 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4732 | if (!name) |
| 4733 | return 0; |
| 4734 | } |
| 4735 | assert(e->kind == SetComp_kind); |
| 4736 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4737 | e->v.SetComp.generators, |
| 4738 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
| 4741 | |
| 4742 | static int |
| 4743 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4744 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | static identifier name; |
| 4746 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4747 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4748 | if (!name) |
| 4749 | return 0; |
| 4750 | } |
| 4751 | assert(e->kind == DictComp_kind); |
| 4752 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4753 | e->v.DictComp.generators, |
| 4754 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | |
| 4758 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4759 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4760 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | VISIT(c, expr, k->value); |
| 4762 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4763 | } |
| 4764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4765 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4766 | whether they are true or false. |
| 4767 | |
| 4768 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4769 | */ |
| 4770 | |
| 4771 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4772 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4773 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4774 | if (e->kind == Constant_kind) { |
| 4775 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4776 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4777 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4778 | } |
| 4779 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4780 | static int |
| 4781 | compiler_with_except_finish(struct compiler *c) { |
| 4782 | basicblock *exit; |
| 4783 | exit = compiler_new_block(c); |
| 4784 | if (exit == NULL) |
| 4785 | return 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4786 | ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4787 | ADDOP(c, RERAISE); |
| 4788 | compiler_use_next_block(c, exit); |
| 4789 | ADDOP(c, POP_TOP); |
| 4790 | ADDOP(c, POP_TOP); |
| 4791 | ADDOP(c, POP_TOP); |
| 4792 | ADDOP(c, POP_EXCEPT); |
| 4793 | ADDOP(c, POP_TOP); |
| 4794 | return 1; |
| 4795 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4796 | |
| 4797 | /* |
| 4798 | Implements the async with statement. |
| 4799 | |
| 4800 | The semantics outlined in that PEP are as follows: |
| 4801 | |
| 4802 | async with EXPR as VAR: |
| 4803 | BLOCK |
| 4804 | |
| 4805 | It is implemented roughly as: |
| 4806 | |
| 4807 | context = EXPR |
| 4808 | exit = context.__aexit__ # not calling it |
| 4809 | value = await context.__aenter__() |
| 4810 | try: |
| 4811 | VAR = value # if VAR present in the syntax |
| 4812 | BLOCK |
| 4813 | finally: |
| 4814 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4815 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4816 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4817 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4818 | if not (await exit(*exc)): |
| 4819 | raise |
| 4820 | */ |
| 4821 | static int |
| 4822 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4823 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4824 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4825 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4826 | |
| 4827 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4828 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4829 | c->u->u_ste->ste_coroutine = 1; |
| 4830 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4831 | return compiler_error(c, "'async with' outside async function"); |
| 4832 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4833 | |
| 4834 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4835 | final = compiler_new_block(c); |
| 4836 | exit = compiler_new_block(c); |
| 4837 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4838 | return 0; |
| 4839 | |
| 4840 | /* Evaluate EXPR */ |
| 4841 | VISIT(c, expr, item->context_expr); |
| 4842 | |
| 4843 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4844 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4845 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4846 | ADDOP(c, YIELD_FROM); |
| 4847 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4848 | ADDOP_JUMP(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4849 | |
| 4850 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4851 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4852 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4853 | return 0; |
| 4854 | } |
| 4855 | |
| 4856 | if (item->optional_vars) { |
| 4857 | VISIT(c, expr, item->optional_vars); |
| 4858 | } |
| 4859 | else { |
| 4860 | /* Discard result from context.__aenter__() */ |
| 4861 | ADDOP(c, POP_TOP); |
| 4862 | } |
| 4863 | |
| 4864 | pos++; |
| 4865 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4866 | /* BLOCK code */ |
| 4867 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4868 | else if (!compiler_async_with(c, s, pos)) |
| 4869 | return 0; |
| 4870 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4871 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4872 | ADDOP(c, POP_BLOCK); |
| 4873 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4874 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4875 | /* For successful outcome: |
| 4876 | * call __exit__(None, None, None) |
| 4877 | */ |
| 4878 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4879 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4880 | ADDOP(c, GET_AWAITABLE); |
| 4881 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4882 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4883 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4884 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4885 | |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4886 | ADDOP_JUMP(c, JUMP_ABSOLUTE, exit); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4887 | |
| 4888 | /* For exceptional outcome: */ |
| 4889 | compiler_use_next_block(c, final); |
| 4890 | |
| 4891 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4892 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4893 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4894 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4895 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4896 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4897 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4898 | return 1; |
| 4899 | } |
| 4900 | |
| 4901 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4902 | /* |
| 4903 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4904 | with EXPR as VAR: |
| 4905 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4906 | is implemented as: |
| 4907 | <code for EXPR> |
| 4908 | SETUP_WITH E |
| 4909 | <code to store to VAR> or POP_TOP |
| 4910 | <code for BLOCK> |
| 4911 | LOAD_CONST (None, None, None) |
| 4912 | CALL_FUNCTION_EX 0 |
| 4913 | JUMP_FORWARD EXIT |
| 4914 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4915 | POP_JUMP_IF_TRUE T: |
| 4916 | RERAISE |
| 4917 | T: POP_TOP * 3 (remove exception from stack) |
| 4918 | POP_EXCEPT |
| 4919 | POP_TOP |
| 4920 | EXIT: |
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 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4923 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4924 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4925 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4926 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4927 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4928 | |
| 4929 | assert(s->kind == With_kind); |
| 4930 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4931 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4932 | final = compiler_new_block(c); |
| 4933 | exit = compiler_new_block(c); |
| 4934 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4935 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4936 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4937 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4938 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4939 | /* Will push bound __exit__ */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4940 | ADDOP_JUMP(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4941 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4942 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4943 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4944 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4945 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4946 | } |
| 4947 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4948 | if (item->optional_vars) { |
| 4949 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4950 | } |
| 4951 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4952 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4953 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4956 | pos++; |
| 4957 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4958 | /* BLOCK code */ |
| 4959 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4960 | else if (!compiler_with(c, s, pos)) |
| 4961 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4962 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4963 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4964 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4965 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4966 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4967 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4968 | /* For successful outcome: |
| 4969 | * call __exit__(None, None, None) |
| 4970 | */ |
| 4971 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4972 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4973 | ADDOP(c, POP_TOP); |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 4974 | ADDOP_JUMP(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4975 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4976 | /* For exceptional outcome: */ |
| 4977 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4978 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4979 | ADDOP(c, WITH_EXCEPT_START); |
| 4980 | compiler_with_except_finish(c); |
| 4981 | |
| 4982 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4983 | return 1; |
| 4984 | } |
| 4985 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4986 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4987 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4988 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4990 | case NamedExpr_kind: |
| 4991 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4992 | ADDOP(c, DUP_TOP); |
| 4993 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4994 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4995 | case BoolOp_kind: |
| 4996 | return compiler_boolop(c, e); |
| 4997 | case BinOp_kind: |
| 4998 | VISIT(c, expr, e->v.BinOp.left); |
| 4999 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5000 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5001 | break; |
| 5002 | case UnaryOp_kind: |
| 5003 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5004 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5005 | break; |
| 5006 | case Lambda_kind: |
| 5007 | return compiler_lambda(c, e); |
| 5008 | case IfExp_kind: |
| 5009 | return compiler_ifexp(c, e); |
| 5010 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5011 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5013 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5014 | case GeneratorExp_kind: |
| 5015 | return compiler_genexp(c, e); |
| 5016 | case ListComp_kind: |
| 5017 | return compiler_listcomp(c, e); |
| 5018 | case SetComp_kind: |
| 5019 | return compiler_setcomp(c, e); |
| 5020 | case DictComp_kind: |
| 5021 | return compiler_dictcomp(c, e); |
| 5022 | case Yield_kind: |
| 5023 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5024 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5025 | if (e->v.Yield.value) { |
| 5026 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5027 | } |
| 5028 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5029 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5030 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5031 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5033 | case YieldFrom_kind: |
| 5034 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5035 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5036 | |
| 5037 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5038 | return compiler_error(c, "'yield from' inside async function"); |
| 5039 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5040 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5041 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5042 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5043 | ADDOP(c, YIELD_FROM); |
| 5044 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5045 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5046 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5047 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5048 | return compiler_error(c, "'await' outside function"); |
| 5049 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5050 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5051 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5052 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5053 | return compiler_error(c, "'await' outside async function"); |
| 5054 | } |
| 5055 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5056 | |
| 5057 | VISIT(c, expr, e->v.Await.value); |
| 5058 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5059 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5060 | ADDOP(c, YIELD_FROM); |
| 5061 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5062 | case Compare_kind: |
| 5063 | return compiler_compare(c, e); |
| 5064 | case Call_kind: |
| 5065 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5066 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5067 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5068 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5069 | case JoinedStr_kind: |
| 5070 | return compiler_joined_str(c, e); |
| 5071 | case FormattedValue_kind: |
| 5072 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5073 | /* The following exprs can be assignment targets. */ |
| 5074 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5075 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5076 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5077 | case Load: |
| 5078 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5079 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5080 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5081 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5082 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5083 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5084 | break; |
| 5085 | case Del: |
| 5086 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5087 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5088 | } |
| 5089 | break; |
| 5090 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5091 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | case Starred_kind: |
| 5093 | switch (e->v.Starred.ctx) { |
| 5094 | case Store: |
| 5095 | /* In all legitimate cases, the Starred node was already replaced |
| 5096 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5097 | return compiler_error(c, |
| 5098 | "starred assignment target must be in a list or tuple"); |
| 5099 | default: |
| 5100 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5101 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5103 | break; |
| 5104 | case Slice_kind: |
| 5105 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | case Name_kind: |
| 5107 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5108 | /* child nodes of List and Tuple will have expr_context set */ |
| 5109 | case List_kind: |
| 5110 | return compiler_list(c, e); |
| 5111 | case Tuple_kind: |
| 5112 | return compiler_tuple(c, e); |
| 5113 | } |
| 5114 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5118 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5119 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5120 | int old_lineno = c->u->u_lineno; |
| 5121 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5122 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5123 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5124 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5125 | c->u->u_col_offset = old_col_offset; |
| 5126 | return res; |
| 5127 | } |
| 5128 | |
| 5129 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5130 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5132 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5133 | expr_ty e = s->v.AugAssign.target; |
| 5134 | |
| 5135 | int old_lineno = c->u->u_lineno; |
| 5136 | int old_col_offset = c->u->u_col_offset; |
| 5137 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5139 | switch (e->kind) { |
| 5140 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5141 | VISIT(c, expr, e->v.Attribute.value); |
| 5142 | ADDOP(c, DUP_TOP); |
| 5143 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5144 | break; |
| 5145 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5146 | VISIT(c, expr, e->v.Subscript.value); |
| 5147 | VISIT(c, expr, e->v.Subscript.slice); |
| 5148 | ADDOP(c, DUP_TOP_TWO); |
| 5149 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5150 | break; |
| 5151 | case Name_kind: |
| 5152 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5153 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5154 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5155 | default: |
| 5156 | PyErr_Format(PyExc_SystemError, |
| 5157 | "invalid node type (%d) for augmented assignment", |
| 5158 | e->kind); |
| 5159 | return 0; |
| 5160 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5161 | |
| 5162 | c->u->u_lineno = old_lineno; |
| 5163 | c->u->u_col_offset = old_col_offset; |
| 5164 | |
| 5165 | VISIT(c, expr, s->v.AugAssign.value); |
| 5166 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5167 | |
| 5168 | SET_LOC(c, e); |
| 5169 | |
| 5170 | switch (e->kind) { |
| 5171 | case Attribute_kind: |
| 5172 | ADDOP(c, ROT_TWO); |
| 5173 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5174 | break; |
| 5175 | case Subscript_kind: |
| 5176 | ADDOP(c, ROT_THREE); |
| 5177 | ADDOP(c, STORE_SUBSCR); |
| 5178 | break; |
| 5179 | case Name_kind: |
| 5180 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5181 | default: |
| 5182 | Py_UNREACHABLE(); |
| 5183 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5184 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5188 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5189 | { |
| 5190 | VISIT(c, expr, e); |
| 5191 | ADDOP(c, POP_TOP); |
| 5192 | return 1; |
| 5193 | } |
| 5194 | |
| 5195 | static int |
| 5196 | check_annotation(struct compiler *c, stmt_ty s) |
| 5197 | { |
| 5198 | /* Annotations are only evaluated in a module or class. */ |
| 5199 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5200 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5201 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5202 | } |
| 5203 | return 1; |
| 5204 | } |
| 5205 | |
| 5206 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5207 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5208 | { |
| 5209 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5210 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5211 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5212 | 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] | 5213 | return 0; |
| 5214 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5215 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5216 | return 0; |
| 5217 | } |
| 5218 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5219 | return 0; |
| 5220 | } |
| 5221 | return 1; |
| 5222 | case Tuple_kind: { |
| 5223 | /* extended slice */ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 5224 | asdl_expr_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5225 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5226 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5227 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5228 | return 0; |
| 5229 | } |
| 5230 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5231 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5232 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5233 | default: |
| 5234 | return check_ann_expr(c, e); |
| 5235 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5236 | } |
| 5237 | |
| 5238 | static int |
| 5239 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5240 | { |
| 5241 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5242 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5243 | |
| 5244 | assert(s->kind == AnnAssign_kind); |
| 5245 | |
| 5246 | /* We perform the actual assignment first. */ |
| 5247 | if (s->v.AnnAssign.value) { |
| 5248 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5249 | VISIT(c, expr, targ); |
| 5250 | } |
| 5251 | switch (targ->kind) { |
| 5252 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5253 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5254 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5255 | /* If we have a simple name in a module or class, store annotation. */ |
| 5256 | if (s->v.AnnAssign.simple && |
| 5257 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5258 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Batuhan Taskaya | 044a104 | 2020-10-06 23:03:02 +0300 | [diff] [blame] | 5259 | VISIT(c, annexpr, s->v.AnnAssign.annotation); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5260 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5261 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5262 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5263 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5264 | } |
| 5265 | break; |
| 5266 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5267 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5268 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5269 | if (!s->v.AnnAssign.value && |
| 5270 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5271 | return 0; |
| 5272 | } |
| 5273 | break; |
| 5274 | case Subscript_kind: |
| 5275 | if (!s->v.AnnAssign.value && |
| 5276 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5277 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5278 | return 0; |
| 5279 | } |
| 5280 | break; |
| 5281 | default: |
| 5282 | PyErr_Format(PyExc_SystemError, |
| 5283 | "invalid node type (%d) for annotated assignment", |
| 5284 | targ->kind); |
| 5285 | return 0; |
| 5286 | } |
| 5287 | /* Annotation is evaluated last. */ |
| 5288 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5289 | return 0; |
| 5290 | } |
| 5291 | return 1; |
| 5292 | } |
| 5293 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5294 | /* Raises a SyntaxError and returns 0. |
| 5295 | If something goes wrong, a different exception may be raised. |
| 5296 | */ |
| 5297 | |
| 5298 | static int |
| 5299 | compiler_error(struct compiler *c, const char *errstr) |
| 5300 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5301 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5302 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5303 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5304 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5305 | if (!loc) { |
| 5306 | Py_INCREF(Py_None); |
| 5307 | loc = Py_None; |
| 5308 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5309 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5310 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | if (!u) |
| 5312 | goto exit; |
| 5313 | v = Py_BuildValue("(zO)", errstr, u); |
| 5314 | if (!v) |
| 5315 | goto exit; |
| 5316 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5317 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5318 | Py_DECREF(loc); |
| 5319 | Py_XDECREF(u); |
| 5320 | Py_XDECREF(v); |
| 5321 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5324 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5325 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5326 | and returns 0. |
| 5327 | */ |
| 5328 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5329 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5330 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5331 | va_list vargs; |
| 5332 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5333 | va_start(vargs, format); |
| 5334 | #else |
| 5335 | va_start(vargs); |
| 5336 | #endif |
| 5337 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5338 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5339 | if (msg == NULL) { |
| 5340 | return 0; |
| 5341 | } |
| 5342 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5343 | c->u->u_lineno, NULL, NULL) < 0) |
| 5344 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5345 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5346 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5347 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5348 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5349 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5350 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5351 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5352 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5353 | return 0; |
| 5354 | } |
| 5355 | Py_DECREF(msg); |
| 5356 | return 1; |
| 5357 | } |
| 5358 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5359 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5360 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5361 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5362 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5363 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5364 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5365 | if (ctx == Load) { |
| 5366 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5367 | return 0; |
| 5368 | } |
| 5369 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5370 | return 0; |
| 5371 | } |
| 5372 | } |
| 5373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5374 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5376 | case Store: op = STORE_SUBSCR; break; |
| 5377 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5379 | assert(op); |
| 5380 | VISIT(c, expr, e->v.Subscript.value); |
| 5381 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5382 | ADDOP(c, op); |
| 5383 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5384 | } |
| 5385 | |
| 5386 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5387 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5388 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5389 | int n = 2; |
| 5390 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5391 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5393 | if (s->v.Slice.lower) { |
| 5394 | VISIT(c, expr, s->v.Slice.lower); |
| 5395 | } |
| 5396 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5397 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5400 | if (s->v.Slice.upper) { |
| 5401 | VISIT(c, expr, s->v.Slice.upper); |
| 5402 | } |
| 5403 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5404 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | if (s->v.Slice.step) { |
| 5408 | n++; |
| 5409 | VISIT(c, expr, s->v.Slice.step); |
| 5410 | } |
| 5411 | ADDOP_I(c, BUILD_SLICE, n); |
| 5412 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5413 | } |
| 5414 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5415 | /* End of the compiler section, beginning of the assembler section */ |
| 5416 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5417 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5418 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5419 | |
| 5420 | XXX must handle implicit jumps from one block to next |
| 5421 | */ |
| 5422 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5423 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5424 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5425 | int a_offset; /* offset into bytecode */ |
| 5426 | int a_nblocks; /* number of reachable blocks */ |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5427 | basicblock **a_reverse_postorder; /* list of blocks in dfs postorder */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5429 | int a_lnotab_off; /* offset into lnotab */ |
| 5430 | int a_lineno; /* last lineno of emitted instruction */ |
| 5431 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5432 | }; |
| 5433 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5434 | static void |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5435 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5436 | { |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5437 | |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5438 | /* There is no real depth-first-search to do here because all the |
| 5439 | * blocks are emitted in topological order already, so we just need to |
| 5440 | * follow the b_next pointers and place them in a->a_reverse_postorder in |
| 5441 | * reverse order and make sure that the first one starts at 0. */ |
| 5442 | |
| 5443 | for (a->a_nblocks = 0; b != NULL; b = b->b_next) { |
| 5444 | a->a_reverse_postorder[a->a_nblocks++] = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5445 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5448 | Py_LOCAL_INLINE(void) |
| 5449 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5450 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5451 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5452 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5453 | assert(b->b_startdepth < 0); |
| 5454 | b->b_startdepth = depth; |
| 5455 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5456 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5457 | } |
| 5458 | |
| 5459 | /* Find the flow path that needs the largest stack. We assume that |
| 5460 | * cycles in the flow graph have no net effect on the stack depth. |
| 5461 | */ |
| 5462 | static int |
| 5463 | stackdepth(struct compiler *c) |
| 5464 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5465 | basicblock *b, *entryblock = NULL; |
| 5466 | basicblock **stack, **sp; |
| 5467 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5468 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | b->b_startdepth = INT_MIN; |
| 5470 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5471 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5472 | } |
| 5473 | if (!entryblock) |
| 5474 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5475 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5476 | if (!stack) { |
| 5477 | PyErr_NoMemory(); |
| 5478 | return -1; |
| 5479 | } |
| 5480 | |
| 5481 | sp = stack; |
| 5482 | stackdepth_push(&sp, entryblock, 0); |
| 5483 | while (sp != stack) { |
| 5484 | b = *--sp; |
| 5485 | int depth = b->b_startdepth; |
| 5486 | assert(depth >= 0); |
| 5487 | basicblock *next = b->b_next; |
| 5488 | for (int i = 0; i < b->b_iused; i++) { |
| 5489 | struct instr *instr = &b->b_instr[i]; |
| 5490 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5491 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 5492 | _Py_FatalErrorFormat(__func__, |
| 5493 | "opcode = %d", instr->i_opcode); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5494 | } |
| 5495 | int new_depth = depth + effect; |
| 5496 | if (new_depth > maxdepth) { |
| 5497 | maxdepth = new_depth; |
| 5498 | } |
| 5499 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5500 | if (is_jump(instr)) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5501 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5502 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5503 | int target_depth = depth + effect; |
| 5504 | if (target_depth > maxdepth) { |
| 5505 | maxdepth = target_depth; |
| 5506 | } |
| 5507 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5508 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5509 | } |
| 5510 | depth = new_depth; |
| 5511 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5512 | instr->i_opcode == JUMP_FORWARD || |
| 5513 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5514 | instr->i_opcode == RAISE_VARARGS || |
| 5515 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5516 | { |
| 5517 | /* remaining code is dead */ |
| 5518 | next = NULL; |
| 5519 | break; |
| 5520 | } |
| 5521 | } |
| 5522 | if (next != NULL) { |
| 5523 | stackdepth_push(&sp, next, depth); |
| 5524 | } |
| 5525 | } |
| 5526 | PyObject_Free(stack); |
| 5527 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
| 5530 | static int |
| 5531 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5532 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5533 | memset(a, 0, sizeof(struct assembler)); |
| 5534 | a->a_lineno = firstlineno; |
| 5535 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5536 | if (!a->a_bytecode) |
| 5537 | return 0; |
| 5538 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5539 | if (!a->a_lnotab) |
| 5540 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5541 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5542 | PyErr_NoMemory(); |
| 5543 | return 0; |
| 5544 | } |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5545 | a->a_reverse_postorder = (basicblock **)PyObject_Malloc( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5546 | sizeof(basicblock *) * nblocks); |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5547 | if (!a->a_reverse_postorder) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | PyErr_NoMemory(); |
| 5549 | return 0; |
| 5550 | } |
| 5551 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5552 | } |
| 5553 | |
| 5554 | static void |
| 5555 | assemble_free(struct assembler *a) |
| 5556 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5557 | Py_XDECREF(a->a_bytecode); |
| 5558 | Py_XDECREF(a->a_lnotab); |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5559 | if (a->a_reverse_postorder) |
| 5560 | PyObject_Free(a->a_reverse_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5563 | static int |
| 5564 | blocksize(basicblock *b) |
| 5565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | int i; |
| 5567 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5570 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5571 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5572 | } |
| 5573 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5574 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5575 | the instruction's bytecode offset and line number. See |
| 5576 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5577 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5578 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5579 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5581 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5582 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5583 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5584 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5585 | d_lineno = i->i_lineno - a->a_lineno; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5586 | if (d_lineno == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5587 | return 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
| 5591 | assert(d_bytecode >= 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5593 | if (d_bytecode > 255) { |
| 5594 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5595 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5596 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5597 | if (nbytes >= len) { |
| 5598 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5599 | len = nbytes; |
| 5600 | else if (len <= INT_MAX / 2) |
| 5601 | len *= 2; |
| 5602 | else { |
| 5603 | PyErr_NoMemory(); |
| 5604 | return 0; |
| 5605 | } |
| 5606 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5607 | return 0; |
| 5608 | } |
| 5609 | lnotab = (unsigned char *) |
| 5610 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5611 | for (j = 0; j < ncodes; j++) { |
| 5612 | *lnotab++ = 255; |
| 5613 | *lnotab++ = 0; |
| 5614 | } |
| 5615 | d_bytecode -= ncodes * 255; |
| 5616 | a->a_lnotab_off += ncodes * 2; |
| 5617 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5618 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5619 | |
| 5620 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5621 | int j, nbytes, ncodes, k; |
| 5622 | if (d_lineno < 0) { |
| 5623 | k = -128; |
| 5624 | /* use division on positive numbers */ |
| 5625 | ncodes = (-d_lineno) / 128; |
| 5626 | } |
| 5627 | else { |
| 5628 | k = 127; |
| 5629 | ncodes = d_lineno / 127; |
| 5630 | } |
| 5631 | d_lineno -= ncodes * k; |
| 5632 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5634 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5635 | if (nbytes >= len) { |
| 5636 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5637 | len = nbytes; |
| 5638 | else if (len <= INT_MAX / 2) |
| 5639 | len *= 2; |
| 5640 | else { |
| 5641 | PyErr_NoMemory(); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5645 | return 0; |
| 5646 | } |
| 5647 | lnotab = (unsigned char *) |
| 5648 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5649 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5650 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | d_bytecode = 0; |
| 5652 | for (j = 1; j < ncodes; j++) { |
| 5653 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5654 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5655 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5656 | a->a_lnotab_off += ncodes * 2; |
| 5657 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5658 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5660 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5661 | if (a->a_lnotab_off + 2 >= len) { |
| 5662 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5663 | return 0; |
| 5664 | } |
| 5665 | lnotab = (unsigned char *) |
| 5666 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5667 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5668 | a->a_lnotab_off += 2; |
| 5669 | if (d_bytecode) { |
| 5670 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5671 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5672 | } |
| 5673 | else { /* First line of a block; def stmt, etc. */ |
| 5674 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5675 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5676 | } |
| 5677 | a->a_lineno = i->i_lineno; |
| 5678 | a->a_lineno_off = a->a_offset; |
| 5679 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5680 | } |
| 5681 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5682 | /* assemble_emit() |
| 5683 | Extend the bytecode with a new instruction. |
| 5684 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5685 | */ |
| 5686 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5687 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5688 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5689 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5690 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5691 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5692 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5693 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5694 | arg = i->i_oparg; |
| 5695 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5697 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5698 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5699 | if (len > PY_SSIZE_T_MAX / 2) |
| 5700 | return 0; |
| 5701 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5702 | return 0; |
| 5703 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5704 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5705 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5706 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5708 | } |
| 5709 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5710 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5711 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5712 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5713 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5714 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5716 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5717 | /* Compute the size of each block and fixup jump args. |
| 5718 | Replace block pointer with position in bytecode. */ |
| 5719 | do { |
| 5720 | totsize = 0; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5721 | for (i = 0; i < a->a_nblocks; i++) { |
| 5722 | b = a->a_reverse_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5723 | bsize = blocksize(b); |
| 5724 | b->b_offset = totsize; |
| 5725 | totsize += bsize; |
| 5726 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5727 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5728 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5729 | bsize = b->b_offset; |
| 5730 | for (i = 0; i < b->b_iused; i++) { |
| 5731 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5732 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5733 | /* Relative jumps are computed relative to |
| 5734 | the instruction pointer after fetching |
| 5735 | the jump instruction. |
| 5736 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5737 | bsize += isize; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5738 | if (is_jump(instr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5739 | instr->i_oparg = instr->i_target->b_offset; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5740 | if (is_relative_jump(instr)) { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5741 | instr->i_oparg -= bsize; |
| 5742 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5743 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5744 | if (instrsize(instr->i_oparg) != isize) { |
| 5745 | extended_arg_recompile = 1; |
| 5746 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5747 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5748 | } |
| 5749 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5751 | /* XXX: This is an awful hack that could hurt performance, but |
| 5752 | on the bright side it should work until we come up |
| 5753 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5755 | The issue is that in the first loop blocksize() is called |
| 5756 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5757 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5758 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5760 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5761 | The only EXTENDED_ARGs that could be popping up are |
| 5762 | ones in jump instructions. So this should converge |
| 5763 | fairly quickly. |
| 5764 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5765 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5768 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5769 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5770 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5771 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5772 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5774 | tuple = PyTuple_New(size); |
| 5775 | if (tuple == NULL) |
| 5776 | return NULL; |
| 5777 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5778 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5779 | Py_INCREF(k); |
| 5780 | assert((i - offset) < size); |
| 5781 | assert((i - offset) >= 0); |
| 5782 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5783 | } |
| 5784 | return tuple; |
| 5785 | } |
| 5786 | |
| 5787 | static PyObject * |
| 5788 | consts_dict_keys_inorder(PyObject *dict) |
| 5789 | { |
| 5790 | PyObject *consts, *k, *v; |
| 5791 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5792 | |
| 5793 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5794 | if (consts == NULL) |
| 5795 | return NULL; |
| 5796 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5797 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5798 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5799 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5800 | * the object we want is always second. */ |
| 5801 | if (PyTuple_CheckExact(k)) { |
| 5802 | k = PyTuple_GET_ITEM(k, 1); |
| 5803 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5804 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5805 | assert(i < size); |
| 5806 | assert(i >= 0); |
| 5807 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5809 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5810 | } |
| 5811 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5812 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5813 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5815 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5816 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5817 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5818 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5819 | if (ste->ste_nested) |
| 5820 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5821 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5823 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5824 | flags |= CO_COROUTINE; |
| 5825 | if (ste->ste_generator && ste->ste_coroutine) |
| 5826 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | if (ste->ste_varargs) |
| 5828 | flags |= CO_VARARGS; |
| 5829 | if (ste->ste_varkeywords) |
| 5830 | flags |= CO_VARKEYWORDS; |
| 5831 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5833 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5834 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5835 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5836 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5837 | ste->ste_coroutine && |
| 5838 | !ste->ste_generator) { |
| 5839 | flags |= CO_COROUTINE; |
| 5840 | } |
| 5841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5842 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5845 | // Merge *tuple* with constant cache. |
| 5846 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5847 | static int |
| 5848 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5849 | { |
| 5850 | assert(PyTuple_CheckExact(*tuple)); |
| 5851 | |
| 5852 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5853 | if (key == NULL) { |
| 5854 | return 0; |
| 5855 | } |
| 5856 | |
| 5857 | // t is borrowed reference |
| 5858 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5859 | Py_DECREF(key); |
| 5860 | if (t == NULL) { |
| 5861 | return 0; |
| 5862 | } |
| 5863 | if (t == key) { // tuple is new constant. |
| 5864 | return 1; |
| 5865 | } |
| 5866 | |
| 5867 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5868 | Py_INCREF(u); |
| 5869 | Py_DECREF(*tuple); |
| 5870 | *tuple = u; |
| 5871 | return 1; |
| 5872 | } |
| 5873 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5874 | static PyCodeObject * |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5875 | makecode(struct compiler *c, struct assembler *a, PyObject *consts) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5877 | PyCodeObject *co = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5878 | PyObject *names = NULL; |
| 5879 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5880 | PyObject *name = NULL; |
| 5881 | PyObject *freevars = NULL; |
| 5882 | PyObject *cellvars = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5883 | Py_ssize_t nlocals; |
| 5884 | int nlocals_int; |
| 5885 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5886 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5888 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5889 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5890 | if (!names || !varnames) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5891 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5892 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5893 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5894 | if (!cellvars) |
| 5895 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5896 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5897 | if (!freevars) |
| 5898 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5899 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5900 | if (!merge_const_tuple(c, &names) || |
| 5901 | !merge_const_tuple(c, &varnames) || |
| 5902 | !merge_const_tuple(c, &cellvars) || |
| 5903 | !merge_const_tuple(c, &freevars)) |
| 5904 | { |
| 5905 | goto error; |
| 5906 | } |
| 5907 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5908 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5909 | assert(nlocals < INT_MAX); |
| 5910 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5912 | flags = compute_code_flags(c); |
| 5913 | if (flags < 0) |
| 5914 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5915 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5916 | consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5917 | if (consts == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5918 | goto error; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5919 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5920 | if (!merge_const_tuple(c, &consts)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5921 | Py_DECREF(consts); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5922 | goto error; |
| 5923 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5924 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5925 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5926 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5927 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5928 | maxdepth = stackdepth(c); |
| 5929 | if (maxdepth < 0) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5930 | Py_DECREF(consts); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5931 | goto error; |
| 5932 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5933 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5934 | posonlyargcount, kwonlyargcount, nlocals_int, |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5935 | maxdepth, flags, a->a_bytecode, consts, names, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5936 | varnames, freevars, cellvars, c->c_filename, |
| 5937 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5938 | Py_DECREF(consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5939 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5940 | Py_XDECREF(names); |
| 5941 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | Py_XDECREF(name); |
| 5943 | Py_XDECREF(freevars); |
| 5944 | Py_XDECREF(cellvars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5945 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5946 | } |
| 5947 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5948 | |
| 5949 | /* For debugging purposes only */ |
| 5950 | #if 0 |
| 5951 | static void |
| 5952 | dump_instr(const struct instr *i) |
| 5953 | { |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 5954 | const char *jrel = (is_relative_jump(instr)) ? "jrel " : ""; |
| 5955 | const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : ""; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5956 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5958 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5959 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5960 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5961 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5962 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5963 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5964 | } |
| 5965 | |
| 5966 | static void |
| 5967 | dump_basicblock(const basicblock *b) |
| 5968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | const char *b_return = b->b_return ? "return " : ""; |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 5970 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n", |
| 5971 | b->b_iused, b->b_startdepth, b->b_offset, b_return); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5972 | if (b->b_instr) { |
| 5973 | int i; |
| 5974 | for (i = 0; i < b->b_iused; i++) { |
| 5975 | fprintf(stderr, " [%02d] ", i); |
| 5976 | dump_instr(b->b_instr + i); |
| 5977 | } |
| 5978 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5979 | } |
| 5980 | #endif |
| 5981 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5982 | static int |
| 5983 | optimize_cfg(struct assembler *a, PyObject *consts); |
| 5984 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5985 | static PyCodeObject * |
| 5986 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5987 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5988 | basicblock *b, *entryblock; |
| 5989 | struct assembler a; |
| 5990 | int i, j, nblocks; |
| 5991 | PyCodeObject *co = NULL; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 5992 | PyObject *consts = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5994 | /* Make sure every block that falls off the end returns None. |
| 5995 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5996 | block ends with a jump or return b_next shouldn't set. |
| 5997 | */ |
| 5998 | if (!c->u->u_curblock->b_return) { |
| 5999 | NEXT_BLOCK(c); |
| 6000 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6001 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6002 | ADDOP(c, RETURN_VALUE); |
| 6003 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6005 | nblocks = 0; |
| 6006 | entryblock = NULL; |
| 6007 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6008 | nblocks++; |
| 6009 | entryblock = b; |
| 6010 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6012 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6013 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 6014 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6015 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 6016 | else |
| 6017 | c->u->u_firstlineno = 1; |
| 6018 | } |
| 6019 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6020 | goto error; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6021 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6022 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6023 | consts = consts_dict_keys_inorder(c->u->u_consts); |
| 6024 | if (consts == NULL) { |
| 6025 | goto error; |
| 6026 | } |
| 6027 | if (optimize_cfg(&a, consts)) { |
| 6028 | goto error; |
| 6029 | } |
| 6030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6031 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6032 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6033 | |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6034 | /* Emit code in reverse postorder from dfs. */ |
Pablo Galindo | 60eb9f1 | 2020-06-28 01:55:47 +0100 | [diff] [blame] | 6035 | for (i = 0; i < a.a_nblocks; i++) { |
| 6036 | b = a.a_reverse_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6037 | for (j = 0; j < b->b_iused; j++) |
| 6038 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6039 | goto error; |
| 6040 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6042 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6043 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6044 | 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] | 6045 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6046 | |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6047 | co = makecode(c, &a, consts); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6048 | error: |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6049 | Py_XDECREF(consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6050 | assemble_free(&a); |
| 6051 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6052 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6053 | |
| 6054 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6055 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6056 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6057 | PyArena *arena) |
| 6058 | { |
| 6059 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6060 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6061 | |
| 6062 | |
| 6063 | /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n |
| 6064 | with LOAD_CONST (c1, c2, ... cn). |
| 6065 | The consts table must still be in list form so that the |
| 6066 | new constant (c1, c2, ... cn) can be appended. |
| 6067 | Called with codestr pointing to the first LOAD_CONST. |
| 6068 | */ |
| 6069 | static int |
| 6070 | fold_tuple_on_constants(struct instr *inst, |
| 6071 | int n, PyObject *consts) |
| 6072 | { |
| 6073 | /* Pre-conditions */ |
| 6074 | assert(PyList_CheckExact(consts)); |
| 6075 | assert(inst[n].i_opcode == BUILD_TUPLE); |
| 6076 | assert(inst[n].i_oparg == n); |
| 6077 | |
| 6078 | for (int i = 0; i < n; i++) { |
| 6079 | if (inst[i].i_opcode != LOAD_CONST) { |
| 6080 | return 0; |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | /* Buildup new tuple of constants */ |
| 6085 | PyObject *newconst = PyTuple_New(n); |
| 6086 | if (newconst == NULL) { |
| 6087 | return -1; |
| 6088 | } |
| 6089 | for (int i = 0; i < n; i++) { |
| 6090 | int arg = inst[i].i_oparg; |
| 6091 | PyObject *constant = PyList_GET_ITEM(consts, arg); |
| 6092 | Py_INCREF(constant); |
| 6093 | PyTuple_SET_ITEM(newconst, i, constant); |
| 6094 | } |
| 6095 | Py_ssize_t index = PyList_GET_SIZE(consts); |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6096 | if ((size_t)index >= (size_t)INT_MAX - 1) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6097 | Py_DECREF(newconst); |
| 6098 | PyErr_SetString(PyExc_OverflowError, "too many constants"); |
| 6099 | return -1; |
| 6100 | } |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6101 | if (PyList_Append(consts, newconst)) { |
| 6102 | Py_DECREF(newconst); |
| 6103 | return -1; |
| 6104 | } |
| 6105 | Py_DECREF(newconst); |
| 6106 | for (int i = 0; i < n; i++) { |
| 6107 | inst[i].i_opcode = NOP; |
| 6108 | } |
| 6109 | inst[n].i_opcode = LOAD_CONST; |
Victor Stinner | 71f2ff4 | 2020-09-23 14:06:55 +0200 | [diff] [blame] | 6110 | inst[n].i_oparg = (int)index; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6111 | return 0; |
| 6112 | } |
| 6113 | |
| 6114 | |
| 6115 | /* Optimization */ |
| 6116 | static int |
| 6117 | optimize_basic_block(basicblock *bb, PyObject *consts) |
| 6118 | { |
| 6119 | assert(PyList_CheckExact(consts)); |
| 6120 | struct instr nop; |
| 6121 | nop.i_opcode = NOP; |
| 6122 | struct instr *target; |
| 6123 | int lineno; |
| 6124 | for (int i = 0; i < bb->b_iused; i++) { |
| 6125 | struct instr *inst = &bb->b_instr[i]; |
| 6126 | int oparg = inst->i_oparg; |
| 6127 | int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6128 | if (is_jump(inst)) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6129 | /* Skip over empty basic blocks. */ |
| 6130 | while (inst->i_target->b_iused == 0) { |
| 6131 | inst->i_target = inst->i_target->b_next; |
| 6132 | } |
| 6133 | target = &inst->i_target->b_instr[0]; |
| 6134 | } |
| 6135 | else { |
| 6136 | target = &nop; |
| 6137 | } |
| 6138 | switch (inst->i_opcode) { |
| 6139 | /* Skip over LOAD_CONST trueconst |
| 6140 | POP_JUMP_IF_FALSE xx. This improves |
| 6141 | "while 1" performance. */ |
| 6142 | case LOAD_CONST: |
| 6143 | if (nextop != POP_JUMP_IF_FALSE) { |
| 6144 | break; |
| 6145 | } |
| 6146 | PyObject* cnt = PyList_GET_ITEM(consts, oparg); |
| 6147 | int is_true = PyObject_IsTrue(cnt); |
| 6148 | if (is_true == -1) { |
| 6149 | goto error; |
| 6150 | } |
| 6151 | if (is_true == 1) { |
| 6152 | inst->i_opcode = NOP; |
| 6153 | bb->b_instr[i+1].i_opcode = NOP; |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6154 | } |
| 6155 | break; |
| 6156 | |
| 6157 | /* Try to fold tuples of constants. |
| 6158 | Skip over BUILD_SEQN 1 UNPACK_SEQN 1. |
| 6159 | Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2. |
| 6160 | Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */ |
| 6161 | case BUILD_TUPLE: |
| 6162 | if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) { |
| 6163 | switch(oparg) { |
| 6164 | case 1: |
| 6165 | inst->i_opcode = NOP; |
| 6166 | bb->b_instr[i+1].i_opcode = NOP; |
| 6167 | break; |
| 6168 | case 2: |
| 6169 | inst->i_opcode = ROT_TWO; |
| 6170 | bb->b_instr[i+1].i_opcode = NOP; |
| 6171 | break; |
| 6172 | case 3: |
| 6173 | inst->i_opcode = ROT_THREE; |
| 6174 | bb->b_instr[i+1].i_opcode = ROT_TWO; |
| 6175 | } |
| 6176 | break; |
| 6177 | } |
| 6178 | if (i >= oparg) { |
| 6179 | if (fold_tuple_on_constants(inst-oparg, oparg, consts)) { |
| 6180 | goto error; |
| 6181 | } |
| 6182 | } |
| 6183 | break; |
| 6184 | |
| 6185 | /* Simplify conditional jump to conditional jump where the |
| 6186 | result of the first test implies the success of a similar |
| 6187 | test or the failure of the opposite test. |
| 6188 | Arises in code like: |
| 6189 | "a and b or c" |
| 6190 | "(a and b) and c" |
| 6191 | "(a or b) or c" |
| 6192 | "(a or b) and c" |
| 6193 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z |
| 6194 | --> x:JUMP_IF_FALSE_OR_POP z |
| 6195 | x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z |
| 6196 | --> x:POP_JUMP_IF_FALSE y+1 |
| 6197 | where y+1 is the instruction following the second test. |
| 6198 | */ |
| 6199 | case JUMP_IF_FALSE_OR_POP: |
| 6200 | switch(target->i_opcode) { |
| 6201 | case POP_JUMP_IF_FALSE: |
| 6202 | *inst = *target; |
| 6203 | break; |
| 6204 | case JUMP_ABSOLUTE: |
| 6205 | case JUMP_FORWARD: |
| 6206 | case JUMP_IF_FALSE_OR_POP: |
| 6207 | inst->i_target = target->i_target; |
| 6208 | break; |
| 6209 | case JUMP_IF_TRUE_OR_POP: |
| 6210 | assert (inst->i_target->b_iused == 1); |
| 6211 | inst->i_opcode = POP_JUMP_IF_FALSE; |
| 6212 | inst->i_target = inst->i_target->b_next; |
| 6213 | break; |
| 6214 | } |
| 6215 | break; |
| 6216 | |
| 6217 | case JUMP_IF_TRUE_OR_POP: |
| 6218 | switch(target->i_opcode) { |
| 6219 | case POP_JUMP_IF_TRUE: |
| 6220 | *inst = *target; |
| 6221 | break; |
| 6222 | case JUMP_ABSOLUTE: |
| 6223 | case JUMP_FORWARD: |
| 6224 | case JUMP_IF_TRUE_OR_POP: |
| 6225 | inst->i_target = target->i_target; |
| 6226 | break; |
| 6227 | case JUMP_IF_FALSE_OR_POP: |
| 6228 | assert (inst->i_target->b_iused == 1); |
| 6229 | inst->i_opcode = POP_JUMP_IF_TRUE; |
| 6230 | inst->i_target = inst->i_target->b_next; |
| 6231 | break; |
| 6232 | } |
| 6233 | break; |
| 6234 | |
| 6235 | case POP_JUMP_IF_FALSE: |
| 6236 | switch(target->i_opcode) { |
| 6237 | case JUMP_ABSOLUTE: |
| 6238 | case JUMP_FORWARD: |
| 6239 | inst->i_target = target->i_target; |
| 6240 | break; |
| 6241 | } |
| 6242 | break; |
| 6243 | |
| 6244 | case POP_JUMP_IF_TRUE: |
| 6245 | switch(target->i_opcode) { |
| 6246 | case JUMP_ABSOLUTE: |
| 6247 | case JUMP_FORWARD: |
| 6248 | inst->i_target = target->i_target; |
| 6249 | break; |
| 6250 | } |
| 6251 | break; |
| 6252 | |
| 6253 | case JUMP_ABSOLUTE: |
| 6254 | case JUMP_FORWARD: |
| 6255 | switch(target->i_opcode) { |
| 6256 | case JUMP_FORWARD: |
| 6257 | inst->i_target = target->i_target; |
| 6258 | break; |
| 6259 | case JUMP_ABSOLUTE: |
| 6260 | case RETURN_VALUE: |
| 6261 | case RERAISE: |
| 6262 | case RAISE_VARARGS: |
| 6263 | lineno = inst->i_lineno; |
| 6264 | *inst = *target; |
| 6265 | inst->i_lineno = lineno; |
| 6266 | break; |
| 6267 | } |
| 6268 | break; |
| 6269 | } |
| 6270 | } |
| 6271 | return 0; |
| 6272 | error: |
| 6273 | return -1; |
| 6274 | } |
| 6275 | |
| 6276 | |
| 6277 | static void |
| 6278 | clean_basic_block(basicblock *bb) { |
| 6279 | /* Remove NOPs and any code following a return or re-raise. */ |
| 6280 | int dest = 0; |
| 6281 | for (int src = 0; src < bb->b_iused; src++) { |
| 6282 | switch(bb->b_instr[src].i_opcode) { |
| 6283 | case NOP: |
| 6284 | /* skip */ |
| 6285 | break; |
| 6286 | case RETURN_VALUE: |
| 6287 | case RERAISE: |
| 6288 | bb->b_next = NULL; |
| 6289 | bb->b_instr[dest] = bb->b_instr[src]; |
| 6290 | dest++; |
| 6291 | goto end; |
| 6292 | default: |
| 6293 | if (dest != src) { |
| 6294 | bb->b_instr[dest] = bb->b_instr[src]; |
| 6295 | } |
| 6296 | dest++; |
| 6297 | break; |
| 6298 | } |
| 6299 | } |
| 6300 | end: |
| 6301 | assert(dest <= bb->b_iused); |
| 6302 | bb->b_iused = dest; |
| 6303 | } |
| 6304 | |
| 6305 | static int |
| 6306 | mark_reachable(struct assembler *a) { |
| 6307 | basicblock **stack, **sp; |
| 6308 | sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks); |
| 6309 | if (stack == NULL) { |
| 6310 | return -1; |
| 6311 | } |
| 6312 | basicblock *entry = a->a_reverse_postorder[0]; |
| 6313 | entry->b_reachable = 1; |
| 6314 | *sp++ = entry; |
| 6315 | while (sp > stack) { |
| 6316 | basicblock *b = *(--sp); |
| 6317 | if (b->b_next && b->b_next->b_reachable == 0) { |
| 6318 | b->b_next->b_reachable = 1; |
| 6319 | *sp++ = b->b_next; |
| 6320 | } |
| 6321 | for (int i = 0; i < b->b_iused; i++) { |
| 6322 | basicblock *target; |
Mark Shannon | 582aaf1 | 2020-08-04 17:30:11 +0100 | [diff] [blame] | 6323 | if (is_jump(&b->b_instr[i])) { |
Mark Shannon | 6e8128f | 2020-07-30 10:03:00 +0100 | [diff] [blame] | 6324 | target = b->b_instr[i].i_target; |
| 6325 | if (target->b_reachable == 0) { |
| 6326 | target->b_reachable = 1; |
| 6327 | *sp++ = target; |
| 6328 | } |
| 6329 | } |
| 6330 | } |
| 6331 | } |
| 6332 | PyObject_Free(stack); |
| 6333 | return 0; |
| 6334 | } |
| 6335 | |
| 6336 | |
| 6337 | /* Perform basic peephole optimizations on a control flow graph. |
| 6338 | The consts object should still be in list form to allow new constants |
| 6339 | to be appended. |
| 6340 | |
| 6341 | All transformations keep the code size the same or smaller. |
| 6342 | For those that reduce size, the gaps are initially filled with |
| 6343 | NOPs. Later those NOPs are removed. |
| 6344 | */ |
| 6345 | |
| 6346 | static int |
| 6347 | optimize_cfg(struct assembler *a, PyObject *consts) |
| 6348 | { |
| 6349 | for (int i = 0; i < a->a_nblocks; i++) { |
| 6350 | if (optimize_basic_block(a->a_reverse_postorder[i], consts)) { |
| 6351 | return -1; |
| 6352 | } |
| 6353 | clean_basic_block(a->a_reverse_postorder[i]); |
| 6354 | assert(a->a_reverse_postorder[i]->b_reachable == 0); |
| 6355 | } |
| 6356 | if (mark_reachable(a)) { |
| 6357 | return -1; |
| 6358 | } |
| 6359 | /* Delete unreachable instructions */ |
| 6360 | for (int i = 0; i < a->a_nblocks; i++) { |
| 6361 | if (a->a_reverse_postorder[i]->b_reachable == 0) { |
| 6362 | a->a_reverse_postorder[i]->b_iused = 0; |
| 6363 | } |
| 6364 | } |
| 6365 | return 0; |
| 6366 | } |
| 6367 | |
| 6368 | /* Retained for API compatibility. |
| 6369 | * Optimization is now done in optimize_cfg */ |
| 6370 | |
| 6371 | PyObject * |
| 6372 | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
| 6373 | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
| 6374 | { |
| 6375 | Py_INCREF(code); |
| 6376 | return code; |
| 6377 | } |
| 6378 | |