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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 26 | #include "Python-ast.h" |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 27 | #include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */ |
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 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 44 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | unsigned i_jabs : 1; |
| 46 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | unsigned char i_opcode; |
| 48 | int i_oparg; |
| 49 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 50 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 53 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 55 | reverse order that the block are allocated. b_list points to the next |
| 56 | 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] | 57 | struct basicblock_ *b_list; |
| 58 | /* number of instructions used */ |
| 59 | int b_iused; |
| 60 | /* length of instruction array (b_instr) */ |
| 61 | int b_ialloc; |
| 62 | /* pointer to an array of instructions, initially NULL */ |
| 63 | struct instr *b_instr; |
| 64 | /* If b_next is non-NULL, it is a pointer to the next |
| 65 | block reached by normal control flow. */ |
| 66 | struct basicblock_ *b_next; |
| 67 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 68 | unsigned b_seen : 1; |
| 69 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 70 | unsigned b_return : 1; |
| 71 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 72 | int b_startdepth; |
| 73 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 74 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 75 | } basicblock; |
| 76 | |
| 77 | /* fblockinfo tracks the current frame block. |
| 78 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 79 | A frame block is used to handle loops, try/except, and try/finally. |
| 80 | It's called a frame block to distinguish it from a basic block in the |
| 81 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 84 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END, |
| 85 | WITH, ASYNC_WITH, HANDLER_CLEANUP }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 86 | |
| 87 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | enum fblocktype fb_type; |
| 89 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 90 | /* (optional) type-specific exit or cleanup block */ |
| 91 | basicblock *fb_exit; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 94 | enum { |
| 95 | COMPILER_SCOPE_MODULE, |
| 96 | COMPILER_SCOPE_CLASS, |
| 97 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 98 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 99 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 100 | COMPILER_SCOPE_COMPREHENSION, |
| 101 | }; |
| 102 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 103 | /* The following items change on entry and exit of code blocks. |
| 104 | They must be saved and restored when returning to a block. |
| 105 | */ |
| 106 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 110 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 111 | int u_scope_type; |
| 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | /* The following fields are dicts that map objects to |
| 114 | the index of them in co_XXX. The index is used as |
| 115 | the argument for opcodes that refer to those collections. |
| 116 | */ |
| 117 | PyObject *u_consts; /* all constants */ |
| 118 | PyObject *u_names; /* all names */ |
| 119 | PyObject *u_varnames; /* local variables */ |
| 120 | PyObject *u_cellvars; /* cell variables */ |
| 121 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 124 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 125 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 126 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 127 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | /* Pointer to the most recently allocated block. By following b_list |
| 129 | members, you can reach all early allocated blocks. */ |
| 130 | basicblock *u_blocks; |
| 131 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | int u_nfblocks; |
| 134 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | int u_firstlineno; /* the first lineno of the block */ |
| 137 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 138 | int u_col_offset; /* the offset of the current stmt */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | int u_lineno_set; /* boolean to indicate whether instr |
| 140 | has been generated with current lineno */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 144 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 145 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | 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] | 147 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 148 | |
| 149 | Note that we don't track recursion levels during compilation - the |
| 150 | task of detecting and rejecting excessive levels of nesting is |
| 151 | handled by the symbol analysis pass. |
| 152 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | */ |
| 154 | |
| 155 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 156 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | struct symtable *c_st; |
| 158 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 159 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 160 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 161 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | int c_interactive; /* true if in interactive mode */ |
| 163 | int c_nestlevel; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 165 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 166 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | struct compiler_unit *u; /* compiler state for current block */ |
| 168 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 169 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 172 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 173 | static void compiler_free(struct compiler *); |
| 174 | static basicblock *compiler_new_block(struct compiler *); |
| 175 | static int compiler_next_instr(struct compiler *, basicblock *); |
| 176 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 177 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 178 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 179 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 180 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 181 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 182 | |
| 183 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 184 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 185 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 186 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 187 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 188 | static int compiler_annassign(struct compiler *, stmt_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 189 | static int compiler_visit_slice(struct compiler *, slice_ty, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | expr_context_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 191 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 192 | static int inplace_binop(struct compiler *, operator_ty); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 193 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 194 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 195 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 196 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 197 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 198 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 200 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 201 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 202 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 203 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 204 | static int compiler_sync_comprehension_generator( |
| 205 | struct compiler *c, |
| 206 | asdl_seq *generators, int gen_index, |
| 207 | expr_ty elt, expr_ty val, int type); |
| 208 | |
| 209 | static int compiler_async_comprehension_generator( |
| 210 | struct compiler *c, |
| 211 | asdl_seq *generators, int gen_index, |
| 212 | expr_ty elt, expr_ty val, int type); |
| 213 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 215 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 216 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 217 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 218 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 219 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 220 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | /* Name mangling: __private becomes _classname__private. |
| 223 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 224 | PyObject *result; |
| 225 | size_t nlen, plen, ipriv; |
| 226 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 228 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 229 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | Py_INCREF(ident); |
| 231 | return ident; |
| 232 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 233 | nlen = PyUnicode_GET_LENGTH(ident); |
| 234 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | The only time a name with a dot can occur is when |
| 238 | we are compiling an import statement that has a |
| 239 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | TODO(jhylton): Decide whether we want to support |
| 242 | mangling of the module name, e.g. __M.X. |
| 243 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 244 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 245 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 246 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | Py_INCREF(ident); |
| 248 | return ident; /* Don't mangle __whatever__ */ |
| 249 | } |
| 250 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 251 | ipriv = 0; |
| 252 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 253 | ipriv++; |
| 254 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | Py_INCREF(ident); |
| 256 | return ident; /* Don't mangle if class is just underscores */ |
| 257 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 258 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 259 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 260 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 261 | PyErr_SetString(PyExc_OverflowError, |
| 262 | "private identifier too large to be mangled"); |
| 263 | return NULL; |
| 264 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 265 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 266 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 267 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 268 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 269 | |
| 270 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 271 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 273 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 274 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 275 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 276 | Py_DECREF(result); |
| 277 | return NULL; |
| 278 | } |
| 279 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 280 | Py_DECREF(result); |
| 281 | return NULL; |
| 282 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 283 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 284 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 287 | static int |
| 288 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 289 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 291 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 292 | c->c_const_cache = PyDict_New(); |
| 293 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | c->c_stack = PyList_New(0); |
| 298 | if (!c->c_stack) { |
| 299 | Py_CLEAR(c->c_const_cache); |
| 300 | return 0; |
| 301 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 307 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 308 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | struct compiler c; |
| 311 | PyCodeObject *co = NULL; |
| 312 | PyCompilerFlags local_flags; |
| 313 | int merged; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 314 | _PyCoreConfig *config = &_PyInterpreterState_GET_UNSAFE()->core_config; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 316 | if (!__doc__) { |
| 317 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 318 | if (!__doc__) |
| 319 | return NULL; |
| 320 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 321 | if (!__annotations__) { |
| 322 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 323 | if (!__annotations__) |
| 324 | return NULL; |
| 325 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (!compiler_init(&c)) |
| 327 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 328 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | c.c_filename = filename; |
| 330 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 331 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | if (c.c_future == NULL) |
| 333 | goto finally; |
| 334 | if (!flags) { |
| 335 | local_flags.cf_flags = 0; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 336 | local_flags.cf_feature_version = PY_MINOR_VERSION; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | flags = &local_flags; |
| 338 | } |
| 339 | merged = c.c_future->ff_features | flags->cf_flags; |
| 340 | c.c_future->ff_features = merged; |
| 341 | flags->cf_flags = merged; |
| 342 | c.c_flags = flags; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 343 | c.c_optimize = (optimize == -1) ? config->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 345 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 346 | if (!_PyAST_Optimize(mod, arena, c.c_optimize)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 347 | goto finally; |
| 348 | } |
| 349 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 350 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | if (c.c_st == NULL) { |
| 352 | if (!PyErr_Occurred()) |
| 353 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 354 | goto finally; |
| 355 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 358 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 359 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | compiler_free(&c); |
| 361 | assert(co || PyErr_Occurred()); |
| 362 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 366 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 367 | int optimize, PyArena *arena) |
| 368 | { |
| 369 | PyObject *filename; |
| 370 | PyCodeObject *co; |
| 371 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 372 | if (filename == NULL) |
| 373 | return NULL; |
| 374 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 375 | Py_DECREF(filename); |
| 376 | return co; |
| 377 | |
| 378 | } |
| 379 | |
| 380 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 381 | PyNode_Compile(struct _node *n, const char *filename) |
| 382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | PyCodeObject *co = NULL; |
| 384 | mod_ty mod; |
| 385 | PyArena *arena = PyArena_New(); |
| 386 | if (!arena) |
| 387 | return NULL; |
| 388 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 389 | if (mod) |
| 390 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 391 | PyArena_Free(arena); |
| 392 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 395 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 396 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | if (c->c_st) |
| 399 | PySymtable_Free(c->c_st); |
| 400 | if (c->c_future) |
| 401 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 402 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 403 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 407 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 408 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | Py_ssize_t i, n; |
| 411 | PyObject *v, *k; |
| 412 | PyObject *dict = PyDict_New(); |
| 413 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 414 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | n = PyList_Size(list); |
| 416 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 417 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | if (!v) { |
| 419 | Py_DECREF(dict); |
| 420 | return NULL; |
| 421 | } |
| 422 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 423 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | Py_DECREF(v); |
| 425 | Py_DECREF(dict); |
| 426 | return NULL; |
| 427 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | Py_DECREF(v); |
| 429 | } |
| 430 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | /* Return new dict containing names from src that match scope(s). |
| 434 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 435 | 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] | 436 | 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] | 437 | values are integers, starting at offset and increasing by one for |
| 438 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 439 | */ |
| 440 | |
| 441 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 442 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 443 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 444 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 446 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | assert(offset >= 0); |
| 449 | if (dest == NULL) |
| 450 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 451 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 452 | /* Sort the keys so that we have a deterministic order on the indexes |
| 453 | saved in the returned dictionary. These indexes are used as indexes |
| 454 | into the free and cell var storage. Therefore if they aren't |
| 455 | deterministic, then the generated bytecode is not deterministic. |
| 456 | */ |
| 457 | sorted_keys = PyDict_Keys(src); |
| 458 | if (sorted_keys == NULL) |
| 459 | return NULL; |
| 460 | if (PyList_Sort(sorted_keys) != 0) { |
| 461 | Py_DECREF(sorted_keys); |
| 462 | return NULL; |
| 463 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 464 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 465 | |
| 466 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 467 | /* XXX this should probably be a macro in symtable.h */ |
| 468 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 469 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 470 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | assert(PyLong_Check(v)); |
| 472 | vi = PyLong_AS_LONG(v); |
| 473 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 476 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 478 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | Py_DECREF(dest); |
| 480 | return NULL; |
| 481 | } |
| 482 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 483 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 484 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | Py_DECREF(item); |
| 486 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | return NULL; |
| 488 | } |
| 489 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 492 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 494 | } |
| 495 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 496 | static void |
| 497 | compiler_unit_check(struct compiler_unit *u) |
| 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | basicblock *block; |
| 500 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 501 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 502 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 503 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 504 | if (block->b_instr != NULL) { |
| 505 | assert(block->b_ialloc > 0); |
| 506 | assert(block->b_iused > 0); |
| 507 | assert(block->b_ialloc >= block->b_iused); |
| 508 | } |
| 509 | else { |
| 510 | assert (block->b_iused == 0); |
| 511 | assert (block->b_ialloc == 0); |
| 512 | } |
| 513 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | static void |
| 517 | compiler_unit_free(struct compiler_unit *u) |
| 518 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | compiler_unit_check(u); |
| 522 | b = u->u_blocks; |
| 523 | while (b != NULL) { |
| 524 | if (b->b_instr) |
| 525 | PyObject_Free((void *)b->b_instr); |
| 526 | next = b->b_list; |
| 527 | PyObject_Free((void *)b); |
| 528 | b = next; |
| 529 | } |
| 530 | Py_CLEAR(u->u_ste); |
| 531 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 532 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | Py_CLEAR(u->u_consts); |
| 534 | Py_CLEAR(u->u_names); |
| 535 | Py_CLEAR(u->u_varnames); |
| 536 | Py_CLEAR(u->u_freevars); |
| 537 | Py_CLEAR(u->u_cellvars); |
| 538 | Py_CLEAR(u->u_private); |
| 539 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 543 | compiler_enter_scope(struct compiler *c, identifier name, |
| 544 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 545 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 547 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 549 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 550 | struct compiler_unit)); |
| 551 | if (!u) { |
| 552 | PyErr_NoMemory(); |
| 553 | return 0; |
| 554 | } |
| 555 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 556 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 558 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | u->u_kwonlyargcount = 0; |
| 560 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 561 | if (!u->u_ste) { |
| 562 | compiler_unit_free(u); |
| 563 | return 0; |
| 564 | } |
| 565 | Py_INCREF(name); |
| 566 | u->u_name = name; |
| 567 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 568 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 569 | if (!u->u_varnames || !u->u_cellvars) { |
| 570 | compiler_unit_free(u); |
| 571 | return 0; |
| 572 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 573 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 574 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 575 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 576 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 577 | int res; |
| 578 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 579 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 580 | name = _PyUnicode_FromId(&PyId___class__); |
| 581 | if (!name) { |
| 582 | compiler_unit_free(u); |
| 583 | return 0; |
| 584 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 585 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 586 | if (res < 0) { |
| 587 | compiler_unit_free(u); |
| 588 | return 0; |
| 589 | } |
| 590 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | 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] | 593 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 594 | if (!u->u_freevars) { |
| 595 | compiler_unit_free(u); |
| 596 | return 0; |
| 597 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | u->u_blocks = NULL; |
| 600 | u->u_nfblocks = 0; |
| 601 | u->u_firstlineno = lineno; |
| 602 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 603 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | u->u_lineno_set = 0; |
| 605 | u->u_consts = PyDict_New(); |
| 606 | if (!u->u_consts) { |
| 607 | compiler_unit_free(u); |
| 608 | return 0; |
| 609 | } |
| 610 | u->u_names = PyDict_New(); |
| 611 | if (!u->u_names) { |
| 612 | compiler_unit_free(u); |
| 613 | return 0; |
| 614 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | /* Push the old compiler_unit on the stack. */ |
| 619 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 620 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 622 | Py_XDECREF(capsule); |
| 623 | compiler_unit_free(u); |
| 624 | return 0; |
| 625 | } |
| 626 | Py_DECREF(capsule); |
| 627 | u->u_private = c->u->u_private; |
| 628 | Py_XINCREF(u->u_private); |
| 629 | } |
| 630 | c->u = u; |
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 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 633 | |
| 634 | block = compiler_new_block(c); |
| 635 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 637 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 638 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 639 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 640 | if (!compiler_set_qualname(c)) |
| 641 | return 0; |
| 642 | } |
| 643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 644 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 647 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 648 | compiler_exit_scope(struct compiler *c) |
| 649 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 650 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | c->c_nestlevel--; |
| 654 | compiler_unit_free(c->u); |
| 655 | /* Restore c->u to the parent unit. */ |
| 656 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 657 | if (n >= 0) { |
| 658 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 659 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | assert(c->u); |
| 661 | /* we are deleting from a list so this really shouldn't fail */ |
| 662 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 663 | Py_FatalError("compiler_exit_scope()"); |
| 664 | compiler_unit_check(c->u); |
| 665 | } |
| 666 | else |
| 667 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 668 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 671 | static int |
| 672 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 673 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 674 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 675 | _Py_static_string(dot_locals, ".<locals>"); |
| 676 | Py_ssize_t stack_size; |
| 677 | struct compiler_unit *u = c->u; |
| 678 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 679 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 680 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 681 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 682 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 683 | if (stack_size > 1) { |
| 684 | int scope, force_global = 0; |
| 685 | struct compiler_unit *parent; |
| 686 | PyObject *mangled, *capsule; |
| 687 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 688 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 689 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 690 | assert(parent); |
| 691 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 692 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 693 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 694 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 695 | assert(u->u_name); |
| 696 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 697 | if (!mangled) |
| 698 | return 0; |
| 699 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 700 | Py_DECREF(mangled); |
| 701 | assert(scope != GLOBAL_IMPLICIT); |
| 702 | if (scope == GLOBAL_EXPLICIT) |
| 703 | force_global = 1; |
| 704 | } |
| 705 | |
| 706 | if (!force_global) { |
| 707 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 708 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 709 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 710 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 711 | if (dot_locals_str == NULL) |
| 712 | return 0; |
| 713 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 714 | if (base == NULL) |
| 715 | return 0; |
| 716 | } |
| 717 | else { |
| 718 | Py_INCREF(parent->u_qualname); |
| 719 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 720 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 721 | } |
| 722 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 723 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 724 | if (base != NULL) { |
| 725 | dot_str = _PyUnicode_FromId(&dot); |
| 726 | if (dot_str == NULL) { |
| 727 | Py_DECREF(base); |
| 728 | return 0; |
| 729 | } |
| 730 | name = PyUnicode_Concat(base, dot_str); |
| 731 | Py_DECREF(base); |
| 732 | if (name == NULL) |
| 733 | return 0; |
| 734 | PyUnicode_Append(&name, u->u_name); |
| 735 | if (name == NULL) |
| 736 | return 0; |
| 737 | } |
| 738 | else { |
| 739 | Py_INCREF(u->u_name); |
| 740 | name = u->u_name; |
| 741 | } |
| 742 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 743 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 744 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 745 | } |
| 746 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 747 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 748 | /* Allocate a new block and return a pointer to it. |
| 749 | Returns NULL on error. |
| 750 | */ |
| 751 | |
| 752 | static basicblock * |
| 753 | compiler_new_block(struct compiler *c) |
| 754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 755 | basicblock *b; |
| 756 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 757 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | u = c->u; |
| 759 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 760 | if (b == NULL) { |
| 761 | PyErr_NoMemory(); |
| 762 | return NULL; |
| 763 | } |
| 764 | memset((void *)b, 0, sizeof(basicblock)); |
| 765 | /* Extend the singly linked list of blocks with new block. */ |
| 766 | b->b_list = u->u_blocks; |
| 767 | u->u_blocks = b; |
| 768 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 771 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 772 | compiler_next_block(struct compiler *c) |
| 773 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 774 | basicblock *block = compiler_new_block(c); |
| 775 | if (block == NULL) |
| 776 | return NULL; |
| 777 | c->u->u_curblock->b_next = block; |
| 778 | c->u->u_curblock = block; |
| 779 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | static basicblock * |
| 783 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 785 | assert(block != NULL); |
| 786 | c->u->u_curblock->b_next = block; |
| 787 | c->u->u_curblock = block; |
| 788 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | /* Returns the offset of the next instruction in the current block's |
| 792 | b_instr array. Resizes the b_instr as necessary. |
| 793 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 794 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 795 | |
| 796 | static int |
| 797 | compiler_next_instr(struct compiler *c, basicblock *b) |
| 798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | assert(b != NULL); |
| 800 | if (b->b_instr == NULL) { |
| 801 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 802 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 803 | if (b->b_instr == NULL) { |
| 804 | PyErr_NoMemory(); |
| 805 | return -1; |
| 806 | } |
| 807 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 808 | memset((char *)b->b_instr, 0, |
| 809 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 810 | } |
| 811 | else if (b->b_iused == b->b_ialloc) { |
| 812 | struct instr *tmp; |
| 813 | size_t oldsize, newsize; |
| 814 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 815 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 816 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 817 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | PyErr_NoMemory(); |
| 819 | return -1; |
| 820 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 821 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | if (newsize == 0) { |
| 823 | PyErr_NoMemory(); |
| 824 | return -1; |
| 825 | } |
| 826 | b->b_ialloc <<= 1; |
| 827 | tmp = (struct instr *)PyObject_Realloc( |
| 828 | (void *)b->b_instr, newsize); |
| 829 | if (tmp == NULL) { |
| 830 | PyErr_NoMemory(); |
| 831 | return -1; |
| 832 | } |
| 833 | b->b_instr = tmp; |
| 834 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 835 | } |
| 836 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 839 | /* Set the i_lineno member of the instruction at offset off if the |
| 840 | line number for the current expression/statement has not |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 841 | already been set. If it has been set, the call has no effect. |
| 842 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 843 | The line number is reset in the following cases: |
| 844 | - when entering a new scope |
| 845 | - on each statement |
| 846 | - on each expression that start a new line |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 847 | - before the "except" and "finally" clauses |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 848 | - before the "for" and "while" expressions |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 849 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 850 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 851 | static void |
| 852 | compiler_set_lineno(struct compiler *c, int off) |
| 853 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 854 | basicblock *b; |
| 855 | if (c->u->u_lineno_set) |
| 856 | return; |
| 857 | c->u->u_lineno_set = 1; |
| 858 | b = c->u->u_curblock; |
| 859 | b->b_instr[off].i_lineno = c->u->u_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 862 | /* Return the stack effect of opcode with argument oparg. |
| 863 | |
| 864 | Some opcodes have different stack effect when jump to the target and |
| 865 | when not jump. The 'jump' parameter specifies the case: |
| 866 | |
| 867 | * 0 -- when not jump |
| 868 | * 1 -- when jump |
| 869 | * -1 -- maximal |
| 870 | */ |
| 871 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 872 | WITH_CLEANUP_FINISH deterministic. */ |
| 873 | static int |
| 874 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 877 | case NOP: |
| 878 | case EXTENDED_ARG: |
| 879 | return 0; |
| 880 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 881 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | case POP_TOP: |
| 883 | return -1; |
| 884 | case ROT_TWO: |
| 885 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 886 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | return 0; |
| 888 | case DUP_TOP: |
| 889 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 890 | case DUP_TOP_TWO: |
| 891 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 892 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 893 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | case UNARY_POSITIVE: |
| 895 | case UNARY_NEGATIVE: |
| 896 | case UNARY_NOT: |
| 897 | case UNARY_INVERT: |
| 898 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | case SET_ADD: |
| 901 | case LIST_APPEND: |
| 902 | return -1; |
| 903 | case MAP_ADD: |
| 904 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 905 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 906 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | case BINARY_POWER: |
| 908 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 909 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | case BINARY_MODULO: |
| 911 | case BINARY_ADD: |
| 912 | case BINARY_SUBTRACT: |
| 913 | case BINARY_SUBSCR: |
| 914 | case BINARY_FLOOR_DIVIDE: |
| 915 | case BINARY_TRUE_DIVIDE: |
| 916 | return -1; |
| 917 | case INPLACE_FLOOR_DIVIDE: |
| 918 | case INPLACE_TRUE_DIVIDE: |
| 919 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | case INPLACE_ADD: |
| 922 | case INPLACE_SUBTRACT: |
| 923 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 924 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | case INPLACE_MODULO: |
| 926 | return -1; |
| 927 | case STORE_SUBSCR: |
| 928 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | case DELETE_SUBSCR: |
| 930 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | case BINARY_LSHIFT: |
| 933 | case BINARY_RSHIFT: |
| 934 | case BINARY_AND: |
| 935 | case BINARY_XOR: |
| 936 | case BINARY_OR: |
| 937 | return -1; |
| 938 | case INPLACE_POWER: |
| 939 | return -1; |
| 940 | case GET_ITER: |
| 941 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | case PRINT_EXPR: |
| 944 | return -1; |
| 945 | case LOAD_BUILD_CLASS: |
| 946 | return 1; |
| 947 | case INPLACE_LSHIFT: |
| 948 | case INPLACE_RSHIFT: |
| 949 | case INPLACE_AND: |
| 950 | case INPLACE_XOR: |
| 951 | case INPLACE_OR: |
| 952 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 955 | /* 1 in the normal flow. |
| 956 | * Restore the stack position and push 6 values before jumping to |
| 957 | * the handler if an exception be raised. */ |
| 958 | return jump ? 6 : 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 959 | case WITH_CLEANUP_START: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 960 | return 2; /* or 1, depending on TOS */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 961 | case WITH_CLEANUP_FINISH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 962 | /* Pop a variable number of values pushed by WITH_CLEANUP_START |
| 963 | * + __exit__ or __aexit__. */ |
| 964 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | case RETURN_VALUE: |
| 966 | return -1; |
| 967 | case IMPORT_STAR: |
| 968 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 969 | case SETUP_ANNOTATIONS: |
| 970 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | case YIELD_VALUE: |
| 972 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 973 | case YIELD_FROM: |
| 974 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | case POP_BLOCK: |
| 976 | return 0; |
| 977 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 978 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | case END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 980 | case POP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 981 | /* Pop 6 values when an exception was raised. */ |
| 982 | return -6; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | case STORE_NAME: |
| 985 | return -1; |
| 986 | case DELETE_NAME: |
| 987 | return 0; |
| 988 | case UNPACK_SEQUENCE: |
| 989 | return oparg-1; |
| 990 | case UNPACK_EX: |
| 991 | return (oparg&0xFF) + (oparg>>8); |
| 992 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 993 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 994 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 996 | case STORE_ATTR: |
| 997 | return -2; |
| 998 | case DELETE_ATTR: |
| 999 | return -1; |
| 1000 | case STORE_GLOBAL: |
| 1001 | return -1; |
| 1002 | case DELETE_GLOBAL: |
| 1003 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1004 | case LOAD_CONST: |
| 1005 | return 1; |
| 1006 | case LOAD_NAME: |
| 1007 | return 1; |
| 1008 | case BUILD_TUPLE: |
| 1009 | case BUILD_LIST: |
| 1010 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1011 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | return 1-oparg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1013 | case BUILD_LIST_UNPACK: |
| 1014 | case BUILD_TUPLE_UNPACK: |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 1015 | case BUILD_TUPLE_UNPACK_WITH_CALL: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1016 | case BUILD_SET_UNPACK: |
| 1017 | case BUILD_MAP_UNPACK: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1018 | case BUILD_MAP_UNPACK_WITH_CALL: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1019 | return 1 - oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1021 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1022 | case BUILD_CONST_KEY_MAP: |
| 1023 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | case LOAD_ATTR: |
| 1025 | return 0; |
| 1026 | case COMPARE_OP: |
| 1027 | return -1; |
| 1028 | case IMPORT_NAME: |
| 1029 | return -1; |
| 1030 | case IMPORT_FROM: |
| 1031 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1032 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1033 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | case JUMP_ABSOLUTE: |
| 1036 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1037 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1038 | case JUMP_IF_TRUE_OR_POP: |
| 1039 | case JUMP_IF_FALSE_OR_POP: |
| 1040 | return jump ? 0 : -1; |
| 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | case POP_JUMP_IF_FALSE: |
| 1043 | case POP_JUMP_IF_TRUE: |
| 1044 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | case LOAD_GLOBAL: |
| 1047 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1048 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1049 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1051 | /* 0 in the normal flow. |
| 1052 | * Restore the stack position and push 6 values before jumping to |
| 1053 | * the handler if an exception be raised. */ |
| 1054 | return jump ? 6 : 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1055 | case BEGIN_FINALLY: |
| 1056 | /* Actually pushes 1 value, but count 6 for balancing with |
| 1057 | * END_FINALLY and POP_FINALLY. |
| 1058 | * This is the main reason of using this opcode instead of |
| 1059 | * "LOAD_CONST None". */ |
| 1060 | return 6; |
| 1061 | case CALL_FINALLY: |
| 1062 | return jump ? 1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | case LOAD_FAST: |
| 1065 | return 1; |
| 1066 | case STORE_FAST: |
| 1067 | return -1; |
| 1068 | case DELETE_FAST: |
| 1069 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case RAISE_VARARGS: |
| 1072 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1073 | |
| 1074 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1076 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1077 | case CALL_METHOD: |
| 1078 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1080 | return -oparg-1; |
| 1081 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1082 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1083 | case MAKE_FUNCTION: |
| 1084 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1085 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | case BUILD_SLICE: |
| 1087 | if (oparg == 3) |
| 1088 | return -2; |
| 1089 | else |
| 1090 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1091 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1092 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | case LOAD_CLOSURE: |
| 1094 | return 1; |
| 1095 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1096 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | return 1; |
| 1098 | case STORE_DEREF: |
| 1099 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1100 | case DELETE_DEREF: |
| 1101 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1102 | |
| 1103 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1104 | case GET_AWAITABLE: |
| 1105 | return 0; |
| 1106 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1107 | /* 0 in the normal flow. |
| 1108 | * Restore the stack position to the position before the result |
| 1109 | * of __aenter__ and push 6 values before jumping to the handler |
| 1110 | * if an exception be raised. */ |
| 1111 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1112 | case BEFORE_ASYNC_WITH: |
| 1113 | return 1; |
| 1114 | case GET_AITER: |
| 1115 | return 0; |
| 1116 | case GET_ANEXT: |
| 1117 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1118 | case GET_YIELD_FROM_ITER: |
| 1119 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1120 | case END_ASYNC_FOR: |
| 1121 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1122 | case FORMAT_VALUE: |
| 1123 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1124 | else 1->1. */ |
| 1125 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1126 | case LOAD_METHOD: |
| 1127 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1129 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1131 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1134 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1135 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1136 | { |
| 1137 | return stack_effect(opcode, oparg, jump); |
| 1138 | } |
| 1139 | |
| 1140 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1141 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1142 | { |
| 1143 | return stack_effect(opcode, oparg, -1); |
| 1144 | } |
| 1145 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1146 | /* Add an opcode with no argument. |
| 1147 | Returns 0 on failure, 1 on success. |
| 1148 | */ |
| 1149 | |
| 1150 | static int |
| 1151 | compiler_addop(struct compiler *c, int opcode) |
| 1152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | basicblock *b; |
| 1154 | struct instr *i; |
| 1155 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1156 | assert(!HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | off = compiler_next_instr(c, c->u->u_curblock); |
| 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; |
| 1166 | compiler_set_lineno(c, off); |
| 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 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) |
| 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 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1310 | PyObject *key = merge_consts_recursive(c, o); |
| 1311 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1312 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1313 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1314 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1315 | Py_ssize_t arg = compiler_add_o(c, c->u->u_consts, key); |
| 1316 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1321 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1322 | { |
| 1323 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1324 | if (arg < 0) |
| 1325 | return 0; |
| 1326 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1327 | } |
| 1328 | |
| 1329 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1330 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1331 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1332 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1333 | Py_ssize_t arg = compiler_add_o(c, dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1334 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1335 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1336 | return compiler_addop_i(c, opcode, arg); |
| 1337 | } |
| 1338 | |
| 1339 | static int |
| 1340 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1342 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1343 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1344 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1345 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1346 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1347 | arg = compiler_add_o(c, dict, mangled); |
| 1348 | Py_DECREF(mangled); |
| 1349 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1350 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1351 | return compiler_addop_i(c, opcode, arg); |
| 1352 | } |
| 1353 | |
| 1354 | /* Add an opcode with an integer argument. |
| 1355 | Returns 0 on failure, 1 on success. |
| 1356 | */ |
| 1357 | |
| 1358 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1359 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | struct instr *i; |
| 1362 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1363 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1364 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1365 | it in the C code (like Python/ceval.c). |
| 1366 | |
| 1367 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1368 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1369 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1370 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1371 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1372 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1373 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1375 | if (off < 0) |
| 1376 | return 0; |
| 1377 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1378 | i->i_opcode = opcode; |
| 1379 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | compiler_set_lineno(c, off); |
| 1381 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static int |
| 1385 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | struct instr *i; |
| 1388 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1389 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1390 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1391 | assert(b != NULL); |
| 1392 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1393 | if (off < 0) |
| 1394 | return 0; |
| 1395 | i = &c->u->u_curblock->b_instr[off]; |
| 1396 | i->i_opcode = opcode; |
| 1397 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | if (absolute) |
| 1399 | i->i_jabs = 1; |
| 1400 | else |
| 1401 | i->i_jrel = 1; |
| 1402 | compiler_set_lineno(c, off); |
| 1403 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1406 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1407 | to the new block. |
| 1408 | |
| 1409 | The returns inside this macro make it impossible to decref objects |
| 1410 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1411 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1412 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | if (compiler_next_block((C)) == NULL) \ |
| 1414 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | if (!compiler_addop((C), (OP))) \ |
| 1419 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1422 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | if (!compiler_addop((C), (OP))) { \ |
| 1424 | compiler_exit_scope(c); \ |
| 1425 | return 0; \ |
| 1426 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1429 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1430 | if (!compiler_addop_load_const((C), (O))) \ |
| 1431 | return 0; \ |
| 1432 | } |
| 1433 | |
| 1434 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1435 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1436 | PyObject *__new_const = (O); \ |
| 1437 | if (__new_const == NULL) { \ |
| 1438 | return 0; \ |
| 1439 | } \ |
| 1440 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1441 | Py_DECREF(__new_const); \ |
| 1442 | return 0; \ |
| 1443 | } \ |
| 1444 | Py_DECREF(__new_const); \ |
| 1445 | } |
| 1446 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1447 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1449 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1452 | /* Same as ADDOP_O, but steals a reference. */ |
| 1453 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1454 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1455 | Py_DECREF((O)); \ |
| 1456 | return 0; \ |
| 1457 | } \ |
| 1458 | Py_DECREF((O)); \ |
| 1459 | } |
| 1460 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1461 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1463 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1468 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1473 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1477 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1478 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1482 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1483 | */ |
| 1484 | |
| 1485 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1487 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1490 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1492 | compiler_exit_scope(c); \ |
| 1493 | return 0; \ |
| 1494 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1497 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1499 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | int _i; \ |
| 1504 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1505 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1506 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1507 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1508 | return 0; \ |
| 1509 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1512 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | int _i; \ |
| 1514 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1515 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1516 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1517 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1518 | compiler_exit_scope(c); \ |
| 1519 | return 0; \ |
| 1520 | } \ |
| 1521 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1524 | /* Search if variable annotations are present statically in a block. */ |
| 1525 | |
| 1526 | static int |
| 1527 | find_ann(asdl_seq *stmts) |
| 1528 | { |
| 1529 | int i, j, res = 0; |
| 1530 | stmt_ty st; |
| 1531 | |
| 1532 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1533 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1534 | switch (st->kind) { |
| 1535 | case AnnAssign_kind: |
| 1536 | return 1; |
| 1537 | case For_kind: |
| 1538 | res = find_ann(st->v.For.body) || |
| 1539 | find_ann(st->v.For.orelse); |
| 1540 | break; |
| 1541 | case AsyncFor_kind: |
| 1542 | res = find_ann(st->v.AsyncFor.body) || |
| 1543 | find_ann(st->v.AsyncFor.orelse); |
| 1544 | break; |
| 1545 | case While_kind: |
| 1546 | res = find_ann(st->v.While.body) || |
| 1547 | find_ann(st->v.While.orelse); |
| 1548 | break; |
| 1549 | case If_kind: |
| 1550 | res = find_ann(st->v.If.body) || |
| 1551 | find_ann(st->v.If.orelse); |
| 1552 | break; |
| 1553 | case With_kind: |
| 1554 | res = find_ann(st->v.With.body); |
| 1555 | break; |
| 1556 | case AsyncWith_kind: |
| 1557 | res = find_ann(st->v.AsyncWith.body); |
| 1558 | break; |
| 1559 | case Try_kind: |
| 1560 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1561 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1562 | st->v.Try.handlers, j); |
| 1563 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1564 | return 1; |
| 1565 | } |
| 1566 | } |
| 1567 | res = find_ann(st->v.Try.body) || |
| 1568 | find_ann(st->v.Try.finalbody) || |
| 1569 | find_ann(st->v.Try.orelse); |
| 1570 | break; |
| 1571 | default: |
| 1572 | res = 0; |
| 1573 | } |
| 1574 | if (res) { |
| 1575 | break; |
| 1576 | } |
| 1577 | } |
| 1578 | return res; |
| 1579 | } |
| 1580 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1581 | /* |
| 1582 | * Frame block handling functions |
| 1583 | */ |
| 1584 | |
| 1585 | static int |
| 1586 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
| 1587 | basicblock *exit) |
| 1588 | { |
| 1589 | struct fblockinfo *f; |
| 1590 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1591 | PyErr_SetString(PyExc_SyntaxError, |
| 1592 | "too many statically nested blocks"); |
| 1593 | return 0; |
| 1594 | } |
| 1595 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1596 | f->fb_type = t; |
| 1597 | f->fb_block = b; |
| 1598 | f->fb_exit = exit; |
| 1599 | return 1; |
| 1600 | } |
| 1601 | |
| 1602 | static void |
| 1603 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1604 | { |
| 1605 | struct compiler_unit *u = c->u; |
| 1606 | assert(u->u_nfblocks > 0); |
| 1607 | u->u_nfblocks--; |
| 1608 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1609 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1610 | } |
| 1611 | |
| 1612 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
| 1613 | * popping the blocks will be restored afterwards. |
| 1614 | */ |
| 1615 | static int |
| 1616 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1617 | int preserve_tos) |
| 1618 | { |
| 1619 | switch (info->fb_type) { |
| 1620 | case WHILE_LOOP: |
| 1621 | return 1; |
| 1622 | |
| 1623 | case FINALLY_END: |
| 1624 | ADDOP_I(c, POP_FINALLY, preserve_tos); |
| 1625 | return 1; |
| 1626 | |
| 1627 | case FOR_LOOP: |
| 1628 | /* Pop the iterator */ |
| 1629 | if (preserve_tos) { |
| 1630 | ADDOP(c, ROT_TWO); |
| 1631 | } |
| 1632 | ADDOP(c, POP_TOP); |
| 1633 | return 1; |
| 1634 | |
| 1635 | case EXCEPT: |
| 1636 | ADDOP(c, POP_BLOCK); |
| 1637 | return 1; |
| 1638 | |
| 1639 | case FINALLY_TRY: |
| 1640 | ADDOP(c, POP_BLOCK); |
| 1641 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1642 | return 1; |
| 1643 | |
| 1644 | case WITH: |
| 1645 | case ASYNC_WITH: |
| 1646 | ADDOP(c, POP_BLOCK); |
| 1647 | if (preserve_tos) { |
| 1648 | ADDOP(c, ROT_TWO); |
| 1649 | } |
| 1650 | ADDOP(c, BEGIN_FINALLY); |
| 1651 | ADDOP(c, WITH_CLEANUP_START); |
| 1652 | if (info->fb_type == ASYNC_WITH) { |
| 1653 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1654 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1655 | ADDOP(c, YIELD_FROM); |
| 1656 | } |
| 1657 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 1658 | ADDOP_I(c, POP_FINALLY, 0); |
| 1659 | return 1; |
| 1660 | |
| 1661 | case HANDLER_CLEANUP: |
| 1662 | if (preserve_tos) { |
| 1663 | ADDOP(c, ROT_FOUR); |
| 1664 | } |
| 1665 | if (info->fb_exit) { |
| 1666 | ADDOP(c, POP_BLOCK); |
| 1667 | ADDOP(c, POP_EXCEPT); |
| 1668 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1669 | } |
| 1670 | else { |
| 1671 | ADDOP(c, POP_EXCEPT); |
| 1672 | } |
| 1673 | return 1; |
| 1674 | } |
| 1675 | Py_UNREACHABLE(); |
| 1676 | } |
| 1677 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1678 | /* Compile a sequence of statements, checking for a docstring |
| 1679 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1680 | |
| 1681 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1682 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1683 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1684 | int i = 0; |
| 1685 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1686 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1687 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1688 | /* Set current line number to the line number of first statement. |
| 1689 | This way line number for SETUP_ANNOTATIONS will always |
| 1690 | coincide with the line number of first "real" statement in module. |
| 1691 | If body is empy, then lineno will be set later in assemble. */ |
| 1692 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && |
| 1693 | !c->u->u_lineno && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1694 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1695 | c->u->u_lineno = st->lineno; |
| 1696 | } |
| 1697 | /* Every annotated class and module should have __annotations__. */ |
| 1698 | if (find_ann(stmts)) { |
| 1699 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1700 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1701 | if (!asdl_seq_LEN(stmts)) |
| 1702 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1703 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1704 | if (c->c_optimize < 2) { |
| 1705 | docstring = _PyAST_GetDocString(stmts); |
| 1706 | if (docstring) { |
| 1707 | i = 1; |
| 1708 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1709 | assert(st->kind == Expr_kind); |
| 1710 | VISIT(c, expr, st->v.Expr.value); |
| 1711 | if (!compiler_nameop(c, __doc__, Store)) |
| 1712 | return 0; |
| 1713 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1714 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1715 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1716 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1717 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | static PyCodeObject * |
| 1721 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1722 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | PyCodeObject *co; |
| 1724 | int addNone = 1; |
| 1725 | static PyObject *module; |
| 1726 | if (!module) { |
| 1727 | module = PyUnicode_InternFromString("<module>"); |
| 1728 | if (!module) |
| 1729 | return NULL; |
| 1730 | } |
| 1731 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1732 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | return NULL; |
| 1734 | switch (mod->kind) { |
| 1735 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1736 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | compiler_exit_scope(c); |
| 1738 | return 0; |
| 1739 | } |
| 1740 | break; |
| 1741 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1742 | if (find_ann(mod->v.Interactive.body)) { |
| 1743 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1744 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | c->c_interactive = 1; |
| 1746 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1747 | mod->v.Interactive.body); |
| 1748 | break; |
| 1749 | case Expression_kind: |
| 1750 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1751 | addNone = 0; |
| 1752 | break; |
| 1753 | case Suite_kind: |
| 1754 | PyErr_SetString(PyExc_SystemError, |
| 1755 | "suite should not be possible"); |
| 1756 | return 0; |
| 1757 | default: |
| 1758 | PyErr_Format(PyExc_SystemError, |
| 1759 | "module kind %d should not be possible", |
| 1760 | mod->kind); |
| 1761 | return 0; |
| 1762 | } |
| 1763 | co = assemble(c, addNone); |
| 1764 | compiler_exit_scope(c); |
| 1765 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1768 | /* The test for LOCAL must come before the test for FREE in order to |
| 1769 | handle classes where name is both local and free. The local var is |
| 1770 | 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] | 1771 | */ |
| 1772 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1773 | static int |
| 1774 | get_ref_type(struct compiler *c, PyObject *name) |
| 1775 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1776 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1777 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1778 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1779 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1780 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | if (scope == 0) { |
| 1782 | char buf[350]; |
| 1783 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1784 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1786 | PyUnicode_AsUTF8(name), |
| 1787 | PyUnicode_AsUTF8(c->u->u_name), |
| 1788 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1789 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1790 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1791 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | ); |
| 1793 | Py_FatalError(buf); |
| 1794 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | static int |
| 1800 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1801 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1802 | PyObject *v; |
| 1803 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1804 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1805 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1806 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1810 | 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] | 1811 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1812 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1813 | if (qualname == NULL) |
| 1814 | qualname = co->co_name; |
| 1815 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1816 | if (free) { |
| 1817 | for (i = 0; i < free; ++i) { |
| 1818 | /* Bypass com_addop_varname because it will generate |
| 1819 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1820 | */ |
| 1821 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1822 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1823 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1824 | /* Special case: If a class contains a method with a |
| 1825 | free variable that has the same name as a method, |
| 1826 | the name will be considered free *and* local in the |
| 1827 | class. It should be handled by the closure, as |
| 1828 | well as by the normal name loookup logic. |
| 1829 | */ |
| 1830 | reftype = get_ref_type(c, name); |
| 1831 | if (reftype == CELL) |
| 1832 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1833 | else /* (reftype == FREE) */ |
| 1834 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1835 | if (arg == -1) { |
| 1836 | fprintf(stderr, |
| 1837 | "lookup %s in %s %d %d\n" |
| 1838 | "freevars of %s: %s\n", |
| 1839 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1840 | PyUnicode_AsUTF8(c->u->u_name), |
| 1841 | reftype, arg, |
| 1842 | PyUnicode_AsUTF8(co->co_name), |
| 1843 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1844 | Py_FatalError("compiler_make_closure()"); |
| 1845 | } |
| 1846 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1848 | flags |= 0x08; |
| 1849 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1851 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1852 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1853 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | static int |
| 1858 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1859 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | if (!decos) |
| 1863 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1864 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1866 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1867 | } |
| 1868 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1872 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1873 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1874 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1875 | /* Push a dict of keyword-only default values. |
| 1876 | |
| 1877 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1878 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1879 | int i; |
| 1880 | PyObject *keys = NULL; |
| 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1883 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1884 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1885 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1886 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1887 | if (!mangled) { |
| 1888 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1890 | if (keys == NULL) { |
| 1891 | keys = PyList_New(1); |
| 1892 | if (keys == NULL) { |
| 1893 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1894 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1895 | } |
| 1896 | PyList_SET_ITEM(keys, 0, mangled); |
| 1897 | } |
| 1898 | else { |
| 1899 | int res = PyList_Append(keys, mangled); |
| 1900 | Py_DECREF(mangled); |
| 1901 | if (res == -1) { |
| 1902 | goto error; |
| 1903 | } |
| 1904 | } |
| 1905 | if (!compiler_visit_expr(c, default_)) { |
| 1906 | goto error; |
| 1907 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | } |
| 1909 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1910 | if (keys != NULL) { |
| 1911 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 1912 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 1913 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1914 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1915 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1916 | assert(default_count > 0); |
| 1917 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1918 | } |
| 1919 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1920 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1921 | } |
| 1922 | |
| 1923 | error: |
| 1924 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1925 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1929 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 1930 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 1931 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1932 | return 1; |
| 1933 | } |
| 1934 | |
| 1935 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1936 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 1937 | expr_ty annotation, PyObject *names) |
| 1938 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1939 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1940 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1941 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 1942 | VISIT(c, annexpr, annotation) |
| 1943 | } |
| 1944 | else { |
| 1945 | VISIT(c, expr, annotation); |
| 1946 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1947 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1948 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1949 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1950 | if (PyList_Append(names, mangled) < 0) { |
| 1951 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1952 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1953 | } |
| 1954 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1955 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1956 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
| 1959 | static int |
| 1960 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 1961 | PyObject *names) |
| 1962 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1963 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1965 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1966 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1967 | c, |
| 1968 | arg->arg, |
| 1969 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1970 | names)) |
| 1971 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1972 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1973 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | static int |
| 1977 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 1978 | expr_ty returns) |
| 1979 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1980 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1981 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1982 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1983 | 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] | 1984 | */ |
| 1985 | static identifier return_str; |
| 1986 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1987 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1988 | names = PyList_New(0); |
| 1989 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 1990 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1991 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1992 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1994 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1995 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1996 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1997 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1998 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2000 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2001 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2002 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2003 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | if (!return_str) { |
| 2006 | return_str = PyUnicode_InternFromString("return"); |
| 2007 | if (!return_str) |
| 2008 | goto error; |
| 2009 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2010 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2011 | goto error; |
| 2012 | } |
| 2013 | |
| 2014 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2015 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2016 | PyObject *keytuple = PyList_AsTuple(names); |
| 2017 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2018 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2019 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2020 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2022 | else { |
| 2023 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2024 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2025 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2026 | |
| 2027 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2029 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2033 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2034 | { |
| 2035 | VISIT_SEQ(c, expr, args->defaults); |
| 2036 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2037 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2040 | static Py_ssize_t |
| 2041 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2042 | { |
| 2043 | Py_ssize_t funcflags = 0; |
| 2044 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2045 | if (!compiler_visit_defaults(c, args)) |
| 2046 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2047 | funcflags |= 0x01; |
| 2048 | } |
| 2049 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2050 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2051 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2052 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2053 | return -1; |
| 2054 | } |
| 2055 | else if (res > 0) { |
| 2056 | funcflags |= 0x02; |
| 2057 | } |
| 2058 | } |
| 2059 | return funcflags; |
| 2060 | } |
| 2061 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2062 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2063 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2064 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2066 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2067 | arguments_ty args; |
| 2068 | expr_ty returns; |
| 2069 | identifier name; |
| 2070 | asdl_seq* decos; |
| 2071 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2072 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2073 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2074 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2075 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2076 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2077 | if (is_async) { |
| 2078 | assert(s->kind == AsyncFunctionDef_kind); |
| 2079 | |
| 2080 | args = s->v.AsyncFunctionDef.args; |
| 2081 | returns = s->v.AsyncFunctionDef.returns; |
| 2082 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2083 | name = s->v.AsyncFunctionDef.name; |
| 2084 | body = s->v.AsyncFunctionDef.body; |
| 2085 | |
| 2086 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2087 | } else { |
| 2088 | assert(s->kind == FunctionDef_kind); |
| 2089 | |
| 2090 | args = s->v.FunctionDef.args; |
| 2091 | returns = s->v.FunctionDef.returns; |
| 2092 | decos = s->v.FunctionDef.decorator_list; |
| 2093 | name = s->v.FunctionDef.name; |
| 2094 | body = s->v.FunctionDef.body; |
| 2095 | |
| 2096 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2097 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | if (!compiler_decorators(c, decos)) |
| 2100 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2101 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2102 | firstlineno = s->lineno; |
| 2103 | if (asdl_seq_LEN(decos)) { |
| 2104 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2105 | } |
| 2106 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2107 | funcflags = compiler_default_arguments(c, args); |
| 2108 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2110 | } |
| 2111 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2112 | annotations = compiler_visit_annotations(c, args, returns); |
| 2113 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2114 | return 0; |
| 2115 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2116 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2117 | funcflags |= 0x04; |
| 2118 | } |
| 2119 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2120 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2121 | return 0; |
| 2122 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2123 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2124 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2125 | if (c->c_optimize < 2) { |
| 2126 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2127 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2128 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | compiler_exit_scope(c); |
| 2130 | return 0; |
| 2131 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2133 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2134 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2136 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2138 | qualname = c->u->u_qualname; |
| 2139 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2141 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2142 | Py_XDECREF(qualname); |
| 2143 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2145 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2146 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2147 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2148 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2151 | /* decorators */ |
| 2152 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2153 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2154 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2155 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2156 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
| 2159 | static int |
| 2160 | compiler_class(struct compiler *c, stmt_ty s) |
| 2161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | PyCodeObject *co; |
| 2163 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2164 | int i, firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2167 | if (!compiler_decorators(c, decos)) |
| 2168 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2169 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2170 | firstlineno = s->lineno; |
| 2171 | if (asdl_seq_LEN(decos)) { |
| 2172 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2173 | } |
| 2174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2175 | /* ultimately generate code for: |
| 2176 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2177 | where: |
| 2178 | <func> is a function/closure created from the class body; |
| 2179 | it has a single argument (__locals__) where the dict |
| 2180 | (or MutableSequence) representing the locals is passed |
| 2181 | <name> is the class name |
| 2182 | <bases> is the positional arguments and *varargs argument |
| 2183 | <keywords> is the keyword arguments and **kwds argument |
| 2184 | This borrows from compiler_call. |
| 2185 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2187 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2188 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2189 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2190 | return 0; |
| 2191 | /* this block represents what we do in the new scope */ |
| 2192 | { |
| 2193 | /* use the class name for name mangling */ |
| 2194 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2195 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | /* load (global) __name__ ... */ |
| 2197 | str = PyUnicode_InternFromString("__name__"); |
| 2198 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2199 | Py_XDECREF(str); |
| 2200 | compiler_exit_scope(c); |
| 2201 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2202 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | Py_DECREF(str); |
| 2204 | /* ... and store it as __module__ */ |
| 2205 | str = PyUnicode_InternFromString("__module__"); |
| 2206 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2207 | Py_XDECREF(str); |
| 2208 | compiler_exit_scope(c); |
| 2209 | return 0; |
| 2210 | } |
| 2211 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2212 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2213 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2214 | str = PyUnicode_InternFromString("__qualname__"); |
| 2215 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2216 | Py_XDECREF(str); |
| 2217 | compiler_exit_scope(c); |
| 2218 | return 0; |
| 2219 | } |
| 2220 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2222 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | compiler_exit_scope(c); |
| 2224 | return 0; |
| 2225 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2226 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2227 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2228 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2229 | str = PyUnicode_InternFromString("__class__"); |
| 2230 | if (str == NULL) { |
| 2231 | compiler_exit_scope(c); |
| 2232 | return 0; |
| 2233 | } |
| 2234 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2235 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2236 | if (i < 0) { |
| 2237 | compiler_exit_scope(c); |
| 2238 | return 0; |
| 2239 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2240 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2242 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2243 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2244 | str = PyUnicode_InternFromString("__classcell__"); |
| 2245 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2246 | Py_XDECREF(str); |
| 2247 | compiler_exit_scope(c); |
| 2248 | return 0; |
| 2249 | } |
| 2250 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2252 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2253 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2254 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2255 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2256 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2257 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | /* create the code object */ |
| 2259 | co = assemble(c, 1); |
| 2260 | } |
| 2261 | /* leave the new scope */ |
| 2262 | compiler_exit_scope(c); |
| 2263 | if (co == NULL) |
| 2264 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | /* 2. load the 'build_class' function */ |
| 2267 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2268 | |
| 2269 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2270 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2271 | Py_DECREF(co); |
| 2272 | |
| 2273 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2274 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | |
| 2276 | /* 5. generate the rest of the code for the call */ |
| 2277 | if (!compiler_call_helper(c, 2, |
| 2278 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2279 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | return 0; |
| 2281 | |
| 2282 | /* 6. apply decorators */ |
| 2283 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2284 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2285 | } |
| 2286 | |
| 2287 | /* 7. store into <name> */ |
| 2288 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2289 | return 0; |
| 2290 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2293 | /* Return 0 if the expression is a constant value except named singletons. |
| 2294 | Return 1 otherwise. */ |
| 2295 | static int |
| 2296 | check_is_arg(expr_ty e) |
| 2297 | { |
| 2298 | if (e->kind != Constant_kind) { |
| 2299 | return 1; |
| 2300 | } |
| 2301 | PyObject *value = e->v.Constant.value; |
| 2302 | return (value == Py_None |
| 2303 | || value == Py_False |
| 2304 | || value == Py_True |
| 2305 | || value == Py_Ellipsis); |
| 2306 | } |
| 2307 | |
| 2308 | /* Check operands of identity chacks ("is" and "is not"). |
| 2309 | Emit a warning if any operand is a constant except named singletons. |
| 2310 | Return 0 on error. |
| 2311 | */ |
| 2312 | static int |
| 2313 | check_compare(struct compiler *c, expr_ty e) |
| 2314 | { |
| 2315 | Py_ssize_t i, n; |
| 2316 | int left = check_is_arg(e->v.Compare.left); |
| 2317 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2318 | for (i = 0; i < n; i++) { |
| 2319 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2320 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2321 | if (op == Is || op == IsNot) { |
| 2322 | if (!right || !left) { |
| 2323 | const char *msg = (op == Is) |
| 2324 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2325 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2326 | return compiler_warn(c, msg); |
| 2327 | } |
| 2328 | } |
| 2329 | left = right; |
| 2330 | } |
| 2331 | return 1; |
| 2332 | } |
| 2333 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2334 | static int |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2335 | cmpop(cmpop_ty op) |
| 2336 | { |
| 2337 | switch (op) { |
| 2338 | case Eq: |
| 2339 | return PyCmp_EQ; |
| 2340 | case NotEq: |
| 2341 | return PyCmp_NE; |
| 2342 | case Lt: |
| 2343 | return PyCmp_LT; |
| 2344 | case LtE: |
| 2345 | return PyCmp_LE; |
| 2346 | case Gt: |
| 2347 | return PyCmp_GT; |
| 2348 | case GtE: |
| 2349 | return PyCmp_GE; |
| 2350 | case Is: |
| 2351 | return PyCmp_IS; |
| 2352 | case IsNot: |
| 2353 | return PyCmp_IS_NOT; |
| 2354 | case In: |
| 2355 | return PyCmp_IN; |
| 2356 | case NotIn: |
| 2357 | return PyCmp_NOT_IN; |
| 2358 | default: |
| 2359 | return PyCmp_BAD; |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | static int |
| 2364 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2365 | { |
| 2366 | switch (e->kind) { |
| 2367 | case UnaryOp_kind: |
| 2368 | if (e->v.UnaryOp.op == Not) |
| 2369 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2370 | /* fallback to general implementation */ |
| 2371 | break; |
| 2372 | case BoolOp_kind: { |
| 2373 | asdl_seq *s = e->v.BoolOp.values; |
| 2374 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2375 | assert(n >= 0); |
| 2376 | int cond2 = e->v.BoolOp.op == Or; |
| 2377 | basicblock *next2 = next; |
| 2378 | if (!cond2 != !cond) { |
| 2379 | next2 = compiler_new_block(c); |
| 2380 | if (next2 == NULL) |
| 2381 | return 0; |
| 2382 | } |
| 2383 | for (i = 0; i < n; ++i) { |
| 2384 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2385 | return 0; |
| 2386 | } |
| 2387 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2388 | return 0; |
| 2389 | if (next2 != next) |
| 2390 | compiler_use_next_block(c, next2); |
| 2391 | return 1; |
| 2392 | } |
| 2393 | case IfExp_kind: { |
| 2394 | basicblock *end, *next2; |
| 2395 | end = compiler_new_block(c); |
| 2396 | if (end == NULL) |
| 2397 | return 0; |
| 2398 | next2 = compiler_new_block(c); |
| 2399 | if (next2 == NULL) |
| 2400 | return 0; |
| 2401 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2402 | return 0; |
| 2403 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2404 | return 0; |
| 2405 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2406 | compiler_use_next_block(c, next2); |
| 2407 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2408 | return 0; |
| 2409 | compiler_use_next_block(c, end); |
| 2410 | return 1; |
| 2411 | } |
| 2412 | case Compare_kind: { |
| 2413 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2414 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2415 | if (!check_compare(c, e)) { |
| 2416 | return 0; |
| 2417 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2418 | basicblock *cleanup = compiler_new_block(c); |
| 2419 | if (cleanup == NULL) |
| 2420 | return 0; |
| 2421 | VISIT(c, expr, e->v.Compare.left); |
| 2422 | for (i = 0; i < n; i++) { |
| 2423 | VISIT(c, expr, |
| 2424 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2425 | ADDOP(c, DUP_TOP); |
| 2426 | ADDOP(c, ROT_THREE); |
| 2427 | ADDOP_I(c, COMPARE_OP, |
| 2428 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 2429 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2430 | NEXT_BLOCK(c); |
| 2431 | } |
| 2432 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 2433 | ADDOP_I(c, COMPARE_OP, |
| 2434 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
| 2435 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2436 | basicblock *end = compiler_new_block(c); |
| 2437 | if (end == NULL) |
| 2438 | return 0; |
| 2439 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2440 | compiler_use_next_block(c, cleanup); |
| 2441 | ADDOP(c, POP_TOP); |
| 2442 | if (!cond) { |
| 2443 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2444 | } |
| 2445 | compiler_use_next_block(c, end); |
| 2446 | return 1; |
| 2447 | } |
| 2448 | /* fallback to general implementation */ |
| 2449 | break; |
| 2450 | } |
| 2451 | default: |
| 2452 | /* fallback to general implementation */ |
| 2453 | break; |
| 2454 | } |
| 2455 | |
| 2456 | /* general implementation */ |
| 2457 | VISIT(c, expr, e); |
| 2458 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2459 | return 1; |
| 2460 | } |
| 2461 | |
| 2462 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2463 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2465 | basicblock *end, *next; |
| 2466 | |
| 2467 | assert(e->kind == IfExp_kind); |
| 2468 | end = compiler_new_block(c); |
| 2469 | if (end == NULL) |
| 2470 | return 0; |
| 2471 | next = compiler_new_block(c); |
| 2472 | if (next == NULL) |
| 2473 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2474 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2475 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | VISIT(c, expr, e->v.IfExp.body); |
| 2477 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2478 | compiler_use_next_block(c, next); |
| 2479 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2480 | compiler_use_next_block(c, end); |
| 2481 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2485 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2486 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2487 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2488 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2489 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2490 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2491 | arguments_ty args = e->v.Lambda.args; |
| 2492 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2494 | if (!name) { |
| 2495 | name = PyUnicode_InternFromString("<lambda>"); |
| 2496 | if (!name) |
| 2497 | return 0; |
| 2498 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2499 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2500 | funcflags = compiler_default_arguments(c, args); |
| 2501 | if (funcflags == -1) { |
| 2502 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2503 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2504 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2505 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2506 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | /* Make None the first constant, so the lambda can't have a |
| 2510 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2511 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2512 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2513 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2515 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2516 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2517 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2518 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2519 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2520 | } |
| 2521 | else { |
| 2522 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2523 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2524 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2525 | qualname = c->u->u_qualname; |
| 2526 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2528 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2529 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2530 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2531 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2532 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2533 | Py_DECREF(co); |
| 2534 | |
| 2535 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
| 2538 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2539 | compiler_if(struct compiler *c, stmt_ty s) |
| 2540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2541 | basicblock *end, *next; |
| 2542 | int constant; |
| 2543 | assert(s->kind == If_kind); |
| 2544 | end = compiler_new_block(c); |
| 2545 | if (end == NULL) |
| 2546 | return 0; |
| 2547 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2548 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 2549 | /* constant = 0: "if 0" Leave the optimizations to |
| 2550 | * the pephole optimizer to check for syntax errors |
| 2551 | * in the block. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 | * constant = 1: "if 1", "if 2", ... |
| 2553 | * constant = -1: rest */ |
Pablo Galindo | af8646c | 2019-05-17 11:37:08 +0100 | [diff] [blame] | 2554 | if (constant == 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2555 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2556 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2557 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2558 | next = compiler_new_block(c); |
| 2559 | if (next == NULL) |
| 2560 | return 0; |
| 2561 | } |
| 2562 | else |
| 2563 | next = end; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2564 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) |
| 2565 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2567 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2568 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | compiler_use_next_block(c, next); |
| 2570 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2571 | } |
| 2572 | } |
| 2573 | compiler_use_next_block(c, end); |
| 2574 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | static int |
| 2578 | compiler_for(struct compiler *c, stmt_ty s) |
| 2579 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2580 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2582 | start = compiler_new_block(c); |
| 2583 | cleanup = compiler_new_block(c); |
| 2584 | end = compiler_new_block(c); |
| 2585 | if (start == NULL || end == NULL || cleanup == NULL) |
| 2586 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2587 | |
| 2588 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2589 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2590 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | VISIT(c, expr, s->v.For.iter); |
| 2592 | ADDOP(c, GET_ITER); |
| 2593 | compiler_use_next_block(c, start); |
| 2594 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2595 | VISIT(c, expr, s->v.For.target); |
| 2596 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2597 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2598 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2599 | |
| 2600 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2603 | compiler_use_next_block(c, end); |
| 2604 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2607 | |
| 2608 | static int |
| 2609 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2610 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2611 | basicblock *start, *except, *end; |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2612 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
| 2613 | return compiler_error(c, "'async for' outside async function"); |
| 2614 | } |
| 2615 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2616 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2617 | except = compiler_new_block(c); |
| 2618 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2619 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2620 | if (start == NULL || except == NULL || end == NULL) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2621 | return 0; |
| 2622 | |
| 2623 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2624 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2625 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2626 | compiler_use_next_block(c, start); |
| 2627 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
| 2628 | return 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2629 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2630 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2631 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2632 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2633 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2634 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2635 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2636 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2637 | /* Success block for __anext__ */ |
| 2638 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2639 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2640 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2641 | |
| 2642 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2643 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2644 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2645 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2646 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2647 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2648 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2649 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2650 | |
| 2651 | compiler_use_next_block(c, end); |
| 2652 | |
| 2653 | return 1; |
| 2654 | } |
| 2655 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2656 | static int |
| 2657 | compiler_while(struct compiler *c, stmt_ty s) |
| 2658 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2660 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2661 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | if (constant == 0) { |
| 2663 | if (s->v.While.orelse) |
| 2664 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2665 | return 1; |
| 2666 | } |
| 2667 | loop = compiler_new_block(c); |
| 2668 | end = compiler_new_block(c); |
| 2669 | if (constant == -1) { |
| 2670 | anchor = compiler_new_block(c); |
| 2671 | if (anchor == NULL) |
| 2672 | return 0; |
| 2673 | } |
| 2674 | if (loop == NULL || end == NULL) |
| 2675 | return 0; |
| 2676 | if (s->v.While.orelse) { |
| 2677 | orelse = compiler_new_block(c); |
| 2678 | if (orelse == NULL) |
| 2679 | return 0; |
| 2680 | } |
| 2681 | else |
| 2682 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2683 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | compiler_use_next_block(c, loop); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2685 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | return 0; |
| 2687 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2688 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2689 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | } |
| 2691 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2692 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | /* XXX should the two POP instructions be in a separate block |
| 2695 | if there is no else clause ? |
| 2696 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2697 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2698 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2700 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2703 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2704 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2710 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2711 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2712 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2713 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2714 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2715 | return compiler_error(c, "'return' outside function"); |
| 2716 | if (s->v.Return.value != NULL && |
| 2717 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2718 | { |
| 2719 | return compiler_error( |
| 2720 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2722 | if (preserve_tos) { |
| 2723 | VISIT(c, expr, s->v.Return.value); |
| 2724 | } |
| 2725 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2726 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2727 | |
| 2728 | if (!compiler_unwind_fblock(c, info, preserve_tos)) |
| 2729 | return 0; |
| 2730 | } |
| 2731 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2732 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2733 | } |
| 2734 | else if (!preserve_tos) { |
| 2735 | VISIT(c, expr, s->v.Return.value); |
| 2736 | } |
| 2737 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2742 | static int |
| 2743 | compiler_break(struct compiler *c) |
| 2744 | { |
| 2745 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2746 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2747 | |
| 2748 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2749 | return 0; |
| 2750 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2751 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit); |
| 2752 | return 1; |
| 2753 | } |
| 2754 | } |
| 2755 | return compiler_error(c, "'break' outside loop"); |
| 2756 | } |
| 2757 | |
| 2758 | static int |
| 2759 | compiler_continue(struct compiler *c) |
| 2760 | { |
| 2761 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2762 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2763 | |
| 2764 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2765 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block); |
| 2766 | return 1; |
| 2767 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2768 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2769 | return 0; |
| 2770 | } |
| 2771 | return compiler_error(c, "'continue' not properly in loop"); |
| 2772 | } |
| 2773 | |
| 2774 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2775 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2776 | |
| 2777 | SETUP_FINALLY L |
| 2778 | <code for body> |
| 2779 | POP_BLOCK |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2780 | BEGIN_FINALLY |
| 2781 | L: |
| 2782 | <code for finalbody> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | END_FINALLY |
| 2784 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2785 | The special instructions use the block stack. Each block |
| 2786 | stack entry contains the instruction that created it (here |
| 2787 | SETUP_FINALLY), the level of the value stack at the time the |
| 2788 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2790 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | Pushes the current value stack level and the label |
| 2792 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2793 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2794 | Pops en entry from the block stack. |
| 2795 | BEGIN_FINALLY |
| 2796 | Pushes NULL onto the value stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2797 | END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2798 | Pops 1 (NULL or int) or 6 entries from the *value* stack and restore |
| 2799 | the raised and the caught exceptions they specify. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2801 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2802 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2803 | exceptions are pushed onto the value stack (and the exception |
| 2804 | condition is cleared), and the interpreter jumps to the label |
| 2805 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2806 | */ |
| 2807 | |
| 2808 | static int |
| 2809 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2811 | basicblock *body, *end; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | body = compiler_new_block(c); |
| 2814 | end = compiler_new_block(c); |
| 2815 | if (body == NULL || end == NULL) |
| 2816 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2818 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2819 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2820 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2821 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2823 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2824 | if (!compiler_try_except(c, s)) |
| 2825 | return 0; |
| 2826 | } |
| 2827 | else { |
| 2828 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2829 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2831 | ADDOP(c, BEGIN_FINALLY); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | compiler_pop_fblock(c, FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2833 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2834 | /* `finally` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2835 | compiler_use_next_block(c, end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2836 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2838 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | ADDOP(c, END_FINALLY); |
| 2840 | compiler_pop_fblock(c, FINALLY_END, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
| 2844 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2845 | 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] | 2846 | (The contents of the value stack is shown in [], with the top |
| 2847 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2848 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | |
| 2850 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2851 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | [] <code for S> |
| 2853 | [] POP_BLOCK |
| 2854 | [] JUMP_FORWARD L0 |
| 2855 | |
| 2856 | [tb, val, exc] L1: DUP ) |
| 2857 | [tb, val, exc, exc] <evaluate E1> ) |
| 2858 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2859 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2860 | [tb, val, exc] POP |
| 2861 | [tb, val] <assign to V1> (or POP if no V1) |
| 2862 | [tb] POP |
| 2863 | [] <code for S1> |
| 2864 | JUMP_FORWARD L0 |
| 2865 | |
| 2866 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2867 | .............................etc....................... |
| 2868 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2870 | |
| 2871 | [] L0: <next statement> |
| 2872 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2873 | Of course, parts are not generated if Vi or Ei is not present. |
| 2874 | */ |
| 2875 | static int |
| 2876 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2877 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2879 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2881 | body = compiler_new_block(c); |
| 2882 | except = compiler_new_block(c); |
| 2883 | orelse = compiler_new_block(c); |
| 2884 | end = compiler_new_block(c); |
| 2885 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2886 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2887 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2889 | if (!compiler_push_fblock(c, EXCEPT, body, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2891 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | ADDOP(c, POP_BLOCK); |
| 2893 | compiler_pop_fblock(c, EXCEPT, body); |
| 2894 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2895 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2896 | compiler_use_next_block(c, except); |
| 2897 | for (i = 0; i < n; i++) { |
| 2898 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2899 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 2901 | return compiler_error(c, "default 'except:' must be last"); |
| 2902 | c->u->u_lineno_set = 0; |
| 2903 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 2904 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2905 | except = compiler_new_block(c); |
| 2906 | if (except == NULL) |
| 2907 | return 0; |
| 2908 | if (handler->v.ExceptHandler.type) { |
| 2909 | ADDOP(c, DUP_TOP); |
| 2910 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 2911 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 2912 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 2913 | } |
| 2914 | ADDOP(c, POP_TOP); |
| 2915 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2916 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2917 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2918 | cleanup_end = compiler_new_block(c); |
| 2919 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 2920 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2921 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 2922 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2923 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2924 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 2925 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2927 | /* |
| 2928 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2929 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2930 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2931 | try: |
| 2932 | # body |
| 2933 | finally: |
| 2934 | name = None |
| 2935 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2936 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2937 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2938 | /* second try: */ |
| 2939 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 2940 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2941 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2942 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2943 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2944 | /* second # body */ |
| 2945 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 2946 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2947 | ADDOP(c, BEGIN_FINALLY); |
| 2948 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2950 | /* finally: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2951 | compiler_use_next_block(c, cleanup_end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2952 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2953 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2955 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2956 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2957 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2958 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2960 | ADDOP(c, END_FINALLY); |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 2961 | ADDOP(c, POP_EXCEPT); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2962 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | } |
| 2964 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2965 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2967 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2968 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2969 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2970 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2971 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2972 | ADDOP(c, POP_TOP); |
| 2973 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2974 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2975 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2977 | ADDOP(c, POP_EXCEPT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2978 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | } |
| 2980 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2981 | compiler_use_next_block(c, except); |
| 2982 | } |
| 2983 | ADDOP(c, END_FINALLY); |
| 2984 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2985 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2986 | compiler_use_next_block(c, end); |
| 2987 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2991 | compiler_try(struct compiler *c, stmt_ty s) { |
| 2992 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 2993 | return compiler_try_finally(c, s); |
| 2994 | else |
| 2995 | return compiler_try_except(c, s); |
| 2996 | } |
| 2997 | |
| 2998 | |
| 2999 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3000 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3001 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | /* The IMPORT_NAME opcode was already generated. This function |
| 3003 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3004 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | 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] | 3006 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3008 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3009 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3010 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3011 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3012 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3014 | while (1) { |
| 3015 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3017 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3018 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3019 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3020 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3022 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3023 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3024 | if (dot == -1) { |
| 3025 | break; |
| 3026 | } |
| 3027 | ADDOP(c, ROT_TWO); |
| 3028 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3030 | if (!compiler_nameop(c, asname, Store)) { |
| 3031 | return 0; |
| 3032 | } |
| 3033 | ADDOP(c, POP_TOP); |
| 3034 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | } |
| 3036 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
| 3039 | static int |
| 3040 | compiler_import(struct compiler *c, stmt_ty s) |
| 3041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | /* The Import node stores a module name like a.b.c as a single |
| 3043 | string. This is convenient for all cases except |
| 3044 | import a.b.c as d |
| 3045 | where we need to parse that string to extract the individual |
| 3046 | module names. |
| 3047 | XXX Perhaps change the representation to make this case simpler? |
| 3048 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3049 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | for (i = 0; i < n; i++) { |
| 3052 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3053 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3054 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3055 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3056 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3057 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | if (alias->asname) { |
| 3060 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3061 | if (!r) |
| 3062 | return r; |
| 3063 | } |
| 3064 | else { |
| 3065 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3066 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3067 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3068 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3069 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3070 | if (tmp == NULL) |
| 3071 | return 0; |
| 3072 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3073 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3074 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | Py_DECREF(tmp); |
| 3076 | } |
| 3077 | if (!r) |
| 3078 | return r; |
| 3079 | } |
| 3080 | } |
| 3081 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | static int |
| 3085 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3086 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3087 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3088 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | if (!empty_string) { |
| 3092 | empty_string = PyUnicode_FromString(""); |
| 3093 | if (!empty_string) |
| 3094 | return 0; |
| 3095 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3096 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3097 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3098 | |
| 3099 | names = PyTuple_New(n); |
| 3100 | if (!names) |
| 3101 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | /* build up the names */ |
| 3104 | for (i = 0; i < n; i++) { |
| 3105 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3106 | Py_INCREF(alias->name); |
| 3107 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3108 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3111 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | Py_DECREF(names); |
| 3113 | return compiler_error(c, "from __future__ imports must occur " |
| 3114 | "at the beginning of the file"); |
| 3115 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3116 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | if (s->v.ImportFrom.module) { |
| 3119 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3120 | } |
| 3121 | else { |
| 3122 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3123 | } |
| 3124 | for (i = 0; i < n; i++) { |
| 3125 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3126 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3127 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3128 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | assert(n == 1); |
| 3130 | ADDOP(c, IMPORT_STAR); |
| 3131 | return 1; |
| 3132 | } |
| 3133 | |
| 3134 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3135 | store_name = alias->name; |
| 3136 | if (alias->asname) |
| 3137 | store_name = alias->asname; |
| 3138 | |
| 3139 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | return 0; |
| 3141 | } |
| 3142 | } |
| 3143 | /* remove imported module */ |
| 3144 | ADDOP(c, POP_TOP); |
| 3145 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | static int |
| 3149 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | static PyObject *assertion_error = NULL; |
| 3152 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3153 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3154 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | return 1; |
| 3156 | if (assertion_error == NULL) { |
| 3157 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 3158 | if (assertion_error == NULL) |
| 3159 | return 0; |
| 3160 | } |
| 3161 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3162 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3163 | { |
| 3164 | if (!compiler_warn(c, "assertion is always true, " |
| 3165 | "perhaps remove parentheses?")) |
| 3166 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3167 | return 0; |
| 3168 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | end = compiler_new_block(c); |
| 3171 | if (end == NULL) |
| 3172 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3173 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3174 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3175 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 3176 | if (s->v.Assert.msg) { |
| 3177 | VISIT(c, expr, s->v.Assert.msg); |
| 3178 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3179 | } |
| 3180 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3181 | compiler_use_next_block(c, end); |
| 3182 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3186 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3187 | { |
| 3188 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3189 | VISIT(c, expr, value); |
| 3190 | ADDOP(c, PRINT_EXPR); |
| 3191 | return 1; |
| 3192 | } |
| 3193 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3194 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3195 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3196 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | VISIT(c, expr, value); |
| 3200 | ADDOP(c, POP_TOP); |
| 3201 | return 1; |
| 3202 | } |
| 3203 | |
| 3204 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3205 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3206 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3207 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 3210 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3211 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | switch (s->kind) { |
| 3215 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3216 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | case ClassDef_kind: |
| 3218 | return compiler_class(c, s); |
| 3219 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3220 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3221 | case Delete_kind: |
| 3222 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3223 | break; |
| 3224 | case Assign_kind: |
| 3225 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3226 | VISIT(c, expr, s->v.Assign.value); |
| 3227 | for (i = 0; i < n; i++) { |
| 3228 | if (i < n - 1) |
| 3229 | ADDOP(c, DUP_TOP); |
| 3230 | VISIT(c, expr, |
| 3231 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3232 | } |
| 3233 | break; |
| 3234 | case AugAssign_kind: |
| 3235 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3236 | case AnnAssign_kind: |
| 3237 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | case For_kind: |
| 3239 | return compiler_for(c, s); |
| 3240 | case While_kind: |
| 3241 | return compiler_while(c, s); |
| 3242 | case If_kind: |
| 3243 | return compiler_if(c, s); |
| 3244 | case Raise_kind: |
| 3245 | n = 0; |
| 3246 | if (s->v.Raise.exc) { |
| 3247 | VISIT(c, expr, s->v.Raise.exc); |
| 3248 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3249 | if (s->v.Raise.cause) { |
| 3250 | VISIT(c, expr, s->v.Raise.cause); |
| 3251 | n++; |
| 3252 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3254 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3256 | case Try_kind: |
| 3257 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3258 | case Assert_kind: |
| 3259 | return compiler_assert(c, s); |
| 3260 | case Import_kind: |
| 3261 | return compiler_import(c, s); |
| 3262 | case ImportFrom_kind: |
| 3263 | return compiler_from_import(c, s); |
| 3264 | case Global_kind: |
| 3265 | case Nonlocal_kind: |
| 3266 | break; |
| 3267 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3268 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | case Pass_kind: |
| 3270 | break; |
| 3271 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3272 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3273 | case Continue_kind: |
| 3274 | return compiler_continue(c); |
| 3275 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3276 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3277 | case AsyncFunctionDef_kind: |
| 3278 | return compiler_function(c, s, 1); |
| 3279 | case AsyncWith_kind: |
| 3280 | return compiler_async_with(c, s, 0); |
| 3281 | case AsyncFor_kind: |
| 3282 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | static int |
| 3289 | unaryop(unaryop_ty op) |
| 3290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | switch (op) { |
| 3292 | case Invert: |
| 3293 | return UNARY_INVERT; |
| 3294 | case Not: |
| 3295 | return UNARY_NOT; |
| 3296 | case UAdd: |
| 3297 | return UNARY_POSITIVE; |
| 3298 | case USub: |
| 3299 | return UNARY_NEGATIVE; |
| 3300 | default: |
| 3301 | PyErr_Format(PyExc_SystemError, |
| 3302 | "unary op %d should not be possible", op); |
| 3303 | return 0; |
| 3304 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | static int |
| 3308 | binop(struct compiler *c, operator_ty op) |
| 3309 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3310 | switch (op) { |
| 3311 | case Add: |
| 3312 | return BINARY_ADD; |
| 3313 | case Sub: |
| 3314 | return BINARY_SUBTRACT; |
| 3315 | case Mult: |
| 3316 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3317 | case MatMult: |
| 3318 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | case Div: |
| 3320 | return BINARY_TRUE_DIVIDE; |
| 3321 | case Mod: |
| 3322 | return BINARY_MODULO; |
| 3323 | case Pow: |
| 3324 | return BINARY_POWER; |
| 3325 | case LShift: |
| 3326 | return BINARY_LSHIFT; |
| 3327 | case RShift: |
| 3328 | return BINARY_RSHIFT; |
| 3329 | case BitOr: |
| 3330 | return BINARY_OR; |
| 3331 | case BitXor: |
| 3332 | return BINARY_XOR; |
| 3333 | case BitAnd: |
| 3334 | return BINARY_AND; |
| 3335 | case FloorDiv: |
| 3336 | return BINARY_FLOOR_DIVIDE; |
| 3337 | default: |
| 3338 | PyErr_Format(PyExc_SystemError, |
| 3339 | "binary op %d should not be possible", op); |
| 3340 | return 0; |
| 3341 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3342 | } |
| 3343 | |
| 3344 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3345 | inplace_binop(struct compiler *c, operator_ty op) |
| 3346 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3347 | switch (op) { |
| 3348 | case Add: |
| 3349 | return INPLACE_ADD; |
| 3350 | case Sub: |
| 3351 | return INPLACE_SUBTRACT; |
| 3352 | case Mult: |
| 3353 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3354 | case MatMult: |
| 3355 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3356 | case Div: |
| 3357 | return INPLACE_TRUE_DIVIDE; |
| 3358 | case Mod: |
| 3359 | return INPLACE_MODULO; |
| 3360 | case Pow: |
| 3361 | return INPLACE_POWER; |
| 3362 | case LShift: |
| 3363 | return INPLACE_LSHIFT; |
| 3364 | case RShift: |
| 3365 | return INPLACE_RSHIFT; |
| 3366 | case BitOr: |
| 3367 | return INPLACE_OR; |
| 3368 | case BitXor: |
| 3369 | return INPLACE_XOR; |
| 3370 | case BitAnd: |
| 3371 | return INPLACE_AND; |
| 3372 | case FloorDiv: |
| 3373 | return INPLACE_FLOOR_DIVIDE; |
| 3374 | default: |
| 3375 | PyErr_Format(PyExc_SystemError, |
| 3376 | "inplace binary op %d should not be possible", op); |
| 3377 | return 0; |
| 3378 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | static int |
| 3382 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3383 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3384 | int op, scope; |
| 3385 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | PyObject *dict = c->u->u_names; |
| 3389 | PyObject *mangled; |
| 3390 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3391 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3392 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3393 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3394 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3395 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3396 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3397 | if (!mangled) |
| 3398 | return 0; |
| 3399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3400 | op = 0; |
| 3401 | optype = OP_NAME; |
| 3402 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3403 | switch (scope) { |
| 3404 | case FREE: |
| 3405 | dict = c->u->u_freevars; |
| 3406 | optype = OP_DEREF; |
| 3407 | break; |
| 3408 | case CELL: |
| 3409 | dict = c->u->u_cellvars; |
| 3410 | optype = OP_DEREF; |
| 3411 | break; |
| 3412 | case LOCAL: |
| 3413 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3414 | optype = OP_FAST; |
| 3415 | break; |
| 3416 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3417 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3418 | optype = OP_GLOBAL; |
| 3419 | break; |
| 3420 | case GLOBAL_EXPLICIT: |
| 3421 | optype = OP_GLOBAL; |
| 3422 | break; |
| 3423 | default: |
| 3424 | /* scope can be 0 */ |
| 3425 | break; |
| 3426 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3428 | /* 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] | 3429 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | switch (optype) { |
| 3432 | case OP_DEREF: |
| 3433 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3434 | case Load: |
| 3435 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3436 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3437 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3438 | op = STORE_DEREF; |
| 3439 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3440 | case AugLoad: |
| 3441 | case AugStore: |
| 3442 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3443 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | case Param: |
| 3445 | default: |
| 3446 | PyErr_SetString(PyExc_SystemError, |
| 3447 | "param invalid for deref variable"); |
| 3448 | return 0; |
| 3449 | } |
| 3450 | break; |
| 3451 | case OP_FAST: |
| 3452 | switch (ctx) { |
| 3453 | case Load: op = LOAD_FAST; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3454 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3455 | op = STORE_FAST; |
| 3456 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3457 | case Del: op = DELETE_FAST; break; |
| 3458 | case AugLoad: |
| 3459 | case AugStore: |
| 3460 | break; |
| 3461 | case Param: |
| 3462 | default: |
| 3463 | PyErr_SetString(PyExc_SystemError, |
| 3464 | "param invalid for local variable"); |
| 3465 | return 0; |
| 3466 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3467 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3468 | return 1; |
| 3469 | case OP_GLOBAL: |
| 3470 | switch (ctx) { |
| 3471 | case Load: op = LOAD_GLOBAL; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3472 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3473 | op = STORE_GLOBAL; |
| 3474 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3475 | case Del: op = DELETE_GLOBAL; break; |
| 3476 | case AugLoad: |
| 3477 | case AugStore: |
| 3478 | break; |
| 3479 | case Param: |
| 3480 | default: |
| 3481 | PyErr_SetString(PyExc_SystemError, |
| 3482 | "param invalid for global variable"); |
| 3483 | return 0; |
| 3484 | } |
| 3485 | break; |
| 3486 | case OP_NAME: |
| 3487 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3488 | case Load: op = LOAD_NAME; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3489 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3490 | op = STORE_NAME; |
| 3491 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3492 | case Del: op = DELETE_NAME; break; |
| 3493 | case AugLoad: |
| 3494 | case AugStore: |
| 3495 | break; |
| 3496 | case Param: |
| 3497 | default: |
| 3498 | PyErr_SetString(PyExc_SystemError, |
| 3499 | "param invalid for name variable"); |
| 3500 | return 0; |
| 3501 | } |
| 3502 | break; |
| 3503 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | assert(op); |
| 3506 | arg = compiler_add_o(c, dict, mangled); |
| 3507 | Py_DECREF(mangled); |
| 3508 | if (arg < 0) |
| 3509 | return 0; |
| 3510 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | static int |
| 3514 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3517 | int jumpi; |
| 3518 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3519 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | assert(e->kind == BoolOp_kind); |
| 3522 | if (e->v.BoolOp.op == And) |
| 3523 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3524 | else |
| 3525 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3526 | end = compiler_new_block(c); |
| 3527 | if (end == NULL) |
| 3528 | return 0; |
| 3529 | s = e->v.BoolOp.values; |
| 3530 | n = asdl_seq_LEN(s) - 1; |
| 3531 | assert(n >= 0); |
| 3532 | for (i = 0; i < n; ++i) { |
| 3533 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3534 | ADDOP_JABS(c, jumpi, end); |
| 3535 | } |
| 3536 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3537 | compiler_use_next_block(c, end); |
| 3538 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3542 | starunpack_helper(struct compiler *c, asdl_seq *elts, |
| 3543 | int single_op, int inner_op, int outer_op) |
| 3544 | { |
| 3545 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3546 | Py_ssize_t i, nsubitems = 0, nseen = 0; |
| 3547 | for (i = 0; i < n; i++) { |
| 3548 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3549 | if (elt->kind == Starred_kind) { |
| 3550 | if (nseen) { |
| 3551 | ADDOP_I(c, inner_op, nseen); |
| 3552 | nseen = 0; |
| 3553 | nsubitems++; |
| 3554 | } |
| 3555 | VISIT(c, expr, elt->v.Starred.value); |
| 3556 | nsubitems++; |
| 3557 | } |
| 3558 | else { |
| 3559 | VISIT(c, expr, elt); |
| 3560 | nseen++; |
| 3561 | } |
| 3562 | } |
| 3563 | if (nsubitems) { |
| 3564 | if (nseen) { |
| 3565 | ADDOP_I(c, inner_op, nseen); |
| 3566 | nsubitems++; |
| 3567 | } |
| 3568 | ADDOP_I(c, outer_op, nsubitems); |
| 3569 | } |
| 3570 | else |
| 3571 | ADDOP_I(c, single_op, nseen); |
| 3572 | return 1; |
| 3573 | } |
| 3574 | |
| 3575 | static int |
| 3576 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3577 | { |
| 3578 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3579 | Py_ssize_t i; |
| 3580 | int seen_star = 0; |
| 3581 | for (i = 0; i < n; i++) { |
| 3582 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3583 | if (elt->kind == Starred_kind && !seen_star) { |
| 3584 | if ((i >= (1 << 8)) || |
| 3585 | (n-i-1 >= (INT_MAX >> 8))) |
| 3586 | return compiler_error(c, |
| 3587 | "too many expressions in " |
| 3588 | "star-unpacking assignment"); |
| 3589 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3590 | seen_star = 1; |
| 3591 | asdl_seq_SET(elts, i, elt->v.Starred.value); |
| 3592 | } |
| 3593 | else if (elt->kind == Starred_kind) { |
| 3594 | return compiler_error(c, |
| 3595 | "two starred expressions in assignment"); |
| 3596 | } |
| 3597 | } |
| 3598 | if (!seen_star) { |
| 3599 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3600 | } |
| 3601 | VISIT_SEQ(c, expr, elts); |
| 3602 | return 1; |
| 3603 | } |
| 3604 | |
| 3605 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3606 | compiler_list(struct compiler *c, expr_ty e) |
| 3607 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3608 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3609 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3610 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3611 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3612 | else if (e->v.List.ctx == Load) { |
| 3613 | return starunpack_helper(c, elts, |
| 3614 | BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3616 | else |
| 3617 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | static int |
| 3622 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3623 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3624 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3625 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3626 | return assignment_helper(c, elts); |
| 3627 | } |
| 3628 | else if (e->v.Tuple.ctx == Load) { |
| 3629 | return starunpack_helper(c, elts, |
| 3630 | BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK); |
| 3631 | } |
| 3632 | else |
| 3633 | VISIT_SEQ(c, expr, elts); |
| 3634 | return 1; |
| 3635 | } |
| 3636 | |
| 3637 | static int |
| 3638 | compiler_set(struct compiler *c, expr_ty e) |
| 3639 | { |
| 3640 | return starunpack_helper(c, e->v.Set.elts, BUILD_SET, |
| 3641 | BUILD_SET, BUILD_SET_UNPACK); |
| 3642 | } |
| 3643 | |
| 3644 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3645 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3646 | { |
| 3647 | Py_ssize_t i; |
| 3648 | for (i = begin; i < end; i++) { |
| 3649 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3650 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3651 | return 0; |
| 3652 | } |
| 3653 | return 1; |
| 3654 | } |
| 3655 | |
| 3656 | static int |
| 3657 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3658 | { |
| 3659 | Py_ssize_t i, n = end - begin; |
| 3660 | PyObject *keys, *key; |
| 3661 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3662 | for (i = begin; i < end; i++) { |
| 3663 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3664 | } |
| 3665 | keys = PyTuple_New(n); |
| 3666 | if (keys == NULL) { |
| 3667 | return 0; |
| 3668 | } |
| 3669 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3670 | 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] | 3671 | Py_INCREF(key); |
| 3672 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3673 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3674 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3675 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3676 | } |
| 3677 | else { |
| 3678 | for (i = begin; i < end; i++) { |
| 3679 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3680 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3681 | } |
| 3682 | ADDOP_I(c, BUILD_MAP, n); |
| 3683 | } |
| 3684 | return 1; |
| 3685 | } |
| 3686 | |
| 3687 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3688 | compiler_dict(struct compiler *c, expr_ty e) |
| 3689 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3690 | Py_ssize_t i, n, elements; |
| 3691 | int containers; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3692 | int is_unpacking = 0; |
| 3693 | n = asdl_seq_LEN(e->v.Dict.values); |
| 3694 | containers = 0; |
| 3695 | elements = 0; |
| 3696 | for (i = 0; i < n; i++) { |
| 3697 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
| 3698 | if (elements == 0xFFFF || (elements && is_unpacking)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3699 | if (!compiler_subdict(c, e, i - elements, i)) |
| 3700 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3701 | containers++; |
| 3702 | elements = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3703 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3704 | if (is_unpacking) { |
| 3705 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3706 | containers++; |
| 3707 | } |
| 3708 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3709 | elements++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3710 | } |
| 3711 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3712 | if (elements || containers == 0) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3713 | if (!compiler_subdict(c, e, n - elements, n)) |
| 3714 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3715 | containers++; |
| 3716 | } |
| 3717 | /* If there is more than one dict, they need to be merged into a new |
| 3718 | * dict. If there is one dict and it's an unpacking, then it needs |
| 3719 | * to be copied into a new dict." */ |
Serhiy Storchaka | 3d85fae | 2016-11-28 20:56:37 +0200 | [diff] [blame] | 3720 | if (containers > 1 || is_unpacking) { |
| 3721 | ADDOP_I(c, BUILD_MAP_UNPACK, containers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3722 | } |
| 3723 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | static int |
| 3727 | compiler_compare(struct compiler *c, expr_ty e) |
| 3728 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3729 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3730 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3731 | if (!check_compare(c, e)) { |
| 3732 | return 0; |
| 3733 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3735 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3736 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3737 | if (n == 0) { |
| 3738 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 3739 | ADDOP_I(c, COMPARE_OP, |
| 3740 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0)))); |
| 3741 | } |
| 3742 | else { |
| 3743 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3744 | if (cleanup == NULL) |
| 3745 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3746 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3747 | VISIT(c, expr, |
| 3748 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3749 | ADDOP(c, DUP_TOP); |
| 3750 | ADDOP(c, ROT_THREE); |
| 3751 | ADDOP_I(c, COMPARE_OP, |
| 3752 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 3753 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3754 | NEXT_BLOCK(c); |
| 3755 | } |
| 3756 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 3757 | ADDOP_I(c, COMPARE_OP, |
| 3758 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | basicblock *end = compiler_new_block(c); |
| 3760 | if (end == NULL) |
| 3761 | return 0; |
| 3762 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3763 | compiler_use_next_block(c, cleanup); |
| 3764 | ADDOP(c, ROT_TWO); |
| 3765 | ADDOP(c, POP_TOP); |
| 3766 | compiler_use_next_block(c, end); |
| 3767 | } |
| 3768 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3769 | } |
| 3770 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3771 | static PyTypeObject * |
| 3772 | infer_type(expr_ty e) |
| 3773 | { |
| 3774 | switch (e->kind) { |
| 3775 | case Tuple_kind: |
| 3776 | return &PyTuple_Type; |
| 3777 | case List_kind: |
| 3778 | case ListComp_kind: |
| 3779 | return &PyList_Type; |
| 3780 | case Dict_kind: |
| 3781 | case DictComp_kind: |
| 3782 | return &PyDict_Type; |
| 3783 | case Set_kind: |
| 3784 | case SetComp_kind: |
| 3785 | return &PySet_Type; |
| 3786 | case GeneratorExp_kind: |
| 3787 | return &PyGen_Type; |
| 3788 | case Lambda_kind: |
| 3789 | return &PyFunction_Type; |
| 3790 | case JoinedStr_kind: |
| 3791 | case FormattedValue_kind: |
| 3792 | return &PyUnicode_Type; |
| 3793 | case Constant_kind: |
| 3794 | return e->v.Constant.value->ob_type; |
| 3795 | default: |
| 3796 | return NULL; |
| 3797 | } |
| 3798 | } |
| 3799 | |
| 3800 | static int |
| 3801 | check_caller(struct compiler *c, expr_ty e) |
| 3802 | { |
| 3803 | switch (e->kind) { |
| 3804 | case Constant_kind: |
| 3805 | case Tuple_kind: |
| 3806 | case List_kind: |
| 3807 | case ListComp_kind: |
| 3808 | case Dict_kind: |
| 3809 | case DictComp_kind: |
| 3810 | case Set_kind: |
| 3811 | case SetComp_kind: |
| 3812 | case GeneratorExp_kind: |
| 3813 | case JoinedStr_kind: |
| 3814 | case FormattedValue_kind: |
| 3815 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3816 | "perhaps you missed a comma?", |
| 3817 | infer_type(e)->tp_name); |
| 3818 | default: |
| 3819 | return 1; |
| 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | static int |
| 3824 | check_subscripter(struct compiler *c, expr_ty e) |
| 3825 | { |
| 3826 | PyObject *v; |
| 3827 | |
| 3828 | switch (e->kind) { |
| 3829 | case Constant_kind: |
| 3830 | v = e->v.Constant.value; |
| 3831 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3832 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3833 | PyAnySet_Check(v))) |
| 3834 | { |
| 3835 | return 1; |
| 3836 | } |
| 3837 | /* fall through */ |
| 3838 | case Set_kind: |
| 3839 | case SetComp_kind: |
| 3840 | case GeneratorExp_kind: |
| 3841 | case Lambda_kind: |
| 3842 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3843 | "perhaps you missed a comma?", |
| 3844 | infer_type(e)->tp_name); |
| 3845 | default: |
| 3846 | return 1; |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | static int |
| 3851 | check_index(struct compiler *c, expr_ty e, slice_ty s) |
| 3852 | { |
| 3853 | PyObject *v; |
| 3854 | |
| 3855 | if (s->kind != Index_kind) { |
| 3856 | return 1; |
| 3857 | } |
| 3858 | PyTypeObject *index_type = infer_type(s->v.Index.value); |
| 3859 | if (index_type == NULL |
| 3860 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 3861 | || index_type == &PySlice_Type) { |
| 3862 | return 1; |
| 3863 | } |
| 3864 | |
| 3865 | switch (e->kind) { |
| 3866 | case Constant_kind: |
| 3867 | v = e->v.Constant.value; |
| 3868 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 3869 | return 1; |
| 3870 | } |
| 3871 | /* fall through */ |
| 3872 | case Tuple_kind: |
| 3873 | case List_kind: |
| 3874 | case ListComp_kind: |
| 3875 | case JoinedStr_kind: |
| 3876 | case FormattedValue_kind: |
| 3877 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 3878 | "not %.200s; " |
| 3879 | "perhaps you missed a comma?", |
| 3880 | infer_type(e)->tp_name, |
| 3881 | index_type->tp_name); |
| 3882 | default: |
| 3883 | return 1; |
| 3884 | } |
| 3885 | } |
| 3886 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 3887 | // 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] | 3888 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3889 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 3890 | { |
| 3891 | Py_ssize_t argsl, i; |
| 3892 | expr_ty meth = e->v.Call.func; |
| 3893 | asdl_seq *args = e->v.Call.args; |
| 3894 | |
| 3895 | /* Check that the call node is an attribute access, and that |
| 3896 | the call doesn't have keyword parameters. */ |
| 3897 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 3898 | asdl_seq_LEN(e->v.Call.keywords)) |
| 3899 | return -1; |
| 3900 | |
| 3901 | /* Check that there are no *varargs types of arguments. */ |
| 3902 | argsl = asdl_seq_LEN(args); |
| 3903 | for (i = 0; i < argsl; i++) { |
| 3904 | expr_ty elt = asdl_seq_GET(args, i); |
| 3905 | if (elt->kind == Starred_kind) { |
| 3906 | return -1; |
| 3907 | } |
| 3908 | } |
| 3909 | |
| 3910 | /* Alright, we can optimize the code. */ |
| 3911 | VISIT(c, expr, meth->v.Attribute.value); |
| 3912 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 3913 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 3914 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 3915 | return 1; |
| 3916 | } |
| 3917 | |
| 3918 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3919 | compiler_call(struct compiler *c, expr_ty e) |
| 3920 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 3921 | int ret = maybe_optimize_method_call(c, e); |
| 3922 | if (ret >= 0) { |
| 3923 | return ret; |
| 3924 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3925 | if (!check_caller(c, e->v.Call.func)) { |
| 3926 | return 0; |
| 3927 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3928 | VISIT(c, expr, e->v.Call.func); |
| 3929 | return compiler_call_helper(c, 0, |
| 3930 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3931 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3934 | static int |
| 3935 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 3936 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3937 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 3938 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 3939 | 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] | 3940 | return 1; |
| 3941 | } |
| 3942 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3943 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3944 | static int |
| 3945 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 3946 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3947 | /* Our oparg encodes 2 pieces of information: the conversion |
| 3948 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3949 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3950 | Convert the conversion char to 3 bits: |
| 3951 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3952 | !s : 001 0x1 FVC_STR |
| 3953 | !r : 010 0x2 FVC_REPR |
| 3954 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3955 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3956 | next bit is whether or not we have a format spec: |
| 3957 | yes : 100 0x4 |
| 3958 | no : 000 0x0 |
| 3959 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3960 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3961 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3962 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3963 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3964 | if (e->v.FormattedValue.expr_text) { |
| 3965 | /* Push the text of the expression (which already has the '=' in |
| 3966 | it. */ |
| 3967 | ADDOP_LOAD_CONST(c, e->v.FormattedValue.expr_text); |
| 3968 | } |
| 3969 | |
| 3970 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3971 | VISIT(c, expr, e->v.FormattedValue.value); |
| 3972 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3973 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3974 | case 's': oparg = FVC_STR; break; |
| 3975 | case 'r': oparg = FVC_REPR; break; |
| 3976 | case 'a': oparg = FVC_ASCII; break; |
| 3977 | case -1: oparg = FVC_NONE; break; |
| 3978 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3979 | PyErr_Format(PyExc_SystemError, |
| 3980 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3981 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3982 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3983 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3984 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3985 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3986 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3987 | } |
| 3988 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3989 | /* And push our opcode and oparg */ |
| 3990 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3991 | |
| 3992 | /* If we have expr_text, join the 2 strings on the stack. */ |
| 3993 | if (e->v.FormattedValue.expr_text) { |
| 3994 | ADDOP_I(c, BUILD_STRING, 2); |
| 3995 | } |
| 3996 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3997 | return 1; |
| 3998 | } |
| 3999 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4000 | static int |
| 4001 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 4002 | { |
| 4003 | Py_ssize_t i, n = end - begin; |
| 4004 | keyword_ty kw; |
| 4005 | PyObject *keys, *key; |
| 4006 | assert(n > 0); |
| 4007 | if (n > 1) { |
| 4008 | for (i = begin; i < end; i++) { |
| 4009 | kw = asdl_seq_GET(keywords, i); |
| 4010 | VISIT(c, expr, kw->value); |
| 4011 | } |
| 4012 | keys = PyTuple_New(n); |
| 4013 | if (keys == NULL) { |
| 4014 | return 0; |
| 4015 | } |
| 4016 | for (i = begin; i < end; i++) { |
| 4017 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4018 | Py_INCREF(key); |
| 4019 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4020 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4021 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4022 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4023 | } |
| 4024 | else { |
| 4025 | /* a for loop only executes once */ |
| 4026 | for (i = begin; i < end; i++) { |
| 4027 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4028 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4029 | VISIT(c, expr, kw->value); |
| 4030 | } |
| 4031 | ADDOP_I(c, BUILD_MAP, n); |
| 4032 | } |
| 4033 | return 1; |
| 4034 | } |
| 4035 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4036 | /* shared code between compiler_call and compiler_class */ |
| 4037 | static int |
| 4038 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4039 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4040 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4041 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4042 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4043 | Py_ssize_t i, nseen, nelts, nkwelts; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4044 | int mustdictunpack = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4045 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4046 | /* the number of tuples and dictionaries on the stack */ |
| 4047 | Py_ssize_t nsubargs = 0, nsubkwargs = 0; |
| 4048 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4049 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4050 | nkwelts = asdl_seq_LEN(keywords); |
| 4051 | |
| 4052 | for (i = 0; i < nkwelts; i++) { |
| 4053 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4054 | if (kw->arg == NULL) { |
| 4055 | mustdictunpack = 1; |
| 4056 | break; |
| 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | nseen = n; /* the number of positional arguments on the stack */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4061 | for (i = 0; i < nelts; i++) { |
| 4062 | expr_ty elt = asdl_seq_GET(args, i); |
| 4063 | if (elt->kind == Starred_kind) { |
| 4064 | /* A star-arg. If we've seen positional arguments, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4065 | pack the positional arguments into a tuple. */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4066 | if (nseen) { |
| 4067 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4068 | nseen = 0; |
| 4069 | nsubargs++; |
| 4070 | } |
| 4071 | VISIT(c, expr, elt->v.Starred.value); |
| 4072 | nsubargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4073 | } |
| 4074 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4075 | VISIT(c, expr, elt); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4076 | nseen++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4077 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4078 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4079 | |
| 4080 | /* Same dance again for keyword arguments */ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4081 | if (nsubargs || mustdictunpack) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4082 | if (nseen) { |
| 4083 | /* Pack up any trailing positional arguments. */ |
| 4084 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4085 | nsubargs++; |
| 4086 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4087 | if (nsubargs > 1) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4088 | /* If we ended up with more than one stararg, we need |
| 4089 | to concatenate them into a single sequence. */ |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 4090 | ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4091 | } |
| 4092 | else if (nsubargs == 0) { |
| 4093 | ADDOP_I(c, BUILD_TUPLE, 0); |
| 4094 | } |
| 4095 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4096 | for (i = 0; i < nkwelts; i++) { |
| 4097 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4098 | if (kw->arg == NULL) { |
| 4099 | /* A keyword argument unpacking. */ |
| 4100 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4101 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) |
| 4102 | return 0; |
| 4103 | nsubkwargs++; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4104 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4105 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4106 | VISIT(c, expr, kw->value); |
| 4107 | nsubkwargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4108 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4109 | else { |
| 4110 | nseen++; |
| 4111 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4112 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4113 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4114 | /* Pack up any trailing keyword arguments. */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4115 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4116 | return 0; |
| 4117 | nsubkwargs++; |
| 4118 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4119 | if (nsubkwargs > 1) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4120 | /* Pack it all up */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4121 | ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4122 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4123 | ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); |
| 4124 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4125 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4126 | else if (nkwelts) { |
| 4127 | PyObject *names; |
| 4128 | VISIT_SEQ(c, keyword, keywords); |
| 4129 | names = PyTuple_New(nkwelts); |
| 4130 | if (names == NULL) { |
| 4131 | return 0; |
| 4132 | } |
| 4133 | for (i = 0; i < nkwelts; i++) { |
| 4134 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4135 | Py_INCREF(kw->arg); |
| 4136 | PyTuple_SET_ITEM(names, i, kw->arg); |
| 4137 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4138 | ADDOP_LOAD_CONST_NEW(c, names); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4139 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4140 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4141 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4142 | else { |
| 4143 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4144 | return 1; |
| 4145 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4146 | } |
| 4147 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4148 | |
| 4149 | /* List and set comprehensions and generator expressions work by creating a |
| 4150 | nested function to perform the actual iteration. This means that the |
| 4151 | iteration variables don't leak into the current scope. |
| 4152 | The defined function is called immediately following its definition, with the |
| 4153 | result of that call being the result of the expression. |
| 4154 | The LC/SC version returns the populated container, while the GE version is |
| 4155 | flagged in symtable.c as a generator, so it returns the generator object |
| 4156 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4157 | |
| 4158 | Possible cleanups: |
| 4159 | - iterate over the generator sequence instead of using recursion |
| 4160 | */ |
| 4161 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4162 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4163 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4164 | compiler_comprehension_generator(struct compiler *c, |
| 4165 | asdl_seq *generators, int gen_index, |
| 4166 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4167 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4168 | comprehension_ty gen; |
| 4169 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4170 | if (gen->is_async) { |
| 4171 | return compiler_async_comprehension_generator( |
| 4172 | c, generators, gen_index, elt, val, type); |
| 4173 | } else { |
| 4174 | return compiler_sync_comprehension_generator( |
| 4175 | c, generators, gen_index, elt, val, type); |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | static int |
| 4180 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4181 | asdl_seq *generators, int gen_index, |
| 4182 | expr_ty elt, expr_ty val, int type) |
| 4183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | /* generate code for the iterator, then each of the ifs, |
| 4185 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4186 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4187 | comprehension_ty gen; |
| 4188 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4189 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4190 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4191 | start = compiler_new_block(c); |
| 4192 | skip = compiler_new_block(c); |
| 4193 | if_cleanup = compiler_new_block(c); |
| 4194 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4197 | anchor == NULL) |
| 4198 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4199 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4200 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4201 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | if (gen_index == 0) { |
| 4203 | /* Receive outermost iter as an implicit argument */ |
| 4204 | c->u->u_argcount = 1; |
| 4205 | ADDOP_I(c, LOAD_FAST, 0); |
| 4206 | } |
| 4207 | else { |
| 4208 | /* Sub-iter - calculate on the fly */ |
| 4209 | VISIT(c, expr, gen->iter); |
| 4210 | ADDOP(c, GET_ITER); |
| 4211 | } |
| 4212 | compiler_use_next_block(c, start); |
| 4213 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4214 | NEXT_BLOCK(c); |
| 4215 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4217 | /* XXX this needs to be cleaned up...a lot! */ |
| 4218 | n = asdl_seq_LEN(gen->ifs); |
| 4219 | for (i = 0; i < n; i++) { |
| 4220 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4221 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4222 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | NEXT_BLOCK(c); |
| 4224 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4226 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4227 | if (!compiler_comprehension_generator(c, |
| 4228 | generators, gen_index, |
| 4229 | elt, val, type)) |
| 4230 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | /* only append after the last for generator */ |
| 4233 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4234 | /* comprehension specific code */ |
| 4235 | switch (type) { |
| 4236 | case COMP_GENEXP: |
| 4237 | VISIT(c, expr, elt); |
| 4238 | ADDOP(c, YIELD_VALUE); |
| 4239 | ADDOP(c, POP_TOP); |
| 4240 | break; |
| 4241 | case COMP_LISTCOMP: |
| 4242 | VISIT(c, expr, elt); |
| 4243 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4244 | break; |
| 4245 | case COMP_SETCOMP: |
| 4246 | VISIT(c, expr, elt); |
| 4247 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4248 | break; |
| 4249 | case COMP_DICTCOMP: |
| 4250 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4251 | the same. */ |
| 4252 | VISIT(c, expr, val); |
| 4253 | VISIT(c, expr, elt); |
| 4254 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4255 | break; |
| 4256 | default: |
| 4257 | return 0; |
| 4258 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4260 | compiler_use_next_block(c, skip); |
| 4261 | } |
| 4262 | compiler_use_next_block(c, if_cleanup); |
| 4263 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4264 | compiler_use_next_block(c, anchor); |
| 4265 | |
| 4266 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
| 4269 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4270 | compiler_async_comprehension_generator(struct compiler *c, |
| 4271 | asdl_seq *generators, int gen_index, |
| 4272 | expr_ty elt, expr_ty val, int type) |
| 4273 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4274 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4275 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4276 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4277 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4278 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4279 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4280 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4281 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4282 | return 0; |
| 4283 | } |
| 4284 | |
| 4285 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4286 | |
| 4287 | if (gen_index == 0) { |
| 4288 | /* Receive outermost iter as an implicit argument */ |
| 4289 | c->u->u_argcount = 1; |
| 4290 | ADDOP_I(c, LOAD_FAST, 0); |
| 4291 | } |
| 4292 | else { |
| 4293 | /* Sub-iter - calculate on the fly */ |
| 4294 | VISIT(c, expr, gen->iter); |
| 4295 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4296 | } |
| 4297 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4298 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4299 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4300 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4301 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4302 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4303 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4304 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4305 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4306 | |
| 4307 | n = asdl_seq_LEN(gen->ifs); |
| 4308 | for (i = 0; i < n; i++) { |
| 4309 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4310 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4311 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4312 | NEXT_BLOCK(c); |
| 4313 | } |
| 4314 | |
| 4315 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4316 | if (!compiler_comprehension_generator(c, |
| 4317 | generators, gen_index, |
| 4318 | elt, val, type)) |
| 4319 | return 0; |
| 4320 | |
| 4321 | /* only append after the last for generator */ |
| 4322 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4323 | /* comprehension specific code */ |
| 4324 | switch (type) { |
| 4325 | case COMP_GENEXP: |
| 4326 | VISIT(c, expr, elt); |
| 4327 | ADDOP(c, YIELD_VALUE); |
| 4328 | ADDOP(c, POP_TOP); |
| 4329 | break; |
| 4330 | case COMP_LISTCOMP: |
| 4331 | VISIT(c, expr, elt); |
| 4332 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4333 | break; |
| 4334 | case COMP_SETCOMP: |
| 4335 | VISIT(c, expr, elt); |
| 4336 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4337 | break; |
| 4338 | case COMP_DICTCOMP: |
| 4339 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4340 | the same. */ |
| 4341 | VISIT(c, expr, val); |
| 4342 | VISIT(c, expr, elt); |
| 4343 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4344 | break; |
| 4345 | default: |
| 4346 | return 0; |
| 4347 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4348 | } |
| 4349 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4350 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4351 | |
| 4352 | compiler_use_next_block(c, except); |
| 4353 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4354 | |
| 4355 | return 1; |
| 4356 | } |
| 4357 | |
| 4358 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4359 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4360 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4361 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4363 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4364 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4365 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4366 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4367 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4368 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4369 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4370 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4371 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4372 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4374 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4378 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4379 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4380 | compiler_error(c, "asynchronous comprehension outside of " |
| 4381 | "an asynchronous function"); |
| 4382 | goto error_in_scope; |
| 4383 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | if (type != COMP_GENEXP) { |
| 4386 | int op; |
| 4387 | switch (type) { |
| 4388 | case COMP_LISTCOMP: |
| 4389 | op = BUILD_LIST; |
| 4390 | break; |
| 4391 | case COMP_SETCOMP: |
| 4392 | op = BUILD_SET; |
| 4393 | break; |
| 4394 | case COMP_DICTCOMP: |
| 4395 | op = BUILD_MAP; |
| 4396 | break; |
| 4397 | default: |
| 4398 | PyErr_Format(PyExc_SystemError, |
| 4399 | "unknown comprehension type %d", type); |
| 4400 | goto error_in_scope; |
| 4401 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | ADDOP_I(c, op, 0); |
| 4404 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4406 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 4407 | val, type)) |
| 4408 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4409 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | if (type != COMP_GENEXP) { |
| 4411 | ADDOP(c, RETURN_VALUE); |
| 4412 | } |
| 4413 | |
| 4414 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4415 | qualname = c->u->u_qualname; |
| 4416 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4417 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4418 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4419 | goto error; |
| 4420 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4421 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4422 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4423 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4424 | Py_DECREF(co); |
| 4425 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4426 | VISIT(c, expr, outermost->iter); |
| 4427 | |
| 4428 | if (outermost->is_async) { |
| 4429 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4430 | } else { |
| 4431 | ADDOP(c, GET_ITER); |
| 4432 | } |
| 4433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4434 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4435 | |
| 4436 | if (is_async_generator && type != COMP_GENEXP) { |
| 4437 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4438 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4439 | ADDOP(c, YIELD_FROM); |
| 4440 | } |
| 4441 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4442 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4443 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4444 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4445 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4446 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4447 | Py_XDECREF(co); |
| 4448 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4452 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | static identifier name; |
| 4455 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4456 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | if (!name) |
| 4458 | return 0; |
| 4459 | } |
| 4460 | assert(e->kind == GeneratorExp_kind); |
| 4461 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4462 | e->v.GeneratorExp.generators, |
| 4463 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4464 | } |
| 4465 | |
| 4466 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4467 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4468 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4469 | static identifier name; |
| 4470 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4471 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4472 | if (!name) |
| 4473 | return 0; |
| 4474 | } |
| 4475 | assert(e->kind == ListComp_kind); |
| 4476 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4477 | e->v.ListComp.generators, |
| 4478 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4479 | } |
| 4480 | |
| 4481 | static int |
| 4482 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4483 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4484 | static identifier name; |
| 4485 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4486 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | if (!name) |
| 4488 | return 0; |
| 4489 | } |
| 4490 | assert(e->kind == SetComp_kind); |
| 4491 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4492 | e->v.SetComp.generators, |
| 4493 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
| 4496 | |
| 4497 | static int |
| 4498 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4499 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | static identifier name; |
| 4501 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4502 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | if (!name) |
| 4504 | return 0; |
| 4505 | } |
| 4506 | assert(e->kind == DictComp_kind); |
| 4507 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4508 | e->v.DictComp.generators, |
| 4509 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
| 4512 | |
| 4513 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4514 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | VISIT(c, expr, k->value); |
| 4517 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4520 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4521 | whether they are true or false. |
| 4522 | |
| 4523 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4524 | */ |
| 4525 | |
| 4526 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4527 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4528 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4529 | if (e->kind == Constant_kind) { |
| 4530 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4531 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4532 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4535 | |
| 4536 | /* |
| 4537 | Implements the async with statement. |
| 4538 | |
| 4539 | The semantics outlined in that PEP are as follows: |
| 4540 | |
| 4541 | async with EXPR as VAR: |
| 4542 | BLOCK |
| 4543 | |
| 4544 | It is implemented roughly as: |
| 4545 | |
| 4546 | context = EXPR |
| 4547 | exit = context.__aexit__ # not calling it |
| 4548 | value = await context.__aenter__() |
| 4549 | try: |
| 4550 | VAR = value # if VAR present in the syntax |
| 4551 | BLOCK |
| 4552 | finally: |
| 4553 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4554 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4555 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4556 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4557 | if not (await exit(*exc)): |
| 4558 | raise |
| 4559 | */ |
| 4560 | static int |
| 4561 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4562 | { |
| 4563 | basicblock *block, *finally; |
| 4564 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4565 | |
| 4566 | assert(s->kind == AsyncWith_kind); |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4567 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
| 4568 | return compiler_error(c, "'async with' outside async function"); |
| 4569 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4570 | |
| 4571 | block = compiler_new_block(c); |
| 4572 | finally = compiler_new_block(c); |
| 4573 | if (!block || !finally) |
| 4574 | return 0; |
| 4575 | |
| 4576 | /* Evaluate EXPR */ |
| 4577 | VISIT(c, expr, item->context_expr); |
| 4578 | |
| 4579 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4580 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4581 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4582 | ADDOP(c, YIELD_FROM); |
| 4583 | |
| 4584 | ADDOP_JREL(c, SETUP_ASYNC_WITH, finally); |
| 4585 | |
| 4586 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4587 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4588 | if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4589 | return 0; |
| 4590 | } |
| 4591 | |
| 4592 | if (item->optional_vars) { |
| 4593 | VISIT(c, expr, item->optional_vars); |
| 4594 | } |
| 4595 | else { |
| 4596 | /* Discard result from context.__aenter__() */ |
| 4597 | ADDOP(c, POP_TOP); |
| 4598 | } |
| 4599 | |
| 4600 | pos++; |
| 4601 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4602 | /* BLOCK code */ |
| 4603 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4604 | else if (!compiler_async_with(c, s, pos)) |
| 4605 | return 0; |
| 4606 | |
| 4607 | /* End of try block; start the finally block */ |
| 4608 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4609 | ADDOP(c, BEGIN_FINALLY); |
| 4610 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4611 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4612 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4613 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4614 | return 0; |
| 4615 | |
| 4616 | /* Finally block starts; context.__exit__ is on the stack under |
| 4617 | the exception or return information. Just issue our magic |
| 4618 | opcode. */ |
| 4619 | ADDOP(c, WITH_CLEANUP_START); |
| 4620 | |
| 4621 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4622 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4623 | ADDOP(c, YIELD_FROM); |
| 4624 | |
| 4625 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 4626 | |
| 4627 | /* Finally block ends. */ |
| 4628 | ADDOP(c, END_FINALLY); |
| 4629 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4630 | return 1; |
| 4631 | } |
| 4632 | |
| 4633 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4634 | /* |
| 4635 | Implements the with statement from PEP 343. |
| 4636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4638 | |
| 4639 | with EXPR as VAR: |
| 4640 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4641 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4642 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4644 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4645 | exit = context.__exit__ # not calling it |
| 4646 | value = context.__enter__() |
| 4647 | try: |
| 4648 | VAR = value # if VAR present in the syntax |
| 4649 | BLOCK |
| 4650 | finally: |
| 4651 | if an exception was raised: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4652 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4653 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4654 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4655 | exit(*exc) |
| 4656 | */ |
| 4657 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4658 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4659 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4660 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4661 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4662 | |
| 4663 | assert(s->kind == With_kind); |
| 4664 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4665 | block = compiler_new_block(c); |
| 4666 | finally = compiler_new_block(c); |
| 4667 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4668 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4669 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4670 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4671 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4672 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4673 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4674 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4675 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4676 | if (!compiler_push_fblock(c, WITH, block, finally)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4677 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4678 | } |
| 4679 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4680 | if (item->optional_vars) { |
| 4681 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4682 | } |
| 4683 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4684 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4685 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4686 | } |
| 4687 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4688 | pos++; |
| 4689 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4690 | /* BLOCK code */ |
| 4691 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4692 | else if (!compiler_with(c, s, pos)) |
| 4693 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4694 | |
| 4695 | /* End of try block; start the finally block */ |
| 4696 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4697 | ADDOP(c, BEGIN_FINALLY); |
| 4698 | compiler_pop_fblock(c, WITH, block); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4699 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4700 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4701 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4702 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4703 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 4704 | /* Finally block starts; context.__exit__ is on the stack under |
| 4705 | the exception or return information. Just issue our magic |
| 4706 | opcode. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4707 | ADDOP(c, WITH_CLEANUP_START); |
| 4708 | ADDOP(c, WITH_CLEANUP_FINISH); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4709 | |
| 4710 | /* Finally block ends. */ |
| 4711 | ADDOP(c, END_FINALLY); |
| 4712 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4713 | return 1; |
| 4714 | } |
| 4715 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4716 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4717 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4719 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4720 | case NamedExpr_kind: |
| 4721 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4722 | ADDOP(c, DUP_TOP); |
| 4723 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4724 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4725 | case BoolOp_kind: |
| 4726 | return compiler_boolop(c, e); |
| 4727 | case BinOp_kind: |
| 4728 | VISIT(c, expr, e->v.BinOp.left); |
| 4729 | VISIT(c, expr, e->v.BinOp.right); |
| 4730 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 4731 | break; |
| 4732 | case UnaryOp_kind: |
| 4733 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4734 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4735 | break; |
| 4736 | case Lambda_kind: |
| 4737 | return compiler_lambda(c, e); |
| 4738 | case IfExp_kind: |
| 4739 | return compiler_ifexp(c, e); |
| 4740 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4741 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4742 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4743 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4744 | case GeneratorExp_kind: |
| 4745 | return compiler_genexp(c, e); |
| 4746 | case ListComp_kind: |
| 4747 | return compiler_listcomp(c, e); |
| 4748 | case SetComp_kind: |
| 4749 | return compiler_setcomp(c, e); |
| 4750 | case DictComp_kind: |
| 4751 | return compiler_dictcomp(c, e); |
| 4752 | case Yield_kind: |
| 4753 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4754 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4755 | if (e->v.Yield.value) { |
| 4756 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4757 | } |
| 4758 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4759 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4760 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4761 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4763 | case YieldFrom_kind: |
| 4764 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4765 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4766 | |
| 4767 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4768 | return compiler_error(c, "'yield from' inside async function"); |
| 4769 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4770 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4771 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4772 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4773 | ADDOP(c, YIELD_FROM); |
| 4774 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4775 | case Await_kind: |
| 4776 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4777 | return compiler_error(c, "'await' outside function"); |
| 4778 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4779 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
| 4780 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4781 | return compiler_error(c, "'await' outside async function"); |
| 4782 | |
| 4783 | VISIT(c, expr, e->v.Await.value); |
| 4784 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4785 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4786 | ADDOP(c, YIELD_FROM); |
| 4787 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4788 | case Compare_kind: |
| 4789 | return compiler_compare(c, e); |
| 4790 | case Call_kind: |
| 4791 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4792 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4793 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4794 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4795 | case JoinedStr_kind: |
| 4796 | return compiler_joined_str(c, e); |
| 4797 | case FormattedValue_kind: |
| 4798 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4799 | /* The following exprs can be assignment targets. */ |
| 4800 | case Attribute_kind: |
| 4801 | if (e->v.Attribute.ctx != AugStore) |
| 4802 | VISIT(c, expr, e->v.Attribute.value); |
| 4803 | switch (e->v.Attribute.ctx) { |
| 4804 | case AugLoad: |
| 4805 | ADDOP(c, DUP_TOP); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4806 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4807 | case Load: |
| 4808 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4809 | break; |
| 4810 | case AugStore: |
| 4811 | ADDOP(c, ROT_TWO); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4812 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4813 | case Store: |
| 4814 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4815 | break; |
| 4816 | case Del: |
| 4817 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4818 | break; |
| 4819 | case Param: |
| 4820 | default: |
| 4821 | PyErr_SetString(PyExc_SystemError, |
| 4822 | "param invalid in attribute expression"); |
| 4823 | return 0; |
| 4824 | } |
| 4825 | break; |
| 4826 | case Subscript_kind: |
| 4827 | switch (e->v.Subscript.ctx) { |
| 4828 | case AugLoad: |
| 4829 | VISIT(c, expr, e->v.Subscript.value); |
| 4830 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 4831 | break; |
| 4832 | case Load: |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4833 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 4834 | return 0; |
| 4835 | } |
| 4836 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 4837 | return 0; |
| 4838 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4839 | VISIT(c, expr, e->v.Subscript.value); |
| 4840 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 4841 | break; |
| 4842 | case AugStore: |
| 4843 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 4844 | break; |
| 4845 | case Store: |
| 4846 | VISIT(c, expr, e->v.Subscript.value); |
| 4847 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 4848 | break; |
| 4849 | case Del: |
| 4850 | VISIT(c, expr, e->v.Subscript.value); |
| 4851 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 4852 | break; |
| 4853 | case Param: |
| 4854 | default: |
| 4855 | PyErr_SetString(PyExc_SystemError, |
| 4856 | "param invalid in subscript expression"); |
| 4857 | return 0; |
| 4858 | } |
| 4859 | break; |
| 4860 | case Starred_kind: |
| 4861 | switch (e->v.Starred.ctx) { |
| 4862 | case Store: |
| 4863 | /* In all legitimate cases, the Starred node was already replaced |
| 4864 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 4865 | return compiler_error(c, |
| 4866 | "starred assignment target must be in a list or tuple"); |
| 4867 | default: |
| 4868 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4869 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4870 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4871 | case Name_kind: |
| 4872 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 4873 | /* child nodes of List and Tuple will have expr_context set */ |
| 4874 | case List_kind: |
| 4875 | return compiler_list(c, e); |
| 4876 | case Tuple_kind: |
| 4877 | return compiler_tuple(c, e); |
| 4878 | } |
| 4879 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4880 | } |
| 4881 | |
| 4882 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4883 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 4884 | { |
| 4885 | /* If expr e has a different line number than the last expr/stmt, |
| 4886 | set a new line number for the next instruction. |
| 4887 | */ |
| 4888 | int old_lineno = c->u->u_lineno; |
| 4889 | int old_col_offset = c->u->u_col_offset; |
| 4890 | if (e->lineno != c->u->u_lineno) { |
| 4891 | c->u->u_lineno = e->lineno; |
| 4892 | c->u->u_lineno_set = 0; |
| 4893 | } |
| 4894 | /* Updating the column offset is always harmless. */ |
| 4895 | c->u->u_col_offset = e->col_offset; |
| 4896 | |
| 4897 | int res = compiler_visit_expr1(c, e); |
| 4898 | |
| 4899 | if (old_lineno != c->u->u_lineno) { |
| 4900 | c->u->u_lineno = old_lineno; |
| 4901 | c->u->u_lineno_set = 0; |
| 4902 | } |
| 4903 | c->u->u_col_offset = old_col_offset; |
| 4904 | return res; |
| 4905 | } |
| 4906 | |
| 4907 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4908 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 4909 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | expr_ty e = s->v.AugAssign.target; |
| 4911 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | switch (e->kind) { |
| 4916 | case Attribute_kind: |
| 4917 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4918 | AugLoad, e->lineno, e->col_offset, |
| 4919 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4920 | if (auge == NULL) |
| 4921 | return 0; |
| 4922 | VISIT(c, expr, auge); |
| 4923 | VISIT(c, expr, s->v.AugAssign.value); |
| 4924 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4925 | auge->v.Attribute.ctx = AugStore; |
| 4926 | VISIT(c, expr, auge); |
| 4927 | break; |
| 4928 | case Subscript_kind: |
| 4929 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4930 | AugLoad, e->lineno, e->col_offset, |
| 4931 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4932 | if (auge == NULL) |
| 4933 | return 0; |
| 4934 | VISIT(c, expr, auge); |
| 4935 | VISIT(c, expr, s->v.AugAssign.value); |
| 4936 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4937 | auge->v.Subscript.ctx = AugStore; |
| 4938 | VISIT(c, expr, auge); |
| 4939 | break; |
| 4940 | case Name_kind: |
| 4941 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 4942 | return 0; |
| 4943 | VISIT(c, expr, s->v.AugAssign.value); |
| 4944 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4945 | return compiler_nameop(c, e->v.Name.id, Store); |
| 4946 | default: |
| 4947 | PyErr_Format(PyExc_SystemError, |
| 4948 | "invalid node type (%d) for augmented assignment", |
| 4949 | e->kind); |
| 4950 | return 0; |
| 4951 | } |
| 4952 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 4956 | check_ann_expr(struct compiler *c, expr_ty e) |
| 4957 | { |
| 4958 | VISIT(c, expr, e); |
| 4959 | ADDOP(c, POP_TOP); |
| 4960 | return 1; |
| 4961 | } |
| 4962 | |
| 4963 | static int |
| 4964 | check_annotation(struct compiler *c, stmt_ty s) |
| 4965 | { |
| 4966 | /* Annotations are only evaluated in a module or class. */ |
| 4967 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 4968 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 4969 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 4970 | } |
| 4971 | return 1; |
| 4972 | } |
| 4973 | |
| 4974 | static int |
| 4975 | check_ann_slice(struct compiler *c, slice_ty sl) |
| 4976 | { |
| 4977 | switch(sl->kind) { |
| 4978 | case Index_kind: |
| 4979 | return check_ann_expr(c, sl->v.Index.value); |
| 4980 | case Slice_kind: |
| 4981 | if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { |
| 4982 | return 0; |
| 4983 | } |
| 4984 | if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { |
| 4985 | return 0; |
| 4986 | } |
| 4987 | if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { |
| 4988 | return 0; |
| 4989 | } |
| 4990 | break; |
| 4991 | default: |
| 4992 | PyErr_SetString(PyExc_SystemError, |
| 4993 | "unexpected slice kind"); |
| 4994 | return 0; |
| 4995 | } |
| 4996 | return 1; |
| 4997 | } |
| 4998 | |
| 4999 | static int |
| 5000 | check_ann_subscr(struct compiler *c, slice_ty sl) |
| 5001 | { |
| 5002 | /* We check that everything in a subscript is defined at runtime. */ |
| 5003 | Py_ssize_t i, n; |
| 5004 | |
| 5005 | switch (sl->kind) { |
| 5006 | case Index_kind: |
| 5007 | case Slice_kind: |
| 5008 | if (!check_ann_slice(c, sl)) { |
| 5009 | return 0; |
| 5010 | } |
| 5011 | break; |
| 5012 | case ExtSlice_kind: |
| 5013 | n = asdl_seq_LEN(sl->v.ExtSlice.dims); |
| 5014 | for (i = 0; i < n; i++) { |
| 5015 | slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i); |
| 5016 | switch (subsl->kind) { |
| 5017 | case Index_kind: |
| 5018 | case Slice_kind: |
| 5019 | if (!check_ann_slice(c, subsl)) { |
| 5020 | return 0; |
| 5021 | } |
| 5022 | break; |
| 5023 | case ExtSlice_kind: |
| 5024 | default: |
| 5025 | PyErr_SetString(PyExc_SystemError, |
| 5026 | "extended slice invalid in nested slice"); |
| 5027 | return 0; |
| 5028 | } |
| 5029 | } |
| 5030 | break; |
| 5031 | default: |
| 5032 | PyErr_Format(PyExc_SystemError, |
| 5033 | "invalid subscript kind %d", sl->kind); |
| 5034 | return 0; |
| 5035 | } |
| 5036 | return 1; |
| 5037 | } |
| 5038 | |
| 5039 | static int |
| 5040 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5041 | { |
| 5042 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5043 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5044 | |
| 5045 | assert(s->kind == AnnAssign_kind); |
| 5046 | |
| 5047 | /* We perform the actual assignment first. */ |
| 5048 | if (s->v.AnnAssign.value) { |
| 5049 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5050 | VISIT(c, expr, targ); |
| 5051 | } |
| 5052 | switch (targ->kind) { |
| 5053 | case Name_kind: |
| 5054 | /* If we have a simple name in a module or class, store annotation. */ |
| 5055 | if (s->v.AnnAssign.simple && |
| 5056 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5057 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5058 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5059 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5060 | } |
| 5061 | else { |
| 5062 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5063 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5064 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5065 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5066 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5067 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5068 | } |
| 5069 | break; |
| 5070 | case Attribute_kind: |
| 5071 | if (!s->v.AnnAssign.value && |
| 5072 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5073 | return 0; |
| 5074 | } |
| 5075 | break; |
| 5076 | case Subscript_kind: |
| 5077 | if (!s->v.AnnAssign.value && |
| 5078 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5079 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5080 | return 0; |
| 5081 | } |
| 5082 | break; |
| 5083 | default: |
| 5084 | PyErr_Format(PyExc_SystemError, |
| 5085 | "invalid node type (%d) for annotated assignment", |
| 5086 | targ->kind); |
| 5087 | return 0; |
| 5088 | } |
| 5089 | /* Annotation is evaluated last. */ |
| 5090 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5091 | return 0; |
| 5092 | } |
| 5093 | return 1; |
| 5094 | } |
| 5095 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5096 | /* Raises a SyntaxError and returns 0. |
| 5097 | If something goes wrong, a different exception may be raised. |
| 5098 | */ |
| 5099 | |
| 5100 | static int |
| 5101 | compiler_error(struct compiler *c, const char *errstr) |
| 5102 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5103 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5104 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5105 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5106 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5107 | if (!loc) { |
| 5108 | Py_INCREF(Py_None); |
| 5109 | loc = Py_None; |
| 5110 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5111 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5112 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5113 | if (!u) |
| 5114 | goto exit; |
| 5115 | v = Py_BuildValue("(zO)", errstr, u); |
| 5116 | if (!v) |
| 5117 | goto exit; |
| 5118 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5119 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5120 | Py_DECREF(loc); |
| 5121 | Py_XDECREF(u); |
| 5122 | Py_XDECREF(v); |
| 5123 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5124 | } |
| 5125 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5126 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5127 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5128 | and returns 0. |
| 5129 | */ |
| 5130 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5131 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5132 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5133 | va_list vargs; |
| 5134 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5135 | va_start(vargs, format); |
| 5136 | #else |
| 5137 | va_start(vargs); |
| 5138 | #endif |
| 5139 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5140 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5141 | if (msg == NULL) { |
| 5142 | return 0; |
| 5143 | } |
| 5144 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5145 | c->u->u_lineno, NULL, NULL) < 0) |
| 5146 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5147 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5148 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5149 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5150 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5151 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5152 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5153 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5154 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5155 | return 0; |
| 5156 | } |
| 5157 | Py_DECREF(msg); |
| 5158 | return 1; |
| 5159 | } |
| 5160 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5161 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 5163 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5165 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5166 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5167 | /* XXX this code is duplicated */ |
| 5168 | switch (ctx) { |
| 5169 | case AugLoad: /* fall through to Load */ |
| 5170 | case Load: op = BINARY_SUBSCR; break; |
| 5171 | case AugStore:/* fall through to Store */ |
| 5172 | case Store: op = STORE_SUBSCR; break; |
| 5173 | case Del: op = DELETE_SUBSCR; break; |
| 5174 | case Param: |
| 5175 | PyErr_Format(PyExc_SystemError, |
| 5176 | "invalid %s kind %d in subscript\n", |
| 5177 | kind, ctx); |
| 5178 | return 0; |
| 5179 | } |
| 5180 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 5181 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5182 | } |
| 5183 | else if (ctx == AugStore) { |
| 5184 | ADDOP(c, ROT_THREE); |
| 5185 | } |
| 5186 | ADDOP(c, op); |
| 5187 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
| 5190 | static int |
| 5191 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5192 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | int n = 2; |
| 5194 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5196 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5197 | if (s->v.Slice.lower) { |
| 5198 | VISIT(c, expr, s->v.Slice.lower); |
| 5199 | } |
| 5200 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5201 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5202 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | if (s->v.Slice.upper) { |
| 5205 | VISIT(c, expr, s->v.Slice.upper); |
| 5206 | } |
| 5207 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5208 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | } |
| 5210 | |
| 5211 | if (s->v.Slice.step) { |
| 5212 | n++; |
| 5213 | VISIT(c, expr, s->v.Slice.step); |
| 5214 | } |
| 5215 | ADDOP_I(c, BUILD_SLICE, n); |
| 5216 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 5221 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5223 | switch (s->kind) { |
| 5224 | case Slice_kind: |
| 5225 | return compiler_slice(c, s, ctx); |
| 5226 | case Index_kind: |
| 5227 | VISIT(c, expr, s->v.Index.value); |
| 5228 | break; |
| 5229 | case ExtSlice_kind: |
| 5230 | default: |
| 5231 | PyErr_SetString(PyExc_SystemError, |
| 5232 | "extended slice invalid in nested slice"); |
| 5233 | return 0; |
| 5234 | } |
| 5235 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5236 | } |
| 5237 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5238 | static int |
| 5239 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5240 | { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 5241 | const char * kindname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5242 | switch (s->kind) { |
| 5243 | case Index_kind: |
| 5244 | kindname = "index"; |
| 5245 | if (ctx != AugStore) { |
| 5246 | VISIT(c, expr, s->v.Index.value); |
| 5247 | } |
| 5248 | break; |
| 5249 | case Slice_kind: |
| 5250 | kindname = "slice"; |
| 5251 | if (ctx != AugStore) { |
| 5252 | if (!compiler_slice(c, s, ctx)) |
| 5253 | return 0; |
| 5254 | } |
| 5255 | break; |
| 5256 | case ExtSlice_kind: |
| 5257 | kindname = "extended slice"; |
| 5258 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5259 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | for (i = 0; i < n; i++) { |
| 5261 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 5262 | s->v.ExtSlice.dims, i); |
| 5263 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 5264 | return 0; |
| 5265 | } |
| 5266 | ADDOP_I(c, BUILD_TUPLE, n); |
| 5267 | } |
| 5268 | break; |
| 5269 | default: |
| 5270 | PyErr_Format(PyExc_SystemError, |
| 5271 | "invalid subscript kind %d", s->kind); |
| 5272 | return 0; |
| 5273 | } |
| 5274 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5275 | } |
| 5276 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5277 | /* End of the compiler section, beginning of the assembler section */ |
| 5278 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5279 | /* do depth-first search of basic block graph, starting with block. |
| 5280 | post records the block indices in post-order. |
| 5281 | |
| 5282 | XXX must handle implicit jumps from one block to next |
| 5283 | */ |
| 5284 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5285 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5286 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5287 | int a_offset; /* offset into bytecode */ |
| 5288 | int a_nblocks; /* number of reachable blocks */ |
| 5289 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 5290 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5291 | int a_lnotab_off; /* offset into lnotab */ |
| 5292 | int a_lineno; /* last lineno of emitted instruction */ |
| 5293 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5294 | }; |
| 5295 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5296 | static void |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5297 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5298 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5299 | int i, j; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5300 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5301 | /* Get rid of recursion for normal control flow. |
| 5302 | Since the number of blocks is limited, unused space in a_postorder |
| 5303 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5304 | blocks. */ |
| 5305 | for (j = end; b && !b->b_seen; b = b->b_next) { |
| 5306 | b->b_seen = 1; |
| 5307 | assert(a->a_nblocks < j); |
| 5308 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5309 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5310 | while (j < end) { |
| 5311 | b = a->a_postorder[j++]; |
| 5312 | for (i = 0; i < b->b_iused; i++) { |
| 5313 | struct instr *instr = &b->b_instr[i]; |
| 5314 | if (instr->i_jrel || instr->i_jabs) |
| 5315 | dfs(c, instr->i_target, a, j); |
| 5316 | } |
| 5317 | assert(a->a_nblocks < j); |
| 5318 | a->a_postorder[a->a_nblocks++] = b; |
| 5319 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5320 | } |
| 5321 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5322 | Py_LOCAL_INLINE(void) |
| 5323 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5324 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5325 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5326 | if (b->b_startdepth < depth) { |
| 5327 | assert(b->b_startdepth < 0); |
| 5328 | b->b_startdepth = depth; |
| 5329 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5330 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | /* Find the flow path that needs the largest stack. We assume that |
| 5334 | * cycles in the flow graph have no net effect on the stack depth. |
| 5335 | */ |
| 5336 | static int |
| 5337 | stackdepth(struct compiler *c) |
| 5338 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5339 | basicblock *b, *entryblock = NULL; |
| 5340 | basicblock **stack, **sp; |
| 5341 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5342 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5343 | b->b_startdepth = INT_MIN; |
| 5344 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5345 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5346 | } |
| 5347 | if (!entryblock) |
| 5348 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5349 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5350 | if (!stack) { |
| 5351 | PyErr_NoMemory(); |
| 5352 | return -1; |
| 5353 | } |
| 5354 | |
| 5355 | sp = stack; |
| 5356 | stackdepth_push(&sp, entryblock, 0); |
| 5357 | while (sp != stack) { |
| 5358 | b = *--sp; |
| 5359 | int depth = b->b_startdepth; |
| 5360 | assert(depth >= 0); |
| 5361 | basicblock *next = b->b_next; |
| 5362 | for (int i = 0; i < b->b_iused; i++) { |
| 5363 | struct instr *instr = &b->b_instr[i]; |
| 5364 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5365 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5366 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5367 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5368 | } |
| 5369 | int new_depth = depth + effect; |
| 5370 | if (new_depth > maxdepth) { |
| 5371 | maxdepth = new_depth; |
| 5372 | } |
| 5373 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5374 | if (instr->i_jrel || instr->i_jabs) { |
| 5375 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5376 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5377 | int target_depth = depth + effect; |
| 5378 | if (target_depth > maxdepth) { |
| 5379 | maxdepth = target_depth; |
| 5380 | } |
| 5381 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5382 | if (instr->i_opcode == CALL_FINALLY) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5383 | assert(instr->i_target->b_startdepth >= 0); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5384 | assert(instr->i_target->b_startdepth >= target_depth); |
| 5385 | depth = new_depth; |
| 5386 | continue; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5387 | } |
| 5388 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5389 | } |
| 5390 | depth = new_depth; |
| 5391 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5392 | instr->i_opcode == JUMP_FORWARD || |
| 5393 | instr->i_opcode == RETURN_VALUE || |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5394 | instr->i_opcode == RAISE_VARARGS) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5395 | { |
| 5396 | /* remaining code is dead */ |
| 5397 | next = NULL; |
| 5398 | break; |
| 5399 | } |
| 5400 | } |
| 5401 | if (next != NULL) { |
| 5402 | stackdepth_push(&sp, next, depth); |
| 5403 | } |
| 5404 | } |
| 5405 | PyObject_Free(stack); |
| 5406 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
| 5409 | static int |
| 5410 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5411 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5412 | memset(a, 0, sizeof(struct assembler)); |
| 5413 | a->a_lineno = firstlineno; |
| 5414 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5415 | if (!a->a_bytecode) |
| 5416 | return 0; |
| 5417 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5418 | if (!a->a_lnotab) |
| 5419 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5420 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5421 | PyErr_NoMemory(); |
| 5422 | return 0; |
| 5423 | } |
| 5424 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 5425 | sizeof(basicblock *) * nblocks); |
| 5426 | if (!a->a_postorder) { |
| 5427 | PyErr_NoMemory(); |
| 5428 | return 0; |
| 5429 | } |
| 5430 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | static void |
| 5434 | assemble_free(struct assembler *a) |
| 5435 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5436 | Py_XDECREF(a->a_bytecode); |
| 5437 | Py_XDECREF(a->a_lnotab); |
| 5438 | if (a->a_postorder) |
| 5439 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5442 | static int |
| 5443 | blocksize(basicblock *b) |
| 5444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | int i; |
| 5446 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5447 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5448 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5449 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5450 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5451 | } |
| 5452 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5453 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5454 | the instruction's bytecode offset and line number. See |
| 5455 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5456 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5457 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5458 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5460 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5461 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5463 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5464 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5465 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5467 | assert(d_bytecode >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | if(d_bytecode == 0 && d_lineno == 0) |
| 5470 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5472 | if (d_bytecode > 255) { |
| 5473 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5474 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5475 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5476 | if (nbytes >= len) { |
| 5477 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5478 | len = nbytes; |
| 5479 | else if (len <= INT_MAX / 2) |
| 5480 | len *= 2; |
| 5481 | else { |
| 5482 | PyErr_NoMemory(); |
| 5483 | return 0; |
| 5484 | } |
| 5485 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5486 | return 0; |
| 5487 | } |
| 5488 | lnotab = (unsigned char *) |
| 5489 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5490 | for (j = 0; j < ncodes; j++) { |
| 5491 | *lnotab++ = 255; |
| 5492 | *lnotab++ = 0; |
| 5493 | } |
| 5494 | d_bytecode -= ncodes * 255; |
| 5495 | a->a_lnotab_off += ncodes * 2; |
| 5496 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5497 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5498 | |
| 5499 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5500 | int j, nbytes, ncodes, k; |
| 5501 | if (d_lineno < 0) { |
| 5502 | k = -128; |
| 5503 | /* use division on positive numbers */ |
| 5504 | ncodes = (-d_lineno) / 128; |
| 5505 | } |
| 5506 | else { |
| 5507 | k = 127; |
| 5508 | ncodes = d_lineno / 127; |
| 5509 | } |
| 5510 | d_lineno -= ncodes * k; |
| 5511 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5513 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5514 | if (nbytes >= len) { |
| 5515 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5516 | len = nbytes; |
| 5517 | else if (len <= INT_MAX / 2) |
| 5518 | len *= 2; |
| 5519 | else { |
| 5520 | PyErr_NoMemory(); |
| 5521 | return 0; |
| 5522 | } |
| 5523 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5524 | return 0; |
| 5525 | } |
| 5526 | lnotab = (unsigned char *) |
| 5527 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5528 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5529 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5530 | d_bytecode = 0; |
| 5531 | for (j = 1; j < ncodes; j++) { |
| 5532 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5533 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5534 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | a->a_lnotab_off += ncodes * 2; |
| 5536 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5537 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5539 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5540 | if (a->a_lnotab_off + 2 >= len) { |
| 5541 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5542 | return 0; |
| 5543 | } |
| 5544 | lnotab = (unsigned char *) |
| 5545 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5547 | a->a_lnotab_off += 2; |
| 5548 | if (d_bytecode) { |
| 5549 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5550 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5551 | } |
| 5552 | else { /* First line of a block; def stmt, etc. */ |
| 5553 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5554 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | } |
| 5556 | a->a_lineno = i->i_lineno; |
| 5557 | a->a_lineno_off = a->a_offset; |
| 5558 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5561 | /* assemble_emit() |
| 5562 | Extend the bytecode with a new instruction. |
| 5563 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5564 | */ |
| 5565 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5566 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5567 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5568 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5569 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5570 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5571 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5572 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5573 | arg = i->i_oparg; |
| 5574 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5575 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5576 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5577 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | if (len > PY_SSIZE_T_MAX / 2) |
| 5579 | return 0; |
| 5580 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5581 | return 0; |
| 5582 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5583 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5585 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5586 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5587 | } |
| 5588 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5589 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5590 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5591 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5593 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5594 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5596 | /* Compute the size of each block and fixup jump args. |
| 5597 | Replace block pointer with position in bytecode. */ |
| 5598 | do { |
| 5599 | totsize = 0; |
| 5600 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5601 | b = a->a_postorder[i]; |
| 5602 | bsize = blocksize(b); |
| 5603 | b->b_offset = totsize; |
| 5604 | totsize += bsize; |
| 5605 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5606 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5607 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5608 | bsize = b->b_offset; |
| 5609 | for (i = 0; i < b->b_iused; i++) { |
| 5610 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5611 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | /* Relative jumps are computed relative to |
| 5613 | the instruction pointer after fetching |
| 5614 | the jump instruction. |
| 5615 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5616 | bsize += isize; |
| 5617 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5618 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5619 | if (instr->i_jrel) { |
| 5620 | instr->i_oparg -= bsize; |
| 5621 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5622 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5623 | if (instrsize(instr->i_oparg) != isize) { |
| 5624 | extended_arg_recompile = 1; |
| 5625 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5626 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5627 | } |
| 5628 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | /* XXX: This is an awful hack that could hurt performance, but |
| 5631 | on the bright side it should work until we come up |
| 5632 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5634 | The issue is that in the first loop blocksize() is called |
| 5635 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5636 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5637 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5639 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5640 | The only EXTENDED_ARGs that could be popping up are |
| 5641 | ones in jump instructions. So this should converge |
| 5642 | fairly quickly. |
| 5643 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5644 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5645 | } |
| 5646 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5647 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5648 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5649 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5650 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5651 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5653 | tuple = PyTuple_New(size); |
| 5654 | if (tuple == NULL) |
| 5655 | return NULL; |
| 5656 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5657 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5658 | Py_INCREF(k); |
| 5659 | assert((i - offset) < size); |
| 5660 | assert((i - offset) >= 0); |
| 5661 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5662 | } |
| 5663 | return tuple; |
| 5664 | } |
| 5665 | |
| 5666 | static PyObject * |
| 5667 | consts_dict_keys_inorder(PyObject *dict) |
| 5668 | { |
| 5669 | PyObject *consts, *k, *v; |
| 5670 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5671 | |
| 5672 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5673 | if (consts == NULL) |
| 5674 | return NULL; |
| 5675 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5676 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5677 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5678 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5679 | * the object we want is always second. */ |
| 5680 | if (PyTuple_CheckExact(k)) { |
| 5681 | k = PyTuple_GET_ITEM(k, 1); |
| 5682 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5684 | assert(i < size); |
| 5685 | assert(i >= 0); |
| 5686 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5687 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5688 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5689 | } |
| 5690 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5691 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5692 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5695 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5697 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5698 | if (ste->ste_nested) |
| 5699 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5700 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5701 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5702 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5703 | flags |= CO_COROUTINE; |
| 5704 | if (ste->ste_generator && ste->ste_coroutine) |
| 5705 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5706 | if (ste->ste_varargs) |
| 5707 | flags |= CO_VARARGS; |
| 5708 | if (ste->ste_varkeywords) |
| 5709 | flags |= CO_VARKEYWORDS; |
| 5710 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5713 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5716 | } |
| 5717 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5718 | // Merge *tuple* with constant cache. |
| 5719 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5720 | static int |
| 5721 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5722 | { |
| 5723 | assert(PyTuple_CheckExact(*tuple)); |
| 5724 | |
| 5725 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5726 | if (key == NULL) { |
| 5727 | return 0; |
| 5728 | } |
| 5729 | |
| 5730 | // t is borrowed reference |
| 5731 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5732 | Py_DECREF(key); |
| 5733 | if (t == NULL) { |
| 5734 | return 0; |
| 5735 | } |
| 5736 | if (t == key) { // tuple is new constant. |
| 5737 | return 1; |
| 5738 | } |
| 5739 | |
| 5740 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5741 | Py_INCREF(u); |
| 5742 | Py_DECREF(*tuple); |
| 5743 | *tuple = u; |
| 5744 | return 1; |
| 5745 | } |
| 5746 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5747 | static PyCodeObject * |
| 5748 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5750 | PyObject *tmp; |
| 5751 | PyCodeObject *co = NULL; |
| 5752 | PyObject *consts = NULL; |
| 5753 | PyObject *names = NULL; |
| 5754 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5755 | PyObject *name = NULL; |
| 5756 | PyObject *freevars = NULL; |
| 5757 | PyObject *cellvars = NULL; |
| 5758 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5759 | Py_ssize_t nlocals; |
| 5760 | int nlocals_int; |
| 5761 | int flags; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5762 | int argcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5763 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5764 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5765 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5766 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5767 | if (!consts || !names || !varnames) |
| 5768 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5770 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5771 | if (!cellvars) |
| 5772 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5773 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5774 | if (!freevars) |
| 5775 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5776 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5777 | if (!merge_const_tuple(c, &names) || |
| 5778 | !merge_const_tuple(c, &varnames) || |
| 5779 | !merge_const_tuple(c, &cellvars) || |
| 5780 | !merge_const_tuple(c, &freevars)) |
| 5781 | { |
| 5782 | goto error; |
| 5783 | } |
| 5784 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5785 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5786 | assert(nlocals < INT_MAX); |
| 5787 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5789 | flags = compute_code_flags(c); |
| 5790 | if (flags < 0) |
| 5791 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5793 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5794 | if (!bytecode) |
| 5795 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5798 | if (!tmp) |
| 5799 | goto error; |
| 5800 | Py_DECREF(consts); |
| 5801 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5802 | if (!merge_const_tuple(c, &consts)) { |
| 5803 | goto error; |
| 5804 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5805 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5806 | argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5807 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5808 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5809 | maxdepth = stackdepth(c); |
| 5810 | if (maxdepth < 0) { |
| 5811 | goto error; |
| 5812 | } |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5813 | co = PyCode_New(argcount, posonlyargcount, kwonlyargcount, |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5814 | nlocals_int, maxdepth, flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5815 | bytecode, consts, names, varnames, |
| 5816 | freevars, cellvars, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5817 | c->c_filename, c->u->u_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5818 | c->u->u_firstlineno, |
| 5819 | a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5820 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5821 | Py_XDECREF(consts); |
| 5822 | Py_XDECREF(names); |
| 5823 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | Py_XDECREF(name); |
| 5825 | Py_XDECREF(freevars); |
| 5826 | Py_XDECREF(cellvars); |
| 5827 | Py_XDECREF(bytecode); |
| 5828 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5831 | |
| 5832 | /* For debugging purposes only */ |
| 5833 | #if 0 |
| 5834 | static void |
| 5835 | dump_instr(const struct instr *i) |
| 5836 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5837 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5838 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5839 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5840 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5841 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5842 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5843 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5844 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5845 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5846 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | static void |
| 5850 | dump_basicblock(const basicblock *b) |
| 5851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5852 | const char *seen = b->b_seen ? "seen " : ""; |
| 5853 | const char *b_return = b->b_return ? "return " : ""; |
| 5854 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5855 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5856 | if (b->b_instr) { |
| 5857 | int i; |
| 5858 | for (i = 0; i < b->b_iused; i++) { |
| 5859 | fprintf(stderr, " [%02d] ", i); |
| 5860 | dump_instr(b->b_instr + i); |
| 5861 | } |
| 5862 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5863 | } |
| 5864 | #endif |
| 5865 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5866 | static PyCodeObject * |
| 5867 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | basicblock *b, *entryblock; |
| 5870 | struct assembler a; |
| 5871 | int i, j, nblocks; |
| 5872 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5873 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5874 | /* Make sure every block that falls off the end returns None. |
| 5875 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5876 | block ends with a jump or return b_next shouldn't set. |
| 5877 | */ |
| 5878 | if (!c->u->u_curblock->b_return) { |
| 5879 | NEXT_BLOCK(c); |
| 5880 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5881 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | ADDOP(c, RETURN_VALUE); |
| 5883 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | nblocks = 0; |
| 5886 | entryblock = NULL; |
| 5887 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5888 | nblocks++; |
| 5889 | entryblock = b; |
| 5890 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5892 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5893 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5894 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 5896 | else |
| 5897 | c->u->u_firstlineno = 1; |
| 5898 | } |
| 5899 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5900 | goto error; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5901 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5903 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5904 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5906 | /* Emit code in reverse postorder from dfs. */ |
| 5907 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 5908 | b = a.a_postorder[i]; |
| 5909 | for (j = 0; j < b->b_iused; j++) |
| 5910 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5911 | goto error; |
| 5912 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5914 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5915 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5916 | 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] | 5917 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5920 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5921 | assemble_free(&a); |
| 5922 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5923 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5924 | |
| 5925 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5926 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5927 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 5928 | PyArena *arena) |
| 5929 | { |
| 5930 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 5931 | } |