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. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | * 5. Optimize the byte code (peephole optimizations). See peephole.c |
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 | |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 26 | #include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */ |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 27 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 28 | #include "ast.h" |
| 29 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 30 | #include "symtable.h" |
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 i_jabs : 1; |
| 50 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | unsigned char i_opcode; |
| 52 | int i_oparg; |
| 53 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 54 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 57 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 59 | reverse order that the block are allocated. b_list points to the next |
| 60 | 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] | 61 | struct basicblock_ *b_list; |
| 62 | /* number of instructions used */ |
| 63 | int b_iused; |
| 64 | /* length of instruction array (b_instr) */ |
| 65 | int b_ialloc; |
| 66 | /* pointer to an array of instructions, initially NULL */ |
| 67 | struct instr *b_instr; |
| 68 | /* If b_next is non-NULL, it is a pointer to the next |
| 69 | block reached by normal control flow. */ |
| 70 | struct basicblock_ *b_next; |
| 71 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 72 | unsigned b_seen : 1; |
| 73 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 74 | unsigned b_return : 1; |
| 75 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 76 | int b_startdepth; |
| 77 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 78 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 79 | } basicblock; |
| 80 | |
| 81 | /* fblockinfo tracks the current frame block. |
| 82 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 83 | A frame block is used to handle loops, try/except, and try/finally. |
| 84 | It's called a frame block to distinguish it from a basic block in the |
| 85 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 86 | */ |
| 87 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 88 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END, |
| 89 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 90 | |
| 91 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | enum fblocktype fb_type; |
| 93 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 94 | /* (optional) type-specific exit or cleanup block */ |
| 95 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 96 | /* (optional) additional information required for unwinding */ |
| 97 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 100 | enum { |
| 101 | COMPILER_SCOPE_MODULE, |
| 102 | COMPILER_SCOPE_CLASS, |
| 103 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 104 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 105 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 106 | COMPILER_SCOPE_COMPREHENSION, |
| 107 | }; |
| 108 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | /* The following items change on entry and exit of code blocks. |
| 110 | They must be saved and restored when returning to a block. |
| 111 | */ |
| 112 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 116 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 117 | int u_scope_type; |
| 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | /* The following fields are dicts that map objects to |
| 120 | the index of them in co_XXX. The index is used as |
| 121 | the argument for opcodes that refer to those collections. |
| 122 | */ |
| 123 | PyObject *u_consts; /* all constants */ |
| 124 | PyObject *u_names; /* all names */ |
| 125 | PyObject *u_varnames; /* local variables */ |
| 126 | PyObject *u_cellvars; /* cell variables */ |
| 127 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 130 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 131 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 132 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 133 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | /* Pointer to the most recently allocated block. By following b_list |
| 135 | members, you can reach all early allocated blocks. */ |
| 136 | basicblock *u_blocks; |
| 137 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | int u_nfblocks; |
| 140 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | int u_firstlineno; /* the first lineno of the block */ |
| 143 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 144 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 148 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 149 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | 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] | 151 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 152 | |
| 153 | Note that we don't track recursion levels during compilation - the |
| 154 | task of detecting and rejecting excessive levels of nesting is |
| 155 | handled by the symbol analysis pass. |
| 156 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 157 | */ |
| 158 | |
| 159 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 160 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | struct symtable *c_st; |
| 162 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 163 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 165 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | int c_interactive; /* true if in interactive mode */ |
| 167 | int c_nestlevel; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 168 | int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode |
| 169 | if this value is different from zero. |
| 170 | This can be used to temporarily visit |
| 171 | nodes without emitting bytecode to |
| 172 | check only errors. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 173 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 174 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 175 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | struct compiler_unit *u; /* compiler state for current block */ |
| 177 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 178 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 181 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 182 | static void compiler_free(struct compiler *); |
| 183 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 184 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 186 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 188 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 189 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 190 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 191 | |
| 192 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 193 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 194 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 195 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 196 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 197 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 198 | static int compiler_subscript(struct compiler *, expr_ty); |
| 199 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 200 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 201 | static int inplace_binop(operator_ty); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 202 | static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 203 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 204 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 205 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 206 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 207 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 208 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 210 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 211 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 212 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 213 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 214 | static int compiler_sync_comprehension_generator( |
| 215 | struct compiler *c, |
| 216 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 217 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 218 | expr_ty elt, expr_ty val, int type); |
| 219 | |
| 220 | static int compiler_async_comprehension_generator( |
| 221 | struct compiler *c, |
| 222 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 223 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 224 | expr_ty elt, expr_ty val, int type); |
| 225 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 226 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 227 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 229 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 230 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 231 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 232 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | /* Name mangling: __private becomes _classname__private. |
| 235 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 236 | PyObject *result; |
| 237 | size_t nlen, plen, ipriv; |
| 238 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 240 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 241 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | Py_INCREF(ident); |
| 243 | return ident; |
| 244 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 245 | nlen = PyUnicode_GET_LENGTH(ident); |
| 246 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | The only time a name with a dot can occur is when |
| 250 | we are compiling an import statement that has a |
| 251 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | TODO(jhylton): Decide whether we want to support |
| 254 | mangling of the module name, e.g. __M.X. |
| 255 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 256 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 257 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 258 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | Py_INCREF(ident); |
| 260 | return ident; /* Don't mangle __whatever__ */ |
| 261 | } |
| 262 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 263 | ipriv = 0; |
| 264 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 265 | ipriv++; |
| 266 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | Py_INCREF(ident); |
| 268 | return ident; /* Don't mangle if class is just underscores */ |
| 269 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 270 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 272 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 273 | PyErr_SetString(PyExc_OverflowError, |
| 274 | "private identifier too large to be mangled"); |
| 275 | return NULL; |
| 276 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 278 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 279 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 280 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 281 | |
| 282 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 283 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 285 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 286 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 287 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 288 | Py_DECREF(result); |
| 289 | return NULL; |
| 290 | } |
| 291 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 292 | Py_DECREF(result); |
| 293 | return NULL; |
| 294 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 295 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 296 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 299 | static int |
| 300 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 303 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 304 | c->c_const_cache = PyDict_New(); |
| 305 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | c->c_stack = PyList_New(0); |
| 310 | if (!c->c_stack) { |
| 311 | Py_CLEAR(c->c_const_cache); |
| 312 | return 0; |
| 313 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 319 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 320 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | struct compiler c; |
| 323 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 324 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | int merged; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 326 | PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | if (!__doc__) { |
| 329 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 330 | if (!__doc__) |
| 331 | return NULL; |
| 332 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 333 | if (!__annotations__) { |
| 334 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 335 | if (!__annotations__) |
| 336 | return NULL; |
| 337 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | if (!compiler_init(&c)) |
| 339 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 340 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | c.c_filename = filename; |
| 342 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 343 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | if (c.c_future == NULL) |
| 345 | goto finally; |
| 346 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | flags = &local_flags; |
| 348 | } |
| 349 | merged = c.c_future->ff_features | flags->cf_flags; |
| 350 | c.c_future->ff_features = merged; |
| 351 | flags->cf_flags = merged; |
| 352 | c.c_flags = flags; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 353 | c.c_optimize = (optimize == -1) ? config->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | c.c_nestlevel = 0; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 355 | c.c_do_not_emit_bytecode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 357 | _PyASTOptimizeState state; |
| 358 | state.optimize = c.c_optimize; |
| 359 | state.ff_features = merged; |
| 360 | |
| 361 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 362 | goto finally; |
| 363 | } |
| 364 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 365 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (c.c_st == NULL) { |
| 367 | if (!PyErr_Occurred()) |
| 368 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 369 | goto finally; |
| 370 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 373 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 374 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | compiler_free(&c); |
| 376 | assert(co || PyErr_Occurred()); |
| 377 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 381 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 382 | int optimize, PyArena *arena) |
| 383 | { |
| 384 | PyObject *filename; |
| 385 | PyCodeObject *co; |
| 386 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 387 | if (filename == NULL) |
| 388 | return NULL; |
| 389 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 390 | Py_DECREF(filename); |
| 391 | return co; |
| 392 | |
| 393 | } |
| 394 | |
| 395 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 396 | PyNode_Compile(struct _node *n, const char *filename) |
| 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | PyCodeObject *co = NULL; |
| 399 | mod_ty mod; |
| 400 | PyArena *arena = PyArena_New(); |
| 401 | if (!arena) |
| 402 | return NULL; |
| 403 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 404 | if (mod) |
| 405 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 406 | PyArena_Free(arena); |
| 407 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 410 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 411 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | if (c->c_st) |
| 414 | PySymtable_Free(c->c_st); |
| 415 | if (c->c_future) |
| 416 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 417 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 418 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 422 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 423 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | Py_ssize_t i, n; |
| 426 | PyObject *v, *k; |
| 427 | PyObject *dict = PyDict_New(); |
| 428 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | n = PyList_Size(list); |
| 431 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 432 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | if (!v) { |
| 434 | Py_DECREF(dict); |
| 435 | return NULL; |
| 436 | } |
| 437 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 438 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | Py_DECREF(v); |
| 440 | Py_DECREF(dict); |
| 441 | return NULL; |
| 442 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | Py_DECREF(v); |
| 444 | } |
| 445 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Return new dict containing names from src that match scope(s). |
| 449 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 450 | 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] | 451 | 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] | 452 | values are integers, starting at offset and increasing by one for |
| 453 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 454 | */ |
| 455 | |
| 456 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 457 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 458 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 459 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 461 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | assert(offset >= 0); |
| 464 | if (dest == NULL) |
| 465 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 466 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 467 | /* Sort the keys so that we have a deterministic order on the indexes |
| 468 | saved in the returned dictionary. These indexes are used as indexes |
| 469 | into the free and cell var storage. Therefore if they aren't |
| 470 | deterministic, then the generated bytecode is not deterministic. |
| 471 | */ |
| 472 | sorted_keys = PyDict_Keys(src); |
| 473 | if (sorted_keys == NULL) |
| 474 | return NULL; |
| 475 | if (PyList_Sort(sorted_keys) != 0) { |
| 476 | Py_DECREF(sorted_keys); |
| 477 | return NULL; |
| 478 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 479 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 480 | |
| 481 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | /* XXX this should probably be a macro in symtable.h */ |
| 483 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 484 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 485 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | assert(PyLong_Check(v)); |
| 487 | vi = PyLong_AS_LONG(v); |
| 488 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 491 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 493 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | Py_DECREF(dest); |
| 495 | return NULL; |
| 496 | } |
| 497 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 498 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 499 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | Py_DECREF(item); |
| 501 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | return NULL; |
| 503 | } |
| 504 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 507 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 511 | static void |
| 512 | compiler_unit_check(struct compiler_unit *u) |
| 513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | basicblock *block; |
| 515 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 516 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 517 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 518 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | if (block->b_instr != NULL) { |
| 520 | assert(block->b_ialloc > 0); |
| 521 | assert(block->b_iused > 0); |
| 522 | assert(block->b_ialloc >= block->b_iused); |
| 523 | } |
| 524 | else { |
| 525 | assert (block->b_iused == 0); |
| 526 | assert (block->b_ialloc == 0); |
| 527 | } |
| 528 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static void |
| 532 | compiler_unit_free(struct compiler_unit *u) |
| 533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | compiler_unit_check(u); |
| 537 | b = u->u_blocks; |
| 538 | while (b != NULL) { |
| 539 | if (b->b_instr) |
| 540 | PyObject_Free((void *)b->b_instr); |
| 541 | next = b->b_list; |
| 542 | PyObject_Free((void *)b); |
| 543 | b = next; |
| 544 | } |
| 545 | Py_CLEAR(u->u_ste); |
| 546 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 547 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | Py_CLEAR(u->u_consts); |
| 549 | Py_CLEAR(u->u_names); |
| 550 | Py_CLEAR(u->u_varnames); |
| 551 | Py_CLEAR(u->u_freevars); |
| 552 | Py_CLEAR(u->u_cellvars); |
| 553 | Py_CLEAR(u->u_private); |
| 554 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 558 | compiler_enter_scope(struct compiler *c, identifier name, |
| 559 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 562 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 564 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 565 | struct compiler_unit)); |
| 566 | if (!u) { |
| 567 | PyErr_NoMemory(); |
| 568 | return 0; |
| 569 | } |
| 570 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 571 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 572 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 573 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | u->u_kwonlyargcount = 0; |
| 575 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 576 | if (!u->u_ste) { |
| 577 | compiler_unit_free(u); |
| 578 | return 0; |
| 579 | } |
| 580 | Py_INCREF(name); |
| 581 | u->u_name = name; |
| 582 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 583 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 584 | if (!u->u_varnames || !u->u_cellvars) { |
| 585 | compiler_unit_free(u); |
| 586 | return 0; |
| 587 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 588 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 589 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 590 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 591 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 592 | int res; |
| 593 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 594 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 595 | name = _PyUnicode_FromId(&PyId___class__); |
| 596 | if (!name) { |
| 597 | compiler_unit_free(u); |
| 598 | return 0; |
| 599 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 600 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 601 | if (res < 0) { |
| 602 | compiler_unit_free(u); |
| 603 | return 0; |
| 604 | } |
| 605 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | 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] | 608 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | if (!u->u_freevars) { |
| 610 | compiler_unit_free(u); |
| 611 | return 0; |
| 612 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | u->u_blocks = NULL; |
| 615 | u->u_nfblocks = 0; |
| 616 | u->u_firstlineno = lineno; |
| 617 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 618 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | u->u_consts = PyDict_New(); |
| 620 | if (!u->u_consts) { |
| 621 | compiler_unit_free(u); |
| 622 | return 0; |
| 623 | } |
| 624 | u->u_names = PyDict_New(); |
| 625 | if (!u->u_names) { |
| 626 | compiler_unit_free(u); |
| 627 | return 0; |
| 628 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | /* Push the old compiler_unit on the stack. */ |
| 633 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 634 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 635 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 636 | Py_XDECREF(capsule); |
| 637 | compiler_unit_free(u); |
| 638 | return 0; |
| 639 | } |
| 640 | Py_DECREF(capsule); |
| 641 | u->u_private = c->u->u_private; |
| 642 | Py_XINCREF(u->u_private); |
| 643 | } |
| 644 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 647 | |
| 648 | block = compiler_new_block(c); |
| 649 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 650 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 651 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 653 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 654 | if (!compiler_set_qualname(c)) |
| 655 | return 0; |
| 656 | } |
| 657 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 661 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 662 | compiler_exit_scope(struct compiler *c) |
| 663 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 664 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 665 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 667 | c->c_nestlevel--; |
| 668 | compiler_unit_free(c->u); |
| 669 | /* Restore c->u to the parent unit. */ |
| 670 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 671 | if (n >= 0) { |
| 672 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 673 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | assert(c->u); |
| 675 | /* we are deleting from a list so this really shouldn't fail */ |
| 676 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 677 | Py_FatalError("compiler_exit_scope()"); |
| 678 | compiler_unit_check(c->u); |
| 679 | } |
| 680 | else |
| 681 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 685 | static int |
| 686 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 687 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 688 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 689 | _Py_static_string(dot_locals, ".<locals>"); |
| 690 | Py_ssize_t stack_size; |
| 691 | struct compiler_unit *u = c->u; |
| 692 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 693 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 694 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 695 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 696 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 697 | if (stack_size > 1) { |
| 698 | int scope, force_global = 0; |
| 699 | struct compiler_unit *parent; |
| 700 | PyObject *mangled, *capsule; |
| 701 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 702 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 703 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 704 | assert(parent); |
| 705 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 706 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 707 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 708 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 709 | assert(u->u_name); |
| 710 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 711 | if (!mangled) |
| 712 | return 0; |
| 713 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 714 | Py_DECREF(mangled); |
| 715 | assert(scope != GLOBAL_IMPLICIT); |
| 716 | if (scope == GLOBAL_EXPLICIT) |
| 717 | force_global = 1; |
| 718 | } |
| 719 | |
| 720 | if (!force_global) { |
| 721 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 722 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 723 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 724 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 725 | if (dot_locals_str == NULL) |
| 726 | return 0; |
| 727 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 728 | if (base == NULL) |
| 729 | return 0; |
| 730 | } |
| 731 | else { |
| 732 | Py_INCREF(parent->u_qualname); |
| 733 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 734 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 735 | } |
| 736 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 737 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 738 | if (base != NULL) { |
| 739 | dot_str = _PyUnicode_FromId(&dot); |
| 740 | if (dot_str == NULL) { |
| 741 | Py_DECREF(base); |
| 742 | return 0; |
| 743 | } |
| 744 | name = PyUnicode_Concat(base, dot_str); |
| 745 | Py_DECREF(base); |
| 746 | if (name == NULL) |
| 747 | return 0; |
| 748 | PyUnicode_Append(&name, u->u_name); |
| 749 | if (name == NULL) |
| 750 | return 0; |
| 751 | } |
| 752 | else { |
| 753 | Py_INCREF(u->u_name); |
| 754 | name = u->u_name; |
| 755 | } |
| 756 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 757 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 758 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 761 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 762 | /* Allocate a new block and return a pointer to it. |
| 763 | Returns NULL on error. |
| 764 | */ |
| 765 | |
| 766 | static basicblock * |
| 767 | compiler_new_block(struct compiler *c) |
| 768 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | basicblock *b; |
| 770 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | u = c->u; |
| 773 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 774 | if (b == NULL) { |
| 775 | PyErr_NoMemory(); |
| 776 | return NULL; |
| 777 | } |
| 778 | memset((void *)b, 0, sizeof(basicblock)); |
| 779 | /* Extend the singly linked list of blocks with new block. */ |
| 780 | b->b_list = u->u_blocks; |
| 781 | u->u_blocks = b; |
| 782 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 785 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 786 | compiler_next_block(struct compiler *c) |
| 787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 788 | basicblock *block = compiler_new_block(c); |
| 789 | if (block == NULL) |
| 790 | return NULL; |
| 791 | c->u->u_curblock->b_next = block; |
| 792 | c->u->u_curblock = block; |
| 793 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | static basicblock * |
| 797 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | assert(block != NULL); |
| 800 | c->u->u_curblock->b_next = block; |
| 801 | c->u->u_curblock = block; |
| 802 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* Returns the offset of the next instruction in the current block's |
| 806 | b_instr array. Resizes the b_instr as necessary. |
| 807 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 808 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 809 | |
| 810 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 811 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | assert(b != NULL); |
| 814 | if (b->b_instr == NULL) { |
| 815 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 816 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 817 | if (b->b_instr == NULL) { |
| 818 | PyErr_NoMemory(); |
| 819 | return -1; |
| 820 | } |
| 821 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 822 | memset((char *)b->b_instr, 0, |
| 823 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 824 | } |
| 825 | else if (b->b_iused == b->b_ialloc) { |
| 826 | struct instr *tmp; |
| 827 | size_t oldsize, newsize; |
| 828 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 829 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 830 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 831 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | PyErr_NoMemory(); |
| 833 | return -1; |
| 834 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | if (newsize == 0) { |
| 837 | PyErr_NoMemory(); |
| 838 | return -1; |
| 839 | } |
| 840 | b->b_ialloc <<= 1; |
| 841 | tmp = (struct instr *)PyObject_Realloc( |
| 842 | (void *)b->b_instr, newsize); |
| 843 | if (tmp == NULL) { |
| 844 | PyErr_NoMemory(); |
| 845 | return -1; |
| 846 | } |
| 847 | b->b_instr = tmp; |
| 848 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 849 | } |
| 850 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 853 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 854 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 855 | The line number is reset in the following cases: |
| 856 | - when entering a new scope |
| 857 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 858 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 859 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 860 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 861 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 862 | #define SET_LOC(c, x) \ |
| 863 | (c)->u->u_lineno = (x)->lineno; \ |
| 864 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 866 | /* Return the stack effect of opcode with argument oparg. |
| 867 | |
| 868 | Some opcodes have different stack effect when jump to the target and |
| 869 | when not jump. The 'jump' parameter specifies the case: |
| 870 | |
| 871 | * 0 -- when not jump |
| 872 | * 1 -- when jump |
| 873 | * -1 -- maximal |
| 874 | */ |
| 875 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 876 | WITH_CLEANUP_FINISH deterministic. */ |
| 877 | static int |
| 878 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 881 | case NOP: |
| 882 | case EXTENDED_ARG: |
| 883 | return 0; |
| 884 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 885 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | case POP_TOP: |
| 887 | return -1; |
| 888 | case ROT_TWO: |
| 889 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 890 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | return 0; |
| 892 | case DUP_TOP: |
| 893 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 894 | case DUP_TOP_TWO: |
| 895 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 896 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 897 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | case UNARY_POSITIVE: |
| 899 | case UNARY_NEGATIVE: |
| 900 | case UNARY_NOT: |
| 901 | case UNARY_INVERT: |
| 902 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | case SET_ADD: |
| 905 | case LIST_APPEND: |
| 906 | return -1; |
| 907 | case MAP_ADD: |
| 908 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 909 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 910 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | case BINARY_POWER: |
| 912 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 913 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | case BINARY_MODULO: |
| 915 | case BINARY_ADD: |
| 916 | case BINARY_SUBTRACT: |
| 917 | case BINARY_SUBSCR: |
| 918 | case BINARY_FLOOR_DIVIDE: |
| 919 | case BINARY_TRUE_DIVIDE: |
| 920 | return -1; |
| 921 | case INPLACE_FLOOR_DIVIDE: |
| 922 | case INPLACE_TRUE_DIVIDE: |
| 923 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | case INPLACE_ADD: |
| 926 | case INPLACE_SUBTRACT: |
| 927 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 928 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | case INPLACE_MODULO: |
| 930 | return -1; |
| 931 | case STORE_SUBSCR: |
| 932 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | case DELETE_SUBSCR: |
| 934 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | case BINARY_LSHIFT: |
| 937 | case BINARY_RSHIFT: |
| 938 | case BINARY_AND: |
| 939 | case BINARY_XOR: |
| 940 | case BINARY_OR: |
| 941 | return -1; |
| 942 | case INPLACE_POWER: |
| 943 | return -1; |
| 944 | case GET_ITER: |
| 945 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | case PRINT_EXPR: |
| 948 | return -1; |
| 949 | case LOAD_BUILD_CLASS: |
| 950 | return 1; |
| 951 | case INPLACE_LSHIFT: |
| 952 | case INPLACE_RSHIFT: |
| 953 | case INPLACE_AND: |
| 954 | case INPLACE_XOR: |
| 955 | case INPLACE_OR: |
| 956 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 959 | /* 1 in the normal flow. |
| 960 | * Restore the stack position and push 6 values before jumping to |
| 961 | * the handler if an exception be raised. */ |
| 962 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | case RETURN_VALUE: |
| 964 | return -1; |
| 965 | case IMPORT_STAR: |
| 966 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 967 | case SETUP_ANNOTATIONS: |
| 968 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | case YIELD_VALUE: |
| 970 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 971 | case YIELD_FROM: |
| 972 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | case POP_BLOCK: |
| 974 | return 0; |
| 975 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 976 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 978 | case STORE_NAME: |
| 979 | return -1; |
| 980 | case DELETE_NAME: |
| 981 | return 0; |
| 982 | case UNPACK_SEQUENCE: |
| 983 | return oparg-1; |
| 984 | case UNPACK_EX: |
| 985 | return (oparg&0xFF) + (oparg>>8); |
| 986 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 987 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 988 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | case STORE_ATTR: |
| 991 | return -2; |
| 992 | case DELETE_ATTR: |
| 993 | return -1; |
| 994 | case STORE_GLOBAL: |
| 995 | return -1; |
| 996 | case DELETE_GLOBAL: |
| 997 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 998 | case LOAD_CONST: |
| 999 | return 1; |
| 1000 | case LOAD_NAME: |
| 1001 | return 1; |
| 1002 | case BUILD_TUPLE: |
| 1003 | case BUILD_LIST: |
| 1004 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1005 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1006 | return 1-oparg; |
| 1007 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1008 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1009 | case BUILD_CONST_KEY_MAP: |
| 1010 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | case LOAD_ATTR: |
| 1012 | return 0; |
| 1013 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1014 | case IS_OP: |
| 1015 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1017 | case JUMP_IF_NOT_EXC_MATCH: |
| 1018 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | case IMPORT_NAME: |
| 1020 | return -1; |
| 1021 | case IMPORT_FROM: |
| 1022 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1023 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1024 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1025 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1026 | case JUMP_ABSOLUTE: |
| 1027 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1028 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1029 | case JUMP_IF_TRUE_OR_POP: |
| 1030 | case JUMP_IF_FALSE_OR_POP: |
| 1031 | return jump ? 0 : -1; |
| 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | case POP_JUMP_IF_FALSE: |
| 1034 | case POP_JUMP_IF_TRUE: |
| 1035 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | case LOAD_GLOBAL: |
| 1038 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1039 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1040 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1042 | /* 0 in the normal flow. |
| 1043 | * Restore the stack position and push 6 values before jumping to |
| 1044 | * the handler if an exception be raised. */ |
| 1045 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1046 | case RERAISE: |
| 1047 | return -3; |
| 1048 | |
| 1049 | case WITH_EXCEPT_START: |
| 1050 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | case LOAD_FAST: |
| 1053 | return 1; |
| 1054 | case STORE_FAST: |
| 1055 | return -1; |
| 1056 | case DELETE_FAST: |
| 1057 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | case RAISE_VARARGS: |
| 1060 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1061 | |
| 1062 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1064 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1065 | case CALL_METHOD: |
| 1066 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1068 | return -oparg-1; |
| 1069 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1070 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1071 | case MAKE_FUNCTION: |
| 1072 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1073 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1074 | case BUILD_SLICE: |
| 1075 | if (oparg == 3) |
| 1076 | return -2; |
| 1077 | else |
| 1078 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1079 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1080 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | case LOAD_CLOSURE: |
| 1082 | return 1; |
| 1083 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1084 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | return 1; |
| 1086 | case STORE_DEREF: |
| 1087 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1088 | case DELETE_DEREF: |
| 1089 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1090 | |
| 1091 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1092 | case GET_AWAITABLE: |
| 1093 | return 0; |
| 1094 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1095 | /* 0 in the normal flow. |
| 1096 | * Restore the stack position to the position before the result |
| 1097 | * of __aenter__ and push 6 values before jumping to the handler |
| 1098 | * if an exception be raised. */ |
| 1099 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1100 | case BEFORE_ASYNC_WITH: |
| 1101 | return 1; |
| 1102 | case GET_AITER: |
| 1103 | return 0; |
| 1104 | case GET_ANEXT: |
| 1105 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1106 | case GET_YIELD_FROM_ITER: |
| 1107 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1108 | case END_ASYNC_FOR: |
| 1109 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1110 | case FORMAT_VALUE: |
| 1111 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1112 | else 1->1. */ |
| 1113 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1114 | case LOAD_METHOD: |
| 1115 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1116 | case LOAD_ASSERTION_ERROR: |
| 1117 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1118 | case LIST_TO_TUPLE: |
| 1119 | return 0; |
| 1120 | case LIST_EXTEND: |
| 1121 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1122 | case DICT_MERGE: |
| 1123 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1124 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1125 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1126 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1127 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1128 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1131 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1132 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1133 | { |
| 1134 | return stack_effect(opcode, oparg, jump); |
| 1135 | } |
| 1136 | |
| 1137 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1138 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1139 | { |
| 1140 | return stack_effect(opcode, oparg, -1); |
| 1141 | } |
| 1142 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1143 | /* Add an opcode with no argument. |
| 1144 | Returns 0 on failure, 1 on success. |
| 1145 | */ |
| 1146 | |
| 1147 | static int |
| 1148 | compiler_addop(struct compiler *c, int opcode) |
| 1149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1150 | basicblock *b; |
| 1151 | struct instr *i; |
| 1152 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1153 | assert(!HAS_ARG(opcode)); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1154 | if (c->c_do_not_emit_bytecode) { |
| 1155 | return 1; |
| 1156 | } |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1157 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | if (off < 0) |
| 1159 | return 0; |
| 1160 | b = c->u->u_curblock; |
| 1161 | i = &b->b_instr[off]; |
| 1162 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1163 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | if (opcode == RETURN_VALUE) |
| 1165 | b->b_return = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1166 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1170 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1171 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1172 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1173 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1174 | Py_ssize_t arg; |
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 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1178 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1180 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1181 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1182 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | return -1; |
| 1185 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1186 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1187 | Py_DECREF(v); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | Py_DECREF(v); |
| 1191 | } |
| 1192 | else |
| 1193 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1194 | return arg; |
| 1195 | } |
| 1196 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1197 | // Merge const *o* recursively and return constant key object. |
| 1198 | static PyObject* |
| 1199 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1200 | { |
| 1201 | // None and Ellipsis are singleton, and key is the singleton. |
| 1202 | // No need to merge object and key. |
| 1203 | if (o == Py_None || o == Py_Ellipsis) { |
| 1204 | Py_INCREF(o); |
| 1205 | return o; |
| 1206 | } |
| 1207 | |
| 1208 | PyObject *key = _PyCode_ConstantKey(o); |
| 1209 | if (key == NULL) { |
| 1210 | return NULL; |
| 1211 | } |
| 1212 | |
| 1213 | // t is borrowed reference |
| 1214 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1215 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1216 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1217 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1218 | Py_DECREF(key); |
| 1219 | return t; |
| 1220 | } |
| 1221 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1222 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1223 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1224 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1225 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1226 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1227 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1228 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1229 | PyObject *u = merge_consts_recursive(c, item); |
| 1230 | if (u == NULL) { |
| 1231 | Py_DECREF(key); |
| 1232 | return NULL; |
| 1233 | } |
| 1234 | |
| 1235 | // See _PyCode_ConstantKey() |
| 1236 | PyObject *v; // borrowed |
| 1237 | if (PyTuple_CheckExact(u)) { |
| 1238 | v = PyTuple_GET_ITEM(u, 1); |
| 1239 | } |
| 1240 | else { |
| 1241 | v = u; |
| 1242 | } |
| 1243 | if (v != item) { |
| 1244 | Py_INCREF(v); |
| 1245 | PyTuple_SET_ITEM(o, i, v); |
| 1246 | Py_DECREF(item); |
| 1247 | } |
| 1248 | |
| 1249 | Py_DECREF(u); |
| 1250 | } |
| 1251 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1252 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1253 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1254 | // constant keys. |
| 1255 | // See _PyCode_ConstantKey() for detail. |
| 1256 | assert(PyTuple_CheckExact(key)); |
| 1257 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1258 | |
| 1259 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1260 | if (len == 0) { // empty frozenset should not be re-created. |
| 1261 | return key; |
| 1262 | } |
| 1263 | PyObject *tuple = PyTuple_New(len); |
| 1264 | if (tuple == NULL) { |
| 1265 | Py_DECREF(key); |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | Py_ssize_t i = 0, pos = 0; |
| 1269 | PyObject *item; |
| 1270 | Py_hash_t hash; |
| 1271 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1272 | PyObject *k = merge_consts_recursive(c, item); |
| 1273 | if (k == NULL) { |
| 1274 | Py_DECREF(tuple); |
| 1275 | Py_DECREF(key); |
| 1276 | return NULL; |
| 1277 | } |
| 1278 | PyObject *u; |
| 1279 | if (PyTuple_CheckExact(k)) { |
| 1280 | u = PyTuple_GET_ITEM(k, 1); |
| 1281 | Py_INCREF(u); |
| 1282 | Py_DECREF(k); |
| 1283 | } |
| 1284 | else { |
| 1285 | u = k; |
| 1286 | } |
| 1287 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1288 | i++; |
| 1289 | } |
| 1290 | |
| 1291 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1292 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1293 | PyObject *new = PyFrozenSet_New(tuple); |
| 1294 | Py_DECREF(tuple); |
| 1295 | if (new == NULL) { |
| 1296 | Py_DECREF(key); |
| 1297 | return NULL; |
| 1298 | } |
| 1299 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1300 | Py_DECREF(o); |
| 1301 | PyTuple_SET_ITEM(key, 1, new); |
| 1302 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1303 | |
| 1304 | return key; |
| 1305 | } |
| 1306 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1307 | static Py_ssize_t |
| 1308 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1309 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1310 | if (c->c_do_not_emit_bytecode) { |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1314 | PyObject *key = merge_consts_recursive(c, o); |
| 1315 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1316 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1317 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1318 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1319 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1320 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1325 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1326 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1327 | if (c->c_do_not_emit_bytecode) { |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1331 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1332 | if (arg < 0) |
| 1333 | return 0; |
| 1334 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1335 | } |
| 1336 | |
| 1337 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1338 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1340 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1341 | if (c->c_do_not_emit_bytecode) { |
| 1342 | return 1; |
| 1343 | } |
| 1344 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1345 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1346 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1347 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1348 | return compiler_addop_i(c, opcode, arg); |
| 1349 | } |
| 1350 | |
| 1351 | static int |
| 1352 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1354 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1355 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1356 | |
| 1357 | if (c->c_do_not_emit_bytecode) { |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1361 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1362 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1363 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1364 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1365 | Py_DECREF(mangled); |
| 1366 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1367 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1368 | return compiler_addop_i(c, opcode, arg); |
| 1369 | } |
| 1370 | |
| 1371 | /* Add an opcode with an integer argument. |
| 1372 | Returns 0 on failure, 1 on success. |
| 1373 | */ |
| 1374 | |
| 1375 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1376 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1377 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | struct instr *i; |
| 1379 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1380 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1381 | if (c->c_do_not_emit_bytecode) { |
| 1382 | return 1; |
| 1383 | } |
| 1384 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1385 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1386 | it in the C code (like Python/ceval.c). |
| 1387 | |
| 1388 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1389 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1390 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1391 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1392 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1393 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1394 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1395 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | if (off < 0) |
| 1397 | return 0; |
| 1398 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1399 | i->i_opcode = opcode; |
| 1400 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1401 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | static int |
| 1406 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | struct instr *i; |
| 1409 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1411 | if (c->c_do_not_emit_bytecode) { |
| 1412 | return 1; |
| 1413 | } |
| 1414 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1415 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | assert(b != NULL); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1417 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | if (off < 0) |
| 1419 | return 0; |
| 1420 | i = &c->u->u_curblock->b_instr[off]; |
| 1421 | i->i_opcode = opcode; |
| 1422 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | if (absolute) |
| 1424 | i->i_jabs = 1; |
| 1425 | else |
| 1426 | i->i_jrel = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1427 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1431 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1432 | to the new block. |
| 1433 | |
| 1434 | The returns inside this macro make it impossible to decref objects |
| 1435 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1436 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1438 | if (compiler_next_block((C)) == NULL) \ |
| 1439 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | if (!compiler_addop((C), (OP))) \ |
| 1444 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1447 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (!compiler_addop((C), (OP))) { \ |
| 1449 | compiler_exit_scope(c); \ |
| 1450 | return 0; \ |
| 1451 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1454 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1455 | if (!compiler_addop_load_const((C), (O))) \ |
| 1456 | return 0; \ |
| 1457 | } |
| 1458 | |
| 1459 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1460 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1461 | PyObject *__new_const = (O); \ |
| 1462 | if (__new_const == NULL) { \ |
| 1463 | return 0; \ |
| 1464 | } \ |
| 1465 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1466 | Py_DECREF(__new_const); \ |
| 1467 | return 0; \ |
| 1468 | } \ |
| 1469 | Py_DECREF(__new_const); \ |
| 1470 | } |
| 1471 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1472 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1474 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1477 | /* Same as ADDOP_O, but steals a reference. */ |
| 1478 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1479 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1480 | Py_DECREF((O)); \ |
| 1481 | return 0; \ |
| 1482 | } \ |
| 1483 | Py_DECREF((O)); \ |
| 1484 | } |
| 1485 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1486 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1488 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1492 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1493 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1498 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1502 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1503 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1506 | |
| 1507 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1508 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1509 | return 0; \ |
| 1510 | } |
| 1511 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1512 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1513 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1514 | */ |
| 1515 | |
| 1516 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1517 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1518 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1521 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1522 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1523 | compiler_exit_scope(c); \ |
| 1524 | return 0; \ |
| 1525 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1528 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1529 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1530 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1534 | int _i; \ |
| 1535 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1536 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1537 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1538 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1539 | return 0; \ |
| 1540 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1543 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1544 | int _i; \ |
| 1545 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1546 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1547 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1548 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1549 | compiler_exit_scope(c); \ |
| 1550 | return 0; \ |
| 1551 | } \ |
| 1552 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1555 | /* These macros allows to check only for errors and not emmit bytecode |
| 1556 | * while visiting nodes. |
| 1557 | */ |
| 1558 | |
| 1559 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1560 | c->c_do_not_emit_bytecode++; |
| 1561 | |
| 1562 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1563 | c->c_do_not_emit_bytecode--; \ |
| 1564 | } |
| 1565 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1566 | /* Search if variable annotations are present statically in a block. */ |
| 1567 | |
| 1568 | static int |
| 1569 | find_ann(asdl_seq *stmts) |
| 1570 | { |
| 1571 | int i, j, res = 0; |
| 1572 | stmt_ty st; |
| 1573 | |
| 1574 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1575 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1576 | switch (st->kind) { |
| 1577 | case AnnAssign_kind: |
| 1578 | return 1; |
| 1579 | case For_kind: |
| 1580 | res = find_ann(st->v.For.body) || |
| 1581 | find_ann(st->v.For.orelse); |
| 1582 | break; |
| 1583 | case AsyncFor_kind: |
| 1584 | res = find_ann(st->v.AsyncFor.body) || |
| 1585 | find_ann(st->v.AsyncFor.orelse); |
| 1586 | break; |
| 1587 | case While_kind: |
| 1588 | res = find_ann(st->v.While.body) || |
| 1589 | find_ann(st->v.While.orelse); |
| 1590 | break; |
| 1591 | case If_kind: |
| 1592 | res = find_ann(st->v.If.body) || |
| 1593 | find_ann(st->v.If.orelse); |
| 1594 | break; |
| 1595 | case With_kind: |
| 1596 | res = find_ann(st->v.With.body); |
| 1597 | break; |
| 1598 | case AsyncWith_kind: |
| 1599 | res = find_ann(st->v.AsyncWith.body); |
| 1600 | break; |
| 1601 | case Try_kind: |
| 1602 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1603 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1604 | st->v.Try.handlers, j); |
| 1605 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1606 | return 1; |
| 1607 | } |
| 1608 | } |
| 1609 | res = find_ann(st->v.Try.body) || |
| 1610 | find_ann(st->v.Try.finalbody) || |
| 1611 | find_ann(st->v.Try.orelse); |
| 1612 | break; |
| 1613 | default: |
| 1614 | res = 0; |
| 1615 | } |
| 1616 | if (res) { |
| 1617 | break; |
| 1618 | } |
| 1619 | } |
| 1620 | return res; |
| 1621 | } |
| 1622 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1623 | /* |
| 1624 | * Frame block handling functions |
| 1625 | */ |
| 1626 | |
| 1627 | static int |
| 1628 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1629 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1630 | { |
| 1631 | struct fblockinfo *f; |
| 1632 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1633 | PyErr_SetString(PyExc_SyntaxError, |
| 1634 | "too many statically nested blocks"); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1638 | f->fb_type = t; |
| 1639 | f->fb_block = b; |
| 1640 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1641 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1642 | return 1; |
| 1643 | } |
| 1644 | |
| 1645 | static void |
| 1646 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1647 | { |
| 1648 | struct compiler_unit *u = c->u; |
| 1649 | assert(u->u_nfblocks > 0); |
| 1650 | u->u_nfblocks--; |
| 1651 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1652 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1653 | } |
| 1654 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1655 | static int |
| 1656 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1657 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1658 | ADDOP(c, DUP_TOP); |
| 1659 | ADDOP(c, DUP_TOP); |
| 1660 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1661 | return 1; |
| 1662 | } |
| 1663 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1664 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1665 | * popping the blocks will be restored afterwards, unless another |
| 1666 | * return, break or continue is found. In which case, the TOS will |
| 1667 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1668 | */ |
| 1669 | static int |
| 1670 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1671 | int preserve_tos) |
| 1672 | { |
| 1673 | switch (info->fb_type) { |
| 1674 | case WHILE_LOOP: |
| 1675 | return 1; |
| 1676 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1677 | case FOR_LOOP: |
| 1678 | /* Pop the iterator */ |
| 1679 | if (preserve_tos) { |
| 1680 | ADDOP(c, ROT_TWO); |
| 1681 | } |
| 1682 | ADDOP(c, POP_TOP); |
| 1683 | return 1; |
| 1684 | |
| 1685 | case EXCEPT: |
| 1686 | ADDOP(c, POP_BLOCK); |
| 1687 | return 1; |
| 1688 | |
| 1689 | case FINALLY_TRY: |
| 1690 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1691 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1692 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1693 | return 0; |
| 1694 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1695 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1696 | /* Emit the finally block, restoring the line number when done */ |
| 1697 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1698 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1699 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1700 | if (preserve_tos) { |
| 1701 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1702 | } |
| 1703 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1704 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1705 | case FINALLY_END: |
| 1706 | if (preserve_tos) { |
| 1707 | ADDOP(c, ROT_FOUR); |
| 1708 | } |
| 1709 | ADDOP(c, POP_TOP); |
| 1710 | ADDOP(c, POP_TOP); |
| 1711 | ADDOP(c, POP_TOP); |
| 1712 | if (preserve_tos) { |
| 1713 | ADDOP(c, ROT_FOUR); |
| 1714 | } |
| 1715 | ADDOP(c, POP_EXCEPT); |
| 1716 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1717 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1718 | case WITH: |
| 1719 | case ASYNC_WITH: |
| 1720 | ADDOP(c, POP_BLOCK); |
| 1721 | if (preserve_tos) { |
| 1722 | ADDOP(c, ROT_TWO); |
| 1723 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1724 | if(!compiler_call_exit_with_nones(c)) { |
| 1725 | return 0; |
| 1726 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1727 | if (info->fb_type == ASYNC_WITH) { |
| 1728 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1729 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1730 | ADDOP(c, YIELD_FROM); |
| 1731 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1732 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1733 | return 1; |
| 1734 | |
| 1735 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1736 | if (info->fb_datum) { |
| 1737 | ADDOP(c, POP_BLOCK); |
| 1738 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1739 | if (preserve_tos) { |
| 1740 | ADDOP(c, ROT_FOUR); |
| 1741 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1742 | ADDOP(c, POP_EXCEPT); |
| 1743 | if (info->fb_datum) { |
| 1744 | ADDOP_LOAD_CONST(c, Py_None); |
| 1745 | compiler_nameop(c, info->fb_datum, Store); |
| 1746 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1747 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1748 | return 1; |
| 1749 | |
| 1750 | case POP_VALUE: |
| 1751 | if (preserve_tos) { |
| 1752 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1753 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1754 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1755 | return 1; |
| 1756 | } |
| 1757 | Py_UNREACHABLE(); |
| 1758 | } |
| 1759 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1760 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1761 | static int |
| 1762 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1763 | if (c->u->u_nfblocks == 0) { |
| 1764 | return 1; |
| 1765 | } |
| 1766 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1767 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1768 | *loop = top; |
| 1769 | return 1; |
| 1770 | } |
| 1771 | struct fblockinfo copy = *top; |
| 1772 | c->u->u_nfblocks--; |
| 1773 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1774 | return 0; |
| 1775 | } |
| 1776 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1777 | return 0; |
| 1778 | } |
| 1779 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1780 | c->u->u_nfblocks++; |
| 1781 | return 1; |
| 1782 | } |
| 1783 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1784 | /* Compile a sequence of statements, checking for a docstring |
| 1785 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1786 | |
| 1787 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1788 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1789 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1790 | int i = 0; |
| 1791 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1792 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1793 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1794 | /* Set current line number to the line number of first statement. |
| 1795 | This way line number for SETUP_ANNOTATIONS will always |
| 1796 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1797 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1798 | 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] | 1799 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1800 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1801 | } |
| 1802 | /* Every annotated class and module should have __annotations__. */ |
| 1803 | if (find_ann(stmts)) { |
| 1804 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1805 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1806 | if (!asdl_seq_LEN(stmts)) |
| 1807 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1808 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1809 | if (c->c_optimize < 2) { |
| 1810 | docstring = _PyAST_GetDocString(stmts); |
| 1811 | if (docstring) { |
| 1812 | i = 1; |
| 1813 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1814 | assert(st->kind == Expr_kind); |
| 1815 | VISIT(c, expr, st->v.Expr.value); |
| 1816 | if (!compiler_nameop(c, __doc__, Store)) |
| 1817 | return 0; |
| 1818 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1819 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1820 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1821 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | static PyCodeObject * |
| 1826 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1827 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | PyCodeObject *co; |
| 1829 | int addNone = 1; |
| 1830 | static PyObject *module; |
| 1831 | if (!module) { |
| 1832 | module = PyUnicode_InternFromString("<module>"); |
| 1833 | if (!module) |
| 1834 | return NULL; |
| 1835 | } |
| 1836 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1837 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | return NULL; |
| 1839 | switch (mod->kind) { |
| 1840 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1841 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | compiler_exit_scope(c); |
| 1843 | return 0; |
| 1844 | } |
| 1845 | break; |
| 1846 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1847 | if (find_ann(mod->v.Interactive.body)) { |
| 1848 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1849 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | c->c_interactive = 1; |
| 1851 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1852 | mod->v.Interactive.body); |
| 1853 | break; |
| 1854 | case Expression_kind: |
| 1855 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1856 | addNone = 0; |
| 1857 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | default: |
| 1859 | PyErr_Format(PyExc_SystemError, |
| 1860 | "module kind %d should not be possible", |
| 1861 | mod->kind); |
| 1862 | return 0; |
| 1863 | } |
| 1864 | co = assemble(c, addNone); |
| 1865 | compiler_exit_scope(c); |
| 1866 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1869 | /* The test for LOCAL must come before the test for FREE in order to |
| 1870 | handle classes where name is both local and free. The local var is |
| 1871 | 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] | 1872 | */ |
| 1873 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1874 | static int |
| 1875 | get_ref_type(struct compiler *c, PyObject *name) |
| 1876 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1877 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1878 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1879 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1880 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1881 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | if (scope == 0) { |
| 1883 | char buf[350]; |
| 1884 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1885 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1887 | PyUnicode_AsUTF8(name), |
| 1888 | PyUnicode_AsUTF8(c->u->u_name), |
| 1889 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1890 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1891 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1892 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | ); |
| 1894 | Py_FatalError(buf); |
| 1895 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1896 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1897 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | static int |
| 1901 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1902 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1903 | PyObject *v; |
| 1904 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1905 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1906 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1907 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1911 | 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] | 1912 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1913 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1914 | if (qualname == NULL) |
| 1915 | qualname = co->co_name; |
| 1916 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1917 | if (free) { |
| 1918 | for (i = 0; i < free; ++i) { |
| 1919 | /* Bypass com_addop_varname because it will generate |
| 1920 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1921 | */ |
| 1922 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1923 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1924 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1925 | /* Special case: If a class contains a method with a |
| 1926 | free variable that has the same name as a method, |
| 1927 | the name will be considered free *and* local in the |
| 1928 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1929 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1930 | */ |
| 1931 | reftype = get_ref_type(c, name); |
| 1932 | if (reftype == CELL) |
| 1933 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1934 | else /* (reftype == FREE) */ |
| 1935 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1936 | if (arg == -1) { |
| 1937 | fprintf(stderr, |
| 1938 | "lookup %s in %s %d %d\n" |
| 1939 | "freevars of %s: %s\n", |
| 1940 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1941 | PyUnicode_AsUTF8(c->u->u_name), |
| 1942 | reftype, arg, |
| 1943 | PyUnicode_AsUTF8(co->co_name), |
| 1944 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1945 | Py_FatalError("compiler_make_closure()"); |
| 1946 | } |
| 1947 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1948 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1949 | flags |= 0x08; |
| 1950 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1952 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1953 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1954 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | static int |
| 1959 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1960 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | if (!decos) |
| 1964 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1967 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1968 | } |
| 1969 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1973 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1975 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1976 | /* Push a dict of keyword-only default values. |
| 1977 | |
| 1978 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1979 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1980 | int i; |
| 1981 | PyObject *keys = NULL; |
| 1982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1983 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1984 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1985 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1986 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1987 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1988 | if (!mangled) { |
| 1989 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1991 | if (keys == NULL) { |
| 1992 | keys = PyList_New(1); |
| 1993 | if (keys == NULL) { |
| 1994 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1995 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1996 | } |
| 1997 | PyList_SET_ITEM(keys, 0, mangled); |
| 1998 | } |
| 1999 | else { |
| 2000 | int res = PyList_Append(keys, mangled); |
| 2001 | Py_DECREF(mangled); |
| 2002 | if (res == -1) { |
| 2003 | goto error; |
| 2004 | } |
| 2005 | } |
| 2006 | if (!compiler_visit_expr(c, default_)) { |
| 2007 | goto error; |
| 2008 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | } |
| 2010 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2011 | if (keys != NULL) { |
| 2012 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2013 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2014 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2015 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2016 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2017 | assert(default_count > 0); |
| 2018 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2019 | } |
| 2020 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2021 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | error: |
| 2025 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2026 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2030 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2031 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2032 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2037 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 2038 | expr_ty annotation, PyObject *names) |
| 2039 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2041 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2042 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2043 | VISIT(c, annexpr, annotation) |
| 2044 | } |
| 2045 | else { |
| 2046 | VISIT(c, expr, annotation); |
| 2047 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2048 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2049 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2050 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2051 | if (PyList_Append(names, mangled) < 0) { |
| 2052 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2053 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2054 | } |
| 2055 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2057 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | static int |
| 2061 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 2062 | PyObject *names) |
| 2063 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2064 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2066 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2067 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | c, |
| 2069 | arg->arg, |
| 2070 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2071 | names)) |
| 2072 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2073 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2074 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | static int |
| 2078 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2079 | expr_ty returns) |
| 2080 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2081 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2082 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2083 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2084 | 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] | 2085 | */ |
| 2086 | static identifier return_str; |
| 2087 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2088 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2089 | names = PyList_New(0); |
| 2090 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2091 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2092 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2093 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2094 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 2095 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 2096 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2097 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2098 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2099 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2100 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2101 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2103 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2104 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2105 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2106 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | if (!return_str) { |
| 2109 | return_str = PyUnicode_InternFromString("return"); |
| 2110 | if (!return_str) |
| 2111 | goto error; |
| 2112 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2113 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | goto error; |
| 2115 | } |
| 2116 | |
| 2117 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2118 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2119 | PyObject *keytuple = PyList_AsTuple(names); |
| 2120 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2121 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2122 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2123 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2124 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2125 | else { |
| 2126 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2127 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2128 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2129 | |
| 2130 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2132 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2136 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2137 | { |
| 2138 | VISIT_SEQ(c, expr, args->defaults); |
| 2139 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2140 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2141 | } |
| 2142 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2143 | static Py_ssize_t |
| 2144 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2145 | { |
| 2146 | Py_ssize_t funcflags = 0; |
| 2147 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2148 | if (!compiler_visit_defaults(c, args)) |
| 2149 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2150 | funcflags |= 0x01; |
| 2151 | } |
| 2152 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2153 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2154 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2155 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2156 | return -1; |
| 2157 | } |
| 2158 | else if (res > 0) { |
| 2159 | funcflags |= 0x02; |
| 2160 | } |
| 2161 | } |
| 2162 | return funcflags; |
| 2163 | } |
| 2164 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2165 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2166 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2167 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2168 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2169 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2170 | arguments_ty args; |
| 2171 | expr_ty returns; |
| 2172 | identifier name; |
| 2173 | asdl_seq* decos; |
| 2174 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2175 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2176 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2177 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2178 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2179 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2180 | if (is_async) { |
| 2181 | assert(s->kind == AsyncFunctionDef_kind); |
| 2182 | |
| 2183 | args = s->v.AsyncFunctionDef.args; |
| 2184 | returns = s->v.AsyncFunctionDef.returns; |
| 2185 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2186 | name = s->v.AsyncFunctionDef.name; |
| 2187 | body = s->v.AsyncFunctionDef.body; |
| 2188 | |
| 2189 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2190 | } else { |
| 2191 | assert(s->kind == FunctionDef_kind); |
| 2192 | |
| 2193 | args = s->v.FunctionDef.args; |
| 2194 | returns = s->v.FunctionDef.returns; |
| 2195 | decos = s->v.FunctionDef.decorator_list; |
| 2196 | name = s->v.FunctionDef.name; |
| 2197 | body = s->v.FunctionDef.body; |
| 2198 | |
| 2199 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2200 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2202 | if (!compiler_decorators(c, decos)) |
| 2203 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2204 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2205 | firstlineno = s->lineno; |
| 2206 | if (asdl_seq_LEN(decos)) { |
| 2207 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2208 | } |
| 2209 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2210 | funcflags = compiler_default_arguments(c, args); |
| 2211 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2213 | } |
| 2214 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2215 | annotations = compiler_visit_annotations(c, args, returns); |
| 2216 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2217 | return 0; |
| 2218 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2219 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2220 | funcflags |= 0x04; |
| 2221 | } |
| 2222 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2223 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2224 | return 0; |
| 2225 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2226 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2227 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2228 | if (c->c_optimize < 2) { |
| 2229 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2230 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2231 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | compiler_exit_scope(c); |
| 2233 | return 0; |
| 2234 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2235 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2237 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2239 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2240 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2241 | qualname = c->u->u_qualname; |
| 2242 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2244 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2245 | Py_XDECREF(qualname); |
| 2246 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2247 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2248 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2249 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2250 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2251 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | /* decorators */ |
| 2255 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2256 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2257 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2258 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2259 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | static int |
| 2263 | compiler_class(struct compiler *c, stmt_ty s) |
| 2264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2265 | PyCodeObject *co; |
| 2266 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2267 | int i, firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2268 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2269 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | if (!compiler_decorators(c, decos)) |
| 2271 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2272 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2273 | firstlineno = s->lineno; |
| 2274 | if (asdl_seq_LEN(decos)) { |
| 2275 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2276 | } |
| 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | /* ultimately generate code for: |
| 2279 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2280 | where: |
| 2281 | <func> is a function/closure created from the class body; |
| 2282 | it has a single argument (__locals__) where the dict |
| 2283 | (or MutableSequence) representing the locals is passed |
| 2284 | <name> is the class name |
| 2285 | <bases> is the positional arguments and *varargs argument |
| 2286 | <keywords> is the keyword arguments and **kwds argument |
| 2287 | This borrows from compiler_call. |
| 2288 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2290 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2291 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2292 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2293 | return 0; |
| 2294 | /* this block represents what we do in the new scope */ |
| 2295 | { |
| 2296 | /* use the class name for name mangling */ |
| 2297 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2298 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | /* load (global) __name__ ... */ |
| 2300 | str = PyUnicode_InternFromString("__name__"); |
| 2301 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2302 | Py_XDECREF(str); |
| 2303 | compiler_exit_scope(c); |
| 2304 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2305 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2306 | Py_DECREF(str); |
| 2307 | /* ... and store it as __module__ */ |
| 2308 | str = PyUnicode_InternFromString("__module__"); |
| 2309 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2310 | Py_XDECREF(str); |
| 2311 | compiler_exit_scope(c); |
| 2312 | return 0; |
| 2313 | } |
| 2314 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2315 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2316 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2317 | str = PyUnicode_InternFromString("__qualname__"); |
| 2318 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2319 | Py_XDECREF(str); |
| 2320 | compiler_exit_scope(c); |
| 2321 | return 0; |
| 2322 | } |
| 2323 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2325 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | compiler_exit_scope(c); |
| 2327 | return 0; |
| 2328 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2329 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2330 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2331 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2332 | str = PyUnicode_InternFromString("__class__"); |
| 2333 | if (str == NULL) { |
| 2334 | compiler_exit_scope(c); |
| 2335 | return 0; |
| 2336 | } |
| 2337 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2338 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2339 | if (i < 0) { |
| 2340 | compiler_exit_scope(c); |
| 2341 | return 0; |
| 2342 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2343 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2345 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2346 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2347 | str = PyUnicode_InternFromString("__classcell__"); |
| 2348 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2349 | Py_XDECREF(str); |
| 2350 | compiler_exit_scope(c); |
| 2351 | return 0; |
| 2352 | } |
| 2353 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2355 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2356 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2357 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2358 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2359 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2360 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | /* create the code object */ |
| 2362 | co = assemble(c, 1); |
| 2363 | } |
| 2364 | /* leave the new scope */ |
| 2365 | compiler_exit_scope(c); |
| 2366 | if (co == NULL) |
| 2367 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | /* 2. load the 'build_class' function */ |
| 2370 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2371 | |
| 2372 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2373 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | Py_DECREF(co); |
| 2375 | |
| 2376 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2377 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | |
| 2379 | /* 5. generate the rest of the code for the call */ |
| 2380 | if (!compiler_call_helper(c, 2, |
| 2381 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2382 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2383 | return 0; |
| 2384 | |
| 2385 | /* 6. apply decorators */ |
| 2386 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2387 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2388 | } |
| 2389 | |
| 2390 | /* 7. store into <name> */ |
| 2391 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2392 | return 0; |
| 2393 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2396 | /* Return 0 if the expression is a constant value except named singletons. |
| 2397 | Return 1 otherwise. */ |
| 2398 | static int |
| 2399 | check_is_arg(expr_ty e) |
| 2400 | { |
| 2401 | if (e->kind != Constant_kind) { |
| 2402 | return 1; |
| 2403 | } |
| 2404 | PyObject *value = e->v.Constant.value; |
| 2405 | return (value == Py_None |
| 2406 | || value == Py_False |
| 2407 | || value == Py_True |
| 2408 | || value == Py_Ellipsis); |
| 2409 | } |
| 2410 | |
| 2411 | /* Check operands of identity chacks ("is" and "is not"). |
| 2412 | Emit a warning if any operand is a constant except named singletons. |
| 2413 | Return 0 on error. |
| 2414 | */ |
| 2415 | static int |
| 2416 | check_compare(struct compiler *c, expr_ty e) |
| 2417 | { |
| 2418 | Py_ssize_t i, n; |
| 2419 | int left = check_is_arg(e->v.Compare.left); |
| 2420 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2421 | for (i = 0; i < n; i++) { |
| 2422 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2423 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2424 | if (op == Is || op == IsNot) { |
| 2425 | if (!right || !left) { |
| 2426 | const char *msg = (op == Is) |
| 2427 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2428 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2429 | return compiler_warn(c, msg); |
| 2430 | } |
| 2431 | } |
| 2432 | left = right; |
| 2433 | } |
| 2434 | return 1; |
| 2435 | } |
| 2436 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2437 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2438 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2439 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2440 | switch (op) { |
| 2441 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2442 | cmp = Py_EQ; |
| 2443 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2444 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2445 | cmp = Py_NE; |
| 2446 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2447 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2448 | cmp = Py_LT; |
| 2449 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2450 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2451 | cmp = Py_LE; |
| 2452 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2453 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2454 | cmp = Py_GT; |
| 2455 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2456 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2457 | cmp = Py_GE; |
| 2458 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2459 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2460 | ADDOP_I(c, IS_OP, 0); |
| 2461 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2462 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2463 | ADDOP_I(c, IS_OP, 1); |
| 2464 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2465 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2466 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2467 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2468 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2469 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2470 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2471 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2472 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2473 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2474 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2475 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2476 | } |
| 2477 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2478 | |
| 2479 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2480 | static int |
| 2481 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2482 | { |
| 2483 | switch (e->kind) { |
| 2484 | case UnaryOp_kind: |
| 2485 | if (e->v.UnaryOp.op == Not) |
| 2486 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2487 | /* fallback to general implementation */ |
| 2488 | break; |
| 2489 | case BoolOp_kind: { |
| 2490 | asdl_seq *s = e->v.BoolOp.values; |
| 2491 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2492 | assert(n >= 0); |
| 2493 | int cond2 = e->v.BoolOp.op == Or; |
| 2494 | basicblock *next2 = next; |
| 2495 | if (!cond2 != !cond) { |
| 2496 | next2 = compiler_new_block(c); |
| 2497 | if (next2 == NULL) |
| 2498 | return 0; |
| 2499 | } |
| 2500 | for (i = 0; i < n; ++i) { |
| 2501 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2502 | return 0; |
| 2503 | } |
| 2504 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2505 | return 0; |
| 2506 | if (next2 != next) |
| 2507 | compiler_use_next_block(c, next2); |
| 2508 | return 1; |
| 2509 | } |
| 2510 | case IfExp_kind: { |
| 2511 | basicblock *end, *next2; |
| 2512 | end = compiler_new_block(c); |
| 2513 | if (end == NULL) |
| 2514 | return 0; |
| 2515 | next2 = compiler_new_block(c); |
| 2516 | if (next2 == NULL) |
| 2517 | return 0; |
| 2518 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2519 | return 0; |
| 2520 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2521 | return 0; |
| 2522 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2523 | compiler_use_next_block(c, next2); |
| 2524 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2525 | return 0; |
| 2526 | compiler_use_next_block(c, end); |
| 2527 | return 1; |
| 2528 | } |
| 2529 | case Compare_kind: { |
| 2530 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2531 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2532 | if (!check_compare(c, e)) { |
| 2533 | return 0; |
| 2534 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2535 | basicblock *cleanup = compiler_new_block(c); |
| 2536 | if (cleanup == NULL) |
| 2537 | return 0; |
| 2538 | VISIT(c, expr, e->v.Compare.left); |
| 2539 | for (i = 0; i < n; i++) { |
| 2540 | VISIT(c, expr, |
| 2541 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2542 | ADDOP(c, DUP_TOP); |
| 2543 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2544 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2545 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2546 | NEXT_BLOCK(c); |
| 2547 | } |
| 2548 | 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] | 2549 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2550 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2551 | basicblock *end = compiler_new_block(c); |
| 2552 | if (end == NULL) |
| 2553 | return 0; |
| 2554 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2555 | compiler_use_next_block(c, cleanup); |
| 2556 | ADDOP(c, POP_TOP); |
| 2557 | if (!cond) { |
| 2558 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2559 | } |
| 2560 | compiler_use_next_block(c, end); |
| 2561 | return 1; |
| 2562 | } |
| 2563 | /* fallback to general implementation */ |
| 2564 | break; |
| 2565 | } |
| 2566 | default: |
| 2567 | /* fallback to general implementation */ |
| 2568 | break; |
| 2569 | } |
| 2570 | |
| 2571 | /* general implementation */ |
| 2572 | VISIT(c, expr, e); |
| 2573 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2574 | return 1; |
| 2575 | } |
| 2576 | |
| 2577 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2578 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | basicblock *end, *next; |
| 2581 | |
| 2582 | assert(e->kind == IfExp_kind); |
| 2583 | end = compiler_new_block(c); |
| 2584 | if (end == NULL) |
| 2585 | return 0; |
| 2586 | next = compiler_new_block(c); |
| 2587 | if (next == NULL) |
| 2588 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2589 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2590 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | VISIT(c, expr, e->v.IfExp.body); |
| 2592 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2593 | compiler_use_next_block(c, next); |
| 2594 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2595 | compiler_use_next_block(c, end); |
| 2596 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2600 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2603 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2605 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2606 | arguments_ty args = e->v.Lambda.args; |
| 2607 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2609 | if (!name) { |
| 2610 | name = PyUnicode_InternFromString("<lambda>"); |
| 2611 | if (!name) |
| 2612 | return 0; |
| 2613 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2614 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2615 | funcflags = compiler_default_arguments(c, args); |
| 2616 | if (funcflags == -1) { |
| 2617 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2619 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2620 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2621 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2624 | /* Make None the first constant, so the lambda can't have a |
| 2625 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2626 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2630 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2632 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2633 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2634 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2635 | } |
| 2636 | else { |
| 2637 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2638 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2639 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2640 | qualname = c->u->u_qualname; |
| 2641 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2643 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2645 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2646 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2647 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | Py_DECREF(co); |
| 2649 | |
| 2650 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | compiler_if(struct compiler *c, stmt_ty s) |
| 2655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2656 | basicblock *end, *next; |
| 2657 | int constant; |
| 2658 | assert(s->kind == If_kind); |
| 2659 | end = compiler_new_block(c); |
| 2660 | if (end == NULL) |
| 2661 | return 0; |
| 2662 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2663 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2664 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | * constant = 1: "if 1", "if 2", ... |
| 2666 | * constant = -1: rest */ |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2667 | if (constant == 0) { |
| 2668 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | VISIT_SEQ(c, stmt, s->v.If.body); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2670 | END_DO_NOT_EMIT_BYTECODE |
| 2671 | if (s->v.If.orelse) { |
| 2672 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2673 | } |
| 2674 | } else if (constant == 1) { |
| 2675 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2676 | if (s->v.If.orelse) { |
| 2677 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2678 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2679 | END_DO_NOT_EMIT_BYTECODE |
| 2680 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2682 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2683 | next = compiler_new_block(c); |
| 2684 | if (next == NULL) |
| 2685 | return 0; |
| 2686 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2687 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | next = end; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2689 | } |
| 2690 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2691 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2692 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2694 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2695 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | compiler_use_next_block(c, next); |
| 2697 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2698 | } |
| 2699 | } |
| 2700 | compiler_use_next_block(c, end); |
| 2701 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | static int |
| 2705 | compiler_for(struct compiler *c, stmt_ty s) |
| 2706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2707 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | start = compiler_new_block(c); |
| 2710 | cleanup = compiler_new_block(c); |
| 2711 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2712 | if (start == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2713 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2714 | } |
| 2715 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2717 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | VISIT(c, expr, s->v.For.iter); |
| 2719 | ADDOP(c, GET_ITER); |
| 2720 | compiler_use_next_block(c, start); |
| 2721 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2722 | VISIT(c, expr, s->v.For.target); |
| 2723 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2724 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2725 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2726 | |
| 2727 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2728 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2730 | compiler_use_next_block(c, end); |
| 2731 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2732 | } |
| 2733 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2734 | |
| 2735 | static int |
| 2736 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2737 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2738 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2739 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2740 | c->u->u_ste->ste_coroutine = 1; |
| 2741 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2742 | return compiler_error(c, "'async for' outside async function"); |
| 2743 | } |
| 2744 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2745 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2746 | except = compiler_new_block(c); |
| 2747 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2748 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2749 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2750 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2751 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2752 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2753 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2754 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2755 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2756 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2757 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2758 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2759 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2760 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2761 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2762 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2763 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2764 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2765 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2766 | /* Success block for __anext__ */ |
| 2767 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2768 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2769 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2770 | |
| 2771 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2772 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2773 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2774 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2775 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2776 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2777 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2778 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2779 | |
| 2780 | compiler_use_next_block(c, end); |
| 2781 | |
| 2782 | return 1; |
| 2783 | } |
| 2784 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2785 | static int |
| 2786 | compiler_while(struct compiler *c, stmt_ty s) |
| 2787 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2788 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2789 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2790 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | if (constant == 0) { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2792 | BEGIN_DO_NOT_EMIT_BYTECODE |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2793 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2794 | // inside a while loop so it can correctly evaluate syntax |
| 2795 | // errors. |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2796 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) { |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2797 | return 0; |
| 2798 | } |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2799 | VISIT_SEQ(c, stmt, s->v.While.body); |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2800 | // Remove the dummy block now that is not needed. |
| 2801 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2802 | END_DO_NOT_EMIT_BYTECODE |
| 2803 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2805 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | return 1; |
| 2807 | } |
| 2808 | loop = compiler_new_block(c); |
| 2809 | end = compiler_new_block(c); |
| 2810 | if (constant == -1) { |
| 2811 | anchor = compiler_new_block(c); |
| 2812 | if (anchor == NULL) |
| 2813 | return 0; |
| 2814 | } |
| 2815 | if (loop == NULL || end == NULL) |
| 2816 | return 0; |
| 2817 | if (s->v.While.orelse) { |
| 2818 | orelse = compiler_new_block(c); |
| 2819 | if (orelse == NULL) |
| 2820 | return 0; |
| 2821 | } |
| 2822 | else |
| 2823 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | compiler_use_next_block(c, loop); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2826 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | return 0; |
| 2828 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2829 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2830 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | } |
| 2832 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2833 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2834 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | /* XXX should the two POP instructions be in a separate block |
| 2836 | if there is no else clause ? |
| 2837 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2838 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2839 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2840 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2841 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2844 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2845 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2848 | } |
| 2849 | |
| 2850 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2851 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2852 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2853 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2854 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2855 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2856 | return compiler_error(c, "'return' outside function"); |
| 2857 | if (s->v.Return.value != NULL && |
| 2858 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2859 | { |
| 2860 | return compiler_error( |
| 2861 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2863 | if (preserve_tos) { |
| 2864 | VISIT(c, expr, s->v.Return.value); |
| 2865 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2866 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2867 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2868 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2869 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2870 | } |
| 2871 | else if (!preserve_tos) { |
| 2872 | VISIT(c, expr, s->v.Return.value); |
| 2873 | } |
| 2874 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2879 | static int |
| 2880 | compiler_break(struct compiler *c) |
| 2881 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2882 | struct fblockinfo *loop = NULL; |
| 2883 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2884 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2885 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2886 | if (loop == NULL) { |
| 2887 | return compiler_error(c, "'break' outside loop"); |
| 2888 | } |
| 2889 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2890 | return 0; |
| 2891 | } |
| 2892 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_exit); |
| 2893 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | static int |
| 2897 | compiler_continue(struct compiler *c) |
| 2898 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2899 | struct fblockinfo *loop = NULL; |
| 2900 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2901 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2902 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2903 | if (loop == NULL) { |
| 2904 | return compiler_error(c, "'continue' not properly in loop"); |
| 2905 | } |
| 2906 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_block); |
| 2907 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2911 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2912 | |
| 2913 | SETUP_FINALLY L |
| 2914 | <code for body> |
| 2915 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2916 | <code for finalbody> |
| 2917 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2918 | L: |
| 2919 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2920 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | The special instructions use the block stack. Each block |
| 2923 | stack entry contains the instruction that created it (here |
| 2924 | SETUP_FINALLY), the level of the value stack at the time the |
| 2925 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2927 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | Pushes the current value stack level and the label |
| 2929 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2930 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2931 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2933 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2934 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2935 | exceptions are pushed onto the value stack (and the exception |
| 2936 | condition is cleared), and the interpreter jumps to the label |
| 2937 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2938 | */ |
| 2939 | |
| 2940 | static int |
| 2941 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2942 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2943 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | body = compiler_new_block(c); |
| 2946 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2947 | exit = compiler_new_block(c); |
| 2948 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2950 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2951 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2953 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2954 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2955 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2956 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2957 | if (!compiler_try_except(c, s)) |
| 2958 | return 0; |
| 2959 | } |
| 2960 | else { |
| 2961 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2962 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2964 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 2965 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2966 | ADDOP_JREL(c, JUMP_FORWARD, exit); |
| 2967 | /* `finally` block */ |
| 2968 | compiler_use_next_block(c, end); |
| 2969 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 2970 | return 0; |
| 2971 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2972 | compiler_pop_fblock(c, FINALLY_END, end); |
| 2973 | ADDOP(c, RERAISE); |
| 2974 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2979 | 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] | 2980 | (The contents of the value stack is shown in [], with the top |
| 2981 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2982 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | |
| 2984 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2985 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2986 | [] <code for S> |
| 2987 | [] POP_BLOCK |
| 2988 | [] JUMP_FORWARD L0 |
| 2989 | |
| 2990 | [tb, val, exc] L1: DUP ) |
| 2991 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2992 | [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] | 2993 | [tb, val, exc] POP |
| 2994 | [tb, val] <assign to V1> (or POP if no V1) |
| 2995 | [tb] POP |
| 2996 | [] <code for S1> |
| 2997 | JUMP_FORWARD L0 |
| 2998 | |
| 2999 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 | .............................etc....................... |
| 3001 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3002 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3003 | |
| 3004 | [] L0: <next statement> |
| 3005 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3006 | Of course, parts are not generated if Vi or Ei is not present. |
| 3007 | */ |
| 3008 | static int |
| 3009 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3010 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3012 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3013 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3014 | body = compiler_new_block(c); |
| 3015 | except = compiler_new_block(c); |
| 3016 | orelse = compiler_new_block(c); |
| 3017 | end = compiler_new_block(c); |
| 3018 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3019 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3020 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3022 | if (!compiler_push_fblock(c, EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3024 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | ADDOP(c, POP_BLOCK); |
| 3026 | compiler_pop_fblock(c, EXCEPT, body); |
| 3027 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3028 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | compiler_use_next_block(c, except); |
| 3030 | for (i = 0; i < n; i++) { |
| 3031 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3032 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3033 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3034 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3035 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3036 | except = compiler_new_block(c); |
| 3037 | if (except == NULL) |
| 3038 | return 0; |
| 3039 | if (handler->v.ExceptHandler.type) { |
| 3040 | ADDOP(c, DUP_TOP); |
| 3041 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3042 | ADDOP_JABS(c, JUMP_IF_NOT_EXC_MATCH, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | } |
| 3044 | ADDOP(c, POP_TOP); |
| 3045 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3046 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3047 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3048 | cleanup_end = compiler_new_block(c); |
| 3049 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3050 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3051 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3052 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3053 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3054 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3055 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3057 | /* |
| 3058 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3059 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3060 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3061 | try: |
| 3062 | # body |
| 3063 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3064 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3065 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3066 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3068 | /* second try: */ |
| 3069 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 3070 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3071 | 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] | 3072 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3074 | /* second # body */ |
| 3075 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3076 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3077 | ADDOP(c, POP_BLOCK); |
| 3078 | ADDOP(c, POP_EXCEPT); |
| 3079 | /* name = None; del name */ |
| 3080 | ADDOP_LOAD_CONST(c, Py_None); |
| 3081 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3082 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
| 3083 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3085 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3086 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3088 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3089 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3090 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3091 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3093 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | } |
| 3095 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3096 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3098 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3099 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3100 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3102 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3103 | ADDOP(c, POP_TOP); |
| 3104 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3105 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3106 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3108 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3109 | ADDOP(c, POP_EXCEPT); |
| 3110 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | compiler_use_next_block(c, except); |
| 3113 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3114 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3116 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | compiler_use_next_block(c, end); |
| 3118 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
| 3121 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3122 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3123 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3124 | return compiler_try_finally(c, s); |
| 3125 | else |
| 3126 | return compiler_try_except(c, s); |
| 3127 | } |
| 3128 | |
| 3129 | |
| 3130 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3131 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | /* The IMPORT_NAME opcode was already generated. This function |
| 3134 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | 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] | 3137 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3139 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3140 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3141 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3142 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3143 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3145 | while (1) { |
| 3146 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3148 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3149 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3150 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3151 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3153 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3154 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3155 | if (dot == -1) { |
| 3156 | break; |
| 3157 | } |
| 3158 | ADDOP(c, ROT_TWO); |
| 3159 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3161 | if (!compiler_nameop(c, asname, Store)) { |
| 3162 | return 0; |
| 3163 | } |
| 3164 | ADDOP(c, POP_TOP); |
| 3165 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3166 | } |
| 3167 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | static int |
| 3171 | compiler_import(struct compiler *c, stmt_ty s) |
| 3172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | /* The Import node stores a module name like a.b.c as a single |
| 3174 | string. This is convenient for all cases except |
| 3175 | import a.b.c as d |
| 3176 | where we need to parse that string to extract the individual |
| 3177 | module names. |
| 3178 | XXX Perhaps change the representation to make this case simpler? |
| 3179 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3180 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | for (i = 0; i < n; i++) { |
| 3183 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3184 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3186 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3187 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3189 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | if (alias->asname) { |
| 3191 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3192 | if (!r) |
| 3193 | return r; |
| 3194 | } |
| 3195 | else { |
| 3196 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3197 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3198 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3199 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3200 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3201 | if (tmp == NULL) |
| 3202 | return 0; |
| 3203 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3205 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | Py_DECREF(tmp); |
| 3207 | } |
| 3208 | if (!r) |
| 3209 | return r; |
| 3210 | } |
| 3211 | } |
| 3212 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | static int |
| 3216 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3217 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3218 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3219 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3220 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | if (!empty_string) { |
| 3223 | empty_string = PyUnicode_FromString(""); |
| 3224 | if (!empty_string) |
| 3225 | return 0; |
| 3226 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3227 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3228 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3229 | |
| 3230 | names = PyTuple_New(n); |
| 3231 | if (!names) |
| 3232 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | /* build up the names */ |
| 3235 | for (i = 0; i < n; i++) { |
| 3236 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3237 | Py_INCREF(alias->name); |
| 3238 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3239 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3241 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3242 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3243 | Py_DECREF(names); |
| 3244 | return compiler_error(c, "from __future__ imports must occur " |
| 3245 | "at the beginning of the file"); |
| 3246 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3247 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | if (s->v.ImportFrom.module) { |
| 3250 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3251 | } |
| 3252 | else { |
| 3253 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3254 | } |
| 3255 | for (i = 0; i < n; i++) { |
| 3256 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3257 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3258 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3259 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | assert(n == 1); |
| 3261 | ADDOP(c, IMPORT_STAR); |
| 3262 | return 1; |
| 3263 | } |
| 3264 | |
| 3265 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3266 | store_name = alias->name; |
| 3267 | if (alias->asname) |
| 3268 | store_name = alias->asname; |
| 3269 | |
| 3270 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | return 0; |
| 3272 | } |
| 3273 | } |
| 3274 | /* remove imported module */ |
| 3275 | ADDOP(c, POP_TOP); |
| 3276 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | static int |
| 3280 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3283 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3284 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3286 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3287 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3288 | { |
| 3289 | if (!compiler_warn(c, "assertion is always true, " |
| 3290 | "perhaps remove parentheses?")) |
| 3291 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3292 | return 0; |
| 3293 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3295 | end = compiler_new_block(c); |
| 3296 | if (end == NULL) |
| 3297 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3298 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3299 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3300 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3301 | if (s->v.Assert.msg) { |
| 3302 | VISIT(c, expr, s->v.Assert.msg); |
| 3303 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3304 | } |
| 3305 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3306 | compiler_use_next_block(c, end); |
| 3307 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3311 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3312 | { |
| 3313 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3314 | VISIT(c, expr, value); |
| 3315 | ADDOP(c, PRINT_EXPR); |
| 3316 | return 1; |
| 3317 | } |
| 3318 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3319 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3320 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3321 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | VISIT(c, expr, value); |
| 3325 | ADDOP(c, POP_TOP); |
| 3326 | return 1; |
| 3327 | } |
| 3328 | |
| 3329 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3330 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3331 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3332 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3335 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | switch (s->kind) { |
| 3338 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3339 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | case ClassDef_kind: |
| 3341 | return compiler_class(c, s); |
| 3342 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3343 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | case Delete_kind: |
| 3345 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3346 | break; |
| 3347 | case Assign_kind: |
| 3348 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3349 | VISIT(c, expr, s->v.Assign.value); |
| 3350 | for (i = 0; i < n; i++) { |
| 3351 | if (i < n - 1) |
| 3352 | ADDOP(c, DUP_TOP); |
| 3353 | VISIT(c, expr, |
| 3354 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3355 | } |
| 3356 | break; |
| 3357 | case AugAssign_kind: |
| 3358 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3359 | case AnnAssign_kind: |
| 3360 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | case For_kind: |
| 3362 | return compiler_for(c, s); |
| 3363 | case While_kind: |
| 3364 | return compiler_while(c, s); |
| 3365 | case If_kind: |
| 3366 | return compiler_if(c, s); |
| 3367 | case Raise_kind: |
| 3368 | n = 0; |
| 3369 | if (s->v.Raise.exc) { |
| 3370 | VISIT(c, expr, s->v.Raise.exc); |
| 3371 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3372 | if (s->v.Raise.cause) { |
| 3373 | VISIT(c, expr, s->v.Raise.cause); |
| 3374 | n++; |
| 3375 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3376 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3377 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3379 | case Try_kind: |
| 3380 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | case Assert_kind: |
| 3382 | return compiler_assert(c, s); |
| 3383 | case Import_kind: |
| 3384 | return compiler_import(c, s); |
| 3385 | case ImportFrom_kind: |
| 3386 | return compiler_from_import(c, s); |
| 3387 | case Global_kind: |
| 3388 | case Nonlocal_kind: |
| 3389 | break; |
| 3390 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3391 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | case Pass_kind: |
| 3393 | break; |
| 3394 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3395 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | case Continue_kind: |
| 3397 | return compiler_continue(c); |
| 3398 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3399 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3400 | case AsyncFunctionDef_kind: |
| 3401 | return compiler_function(c, s, 1); |
| 3402 | case AsyncWith_kind: |
| 3403 | return compiler_async_with(c, s, 0); |
| 3404 | case AsyncFor_kind: |
| 3405 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3408 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3409 | } |
| 3410 | |
| 3411 | static int |
| 3412 | unaryop(unaryop_ty op) |
| 3413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3414 | switch (op) { |
| 3415 | case Invert: |
| 3416 | return UNARY_INVERT; |
| 3417 | case Not: |
| 3418 | return UNARY_NOT; |
| 3419 | case UAdd: |
| 3420 | return UNARY_POSITIVE; |
| 3421 | case USub: |
| 3422 | return UNARY_NEGATIVE; |
| 3423 | default: |
| 3424 | PyErr_Format(PyExc_SystemError, |
| 3425 | "unary op %d should not be possible", op); |
| 3426 | return 0; |
| 3427 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3428 | } |
| 3429 | |
| 3430 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3431 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | switch (op) { |
| 3434 | case Add: |
| 3435 | return BINARY_ADD; |
| 3436 | case Sub: |
| 3437 | return BINARY_SUBTRACT; |
| 3438 | case Mult: |
| 3439 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3440 | case MatMult: |
| 3441 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | case Div: |
| 3443 | return BINARY_TRUE_DIVIDE; |
| 3444 | case Mod: |
| 3445 | return BINARY_MODULO; |
| 3446 | case Pow: |
| 3447 | return BINARY_POWER; |
| 3448 | case LShift: |
| 3449 | return BINARY_LSHIFT; |
| 3450 | case RShift: |
| 3451 | return BINARY_RSHIFT; |
| 3452 | case BitOr: |
| 3453 | return BINARY_OR; |
| 3454 | case BitXor: |
| 3455 | return BINARY_XOR; |
| 3456 | case BitAnd: |
| 3457 | return BINARY_AND; |
| 3458 | case FloorDiv: |
| 3459 | return BINARY_FLOOR_DIVIDE; |
| 3460 | default: |
| 3461 | PyErr_Format(PyExc_SystemError, |
| 3462 | "binary op %d should not be possible", op); |
| 3463 | return 0; |
| 3464 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3465 | } |
| 3466 | |
| 3467 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3468 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3469 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | switch (op) { |
| 3471 | case Add: |
| 3472 | return INPLACE_ADD; |
| 3473 | case Sub: |
| 3474 | return INPLACE_SUBTRACT; |
| 3475 | case Mult: |
| 3476 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3477 | case MatMult: |
| 3478 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3479 | case Div: |
| 3480 | return INPLACE_TRUE_DIVIDE; |
| 3481 | case Mod: |
| 3482 | return INPLACE_MODULO; |
| 3483 | case Pow: |
| 3484 | return INPLACE_POWER; |
| 3485 | case LShift: |
| 3486 | return INPLACE_LSHIFT; |
| 3487 | case RShift: |
| 3488 | return INPLACE_RSHIFT; |
| 3489 | case BitOr: |
| 3490 | return INPLACE_OR; |
| 3491 | case BitXor: |
| 3492 | return INPLACE_XOR; |
| 3493 | case BitAnd: |
| 3494 | return INPLACE_AND; |
| 3495 | case FloorDiv: |
| 3496 | return INPLACE_FLOOR_DIVIDE; |
| 3497 | default: |
| 3498 | PyErr_Format(PyExc_SystemError, |
| 3499 | "inplace binary op %d should not be possible", op); |
| 3500 | return 0; |
| 3501 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static int |
| 3505 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3506 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3507 | int op, scope; |
| 3508 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3509 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | PyObject *dict = c->u->u_names; |
| 3512 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3513 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3514 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3515 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3516 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3517 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3518 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3519 | if (!mangled) |
| 3520 | return 0; |
| 3521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | op = 0; |
| 3523 | optype = OP_NAME; |
| 3524 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3525 | switch (scope) { |
| 3526 | case FREE: |
| 3527 | dict = c->u->u_freevars; |
| 3528 | optype = OP_DEREF; |
| 3529 | break; |
| 3530 | case CELL: |
| 3531 | dict = c->u->u_cellvars; |
| 3532 | optype = OP_DEREF; |
| 3533 | break; |
| 3534 | case LOCAL: |
| 3535 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3536 | optype = OP_FAST; |
| 3537 | break; |
| 3538 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3539 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | optype = OP_GLOBAL; |
| 3541 | break; |
| 3542 | case GLOBAL_EXPLICIT: |
| 3543 | optype = OP_GLOBAL; |
| 3544 | break; |
| 3545 | default: |
| 3546 | /* scope can be 0 */ |
| 3547 | break; |
| 3548 | } |
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 | /* 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] | 3551 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | switch (optype) { |
| 3554 | case OP_DEREF: |
| 3555 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3556 | case Load: |
| 3557 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3558 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3559 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3560 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3561 | } |
| 3562 | break; |
| 3563 | case OP_FAST: |
| 3564 | switch (ctx) { |
| 3565 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3566 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3568 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3569 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | return 1; |
| 3571 | case OP_GLOBAL: |
| 3572 | switch (ctx) { |
| 3573 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3574 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3575 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3576 | } |
| 3577 | break; |
| 3578 | case OP_NAME: |
| 3579 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3580 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3581 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3582 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | } |
| 3584 | break; |
| 3585 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3588 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3589 | Py_DECREF(mangled); |
| 3590 | if (arg < 0) |
| 3591 | return 0; |
| 3592 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
| 3595 | static int |
| 3596 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3599 | int jumpi; |
| 3600 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3601 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3603 | assert(e->kind == BoolOp_kind); |
| 3604 | if (e->v.BoolOp.op == And) |
| 3605 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3606 | else |
| 3607 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3608 | end = compiler_new_block(c); |
| 3609 | if (end == NULL) |
| 3610 | return 0; |
| 3611 | s = e->v.BoolOp.values; |
| 3612 | n = asdl_seq_LEN(s) - 1; |
| 3613 | assert(n >= 0); |
| 3614 | for (i = 0; i < n; ++i) { |
| 3615 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3616 | ADDOP_JABS(c, jumpi, end); |
| 3617 | } |
| 3618 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3619 | compiler_use_next_block(c, end); |
| 3620 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | static int |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3624 | starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed, |
| 3625 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3626 | { |
| 3627 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3628 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3629 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3630 | PyObject *folded = PyTuple_New(n); |
| 3631 | if (folded == NULL) { |
| 3632 | return 0; |
| 3633 | } |
| 3634 | PyObject *val; |
| 3635 | for (i = 0; i < n; i++) { |
| 3636 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3637 | Py_INCREF(val); |
| 3638 | PyTuple_SET_ITEM(folded, i, val); |
| 3639 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3640 | if (tuple) { |
| 3641 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3642 | } else { |
| 3643 | if (add == SET_ADD) { |
| 3644 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3645 | if (folded == NULL) { |
| 3646 | return 0; |
| 3647 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3648 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3649 | ADDOP_I(c, build, pushed); |
| 3650 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3651 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3652 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3653 | return 1; |
| 3654 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3655 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3656 | for (i = 0; i < n; i++) { |
| 3657 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3658 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3659 | seen_star = 1; |
| 3660 | } |
| 3661 | } |
| 3662 | if (seen_star) { |
| 3663 | seen_star = 0; |
| 3664 | for (i = 0; i < n; i++) { |
| 3665 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3666 | if (elt->kind == Starred_kind) { |
| 3667 | if (seen_star == 0) { |
| 3668 | ADDOP_I(c, build, i+pushed); |
| 3669 | seen_star = 1; |
| 3670 | } |
| 3671 | VISIT(c, expr, elt->v.Starred.value); |
| 3672 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3673 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3674 | else { |
| 3675 | VISIT(c, expr, elt); |
| 3676 | if (seen_star) { |
| 3677 | ADDOP_I(c, add, 1); |
| 3678 | } |
| 3679 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3680 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3681 | assert(seen_star); |
| 3682 | if (tuple) { |
| 3683 | ADDOP(c, LIST_TO_TUPLE); |
| 3684 | } |
| 3685 | } |
| 3686 | else { |
| 3687 | for (i = 0; i < n; i++) { |
| 3688 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3689 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3690 | } |
| 3691 | if (tuple) { |
| 3692 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3693 | } else { |
| 3694 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3695 | } |
| 3696 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3697 | return 1; |
| 3698 | } |
| 3699 | |
| 3700 | static int |
| 3701 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3702 | { |
| 3703 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3704 | Py_ssize_t i; |
| 3705 | int seen_star = 0; |
| 3706 | for (i = 0; i < n; i++) { |
| 3707 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3708 | if (elt->kind == Starred_kind && !seen_star) { |
| 3709 | if ((i >= (1 << 8)) || |
| 3710 | (n-i-1 >= (INT_MAX >> 8))) |
| 3711 | return compiler_error(c, |
| 3712 | "too many expressions in " |
| 3713 | "star-unpacking assignment"); |
| 3714 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3715 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3716 | } |
| 3717 | else if (elt->kind == Starred_kind) { |
| 3718 | return compiler_error(c, |
| 3719 | "two starred expressions in assignment"); |
| 3720 | } |
| 3721 | } |
| 3722 | if (!seen_star) { |
| 3723 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3724 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3725 | for (i = 0; i < n; i++) { |
| 3726 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3727 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3728 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3729 | return 1; |
| 3730 | } |
| 3731 | |
| 3732 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3733 | compiler_list(struct compiler *c, expr_ty e) |
| 3734 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3735 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3736 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3737 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3739 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3740 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3741 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3742 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3743 | else |
| 3744 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3745 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | static int |
| 3749 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3750 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3751 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3752 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3753 | return assignment_helper(c, elts); |
| 3754 | } |
| 3755 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3756 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3757 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3758 | } |
| 3759 | else |
| 3760 | VISIT_SEQ(c, expr, elts); |
| 3761 | return 1; |
| 3762 | } |
| 3763 | |
| 3764 | static int |
| 3765 | compiler_set(struct compiler *c, expr_ty e) |
| 3766 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3767 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3768 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3772 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3773 | { |
| 3774 | Py_ssize_t i; |
| 3775 | for (i = begin; i < end; i++) { |
| 3776 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3777 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3778 | return 0; |
| 3779 | } |
| 3780 | return 1; |
| 3781 | } |
| 3782 | |
| 3783 | static int |
| 3784 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3785 | { |
| 3786 | Py_ssize_t i, n = end - begin; |
| 3787 | PyObject *keys, *key; |
| 3788 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3789 | for (i = begin; i < end; i++) { |
| 3790 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3791 | } |
| 3792 | keys = PyTuple_New(n); |
| 3793 | if (keys == NULL) { |
| 3794 | return 0; |
| 3795 | } |
| 3796 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3797 | 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] | 3798 | Py_INCREF(key); |
| 3799 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3800 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3801 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3802 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3803 | } |
| 3804 | else { |
| 3805 | for (i = begin; i < end; i++) { |
| 3806 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3807 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3808 | } |
| 3809 | ADDOP_I(c, BUILD_MAP, n); |
| 3810 | } |
| 3811 | return 1; |
| 3812 | } |
| 3813 | |
| 3814 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3815 | compiler_dict(struct compiler *c, expr_ty e) |
| 3816 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3817 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3818 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3819 | int is_unpacking = 0; |
| 3820 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3821 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3822 | elements = 0; |
| 3823 | for (i = 0; i < n; i++) { |
| 3824 | 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] | 3825 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3826 | if (elements) { |
| 3827 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3828 | return 0; |
| 3829 | } |
| 3830 | if (have_dict) { |
| 3831 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3832 | } |
| 3833 | have_dict = 1; |
| 3834 | elements = 0; |
| 3835 | } |
| 3836 | if (have_dict == 0) { |
| 3837 | ADDOP_I(c, BUILD_MAP, 0); |
| 3838 | have_dict = 1; |
| 3839 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3840 | 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] | 3841 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3842 | } |
| 3843 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3844 | if (elements == 0xFFFF) { |
| 3845 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3846 | return 0; |
| 3847 | } |
| 3848 | if (have_dict) { |
| 3849 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3850 | } |
| 3851 | have_dict = 1; |
| 3852 | elements = 0; |
| 3853 | } |
| 3854 | else { |
| 3855 | elements++; |
| 3856 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | } |
| 3858 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3859 | if (elements) { |
| 3860 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3861 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3862 | } |
| 3863 | if (have_dict) { |
| 3864 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3865 | } |
| 3866 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3867 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3868 | if (!have_dict) { |
| 3869 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3870 | } |
| 3871 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | static int |
| 3875 | compiler_compare(struct compiler *c, expr_ty e) |
| 3876 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3877 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3878 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3879 | if (!check_compare(c, e)) { |
| 3880 | return 0; |
| 3881 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3882 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3883 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3884 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3885 | if (n == 0) { |
| 3886 | 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] | 3887 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3888 | } |
| 3889 | else { |
| 3890 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3891 | if (cleanup == NULL) |
| 3892 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3893 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3894 | VISIT(c, expr, |
| 3895 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3896 | ADDOP(c, DUP_TOP); |
| 3897 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3898 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3899 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3900 | NEXT_BLOCK(c); |
| 3901 | } |
| 3902 | 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] | 3903 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3904 | basicblock *end = compiler_new_block(c); |
| 3905 | if (end == NULL) |
| 3906 | return 0; |
| 3907 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3908 | compiler_use_next_block(c, cleanup); |
| 3909 | ADDOP(c, ROT_TWO); |
| 3910 | ADDOP(c, POP_TOP); |
| 3911 | compiler_use_next_block(c, end); |
| 3912 | } |
| 3913 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3916 | static PyTypeObject * |
| 3917 | infer_type(expr_ty e) |
| 3918 | { |
| 3919 | switch (e->kind) { |
| 3920 | case Tuple_kind: |
| 3921 | return &PyTuple_Type; |
| 3922 | case List_kind: |
| 3923 | case ListComp_kind: |
| 3924 | return &PyList_Type; |
| 3925 | case Dict_kind: |
| 3926 | case DictComp_kind: |
| 3927 | return &PyDict_Type; |
| 3928 | case Set_kind: |
| 3929 | case SetComp_kind: |
| 3930 | return &PySet_Type; |
| 3931 | case GeneratorExp_kind: |
| 3932 | return &PyGen_Type; |
| 3933 | case Lambda_kind: |
| 3934 | return &PyFunction_Type; |
| 3935 | case JoinedStr_kind: |
| 3936 | case FormattedValue_kind: |
| 3937 | return &PyUnicode_Type; |
| 3938 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3939 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3940 | default: |
| 3941 | return NULL; |
| 3942 | } |
| 3943 | } |
| 3944 | |
| 3945 | static int |
| 3946 | check_caller(struct compiler *c, expr_ty e) |
| 3947 | { |
| 3948 | switch (e->kind) { |
| 3949 | case Constant_kind: |
| 3950 | case Tuple_kind: |
| 3951 | case List_kind: |
| 3952 | case ListComp_kind: |
| 3953 | case Dict_kind: |
| 3954 | case DictComp_kind: |
| 3955 | case Set_kind: |
| 3956 | case SetComp_kind: |
| 3957 | case GeneratorExp_kind: |
| 3958 | case JoinedStr_kind: |
| 3959 | case FormattedValue_kind: |
| 3960 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3961 | "perhaps you missed a comma?", |
| 3962 | infer_type(e)->tp_name); |
| 3963 | default: |
| 3964 | return 1; |
| 3965 | } |
| 3966 | } |
| 3967 | |
| 3968 | static int |
| 3969 | check_subscripter(struct compiler *c, expr_ty e) |
| 3970 | { |
| 3971 | PyObject *v; |
| 3972 | |
| 3973 | switch (e->kind) { |
| 3974 | case Constant_kind: |
| 3975 | v = e->v.Constant.value; |
| 3976 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3977 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3978 | PyAnySet_Check(v))) |
| 3979 | { |
| 3980 | return 1; |
| 3981 | } |
| 3982 | /* fall through */ |
| 3983 | case Set_kind: |
| 3984 | case SetComp_kind: |
| 3985 | case GeneratorExp_kind: |
| 3986 | case Lambda_kind: |
| 3987 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3988 | "perhaps you missed a comma?", |
| 3989 | infer_type(e)->tp_name); |
| 3990 | default: |
| 3991 | return 1; |
| 3992 | } |
| 3993 | } |
| 3994 | |
| 3995 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 3996 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3997 | { |
| 3998 | PyObject *v; |
| 3999 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4000 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4001 | if (index_type == NULL |
| 4002 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4003 | || index_type == &PySlice_Type) { |
| 4004 | return 1; |
| 4005 | } |
| 4006 | |
| 4007 | switch (e->kind) { |
| 4008 | case Constant_kind: |
| 4009 | v = e->v.Constant.value; |
| 4010 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4011 | return 1; |
| 4012 | } |
| 4013 | /* fall through */ |
| 4014 | case Tuple_kind: |
| 4015 | case List_kind: |
| 4016 | case ListComp_kind: |
| 4017 | case JoinedStr_kind: |
| 4018 | case FormattedValue_kind: |
| 4019 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4020 | "not %.200s; " |
| 4021 | "perhaps you missed a comma?", |
| 4022 | infer_type(e)->tp_name, |
| 4023 | index_type->tp_name); |
| 4024 | default: |
| 4025 | return 1; |
| 4026 | } |
| 4027 | } |
| 4028 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4029 | // 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] | 4030 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4031 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4032 | { |
| 4033 | Py_ssize_t argsl, i; |
| 4034 | expr_ty meth = e->v.Call.func; |
| 4035 | asdl_seq *args = e->v.Call.args; |
| 4036 | |
| 4037 | /* Check that the call node is an attribute access, and that |
| 4038 | the call doesn't have keyword parameters. */ |
| 4039 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4040 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4041 | return -1; |
| 4042 | |
| 4043 | /* Check that there are no *varargs types of arguments. */ |
| 4044 | argsl = asdl_seq_LEN(args); |
| 4045 | for (i = 0; i < argsl; i++) { |
| 4046 | expr_ty elt = asdl_seq_GET(args, i); |
| 4047 | if (elt->kind == Starred_kind) { |
| 4048 | return -1; |
| 4049 | } |
| 4050 | } |
| 4051 | |
| 4052 | /* Alright, we can optimize the code. */ |
| 4053 | VISIT(c, expr, meth->v.Attribute.value); |
| 4054 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4055 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4056 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4057 | return 1; |
| 4058 | } |
| 4059 | |
| 4060 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4061 | compiler_call(struct compiler *c, expr_ty e) |
| 4062 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4063 | int ret = maybe_optimize_method_call(c, e); |
| 4064 | if (ret >= 0) { |
| 4065 | return ret; |
| 4066 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4067 | if (!check_caller(c, e->v.Call.func)) { |
| 4068 | return 0; |
| 4069 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4070 | VISIT(c, expr, e->v.Call.func); |
| 4071 | return compiler_call_helper(c, 0, |
| 4072 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4073 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4076 | static int |
| 4077 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4078 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4079 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4080 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4081 | 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] | 4082 | return 1; |
| 4083 | } |
| 4084 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4085 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4086 | static int |
| 4087 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4088 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4089 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4090 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4091 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4092 | Convert the conversion char to 3 bits: |
| 4093 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4094 | !s : 001 0x1 FVC_STR |
| 4095 | !r : 010 0x2 FVC_REPR |
| 4096 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4097 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4098 | next bit is whether or not we have a format spec: |
| 4099 | yes : 100 0x4 |
| 4100 | no : 000 0x0 |
| 4101 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4102 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4103 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4104 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4105 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4106 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4107 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4108 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4109 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4110 | case 's': oparg = FVC_STR; break; |
| 4111 | case 'r': oparg = FVC_REPR; break; |
| 4112 | case 'a': oparg = FVC_ASCII; break; |
| 4113 | case -1: oparg = FVC_NONE; break; |
| 4114 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4115 | PyErr_Format(PyExc_SystemError, |
| 4116 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4117 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4118 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4119 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4120 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4121 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4122 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4123 | } |
| 4124 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4125 | /* And push our opcode and oparg */ |
| 4126 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4127 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4128 | return 1; |
| 4129 | } |
| 4130 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4131 | static int |
| 4132 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 4133 | { |
| 4134 | Py_ssize_t i, n = end - begin; |
| 4135 | keyword_ty kw; |
| 4136 | PyObject *keys, *key; |
| 4137 | assert(n > 0); |
| 4138 | if (n > 1) { |
| 4139 | for (i = begin; i < end; i++) { |
| 4140 | kw = asdl_seq_GET(keywords, i); |
| 4141 | VISIT(c, expr, kw->value); |
| 4142 | } |
| 4143 | keys = PyTuple_New(n); |
| 4144 | if (keys == NULL) { |
| 4145 | return 0; |
| 4146 | } |
| 4147 | for (i = begin; i < end; i++) { |
| 4148 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4149 | Py_INCREF(key); |
| 4150 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4151 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4152 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4153 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4154 | } |
| 4155 | else { |
| 4156 | /* a for loop only executes once */ |
| 4157 | for (i = begin; i < end; i++) { |
| 4158 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4159 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4160 | VISIT(c, expr, kw->value); |
| 4161 | } |
| 4162 | ADDOP_I(c, BUILD_MAP, n); |
| 4163 | } |
| 4164 | return 1; |
| 4165 | } |
| 4166 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4167 | /* shared code between compiler_call and compiler_class */ |
| 4168 | static int |
| 4169 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4170 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4171 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4172 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4173 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4174 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4175 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4176 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4177 | nkwelts = asdl_seq_LEN(keywords); |
| 4178 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4179 | for (i = 0; i < nelts; i++) { |
| 4180 | expr_ty elt = asdl_seq_GET(args, i); |
| 4181 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4182 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4183 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4184 | } |
| 4185 | for (i = 0; i < nkwelts; i++) { |
| 4186 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4187 | if (kw->arg == NULL) { |
| 4188 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4189 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4190 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4191 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4192 | /* No * or ** args, so can use faster calling sequence */ |
| 4193 | for (i = 0; i < nelts; i++) { |
| 4194 | expr_ty elt = asdl_seq_GET(args, i); |
| 4195 | assert(elt->kind != Starred_kind); |
| 4196 | VISIT(c, expr, elt); |
| 4197 | } |
| 4198 | if (nkwelts) { |
| 4199 | PyObject *names; |
| 4200 | VISIT_SEQ(c, keyword, keywords); |
| 4201 | names = PyTuple_New(nkwelts); |
| 4202 | if (names == NULL) { |
| 4203 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4204 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4205 | for (i = 0; i < nkwelts; i++) { |
| 4206 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4207 | Py_INCREF(kw->arg); |
| 4208 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4209 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4210 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4211 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4212 | return 1; |
| 4213 | } |
| 4214 | else { |
| 4215 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4216 | return 1; |
| 4217 | } |
| 4218 | |
| 4219 | ex_call: |
| 4220 | |
| 4221 | /* Do positional arguments. */ |
| 4222 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4223 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4224 | } |
| 4225 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4226 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4227 | return 0; |
| 4228 | } |
| 4229 | /* Then keyword arguments */ |
| 4230 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4231 | /* Has a new dict been pushed */ |
| 4232 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4233 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4234 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4235 | for (i = 0; i < nkwelts; i++) { |
| 4236 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4237 | if (kw->arg == NULL) { |
| 4238 | /* A keyword argument unpacking. */ |
| 4239 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4240 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4241 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4242 | } |
| 4243 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4244 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4245 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4246 | if (!have_dict) { |
| 4247 | ADDOP_I(c, BUILD_MAP, 0); |
| 4248 | have_dict = 1; |
| 4249 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4250 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4251 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4252 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4253 | else { |
| 4254 | nseen++; |
| 4255 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4256 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4257 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4258 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4259 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4260 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4261 | } |
| 4262 | if (have_dict) { |
| 4263 | ADDOP_I(c, DICT_MERGE, 1); |
| 4264 | } |
| 4265 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4266 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4267 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4268 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4269 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4270 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4271 | } |
| 4272 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4273 | |
| 4274 | /* List and set comprehensions and generator expressions work by creating a |
| 4275 | nested function to perform the actual iteration. This means that the |
| 4276 | iteration variables don't leak into the current scope. |
| 4277 | The defined function is called immediately following its definition, with the |
| 4278 | result of that call being the result of the expression. |
| 4279 | The LC/SC version returns the populated container, while the GE version is |
| 4280 | flagged in symtable.c as a generator, so it returns the generator object |
| 4281 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4282 | |
| 4283 | Possible cleanups: |
| 4284 | - iterate over the generator sequence instead of using recursion |
| 4285 | */ |
| 4286 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4287 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4288 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | compiler_comprehension_generator(struct compiler *c, |
| 4290 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4291 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4292 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4293 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4294 | comprehension_ty gen; |
| 4295 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4296 | if (gen->is_async) { |
| 4297 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4298 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4299 | } else { |
| 4300 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4301 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | static int |
| 4306 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4307 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4308 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4309 | expr_ty elt, expr_ty val, int type) |
| 4310 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4311 | /* generate code for the iterator, then each of the ifs, |
| 4312 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | comprehension_ty gen; |
| 4315 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4316 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4318 | start = compiler_new_block(c); |
| 4319 | skip = compiler_new_block(c); |
| 4320 | if_cleanup = compiler_new_block(c); |
| 4321 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4324 | anchor == NULL) |
| 4325 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4329 | if (gen_index == 0) { |
| 4330 | /* Receive outermost iter as an implicit argument */ |
| 4331 | c->u->u_argcount = 1; |
| 4332 | ADDOP_I(c, LOAD_FAST, 0); |
| 4333 | } |
| 4334 | else { |
| 4335 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4336 | /* Fast path for the temporary variable assignment idiom: |
| 4337 | for y in [f(x)] |
| 4338 | */ |
| 4339 | asdl_seq *elts; |
| 4340 | switch (gen->iter->kind) { |
| 4341 | case List_kind: |
| 4342 | elts = gen->iter->v.List.elts; |
| 4343 | break; |
| 4344 | case Tuple_kind: |
| 4345 | elts = gen->iter->v.Tuple.elts; |
| 4346 | break; |
| 4347 | default: |
| 4348 | elts = NULL; |
| 4349 | } |
| 4350 | if (asdl_seq_LEN(elts) == 1) { |
| 4351 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4352 | if (elt->kind != Starred_kind) { |
| 4353 | VISIT(c, expr, elt); |
| 4354 | start = NULL; |
| 4355 | } |
| 4356 | } |
| 4357 | if (start) { |
| 4358 | VISIT(c, expr, gen->iter); |
| 4359 | ADDOP(c, GET_ITER); |
| 4360 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4362 | if (start) { |
| 4363 | depth++; |
| 4364 | compiler_use_next_block(c, start); |
| 4365 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4366 | NEXT_BLOCK(c); |
| 4367 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4368 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4370 | /* XXX this needs to be cleaned up...a lot! */ |
| 4371 | n = asdl_seq_LEN(gen->ifs); |
| 4372 | for (i = 0; i < n; i++) { |
| 4373 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4374 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4375 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | NEXT_BLOCK(c); |
| 4377 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4379 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4380 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4381 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | elt, val, type)) |
| 4383 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | /* only append after the last for generator */ |
| 4386 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4387 | /* comprehension specific code */ |
| 4388 | switch (type) { |
| 4389 | case COMP_GENEXP: |
| 4390 | VISIT(c, expr, elt); |
| 4391 | ADDOP(c, YIELD_VALUE); |
| 4392 | ADDOP(c, POP_TOP); |
| 4393 | break; |
| 4394 | case COMP_LISTCOMP: |
| 4395 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4396 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4397 | break; |
| 4398 | case COMP_SETCOMP: |
| 4399 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4400 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | break; |
| 4402 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4403 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4405 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4406 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4407 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | default: |
| 4410 | return 0; |
| 4411 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | compiler_use_next_block(c, skip); |
| 4414 | } |
| 4415 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4416 | if (start) { |
| 4417 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4418 | compiler_use_next_block(c, anchor); |
| 4419 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | |
| 4421 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
| 4424 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4425 | compiler_async_comprehension_generator(struct compiler *c, |
| 4426 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4427 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4428 | expr_ty elt, expr_ty val, int type) |
| 4429 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4430 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4431 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4432 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4433 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4434 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4435 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4436 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4437 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4438 | return 0; |
| 4439 | } |
| 4440 | |
| 4441 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4442 | |
| 4443 | if (gen_index == 0) { |
| 4444 | /* Receive outermost iter as an implicit argument */ |
| 4445 | c->u->u_argcount = 1; |
| 4446 | ADDOP_I(c, LOAD_FAST, 0); |
| 4447 | } |
| 4448 | else { |
| 4449 | /* Sub-iter - calculate on the fly */ |
| 4450 | VISIT(c, expr, gen->iter); |
| 4451 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4452 | } |
| 4453 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4454 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4455 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4456 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4457 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4458 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4459 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4460 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4461 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4462 | |
| 4463 | n = asdl_seq_LEN(gen->ifs); |
| 4464 | for (i = 0; i < n; i++) { |
| 4465 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4466 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4467 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4468 | NEXT_BLOCK(c); |
| 4469 | } |
| 4470 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4471 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4472 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4473 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4474 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4475 | elt, val, type)) |
| 4476 | return 0; |
| 4477 | |
| 4478 | /* only append after the last for generator */ |
| 4479 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4480 | /* comprehension specific code */ |
| 4481 | switch (type) { |
| 4482 | case COMP_GENEXP: |
| 4483 | VISIT(c, expr, elt); |
| 4484 | ADDOP(c, YIELD_VALUE); |
| 4485 | ADDOP(c, POP_TOP); |
| 4486 | break; |
| 4487 | case COMP_LISTCOMP: |
| 4488 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4489 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4490 | break; |
| 4491 | case COMP_SETCOMP: |
| 4492 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4493 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4494 | break; |
| 4495 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4496 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4497 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4498 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4499 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4500 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4501 | break; |
| 4502 | default: |
| 4503 | return 0; |
| 4504 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4505 | } |
| 4506 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4507 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4508 | |
| 4509 | compiler_use_next_block(c, except); |
| 4510 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4511 | |
| 4512 | return 1; |
| 4513 | } |
| 4514 | |
| 4515 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4516 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4517 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4518 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4521 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4522 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4523 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4524 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4525 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4526 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4527 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4528 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4529 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4531 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4532 | } |
| 4533 | |
| 4534 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4535 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4536 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4537 | compiler_error(c, "asynchronous comprehension outside of " |
| 4538 | "an asynchronous function"); |
| 4539 | goto error_in_scope; |
| 4540 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4542 | if (type != COMP_GENEXP) { |
| 4543 | int op; |
| 4544 | switch (type) { |
| 4545 | case COMP_LISTCOMP: |
| 4546 | op = BUILD_LIST; |
| 4547 | break; |
| 4548 | case COMP_SETCOMP: |
| 4549 | op = BUILD_SET; |
| 4550 | break; |
| 4551 | case COMP_DICTCOMP: |
| 4552 | op = BUILD_MAP; |
| 4553 | break; |
| 4554 | default: |
| 4555 | PyErr_Format(PyExc_SystemError, |
| 4556 | "unknown comprehension type %d", type); |
| 4557 | goto error_in_scope; |
| 4558 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4560 | ADDOP_I(c, op, 0); |
| 4561 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4562 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4563 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4564 | val, type)) |
| 4565 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4567 | if (type != COMP_GENEXP) { |
| 4568 | ADDOP(c, RETURN_VALUE); |
| 4569 | } |
| 4570 | |
| 4571 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4572 | qualname = c->u->u_qualname; |
| 4573 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4574 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4575 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4576 | goto error; |
| 4577 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4578 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4579 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4580 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | Py_DECREF(co); |
| 4582 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4583 | VISIT(c, expr, outermost->iter); |
| 4584 | |
| 4585 | if (outermost->is_async) { |
| 4586 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4587 | } else { |
| 4588 | ADDOP(c, GET_ITER); |
| 4589 | } |
| 4590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4591 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4592 | |
| 4593 | if (is_async_generator && type != COMP_GENEXP) { |
| 4594 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4595 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4596 | ADDOP(c, YIELD_FROM); |
| 4597 | } |
| 4598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4599 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4600 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4601 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4602 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4603 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | Py_XDECREF(co); |
| 4605 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4606 | } |
| 4607 | |
| 4608 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4609 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4610 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4611 | static identifier name; |
| 4612 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4613 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | if (!name) |
| 4615 | return 0; |
| 4616 | } |
| 4617 | assert(e->kind == GeneratorExp_kind); |
| 4618 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4619 | e->v.GeneratorExp.generators, |
| 4620 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4621 | } |
| 4622 | |
| 4623 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4624 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | static identifier name; |
| 4627 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4628 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4629 | if (!name) |
| 4630 | return 0; |
| 4631 | } |
| 4632 | assert(e->kind == ListComp_kind); |
| 4633 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4634 | e->v.ListComp.generators, |
| 4635 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | static int |
| 4639 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | static identifier name; |
| 4642 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4643 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4644 | if (!name) |
| 4645 | return 0; |
| 4646 | } |
| 4647 | assert(e->kind == SetComp_kind); |
| 4648 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4649 | e->v.SetComp.generators, |
| 4650 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | |
| 4654 | static int |
| 4655 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4656 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4657 | static identifier name; |
| 4658 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4659 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | if (!name) |
| 4661 | return 0; |
| 4662 | } |
| 4663 | assert(e->kind == DictComp_kind); |
| 4664 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4665 | e->v.DictComp.generators, |
| 4666 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | |
| 4670 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4671 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4672 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4673 | VISIT(c, expr, k->value); |
| 4674 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4675 | } |
| 4676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4678 | whether they are true or false. |
| 4679 | |
| 4680 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4681 | */ |
| 4682 | |
| 4683 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4684 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4685 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4686 | if (e->kind == Constant_kind) { |
| 4687 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4688 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4689 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4692 | static int |
| 4693 | compiler_with_except_finish(struct compiler *c) { |
| 4694 | basicblock *exit; |
| 4695 | exit = compiler_new_block(c); |
| 4696 | if (exit == NULL) |
| 4697 | return 0; |
| 4698 | ADDOP_JABS(c, POP_JUMP_IF_TRUE, exit); |
| 4699 | ADDOP(c, RERAISE); |
| 4700 | compiler_use_next_block(c, exit); |
| 4701 | ADDOP(c, POP_TOP); |
| 4702 | ADDOP(c, POP_TOP); |
| 4703 | ADDOP(c, POP_TOP); |
| 4704 | ADDOP(c, POP_EXCEPT); |
| 4705 | ADDOP(c, POP_TOP); |
| 4706 | return 1; |
| 4707 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4708 | |
| 4709 | /* |
| 4710 | Implements the async with statement. |
| 4711 | |
| 4712 | The semantics outlined in that PEP are as follows: |
| 4713 | |
| 4714 | async with EXPR as VAR: |
| 4715 | BLOCK |
| 4716 | |
| 4717 | It is implemented roughly as: |
| 4718 | |
| 4719 | context = EXPR |
| 4720 | exit = context.__aexit__ # not calling it |
| 4721 | value = await context.__aenter__() |
| 4722 | try: |
| 4723 | VAR = value # if VAR present in the syntax |
| 4724 | BLOCK |
| 4725 | finally: |
| 4726 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4727 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4728 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4729 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4730 | if not (await exit(*exc)): |
| 4731 | raise |
| 4732 | */ |
| 4733 | static int |
| 4734 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4735 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4736 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4737 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4738 | |
| 4739 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4740 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4741 | c->u->u_ste->ste_coroutine = 1; |
| 4742 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4743 | return compiler_error(c, "'async with' outside async function"); |
| 4744 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4745 | |
| 4746 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4747 | final = compiler_new_block(c); |
| 4748 | exit = compiler_new_block(c); |
| 4749 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4750 | return 0; |
| 4751 | |
| 4752 | /* Evaluate EXPR */ |
| 4753 | VISIT(c, expr, item->context_expr); |
| 4754 | |
| 4755 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4756 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4757 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4758 | ADDOP(c, YIELD_FROM); |
| 4759 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4760 | ADDOP_JREL(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4761 | |
| 4762 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4763 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4764 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4765 | return 0; |
| 4766 | } |
| 4767 | |
| 4768 | if (item->optional_vars) { |
| 4769 | VISIT(c, expr, item->optional_vars); |
| 4770 | } |
| 4771 | else { |
| 4772 | /* Discard result from context.__aenter__() */ |
| 4773 | ADDOP(c, POP_TOP); |
| 4774 | } |
| 4775 | |
| 4776 | pos++; |
| 4777 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4778 | /* BLOCK code */ |
| 4779 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4780 | else if (!compiler_async_with(c, s, pos)) |
| 4781 | return 0; |
| 4782 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4783 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4784 | ADDOP(c, POP_BLOCK); |
| 4785 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4786 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4787 | /* For successful outcome: |
| 4788 | * call __exit__(None, None, None) |
| 4789 | */ |
| 4790 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4791 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4792 | ADDOP(c, GET_AWAITABLE); |
| 4793 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4794 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4795 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4796 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4797 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4798 | ADDOP_JABS(c, JUMP_ABSOLUTE, exit); |
| 4799 | |
| 4800 | /* For exceptional outcome: */ |
| 4801 | compiler_use_next_block(c, final); |
| 4802 | |
| 4803 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4804 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4805 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4806 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4807 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4808 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4809 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4810 | return 1; |
| 4811 | } |
| 4812 | |
| 4813 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4814 | /* |
| 4815 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4816 | with EXPR as VAR: |
| 4817 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4818 | is implemented as: |
| 4819 | <code for EXPR> |
| 4820 | SETUP_WITH E |
| 4821 | <code to store to VAR> or POP_TOP |
| 4822 | <code for BLOCK> |
| 4823 | LOAD_CONST (None, None, None) |
| 4824 | CALL_FUNCTION_EX 0 |
| 4825 | JUMP_FORWARD EXIT |
| 4826 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4827 | POP_JUMP_IF_TRUE T: |
| 4828 | RERAISE |
| 4829 | T: POP_TOP * 3 (remove exception from stack) |
| 4830 | POP_EXCEPT |
| 4831 | POP_TOP |
| 4832 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4833 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4834 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4835 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4836 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4837 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4838 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4839 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4840 | |
| 4841 | assert(s->kind == With_kind); |
| 4842 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4843 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4844 | final = compiler_new_block(c); |
| 4845 | exit = compiler_new_block(c); |
| 4846 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4847 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4848 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4849 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4850 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4851 | /* Will push bound __exit__ */ |
| 4852 | ADDOP_JREL(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4853 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4854 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4855 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4856 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4857 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4858 | } |
| 4859 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4860 | if (item->optional_vars) { |
| 4861 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4862 | } |
| 4863 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4864 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4865 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4866 | } |
| 4867 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4868 | pos++; |
| 4869 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4870 | /* BLOCK code */ |
| 4871 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4872 | else if (!compiler_with(c, s, pos)) |
| 4873 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4874 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4875 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4876 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4877 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4878 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4879 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4880 | /* For successful outcome: |
| 4881 | * call __exit__(None, None, None) |
| 4882 | */ |
| 4883 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4884 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4885 | ADDOP(c, POP_TOP); |
| 4886 | ADDOP_JREL(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4887 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4888 | /* For exceptional outcome: */ |
| 4889 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4890 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4891 | ADDOP(c, WITH_EXCEPT_START); |
| 4892 | compiler_with_except_finish(c); |
| 4893 | |
| 4894 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4895 | return 1; |
| 4896 | } |
| 4897 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4898 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4899 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4900 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4901 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4902 | case NamedExpr_kind: |
| 4903 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4904 | ADDOP(c, DUP_TOP); |
| 4905 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4906 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | case BoolOp_kind: |
| 4908 | return compiler_boolop(c, e); |
| 4909 | case BinOp_kind: |
| 4910 | VISIT(c, expr, e->v.BinOp.left); |
| 4911 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 4912 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | break; |
| 4914 | case UnaryOp_kind: |
| 4915 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4916 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4917 | break; |
| 4918 | case Lambda_kind: |
| 4919 | return compiler_lambda(c, e); |
| 4920 | case IfExp_kind: |
| 4921 | return compiler_ifexp(c, e); |
| 4922 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4923 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4924 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4925 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4926 | case GeneratorExp_kind: |
| 4927 | return compiler_genexp(c, e); |
| 4928 | case ListComp_kind: |
| 4929 | return compiler_listcomp(c, e); |
| 4930 | case SetComp_kind: |
| 4931 | return compiler_setcomp(c, e); |
| 4932 | case DictComp_kind: |
| 4933 | return compiler_dictcomp(c, e); |
| 4934 | case Yield_kind: |
| 4935 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4936 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4937 | if (e->v.Yield.value) { |
| 4938 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | } |
| 4940 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4941 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4942 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4943 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4945 | case YieldFrom_kind: |
| 4946 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4947 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4948 | |
| 4949 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4950 | return compiler_error(c, "'yield from' inside async function"); |
| 4951 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4952 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4953 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4954 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4955 | ADDOP(c, YIELD_FROM); |
| 4956 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4957 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4958 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4959 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 4960 | return compiler_error(c, "'await' outside function"); |
| 4961 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4962 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 4963 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4964 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 4965 | return compiler_error(c, "'await' outside async function"); |
| 4966 | } |
| 4967 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4968 | |
| 4969 | VISIT(c, expr, e->v.Await.value); |
| 4970 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4971 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4972 | ADDOP(c, YIELD_FROM); |
| 4973 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4974 | case Compare_kind: |
| 4975 | return compiler_compare(c, e); |
| 4976 | case Call_kind: |
| 4977 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4978 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4979 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4980 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4981 | case JoinedStr_kind: |
| 4982 | return compiler_joined_str(c, e); |
| 4983 | case FormattedValue_kind: |
| 4984 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4985 | /* The following exprs can be assignment targets. */ |
| 4986 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 4987 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4989 | case Load: |
| 4990 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4991 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4992 | case Store: |
| 4993 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4994 | break; |
| 4995 | case Del: |
| 4996 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4997 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | } |
| 4999 | break; |
| 5000 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5001 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | case Starred_kind: |
| 5003 | switch (e->v.Starred.ctx) { |
| 5004 | case Store: |
| 5005 | /* In all legitimate cases, the Starred node was already replaced |
| 5006 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5007 | return compiler_error(c, |
| 5008 | "starred assignment target must be in a list or tuple"); |
| 5009 | default: |
| 5010 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5011 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5012 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5013 | break; |
| 5014 | case Slice_kind: |
| 5015 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5016 | case Name_kind: |
| 5017 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5018 | /* child nodes of List and Tuple will have expr_context set */ |
| 5019 | case List_kind: |
| 5020 | return compiler_list(c, e); |
| 5021 | case Tuple_kind: |
| 5022 | return compiler_tuple(c, e); |
| 5023 | } |
| 5024 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
| 5027 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5028 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5029 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5030 | int old_lineno = c->u->u_lineno; |
| 5031 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5032 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5033 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5034 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5035 | c->u->u_col_offset = old_col_offset; |
| 5036 | return res; |
| 5037 | } |
| 5038 | |
| 5039 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5040 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5042 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5043 | expr_ty e = s->v.AugAssign.target; |
| 5044 | |
| 5045 | int old_lineno = c->u->u_lineno; |
| 5046 | int old_col_offset = c->u->u_col_offset; |
| 5047 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5049 | switch (e->kind) { |
| 5050 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5051 | VISIT(c, expr, e->v.Attribute.value); |
| 5052 | ADDOP(c, DUP_TOP); |
| 5053 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5054 | break; |
| 5055 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5056 | VISIT(c, expr, e->v.Subscript.value); |
| 5057 | VISIT(c, expr, e->v.Subscript.slice); |
| 5058 | ADDOP(c, DUP_TOP_TWO); |
| 5059 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5060 | break; |
| 5061 | case Name_kind: |
| 5062 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5063 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5064 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5065 | default: |
| 5066 | PyErr_Format(PyExc_SystemError, |
| 5067 | "invalid node type (%d) for augmented assignment", |
| 5068 | e->kind); |
| 5069 | return 0; |
| 5070 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5071 | |
| 5072 | c->u->u_lineno = old_lineno; |
| 5073 | c->u->u_col_offset = old_col_offset; |
| 5074 | |
| 5075 | VISIT(c, expr, s->v.AugAssign.value); |
| 5076 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5077 | |
| 5078 | SET_LOC(c, e); |
| 5079 | |
| 5080 | switch (e->kind) { |
| 5081 | case Attribute_kind: |
| 5082 | ADDOP(c, ROT_TWO); |
| 5083 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5084 | break; |
| 5085 | case Subscript_kind: |
| 5086 | ADDOP(c, ROT_THREE); |
| 5087 | ADDOP(c, STORE_SUBSCR); |
| 5088 | break; |
| 5089 | case Name_kind: |
| 5090 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5091 | default: |
| 5092 | Py_UNREACHABLE(); |
| 5093 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5098 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5099 | { |
| 5100 | VISIT(c, expr, e); |
| 5101 | ADDOP(c, POP_TOP); |
| 5102 | return 1; |
| 5103 | } |
| 5104 | |
| 5105 | static int |
| 5106 | check_annotation(struct compiler *c, stmt_ty s) |
| 5107 | { |
| 5108 | /* Annotations are only evaluated in a module or class. */ |
| 5109 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5110 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5111 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5112 | } |
| 5113 | return 1; |
| 5114 | } |
| 5115 | |
| 5116 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5117 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5118 | { |
| 5119 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5120 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5121 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5122 | 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] | 5123 | return 0; |
| 5124 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5125 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5126 | return 0; |
| 5127 | } |
| 5128 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5129 | return 0; |
| 5130 | } |
| 5131 | return 1; |
| 5132 | case Tuple_kind: { |
| 5133 | /* extended slice */ |
| 5134 | asdl_seq *elts = e->v.Tuple.elts; |
| 5135 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5136 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5137 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5138 | return 0; |
| 5139 | } |
| 5140 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5141 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5142 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5143 | default: |
| 5144 | return check_ann_expr(c, e); |
| 5145 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5146 | } |
| 5147 | |
| 5148 | static int |
| 5149 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5150 | { |
| 5151 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5152 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5153 | |
| 5154 | assert(s->kind == AnnAssign_kind); |
| 5155 | |
| 5156 | /* We perform the actual assignment first. */ |
| 5157 | if (s->v.AnnAssign.value) { |
| 5158 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5159 | VISIT(c, expr, targ); |
| 5160 | } |
| 5161 | switch (targ->kind) { |
| 5162 | case Name_kind: |
| 5163 | /* If we have a simple name in a module or class, store annotation. */ |
| 5164 | if (s->v.AnnAssign.simple && |
| 5165 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5166 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5167 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5168 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5169 | } |
| 5170 | else { |
| 5171 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5172 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5173 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5174 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5175 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5176 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5177 | } |
| 5178 | break; |
| 5179 | case Attribute_kind: |
| 5180 | if (!s->v.AnnAssign.value && |
| 5181 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5182 | return 0; |
| 5183 | } |
| 5184 | break; |
| 5185 | case Subscript_kind: |
| 5186 | if (!s->v.AnnAssign.value && |
| 5187 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5188 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5189 | return 0; |
| 5190 | } |
| 5191 | break; |
| 5192 | default: |
| 5193 | PyErr_Format(PyExc_SystemError, |
| 5194 | "invalid node type (%d) for annotated assignment", |
| 5195 | targ->kind); |
| 5196 | return 0; |
| 5197 | } |
| 5198 | /* Annotation is evaluated last. */ |
| 5199 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5200 | return 0; |
| 5201 | } |
| 5202 | return 1; |
| 5203 | } |
| 5204 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5205 | /* Raises a SyntaxError and returns 0. |
| 5206 | If something goes wrong, a different exception may be raised. |
| 5207 | */ |
| 5208 | |
| 5209 | static int |
| 5210 | compiler_error(struct compiler *c, const char *errstr) |
| 5211 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5212 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5213 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5214 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5215 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5216 | if (!loc) { |
| 5217 | Py_INCREF(Py_None); |
| 5218 | loc = Py_None; |
| 5219 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5220 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5221 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | if (!u) |
| 5223 | goto exit; |
| 5224 | v = Py_BuildValue("(zO)", errstr, u); |
| 5225 | if (!v) |
| 5226 | goto exit; |
| 5227 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5228 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5229 | Py_DECREF(loc); |
| 5230 | Py_XDECREF(u); |
| 5231 | Py_XDECREF(v); |
| 5232 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5235 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5236 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5237 | and returns 0. |
| 5238 | */ |
| 5239 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5240 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5241 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5242 | va_list vargs; |
| 5243 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5244 | va_start(vargs, format); |
| 5245 | #else |
| 5246 | va_start(vargs); |
| 5247 | #endif |
| 5248 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5249 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5250 | if (msg == NULL) { |
| 5251 | return 0; |
| 5252 | } |
| 5253 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5254 | c->u->u_lineno, NULL, NULL) < 0) |
| 5255 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5256 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5257 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5258 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5259 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5260 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5261 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5262 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5263 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5264 | return 0; |
| 5265 | } |
| 5266 | Py_DECREF(msg); |
| 5267 | return 1; |
| 5268 | } |
| 5269 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5270 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5271 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5272 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5273 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5274 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5275 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5276 | if (ctx == Load) { |
| 5277 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5278 | return 0; |
| 5279 | } |
| 5280 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5281 | return 0; |
| 5282 | } |
| 5283 | } |
| 5284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5286 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | case Store: op = STORE_SUBSCR; break; |
| 5288 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5290 | assert(op); |
| 5291 | VISIT(c, expr, e->v.Subscript.value); |
| 5292 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5293 | ADDOP(c, op); |
| 5294 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5295 | } |
| 5296 | |
| 5297 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5298 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5299 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | int n = 2; |
| 5301 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5303 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5304 | if (s->v.Slice.lower) { |
| 5305 | VISIT(c, expr, s->v.Slice.lower); |
| 5306 | } |
| 5307 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5308 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5309 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | if (s->v.Slice.upper) { |
| 5312 | VISIT(c, expr, s->v.Slice.upper); |
| 5313 | } |
| 5314 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5315 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5316 | } |
| 5317 | |
| 5318 | if (s->v.Slice.step) { |
| 5319 | n++; |
| 5320 | VISIT(c, expr, s->v.Slice.step); |
| 5321 | } |
| 5322 | ADDOP_I(c, BUILD_SLICE, n); |
| 5323 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5324 | } |
| 5325 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5326 | /* End of the compiler section, beginning of the assembler section */ |
| 5327 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5328 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5329 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5330 | |
| 5331 | XXX must handle implicit jumps from one block to next |
| 5332 | */ |
| 5333 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5334 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5336 | int a_offset; /* offset into bytecode */ |
| 5337 | int a_nblocks; /* number of reachable blocks */ |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5338 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5340 | int a_lnotab_off; /* offset into lnotab */ |
| 5341 | int a_lineno; /* last lineno of emitted instruction */ |
| 5342 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5343 | }; |
| 5344 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5345 | static void |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5346 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5347 | { |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5348 | int i, j; |
| 5349 | |
| 5350 | /* Get rid of recursion for normal control flow. |
| 5351 | Since the number of blocks is limited, unused space in a_postorder |
| 5352 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5353 | blocks. */ |
| 5354 | for (j = end; b && !b->b_seen; b = b->b_next) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5355 | b->b_seen = 1; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5356 | assert(a->a_nblocks < j); |
| 5357 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5358 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5359 | while (j < end) { |
| 5360 | b = a->a_postorder[j++]; |
| 5361 | for (i = 0; i < b->b_iused; i++) { |
| 5362 | struct instr *instr = &b->b_instr[i]; |
| 5363 | if (instr->i_jrel || instr->i_jabs) |
| 5364 | dfs(c, instr->i_target, a, j); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5365 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5366 | assert(a->a_nblocks < j); |
| 5367 | a->a_postorder[a->a_nblocks++] = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5368 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5371 | Py_LOCAL_INLINE(void) |
| 5372 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5373 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5374 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5375 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5376 | assert(b->b_startdepth < 0); |
| 5377 | b->b_startdepth = depth; |
| 5378 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5379 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
| 5382 | /* Find the flow path that needs the largest stack. We assume that |
| 5383 | * cycles in the flow graph have no net effect on the stack depth. |
| 5384 | */ |
| 5385 | static int |
| 5386 | stackdepth(struct compiler *c) |
| 5387 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5388 | basicblock *b, *entryblock = NULL; |
| 5389 | basicblock **stack, **sp; |
| 5390 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5391 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | b->b_startdepth = INT_MIN; |
| 5393 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5394 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5395 | } |
| 5396 | if (!entryblock) |
| 5397 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5398 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5399 | if (!stack) { |
| 5400 | PyErr_NoMemory(); |
| 5401 | return -1; |
| 5402 | } |
| 5403 | |
| 5404 | sp = stack; |
| 5405 | stackdepth_push(&sp, entryblock, 0); |
| 5406 | while (sp != stack) { |
| 5407 | b = *--sp; |
| 5408 | int depth = b->b_startdepth; |
| 5409 | assert(depth >= 0); |
| 5410 | basicblock *next = b->b_next; |
| 5411 | for (int i = 0; i < b->b_iused; i++) { |
| 5412 | struct instr *instr = &b->b_instr[i]; |
| 5413 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5414 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5415 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5416 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5417 | } |
| 5418 | int new_depth = depth + effect; |
| 5419 | if (new_depth > maxdepth) { |
| 5420 | maxdepth = new_depth; |
| 5421 | } |
| 5422 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5423 | if (instr->i_jrel || instr->i_jabs) { |
| 5424 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5425 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5426 | int target_depth = depth + effect; |
| 5427 | if (target_depth > maxdepth) { |
| 5428 | maxdepth = target_depth; |
| 5429 | } |
| 5430 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5431 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5432 | } |
| 5433 | depth = new_depth; |
| 5434 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5435 | instr->i_opcode == JUMP_FORWARD || |
| 5436 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5437 | instr->i_opcode == RAISE_VARARGS || |
| 5438 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5439 | { |
| 5440 | /* remaining code is dead */ |
| 5441 | next = NULL; |
| 5442 | break; |
| 5443 | } |
| 5444 | } |
| 5445 | if (next != NULL) { |
| 5446 | stackdepth_push(&sp, next, depth); |
| 5447 | } |
| 5448 | } |
| 5449 | PyObject_Free(stack); |
| 5450 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
| 5453 | static int |
| 5454 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5455 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5456 | memset(a, 0, sizeof(struct assembler)); |
| 5457 | a->a_lineno = firstlineno; |
| 5458 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5459 | if (!a->a_bytecode) |
| 5460 | return 0; |
| 5461 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5462 | if (!a->a_lnotab) |
| 5463 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5464 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | PyErr_NoMemory(); |
| 5466 | return 0; |
| 5467 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5468 | a->a_postorder = (basicblock **)PyObject_Malloc( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | sizeof(basicblock *) * nblocks); |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5470 | if (!a->a_postorder) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5471 | PyErr_NoMemory(); |
| 5472 | return 0; |
| 5473 | } |
| 5474 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | static void |
| 5478 | assemble_free(struct assembler *a) |
| 5479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5480 | Py_XDECREF(a->a_bytecode); |
| 5481 | Py_XDECREF(a->a_lnotab); |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5482 | if (a->a_postorder) |
| 5483 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5484 | } |
| 5485 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5486 | static int |
| 5487 | blocksize(basicblock *b) |
| 5488 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5489 | int i; |
| 5490 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5491 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5493 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5494 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5497 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5498 | the instruction's bytecode offset and line number. See |
| 5499 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5500 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5501 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5502 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5503 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5504 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5505 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5506 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5508 | d_lineno = i->i_lineno - a->a_lineno; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5509 | if (d_lineno == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5510 | return 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
| 5514 | assert(d_bytecode >= 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5516 | if (d_bytecode > 255) { |
| 5517 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5518 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5519 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5520 | if (nbytes >= len) { |
| 5521 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5522 | len = nbytes; |
| 5523 | else if (len <= INT_MAX / 2) |
| 5524 | len *= 2; |
| 5525 | else { |
| 5526 | PyErr_NoMemory(); |
| 5527 | return 0; |
| 5528 | } |
| 5529 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5530 | return 0; |
| 5531 | } |
| 5532 | lnotab = (unsigned char *) |
| 5533 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5534 | for (j = 0; j < ncodes; j++) { |
| 5535 | *lnotab++ = 255; |
| 5536 | *lnotab++ = 0; |
| 5537 | } |
| 5538 | d_bytecode -= ncodes * 255; |
| 5539 | a->a_lnotab_off += ncodes * 2; |
| 5540 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5541 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5542 | |
| 5543 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5544 | int j, nbytes, ncodes, k; |
| 5545 | if (d_lineno < 0) { |
| 5546 | k = -128; |
| 5547 | /* use division on positive numbers */ |
| 5548 | ncodes = (-d_lineno) / 128; |
| 5549 | } |
| 5550 | else { |
| 5551 | k = 127; |
| 5552 | ncodes = d_lineno / 127; |
| 5553 | } |
| 5554 | d_lineno -= ncodes * k; |
| 5555 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5557 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5558 | if (nbytes >= len) { |
| 5559 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5560 | len = nbytes; |
| 5561 | else if (len <= INT_MAX / 2) |
| 5562 | len *= 2; |
| 5563 | else { |
| 5564 | PyErr_NoMemory(); |
| 5565 | return 0; |
| 5566 | } |
| 5567 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5568 | return 0; |
| 5569 | } |
| 5570 | lnotab = (unsigned char *) |
| 5571 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5572 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5573 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | d_bytecode = 0; |
| 5575 | for (j = 1; j < ncodes; j++) { |
| 5576 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5577 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5579 | a->a_lnotab_off += ncodes * 2; |
| 5580 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5581 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5583 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5584 | if (a->a_lnotab_off + 2 >= len) { |
| 5585 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5586 | return 0; |
| 5587 | } |
| 5588 | lnotab = (unsigned char *) |
| 5589 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5591 | a->a_lnotab_off += 2; |
| 5592 | if (d_bytecode) { |
| 5593 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5594 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5595 | } |
| 5596 | else { /* First line of a block; def stmt, etc. */ |
| 5597 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5598 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | } |
| 5600 | a->a_lineno = i->i_lineno; |
| 5601 | a->a_lineno_off = a->a_offset; |
| 5602 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5603 | } |
| 5604 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5605 | /* assemble_emit() |
| 5606 | Extend the bytecode with a new instruction. |
| 5607 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5608 | */ |
| 5609 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5610 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5611 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5612 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5613 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5614 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5615 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5616 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5617 | arg = i->i_oparg; |
| 5618 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5619 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5620 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5621 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5622 | if (len > PY_SSIZE_T_MAX / 2) |
| 5623 | return 0; |
| 5624 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5625 | return 0; |
| 5626 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5627 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5628 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5629 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5633 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5634 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5635 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5636 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5637 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5638 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5640 | /* Compute the size of each block and fixup jump args. |
| 5641 | Replace block pointer with position in bytecode. */ |
| 5642 | do { |
| 5643 | totsize = 0; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5644 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5645 | b = a->a_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5646 | bsize = blocksize(b); |
| 5647 | b->b_offset = totsize; |
| 5648 | totsize += bsize; |
| 5649 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5650 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5652 | bsize = b->b_offset; |
| 5653 | for (i = 0; i < b->b_iused; i++) { |
| 5654 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5655 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5656 | /* Relative jumps are computed relative to |
| 5657 | the instruction pointer after fetching |
| 5658 | the jump instruction. |
| 5659 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5660 | bsize += isize; |
| 5661 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5662 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5663 | if (instr->i_jrel) { |
| 5664 | instr->i_oparg -= bsize; |
| 5665 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5666 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5667 | if (instrsize(instr->i_oparg) != isize) { |
| 5668 | extended_arg_recompile = 1; |
| 5669 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5670 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5671 | } |
| 5672 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | /* XXX: This is an awful hack that could hurt performance, but |
| 5675 | on the bright side it should work until we come up |
| 5676 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | The issue is that in the first loop blocksize() is called |
| 5679 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5680 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5684 | The only EXTENDED_ARGs that could be popping up are |
| 5685 | ones in jump instructions. So this should converge |
| 5686 | fairly quickly. |
| 5687 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5688 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5691 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5692 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5695 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5697 | tuple = PyTuple_New(size); |
| 5698 | if (tuple == NULL) |
| 5699 | return NULL; |
| 5700 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5701 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5702 | Py_INCREF(k); |
| 5703 | assert((i - offset) < size); |
| 5704 | assert((i - offset) >= 0); |
| 5705 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5706 | } |
| 5707 | return tuple; |
| 5708 | } |
| 5709 | |
| 5710 | static PyObject * |
| 5711 | consts_dict_keys_inorder(PyObject *dict) |
| 5712 | { |
| 5713 | PyObject *consts, *k, *v; |
| 5714 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5715 | |
| 5716 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5717 | if (consts == NULL) |
| 5718 | return NULL; |
| 5719 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5720 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5721 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5722 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5723 | * the object we want is always second. */ |
| 5724 | if (PyTuple_CheckExact(k)) { |
| 5725 | k = PyTuple_GET_ITEM(k, 1); |
| 5726 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5727 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5728 | assert(i < size); |
| 5729 | assert(i >= 0); |
| 5730 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5731 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5732 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5733 | } |
| 5734 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5735 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5736 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5738 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5739 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5740 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5741 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5742 | if (ste->ste_nested) |
| 5743 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5744 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5745 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5746 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5747 | flags |= CO_COROUTINE; |
| 5748 | if (ste->ste_generator && ste->ste_coroutine) |
| 5749 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5750 | if (ste->ste_varargs) |
| 5751 | flags |= CO_VARARGS; |
| 5752 | if (ste->ste_varkeywords) |
| 5753 | flags |= CO_VARKEYWORDS; |
| 5754 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5756 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5757 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5758 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5759 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5760 | ste->ste_coroutine && |
| 5761 | !ste->ste_generator) { |
| 5762 | flags |= CO_COROUTINE; |
| 5763 | } |
| 5764 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5765 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5766 | } |
| 5767 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5768 | // Merge *tuple* with constant cache. |
| 5769 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5770 | static int |
| 5771 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5772 | { |
| 5773 | assert(PyTuple_CheckExact(*tuple)); |
| 5774 | |
| 5775 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5776 | if (key == NULL) { |
| 5777 | return 0; |
| 5778 | } |
| 5779 | |
| 5780 | // t is borrowed reference |
| 5781 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5782 | Py_DECREF(key); |
| 5783 | if (t == NULL) { |
| 5784 | return 0; |
| 5785 | } |
| 5786 | if (t == key) { // tuple is new constant. |
| 5787 | return 1; |
| 5788 | } |
| 5789 | |
| 5790 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5791 | Py_INCREF(u); |
| 5792 | Py_DECREF(*tuple); |
| 5793 | *tuple = u; |
| 5794 | return 1; |
| 5795 | } |
| 5796 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5797 | static PyCodeObject * |
| 5798 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5800 | PyObject *tmp; |
| 5801 | PyCodeObject *co = NULL; |
| 5802 | PyObject *consts = NULL; |
| 5803 | PyObject *names = NULL; |
| 5804 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5805 | PyObject *name = NULL; |
| 5806 | PyObject *freevars = NULL; |
| 5807 | PyObject *cellvars = NULL; |
| 5808 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5809 | Py_ssize_t nlocals; |
| 5810 | int nlocals_int; |
| 5811 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5812 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5813 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5814 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5815 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5816 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5817 | if (!consts || !names || !varnames) |
| 5818 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5821 | if (!cellvars) |
| 5822 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5823 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | if (!freevars) |
| 5825 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5826 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5827 | if (!merge_const_tuple(c, &names) || |
| 5828 | !merge_const_tuple(c, &varnames) || |
| 5829 | !merge_const_tuple(c, &cellvars) || |
| 5830 | !merge_const_tuple(c, &freevars)) |
| 5831 | { |
| 5832 | goto error; |
| 5833 | } |
| 5834 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5835 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5836 | assert(nlocals < INT_MAX); |
| 5837 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5839 | flags = compute_code_flags(c); |
| 5840 | if (flags < 0) |
| 5841 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5844 | if (!bytecode) |
| 5845 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5846 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5847 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5848 | if (!tmp) |
| 5849 | goto error; |
| 5850 | Py_DECREF(consts); |
| 5851 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5852 | if (!merge_const_tuple(c, &consts)) { |
| 5853 | goto error; |
| 5854 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5855 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5856 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5857 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5858 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5859 | maxdepth = stackdepth(c); |
| 5860 | if (maxdepth < 0) { |
| 5861 | goto error; |
| 5862 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5863 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5864 | posonlyargcount, kwonlyargcount, nlocals_int, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5865 | maxdepth, flags, bytecode, consts, names, |
| 5866 | varnames, freevars, cellvars, c->c_filename, |
| 5867 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5868 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | Py_XDECREF(consts); |
| 5870 | Py_XDECREF(names); |
| 5871 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5872 | Py_XDECREF(name); |
| 5873 | Py_XDECREF(freevars); |
| 5874 | Py_XDECREF(cellvars); |
| 5875 | Py_XDECREF(bytecode); |
| 5876 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5877 | } |
| 5878 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5879 | |
| 5880 | /* For debugging purposes only */ |
| 5881 | #if 0 |
| 5882 | static void |
| 5883 | dump_instr(const struct instr *i) |
| 5884 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5886 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5887 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5889 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5890 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5891 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5892 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5893 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5894 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5895 | } |
| 5896 | |
| 5897 | static void |
| 5898 | dump_basicblock(const basicblock *b) |
| 5899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5900 | const char *seen = b->b_seen ? "seen " : ""; |
| 5901 | const char *b_return = b->b_return ? "return " : ""; |
| 5902 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5903 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5904 | if (b->b_instr) { |
| 5905 | int i; |
| 5906 | for (i = 0; i < b->b_iused; i++) { |
| 5907 | fprintf(stderr, " [%02d] ", i); |
| 5908 | dump_instr(b->b_instr + i); |
| 5909 | } |
| 5910 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5911 | } |
| 5912 | #endif |
| 5913 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5914 | static PyCodeObject * |
| 5915 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5917 | basicblock *b, *entryblock; |
| 5918 | struct assembler a; |
| 5919 | int i, j, nblocks; |
| 5920 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5922 | /* Make sure every block that falls off the end returns None. |
| 5923 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5924 | block ends with a jump or return b_next shouldn't set. |
| 5925 | */ |
| 5926 | if (!c->u->u_curblock->b_return) { |
| 5927 | NEXT_BLOCK(c); |
| 5928 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5929 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5930 | ADDOP(c, RETURN_VALUE); |
| 5931 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5933 | nblocks = 0; |
| 5934 | entryblock = NULL; |
| 5935 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5936 | nblocks++; |
| 5937 | entryblock = b; |
| 5938 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5940 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5941 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5942 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5943 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 5944 | else |
| 5945 | c->u->u_firstlineno = 1; |
| 5946 | } |
| 5947 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5948 | goto error; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5949 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5951 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5952 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5953 | |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5954 | /* Emit code in reverse postorder from dfs. */ |
| 5955 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 5956 | b = a.a_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | for (j = 0; j < b->b_iused; j++) |
| 5958 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5959 | goto error; |
| 5960 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5962 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5963 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5964 | 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] | 5965 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5966 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5968 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5969 | assemble_free(&a); |
| 5970 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5971 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5972 | |
| 5973 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5974 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5975 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 5976 | PyArena *arena) |
| 5977 | { |
| 5978 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 5979 | } |