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; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 312 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | int merged; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 314 | PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->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) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | flags = &local_flags; |
| 336 | } |
| 337 | merged = c.c_future->ff_features | flags->cf_flags; |
| 338 | c.c_future->ff_features = merged; |
| 339 | flags->cf_flags = merged; |
| 340 | c.c_flags = flags; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 341 | c.c_optimize = (optimize == -1) ? config->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 343 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 344 | if (!_PyAST_Optimize(mod, arena, c.c_optimize)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 345 | goto finally; |
| 346 | } |
| 347 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 348 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | if (c.c_st == NULL) { |
| 350 | if (!PyErr_Occurred()) |
| 351 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 352 | goto finally; |
| 353 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 357 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | compiler_free(&c); |
| 359 | assert(co || PyErr_Occurred()); |
| 360 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 364 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 365 | int optimize, PyArena *arena) |
| 366 | { |
| 367 | PyObject *filename; |
| 368 | PyCodeObject *co; |
| 369 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 370 | if (filename == NULL) |
| 371 | return NULL; |
| 372 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 373 | Py_DECREF(filename); |
| 374 | return co; |
| 375 | |
| 376 | } |
| 377 | |
| 378 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 379 | PyNode_Compile(struct _node *n, const char *filename) |
| 380 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 381 | PyCodeObject *co = NULL; |
| 382 | mod_ty mod; |
| 383 | PyArena *arena = PyArena_New(); |
| 384 | if (!arena) |
| 385 | return NULL; |
| 386 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 387 | if (mod) |
| 388 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 389 | PyArena_Free(arena); |
| 390 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 393 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 394 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | if (c->c_st) |
| 397 | PySymtable_Free(c->c_st); |
| 398 | if (c->c_future) |
| 399 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 400 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 401 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 405 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 406 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 407 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | Py_ssize_t i, n; |
| 409 | PyObject *v, *k; |
| 410 | PyObject *dict = PyDict_New(); |
| 411 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | n = PyList_Size(list); |
| 414 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 415 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 416 | if (!v) { |
| 417 | Py_DECREF(dict); |
| 418 | return NULL; |
| 419 | } |
| 420 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 421 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | Py_DECREF(v); |
| 423 | Py_DECREF(dict); |
| 424 | return NULL; |
| 425 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | Py_DECREF(v); |
| 427 | } |
| 428 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* Return new dict containing names from src that match scope(s). |
| 432 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 433 | 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] | 434 | 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] | 435 | values are integers, starting at offset and increasing by one for |
| 436 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 437 | */ |
| 438 | |
| 439 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 440 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 441 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 442 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 444 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 446 | assert(offset >= 0); |
| 447 | if (dest == NULL) |
| 448 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 449 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 450 | /* Sort the keys so that we have a deterministic order on the indexes |
| 451 | saved in the returned dictionary. These indexes are used as indexes |
| 452 | into the free and cell var storage. Therefore if they aren't |
| 453 | deterministic, then the generated bytecode is not deterministic. |
| 454 | */ |
| 455 | sorted_keys = PyDict_Keys(src); |
| 456 | if (sorted_keys == NULL) |
| 457 | return NULL; |
| 458 | if (PyList_Sort(sorted_keys) != 0) { |
| 459 | Py_DECREF(sorted_keys); |
| 460 | return NULL; |
| 461 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 462 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 463 | |
| 464 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | /* XXX this should probably be a macro in symtable.h */ |
| 466 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 467 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 468 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 469 | assert(PyLong_Check(v)); |
| 470 | vi = PyLong_AS_LONG(v); |
| 471 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 474 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 476 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | Py_DECREF(dest); |
| 478 | return NULL; |
| 479 | } |
| 480 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 481 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 482 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | Py_DECREF(item); |
| 484 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | return NULL; |
| 486 | } |
| 487 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 490 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 494 | static void |
| 495 | compiler_unit_check(struct compiler_unit *u) |
| 496 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | basicblock *block; |
| 498 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 499 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 500 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 501 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | if (block->b_instr != NULL) { |
| 503 | assert(block->b_ialloc > 0); |
| 504 | assert(block->b_iused > 0); |
| 505 | assert(block->b_ialloc >= block->b_iused); |
| 506 | } |
| 507 | else { |
| 508 | assert (block->b_iused == 0); |
| 509 | assert (block->b_ialloc == 0); |
| 510 | } |
| 511 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | static void |
| 515 | compiler_unit_free(struct compiler_unit *u) |
| 516 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | compiler_unit_check(u); |
| 520 | b = u->u_blocks; |
| 521 | while (b != NULL) { |
| 522 | if (b->b_instr) |
| 523 | PyObject_Free((void *)b->b_instr); |
| 524 | next = b->b_list; |
| 525 | PyObject_Free((void *)b); |
| 526 | b = next; |
| 527 | } |
| 528 | Py_CLEAR(u->u_ste); |
| 529 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 530 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | Py_CLEAR(u->u_consts); |
| 532 | Py_CLEAR(u->u_names); |
| 533 | Py_CLEAR(u->u_varnames); |
| 534 | Py_CLEAR(u->u_freevars); |
| 535 | Py_CLEAR(u->u_cellvars); |
| 536 | Py_CLEAR(u->u_private); |
| 537 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 541 | compiler_enter_scope(struct compiler *c, identifier name, |
| 542 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 544 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 545 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 547 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 548 | struct compiler_unit)); |
| 549 | if (!u) { |
| 550 | PyErr_NoMemory(); |
| 551 | return 0; |
| 552 | } |
| 553 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 554 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 556 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | u->u_kwonlyargcount = 0; |
| 558 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 559 | if (!u->u_ste) { |
| 560 | compiler_unit_free(u); |
| 561 | return 0; |
| 562 | } |
| 563 | Py_INCREF(name); |
| 564 | u->u_name = name; |
| 565 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 566 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 567 | if (!u->u_varnames || !u->u_cellvars) { |
| 568 | compiler_unit_free(u); |
| 569 | return 0; |
| 570 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 571 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 572 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 573 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 574 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 575 | int res; |
| 576 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 577 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 578 | name = _PyUnicode_FromId(&PyId___class__); |
| 579 | if (!name) { |
| 580 | compiler_unit_free(u); |
| 581 | return 0; |
| 582 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 583 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 584 | if (res < 0) { |
| 585 | compiler_unit_free(u); |
| 586 | return 0; |
| 587 | } |
| 588 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | 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] | 591 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | if (!u->u_freevars) { |
| 593 | compiler_unit_free(u); |
| 594 | return 0; |
| 595 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | u->u_blocks = NULL; |
| 598 | u->u_nfblocks = 0; |
| 599 | u->u_firstlineno = lineno; |
| 600 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 601 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | u->u_lineno_set = 0; |
| 603 | u->u_consts = PyDict_New(); |
| 604 | if (!u->u_consts) { |
| 605 | compiler_unit_free(u); |
| 606 | return 0; |
| 607 | } |
| 608 | u->u_names = PyDict_New(); |
| 609 | if (!u->u_names) { |
| 610 | compiler_unit_free(u); |
| 611 | return 0; |
| 612 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 614 | u->u_private = NULL; |
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 | /* Push the old compiler_unit on the stack. */ |
| 617 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 618 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 619 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 620 | Py_XDECREF(capsule); |
| 621 | compiler_unit_free(u); |
| 622 | return 0; |
| 623 | } |
| 624 | Py_DECREF(capsule); |
| 625 | u->u_private = c->u->u_private; |
| 626 | Py_XINCREF(u->u_private); |
| 627 | } |
| 628 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 631 | |
| 632 | block = compiler_new_block(c); |
| 633 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 635 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 636 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 637 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 638 | if (!compiler_set_qualname(c)) |
| 639 | return 0; |
| 640 | } |
| 641 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 642 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 645 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 646 | compiler_exit_scope(struct compiler *c) |
| 647 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 648 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 651 | c->c_nestlevel--; |
| 652 | compiler_unit_free(c->u); |
| 653 | /* Restore c->u to the parent unit. */ |
| 654 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 655 | if (n >= 0) { |
| 656 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 657 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 658 | assert(c->u); |
| 659 | /* we are deleting from a list so this really shouldn't fail */ |
| 660 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 661 | Py_FatalError("compiler_exit_scope()"); |
| 662 | compiler_unit_check(c->u); |
| 663 | } |
| 664 | else |
| 665 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 666 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 669 | static int |
| 670 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 671 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 672 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 673 | _Py_static_string(dot_locals, ".<locals>"); |
| 674 | Py_ssize_t stack_size; |
| 675 | struct compiler_unit *u = c->u; |
| 676 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 677 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 678 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 679 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 680 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 681 | if (stack_size > 1) { |
| 682 | int scope, force_global = 0; |
| 683 | struct compiler_unit *parent; |
| 684 | PyObject *mangled, *capsule; |
| 685 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 686 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 687 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 688 | assert(parent); |
| 689 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 690 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 691 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 692 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 693 | assert(u->u_name); |
| 694 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 695 | if (!mangled) |
| 696 | return 0; |
| 697 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 698 | Py_DECREF(mangled); |
| 699 | assert(scope != GLOBAL_IMPLICIT); |
| 700 | if (scope == GLOBAL_EXPLICIT) |
| 701 | force_global = 1; |
| 702 | } |
| 703 | |
| 704 | if (!force_global) { |
| 705 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 706 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 707 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 708 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 709 | if (dot_locals_str == NULL) |
| 710 | return 0; |
| 711 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 712 | if (base == NULL) |
| 713 | return 0; |
| 714 | } |
| 715 | else { |
| 716 | Py_INCREF(parent->u_qualname); |
| 717 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 718 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 719 | } |
| 720 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 721 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 722 | if (base != NULL) { |
| 723 | dot_str = _PyUnicode_FromId(&dot); |
| 724 | if (dot_str == NULL) { |
| 725 | Py_DECREF(base); |
| 726 | return 0; |
| 727 | } |
| 728 | name = PyUnicode_Concat(base, dot_str); |
| 729 | Py_DECREF(base); |
| 730 | if (name == NULL) |
| 731 | return 0; |
| 732 | PyUnicode_Append(&name, u->u_name); |
| 733 | if (name == NULL) |
| 734 | return 0; |
| 735 | } |
| 736 | else { |
| 737 | Py_INCREF(u->u_name); |
| 738 | name = u->u_name; |
| 739 | } |
| 740 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 741 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 742 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 743 | } |
| 744 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 745 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 746 | /* Allocate a new block and return a pointer to it. |
| 747 | Returns NULL on error. |
| 748 | */ |
| 749 | |
| 750 | static basicblock * |
| 751 | compiler_new_block(struct compiler *c) |
| 752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 753 | basicblock *b; |
| 754 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | u = c->u; |
| 757 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 758 | if (b == NULL) { |
| 759 | PyErr_NoMemory(); |
| 760 | return NULL; |
| 761 | } |
| 762 | memset((void *)b, 0, sizeof(basicblock)); |
| 763 | /* Extend the singly linked list of blocks with new block. */ |
| 764 | b->b_list = u->u_blocks; |
| 765 | u->u_blocks = b; |
| 766 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 769 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 770 | compiler_next_block(struct compiler *c) |
| 771 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | basicblock *block = compiler_new_block(c); |
| 773 | if (block == NULL) |
| 774 | return NULL; |
| 775 | c->u->u_curblock->b_next = block; |
| 776 | c->u->u_curblock = block; |
| 777 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | static basicblock * |
| 781 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 782 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 783 | assert(block != NULL); |
| 784 | c->u->u_curblock->b_next = block; |
| 785 | c->u->u_curblock = block; |
| 786 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* Returns the offset of the next instruction in the current block's |
| 790 | b_instr array. Resizes the b_instr as necessary. |
| 791 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 792 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 | |
| 794 | static int |
| 795 | compiler_next_instr(struct compiler *c, basicblock *b) |
| 796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | assert(b != NULL); |
| 798 | if (b->b_instr == NULL) { |
| 799 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 800 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 801 | if (b->b_instr == NULL) { |
| 802 | PyErr_NoMemory(); |
| 803 | return -1; |
| 804 | } |
| 805 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 806 | memset((char *)b->b_instr, 0, |
| 807 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 808 | } |
| 809 | else if (b->b_iused == b->b_ialloc) { |
| 810 | struct instr *tmp; |
| 811 | size_t oldsize, newsize; |
| 812 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 813 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 814 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 815 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | PyErr_NoMemory(); |
| 817 | return -1; |
| 818 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | if (newsize == 0) { |
| 821 | PyErr_NoMemory(); |
| 822 | return -1; |
| 823 | } |
| 824 | b->b_ialloc <<= 1; |
| 825 | tmp = (struct instr *)PyObject_Realloc( |
| 826 | (void *)b->b_instr, newsize); |
| 827 | if (tmp == NULL) { |
| 828 | PyErr_NoMemory(); |
| 829 | return -1; |
| 830 | } |
| 831 | b->b_instr = tmp; |
| 832 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 833 | } |
| 834 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 837 | /* Set the i_lineno member of the instruction at offset off if the |
| 838 | line number for the current expression/statement has not |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 839 | already been set. If it has been set, the call has no effect. |
| 840 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 841 | The line number is reset in the following cases: |
| 842 | - when entering a new scope |
| 843 | - on each statement |
| 844 | - on each expression that start a new line |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 845 | - before the "except" and "finally" clauses |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 846 | - before the "for" and "while" expressions |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 847 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 848 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 849 | static void |
| 850 | compiler_set_lineno(struct compiler *c, int off) |
| 851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 852 | basicblock *b; |
| 853 | if (c->u->u_lineno_set) |
| 854 | return; |
| 855 | c->u->u_lineno_set = 1; |
| 856 | b = c->u->u_curblock; |
| 857 | b->b_instr[off].i_lineno = c->u->u_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 860 | /* Return the stack effect of opcode with argument oparg. |
| 861 | |
| 862 | Some opcodes have different stack effect when jump to the target and |
| 863 | when not jump. The 'jump' parameter specifies the case: |
| 864 | |
| 865 | * 0 -- when not jump |
| 866 | * 1 -- when jump |
| 867 | * -1 -- maximal |
| 868 | */ |
| 869 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 870 | WITH_CLEANUP_FINISH deterministic. */ |
| 871 | static int |
| 872 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 875 | case NOP: |
| 876 | case EXTENDED_ARG: |
| 877 | return 0; |
| 878 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 879 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | case POP_TOP: |
| 881 | return -1; |
| 882 | case ROT_TWO: |
| 883 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 884 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | return 0; |
| 886 | case DUP_TOP: |
| 887 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 888 | case DUP_TOP_TWO: |
| 889 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 890 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 891 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | case UNARY_POSITIVE: |
| 893 | case UNARY_NEGATIVE: |
| 894 | case UNARY_NOT: |
| 895 | case UNARY_INVERT: |
| 896 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | case SET_ADD: |
| 899 | case LIST_APPEND: |
| 900 | return -1; |
| 901 | case MAP_ADD: |
| 902 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 903 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 904 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | case BINARY_POWER: |
| 906 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 907 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | case BINARY_MODULO: |
| 909 | case BINARY_ADD: |
| 910 | case BINARY_SUBTRACT: |
| 911 | case BINARY_SUBSCR: |
| 912 | case BINARY_FLOOR_DIVIDE: |
| 913 | case BINARY_TRUE_DIVIDE: |
| 914 | return -1; |
| 915 | case INPLACE_FLOOR_DIVIDE: |
| 916 | case INPLACE_TRUE_DIVIDE: |
| 917 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | case INPLACE_ADD: |
| 920 | case INPLACE_SUBTRACT: |
| 921 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 922 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | case INPLACE_MODULO: |
| 924 | return -1; |
| 925 | case STORE_SUBSCR: |
| 926 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | case DELETE_SUBSCR: |
| 928 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | case BINARY_LSHIFT: |
| 931 | case BINARY_RSHIFT: |
| 932 | case BINARY_AND: |
| 933 | case BINARY_XOR: |
| 934 | case BINARY_OR: |
| 935 | return -1; |
| 936 | case INPLACE_POWER: |
| 937 | return -1; |
| 938 | case GET_ITER: |
| 939 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | case PRINT_EXPR: |
| 942 | return -1; |
| 943 | case LOAD_BUILD_CLASS: |
| 944 | return 1; |
| 945 | case INPLACE_LSHIFT: |
| 946 | case INPLACE_RSHIFT: |
| 947 | case INPLACE_AND: |
| 948 | case INPLACE_XOR: |
| 949 | case INPLACE_OR: |
| 950 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 953 | /* 1 in the normal flow. |
| 954 | * Restore the stack position and push 6 values before jumping to |
| 955 | * the handler if an exception be raised. */ |
| 956 | return jump ? 6 : 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 957 | case WITH_CLEANUP_START: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 958 | return 2; /* or 1, depending on TOS */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 959 | case WITH_CLEANUP_FINISH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 960 | /* Pop a variable number of values pushed by WITH_CLEANUP_START |
| 961 | * + __exit__ or __aexit__. */ |
| 962 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | case RETURN_VALUE: |
| 964 | return -1; |
| 965 | case IMPORT_STAR: |
| 966 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 967 | case SETUP_ANNOTATIONS: |
| 968 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | case YIELD_VALUE: |
| 970 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 971 | case YIELD_FROM: |
| 972 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | case POP_BLOCK: |
| 974 | return 0; |
| 975 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 976 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 977 | case END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 978 | case POP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 979 | /* Pop 6 values when an exception was raised. */ |
| 980 | return -6; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 982 | case STORE_NAME: |
| 983 | return -1; |
| 984 | case DELETE_NAME: |
| 985 | return 0; |
| 986 | case UNPACK_SEQUENCE: |
| 987 | return oparg-1; |
| 988 | case UNPACK_EX: |
| 989 | return (oparg&0xFF) + (oparg>>8); |
| 990 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 991 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 992 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | case STORE_ATTR: |
| 995 | return -2; |
| 996 | case DELETE_ATTR: |
| 997 | return -1; |
| 998 | case STORE_GLOBAL: |
| 999 | return -1; |
| 1000 | case DELETE_GLOBAL: |
| 1001 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | case LOAD_CONST: |
| 1003 | return 1; |
| 1004 | case LOAD_NAME: |
| 1005 | return 1; |
| 1006 | case BUILD_TUPLE: |
| 1007 | case BUILD_LIST: |
| 1008 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1009 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | return 1-oparg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1011 | case BUILD_LIST_UNPACK: |
| 1012 | case BUILD_TUPLE_UNPACK: |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 1013 | case BUILD_TUPLE_UNPACK_WITH_CALL: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1014 | case BUILD_SET_UNPACK: |
| 1015 | case BUILD_MAP_UNPACK: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1016 | case BUILD_MAP_UNPACK_WITH_CALL: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1017 | return 1 - oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1019 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1020 | case BUILD_CONST_KEY_MAP: |
| 1021 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | case LOAD_ATTR: |
| 1023 | return 0; |
| 1024 | case COMPARE_OP: |
| 1025 | return -1; |
| 1026 | case IMPORT_NAME: |
| 1027 | return -1; |
| 1028 | case IMPORT_FROM: |
| 1029 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1030 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1031 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | case JUMP_ABSOLUTE: |
| 1034 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1035 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1036 | case JUMP_IF_TRUE_OR_POP: |
| 1037 | case JUMP_IF_FALSE_OR_POP: |
| 1038 | return jump ? 0 : -1; |
| 1039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1040 | case POP_JUMP_IF_FALSE: |
| 1041 | case POP_JUMP_IF_TRUE: |
| 1042 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1044 | case LOAD_GLOBAL: |
| 1045 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1046 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1047 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1049 | /* 0 in the normal flow. |
| 1050 | * Restore the stack position and push 6 values before jumping to |
| 1051 | * the handler if an exception be raised. */ |
| 1052 | return jump ? 6 : 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1053 | case BEGIN_FINALLY: |
| 1054 | /* Actually pushes 1 value, but count 6 for balancing with |
| 1055 | * END_FINALLY and POP_FINALLY. |
| 1056 | * This is the main reason of using this opcode instead of |
| 1057 | * "LOAD_CONST None". */ |
| 1058 | return 6; |
| 1059 | case CALL_FINALLY: |
| 1060 | return jump ? 1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1062 | case LOAD_FAST: |
| 1063 | return 1; |
| 1064 | case STORE_FAST: |
| 1065 | return -1; |
| 1066 | case DELETE_FAST: |
| 1067 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1069 | case RAISE_VARARGS: |
| 1070 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1071 | |
| 1072 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1074 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1075 | case CALL_METHOD: |
| 1076 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1078 | return -oparg-1; |
| 1079 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1080 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1081 | case MAKE_FUNCTION: |
| 1082 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1083 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1084 | case BUILD_SLICE: |
| 1085 | if (oparg == 3) |
| 1086 | return -2; |
| 1087 | else |
| 1088 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1089 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1090 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | case LOAD_CLOSURE: |
| 1092 | return 1; |
| 1093 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1094 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | return 1; |
| 1096 | case STORE_DEREF: |
| 1097 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1098 | case DELETE_DEREF: |
| 1099 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1100 | |
| 1101 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1102 | case GET_AWAITABLE: |
| 1103 | return 0; |
| 1104 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1105 | /* 0 in the normal flow. |
| 1106 | * Restore the stack position to the position before the result |
| 1107 | * of __aenter__ and push 6 values before jumping to the handler |
| 1108 | * if an exception be raised. */ |
| 1109 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1110 | case BEFORE_ASYNC_WITH: |
| 1111 | return 1; |
| 1112 | case GET_AITER: |
| 1113 | return 0; |
| 1114 | case GET_ANEXT: |
| 1115 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1116 | case GET_YIELD_FROM_ITER: |
| 1117 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1118 | case END_ASYNC_FOR: |
| 1119 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1120 | case FORMAT_VALUE: |
| 1121 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1122 | else 1->1. */ |
| 1123 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1124 | case LOAD_METHOD: |
| 1125 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1127 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1128 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1129 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1130 | } |
| 1131 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1132 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1133 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1134 | { |
| 1135 | return stack_effect(opcode, oparg, jump); |
| 1136 | } |
| 1137 | |
| 1138 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1139 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1140 | { |
| 1141 | return stack_effect(opcode, oparg, -1); |
| 1142 | } |
| 1143 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1144 | /* Add an opcode with no argument. |
| 1145 | Returns 0 on failure, 1 on success. |
| 1146 | */ |
| 1147 | |
| 1148 | static int |
| 1149 | compiler_addop(struct compiler *c, int opcode) |
| 1150 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | basicblock *b; |
| 1152 | struct instr *i; |
| 1153 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1154 | assert(!HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1156 | if (off < 0) |
| 1157 | return 0; |
| 1158 | b = c->u->u_curblock; |
| 1159 | i = &b->b_instr[off]; |
| 1160 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1161 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | if (opcode == RETURN_VALUE) |
| 1163 | b->b_return = 1; |
| 1164 | compiler_set_lineno(c, off); |
| 1165 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1168 | static Py_ssize_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1169 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) |
| 1170 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1171 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1173 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1174 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1176 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1178 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1179 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1180 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | return -1; |
| 1183 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1184 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | Py_DECREF(v); |
| 1186 | return -1; |
| 1187 | } |
| 1188 | Py_DECREF(v); |
| 1189 | } |
| 1190 | else |
| 1191 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1192 | return arg; |
| 1193 | } |
| 1194 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1195 | // Merge const *o* recursively and return constant key object. |
| 1196 | static PyObject* |
| 1197 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1198 | { |
| 1199 | // None and Ellipsis are singleton, and key is the singleton. |
| 1200 | // No need to merge object and key. |
| 1201 | if (o == Py_None || o == Py_Ellipsis) { |
| 1202 | Py_INCREF(o); |
| 1203 | return o; |
| 1204 | } |
| 1205 | |
| 1206 | PyObject *key = _PyCode_ConstantKey(o); |
| 1207 | if (key == NULL) { |
| 1208 | return NULL; |
| 1209 | } |
| 1210 | |
| 1211 | // t is borrowed reference |
| 1212 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1213 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1214 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1215 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1216 | Py_DECREF(key); |
| 1217 | return t; |
| 1218 | } |
| 1219 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1220 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1221 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1222 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1223 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1224 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1225 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1226 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1227 | PyObject *u = merge_consts_recursive(c, item); |
| 1228 | if (u == NULL) { |
| 1229 | Py_DECREF(key); |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | // See _PyCode_ConstantKey() |
| 1234 | PyObject *v; // borrowed |
| 1235 | if (PyTuple_CheckExact(u)) { |
| 1236 | v = PyTuple_GET_ITEM(u, 1); |
| 1237 | } |
| 1238 | else { |
| 1239 | v = u; |
| 1240 | } |
| 1241 | if (v != item) { |
| 1242 | Py_INCREF(v); |
| 1243 | PyTuple_SET_ITEM(o, i, v); |
| 1244 | Py_DECREF(item); |
| 1245 | } |
| 1246 | |
| 1247 | Py_DECREF(u); |
| 1248 | } |
| 1249 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1250 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1251 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1252 | // constant keys. |
| 1253 | // See _PyCode_ConstantKey() for detail. |
| 1254 | assert(PyTuple_CheckExact(key)); |
| 1255 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1256 | |
| 1257 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1258 | if (len == 0) { // empty frozenset should not be re-created. |
| 1259 | return key; |
| 1260 | } |
| 1261 | PyObject *tuple = PyTuple_New(len); |
| 1262 | if (tuple == NULL) { |
| 1263 | Py_DECREF(key); |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | Py_ssize_t i = 0, pos = 0; |
| 1267 | PyObject *item; |
| 1268 | Py_hash_t hash; |
| 1269 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1270 | PyObject *k = merge_consts_recursive(c, item); |
| 1271 | if (k == NULL) { |
| 1272 | Py_DECREF(tuple); |
| 1273 | Py_DECREF(key); |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | PyObject *u; |
| 1277 | if (PyTuple_CheckExact(k)) { |
| 1278 | u = PyTuple_GET_ITEM(k, 1); |
| 1279 | Py_INCREF(u); |
| 1280 | Py_DECREF(k); |
| 1281 | } |
| 1282 | else { |
| 1283 | u = k; |
| 1284 | } |
| 1285 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1286 | i++; |
| 1287 | } |
| 1288 | |
| 1289 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1290 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1291 | PyObject *new = PyFrozenSet_New(tuple); |
| 1292 | Py_DECREF(tuple); |
| 1293 | if (new == NULL) { |
| 1294 | Py_DECREF(key); |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1298 | Py_DECREF(o); |
| 1299 | PyTuple_SET_ITEM(key, 1, new); |
| 1300 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1301 | |
| 1302 | return key; |
| 1303 | } |
| 1304 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1305 | static Py_ssize_t |
| 1306 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1307 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1308 | PyObject *key = merge_consts_recursive(c, o); |
| 1309 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1310 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1311 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1312 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1313 | Py_ssize_t arg = compiler_add_o(c, c->u->u_consts, key); |
| 1314 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1319 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1320 | { |
| 1321 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1322 | if (arg < 0) |
| 1323 | return 0; |
| 1324 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1325 | } |
| 1326 | |
| 1327 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1328 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1329 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1330 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1331 | Py_ssize_t arg = compiler_add_o(c, dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1332 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1333 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1334 | return compiler_addop_i(c, opcode, arg); |
| 1335 | } |
| 1336 | |
| 1337 | static int |
| 1338 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1340 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1341 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1342 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1343 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1344 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1345 | arg = compiler_add_o(c, dict, mangled); |
| 1346 | Py_DECREF(mangled); |
| 1347 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1348 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1349 | return compiler_addop_i(c, opcode, arg); |
| 1350 | } |
| 1351 | |
| 1352 | /* Add an opcode with an integer argument. |
| 1353 | Returns 0 on failure, 1 on success. |
| 1354 | */ |
| 1355 | |
| 1356 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1357 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1358 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1359 | struct instr *i; |
| 1360 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1361 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1362 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1363 | it in the C code (like Python/ceval.c). |
| 1364 | |
| 1365 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1366 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1367 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1368 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1369 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1370 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1373 | if (off < 0) |
| 1374 | return 0; |
| 1375 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1376 | i->i_opcode = opcode; |
| 1377 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1378 | compiler_set_lineno(c, off); |
| 1379 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | static int |
| 1383 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | struct instr *i; |
| 1386 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1387 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1388 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1389 | assert(b != NULL); |
| 1390 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1391 | if (off < 0) |
| 1392 | return 0; |
| 1393 | i = &c->u->u_curblock->b_instr[off]; |
| 1394 | i->i_opcode = opcode; |
| 1395 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | if (absolute) |
| 1397 | i->i_jabs = 1; |
| 1398 | else |
| 1399 | i->i_jrel = 1; |
| 1400 | compiler_set_lineno(c, off); |
| 1401 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1404 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1405 | to the new block. |
| 1406 | |
| 1407 | The returns inside this macro make it impossible to decref objects |
| 1408 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1409 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1411 | if (compiler_next_block((C)) == NULL) \ |
| 1412 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | if (!compiler_addop((C), (OP))) \ |
| 1417 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1418 | } |
| 1419 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1420 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1421 | if (!compiler_addop((C), (OP))) { \ |
| 1422 | compiler_exit_scope(c); \ |
| 1423 | return 0; \ |
| 1424 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1427 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1428 | if (!compiler_addop_load_const((C), (O))) \ |
| 1429 | return 0; \ |
| 1430 | } |
| 1431 | |
| 1432 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1433 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1434 | PyObject *__new_const = (O); \ |
| 1435 | if (__new_const == NULL) { \ |
| 1436 | return 0; \ |
| 1437 | } \ |
| 1438 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1439 | Py_DECREF(__new_const); \ |
| 1440 | return 0; \ |
| 1441 | } \ |
| 1442 | Py_DECREF(__new_const); \ |
| 1443 | } |
| 1444 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1445 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1447 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1450 | /* Same as ADDOP_O, but steals a reference. */ |
| 1451 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1452 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1453 | Py_DECREF((O)); \ |
| 1454 | return 0; \ |
| 1455 | } \ |
| 1456 | Py_DECREF((O)); \ |
| 1457 | } |
| 1458 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1459 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1460 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1461 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1465 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1466 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1471 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1476 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1480 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1481 | */ |
| 1482 | |
| 1483 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1484 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1485 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1488 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1489 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1490 | compiler_exit_scope(c); \ |
| 1491 | return 0; \ |
| 1492 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1495 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1497 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | int _i; \ |
| 1502 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1503 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1504 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1505 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1506 | return 0; \ |
| 1507 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1510 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | int _i; \ |
| 1512 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1513 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1514 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1515 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1516 | compiler_exit_scope(c); \ |
| 1517 | return 0; \ |
| 1518 | } \ |
| 1519 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1522 | /* Search if variable annotations are present statically in a block. */ |
| 1523 | |
| 1524 | static int |
| 1525 | find_ann(asdl_seq *stmts) |
| 1526 | { |
| 1527 | int i, j, res = 0; |
| 1528 | stmt_ty st; |
| 1529 | |
| 1530 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1531 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1532 | switch (st->kind) { |
| 1533 | case AnnAssign_kind: |
| 1534 | return 1; |
| 1535 | case For_kind: |
| 1536 | res = find_ann(st->v.For.body) || |
| 1537 | find_ann(st->v.For.orelse); |
| 1538 | break; |
| 1539 | case AsyncFor_kind: |
| 1540 | res = find_ann(st->v.AsyncFor.body) || |
| 1541 | find_ann(st->v.AsyncFor.orelse); |
| 1542 | break; |
| 1543 | case While_kind: |
| 1544 | res = find_ann(st->v.While.body) || |
| 1545 | find_ann(st->v.While.orelse); |
| 1546 | break; |
| 1547 | case If_kind: |
| 1548 | res = find_ann(st->v.If.body) || |
| 1549 | find_ann(st->v.If.orelse); |
| 1550 | break; |
| 1551 | case With_kind: |
| 1552 | res = find_ann(st->v.With.body); |
| 1553 | break; |
| 1554 | case AsyncWith_kind: |
| 1555 | res = find_ann(st->v.AsyncWith.body); |
| 1556 | break; |
| 1557 | case Try_kind: |
| 1558 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1559 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1560 | st->v.Try.handlers, j); |
| 1561 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1562 | return 1; |
| 1563 | } |
| 1564 | } |
| 1565 | res = find_ann(st->v.Try.body) || |
| 1566 | find_ann(st->v.Try.finalbody) || |
| 1567 | find_ann(st->v.Try.orelse); |
| 1568 | break; |
| 1569 | default: |
| 1570 | res = 0; |
| 1571 | } |
| 1572 | if (res) { |
| 1573 | break; |
| 1574 | } |
| 1575 | } |
| 1576 | return res; |
| 1577 | } |
| 1578 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1579 | /* |
| 1580 | * Frame block handling functions |
| 1581 | */ |
| 1582 | |
| 1583 | static int |
| 1584 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
| 1585 | basicblock *exit) |
| 1586 | { |
| 1587 | struct fblockinfo *f; |
| 1588 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1589 | PyErr_SetString(PyExc_SyntaxError, |
| 1590 | "too many statically nested blocks"); |
| 1591 | return 0; |
| 1592 | } |
| 1593 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1594 | f->fb_type = t; |
| 1595 | f->fb_block = b; |
| 1596 | f->fb_exit = exit; |
| 1597 | return 1; |
| 1598 | } |
| 1599 | |
| 1600 | static void |
| 1601 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1602 | { |
| 1603 | struct compiler_unit *u = c->u; |
| 1604 | assert(u->u_nfblocks > 0); |
| 1605 | u->u_nfblocks--; |
| 1606 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1607 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1608 | } |
| 1609 | |
| 1610 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
| 1611 | * popping the blocks will be restored afterwards. |
| 1612 | */ |
| 1613 | static int |
| 1614 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1615 | int preserve_tos) |
| 1616 | { |
| 1617 | switch (info->fb_type) { |
| 1618 | case WHILE_LOOP: |
| 1619 | return 1; |
| 1620 | |
| 1621 | case FINALLY_END: |
| 1622 | ADDOP_I(c, POP_FINALLY, preserve_tos); |
| 1623 | return 1; |
| 1624 | |
| 1625 | case FOR_LOOP: |
| 1626 | /* Pop the iterator */ |
| 1627 | if (preserve_tos) { |
| 1628 | ADDOP(c, ROT_TWO); |
| 1629 | } |
| 1630 | ADDOP(c, POP_TOP); |
| 1631 | return 1; |
| 1632 | |
| 1633 | case EXCEPT: |
| 1634 | ADDOP(c, POP_BLOCK); |
| 1635 | return 1; |
| 1636 | |
| 1637 | case FINALLY_TRY: |
| 1638 | ADDOP(c, POP_BLOCK); |
| 1639 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1640 | return 1; |
| 1641 | |
| 1642 | case WITH: |
| 1643 | case ASYNC_WITH: |
| 1644 | ADDOP(c, POP_BLOCK); |
| 1645 | if (preserve_tos) { |
| 1646 | ADDOP(c, ROT_TWO); |
| 1647 | } |
| 1648 | ADDOP(c, BEGIN_FINALLY); |
| 1649 | ADDOP(c, WITH_CLEANUP_START); |
| 1650 | if (info->fb_type == ASYNC_WITH) { |
| 1651 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1652 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1653 | ADDOP(c, YIELD_FROM); |
| 1654 | } |
| 1655 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 1656 | ADDOP_I(c, POP_FINALLY, 0); |
| 1657 | return 1; |
| 1658 | |
| 1659 | case HANDLER_CLEANUP: |
| 1660 | if (preserve_tos) { |
| 1661 | ADDOP(c, ROT_FOUR); |
| 1662 | } |
| 1663 | if (info->fb_exit) { |
| 1664 | ADDOP(c, POP_BLOCK); |
| 1665 | ADDOP(c, POP_EXCEPT); |
| 1666 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1667 | } |
| 1668 | else { |
| 1669 | ADDOP(c, POP_EXCEPT); |
| 1670 | } |
| 1671 | return 1; |
| 1672 | } |
| 1673 | Py_UNREACHABLE(); |
| 1674 | } |
| 1675 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1676 | /* Compile a sequence of statements, checking for a docstring |
| 1677 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1678 | |
| 1679 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1680 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1681 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1682 | int i = 0; |
| 1683 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1684 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1685 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1686 | /* Set current line number to the line number of first statement. |
| 1687 | This way line number for SETUP_ANNOTATIONS will always |
| 1688 | coincide with the line number of first "real" statement in module. |
| 1689 | If body is empy, then lineno will be set later in assemble. */ |
| 1690 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && |
| 1691 | !c->u->u_lineno && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1692 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1693 | c->u->u_lineno = st->lineno; |
| 1694 | } |
| 1695 | /* Every annotated class and module should have __annotations__. */ |
| 1696 | if (find_ann(stmts)) { |
| 1697 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1698 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1699 | if (!asdl_seq_LEN(stmts)) |
| 1700 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1701 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1702 | if (c->c_optimize < 2) { |
| 1703 | docstring = _PyAST_GetDocString(stmts); |
| 1704 | if (docstring) { |
| 1705 | i = 1; |
| 1706 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1707 | assert(st->kind == Expr_kind); |
| 1708 | VISIT(c, expr, st->v.Expr.value); |
| 1709 | if (!compiler_nameop(c, __doc__, Store)) |
| 1710 | return 0; |
| 1711 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1713 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1714 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | static PyCodeObject * |
| 1719 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1721 | PyCodeObject *co; |
| 1722 | int addNone = 1; |
| 1723 | static PyObject *module; |
| 1724 | if (!module) { |
| 1725 | module = PyUnicode_InternFromString("<module>"); |
| 1726 | if (!module) |
| 1727 | return NULL; |
| 1728 | } |
| 1729 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1730 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | return NULL; |
| 1732 | switch (mod->kind) { |
| 1733 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1734 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | compiler_exit_scope(c); |
| 1736 | return 0; |
| 1737 | } |
| 1738 | break; |
| 1739 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1740 | if (find_ann(mod->v.Interactive.body)) { |
| 1741 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1742 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1743 | c->c_interactive = 1; |
| 1744 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1745 | mod->v.Interactive.body); |
| 1746 | break; |
| 1747 | case Expression_kind: |
| 1748 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1749 | addNone = 0; |
| 1750 | break; |
| 1751 | case Suite_kind: |
| 1752 | PyErr_SetString(PyExc_SystemError, |
| 1753 | "suite should not be possible"); |
| 1754 | return 0; |
| 1755 | default: |
| 1756 | PyErr_Format(PyExc_SystemError, |
| 1757 | "module kind %d should not be possible", |
| 1758 | mod->kind); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | co = assemble(c, addNone); |
| 1762 | compiler_exit_scope(c); |
| 1763 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1766 | /* The test for LOCAL must come before the test for FREE in order to |
| 1767 | handle classes where name is both local and free. The local var is |
| 1768 | 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] | 1769 | */ |
| 1770 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1771 | static int |
| 1772 | get_ref_type(struct compiler *c, PyObject *name) |
| 1773 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1774 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1775 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1776 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1777 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1778 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | if (scope == 0) { |
| 1780 | char buf[350]; |
| 1781 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1782 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1784 | PyUnicode_AsUTF8(name), |
| 1785 | PyUnicode_AsUTF8(c->u->u_name), |
| 1786 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1787 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1788 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1789 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | ); |
| 1791 | Py_FatalError(buf); |
| 1792 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | static int |
| 1798 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1799 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1800 | PyObject *v; |
| 1801 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1802 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1803 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1804 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1808 | 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] | 1809 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1810 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1811 | if (qualname == NULL) |
| 1812 | qualname = co->co_name; |
| 1813 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1814 | if (free) { |
| 1815 | for (i = 0; i < free; ++i) { |
| 1816 | /* Bypass com_addop_varname because it will generate |
| 1817 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1818 | */ |
| 1819 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1820 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1821 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1822 | /* Special case: If a class contains a method with a |
| 1823 | free variable that has the same name as a method, |
| 1824 | the name will be considered free *and* local in the |
| 1825 | class. It should be handled by the closure, as |
| 1826 | well as by the normal name loookup logic. |
| 1827 | */ |
| 1828 | reftype = get_ref_type(c, name); |
| 1829 | if (reftype == CELL) |
| 1830 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1831 | else /* (reftype == FREE) */ |
| 1832 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1833 | if (arg == -1) { |
| 1834 | fprintf(stderr, |
| 1835 | "lookup %s in %s %d %d\n" |
| 1836 | "freevars of %s: %s\n", |
| 1837 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1838 | PyUnicode_AsUTF8(c->u->u_name), |
| 1839 | reftype, arg, |
| 1840 | PyUnicode_AsUTF8(co->co_name), |
| 1841 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1842 | Py_FatalError("compiler_make_closure()"); |
| 1843 | } |
| 1844 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1846 | flags |= 0x08; |
| 1847 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1849 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1850 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1851 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | static int |
| 1856 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | if (!decos) |
| 1861 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1862 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1863 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1864 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1865 | } |
| 1866 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1870 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1871 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1872 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1873 | /* Push a dict of keyword-only default values. |
| 1874 | |
| 1875 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1876 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1877 | int i; |
| 1878 | PyObject *keys = NULL; |
| 1879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1881 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1882 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1883 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1884 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1885 | if (!mangled) { |
| 1886 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1887 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1888 | if (keys == NULL) { |
| 1889 | keys = PyList_New(1); |
| 1890 | if (keys == NULL) { |
| 1891 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1892 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1893 | } |
| 1894 | PyList_SET_ITEM(keys, 0, mangled); |
| 1895 | } |
| 1896 | else { |
| 1897 | int res = PyList_Append(keys, mangled); |
| 1898 | Py_DECREF(mangled); |
| 1899 | if (res == -1) { |
| 1900 | goto error; |
| 1901 | } |
| 1902 | } |
| 1903 | if (!compiler_visit_expr(c, default_)) { |
| 1904 | goto error; |
| 1905 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1908 | if (keys != NULL) { |
| 1909 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 1910 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 1911 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1912 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1913 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1914 | assert(default_count > 0); |
| 1915 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1916 | } |
| 1917 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1918 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | error: |
| 1922 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1923 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1927 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 1928 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 1929 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1930 | return 1; |
| 1931 | } |
| 1932 | |
| 1933 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1934 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 1935 | expr_ty annotation, PyObject *names) |
| 1936 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1937 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1938 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1939 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 1940 | VISIT(c, annexpr, annotation) |
| 1941 | } |
| 1942 | else { |
| 1943 | VISIT(c, expr, annotation); |
| 1944 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1945 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1946 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1947 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1948 | if (PyList_Append(names, mangled) < 0) { |
| 1949 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1950 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1951 | } |
| 1952 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1954 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | static int |
| 1958 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 1959 | PyObject *names) |
| 1960 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1961 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1963 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1964 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1965 | c, |
| 1966 | arg->arg, |
| 1967 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1968 | names)) |
| 1969 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1971 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | static int |
| 1975 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 1976 | expr_ty returns) |
| 1977 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1978 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1979 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1980 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1981 | 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] | 1982 | */ |
| 1983 | static identifier return_str; |
| 1984 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1985 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | names = PyList_New(0); |
| 1987 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 1988 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1989 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1990 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1991 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 1992 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 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; |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2612 | if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ |
| 2613 | c->u->u_ste->ste_coroutine = 1; |
| 2614 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2615 | return compiler_error(c, "'async for' outside async function"); |
| 2616 | } |
| 2617 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2618 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2619 | except = compiler_new_block(c); |
| 2620 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2621 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2622 | if (start == NULL || except == NULL || end == NULL) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2623 | return 0; |
| 2624 | |
| 2625 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2626 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2627 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2628 | compiler_use_next_block(c, start); |
| 2629 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
| 2630 | return 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2631 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2632 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2633 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2634 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2635 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2636 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2637 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2638 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2639 | /* Success block for __anext__ */ |
| 2640 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2641 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2642 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2643 | |
| 2644 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2645 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2646 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2647 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2648 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2649 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2650 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2651 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2652 | |
| 2653 | compiler_use_next_block(c, end); |
| 2654 | |
| 2655 | return 1; |
| 2656 | } |
| 2657 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2658 | static int |
| 2659 | compiler_while(struct compiler *c, stmt_ty s) |
| 2660 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2662 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | if (constant == 0) { |
| 2665 | if (s->v.While.orelse) |
| 2666 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2667 | return 1; |
| 2668 | } |
| 2669 | loop = compiler_new_block(c); |
| 2670 | end = compiler_new_block(c); |
| 2671 | if (constant == -1) { |
| 2672 | anchor = compiler_new_block(c); |
| 2673 | if (anchor == NULL) |
| 2674 | return 0; |
| 2675 | } |
| 2676 | if (loop == NULL || end == NULL) |
| 2677 | return 0; |
| 2678 | if (s->v.While.orelse) { |
| 2679 | orelse = compiler_new_block(c); |
| 2680 | if (orelse == NULL) |
| 2681 | return 0; |
| 2682 | } |
| 2683 | else |
| 2684 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2686 | compiler_use_next_block(c, loop); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2687 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2688 | return 0; |
| 2689 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2690 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2691 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | } |
| 2693 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2694 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2696 | /* XXX should the two POP instructions be in a separate block |
| 2697 | if there is no else clause ? |
| 2698 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2700 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2701 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2702 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2704 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2705 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2706 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2708 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2712 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2713 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2714 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2715 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2716 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2717 | return compiler_error(c, "'return' outside function"); |
| 2718 | if (s->v.Return.value != NULL && |
| 2719 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2720 | { |
| 2721 | return compiler_error( |
| 2722 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2724 | if (preserve_tos) { |
| 2725 | VISIT(c, expr, s->v.Return.value); |
| 2726 | } |
| 2727 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2728 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2729 | |
| 2730 | if (!compiler_unwind_fblock(c, info, preserve_tos)) |
| 2731 | return 0; |
| 2732 | } |
| 2733 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2734 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2735 | } |
| 2736 | else if (!preserve_tos) { |
| 2737 | VISIT(c, expr, s->v.Return.value); |
| 2738 | } |
| 2739 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2744 | static int |
| 2745 | compiler_break(struct compiler *c) |
| 2746 | { |
| 2747 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2748 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2749 | |
| 2750 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2751 | return 0; |
| 2752 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2753 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit); |
| 2754 | return 1; |
| 2755 | } |
| 2756 | } |
| 2757 | return compiler_error(c, "'break' outside loop"); |
| 2758 | } |
| 2759 | |
| 2760 | static int |
| 2761 | compiler_continue(struct compiler *c) |
| 2762 | { |
| 2763 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2764 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2765 | |
| 2766 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2767 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block); |
| 2768 | return 1; |
| 2769 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2770 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2771 | return 0; |
| 2772 | } |
| 2773 | return compiler_error(c, "'continue' not properly in loop"); |
| 2774 | } |
| 2775 | |
| 2776 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2777 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | |
| 2779 | SETUP_FINALLY L |
| 2780 | <code for body> |
| 2781 | POP_BLOCK |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2782 | BEGIN_FINALLY |
| 2783 | L: |
| 2784 | <code for finalbody> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | END_FINALLY |
| 2786 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2787 | The special instructions use the block stack. Each block |
| 2788 | stack entry contains the instruction that created it (here |
| 2789 | SETUP_FINALLY), the level of the value stack at the time the |
| 2790 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2791 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2792 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | Pushes the current value stack level and the label |
| 2794 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2795 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2796 | Pops en entry from the block stack. |
| 2797 | BEGIN_FINALLY |
| 2798 | Pushes NULL onto the value stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2799 | END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2800 | Pops 1 (NULL or int) or 6 entries from the *value* stack and restore |
| 2801 | the raised and the caught exceptions they specify. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2803 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2804 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2805 | exceptions are pushed onto the value stack (and the exception |
| 2806 | condition is cleared), and the interpreter jumps to the label |
| 2807 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2808 | */ |
| 2809 | |
| 2810 | static int |
| 2811 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | basicblock *body, *end; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2815 | body = compiler_new_block(c); |
| 2816 | end = compiler_new_block(c); |
| 2817 | if (body == NULL || end == NULL) |
| 2818 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2819 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2820 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2821 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2822 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2823 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2825 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2826 | if (!compiler_try_except(c, s)) |
| 2827 | return 0; |
| 2828 | } |
| 2829 | else { |
| 2830 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2831 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2833 | ADDOP(c, BEGIN_FINALLY); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | compiler_pop_fblock(c, FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2835 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2836 | /* `finally` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2837 | compiler_use_next_block(c, end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2838 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2840 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | ADDOP(c, END_FINALLY); |
| 2842 | compiler_pop_fblock(c, FINALLY_END, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2847 | 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] | 2848 | (The contents of the value stack is shown in [], with the top |
| 2849 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2850 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | |
| 2852 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2853 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2854 | [] <code for S> |
| 2855 | [] POP_BLOCK |
| 2856 | [] JUMP_FORWARD L0 |
| 2857 | |
| 2858 | [tb, val, exc] L1: DUP ) |
| 2859 | [tb, val, exc, exc] <evaluate E1> ) |
| 2860 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2861 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2862 | [tb, val, exc] POP |
| 2863 | [tb, val] <assign to V1> (or POP if no V1) |
| 2864 | [tb] POP |
| 2865 | [] <code for S1> |
| 2866 | JUMP_FORWARD L0 |
| 2867 | |
| 2868 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | .............................etc....................... |
| 2870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2872 | |
| 2873 | [] L0: <next statement> |
| 2874 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | Of course, parts are not generated if Vi or Ei is not present. |
| 2876 | */ |
| 2877 | static int |
| 2878 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2881 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | body = compiler_new_block(c); |
| 2884 | except = compiler_new_block(c); |
| 2885 | orelse = compiler_new_block(c); |
| 2886 | end = compiler_new_block(c); |
| 2887 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2888 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2889 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2891 | if (!compiler_push_fblock(c, EXCEPT, body, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2893 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2894 | ADDOP(c, POP_BLOCK); |
| 2895 | compiler_pop_fblock(c, EXCEPT, body); |
| 2896 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2897 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2898 | compiler_use_next_block(c, except); |
| 2899 | for (i = 0; i < n; i++) { |
| 2900 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2901 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 2903 | return compiler_error(c, "default 'except:' must be last"); |
| 2904 | c->u->u_lineno_set = 0; |
| 2905 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 2906 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | except = compiler_new_block(c); |
| 2908 | if (except == NULL) |
| 2909 | return 0; |
| 2910 | if (handler->v.ExceptHandler.type) { |
| 2911 | ADDOP(c, DUP_TOP); |
| 2912 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 2913 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 2914 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 2915 | } |
| 2916 | ADDOP(c, POP_TOP); |
| 2917 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2918 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2919 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2920 | cleanup_end = compiler_new_block(c); |
| 2921 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 2922 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2923 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 2924 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2925 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2926 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 2927 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2929 | /* |
| 2930 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2931 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2932 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2933 | try: |
| 2934 | # body |
| 2935 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 2936 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2937 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2938 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2940 | /* second try: */ |
| 2941 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 2942 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2943 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2944 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2946 | /* second # body */ |
| 2947 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 2948 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2949 | ADDOP(c, BEGIN_FINALLY); |
| 2950 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2952 | /* finally: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2953 | compiler_use_next_block(c, cleanup_end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2954 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2955 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2956 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2957 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2958 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2959 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2960 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2961 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2962 | ADDOP(c, END_FINALLY); |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 2963 | ADDOP(c, POP_EXCEPT); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2964 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | } |
| 2966 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2967 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2968 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2969 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2970 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2971 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2972 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2973 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2974 | ADDOP(c, POP_TOP); |
| 2975 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2976 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2977 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2979 | ADDOP(c, POP_EXCEPT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2980 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2981 | } |
| 2982 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2983 | compiler_use_next_block(c, except); |
| 2984 | } |
| 2985 | ADDOP(c, END_FINALLY); |
| 2986 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2987 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2988 | compiler_use_next_block(c, end); |
| 2989 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2993 | compiler_try(struct compiler *c, stmt_ty s) { |
| 2994 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 2995 | return compiler_try_finally(c, s); |
| 2996 | else |
| 2997 | return compiler_try_except(c, s); |
| 2998 | } |
| 2999 | |
| 3000 | |
| 3001 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3002 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3003 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | /* The IMPORT_NAME opcode was already generated. This function |
| 3005 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | 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] | 3008 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3009 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3010 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3011 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3012 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3013 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3014 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3015 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3016 | while (1) { |
| 3017 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3019 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3020 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3021 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3022 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3023 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3024 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3025 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3026 | if (dot == -1) { |
| 3027 | break; |
| 3028 | } |
| 3029 | ADDOP(c, ROT_TWO); |
| 3030 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3032 | if (!compiler_nameop(c, asname, Store)) { |
| 3033 | return 0; |
| 3034 | } |
| 3035 | ADDOP(c, POP_TOP); |
| 3036 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | } |
| 3038 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | static int |
| 3042 | compiler_import(struct compiler *c, stmt_ty s) |
| 3043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3044 | /* The Import node stores a module name like a.b.c as a single |
| 3045 | string. This is convenient for all cases except |
| 3046 | import a.b.c as d |
| 3047 | where we need to parse that string to extract the individual |
| 3048 | module names. |
| 3049 | XXX Perhaps change the representation to make this case simpler? |
| 3050 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3051 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3053 | for (i = 0; i < n; i++) { |
| 3054 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3055 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3056 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3057 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3058 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3060 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3061 | if (alias->asname) { |
| 3062 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3063 | if (!r) |
| 3064 | return r; |
| 3065 | } |
| 3066 | else { |
| 3067 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3068 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3069 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3070 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3071 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3072 | if (tmp == NULL) |
| 3073 | return 0; |
| 3074 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3076 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3077 | Py_DECREF(tmp); |
| 3078 | } |
| 3079 | if (!r) |
| 3080 | return r; |
| 3081 | } |
| 3082 | } |
| 3083 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | static int |
| 3087 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3088 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3089 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3090 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | if (!empty_string) { |
| 3094 | empty_string = PyUnicode_FromString(""); |
| 3095 | if (!empty_string) |
| 3096 | return 0; |
| 3097 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3098 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3099 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3100 | |
| 3101 | names = PyTuple_New(n); |
| 3102 | if (!names) |
| 3103 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3105 | /* build up the names */ |
| 3106 | for (i = 0; i < n; i++) { |
| 3107 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3108 | Py_INCREF(alias->name); |
| 3109 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3110 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3113 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3114 | Py_DECREF(names); |
| 3115 | return compiler_error(c, "from __future__ imports must occur " |
| 3116 | "at the beginning of the file"); |
| 3117 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3118 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3119 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | if (s->v.ImportFrom.module) { |
| 3121 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3122 | } |
| 3123 | else { |
| 3124 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3125 | } |
| 3126 | for (i = 0; i < n; i++) { |
| 3127 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3128 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3129 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3130 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3131 | assert(n == 1); |
| 3132 | ADDOP(c, IMPORT_STAR); |
| 3133 | return 1; |
| 3134 | } |
| 3135 | |
| 3136 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3137 | store_name = alias->name; |
| 3138 | if (alias->asname) |
| 3139 | store_name = alias->asname; |
| 3140 | |
| 3141 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | return 0; |
| 3143 | } |
| 3144 | } |
| 3145 | /* remove imported module */ |
| 3146 | ADDOP(c, POP_TOP); |
| 3147 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | static int |
| 3151 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3152 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3153 | static PyObject *assertion_error = NULL; |
| 3154 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3155 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3156 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | return 1; |
| 3158 | if (assertion_error == NULL) { |
| 3159 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 3160 | if (assertion_error == NULL) |
| 3161 | return 0; |
| 3162 | } |
| 3163 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3164 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3165 | { |
| 3166 | if (!compiler_warn(c, "assertion is always true, " |
| 3167 | "perhaps remove parentheses?")) |
| 3168 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3169 | return 0; |
| 3170 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3171 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | end = compiler_new_block(c); |
| 3173 | if (end == NULL) |
| 3174 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3175 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3176 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3177 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 3178 | if (s->v.Assert.msg) { |
| 3179 | VISIT(c, expr, s->v.Assert.msg); |
| 3180 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3181 | } |
| 3182 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3183 | compiler_use_next_block(c, end); |
| 3184 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3188 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3189 | { |
| 3190 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3191 | VISIT(c, expr, value); |
| 3192 | ADDOP(c, PRINT_EXPR); |
| 3193 | return 1; |
| 3194 | } |
| 3195 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3196 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3197 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3198 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | VISIT(c, expr, value); |
| 3202 | ADDOP(c, POP_TOP); |
| 3203 | return 1; |
| 3204 | } |
| 3205 | |
| 3206 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3208 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3209 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 3212 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3213 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | switch (s->kind) { |
| 3217 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3218 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3219 | case ClassDef_kind: |
| 3220 | return compiler_class(c, s); |
| 3221 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3222 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | case Delete_kind: |
| 3224 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3225 | break; |
| 3226 | case Assign_kind: |
| 3227 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3228 | VISIT(c, expr, s->v.Assign.value); |
| 3229 | for (i = 0; i < n; i++) { |
| 3230 | if (i < n - 1) |
| 3231 | ADDOP(c, DUP_TOP); |
| 3232 | VISIT(c, expr, |
| 3233 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3234 | } |
| 3235 | break; |
| 3236 | case AugAssign_kind: |
| 3237 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3238 | case AnnAssign_kind: |
| 3239 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3240 | case For_kind: |
| 3241 | return compiler_for(c, s); |
| 3242 | case While_kind: |
| 3243 | return compiler_while(c, s); |
| 3244 | case If_kind: |
| 3245 | return compiler_if(c, s); |
| 3246 | case Raise_kind: |
| 3247 | n = 0; |
| 3248 | if (s->v.Raise.exc) { |
| 3249 | VISIT(c, expr, s->v.Raise.exc); |
| 3250 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3251 | if (s->v.Raise.cause) { |
| 3252 | VISIT(c, expr, s->v.Raise.cause); |
| 3253 | n++; |
| 3254 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3255 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3256 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3258 | case Try_kind: |
| 3259 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | case Assert_kind: |
| 3261 | return compiler_assert(c, s); |
| 3262 | case Import_kind: |
| 3263 | return compiler_import(c, s); |
| 3264 | case ImportFrom_kind: |
| 3265 | return compiler_from_import(c, s); |
| 3266 | case Global_kind: |
| 3267 | case Nonlocal_kind: |
| 3268 | break; |
| 3269 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3270 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3271 | case Pass_kind: |
| 3272 | break; |
| 3273 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3274 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | case Continue_kind: |
| 3276 | return compiler_continue(c); |
| 3277 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3278 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3279 | case AsyncFunctionDef_kind: |
| 3280 | return compiler_function(c, s, 1); |
| 3281 | case AsyncWith_kind: |
| 3282 | return compiler_async_with(c, s, 0); |
| 3283 | case AsyncFor_kind: |
| 3284 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3285 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3287 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | static int |
| 3291 | unaryop(unaryop_ty op) |
| 3292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3293 | switch (op) { |
| 3294 | case Invert: |
| 3295 | return UNARY_INVERT; |
| 3296 | case Not: |
| 3297 | return UNARY_NOT; |
| 3298 | case UAdd: |
| 3299 | return UNARY_POSITIVE; |
| 3300 | case USub: |
| 3301 | return UNARY_NEGATIVE; |
| 3302 | default: |
| 3303 | PyErr_Format(PyExc_SystemError, |
| 3304 | "unary op %d should not be possible", op); |
| 3305 | return 0; |
| 3306 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | static int |
| 3310 | binop(struct compiler *c, operator_ty op) |
| 3311 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3312 | switch (op) { |
| 3313 | case Add: |
| 3314 | return BINARY_ADD; |
| 3315 | case Sub: |
| 3316 | return BINARY_SUBTRACT; |
| 3317 | case Mult: |
| 3318 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3319 | case MatMult: |
| 3320 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3321 | case Div: |
| 3322 | return BINARY_TRUE_DIVIDE; |
| 3323 | case Mod: |
| 3324 | return BINARY_MODULO; |
| 3325 | case Pow: |
| 3326 | return BINARY_POWER; |
| 3327 | case LShift: |
| 3328 | return BINARY_LSHIFT; |
| 3329 | case RShift: |
| 3330 | return BINARY_RSHIFT; |
| 3331 | case BitOr: |
| 3332 | return BINARY_OR; |
| 3333 | case BitXor: |
| 3334 | return BINARY_XOR; |
| 3335 | case BitAnd: |
| 3336 | return BINARY_AND; |
| 3337 | case FloorDiv: |
| 3338 | return BINARY_FLOOR_DIVIDE; |
| 3339 | default: |
| 3340 | PyErr_Format(PyExc_SystemError, |
| 3341 | "binary op %d should not be possible", op); |
| 3342 | return 0; |
| 3343 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3347 | inplace_binop(struct compiler *c, operator_ty op) |
| 3348 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3349 | switch (op) { |
| 3350 | case Add: |
| 3351 | return INPLACE_ADD; |
| 3352 | case Sub: |
| 3353 | return INPLACE_SUBTRACT; |
| 3354 | case Mult: |
| 3355 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3356 | case MatMult: |
| 3357 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3358 | case Div: |
| 3359 | return INPLACE_TRUE_DIVIDE; |
| 3360 | case Mod: |
| 3361 | return INPLACE_MODULO; |
| 3362 | case Pow: |
| 3363 | return INPLACE_POWER; |
| 3364 | case LShift: |
| 3365 | return INPLACE_LSHIFT; |
| 3366 | case RShift: |
| 3367 | return INPLACE_RSHIFT; |
| 3368 | case BitOr: |
| 3369 | return INPLACE_OR; |
| 3370 | case BitXor: |
| 3371 | return INPLACE_XOR; |
| 3372 | case BitAnd: |
| 3373 | return INPLACE_AND; |
| 3374 | case FloorDiv: |
| 3375 | return INPLACE_FLOOR_DIVIDE; |
| 3376 | default: |
| 3377 | PyErr_Format(PyExc_SystemError, |
| 3378 | "inplace binary op %d should not be possible", op); |
| 3379 | return 0; |
| 3380 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3381 | } |
| 3382 | |
| 3383 | static int |
| 3384 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3385 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3386 | int op, scope; |
| 3387 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | PyObject *dict = c->u->u_names; |
| 3391 | PyObject *mangled; |
| 3392 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3393 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3394 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3395 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3396 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3397 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3398 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3399 | if (!mangled) |
| 3400 | return 0; |
| 3401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | op = 0; |
| 3403 | optype = OP_NAME; |
| 3404 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3405 | switch (scope) { |
| 3406 | case FREE: |
| 3407 | dict = c->u->u_freevars; |
| 3408 | optype = OP_DEREF; |
| 3409 | break; |
| 3410 | case CELL: |
| 3411 | dict = c->u->u_cellvars; |
| 3412 | optype = OP_DEREF; |
| 3413 | break; |
| 3414 | case LOCAL: |
| 3415 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3416 | optype = OP_FAST; |
| 3417 | break; |
| 3418 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3419 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3420 | optype = OP_GLOBAL; |
| 3421 | break; |
| 3422 | case GLOBAL_EXPLICIT: |
| 3423 | optype = OP_GLOBAL; |
| 3424 | break; |
| 3425 | default: |
| 3426 | /* scope can be 0 */ |
| 3427 | break; |
| 3428 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3430 | /* 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] | 3431 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | switch (optype) { |
| 3434 | case OP_DEREF: |
| 3435 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3436 | case Load: |
| 3437 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3438 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3439 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3440 | op = STORE_DEREF; |
| 3441 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3442 | case AugLoad: |
| 3443 | case AugStore: |
| 3444 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3445 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3446 | case Param: |
| 3447 | default: |
| 3448 | PyErr_SetString(PyExc_SystemError, |
| 3449 | "param invalid for deref variable"); |
| 3450 | return 0; |
| 3451 | } |
| 3452 | break; |
| 3453 | case OP_FAST: |
| 3454 | switch (ctx) { |
| 3455 | case Load: op = LOAD_FAST; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3456 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3457 | op = STORE_FAST; |
| 3458 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | case Del: op = DELETE_FAST; break; |
| 3460 | case AugLoad: |
| 3461 | case AugStore: |
| 3462 | break; |
| 3463 | case Param: |
| 3464 | default: |
| 3465 | PyErr_SetString(PyExc_SystemError, |
| 3466 | "param invalid for local variable"); |
| 3467 | return 0; |
| 3468 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3469 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | return 1; |
| 3471 | case OP_GLOBAL: |
| 3472 | switch (ctx) { |
| 3473 | case Load: op = LOAD_GLOBAL; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3474 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3475 | op = STORE_GLOBAL; |
| 3476 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3477 | case Del: op = DELETE_GLOBAL; break; |
| 3478 | case AugLoad: |
| 3479 | case AugStore: |
| 3480 | break; |
| 3481 | case Param: |
| 3482 | default: |
| 3483 | PyErr_SetString(PyExc_SystemError, |
| 3484 | "param invalid for global variable"); |
| 3485 | return 0; |
| 3486 | } |
| 3487 | break; |
| 3488 | case OP_NAME: |
| 3489 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3490 | case Load: op = LOAD_NAME; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3491 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3492 | op = STORE_NAME; |
| 3493 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3494 | case Del: op = DELETE_NAME; break; |
| 3495 | case AugLoad: |
| 3496 | case AugStore: |
| 3497 | break; |
| 3498 | case Param: |
| 3499 | default: |
| 3500 | PyErr_SetString(PyExc_SystemError, |
| 3501 | "param invalid for name variable"); |
| 3502 | return 0; |
| 3503 | } |
| 3504 | break; |
| 3505 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | assert(op); |
| 3508 | arg = compiler_add_o(c, dict, mangled); |
| 3509 | Py_DECREF(mangled); |
| 3510 | if (arg < 0) |
| 3511 | return 0; |
| 3512 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | static int |
| 3516 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3517 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3518 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3519 | int jumpi; |
| 3520 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | assert(e->kind == BoolOp_kind); |
| 3524 | if (e->v.BoolOp.op == And) |
| 3525 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3526 | else |
| 3527 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3528 | end = compiler_new_block(c); |
| 3529 | if (end == NULL) |
| 3530 | return 0; |
| 3531 | s = e->v.BoolOp.values; |
| 3532 | n = asdl_seq_LEN(s) - 1; |
| 3533 | assert(n >= 0); |
| 3534 | for (i = 0; i < n; ++i) { |
| 3535 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3536 | ADDOP_JABS(c, jumpi, end); |
| 3537 | } |
| 3538 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3539 | compiler_use_next_block(c, end); |
| 3540 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3544 | starunpack_helper(struct compiler *c, asdl_seq *elts, |
| 3545 | int single_op, int inner_op, int outer_op) |
| 3546 | { |
| 3547 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3548 | Py_ssize_t i, nsubitems = 0, nseen = 0; |
| 3549 | for (i = 0; i < n; i++) { |
| 3550 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3551 | if (elt->kind == Starred_kind) { |
| 3552 | if (nseen) { |
| 3553 | ADDOP_I(c, inner_op, nseen); |
| 3554 | nseen = 0; |
| 3555 | nsubitems++; |
| 3556 | } |
| 3557 | VISIT(c, expr, elt->v.Starred.value); |
| 3558 | nsubitems++; |
| 3559 | } |
| 3560 | else { |
| 3561 | VISIT(c, expr, elt); |
| 3562 | nseen++; |
| 3563 | } |
| 3564 | } |
| 3565 | if (nsubitems) { |
| 3566 | if (nseen) { |
| 3567 | ADDOP_I(c, inner_op, nseen); |
| 3568 | nsubitems++; |
| 3569 | } |
| 3570 | ADDOP_I(c, outer_op, nsubitems); |
| 3571 | } |
| 3572 | else |
| 3573 | ADDOP_I(c, single_op, nseen); |
| 3574 | return 1; |
| 3575 | } |
| 3576 | |
| 3577 | static int |
| 3578 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3579 | { |
| 3580 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3581 | Py_ssize_t i; |
| 3582 | int seen_star = 0; |
| 3583 | for (i = 0; i < n; i++) { |
| 3584 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3585 | if (elt->kind == Starred_kind && !seen_star) { |
| 3586 | if ((i >= (1 << 8)) || |
| 3587 | (n-i-1 >= (INT_MAX >> 8))) |
| 3588 | return compiler_error(c, |
| 3589 | "too many expressions in " |
| 3590 | "star-unpacking assignment"); |
| 3591 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3592 | seen_star = 1; |
| 3593 | asdl_seq_SET(elts, i, elt->v.Starred.value); |
| 3594 | } |
| 3595 | else if (elt->kind == Starred_kind) { |
| 3596 | return compiler_error(c, |
| 3597 | "two starred expressions in assignment"); |
| 3598 | } |
| 3599 | } |
| 3600 | if (!seen_star) { |
| 3601 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3602 | } |
| 3603 | VISIT_SEQ(c, expr, elts); |
| 3604 | return 1; |
| 3605 | } |
| 3606 | |
| 3607 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3608 | compiler_list(struct compiler *c, expr_ty e) |
| 3609 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3610 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3611 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3612 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3614 | else if (e->v.List.ctx == Load) { |
| 3615 | return starunpack_helper(c, elts, |
| 3616 | BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3617 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3618 | else |
| 3619 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3620 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | static int |
| 3624 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3625 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3626 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3627 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3628 | return assignment_helper(c, elts); |
| 3629 | } |
| 3630 | else if (e->v.Tuple.ctx == Load) { |
| 3631 | return starunpack_helper(c, elts, |
| 3632 | BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK); |
| 3633 | } |
| 3634 | else |
| 3635 | VISIT_SEQ(c, expr, elts); |
| 3636 | return 1; |
| 3637 | } |
| 3638 | |
| 3639 | static int |
| 3640 | compiler_set(struct compiler *c, expr_ty e) |
| 3641 | { |
| 3642 | return starunpack_helper(c, e->v.Set.elts, BUILD_SET, |
| 3643 | BUILD_SET, BUILD_SET_UNPACK); |
| 3644 | } |
| 3645 | |
| 3646 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3647 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3648 | { |
| 3649 | Py_ssize_t i; |
| 3650 | for (i = begin; i < end; i++) { |
| 3651 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3652 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3653 | return 0; |
| 3654 | } |
| 3655 | return 1; |
| 3656 | } |
| 3657 | |
| 3658 | static int |
| 3659 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3660 | { |
| 3661 | Py_ssize_t i, n = end - begin; |
| 3662 | PyObject *keys, *key; |
| 3663 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3664 | for (i = begin; i < end; i++) { |
| 3665 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3666 | } |
| 3667 | keys = PyTuple_New(n); |
| 3668 | if (keys == NULL) { |
| 3669 | return 0; |
| 3670 | } |
| 3671 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3672 | 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] | 3673 | Py_INCREF(key); |
| 3674 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3675 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3676 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3677 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3678 | } |
| 3679 | else { |
| 3680 | for (i = begin; i < end; i++) { |
| 3681 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3682 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3683 | } |
| 3684 | ADDOP_I(c, BUILD_MAP, n); |
| 3685 | } |
| 3686 | return 1; |
| 3687 | } |
| 3688 | |
| 3689 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3690 | compiler_dict(struct compiler *c, expr_ty e) |
| 3691 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3692 | Py_ssize_t i, n, elements; |
| 3693 | int containers; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3694 | int is_unpacking = 0; |
| 3695 | n = asdl_seq_LEN(e->v.Dict.values); |
| 3696 | containers = 0; |
| 3697 | elements = 0; |
| 3698 | for (i = 0; i < n; i++) { |
| 3699 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
| 3700 | if (elements == 0xFFFF || (elements && is_unpacking)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3701 | if (!compiler_subdict(c, e, i - elements, i)) |
| 3702 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3703 | containers++; |
| 3704 | elements = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3705 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3706 | if (is_unpacking) { |
| 3707 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3708 | containers++; |
| 3709 | } |
| 3710 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3711 | elements++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3712 | } |
| 3713 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3714 | if (elements || containers == 0) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3715 | if (!compiler_subdict(c, e, n - elements, n)) |
| 3716 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3717 | containers++; |
| 3718 | } |
| 3719 | /* If there is more than one dict, they need to be merged into a new |
| 3720 | * dict. If there is one dict and it's an unpacking, then it needs |
| 3721 | * to be copied into a new dict." */ |
Serhiy Storchaka | 3d85fae | 2016-11-28 20:56:37 +0200 | [diff] [blame] | 3722 | if (containers > 1 || is_unpacking) { |
| 3723 | ADDOP_I(c, BUILD_MAP_UNPACK, containers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3724 | } |
| 3725 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | static int |
| 3729 | compiler_compare(struct compiler *c, expr_ty e) |
| 3730 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3731 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3732 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3733 | if (!check_compare(c, e)) { |
| 3734 | return 0; |
| 3735 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3736 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3737 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3738 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3739 | if (n == 0) { |
| 3740 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 3741 | ADDOP_I(c, COMPARE_OP, |
| 3742 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0)))); |
| 3743 | } |
| 3744 | else { |
| 3745 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3746 | if (cleanup == NULL) |
| 3747 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3748 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | VISIT(c, expr, |
| 3750 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3751 | ADDOP(c, DUP_TOP); |
| 3752 | ADDOP(c, ROT_THREE); |
| 3753 | ADDOP_I(c, COMPARE_OP, |
| 3754 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 3755 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3756 | NEXT_BLOCK(c); |
| 3757 | } |
| 3758 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 3759 | ADDOP_I(c, COMPARE_OP, |
| 3760 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3761 | basicblock *end = compiler_new_block(c); |
| 3762 | if (end == NULL) |
| 3763 | return 0; |
| 3764 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3765 | compiler_use_next_block(c, cleanup); |
| 3766 | ADDOP(c, ROT_TWO); |
| 3767 | ADDOP(c, POP_TOP); |
| 3768 | compiler_use_next_block(c, end); |
| 3769 | } |
| 3770 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3773 | static PyTypeObject * |
| 3774 | infer_type(expr_ty e) |
| 3775 | { |
| 3776 | switch (e->kind) { |
| 3777 | case Tuple_kind: |
| 3778 | return &PyTuple_Type; |
| 3779 | case List_kind: |
| 3780 | case ListComp_kind: |
| 3781 | return &PyList_Type; |
| 3782 | case Dict_kind: |
| 3783 | case DictComp_kind: |
| 3784 | return &PyDict_Type; |
| 3785 | case Set_kind: |
| 3786 | case SetComp_kind: |
| 3787 | return &PySet_Type; |
| 3788 | case GeneratorExp_kind: |
| 3789 | return &PyGen_Type; |
| 3790 | case Lambda_kind: |
| 3791 | return &PyFunction_Type; |
| 3792 | case JoinedStr_kind: |
| 3793 | case FormattedValue_kind: |
| 3794 | return &PyUnicode_Type; |
| 3795 | case Constant_kind: |
| 3796 | return e->v.Constant.value->ob_type; |
| 3797 | default: |
| 3798 | return NULL; |
| 3799 | } |
| 3800 | } |
| 3801 | |
| 3802 | static int |
| 3803 | check_caller(struct compiler *c, expr_ty e) |
| 3804 | { |
| 3805 | switch (e->kind) { |
| 3806 | case Constant_kind: |
| 3807 | case Tuple_kind: |
| 3808 | case List_kind: |
| 3809 | case ListComp_kind: |
| 3810 | case Dict_kind: |
| 3811 | case DictComp_kind: |
| 3812 | case Set_kind: |
| 3813 | case SetComp_kind: |
| 3814 | case GeneratorExp_kind: |
| 3815 | case JoinedStr_kind: |
| 3816 | case FormattedValue_kind: |
| 3817 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3818 | "perhaps you missed a comma?", |
| 3819 | infer_type(e)->tp_name); |
| 3820 | default: |
| 3821 | return 1; |
| 3822 | } |
| 3823 | } |
| 3824 | |
| 3825 | static int |
| 3826 | check_subscripter(struct compiler *c, expr_ty e) |
| 3827 | { |
| 3828 | PyObject *v; |
| 3829 | |
| 3830 | switch (e->kind) { |
| 3831 | case Constant_kind: |
| 3832 | v = e->v.Constant.value; |
| 3833 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3834 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3835 | PyAnySet_Check(v))) |
| 3836 | { |
| 3837 | return 1; |
| 3838 | } |
| 3839 | /* fall through */ |
| 3840 | case Set_kind: |
| 3841 | case SetComp_kind: |
| 3842 | case GeneratorExp_kind: |
| 3843 | case Lambda_kind: |
| 3844 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3845 | "perhaps you missed a comma?", |
| 3846 | infer_type(e)->tp_name); |
| 3847 | default: |
| 3848 | return 1; |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | static int |
| 3853 | check_index(struct compiler *c, expr_ty e, slice_ty s) |
| 3854 | { |
| 3855 | PyObject *v; |
| 3856 | |
| 3857 | if (s->kind != Index_kind) { |
| 3858 | return 1; |
| 3859 | } |
| 3860 | PyTypeObject *index_type = infer_type(s->v.Index.value); |
| 3861 | if (index_type == NULL |
| 3862 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 3863 | || index_type == &PySlice_Type) { |
| 3864 | return 1; |
| 3865 | } |
| 3866 | |
| 3867 | switch (e->kind) { |
| 3868 | case Constant_kind: |
| 3869 | v = e->v.Constant.value; |
| 3870 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 3871 | return 1; |
| 3872 | } |
| 3873 | /* fall through */ |
| 3874 | case Tuple_kind: |
| 3875 | case List_kind: |
| 3876 | case ListComp_kind: |
| 3877 | case JoinedStr_kind: |
| 3878 | case FormattedValue_kind: |
| 3879 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 3880 | "not %.200s; " |
| 3881 | "perhaps you missed a comma?", |
| 3882 | infer_type(e)->tp_name, |
| 3883 | index_type->tp_name); |
| 3884 | default: |
| 3885 | return 1; |
| 3886 | } |
| 3887 | } |
| 3888 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 3889 | // 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] | 3890 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3891 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 3892 | { |
| 3893 | Py_ssize_t argsl, i; |
| 3894 | expr_ty meth = e->v.Call.func; |
| 3895 | asdl_seq *args = e->v.Call.args; |
| 3896 | |
| 3897 | /* Check that the call node is an attribute access, and that |
| 3898 | the call doesn't have keyword parameters. */ |
| 3899 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 3900 | asdl_seq_LEN(e->v.Call.keywords)) |
| 3901 | return -1; |
| 3902 | |
| 3903 | /* Check that there are no *varargs types of arguments. */ |
| 3904 | argsl = asdl_seq_LEN(args); |
| 3905 | for (i = 0; i < argsl; i++) { |
| 3906 | expr_ty elt = asdl_seq_GET(args, i); |
| 3907 | if (elt->kind == Starred_kind) { |
| 3908 | return -1; |
| 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | /* Alright, we can optimize the code. */ |
| 3913 | VISIT(c, expr, meth->v.Attribute.value); |
| 3914 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 3915 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 3916 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 3917 | return 1; |
| 3918 | } |
| 3919 | |
| 3920 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3921 | compiler_call(struct compiler *c, expr_ty e) |
| 3922 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 3923 | int ret = maybe_optimize_method_call(c, e); |
| 3924 | if (ret >= 0) { |
| 3925 | return ret; |
| 3926 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3927 | if (!check_caller(c, e->v.Call.func)) { |
| 3928 | return 0; |
| 3929 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | VISIT(c, expr, e->v.Call.func); |
| 3931 | return compiler_call_helper(c, 0, |
| 3932 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3933 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3936 | static int |
| 3937 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 3938 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3939 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 3940 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 3941 | 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] | 3942 | return 1; |
| 3943 | } |
| 3944 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3945 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3946 | static int |
| 3947 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 3948 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3949 | /* Our oparg encodes 2 pieces of information: the conversion |
| 3950 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3951 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3952 | Convert the conversion char to 3 bits: |
| 3953 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3954 | !s : 001 0x1 FVC_STR |
| 3955 | !r : 010 0x2 FVC_REPR |
| 3956 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3957 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3958 | next bit is whether or not we have a format spec: |
| 3959 | yes : 100 0x4 |
| 3960 | no : 000 0x0 |
| 3961 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3962 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3963 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3964 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3965 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3966 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3967 | VISIT(c, expr, e->v.FormattedValue.value); |
| 3968 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3969 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3970 | case 's': oparg = FVC_STR; break; |
| 3971 | case 'r': oparg = FVC_REPR; break; |
| 3972 | case 'a': oparg = FVC_ASCII; break; |
| 3973 | case -1: oparg = FVC_NONE; break; |
| 3974 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3975 | PyErr_Format(PyExc_SystemError, |
| 3976 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3977 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3978 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3979 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3980 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3981 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3982 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3983 | } |
| 3984 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3985 | /* And push our opcode and oparg */ |
| 3986 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 3987 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3988 | return 1; |
| 3989 | } |
| 3990 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3991 | static int |
| 3992 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 3993 | { |
| 3994 | Py_ssize_t i, n = end - begin; |
| 3995 | keyword_ty kw; |
| 3996 | PyObject *keys, *key; |
| 3997 | assert(n > 0); |
| 3998 | if (n > 1) { |
| 3999 | for (i = begin; i < end; i++) { |
| 4000 | kw = asdl_seq_GET(keywords, i); |
| 4001 | VISIT(c, expr, kw->value); |
| 4002 | } |
| 4003 | keys = PyTuple_New(n); |
| 4004 | if (keys == NULL) { |
| 4005 | return 0; |
| 4006 | } |
| 4007 | for (i = begin; i < end; i++) { |
| 4008 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4009 | Py_INCREF(key); |
| 4010 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4011 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4012 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4013 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4014 | } |
| 4015 | else { |
| 4016 | /* a for loop only executes once */ |
| 4017 | for (i = begin; i < end; i++) { |
| 4018 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4019 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4020 | VISIT(c, expr, kw->value); |
| 4021 | } |
| 4022 | ADDOP_I(c, BUILD_MAP, n); |
| 4023 | } |
| 4024 | return 1; |
| 4025 | } |
| 4026 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4027 | /* shared code between compiler_call and compiler_class */ |
| 4028 | static int |
| 4029 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4030 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4031 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4032 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4033 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4034 | Py_ssize_t i, nseen, nelts, nkwelts; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4035 | int mustdictunpack = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4036 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4037 | /* the number of tuples and dictionaries on the stack */ |
| 4038 | Py_ssize_t nsubargs = 0, nsubkwargs = 0; |
| 4039 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4040 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4041 | nkwelts = asdl_seq_LEN(keywords); |
| 4042 | |
| 4043 | for (i = 0; i < nkwelts; i++) { |
| 4044 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4045 | if (kw->arg == NULL) { |
| 4046 | mustdictunpack = 1; |
| 4047 | break; |
| 4048 | } |
| 4049 | } |
| 4050 | |
| 4051 | nseen = n; /* the number of positional arguments on the stack */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4052 | for (i = 0; i < nelts; i++) { |
| 4053 | expr_ty elt = asdl_seq_GET(args, i); |
| 4054 | if (elt->kind == Starred_kind) { |
| 4055 | /* A star-arg. If we've seen positional arguments, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4056 | pack the positional arguments into a tuple. */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4057 | if (nseen) { |
| 4058 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4059 | nseen = 0; |
| 4060 | nsubargs++; |
| 4061 | } |
| 4062 | VISIT(c, expr, elt->v.Starred.value); |
| 4063 | nsubargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4064 | } |
| 4065 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4066 | VISIT(c, expr, elt); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4067 | nseen++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4068 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4069 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4070 | |
| 4071 | /* Same dance again for keyword arguments */ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4072 | if (nsubargs || mustdictunpack) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4073 | if (nseen) { |
| 4074 | /* Pack up any trailing positional arguments. */ |
| 4075 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4076 | nsubargs++; |
| 4077 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4078 | if (nsubargs > 1) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4079 | /* If we ended up with more than one stararg, we need |
| 4080 | to concatenate them into a single sequence. */ |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 4081 | ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4082 | } |
| 4083 | else if (nsubargs == 0) { |
| 4084 | ADDOP_I(c, BUILD_TUPLE, 0); |
| 4085 | } |
| 4086 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4087 | for (i = 0; i < nkwelts; i++) { |
| 4088 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4089 | if (kw->arg == NULL) { |
| 4090 | /* A keyword argument unpacking. */ |
| 4091 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4092 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) |
| 4093 | return 0; |
| 4094 | nsubkwargs++; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4095 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4096 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4097 | VISIT(c, expr, kw->value); |
| 4098 | nsubkwargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4099 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4100 | else { |
| 4101 | nseen++; |
| 4102 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4103 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4104 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4105 | /* Pack up any trailing keyword arguments. */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4106 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4107 | return 0; |
| 4108 | nsubkwargs++; |
| 4109 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4110 | if (nsubkwargs > 1) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4111 | /* Pack it all up */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4112 | ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4113 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4114 | ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); |
| 4115 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4116 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4117 | else if (nkwelts) { |
| 4118 | PyObject *names; |
| 4119 | VISIT_SEQ(c, keyword, keywords); |
| 4120 | names = PyTuple_New(nkwelts); |
| 4121 | if (names == NULL) { |
| 4122 | return 0; |
| 4123 | } |
| 4124 | for (i = 0; i < nkwelts; i++) { |
| 4125 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4126 | Py_INCREF(kw->arg); |
| 4127 | PyTuple_SET_ITEM(names, i, kw->arg); |
| 4128 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4129 | ADDOP_LOAD_CONST_NEW(c, names); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4130 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4131 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4132 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4133 | else { |
| 4134 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4135 | return 1; |
| 4136 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4139 | |
| 4140 | /* List and set comprehensions and generator expressions work by creating a |
| 4141 | nested function to perform the actual iteration. This means that the |
| 4142 | iteration variables don't leak into the current scope. |
| 4143 | The defined function is called immediately following its definition, with the |
| 4144 | result of that call being the result of the expression. |
| 4145 | The LC/SC version returns the populated container, while the GE version is |
| 4146 | flagged in symtable.c as a generator, so it returns the generator object |
| 4147 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4148 | |
| 4149 | Possible cleanups: |
| 4150 | - iterate over the generator sequence instead of using recursion |
| 4151 | */ |
| 4152 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4153 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4154 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4155 | compiler_comprehension_generator(struct compiler *c, |
| 4156 | asdl_seq *generators, int gen_index, |
| 4157 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4158 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4159 | comprehension_ty gen; |
| 4160 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4161 | if (gen->is_async) { |
| 4162 | return compiler_async_comprehension_generator( |
| 4163 | c, generators, gen_index, elt, val, type); |
| 4164 | } else { |
| 4165 | return compiler_sync_comprehension_generator( |
| 4166 | c, generators, gen_index, elt, val, type); |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | static int |
| 4171 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4172 | asdl_seq *generators, int gen_index, |
| 4173 | expr_ty elt, expr_ty val, int type) |
| 4174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 | /* generate code for the iterator, then each of the ifs, |
| 4176 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4178 | comprehension_ty gen; |
| 4179 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4180 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4182 | start = compiler_new_block(c); |
| 4183 | skip = compiler_new_block(c); |
| 4184 | if_cleanup = compiler_new_block(c); |
| 4185 | anchor = compiler_new_block(c); |
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 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4188 | anchor == NULL) |
| 4189 | return 0; |
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 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | if (gen_index == 0) { |
| 4194 | /* Receive outermost iter as an implicit argument */ |
| 4195 | c->u->u_argcount = 1; |
| 4196 | ADDOP_I(c, LOAD_FAST, 0); |
| 4197 | } |
| 4198 | else { |
| 4199 | /* Sub-iter - calculate on the fly */ |
| 4200 | VISIT(c, expr, gen->iter); |
| 4201 | ADDOP(c, GET_ITER); |
| 4202 | } |
| 4203 | compiler_use_next_block(c, start); |
| 4204 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4205 | NEXT_BLOCK(c); |
| 4206 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4208 | /* XXX this needs to be cleaned up...a lot! */ |
| 4209 | n = asdl_seq_LEN(gen->ifs); |
| 4210 | for (i = 0; i < n; i++) { |
| 4211 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4212 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4213 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | NEXT_BLOCK(c); |
| 4215 | } |
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 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4218 | if (!compiler_comprehension_generator(c, |
| 4219 | generators, gen_index, |
| 4220 | elt, val, type)) |
| 4221 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | /* only append after the last for generator */ |
| 4224 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4225 | /* comprehension specific code */ |
| 4226 | switch (type) { |
| 4227 | case COMP_GENEXP: |
| 4228 | VISIT(c, expr, elt); |
| 4229 | ADDOP(c, YIELD_VALUE); |
| 4230 | ADDOP(c, POP_TOP); |
| 4231 | break; |
| 4232 | case COMP_LISTCOMP: |
| 4233 | VISIT(c, expr, elt); |
| 4234 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4235 | break; |
| 4236 | case COMP_SETCOMP: |
| 4237 | VISIT(c, expr, elt); |
| 4238 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4239 | break; |
| 4240 | case COMP_DICTCOMP: |
| 4241 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4242 | the same. */ |
| 4243 | VISIT(c, expr, val); |
| 4244 | VISIT(c, expr, elt); |
| 4245 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4246 | break; |
| 4247 | default: |
| 4248 | return 0; |
| 4249 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4251 | compiler_use_next_block(c, skip); |
| 4252 | } |
| 4253 | compiler_use_next_block(c, if_cleanup); |
| 4254 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4255 | compiler_use_next_block(c, anchor); |
| 4256 | |
| 4257 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4261 | compiler_async_comprehension_generator(struct compiler *c, |
| 4262 | asdl_seq *generators, int gen_index, |
| 4263 | expr_ty elt, expr_ty val, int type) |
| 4264 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4265 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4266 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4267 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4268 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4269 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4270 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4271 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4272 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4273 | return 0; |
| 4274 | } |
| 4275 | |
| 4276 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4277 | |
| 4278 | if (gen_index == 0) { |
| 4279 | /* Receive outermost iter as an implicit argument */ |
| 4280 | c->u->u_argcount = 1; |
| 4281 | ADDOP_I(c, LOAD_FAST, 0); |
| 4282 | } |
| 4283 | else { |
| 4284 | /* Sub-iter - calculate on the fly */ |
| 4285 | VISIT(c, expr, gen->iter); |
| 4286 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4289 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4290 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4291 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4292 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4293 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4294 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4295 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4296 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4297 | |
| 4298 | n = asdl_seq_LEN(gen->ifs); |
| 4299 | for (i = 0; i < n; i++) { |
| 4300 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4301 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4302 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4303 | NEXT_BLOCK(c); |
| 4304 | } |
| 4305 | |
| 4306 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4307 | if (!compiler_comprehension_generator(c, |
| 4308 | generators, gen_index, |
| 4309 | elt, val, type)) |
| 4310 | return 0; |
| 4311 | |
| 4312 | /* only append after the last for generator */ |
| 4313 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4314 | /* comprehension specific code */ |
| 4315 | switch (type) { |
| 4316 | case COMP_GENEXP: |
| 4317 | VISIT(c, expr, elt); |
| 4318 | ADDOP(c, YIELD_VALUE); |
| 4319 | ADDOP(c, POP_TOP); |
| 4320 | break; |
| 4321 | case COMP_LISTCOMP: |
| 4322 | VISIT(c, expr, elt); |
| 4323 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4324 | break; |
| 4325 | case COMP_SETCOMP: |
| 4326 | VISIT(c, expr, elt); |
| 4327 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4328 | break; |
| 4329 | case COMP_DICTCOMP: |
| 4330 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4331 | the same. */ |
| 4332 | VISIT(c, expr, val); |
| 4333 | VISIT(c, expr, elt); |
| 4334 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4335 | break; |
| 4336 | default: |
| 4337 | return 0; |
| 4338 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4339 | } |
| 4340 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4341 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4342 | |
| 4343 | compiler_use_next_block(c, except); |
| 4344 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4345 | |
| 4346 | return 1; |
| 4347 | } |
| 4348 | |
| 4349 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4350 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4351 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4352 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4353 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4355 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4356 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4357 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4358 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4359 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4360 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4361 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4362 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4363 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4364 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4365 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4369 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4370 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4371 | compiler_error(c, "asynchronous comprehension outside of " |
| 4372 | "an asynchronous function"); |
| 4373 | goto error_in_scope; |
| 4374 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4375 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | if (type != COMP_GENEXP) { |
| 4377 | int op; |
| 4378 | switch (type) { |
| 4379 | case COMP_LISTCOMP: |
| 4380 | op = BUILD_LIST; |
| 4381 | break; |
| 4382 | case COMP_SETCOMP: |
| 4383 | op = BUILD_SET; |
| 4384 | break; |
| 4385 | case COMP_DICTCOMP: |
| 4386 | op = BUILD_MAP; |
| 4387 | break; |
| 4388 | default: |
| 4389 | PyErr_Format(PyExc_SystemError, |
| 4390 | "unknown comprehension type %d", type); |
| 4391 | goto error_in_scope; |
| 4392 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4394 | ADDOP_I(c, op, 0); |
| 4395 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4397 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 4398 | val, type)) |
| 4399 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | if (type != COMP_GENEXP) { |
| 4402 | ADDOP(c, RETURN_VALUE); |
| 4403 | } |
| 4404 | |
| 4405 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4406 | qualname = c->u->u_qualname; |
| 4407 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4408 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4409 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4410 | goto error; |
| 4411 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4412 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4414 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4415 | Py_DECREF(co); |
| 4416 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4417 | VISIT(c, expr, outermost->iter); |
| 4418 | |
| 4419 | if (outermost->is_async) { |
| 4420 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4421 | } else { |
| 4422 | ADDOP(c, GET_ITER); |
| 4423 | } |
| 4424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4425 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4426 | |
| 4427 | if (is_async_generator && type != COMP_GENEXP) { |
| 4428 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4429 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4430 | ADDOP(c, YIELD_FROM); |
| 4431 | } |
| 4432 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4434 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4435 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4436 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4437 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | Py_XDECREF(co); |
| 4439 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4443 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4445 | static identifier name; |
| 4446 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4447 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4448 | if (!name) |
| 4449 | return 0; |
| 4450 | } |
| 4451 | assert(e->kind == GeneratorExp_kind); |
| 4452 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4453 | e->v.GeneratorExp.generators, |
| 4454 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4458 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | static identifier name; |
| 4461 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4462 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | if (!name) |
| 4464 | return 0; |
| 4465 | } |
| 4466 | assert(e->kind == ListComp_kind); |
| 4467 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4468 | e->v.ListComp.generators, |
| 4469 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4470 | } |
| 4471 | |
| 4472 | static int |
| 4473 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4474 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | static identifier name; |
| 4476 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4477 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4478 | if (!name) |
| 4479 | return 0; |
| 4480 | } |
| 4481 | assert(e->kind == SetComp_kind); |
| 4482 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4483 | e->v.SetComp.generators, |
| 4484 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | |
| 4488 | static int |
| 4489 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4490 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4491 | static identifier name; |
| 4492 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4493 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4494 | if (!name) |
| 4495 | return 0; |
| 4496 | } |
| 4497 | assert(e->kind == DictComp_kind); |
| 4498 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4499 | e->v.DictComp.generators, |
| 4500 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | |
| 4504 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4505 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4506 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4507 | VISIT(c, expr, k->value); |
| 4508 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4509 | } |
| 4510 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4512 | whether they are true or false. |
| 4513 | |
| 4514 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4515 | */ |
| 4516 | |
| 4517 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4518 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4519 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4520 | if (e->kind == Constant_kind) { |
| 4521 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4522 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4523 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4526 | |
| 4527 | /* |
| 4528 | Implements the async with statement. |
| 4529 | |
| 4530 | The semantics outlined in that PEP are as follows: |
| 4531 | |
| 4532 | async with EXPR as VAR: |
| 4533 | BLOCK |
| 4534 | |
| 4535 | It is implemented roughly as: |
| 4536 | |
| 4537 | context = EXPR |
| 4538 | exit = context.__aexit__ # not calling it |
| 4539 | value = await context.__aenter__() |
| 4540 | try: |
| 4541 | VAR = value # if VAR present in the syntax |
| 4542 | BLOCK |
| 4543 | finally: |
| 4544 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4545 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4546 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4547 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4548 | if not (await exit(*exc)): |
| 4549 | raise |
| 4550 | */ |
| 4551 | static int |
| 4552 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4553 | { |
| 4554 | basicblock *block, *finally; |
| 4555 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4556 | |
| 4557 | assert(s->kind == AsyncWith_kind); |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4558 | if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ |
| 4559 | c->u->u_ste->ste_coroutine = 1; |
| 4560 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4561 | return compiler_error(c, "'async with' outside async function"); |
| 4562 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4563 | |
| 4564 | block = compiler_new_block(c); |
| 4565 | finally = compiler_new_block(c); |
| 4566 | if (!block || !finally) |
| 4567 | return 0; |
| 4568 | |
| 4569 | /* Evaluate EXPR */ |
| 4570 | VISIT(c, expr, item->context_expr); |
| 4571 | |
| 4572 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4573 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4574 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4575 | ADDOP(c, YIELD_FROM); |
| 4576 | |
| 4577 | ADDOP_JREL(c, SETUP_ASYNC_WITH, finally); |
| 4578 | |
| 4579 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4580 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4581 | if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4582 | return 0; |
| 4583 | } |
| 4584 | |
| 4585 | if (item->optional_vars) { |
| 4586 | VISIT(c, expr, item->optional_vars); |
| 4587 | } |
| 4588 | else { |
| 4589 | /* Discard result from context.__aenter__() */ |
| 4590 | ADDOP(c, POP_TOP); |
| 4591 | } |
| 4592 | |
| 4593 | pos++; |
| 4594 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4595 | /* BLOCK code */ |
| 4596 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4597 | else if (!compiler_async_with(c, s, pos)) |
| 4598 | return 0; |
| 4599 | |
| 4600 | /* End of try block; start the finally block */ |
| 4601 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4602 | ADDOP(c, BEGIN_FINALLY); |
| 4603 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4604 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4605 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4606 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4607 | return 0; |
| 4608 | |
| 4609 | /* Finally block starts; context.__exit__ is on the stack under |
| 4610 | the exception or return information. Just issue our magic |
| 4611 | opcode. */ |
| 4612 | ADDOP(c, WITH_CLEANUP_START); |
| 4613 | |
| 4614 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4615 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4616 | ADDOP(c, YIELD_FROM); |
| 4617 | |
| 4618 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 4619 | |
| 4620 | /* Finally block ends. */ |
| 4621 | ADDOP(c, END_FINALLY); |
| 4622 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4623 | return 1; |
| 4624 | } |
| 4625 | |
| 4626 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4627 | /* |
| 4628 | Implements the with statement from PEP 343. |
| 4629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4630 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4631 | |
| 4632 | with EXPR as VAR: |
| 4633 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4634 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4635 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4636 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4637 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4638 | exit = context.__exit__ # not calling it |
| 4639 | value = context.__enter__() |
| 4640 | try: |
| 4641 | VAR = value # if VAR present in the syntax |
| 4642 | BLOCK |
| 4643 | finally: |
| 4644 | if an exception was raised: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4645 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4646 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4647 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4648 | exit(*exc) |
| 4649 | */ |
| 4650 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4651 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4652 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4653 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4654 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4655 | |
| 4656 | assert(s->kind == With_kind); |
| 4657 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4658 | block = compiler_new_block(c); |
| 4659 | finally = compiler_new_block(c); |
| 4660 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4661 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4662 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4663 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4664 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4665 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4666 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4667 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4668 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4669 | if (!compiler_push_fblock(c, WITH, block, finally)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4670 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4671 | } |
| 4672 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4673 | if (item->optional_vars) { |
| 4674 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4675 | } |
| 4676 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4677 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4678 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4681 | pos++; |
| 4682 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4683 | /* BLOCK code */ |
| 4684 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4685 | else if (!compiler_with(c, s, pos)) |
| 4686 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4687 | |
| 4688 | /* End of try block; start the finally block */ |
| 4689 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4690 | ADDOP(c, BEGIN_FINALLY); |
| 4691 | compiler_pop_fblock(c, WITH, block); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4692 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4693 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4694 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4695 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4696 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 4697 | /* Finally block starts; context.__exit__ is on the stack under |
| 4698 | the exception or return information. Just issue our magic |
| 4699 | opcode. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4700 | ADDOP(c, WITH_CLEANUP_START); |
| 4701 | ADDOP(c, WITH_CLEANUP_FINISH); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4702 | |
| 4703 | /* Finally block ends. */ |
| 4704 | ADDOP(c, END_FINALLY); |
| 4705 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4706 | return 1; |
| 4707 | } |
| 4708 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4709 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4710 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4711 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4712 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4713 | case NamedExpr_kind: |
| 4714 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4715 | ADDOP(c, DUP_TOP); |
| 4716 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4717 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | case BoolOp_kind: |
| 4719 | return compiler_boolop(c, e); |
| 4720 | case BinOp_kind: |
| 4721 | VISIT(c, expr, e->v.BinOp.left); |
| 4722 | VISIT(c, expr, e->v.BinOp.right); |
| 4723 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 4724 | break; |
| 4725 | case UnaryOp_kind: |
| 4726 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4727 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4728 | break; |
| 4729 | case Lambda_kind: |
| 4730 | return compiler_lambda(c, e); |
| 4731 | case IfExp_kind: |
| 4732 | return compiler_ifexp(c, e); |
| 4733 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4734 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4735 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4736 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 | case GeneratorExp_kind: |
| 4738 | return compiler_genexp(c, e); |
| 4739 | case ListComp_kind: |
| 4740 | return compiler_listcomp(c, e); |
| 4741 | case SetComp_kind: |
| 4742 | return compiler_setcomp(c, e); |
| 4743 | case DictComp_kind: |
| 4744 | return compiler_dictcomp(c, e); |
| 4745 | case Yield_kind: |
| 4746 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4747 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4748 | if (e->v.Yield.value) { |
| 4749 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4750 | } |
| 4751 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4752 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4753 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4754 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4755 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4756 | case YieldFrom_kind: |
| 4757 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4758 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4759 | |
| 4760 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4761 | return compiler_error(c, "'yield from' inside async function"); |
| 4762 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4763 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4764 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4765 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4766 | ADDOP(c, YIELD_FROM); |
| 4767 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4768 | case Await_kind: |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4769 | if (!(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT)){ |
| 4770 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 4771 | return compiler_error(c, "'await' outside function"); |
| 4772 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4773 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 4774 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4775 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 4776 | return compiler_error(c, "'await' outside async function"); |
| 4777 | } |
| 4778 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4779 | |
| 4780 | VISIT(c, expr, e->v.Await.value); |
| 4781 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4782 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4783 | ADDOP(c, YIELD_FROM); |
| 4784 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4785 | case Compare_kind: |
| 4786 | return compiler_compare(c, e); |
| 4787 | case Call_kind: |
| 4788 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4789 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4790 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4791 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4792 | case JoinedStr_kind: |
| 4793 | return compiler_joined_str(c, e); |
| 4794 | case FormattedValue_kind: |
| 4795 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | /* The following exprs can be assignment targets. */ |
| 4797 | case Attribute_kind: |
| 4798 | if (e->v.Attribute.ctx != AugStore) |
| 4799 | VISIT(c, expr, e->v.Attribute.value); |
| 4800 | switch (e->v.Attribute.ctx) { |
| 4801 | case AugLoad: |
| 4802 | ADDOP(c, DUP_TOP); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4803 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4804 | case Load: |
| 4805 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4806 | break; |
| 4807 | case AugStore: |
| 4808 | ADDOP(c, ROT_TWO); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4809 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4810 | case Store: |
| 4811 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4812 | break; |
| 4813 | case Del: |
| 4814 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4815 | break; |
| 4816 | case Param: |
| 4817 | default: |
| 4818 | PyErr_SetString(PyExc_SystemError, |
| 4819 | "param invalid in attribute expression"); |
| 4820 | return 0; |
| 4821 | } |
| 4822 | break; |
| 4823 | case Subscript_kind: |
| 4824 | switch (e->v.Subscript.ctx) { |
| 4825 | case AugLoad: |
| 4826 | VISIT(c, expr, e->v.Subscript.value); |
| 4827 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 4828 | break; |
| 4829 | case Load: |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4830 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 4831 | return 0; |
| 4832 | } |
| 4833 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 4834 | return 0; |
| 4835 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | VISIT(c, expr, e->v.Subscript.value); |
| 4837 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 4838 | break; |
| 4839 | case AugStore: |
| 4840 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 4841 | break; |
| 4842 | case Store: |
| 4843 | VISIT(c, expr, e->v.Subscript.value); |
| 4844 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 4845 | break; |
| 4846 | case Del: |
| 4847 | VISIT(c, expr, e->v.Subscript.value); |
| 4848 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 4849 | break; |
| 4850 | case Param: |
| 4851 | default: |
| 4852 | PyErr_SetString(PyExc_SystemError, |
| 4853 | "param invalid in subscript expression"); |
| 4854 | return 0; |
| 4855 | } |
| 4856 | break; |
| 4857 | case Starred_kind: |
| 4858 | switch (e->v.Starred.ctx) { |
| 4859 | case Store: |
| 4860 | /* In all legitimate cases, the Starred node was already replaced |
| 4861 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 4862 | return compiler_error(c, |
| 4863 | "starred assignment target must be in a list or tuple"); |
| 4864 | default: |
| 4865 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4866 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4867 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | case Name_kind: |
| 4869 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 4870 | /* child nodes of List and Tuple will have expr_context set */ |
| 4871 | case List_kind: |
| 4872 | return compiler_list(c, e); |
| 4873 | case Tuple_kind: |
| 4874 | return compiler_tuple(c, e); |
| 4875 | } |
| 4876 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4880 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 4881 | { |
| 4882 | /* If expr e has a different line number than the last expr/stmt, |
| 4883 | set a new line number for the next instruction. |
| 4884 | */ |
| 4885 | int old_lineno = c->u->u_lineno; |
| 4886 | int old_col_offset = c->u->u_col_offset; |
| 4887 | if (e->lineno != c->u->u_lineno) { |
| 4888 | c->u->u_lineno = e->lineno; |
| 4889 | c->u->u_lineno_set = 0; |
| 4890 | } |
| 4891 | /* Updating the column offset is always harmless. */ |
| 4892 | c->u->u_col_offset = e->col_offset; |
| 4893 | |
| 4894 | int res = compiler_visit_expr1(c, e); |
| 4895 | |
| 4896 | if (old_lineno != c->u->u_lineno) { |
| 4897 | c->u->u_lineno = old_lineno; |
| 4898 | c->u->u_lineno_set = 0; |
| 4899 | } |
| 4900 | c->u->u_col_offset = old_col_offset; |
| 4901 | return res; |
| 4902 | } |
| 4903 | |
| 4904 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4905 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 4906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | expr_ty e = s->v.AugAssign.target; |
| 4908 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4909 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4910 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4911 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4912 | switch (e->kind) { |
| 4913 | case Attribute_kind: |
| 4914 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4915 | AugLoad, e->lineno, e->col_offset, |
| 4916 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4917 | if (auge == NULL) |
| 4918 | return 0; |
| 4919 | VISIT(c, expr, auge); |
| 4920 | VISIT(c, expr, s->v.AugAssign.value); |
| 4921 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4922 | auge->v.Attribute.ctx = AugStore; |
| 4923 | VISIT(c, expr, auge); |
| 4924 | break; |
| 4925 | case Subscript_kind: |
| 4926 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4927 | AugLoad, e->lineno, e->col_offset, |
| 4928 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4929 | if (auge == NULL) |
| 4930 | return 0; |
| 4931 | VISIT(c, expr, auge); |
| 4932 | VISIT(c, expr, s->v.AugAssign.value); |
| 4933 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4934 | auge->v.Subscript.ctx = AugStore; |
| 4935 | VISIT(c, expr, auge); |
| 4936 | break; |
| 4937 | case Name_kind: |
| 4938 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 4939 | return 0; |
| 4940 | VISIT(c, expr, s->v.AugAssign.value); |
| 4941 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4942 | return compiler_nameop(c, e->v.Name.id, Store); |
| 4943 | default: |
| 4944 | PyErr_Format(PyExc_SystemError, |
| 4945 | "invalid node type (%d) for augmented assignment", |
| 4946 | e->kind); |
| 4947 | return 0; |
| 4948 | } |
| 4949 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 4953 | check_ann_expr(struct compiler *c, expr_ty e) |
| 4954 | { |
| 4955 | VISIT(c, expr, e); |
| 4956 | ADDOP(c, POP_TOP); |
| 4957 | return 1; |
| 4958 | } |
| 4959 | |
| 4960 | static int |
| 4961 | check_annotation(struct compiler *c, stmt_ty s) |
| 4962 | { |
| 4963 | /* Annotations are only evaluated in a module or class. */ |
| 4964 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 4965 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 4966 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 4967 | } |
| 4968 | return 1; |
| 4969 | } |
| 4970 | |
| 4971 | static int |
| 4972 | check_ann_slice(struct compiler *c, slice_ty sl) |
| 4973 | { |
| 4974 | switch(sl->kind) { |
| 4975 | case Index_kind: |
| 4976 | return check_ann_expr(c, sl->v.Index.value); |
| 4977 | case Slice_kind: |
| 4978 | if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { |
| 4979 | return 0; |
| 4980 | } |
| 4981 | if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { |
| 4982 | return 0; |
| 4983 | } |
| 4984 | if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { |
| 4985 | return 0; |
| 4986 | } |
| 4987 | break; |
| 4988 | default: |
| 4989 | PyErr_SetString(PyExc_SystemError, |
| 4990 | "unexpected slice kind"); |
| 4991 | return 0; |
| 4992 | } |
| 4993 | return 1; |
| 4994 | } |
| 4995 | |
| 4996 | static int |
| 4997 | check_ann_subscr(struct compiler *c, slice_ty sl) |
| 4998 | { |
| 4999 | /* We check that everything in a subscript is defined at runtime. */ |
| 5000 | Py_ssize_t i, n; |
| 5001 | |
| 5002 | switch (sl->kind) { |
| 5003 | case Index_kind: |
| 5004 | case Slice_kind: |
| 5005 | if (!check_ann_slice(c, sl)) { |
| 5006 | return 0; |
| 5007 | } |
| 5008 | break; |
| 5009 | case ExtSlice_kind: |
| 5010 | n = asdl_seq_LEN(sl->v.ExtSlice.dims); |
| 5011 | for (i = 0; i < n; i++) { |
| 5012 | slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i); |
| 5013 | switch (subsl->kind) { |
| 5014 | case Index_kind: |
| 5015 | case Slice_kind: |
| 5016 | if (!check_ann_slice(c, subsl)) { |
| 5017 | return 0; |
| 5018 | } |
| 5019 | break; |
| 5020 | case ExtSlice_kind: |
| 5021 | default: |
| 5022 | PyErr_SetString(PyExc_SystemError, |
| 5023 | "extended slice invalid in nested slice"); |
| 5024 | return 0; |
| 5025 | } |
| 5026 | } |
| 5027 | break; |
| 5028 | default: |
| 5029 | PyErr_Format(PyExc_SystemError, |
| 5030 | "invalid subscript kind %d", sl->kind); |
| 5031 | return 0; |
| 5032 | } |
| 5033 | return 1; |
| 5034 | } |
| 5035 | |
| 5036 | static int |
| 5037 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5038 | { |
| 5039 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5040 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5041 | |
| 5042 | assert(s->kind == AnnAssign_kind); |
| 5043 | |
| 5044 | /* We perform the actual assignment first. */ |
| 5045 | if (s->v.AnnAssign.value) { |
| 5046 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5047 | VISIT(c, expr, targ); |
| 5048 | } |
| 5049 | switch (targ->kind) { |
| 5050 | case Name_kind: |
| 5051 | /* If we have a simple name in a module or class, store annotation. */ |
| 5052 | if (s->v.AnnAssign.simple && |
| 5053 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5054 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5055 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5056 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5057 | } |
| 5058 | else { |
| 5059 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5060 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5061 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5062 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5063 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5064 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5065 | } |
| 5066 | break; |
| 5067 | case Attribute_kind: |
| 5068 | if (!s->v.AnnAssign.value && |
| 5069 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5070 | return 0; |
| 5071 | } |
| 5072 | break; |
| 5073 | case Subscript_kind: |
| 5074 | if (!s->v.AnnAssign.value && |
| 5075 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5076 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5077 | return 0; |
| 5078 | } |
| 5079 | break; |
| 5080 | default: |
| 5081 | PyErr_Format(PyExc_SystemError, |
| 5082 | "invalid node type (%d) for annotated assignment", |
| 5083 | targ->kind); |
| 5084 | return 0; |
| 5085 | } |
| 5086 | /* Annotation is evaluated last. */ |
| 5087 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5088 | return 0; |
| 5089 | } |
| 5090 | return 1; |
| 5091 | } |
| 5092 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5093 | /* Raises a SyntaxError and returns 0. |
| 5094 | If something goes wrong, a different exception may be raised. |
| 5095 | */ |
| 5096 | |
| 5097 | static int |
| 5098 | compiler_error(struct compiler *c, const char *errstr) |
| 5099 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5100 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5102 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5103 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5104 | if (!loc) { |
| 5105 | Py_INCREF(Py_None); |
| 5106 | loc = Py_None; |
| 5107 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5108 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5109 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5110 | if (!u) |
| 5111 | goto exit; |
| 5112 | v = Py_BuildValue("(zO)", errstr, u); |
| 5113 | if (!v) |
| 5114 | goto exit; |
| 5115 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5116 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5117 | Py_DECREF(loc); |
| 5118 | Py_XDECREF(u); |
| 5119 | Py_XDECREF(v); |
| 5120 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5121 | } |
| 5122 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5123 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5124 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5125 | and returns 0. |
| 5126 | */ |
| 5127 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5128 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5129 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5130 | va_list vargs; |
| 5131 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5132 | va_start(vargs, format); |
| 5133 | #else |
| 5134 | va_start(vargs); |
| 5135 | #endif |
| 5136 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5137 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5138 | if (msg == NULL) { |
| 5139 | return 0; |
| 5140 | } |
| 5141 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5142 | c->u->u_lineno, NULL, NULL) < 0) |
| 5143 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5144 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5145 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5146 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5147 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5148 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5149 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5150 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5151 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5152 | return 0; |
| 5153 | } |
| 5154 | Py_DECREF(msg); |
| 5155 | return 1; |
| 5156 | } |
| 5157 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5158 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5159 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 5160 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5161 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | /* XXX this code is duplicated */ |
| 5165 | switch (ctx) { |
| 5166 | case AugLoad: /* fall through to Load */ |
| 5167 | case Load: op = BINARY_SUBSCR; break; |
| 5168 | case AugStore:/* fall through to Store */ |
| 5169 | case Store: op = STORE_SUBSCR; break; |
| 5170 | case Del: op = DELETE_SUBSCR; break; |
| 5171 | case Param: |
| 5172 | PyErr_Format(PyExc_SystemError, |
| 5173 | "invalid %s kind %d in subscript\n", |
| 5174 | kind, ctx); |
| 5175 | return 0; |
| 5176 | } |
| 5177 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 5178 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5179 | } |
| 5180 | else if (ctx == AugStore) { |
| 5181 | ADDOP(c, ROT_THREE); |
| 5182 | } |
| 5183 | ADDOP(c, op); |
| 5184 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | static int |
| 5188 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5189 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5190 | int n = 2; |
| 5191 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5193 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5194 | if (s->v.Slice.lower) { |
| 5195 | VISIT(c, expr, s->v.Slice.lower); |
| 5196 | } |
| 5197 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5198 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5199 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5201 | if (s->v.Slice.upper) { |
| 5202 | VISIT(c, expr, s->v.Slice.upper); |
| 5203 | } |
| 5204 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5205 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
| 5208 | if (s->v.Slice.step) { |
| 5209 | n++; |
| 5210 | VISIT(c, expr, s->v.Slice.step); |
| 5211 | } |
| 5212 | ADDOP_I(c, BUILD_SLICE, n); |
| 5213 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5217 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 5218 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | switch (s->kind) { |
| 5221 | case Slice_kind: |
| 5222 | return compiler_slice(c, s, ctx); |
| 5223 | case Index_kind: |
| 5224 | VISIT(c, expr, s->v.Index.value); |
| 5225 | break; |
| 5226 | case ExtSlice_kind: |
| 5227 | default: |
| 5228 | PyErr_SetString(PyExc_SystemError, |
| 5229 | "extended slice invalid in nested slice"); |
| 5230 | return 0; |
| 5231 | } |
| 5232 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5233 | } |
| 5234 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5235 | static int |
| 5236 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5237 | { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 5238 | const char * kindname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5239 | switch (s->kind) { |
| 5240 | case Index_kind: |
| 5241 | kindname = "index"; |
| 5242 | if (ctx != AugStore) { |
| 5243 | VISIT(c, expr, s->v.Index.value); |
| 5244 | } |
| 5245 | break; |
| 5246 | case Slice_kind: |
| 5247 | kindname = "slice"; |
| 5248 | if (ctx != AugStore) { |
| 5249 | if (!compiler_slice(c, s, ctx)) |
| 5250 | return 0; |
| 5251 | } |
| 5252 | break; |
| 5253 | case ExtSlice_kind: |
| 5254 | kindname = "extended slice"; |
| 5255 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5256 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5257 | for (i = 0; i < n; i++) { |
| 5258 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 5259 | s->v.ExtSlice.dims, i); |
| 5260 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 5261 | return 0; |
| 5262 | } |
| 5263 | ADDOP_I(c, BUILD_TUPLE, n); |
| 5264 | } |
| 5265 | break; |
| 5266 | default: |
| 5267 | PyErr_Format(PyExc_SystemError, |
| 5268 | "invalid subscript kind %d", s->kind); |
| 5269 | return 0; |
| 5270 | } |
| 5271 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5272 | } |
| 5273 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5274 | /* End of the compiler section, beginning of the assembler section */ |
| 5275 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5276 | /* do depth-first search of basic block graph, starting with block. |
| 5277 | post records the block indices in post-order. |
| 5278 | |
| 5279 | XXX must handle implicit jumps from one block to next |
| 5280 | */ |
| 5281 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5282 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5283 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5284 | int a_offset; /* offset into bytecode */ |
| 5285 | int a_nblocks; /* number of reachable blocks */ |
| 5286 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 5287 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5288 | int a_lnotab_off; /* offset into lnotab */ |
| 5289 | int a_lineno; /* last lineno of emitted instruction */ |
| 5290 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5291 | }; |
| 5292 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5293 | static void |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5294 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5295 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5296 | int i, j; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5297 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5298 | /* Get rid of recursion for normal control flow. |
| 5299 | Since the number of blocks is limited, unused space in a_postorder |
| 5300 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5301 | blocks. */ |
| 5302 | for (j = end; b && !b->b_seen; b = b->b_next) { |
| 5303 | b->b_seen = 1; |
| 5304 | assert(a->a_nblocks < j); |
| 5305 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5306 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5307 | while (j < end) { |
| 5308 | b = a->a_postorder[j++]; |
| 5309 | for (i = 0; i < b->b_iused; i++) { |
| 5310 | struct instr *instr = &b->b_instr[i]; |
| 5311 | if (instr->i_jrel || instr->i_jabs) |
| 5312 | dfs(c, instr->i_target, a, j); |
| 5313 | } |
| 5314 | assert(a->a_nblocks < j); |
| 5315 | a->a_postorder[a->a_nblocks++] = b; |
| 5316 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5319 | Py_LOCAL_INLINE(void) |
| 5320 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5321 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5322 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5323 | if (b->b_startdepth < depth) { |
| 5324 | assert(b->b_startdepth < 0); |
| 5325 | b->b_startdepth = depth; |
| 5326 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5327 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
| 5330 | /* Find the flow path that needs the largest stack. We assume that |
| 5331 | * cycles in the flow graph have no net effect on the stack depth. |
| 5332 | */ |
| 5333 | static int |
| 5334 | stackdepth(struct compiler *c) |
| 5335 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5336 | basicblock *b, *entryblock = NULL; |
| 5337 | basicblock **stack, **sp; |
| 5338 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5339 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5340 | b->b_startdepth = INT_MIN; |
| 5341 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5342 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5343 | } |
| 5344 | if (!entryblock) |
| 5345 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5346 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5347 | if (!stack) { |
| 5348 | PyErr_NoMemory(); |
| 5349 | return -1; |
| 5350 | } |
| 5351 | |
| 5352 | sp = stack; |
| 5353 | stackdepth_push(&sp, entryblock, 0); |
| 5354 | while (sp != stack) { |
| 5355 | b = *--sp; |
| 5356 | int depth = b->b_startdepth; |
| 5357 | assert(depth >= 0); |
| 5358 | basicblock *next = b->b_next; |
| 5359 | for (int i = 0; i < b->b_iused; i++) { |
| 5360 | struct instr *instr = &b->b_instr[i]; |
| 5361 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5362 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5363 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5364 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5365 | } |
| 5366 | int new_depth = depth + effect; |
| 5367 | if (new_depth > maxdepth) { |
| 5368 | maxdepth = new_depth; |
| 5369 | } |
| 5370 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5371 | if (instr->i_jrel || instr->i_jabs) { |
| 5372 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5373 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5374 | int target_depth = depth + effect; |
| 5375 | if (target_depth > maxdepth) { |
| 5376 | maxdepth = target_depth; |
| 5377 | } |
| 5378 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5379 | if (instr->i_opcode == CALL_FINALLY) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5380 | assert(instr->i_target->b_startdepth >= 0); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5381 | assert(instr->i_target->b_startdepth >= target_depth); |
| 5382 | depth = new_depth; |
| 5383 | continue; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5384 | } |
| 5385 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5386 | } |
| 5387 | depth = new_depth; |
| 5388 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5389 | instr->i_opcode == JUMP_FORWARD || |
| 5390 | instr->i_opcode == RETURN_VALUE || |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5391 | instr->i_opcode == RAISE_VARARGS) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5392 | { |
| 5393 | /* remaining code is dead */ |
| 5394 | next = NULL; |
| 5395 | break; |
| 5396 | } |
| 5397 | } |
| 5398 | if (next != NULL) { |
| 5399 | stackdepth_push(&sp, next, depth); |
| 5400 | } |
| 5401 | } |
| 5402 | PyObject_Free(stack); |
| 5403 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | static int |
| 5407 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5408 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | memset(a, 0, sizeof(struct assembler)); |
| 5410 | a->a_lineno = firstlineno; |
| 5411 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5412 | if (!a->a_bytecode) |
| 5413 | return 0; |
| 5414 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5415 | if (!a->a_lnotab) |
| 5416 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5417 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5418 | PyErr_NoMemory(); |
| 5419 | return 0; |
| 5420 | } |
| 5421 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 5422 | sizeof(basicblock *) * nblocks); |
| 5423 | if (!a->a_postorder) { |
| 5424 | PyErr_NoMemory(); |
| 5425 | return 0; |
| 5426 | } |
| 5427 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | static void |
| 5431 | assemble_free(struct assembler *a) |
| 5432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5433 | Py_XDECREF(a->a_bytecode); |
| 5434 | Py_XDECREF(a->a_lnotab); |
| 5435 | if (a->a_postorder) |
| 5436 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5439 | static int |
| 5440 | blocksize(basicblock *b) |
| 5441 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | int i; |
| 5443 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5446 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5447 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5448 | } |
| 5449 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5450 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5451 | the instruction's bytecode offset and line number. See |
| 5452 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5453 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5454 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5455 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5457 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5458 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5459 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5460 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5461 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5464 | assert(d_bytecode >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5466 | if(d_bytecode == 0 && d_lineno == 0) |
| 5467 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5469 | if (d_bytecode > 255) { |
| 5470 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5471 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5472 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5473 | if (nbytes >= len) { |
| 5474 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5475 | len = nbytes; |
| 5476 | else if (len <= INT_MAX / 2) |
| 5477 | len *= 2; |
| 5478 | else { |
| 5479 | PyErr_NoMemory(); |
| 5480 | return 0; |
| 5481 | } |
| 5482 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5483 | return 0; |
| 5484 | } |
| 5485 | lnotab = (unsigned char *) |
| 5486 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5487 | for (j = 0; j < ncodes; j++) { |
| 5488 | *lnotab++ = 255; |
| 5489 | *lnotab++ = 0; |
| 5490 | } |
| 5491 | d_bytecode -= ncodes * 255; |
| 5492 | a->a_lnotab_off += ncodes * 2; |
| 5493 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5494 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5495 | |
| 5496 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5497 | int j, nbytes, ncodes, k; |
| 5498 | if (d_lineno < 0) { |
| 5499 | k = -128; |
| 5500 | /* use division on positive numbers */ |
| 5501 | ncodes = (-d_lineno) / 128; |
| 5502 | } |
| 5503 | else { |
| 5504 | k = 127; |
| 5505 | ncodes = d_lineno / 127; |
| 5506 | } |
| 5507 | d_lineno -= ncodes * k; |
| 5508 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5509 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5510 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5511 | if (nbytes >= len) { |
| 5512 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5513 | len = nbytes; |
| 5514 | else if (len <= INT_MAX / 2) |
| 5515 | len *= 2; |
| 5516 | else { |
| 5517 | PyErr_NoMemory(); |
| 5518 | return 0; |
| 5519 | } |
| 5520 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5521 | return 0; |
| 5522 | } |
| 5523 | lnotab = (unsigned char *) |
| 5524 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5525 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5526 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5527 | d_bytecode = 0; |
| 5528 | for (j = 1; j < ncodes; j++) { |
| 5529 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5530 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5531 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5532 | a->a_lnotab_off += ncodes * 2; |
| 5533 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5534 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5537 | if (a->a_lnotab_off + 2 >= len) { |
| 5538 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5539 | return 0; |
| 5540 | } |
| 5541 | lnotab = (unsigned char *) |
| 5542 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | a->a_lnotab_off += 2; |
| 5545 | if (d_bytecode) { |
| 5546 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5547 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | } |
| 5549 | else { /* First line of a block; def stmt, etc. */ |
| 5550 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5551 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5552 | } |
| 5553 | a->a_lineno = i->i_lineno; |
| 5554 | a->a_lineno_off = a->a_offset; |
| 5555 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5558 | /* assemble_emit() |
| 5559 | Extend the bytecode with a new instruction. |
| 5560 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5561 | */ |
| 5562 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5563 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5564 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5565 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5566 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5567 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5568 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5569 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5570 | arg = i->i_oparg; |
| 5571 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5572 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5573 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5574 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5575 | if (len > PY_SSIZE_T_MAX / 2) |
| 5576 | return 0; |
| 5577 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5578 | return 0; |
| 5579 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5580 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5581 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5582 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5583 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5586 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5587 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5589 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5590 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5591 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5593 | /* Compute the size of each block and fixup jump args. |
| 5594 | Replace block pointer with position in bytecode. */ |
| 5595 | do { |
| 5596 | totsize = 0; |
| 5597 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5598 | b = a->a_postorder[i]; |
| 5599 | bsize = blocksize(b); |
| 5600 | b->b_offset = totsize; |
| 5601 | totsize += bsize; |
| 5602 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5603 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5604 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5605 | bsize = b->b_offset; |
| 5606 | for (i = 0; i < b->b_iused; i++) { |
| 5607 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5608 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5609 | /* Relative jumps are computed relative to |
| 5610 | the instruction pointer after fetching |
| 5611 | the jump instruction. |
| 5612 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5613 | bsize += isize; |
| 5614 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5615 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5616 | if (instr->i_jrel) { |
| 5617 | instr->i_oparg -= bsize; |
| 5618 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5619 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5620 | if (instrsize(instr->i_oparg) != isize) { |
| 5621 | extended_arg_recompile = 1; |
| 5622 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5623 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5624 | } |
| 5625 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5627 | /* XXX: This is an awful hack that could hurt performance, but |
| 5628 | on the bright side it should work until we come up |
| 5629 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5631 | The issue is that in the first loop blocksize() is called |
| 5632 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5633 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5634 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5636 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5637 | The only EXTENDED_ARGs that could be popping up are |
| 5638 | ones in jump instructions. So this should converge |
| 5639 | fairly quickly. |
| 5640 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5641 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5642 | } |
| 5643 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5644 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5645 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5647 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5648 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
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 | tuple = PyTuple_New(size); |
| 5651 | if (tuple == NULL) |
| 5652 | return NULL; |
| 5653 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5654 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5655 | Py_INCREF(k); |
| 5656 | assert((i - offset) < size); |
| 5657 | assert((i - offset) >= 0); |
| 5658 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5659 | } |
| 5660 | return tuple; |
| 5661 | } |
| 5662 | |
| 5663 | static PyObject * |
| 5664 | consts_dict_keys_inorder(PyObject *dict) |
| 5665 | { |
| 5666 | PyObject *consts, *k, *v; |
| 5667 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5668 | |
| 5669 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5670 | if (consts == NULL) |
| 5671 | return NULL; |
| 5672 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5673 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5674 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5675 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5676 | * the object we want is always second. */ |
| 5677 | if (PyTuple_CheckExact(k)) { |
| 5678 | k = PyTuple_GET_ITEM(k, 1); |
| 5679 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5681 | assert(i < size); |
| 5682 | assert(i >= 0); |
| 5683 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5685 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5688 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5689 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5690 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5691 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5692 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5693 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5694 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5695 | if (ste->ste_nested) |
| 5696 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5697 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5698 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5699 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5700 | flags |= CO_COROUTINE; |
| 5701 | if (ste->ste_generator && ste->ste_coroutine) |
| 5702 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5703 | if (ste->ste_varargs) |
| 5704 | flags |= CO_VARARGS; |
| 5705 | if (ste->ste_varkeywords) |
| 5706 | flags |= CO_VARKEYWORDS; |
| 5707 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5710 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5711 | |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5712 | if ((c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) && |
| 5713 | ste->ste_coroutine && |
| 5714 | !ste->ste_generator) { |
| 5715 | flags |= CO_COROUTINE; |
| 5716 | } |
| 5717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5721 | // Merge *tuple* with constant cache. |
| 5722 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5723 | static int |
| 5724 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5725 | { |
| 5726 | assert(PyTuple_CheckExact(*tuple)); |
| 5727 | |
| 5728 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5729 | if (key == NULL) { |
| 5730 | return 0; |
| 5731 | } |
| 5732 | |
| 5733 | // t is borrowed reference |
| 5734 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5735 | Py_DECREF(key); |
| 5736 | if (t == NULL) { |
| 5737 | return 0; |
| 5738 | } |
| 5739 | if (t == key) { // tuple is new constant. |
| 5740 | return 1; |
| 5741 | } |
| 5742 | |
| 5743 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5744 | Py_INCREF(u); |
| 5745 | Py_DECREF(*tuple); |
| 5746 | *tuple = u; |
| 5747 | return 1; |
| 5748 | } |
| 5749 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5750 | static PyCodeObject * |
| 5751 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5752 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | PyObject *tmp; |
| 5754 | PyCodeObject *co = NULL; |
| 5755 | PyObject *consts = NULL; |
| 5756 | PyObject *names = NULL; |
| 5757 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5758 | PyObject *name = NULL; |
| 5759 | PyObject *freevars = NULL; |
| 5760 | PyObject *cellvars = NULL; |
| 5761 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5762 | Py_ssize_t nlocals; |
| 5763 | int nlocals_int; |
| 5764 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5765 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5766 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5767 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5768 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5769 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5770 | if (!consts || !names || !varnames) |
| 5771 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5773 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5774 | if (!cellvars) |
| 5775 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5776 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5777 | if (!freevars) |
| 5778 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5779 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5780 | if (!merge_const_tuple(c, &names) || |
| 5781 | !merge_const_tuple(c, &varnames) || |
| 5782 | !merge_const_tuple(c, &cellvars) || |
| 5783 | !merge_const_tuple(c, &freevars)) |
| 5784 | { |
| 5785 | goto error; |
| 5786 | } |
| 5787 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5788 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5789 | assert(nlocals < INT_MAX); |
| 5790 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5792 | flags = compute_code_flags(c); |
| 5793 | if (flags < 0) |
| 5794 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5796 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5797 | if (!bytecode) |
| 5798 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5799 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5800 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5801 | if (!tmp) |
| 5802 | goto error; |
| 5803 | Py_DECREF(consts); |
| 5804 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5805 | if (!merge_const_tuple(c, &consts)) { |
| 5806 | goto error; |
| 5807 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5808 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5809 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5810 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5811 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5812 | maxdepth = stackdepth(c); |
| 5813 | if (maxdepth < 0) { |
| 5814 | goto error; |
| 5815 | } |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5816 | co = PyCode_New(posonlyargcount+posorkeywordargcount, posonlyargcount, |
| 5817 | kwonlyargcount, nlocals_int, maxdepth, flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5818 | bytecode, consts, names, varnames, |
| 5819 | freevars, cellvars, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5820 | c->c_filename, c->u->u_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5821 | c->u->u_firstlineno, |
| 5822 | a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5823 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | Py_XDECREF(consts); |
| 5825 | Py_XDECREF(names); |
| 5826 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5827 | Py_XDECREF(name); |
| 5828 | Py_XDECREF(freevars); |
| 5829 | Py_XDECREF(cellvars); |
| 5830 | Py_XDECREF(bytecode); |
| 5831 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5832 | } |
| 5833 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5834 | |
| 5835 | /* For debugging purposes only */ |
| 5836 | #if 0 |
| 5837 | static void |
| 5838 | dump_instr(const struct instr *i) |
| 5839 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5840 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5841 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5842 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5844 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5845 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5847 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5848 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5849 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5850 | } |
| 5851 | |
| 5852 | static void |
| 5853 | dump_basicblock(const basicblock *b) |
| 5854 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5855 | const char *seen = b->b_seen ? "seen " : ""; |
| 5856 | const char *b_return = b->b_return ? "return " : ""; |
| 5857 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5858 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5859 | if (b->b_instr) { |
| 5860 | int i; |
| 5861 | for (i = 0; i < b->b_iused; i++) { |
| 5862 | fprintf(stderr, " [%02d] ", i); |
| 5863 | dump_instr(b->b_instr + i); |
| 5864 | } |
| 5865 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5866 | } |
| 5867 | #endif |
| 5868 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5869 | static PyCodeObject * |
| 5870 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5872 | basicblock *b, *entryblock; |
| 5873 | struct assembler a; |
| 5874 | int i, j, nblocks; |
| 5875 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5877 | /* Make sure every block that falls off the end returns None. |
| 5878 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5879 | block ends with a jump or return b_next shouldn't set. |
| 5880 | */ |
| 5881 | if (!c->u->u_curblock->b_return) { |
| 5882 | NEXT_BLOCK(c); |
| 5883 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5884 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | ADDOP(c, RETURN_VALUE); |
| 5886 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5888 | nblocks = 0; |
| 5889 | entryblock = NULL; |
| 5890 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5891 | nblocks++; |
| 5892 | entryblock = b; |
| 5893 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5896 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5897 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5898 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 5899 | else |
| 5900 | c->u->u_firstlineno = 1; |
| 5901 | } |
| 5902 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5903 | goto error; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5904 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5906 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5907 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5908 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5909 | /* Emit code in reverse postorder from dfs. */ |
| 5910 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 5911 | b = a.a_postorder[i]; |
| 5912 | for (j = 0; j < b->b_iused; j++) |
| 5913 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5914 | goto error; |
| 5915 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5917 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5918 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5919 | 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] | 5920 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5921 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5922 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5923 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5924 | assemble_free(&a); |
| 5925 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5926 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5927 | |
| 5928 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5929 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5930 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 5931 | PyArena *arena) |
| 5932 | { |
| 5933 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 5934 | } |