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" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 27 | #include "ast.h" |
| 28 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 29 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 30 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 31 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 33 | #define DEFAULT_BLOCK_SIZE 16 |
| 34 | #define DEFAULT_BLOCKS 8 |
| 35 | #define DEFAULT_CODE_SIZE 128 |
| 36 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 37 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 38 | #define COMP_GENEXP 0 |
| 39 | #define COMP_LISTCOMP 1 |
| 40 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 41 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 42 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 43 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 44 | unsigned i_jabs : 1; |
| 45 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | unsigned char i_opcode; |
| 47 | int i_oparg; |
| 48 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 49 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 52 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 53 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 54 | reverse order that the block are allocated. b_list points to the next |
| 55 | 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] | 56 | struct basicblock_ *b_list; |
| 57 | /* number of instructions used */ |
| 58 | int b_iused; |
| 59 | /* length of instruction array (b_instr) */ |
| 60 | int b_ialloc; |
| 61 | /* pointer to an array of instructions, initially NULL */ |
| 62 | struct instr *b_instr; |
| 63 | /* If b_next is non-NULL, it is a pointer to the next |
| 64 | block reached by normal control flow. */ |
| 65 | struct basicblock_ *b_next; |
| 66 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 67 | unsigned b_seen : 1; |
| 68 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 69 | unsigned b_return : 1; |
| 70 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 71 | int b_startdepth; |
| 72 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 73 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 74 | } basicblock; |
| 75 | |
| 76 | /* fblockinfo tracks the current frame block. |
| 77 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 78 | A frame block is used to handle loops, try/except, and try/finally. |
| 79 | It's called a frame block to distinguish it from a basic block in the |
| 80 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 81 | */ |
| 82 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 83 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END, |
| 84 | WITH, ASYNC_WITH, HANDLER_CLEANUP }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 85 | |
| 86 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | enum fblocktype fb_type; |
| 88 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 89 | /* (optional) type-specific exit or cleanup block */ |
| 90 | basicblock *fb_exit; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 93 | enum { |
| 94 | COMPILER_SCOPE_MODULE, |
| 95 | COMPILER_SCOPE_CLASS, |
| 96 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 97 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 98 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 99 | COMPILER_SCOPE_COMPREHENSION, |
| 100 | }; |
| 101 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 102 | /* The following items change on entry and exit of code blocks. |
| 103 | They must be saved and restored when returning to a block. |
| 104 | */ |
| 105 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 109 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 110 | int u_scope_type; |
| 111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | /* The following fields are dicts that map objects to |
| 113 | the index of them in co_XXX. The index is used as |
| 114 | the argument for opcodes that refer to those collections. |
| 115 | */ |
| 116 | PyObject *u_consts; /* all constants */ |
| 117 | PyObject *u_names; /* all names */ |
| 118 | PyObject *u_varnames; /* local variables */ |
| 119 | PyObject *u_cellvars; /* cell variables */ |
| 120 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 121 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 122 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 123 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 124 | Py_ssize_t u_argcount; /* number of arguments for block */ |
| 125 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 126 | /* Pointer to the most recently allocated block. By following b_list |
| 127 | members, you can reach all early allocated blocks. */ |
| 128 | basicblock *u_blocks; |
| 129 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | int u_nfblocks; |
| 132 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | int u_firstlineno; /* the first lineno of the block */ |
| 135 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 136 | int u_col_offset; /* the offset of the current stmt */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 137 | int u_lineno_set; /* boolean to indicate whether instr |
| 138 | has been generated with current lineno */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 142 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 143 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 144 | 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] | 145 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 146 | |
| 147 | Note that we don't track recursion levels during compilation - the |
| 148 | task of detecting and rejecting excessive levels of nesting is |
| 149 | handled by the symbol analysis pass. |
| 150 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 151 | */ |
| 152 | |
| 153 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 154 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | struct symtable *c_st; |
| 156 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 157 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 158 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 159 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | int c_interactive; /* true if in interactive mode */ |
| 161 | int c_nestlevel; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 162 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 163 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 164 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | struct compiler_unit *u; /* compiler state for current block */ |
| 166 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 167 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 170 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 171 | static void compiler_free(struct compiler *); |
| 172 | static basicblock *compiler_new_block(struct compiler *); |
| 173 | static int compiler_next_instr(struct compiler *, basicblock *); |
| 174 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 175 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 176 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 177 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 178 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 179 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 180 | |
| 181 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 182 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 183 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 184 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 185 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 186 | static int compiler_annassign(struct compiler *, stmt_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | static int compiler_visit_slice(struct compiler *, slice_ty, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | expr_context_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 189 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 190 | static int inplace_binop(struct compiler *, operator_ty); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 191 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 192 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 193 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 194 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 195 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 196 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 197 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 198 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 199 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 200 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 201 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 202 | static int compiler_sync_comprehension_generator( |
| 203 | struct compiler *c, |
| 204 | asdl_seq *generators, int gen_index, |
| 205 | expr_ty elt, expr_ty val, int type); |
| 206 | |
| 207 | static int compiler_async_comprehension_generator( |
| 208 | struct compiler *c, |
| 209 | asdl_seq *generators, int gen_index, |
| 210 | expr_ty elt, expr_ty val, int type); |
| 211 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 212 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 213 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 214 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 215 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 216 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 217 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 218 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 219 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | /* Name mangling: __private becomes _classname__private. |
| 221 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 222 | PyObject *result; |
| 223 | size_t nlen, plen, ipriv; |
| 224 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 225 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 226 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 227 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 228 | Py_INCREF(ident); |
| 229 | return ident; |
| 230 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 231 | nlen = PyUnicode_GET_LENGTH(ident); |
| 232 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | The only time a name with a dot can occur is when |
| 236 | we are compiling an import statement that has a |
| 237 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | TODO(jhylton): Decide whether we want to support |
| 240 | mangling of the module name, e.g. __M.X. |
| 241 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 242 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 243 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 244 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | Py_INCREF(ident); |
| 246 | return ident; /* Don't mangle __whatever__ */ |
| 247 | } |
| 248 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 249 | ipriv = 0; |
| 250 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 251 | ipriv++; |
| 252 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | Py_INCREF(ident); |
| 254 | return ident; /* Don't mangle if class is just underscores */ |
| 255 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 256 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 257 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 258 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 259 | PyErr_SetString(PyExc_OverflowError, |
| 260 | "private identifier too large to be mangled"); |
| 261 | return NULL; |
| 262 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 263 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 264 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 265 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 266 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 267 | |
| 268 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 269 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 271 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 272 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 273 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 274 | Py_DECREF(result); |
| 275 | return NULL; |
| 276 | } |
| 277 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 278 | Py_DECREF(result); |
| 279 | return NULL; |
| 280 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 281 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 282 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 285 | static int |
| 286 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 289 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 290 | c->c_const_cache = PyDict_New(); |
| 291 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | c->c_stack = PyList_New(0); |
| 296 | if (!c->c_stack) { |
| 297 | Py_CLEAR(c->c_const_cache); |
| 298 | return 0; |
| 299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 305 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 306 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | struct compiler c; |
| 309 | PyCodeObject *co = NULL; |
| 310 | PyCompilerFlags local_flags; |
| 311 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 312 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 313 | if (!__doc__) { |
| 314 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 315 | if (!__doc__) |
| 316 | return NULL; |
| 317 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 318 | if (!__annotations__) { |
| 319 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 320 | if (!__annotations__) |
| 321 | return NULL; |
| 322 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | if (!compiler_init(&c)) |
| 324 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 325 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | c.c_filename = filename; |
| 327 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 328 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | if (c.c_future == NULL) |
| 330 | goto finally; |
| 331 | if (!flags) { |
| 332 | local_flags.cf_flags = 0; |
Guido van Rossum | 495da29 | 2019-03-07 12:38:08 -0800 | [diff] [blame] | 333 | local_flags.cf_feature_version = PY_MINOR_VERSION; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | flags = &local_flags; |
| 335 | } |
| 336 | merged = c.c_future->ff_features | flags->cf_flags; |
| 337 | c.c_future->ff_features = merged; |
| 338 | flags->cf_flags = merged; |
| 339 | c.c_flags = flags; |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 340 | c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 342 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 343 | if (!_PyAST_Optimize(mod, arena, c.c_optimize)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 344 | goto finally; |
| 345 | } |
| 346 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 347 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | if (c.c_st == NULL) { |
| 349 | if (!PyErr_Occurred()) |
| 350 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 351 | goto finally; |
| 352 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 355 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 356 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | compiler_free(&c); |
| 358 | assert(co || PyErr_Occurred()); |
| 359 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 363 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 364 | int optimize, PyArena *arena) |
| 365 | { |
| 366 | PyObject *filename; |
| 367 | PyCodeObject *co; |
| 368 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 369 | if (filename == NULL) |
| 370 | return NULL; |
| 371 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 372 | Py_DECREF(filename); |
| 373 | return co; |
| 374 | |
| 375 | } |
| 376 | |
| 377 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 378 | PyNode_Compile(struct _node *n, const char *filename) |
| 379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | PyCodeObject *co = NULL; |
| 381 | mod_ty mod; |
| 382 | PyArena *arena = PyArena_New(); |
| 383 | if (!arena) |
| 384 | return NULL; |
| 385 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 386 | if (mod) |
| 387 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 388 | PyArena_Free(arena); |
| 389 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 392 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 393 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | if (c->c_st) |
| 396 | PySymtable_Free(c->c_st); |
| 397 | if (c->c_future) |
| 398 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 399 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 400 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 404 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 405 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | Py_ssize_t i, n; |
| 408 | PyObject *v, *k; |
| 409 | PyObject *dict = PyDict_New(); |
| 410 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 412 | n = PyList_Size(list); |
| 413 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 414 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | if (!v) { |
| 416 | Py_DECREF(dict); |
| 417 | return NULL; |
| 418 | } |
| 419 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 420 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 421 | Py_DECREF(v); |
| 422 | Py_DECREF(dict); |
| 423 | return NULL; |
| 424 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | Py_DECREF(v); |
| 426 | } |
| 427 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* Return new dict containing names from src that match scope(s). |
| 431 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 432 | 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] | 433 | 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] | 434 | values are integers, starting at offset and increasing by one for |
| 435 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 436 | */ |
| 437 | |
| 438 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 439 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 440 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 441 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 442 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 443 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 445 | assert(offset >= 0); |
| 446 | if (dest == NULL) |
| 447 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 448 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 449 | /* Sort the keys so that we have a deterministic order on the indexes |
| 450 | saved in the returned dictionary. These indexes are used as indexes |
| 451 | into the free and cell var storage. Therefore if they aren't |
| 452 | deterministic, then the generated bytecode is not deterministic. |
| 453 | */ |
| 454 | sorted_keys = PyDict_Keys(src); |
| 455 | if (sorted_keys == NULL) |
| 456 | return NULL; |
| 457 | if (PyList_Sort(sorted_keys) != 0) { |
| 458 | Py_DECREF(sorted_keys); |
| 459 | return NULL; |
| 460 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 461 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 462 | |
| 463 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 464 | /* XXX this should probably be a macro in symtable.h */ |
| 465 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 466 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 467 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | assert(PyLong_Check(v)); |
| 469 | vi = PyLong_AS_LONG(v); |
| 470 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 473 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 475 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | Py_DECREF(dest); |
| 477 | return NULL; |
| 478 | } |
| 479 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 480 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 481 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | Py_DECREF(item); |
| 483 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
| 486 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 489 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 493 | static void |
| 494 | compiler_unit_check(struct compiler_unit *u) |
| 495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | basicblock *block; |
| 497 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 498 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 499 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 500 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | if (block->b_instr != NULL) { |
| 502 | assert(block->b_ialloc > 0); |
| 503 | assert(block->b_iused > 0); |
| 504 | assert(block->b_ialloc >= block->b_iused); |
| 505 | } |
| 506 | else { |
| 507 | assert (block->b_iused == 0); |
| 508 | assert (block->b_ialloc == 0); |
| 509 | } |
| 510 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static void |
| 514 | compiler_unit_free(struct compiler_unit *u) |
| 515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | compiler_unit_check(u); |
| 519 | b = u->u_blocks; |
| 520 | while (b != NULL) { |
| 521 | if (b->b_instr) |
| 522 | PyObject_Free((void *)b->b_instr); |
| 523 | next = b->b_list; |
| 524 | PyObject_Free((void *)b); |
| 525 | b = next; |
| 526 | } |
| 527 | Py_CLEAR(u->u_ste); |
| 528 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 529 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | Py_CLEAR(u->u_consts); |
| 531 | Py_CLEAR(u->u_names); |
| 532 | Py_CLEAR(u->u_varnames); |
| 533 | Py_CLEAR(u->u_freevars); |
| 534 | Py_CLEAR(u->u_cellvars); |
| 535 | Py_CLEAR(u->u_private); |
| 536 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 540 | compiler_enter_scope(struct compiler *c, identifier name, |
| 541 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 542 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 544 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 547 | struct compiler_unit)); |
| 548 | if (!u) { |
| 549 | PyErr_NoMemory(); |
| 550 | return 0; |
| 551 | } |
| 552 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 553 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 554 | u->u_argcount = 0; |
| 555 | u->u_kwonlyargcount = 0; |
| 556 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 557 | if (!u->u_ste) { |
| 558 | compiler_unit_free(u); |
| 559 | return 0; |
| 560 | } |
| 561 | Py_INCREF(name); |
| 562 | u->u_name = name; |
| 563 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 564 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 565 | if (!u->u_varnames || !u->u_cellvars) { |
| 566 | compiler_unit_free(u); |
| 567 | return 0; |
| 568 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 569 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 570 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 571 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 572 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 573 | int res; |
| 574 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 575 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 576 | name = _PyUnicode_FromId(&PyId___class__); |
| 577 | if (!name) { |
| 578 | compiler_unit_free(u); |
| 579 | return 0; |
| 580 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 581 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 582 | if (res < 0) { |
| 583 | compiler_unit_free(u); |
| 584 | return 0; |
| 585 | } |
| 586 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 587 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 588 | 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] | 589 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | if (!u->u_freevars) { |
| 591 | compiler_unit_free(u); |
| 592 | return 0; |
| 593 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 594 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | u->u_blocks = NULL; |
| 596 | u->u_nfblocks = 0; |
| 597 | u->u_firstlineno = lineno; |
| 598 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 599 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | u->u_lineno_set = 0; |
| 601 | u->u_consts = PyDict_New(); |
| 602 | if (!u->u_consts) { |
| 603 | compiler_unit_free(u); |
| 604 | return 0; |
| 605 | } |
| 606 | u->u_names = PyDict_New(); |
| 607 | if (!u->u_names) { |
| 608 | compiler_unit_free(u); |
| 609 | return 0; |
| 610 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 611 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | u->u_private = NULL; |
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 | /* Push the old compiler_unit on the stack. */ |
| 615 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 616 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 618 | Py_XDECREF(capsule); |
| 619 | compiler_unit_free(u); |
| 620 | return 0; |
| 621 | } |
| 622 | Py_DECREF(capsule); |
| 623 | u->u_private = c->u->u_private; |
| 624 | Py_XINCREF(u->u_private); |
| 625 | } |
| 626 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 628 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 629 | |
| 630 | block = compiler_new_block(c); |
| 631 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 633 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 634 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 635 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 636 | if (!compiler_set_qualname(c)) |
| 637 | return 0; |
| 638 | } |
| 639 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 643 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 644 | compiler_exit_scope(struct compiler *c) |
| 645 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 646 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | c->c_nestlevel--; |
| 650 | compiler_unit_free(c->u); |
| 651 | /* Restore c->u to the parent unit. */ |
| 652 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 653 | if (n >= 0) { |
| 654 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 655 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 656 | assert(c->u); |
| 657 | /* we are deleting from a list so this really shouldn't fail */ |
| 658 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 659 | Py_FatalError("compiler_exit_scope()"); |
| 660 | compiler_unit_check(c->u); |
| 661 | } |
| 662 | else |
| 663 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 664 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 667 | static int |
| 668 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 669 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 670 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 671 | _Py_static_string(dot_locals, ".<locals>"); |
| 672 | Py_ssize_t stack_size; |
| 673 | struct compiler_unit *u = c->u; |
| 674 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 675 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 676 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 677 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 678 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 679 | if (stack_size > 1) { |
| 680 | int scope, force_global = 0; |
| 681 | struct compiler_unit *parent; |
| 682 | PyObject *mangled, *capsule; |
| 683 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 684 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 685 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 686 | assert(parent); |
| 687 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 688 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 689 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 690 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 691 | assert(u->u_name); |
| 692 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 693 | if (!mangled) |
| 694 | return 0; |
| 695 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 696 | Py_DECREF(mangled); |
| 697 | assert(scope != GLOBAL_IMPLICIT); |
| 698 | if (scope == GLOBAL_EXPLICIT) |
| 699 | force_global = 1; |
| 700 | } |
| 701 | |
| 702 | if (!force_global) { |
| 703 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 704 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 705 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 706 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 707 | if (dot_locals_str == NULL) |
| 708 | return 0; |
| 709 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 710 | if (base == NULL) |
| 711 | return 0; |
| 712 | } |
| 713 | else { |
| 714 | Py_INCREF(parent->u_qualname); |
| 715 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 716 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 717 | } |
| 718 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 719 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 720 | if (base != NULL) { |
| 721 | dot_str = _PyUnicode_FromId(&dot); |
| 722 | if (dot_str == NULL) { |
| 723 | Py_DECREF(base); |
| 724 | return 0; |
| 725 | } |
| 726 | name = PyUnicode_Concat(base, dot_str); |
| 727 | Py_DECREF(base); |
| 728 | if (name == NULL) |
| 729 | return 0; |
| 730 | PyUnicode_Append(&name, u->u_name); |
| 731 | if (name == NULL) |
| 732 | return 0; |
| 733 | } |
| 734 | else { |
| 735 | Py_INCREF(u->u_name); |
| 736 | name = u->u_name; |
| 737 | } |
| 738 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 739 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 740 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 743 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 744 | /* Allocate a new block and return a pointer to it. |
| 745 | Returns NULL on error. |
| 746 | */ |
| 747 | |
| 748 | static basicblock * |
| 749 | compiler_new_block(struct compiler *c) |
| 750 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | basicblock *b; |
| 752 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 754 | u = c->u; |
| 755 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 756 | if (b == NULL) { |
| 757 | PyErr_NoMemory(); |
| 758 | return NULL; |
| 759 | } |
| 760 | memset((void *)b, 0, sizeof(basicblock)); |
| 761 | /* Extend the singly linked list of blocks with new block. */ |
| 762 | b->b_list = u->u_blocks; |
| 763 | u->u_blocks = b; |
| 764 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 767 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | compiler_next_block(struct compiler *c) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | basicblock *block = compiler_new_block(c); |
| 771 | if (block == NULL) |
| 772 | return NULL; |
| 773 | c->u->u_curblock->b_next = block; |
| 774 | c->u->u_curblock = block; |
| 775 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static basicblock * |
| 779 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 780 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 781 | assert(block != NULL); |
| 782 | c->u->u_curblock->b_next = block; |
| 783 | c->u->u_curblock = block; |
| 784 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | /* Returns the offset of the next instruction in the current block's |
| 788 | b_instr array. Resizes the b_instr as necessary. |
| 789 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 790 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | |
| 792 | static int |
| 793 | compiler_next_instr(struct compiler *c, basicblock *b) |
| 794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | assert(b != NULL); |
| 796 | if (b->b_instr == NULL) { |
| 797 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 798 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 799 | if (b->b_instr == NULL) { |
| 800 | PyErr_NoMemory(); |
| 801 | return -1; |
| 802 | } |
| 803 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 804 | memset((char *)b->b_instr, 0, |
| 805 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 806 | } |
| 807 | else if (b->b_iused == b->b_ialloc) { |
| 808 | struct instr *tmp; |
| 809 | size_t oldsize, newsize; |
| 810 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 811 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 812 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 813 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 814 | PyErr_NoMemory(); |
| 815 | return -1; |
| 816 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | if (newsize == 0) { |
| 819 | PyErr_NoMemory(); |
| 820 | return -1; |
| 821 | } |
| 822 | b->b_ialloc <<= 1; |
| 823 | tmp = (struct instr *)PyObject_Realloc( |
| 824 | (void *)b->b_instr, newsize); |
| 825 | if (tmp == NULL) { |
| 826 | PyErr_NoMemory(); |
| 827 | return -1; |
| 828 | } |
| 829 | b->b_instr = tmp; |
| 830 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 831 | } |
| 832 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 835 | /* Set the i_lineno member of the instruction at offset off if the |
| 836 | line number for the current expression/statement has not |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 837 | already been set. If it has been set, the call has no effect. |
| 838 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 839 | The line number is reset in the following cases: |
| 840 | - when entering a new scope |
| 841 | - on each statement |
| 842 | - on each expression that start a new line |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 843 | - before the "except" and "finally" clauses |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 844 | - before the "for" and "while" expressions |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 845 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 846 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 847 | static void |
| 848 | compiler_set_lineno(struct compiler *c, int off) |
| 849 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 850 | basicblock *b; |
| 851 | if (c->u->u_lineno_set) |
| 852 | return; |
| 853 | c->u->u_lineno_set = 1; |
| 854 | b = c->u->u_curblock; |
| 855 | b->b_instr[off].i_lineno = c->u->u_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 858 | /* Return the stack effect of opcode with argument oparg. |
| 859 | |
| 860 | Some opcodes have different stack effect when jump to the target and |
| 861 | when not jump. The 'jump' parameter specifies the case: |
| 862 | |
| 863 | * 0 -- when not jump |
| 864 | * 1 -- when jump |
| 865 | * -1 -- maximal |
| 866 | */ |
| 867 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 868 | WITH_CLEANUP_FINISH deterministic. */ |
| 869 | static int |
| 870 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 873 | case NOP: |
| 874 | case EXTENDED_ARG: |
| 875 | return 0; |
| 876 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 877 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 878 | case POP_TOP: |
| 879 | return -1; |
| 880 | case ROT_TWO: |
| 881 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 882 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 883 | return 0; |
| 884 | case DUP_TOP: |
| 885 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 886 | case DUP_TOP_TWO: |
| 887 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 888 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 889 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | case UNARY_POSITIVE: |
| 891 | case UNARY_NEGATIVE: |
| 892 | case UNARY_NOT: |
| 893 | case UNARY_INVERT: |
| 894 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | case SET_ADD: |
| 897 | case LIST_APPEND: |
| 898 | return -1; |
| 899 | case MAP_ADD: |
| 900 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 901 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 902 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | case BINARY_POWER: |
| 904 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 905 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | case BINARY_MODULO: |
| 907 | case BINARY_ADD: |
| 908 | case BINARY_SUBTRACT: |
| 909 | case BINARY_SUBSCR: |
| 910 | case BINARY_FLOOR_DIVIDE: |
| 911 | case BINARY_TRUE_DIVIDE: |
| 912 | return -1; |
| 913 | case INPLACE_FLOOR_DIVIDE: |
| 914 | case INPLACE_TRUE_DIVIDE: |
| 915 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | case INPLACE_ADD: |
| 918 | case INPLACE_SUBTRACT: |
| 919 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 920 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | case INPLACE_MODULO: |
| 922 | return -1; |
| 923 | case STORE_SUBSCR: |
| 924 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | case DELETE_SUBSCR: |
| 926 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | case BINARY_LSHIFT: |
| 929 | case BINARY_RSHIFT: |
| 930 | case BINARY_AND: |
| 931 | case BINARY_XOR: |
| 932 | case BINARY_OR: |
| 933 | return -1; |
| 934 | case INPLACE_POWER: |
| 935 | return -1; |
| 936 | case GET_ITER: |
| 937 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | case PRINT_EXPR: |
| 940 | return -1; |
| 941 | case LOAD_BUILD_CLASS: |
| 942 | return 1; |
| 943 | case INPLACE_LSHIFT: |
| 944 | case INPLACE_RSHIFT: |
| 945 | case INPLACE_AND: |
| 946 | case INPLACE_XOR: |
| 947 | case INPLACE_OR: |
| 948 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 951 | /* 1 in the normal flow. |
| 952 | * Restore the stack position and push 6 values before jumping to |
| 953 | * the handler if an exception be raised. */ |
| 954 | return jump ? 6 : 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 955 | case WITH_CLEANUP_START: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 956 | return 2; /* or 1, depending on TOS */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 957 | case WITH_CLEANUP_FINISH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 958 | /* Pop a variable number of values pushed by WITH_CLEANUP_START |
| 959 | * + __exit__ or __aexit__. */ |
| 960 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | case RETURN_VALUE: |
| 962 | return -1; |
| 963 | case IMPORT_STAR: |
| 964 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 965 | case SETUP_ANNOTATIONS: |
| 966 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | case YIELD_VALUE: |
| 968 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 969 | case YIELD_FROM: |
| 970 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 971 | case POP_BLOCK: |
| 972 | return 0; |
| 973 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 974 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | case END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 976 | case POP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 977 | /* Pop 6 values when an exception was raised. */ |
| 978 | return -6; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 979 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 980 | case STORE_NAME: |
| 981 | return -1; |
| 982 | case DELETE_NAME: |
| 983 | return 0; |
| 984 | case UNPACK_SEQUENCE: |
| 985 | return oparg-1; |
| 986 | case UNPACK_EX: |
| 987 | return (oparg&0xFF) + (oparg>>8); |
| 988 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 989 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 990 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 991 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | case STORE_ATTR: |
| 993 | return -2; |
| 994 | case DELETE_ATTR: |
| 995 | return -1; |
| 996 | case STORE_GLOBAL: |
| 997 | return -1; |
| 998 | case DELETE_GLOBAL: |
| 999 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | case LOAD_CONST: |
| 1001 | return 1; |
| 1002 | case LOAD_NAME: |
| 1003 | return 1; |
| 1004 | case BUILD_TUPLE: |
| 1005 | case BUILD_LIST: |
| 1006 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1007 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | return 1-oparg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1009 | case BUILD_LIST_UNPACK: |
| 1010 | case BUILD_TUPLE_UNPACK: |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 1011 | case BUILD_TUPLE_UNPACK_WITH_CALL: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1012 | case BUILD_SET_UNPACK: |
| 1013 | case BUILD_MAP_UNPACK: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1014 | case BUILD_MAP_UNPACK_WITH_CALL: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1015 | return 1 - oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1017 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1018 | case BUILD_CONST_KEY_MAP: |
| 1019 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | case LOAD_ATTR: |
| 1021 | return 0; |
| 1022 | case COMPARE_OP: |
| 1023 | return -1; |
| 1024 | case IMPORT_NAME: |
| 1025 | return -1; |
| 1026 | case IMPORT_FROM: |
| 1027 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1028 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1029 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | case JUMP_ABSOLUTE: |
| 1032 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1033 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1034 | case JUMP_IF_TRUE_OR_POP: |
| 1035 | case JUMP_IF_FALSE_OR_POP: |
| 1036 | return jump ? 0 : -1; |
| 1037 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | case POP_JUMP_IF_FALSE: |
| 1039 | case POP_JUMP_IF_TRUE: |
| 1040 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | case LOAD_GLOBAL: |
| 1043 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1044 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1045 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1047 | /* 0 in the normal flow. |
| 1048 | * Restore the stack position and push 6 values before jumping to |
| 1049 | * the handler if an exception be raised. */ |
| 1050 | return jump ? 6 : 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1051 | case BEGIN_FINALLY: |
| 1052 | /* Actually pushes 1 value, but count 6 for balancing with |
| 1053 | * END_FINALLY and POP_FINALLY. |
| 1054 | * This is the main reason of using this opcode instead of |
| 1055 | * "LOAD_CONST None". */ |
| 1056 | return 6; |
| 1057 | case CALL_FINALLY: |
| 1058 | return jump ? 1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1059 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | case LOAD_FAST: |
| 1061 | return 1; |
| 1062 | case STORE_FAST: |
| 1063 | return -1; |
| 1064 | case DELETE_FAST: |
| 1065 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | case RAISE_VARARGS: |
| 1068 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1072 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1073 | case CALL_METHOD: |
| 1074 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1076 | return -oparg-1; |
| 1077 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1078 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1079 | case MAKE_FUNCTION: |
| 1080 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1081 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | case BUILD_SLICE: |
| 1083 | if (oparg == 3) |
| 1084 | return -2; |
| 1085 | else |
| 1086 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1087 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1088 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1089 | case LOAD_CLOSURE: |
| 1090 | return 1; |
| 1091 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1092 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1093 | return 1; |
| 1094 | case STORE_DEREF: |
| 1095 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1096 | case DELETE_DEREF: |
| 1097 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1098 | |
| 1099 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1100 | case GET_AWAITABLE: |
| 1101 | return 0; |
| 1102 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1103 | /* 0 in the normal flow. |
| 1104 | * Restore the stack position to the position before the result |
| 1105 | * of __aenter__ and push 6 values before jumping to the handler |
| 1106 | * if an exception be raised. */ |
| 1107 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1108 | case BEFORE_ASYNC_WITH: |
| 1109 | return 1; |
| 1110 | case GET_AITER: |
| 1111 | return 0; |
| 1112 | case GET_ANEXT: |
| 1113 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1114 | case GET_YIELD_FROM_ITER: |
| 1115 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1116 | case END_ASYNC_FOR: |
| 1117 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1118 | case FORMAT_VALUE: |
| 1119 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1120 | else 1->1. */ |
| 1121 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1122 | case LOAD_METHOD: |
| 1123 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1125 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1127 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1130 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1131 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1132 | { |
| 1133 | return stack_effect(opcode, oparg, jump); |
| 1134 | } |
| 1135 | |
| 1136 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1137 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1138 | { |
| 1139 | return stack_effect(opcode, oparg, -1); |
| 1140 | } |
| 1141 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1142 | /* Add an opcode with no argument. |
| 1143 | Returns 0 on failure, 1 on success. |
| 1144 | */ |
| 1145 | |
| 1146 | static int |
| 1147 | compiler_addop(struct compiler *c, int opcode) |
| 1148 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | basicblock *b; |
| 1150 | struct instr *i; |
| 1151 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1152 | assert(!HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1153 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1154 | if (off < 0) |
| 1155 | return 0; |
| 1156 | b = c->u->u_curblock; |
| 1157 | i = &b->b_instr[off]; |
| 1158 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1159 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | if (opcode == RETURN_VALUE) |
| 1161 | b->b_return = 1; |
| 1162 | compiler_set_lineno(c, off); |
| 1163 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1166 | static Py_ssize_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1167 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) |
| 1168 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1169 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1172 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1174 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1176 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1177 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1178 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | return -1; |
| 1181 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1182 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | Py_DECREF(v); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | Py_DECREF(v); |
| 1187 | } |
| 1188 | else |
| 1189 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1190 | return arg; |
| 1191 | } |
| 1192 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1193 | // Merge const *o* recursively and return constant key object. |
| 1194 | static PyObject* |
| 1195 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1196 | { |
| 1197 | // None and Ellipsis are singleton, and key is the singleton. |
| 1198 | // No need to merge object and key. |
| 1199 | if (o == Py_None || o == Py_Ellipsis) { |
| 1200 | Py_INCREF(o); |
| 1201 | return o; |
| 1202 | } |
| 1203 | |
| 1204 | PyObject *key = _PyCode_ConstantKey(o); |
| 1205 | if (key == NULL) { |
| 1206 | return NULL; |
| 1207 | } |
| 1208 | |
| 1209 | // t is borrowed reference |
| 1210 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1211 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1212 | // o is registered in c_const_cache. Just use it. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1213 | Py_INCREF(t); |
| 1214 | Py_DECREF(key); |
| 1215 | return t; |
| 1216 | } |
| 1217 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1218 | // We registered o in c_const_cache. |
| 1219 | // When o is a tuple or frozenset, we want to merge it's |
| 1220 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1221 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1222 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1223 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1224 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1225 | PyObject *u = merge_consts_recursive(c, item); |
| 1226 | if (u == NULL) { |
| 1227 | Py_DECREF(key); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
| 1231 | // See _PyCode_ConstantKey() |
| 1232 | PyObject *v; // borrowed |
| 1233 | if (PyTuple_CheckExact(u)) { |
| 1234 | v = PyTuple_GET_ITEM(u, 1); |
| 1235 | } |
| 1236 | else { |
| 1237 | v = u; |
| 1238 | } |
| 1239 | if (v != item) { |
| 1240 | Py_INCREF(v); |
| 1241 | PyTuple_SET_ITEM(o, i, v); |
| 1242 | Py_DECREF(item); |
| 1243 | } |
| 1244 | |
| 1245 | Py_DECREF(u); |
| 1246 | } |
| 1247 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1248 | else if (PyFrozenSet_CheckExact(o)) { |
| 1249 | // *key* is tuple. And it's first item is frozenset of |
| 1250 | // constant keys. |
| 1251 | // See _PyCode_ConstantKey() for detail. |
| 1252 | assert(PyTuple_CheckExact(key)); |
| 1253 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1254 | |
| 1255 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1256 | if (len == 0) { // empty frozenset should not be re-created. |
| 1257 | return key; |
| 1258 | } |
| 1259 | PyObject *tuple = PyTuple_New(len); |
| 1260 | if (tuple == NULL) { |
| 1261 | Py_DECREF(key); |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | Py_ssize_t i = 0, pos = 0; |
| 1265 | PyObject *item; |
| 1266 | Py_hash_t hash; |
| 1267 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1268 | PyObject *k = merge_consts_recursive(c, item); |
| 1269 | if (k == NULL) { |
| 1270 | Py_DECREF(tuple); |
| 1271 | Py_DECREF(key); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | PyObject *u; |
| 1275 | if (PyTuple_CheckExact(k)) { |
| 1276 | u = PyTuple_GET_ITEM(k, 1); |
| 1277 | Py_INCREF(u); |
| 1278 | Py_DECREF(k); |
| 1279 | } |
| 1280 | else { |
| 1281 | u = k; |
| 1282 | } |
| 1283 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1284 | i++; |
| 1285 | } |
| 1286 | |
| 1287 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1288 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1289 | PyObject *new = PyFrozenSet_New(tuple); |
| 1290 | Py_DECREF(tuple); |
| 1291 | if (new == NULL) { |
| 1292 | Py_DECREF(key); |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1296 | Py_DECREF(o); |
| 1297 | PyTuple_SET_ITEM(key, 1, new); |
| 1298 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1299 | |
| 1300 | return key; |
| 1301 | } |
| 1302 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1303 | static Py_ssize_t |
| 1304 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1305 | { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1306 | PyObject *key = merge_consts_recursive(c, o); |
| 1307 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1308 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1309 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1310 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1311 | Py_ssize_t arg = compiler_add_o(c, c->u->u_consts, key); |
| 1312 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1313 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1317 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1318 | { |
| 1319 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1320 | if (arg < 0) |
| 1321 | return 0; |
| 1322 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1323 | } |
| 1324 | |
| 1325 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1326 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1327 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1328 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1329 | Py_ssize_t arg = compiler_add_o(c, dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1330 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1331 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1332 | return compiler_addop_i(c, opcode, arg); |
| 1333 | } |
| 1334 | |
| 1335 | static int |
| 1336 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1338 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1339 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1340 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1341 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1342 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1343 | arg = compiler_add_o(c, dict, mangled); |
| 1344 | Py_DECREF(mangled); |
| 1345 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1346 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1347 | return compiler_addop_i(c, opcode, arg); |
| 1348 | } |
| 1349 | |
| 1350 | /* Add an opcode with an integer argument. |
| 1351 | Returns 0 on failure, 1 on success. |
| 1352 | */ |
| 1353 | |
| 1354 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1355 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | struct instr *i; |
| 1358 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1359 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1360 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1361 | it in the C code (like Python/ceval.c). |
| 1362 | |
| 1363 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1364 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1365 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1366 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1367 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1368 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1370 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1371 | if (off < 0) |
| 1372 | return 0; |
| 1373 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1374 | i->i_opcode = opcode; |
| 1375 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1376 | compiler_set_lineno(c, off); |
| 1377 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | static int |
| 1381 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | struct instr *i; |
| 1384 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1385 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1386 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | assert(b != NULL); |
| 1388 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1389 | if (off < 0) |
| 1390 | return 0; |
| 1391 | i = &c->u->u_curblock->b_instr[off]; |
| 1392 | i->i_opcode = opcode; |
| 1393 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1394 | if (absolute) |
| 1395 | i->i_jabs = 1; |
| 1396 | else |
| 1397 | i->i_jrel = 1; |
| 1398 | compiler_set_lineno(c, off); |
| 1399 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1402 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1403 | to the new block. |
| 1404 | |
| 1405 | The returns inside this macro make it impossible to decref objects |
| 1406 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1407 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1408 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1409 | if (compiler_next_block((C)) == NULL) \ |
| 1410 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | if (!compiler_addop((C), (OP))) \ |
| 1415 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1416 | } |
| 1417 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1418 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | if (!compiler_addop((C), (OP))) { \ |
| 1420 | compiler_exit_scope(c); \ |
| 1421 | return 0; \ |
| 1422 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1425 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1426 | if (!compiler_addop_load_const((C), (O))) \ |
| 1427 | return 0; \ |
| 1428 | } |
| 1429 | |
| 1430 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1431 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1432 | PyObject *__new_const = (O); \ |
| 1433 | if (__new_const == NULL) { \ |
| 1434 | return 0; \ |
| 1435 | } \ |
| 1436 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1437 | Py_DECREF(__new_const); \ |
| 1438 | return 0; \ |
| 1439 | } \ |
| 1440 | Py_DECREF(__new_const); \ |
| 1441 | } |
| 1442 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1443 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1445 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1448 | /* Same as ADDOP_O, but steals a reference. */ |
| 1449 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1450 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1451 | Py_DECREF((O)); \ |
| 1452 | return 0; \ |
| 1453 | } \ |
| 1454 | Py_DECREF((O)); \ |
| 1455 | } |
| 1456 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1457 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1459 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1463 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1464 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1468 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1469 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1473 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1474 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1478 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1479 | */ |
| 1480 | |
| 1481 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1483 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1484 | } |
| 1485 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1486 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1488 | compiler_exit_scope(c); \ |
| 1489 | return 0; \ |
| 1490 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1495 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | int _i; \ |
| 1500 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1501 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1502 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1503 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1504 | return 0; \ |
| 1505 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1508 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | int _i; \ |
| 1510 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1511 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1512 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1513 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1514 | compiler_exit_scope(c); \ |
| 1515 | return 0; \ |
| 1516 | } \ |
| 1517 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1520 | /* Search if variable annotations are present statically in a block. */ |
| 1521 | |
| 1522 | static int |
| 1523 | find_ann(asdl_seq *stmts) |
| 1524 | { |
| 1525 | int i, j, res = 0; |
| 1526 | stmt_ty st; |
| 1527 | |
| 1528 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1529 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1530 | switch (st->kind) { |
| 1531 | case AnnAssign_kind: |
| 1532 | return 1; |
| 1533 | case For_kind: |
| 1534 | res = find_ann(st->v.For.body) || |
| 1535 | find_ann(st->v.For.orelse); |
| 1536 | break; |
| 1537 | case AsyncFor_kind: |
| 1538 | res = find_ann(st->v.AsyncFor.body) || |
| 1539 | find_ann(st->v.AsyncFor.orelse); |
| 1540 | break; |
| 1541 | case While_kind: |
| 1542 | res = find_ann(st->v.While.body) || |
| 1543 | find_ann(st->v.While.orelse); |
| 1544 | break; |
| 1545 | case If_kind: |
| 1546 | res = find_ann(st->v.If.body) || |
| 1547 | find_ann(st->v.If.orelse); |
| 1548 | break; |
| 1549 | case With_kind: |
| 1550 | res = find_ann(st->v.With.body); |
| 1551 | break; |
| 1552 | case AsyncWith_kind: |
| 1553 | res = find_ann(st->v.AsyncWith.body); |
| 1554 | break; |
| 1555 | case Try_kind: |
| 1556 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1557 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1558 | st->v.Try.handlers, j); |
| 1559 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1560 | return 1; |
| 1561 | } |
| 1562 | } |
| 1563 | res = find_ann(st->v.Try.body) || |
| 1564 | find_ann(st->v.Try.finalbody) || |
| 1565 | find_ann(st->v.Try.orelse); |
| 1566 | break; |
| 1567 | default: |
| 1568 | res = 0; |
| 1569 | } |
| 1570 | if (res) { |
| 1571 | break; |
| 1572 | } |
| 1573 | } |
| 1574 | return res; |
| 1575 | } |
| 1576 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1577 | /* |
| 1578 | * Frame block handling functions |
| 1579 | */ |
| 1580 | |
| 1581 | static int |
| 1582 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
| 1583 | basicblock *exit) |
| 1584 | { |
| 1585 | struct fblockinfo *f; |
| 1586 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1587 | PyErr_SetString(PyExc_SyntaxError, |
| 1588 | "too many statically nested blocks"); |
| 1589 | return 0; |
| 1590 | } |
| 1591 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1592 | f->fb_type = t; |
| 1593 | f->fb_block = b; |
| 1594 | f->fb_exit = exit; |
| 1595 | return 1; |
| 1596 | } |
| 1597 | |
| 1598 | static void |
| 1599 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1600 | { |
| 1601 | struct compiler_unit *u = c->u; |
| 1602 | assert(u->u_nfblocks > 0); |
| 1603 | u->u_nfblocks--; |
| 1604 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1605 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1606 | } |
| 1607 | |
| 1608 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
| 1609 | * popping the blocks will be restored afterwards. |
| 1610 | */ |
| 1611 | static int |
| 1612 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1613 | int preserve_tos) |
| 1614 | { |
| 1615 | switch (info->fb_type) { |
| 1616 | case WHILE_LOOP: |
| 1617 | return 1; |
| 1618 | |
| 1619 | case FINALLY_END: |
| 1620 | ADDOP_I(c, POP_FINALLY, preserve_tos); |
| 1621 | return 1; |
| 1622 | |
| 1623 | case FOR_LOOP: |
| 1624 | /* Pop the iterator */ |
| 1625 | if (preserve_tos) { |
| 1626 | ADDOP(c, ROT_TWO); |
| 1627 | } |
| 1628 | ADDOP(c, POP_TOP); |
| 1629 | return 1; |
| 1630 | |
| 1631 | case EXCEPT: |
| 1632 | ADDOP(c, POP_BLOCK); |
| 1633 | return 1; |
| 1634 | |
| 1635 | case FINALLY_TRY: |
| 1636 | ADDOP(c, POP_BLOCK); |
| 1637 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1638 | return 1; |
| 1639 | |
| 1640 | case WITH: |
| 1641 | case ASYNC_WITH: |
| 1642 | ADDOP(c, POP_BLOCK); |
| 1643 | if (preserve_tos) { |
| 1644 | ADDOP(c, ROT_TWO); |
| 1645 | } |
| 1646 | ADDOP(c, BEGIN_FINALLY); |
| 1647 | ADDOP(c, WITH_CLEANUP_START); |
| 1648 | if (info->fb_type == ASYNC_WITH) { |
| 1649 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1650 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1651 | ADDOP(c, YIELD_FROM); |
| 1652 | } |
| 1653 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 1654 | ADDOP_I(c, POP_FINALLY, 0); |
| 1655 | return 1; |
| 1656 | |
| 1657 | case HANDLER_CLEANUP: |
| 1658 | if (preserve_tos) { |
| 1659 | ADDOP(c, ROT_FOUR); |
| 1660 | } |
| 1661 | if (info->fb_exit) { |
| 1662 | ADDOP(c, POP_BLOCK); |
| 1663 | ADDOP(c, POP_EXCEPT); |
| 1664 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1665 | } |
| 1666 | else { |
| 1667 | ADDOP(c, POP_EXCEPT); |
| 1668 | } |
| 1669 | return 1; |
| 1670 | } |
| 1671 | Py_UNREACHABLE(); |
| 1672 | } |
| 1673 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1674 | /* Compile a sequence of statements, checking for a docstring |
| 1675 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1676 | |
| 1677 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1678 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1679 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1680 | int i = 0; |
| 1681 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1682 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1683 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1684 | /* Set current line number to the line number of first statement. |
| 1685 | This way line number for SETUP_ANNOTATIONS will always |
| 1686 | coincide with the line number of first "real" statement in module. |
| 1687 | If body is empy, then lineno will be set later in assemble. */ |
| 1688 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && |
| 1689 | !c->u->u_lineno && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1690 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1691 | c->u->u_lineno = st->lineno; |
| 1692 | } |
| 1693 | /* Every annotated class and module should have __annotations__. */ |
| 1694 | if (find_ann(stmts)) { |
| 1695 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1696 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1697 | if (!asdl_seq_LEN(stmts)) |
| 1698 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1699 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1700 | if (c->c_optimize < 2) { |
| 1701 | docstring = _PyAST_GetDocString(stmts); |
| 1702 | if (docstring) { |
| 1703 | i = 1; |
| 1704 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1705 | assert(st->kind == Expr_kind); |
| 1706 | VISIT(c, expr, st->v.Expr.value); |
| 1707 | if (!compiler_nameop(c, __doc__, Store)) |
| 1708 | return 0; |
| 1709 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1710 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1711 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1712 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static PyCodeObject * |
| 1717 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | PyCodeObject *co; |
| 1720 | int addNone = 1; |
| 1721 | static PyObject *module; |
| 1722 | if (!module) { |
| 1723 | module = PyUnicode_InternFromString("<module>"); |
| 1724 | if (!module) |
| 1725 | return NULL; |
| 1726 | } |
| 1727 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1728 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1729 | return NULL; |
| 1730 | switch (mod->kind) { |
| 1731 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1732 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | compiler_exit_scope(c); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | break; |
| 1737 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1738 | if (find_ann(mod->v.Interactive.body)) { |
| 1739 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1740 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | c->c_interactive = 1; |
| 1742 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1743 | mod->v.Interactive.body); |
| 1744 | break; |
| 1745 | case Expression_kind: |
| 1746 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1747 | addNone = 0; |
| 1748 | break; |
| 1749 | case Suite_kind: |
| 1750 | PyErr_SetString(PyExc_SystemError, |
| 1751 | "suite should not be possible"); |
| 1752 | return 0; |
| 1753 | default: |
| 1754 | PyErr_Format(PyExc_SystemError, |
| 1755 | "module kind %d should not be possible", |
| 1756 | mod->kind); |
| 1757 | return 0; |
| 1758 | } |
| 1759 | co = assemble(c, addNone); |
| 1760 | compiler_exit_scope(c); |
| 1761 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1764 | /* The test for LOCAL must come before the test for FREE in order to |
| 1765 | handle classes where name is both local and free. The local var is |
| 1766 | 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] | 1767 | */ |
| 1768 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1769 | static int |
| 1770 | get_ref_type(struct compiler *c, PyObject *name) |
| 1771 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1772 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1773 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1774 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1775 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1776 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1777 | if (scope == 0) { |
| 1778 | char buf[350]; |
| 1779 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1780 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1781 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1782 | PyUnicode_AsUTF8(name), |
| 1783 | PyUnicode_AsUTF8(c->u->u_name), |
| 1784 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1785 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1786 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1787 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1788 | ); |
| 1789 | Py_FatalError(buf); |
| 1790 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1792 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
| 1795 | static int |
| 1796 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1797 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1798 | PyObject *v; |
| 1799 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1800 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1801 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1802 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1806 | 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] | 1807 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1808 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1809 | if (qualname == NULL) |
| 1810 | qualname = co->co_name; |
| 1811 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1812 | if (free) { |
| 1813 | for (i = 0; i < free; ++i) { |
| 1814 | /* Bypass com_addop_varname because it will generate |
| 1815 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1816 | */ |
| 1817 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1818 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1820 | /* Special case: If a class contains a method with a |
| 1821 | free variable that has the same name as a method, |
| 1822 | the name will be considered free *and* local in the |
| 1823 | class. It should be handled by the closure, as |
| 1824 | well as by the normal name loookup logic. |
| 1825 | */ |
| 1826 | reftype = get_ref_type(c, name); |
| 1827 | if (reftype == CELL) |
| 1828 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1829 | else /* (reftype == FREE) */ |
| 1830 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1831 | if (arg == -1) { |
| 1832 | fprintf(stderr, |
| 1833 | "lookup %s in %s %d %d\n" |
| 1834 | "freevars of %s: %s\n", |
| 1835 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1836 | PyUnicode_AsUTF8(c->u->u_name), |
| 1837 | reftype, arg, |
| 1838 | PyUnicode_AsUTF8(co->co_name), |
| 1839 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1840 | Py_FatalError("compiler_make_closure()"); |
| 1841 | } |
| 1842 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1843 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1844 | flags |= 0x08; |
| 1845 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1847 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1848 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1849 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | static int |
| 1854 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1855 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1856 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | if (!decos) |
| 1859 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1860 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1861 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1862 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1863 | } |
| 1864 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1868 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1869 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1870 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1871 | /* Push a dict of keyword-only default values. |
| 1872 | |
| 1873 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1874 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1875 | int i; |
| 1876 | PyObject *keys = NULL; |
| 1877 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1879 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1880 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1881 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1882 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1883 | if (!mangled) { |
| 1884 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1886 | if (keys == NULL) { |
| 1887 | keys = PyList_New(1); |
| 1888 | if (keys == NULL) { |
| 1889 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1890 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1891 | } |
| 1892 | PyList_SET_ITEM(keys, 0, mangled); |
| 1893 | } |
| 1894 | else { |
| 1895 | int res = PyList_Append(keys, mangled); |
| 1896 | Py_DECREF(mangled); |
| 1897 | if (res == -1) { |
| 1898 | goto error; |
| 1899 | } |
| 1900 | } |
| 1901 | if (!compiler_visit_expr(c, default_)) { |
| 1902 | goto error; |
| 1903 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1906 | if (keys != NULL) { |
| 1907 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 1908 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 1909 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1910 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1911 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1912 | assert(default_count > 0); |
| 1913 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1914 | } |
| 1915 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1916 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | error: |
| 1920 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1921 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1925 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 1926 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 1927 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1928 | return 1; |
| 1929 | } |
| 1930 | |
| 1931 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1932 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 1933 | expr_ty annotation, PyObject *names) |
| 1934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1935 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1936 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1937 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 1938 | VISIT(c, annexpr, annotation) |
| 1939 | } |
| 1940 | else { |
| 1941 | VISIT(c, expr, annotation); |
| 1942 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1943 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1944 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1945 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1946 | if (PyList_Append(names, mangled) < 0) { |
| 1947 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1948 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1949 | } |
| 1950 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1952 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | static int |
| 1956 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 1957 | PyObject *names) |
| 1958 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1959 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1960 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1961 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1962 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1963 | c, |
| 1964 | arg->arg, |
| 1965 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1966 | names)) |
| 1967 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1968 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1969 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | static int |
| 1973 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 1974 | expr_ty returns) |
| 1975 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1976 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1977 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1978 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1979 | 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] | 1980 | */ |
| 1981 | static identifier return_str; |
| 1982 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1983 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1984 | names = PyList_New(0); |
| 1985 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 1986 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1987 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1988 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1989 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1990 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1991 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1992 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1994 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1995 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1996 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1997 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1998 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2000 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2001 | if (!return_str) { |
| 2002 | return_str = PyUnicode_InternFromString("return"); |
| 2003 | if (!return_str) |
| 2004 | goto error; |
| 2005 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2006 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | goto error; |
| 2008 | } |
| 2009 | |
| 2010 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2011 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2012 | PyObject *keytuple = PyList_AsTuple(names); |
| 2013 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2014 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2015 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2016 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2017 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2018 | else { |
| 2019 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2020 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2021 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2022 | |
| 2023 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2024 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2025 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2029 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2030 | { |
| 2031 | VISIT_SEQ(c, expr, args->defaults); |
| 2032 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2033 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2036 | static Py_ssize_t |
| 2037 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2038 | { |
| 2039 | Py_ssize_t funcflags = 0; |
| 2040 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2041 | if (!compiler_visit_defaults(c, args)) |
| 2042 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2043 | funcflags |= 0x01; |
| 2044 | } |
| 2045 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2046 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2047 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2048 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2049 | return -1; |
| 2050 | } |
| 2051 | else if (res > 0) { |
| 2052 | funcflags |= 0x02; |
| 2053 | } |
| 2054 | } |
| 2055 | return funcflags; |
| 2056 | } |
| 2057 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2058 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2059 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2062 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2063 | arguments_ty args; |
| 2064 | expr_ty returns; |
| 2065 | identifier name; |
| 2066 | asdl_seq* decos; |
| 2067 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2068 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2069 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2070 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2071 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2072 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2073 | if (is_async) { |
| 2074 | assert(s->kind == AsyncFunctionDef_kind); |
| 2075 | |
| 2076 | args = s->v.AsyncFunctionDef.args; |
| 2077 | returns = s->v.AsyncFunctionDef.returns; |
| 2078 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2079 | name = s->v.AsyncFunctionDef.name; |
| 2080 | body = s->v.AsyncFunctionDef.body; |
| 2081 | |
| 2082 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2083 | } else { |
| 2084 | assert(s->kind == FunctionDef_kind); |
| 2085 | |
| 2086 | args = s->v.FunctionDef.args; |
| 2087 | returns = s->v.FunctionDef.returns; |
| 2088 | decos = s->v.FunctionDef.decorator_list; |
| 2089 | name = s->v.FunctionDef.name; |
| 2090 | body = s->v.FunctionDef.body; |
| 2091 | |
| 2092 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2093 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2094 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | if (!compiler_decorators(c, decos)) |
| 2096 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2097 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2098 | firstlineno = s->lineno; |
| 2099 | if (asdl_seq_LEN(decos)) { |
| 2100 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2101 | } |
| 2102 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2103 | funcflags = compiler_default_arguments(c, args); |
| 2104 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2105 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2106 | } |
| 2107 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2108 | annotations = compiler_visit_annotations(c, args, returns); |
| 2109 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2110 | return 0; |
| 2111 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2112 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2113 | funcflags |= 0x04; |
| 2114 | } |
| 2115 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2116 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2117 | return 0; |
| 2118 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2119 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2120 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2121 | if (c->c_optimize < 2) { |
| 2122 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2123 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2124 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2125 | compiler_exit_scope(c); |
| 2126 | return 0; |
| 2127 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | c->u->u_argcount = asdl_seq_LEN(args->args); |
| 2130 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2131 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2132 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2133 | qualname = c->u->u_qualname; |
| 2134 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2135 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2136 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2137 | Py_XDECREF(qualname); |
| 2138 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2140 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2141 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2142 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2143 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2144 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2146 | /* decorators */ |
| 2147 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2148 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2149 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2150 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2151 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | static int |
| 2155 | compiler_class(struct compiler *c, stmt_ty s) |
| 2156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2157 | PyCodeObject *co; |
| 2158 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2159 | int i, firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | if (!compiler_decorators(c, decos)) |
| 2163 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2164 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2165 | firstlineno = s->lineno; |
| 2166 | if (asdl_seq_LEN(decos)) { |
| 2167 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2168 | } |
| 2169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2170 | /* ultimately generate code for: |
| 2171 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2172 | where: |
| 2173 | <func> is a function/closure created from the class body; |
| 2174 | it has a single argument (__locals__) where the dict |
| 2175 | (or MutableSequence) representing the locals is passed |
| 2176 | <name> is the class name |
| 2177 | <bases> is the positional arguments and *varargs argument |
| 2178 | <keywords> is the keyword arguments and **kwds argument |
| 2179 | This borrows from compiler_call. |
| 2180 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2182 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2183 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2184 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2185 | return 0; |
| 2186 | /* this block represents what we do in the new scope */ |
| 2187 | { |
| 2188 | /* use the class name for name mangling */ |
| 2189 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2190 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2191 | /* load (global) __name__ ... */ |
| 2192 | str = PyUnicode_InternFromString("__name__"); |
| 2193 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2194 | Py_XDECREF(str); |
| 2195 | compiler_exit_scope(c); |
| 2196 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2197 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | Py_DECREF(str); |
| 2199 | /* ... and store it as __module__ */ |
| 2200 | str = PyUnicode_InternFromString("__module__"); |
| 2201 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2202 | Py_XDECREF(str); |
| 2203 | compiler_exit_scope(c); |
| 2204 | return 0; |
| 2205 | } |
| 2206 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2207 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2208 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2209 | str = PyUnicode_InternFromString("__qualname__"); |
| 2210 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2211 | Py_XDECREF(str); |
| 2212 | compiler_exit_scope(c); |
| 2213 | return 0; |
| 2214 | } |
| 2215 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2216 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2217 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2218 | compiler_exit_scope(c); |
| 2219 | return 0; |
| 2220 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2221 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2222 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2223 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2224 | str = PyUnicode_InternFromString("__class__"); |
| 2225 | if (str == NULL) { |
| 2226 | compiler_exit_scope(c); |
| 2227 | return 0; |
| 2228 | } |
| 2229 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2230 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2231 | if (i < 0) { |
| 2232 | compiler_exit_scope(c); |
| 2233 | return 0; |
| 2234 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2235 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2237 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2238 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2239 | str = PyUnicode_InternFromString("__classcell__"); |
| 2240 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2241 | Py_XDECREF(str); |
| 2242 | compiler_exit_scope(c); |
| 2243 | return 0; |
| 2244 | } |
| 2245 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2246 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2247 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2248 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2249 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2250 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2251 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2252 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | /* create the code object */ |
| 2254 | co = assemble(c, 1); |
| 2255 | } |
| 2256 | /* leave the new scope */ |
| 2257 | compiler_exit_scope(c); |
| 2258 | if (co == NULL) |
| 2259 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2260 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | /* 2. load the 'build_class' function */ |
| 2262 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2263 | |
| 2264 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2265 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | Py_DECREF(co); |
| 2267 | |
| 2268 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2269 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | |
| 2271 | /* 5. generate the rest of the code for the call */ |
| 2272 | if (!compiler_call_helper(c, 2, |
| 2273 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2274 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2275 | return 0; |
| 2276 | |
| 2277 | /* 6. apply decorators */ |
| 2278 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2279 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2280 | } |
| 2281 | |
| 2282 | /* 7. store into <name> */ |
| 2283 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2284 | return 0; |
| 2285 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2286 | } |
| 2287 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2288 | /* Return 0 if the expression is a constant value except named singletons. |
| 2289 | Return 1 otherwise. */ |
| 2290 | static int |
| 2291 | check_is_arg(expr_ty e) |
| 2292 | { |
| 2293 | if (e->kind != Constant_kind) { |
| 2294 | return 1; |
| 2295 | } |
| 2296 | PyObject *value = e->v.Constant.value; |
| 2297 | return (value == Py_None |
| 2298 | || value == Py_False |
| 2299 | || value == Py_True |
| 2300 | || value == Py_Ellipsis); |
| 2301 | } |
| 2302 | |
| 2303 | /* Check operands of identity chacks ("is" and "is not"). |
| 2304 | Emit a warning if any operand is a constant except named singletons. |
| 2305 | Return 0 on error. |
| 2306 | */ |
| 2307 | static int |
| 2308 | check_compare(struct compiler *c, expr_ty e) |
| 2309 | { |
| 2310 | Py_ssize_t i, n; |
| 2311 | int left = check_is_arg(e->v.Compare.left); |
| 2312 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2313 | for (i = 0; i < n; i++) { |
| 2314 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2315 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2316 | if (op == Is || op == IsNot) { |
| 2317 | if (!right || !left) { |
| 2318 | const char *msg = (op == Is) |
| 2319 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2320 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2321 | return compiler_warn(c, msg); |
| 2322 | } |
| 2323 | } |
| 2324 | left = right; |
| 2325 | } |
| 2326 | return 1; |
| 2327 | } |
| 2328 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2329 | static int |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2330 | cmpop(cmpop_ty op) |
| 2331 | { |
| 2332 | switch (op) { |
| 2333 | case Eq: |
| 2334 | return PyCmp_EQ; |
| 2335 | case NotEq: |
| 2336 | return PyCmp_NE; |
| 2337 | case Lt: |
| 2338 | return PyCmp_LT; |
| 2339 | case LtE: |
| 2340 | return PyCmp_LE; |
| 2341 | case Gt: |
| 2342 | return PyCmp_GT; |
| 2343 | case GtE: |
| 2344 | return PyCmp_GE; |
| 2345 | case Is: |
| 2346 | return PyCmp_IS; |
| 2347 | case IsNot: |
| 2348 | return PyCmp_IS_NOT; |
| 2349 | case In: |
| 2350 | return PyCmp_IN; |
| 2351 | case NotIn: |
| 2352 | return PyCmp_NOT_IN; |
| 2353 | default: |
| 2354 | return PyCmp_BAD; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | static int |
| 2359 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2360 | { |
| 2361 | switch (e->kind) { |
| 2362 | case UnaryOp_kind: |
| 2363 | if (e->v.UnaryOp.op == Not) |
| 2364 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2365 | /* fallback to general implementation */ |
| 2366 | break; |
| 2367 | case BoolOp_kind: { |
| 2368 | asdl_seq *s = e->v.BoolOp.values; |
| 2369 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2370 | assert(n >= 0); |
| 2371 | int cond2 = e->v.BoolOp.op == Or; |
| 2372 | basicblock *next2 = next; |
| 2373 | if (!cond2 != !cond) { |
| 2374 | next2 = compiler_new_block(c); |
| 2375 | if (next2 == NULL) |
| 2376 | return 0; |
| 2377 | } |
| 2378 | for (i = 0; i < n; ++i) { |
| 2379 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2380 | return 0; |
| 2381 | } |
| 2382 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2383 | return 0; |
| 2384 | if (next2 != next) |
| 2385 | compiler_use_next_block(c, next2); |
| 2386 | return 1; |
| 2387 | } |
| 2388 | case IfExp_kind: { |
| 2389 | basicblock *end, *next2; |
| 2390 | end = compiler_new_block(c); |
| 2391 | if (end == NULL) |
| 2392 | return 0; |
| 2393 | next2 = compiler_new_block(c); |
| 2394 | if (next2 == NULL) |
| 2395 | return 0; |
| 2396 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2397 | return 0; |
| 2398 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2399 | return 0; |
| 2400 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2401 | compiler_use_next_block(c, next2); |
| 2402 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2403 | return 0; |
| 2404 | compiler_use_next_block(c, end); |
| 2405 | return 1; |
| 2406 | } |
| 2407 | case Compare_kind: { |
| 2408 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2409 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2410 | if (!check_compare(c, e)) { |
| 2411 | return 0; |
| 2412 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2413 | basicblock *cleanup = compiler_new_block(c); |
| 2414 | if (cleanup == NULL) |
| 2415 | return 0; |
| 2416 | VISIT(c, expr, e->v.Compare.left); |
| 2417 | for (i = 0; i < n; i++) { |
| 2418 | VISIT(c, expr, |
| 2419 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2420 | ADDOP(c, DUP_TOP); |
| 2421 | ADDOP(c, ROT_THREE); |
| 2422 | ADDOP_I(c, COMPARE_OP, |
| 2423 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 2424 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2425 | NEXT_BLOCK(c); |
| 2426 | } |
| 2427 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 2428 | ADDOP_I(c, COMPARE_OP, |
| 2429 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
| 2430 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2431 | basicblock *end = compiler_new_block(c); |
| 2432 | if (end == NULL) |
| 2433 | return 0; |
| 2434 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2435 | compiler_use_next_block(c, cleanup); |
| 2436 | ADDOP(c, POP_TOP); |
| 2437 | if (!cond) { |
| 2438 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2439 | } |
| 2440 | compiler_use_next_block(c, end); |
| 2441 | return 1; |
| 2442 | } |
| 2443 | /* fallback to general implementation */ |
| 2444 | break; |
| 2445 | } |
| 2446 | default: |
| 2447 | /* fallback to general implementation */ |
| 2448 | break; |
| 2449 | } |
| 2450 | |
| 2451 | /* general implementation */ |
| 2452 | VISIT(c, expr, e); |
| 2453 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2454 | return 1; |
| 2455 | } |
| 2456 | |
| 2457 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2458 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2460 | basicblock *end, *next; |
| 2461 | |
| 2462 | assert(e->kind == IfExp_kind); |
| 2463 | end = compiler_new_block(c); |
| 2464 | if (end == NULL) |
| 2465 | return 0; |
| 2466 | next = compiler_new_block(c); |
| 2467 | if (next == NULL) |
| 2468 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2469 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2470 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2471 | VISIT(c, expr, e->v.IfExp.body); |
| 2472 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2473 | compiler_use_next_block(c, next); |
| 2474 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2475 | compiler_use_next_block(c, end); |
| 2476 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2480 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2481 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2483 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2484 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2485 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2486 | arguments_ty args = e->v.Lambda.args; |
| 2487 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2488 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2489 | if (!name) { |
| 2490 | name = PyUnicode_InternFromString("<lambda>"); |
| 2491 | if (!name) |
| 2492 | return 0; |
| 2493 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2494 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2495 | funcflags = compiler_default_arguments(c, args); |
| 2496 | if (funcflags == -1) { |
| 2497 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2498 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2499 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2500 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2501 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2502 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | /* Make None the first constant, so the lambda can't have a |
| 2505 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2506 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | c->u->u_argcount = asdl_seq_LEN(args->args); |
| 2510 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2511 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2512 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2513 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | } |
| 2515 | else { |
| 2516 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2517 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2518 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2519 | qualname = c->u->u_qualname; |
| 2520 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2522 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2524 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2525 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2526 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | Py_DECREF(co); |
| 2528 | |
| 2529 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2533 | compiler_if(struct compiler *c, stmt_ty s) |
| 2534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2535 | basicblock *end, *next; |
| 2536 | int constant; |
| 2537 | assert(s->kind == If_kind); |
| 2538 | end = compiler_new_block(c); |
| 2539 | if (end == NULL) |
| 2540 | return 0; |
| 2541 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2542 | constant = expr_constant(s->v.If.test); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | /* constant = 0: "if 0" |
| 2544 | * constant = 1: "if 1", "if 2", ... |
| 2545 | * constant = -1: rest */ |
| 2546 | if (constant == 0) { |
| 2547 | if (s->v.If.orelse) |
| 2548 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2549 | } else if (constant == 1) { |
| 2550 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2551 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2552 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2553 | next = compiler_new_block(c); |
| 2554 | if (next == NULL) |
| 2555 | return 0; |
| 2556 | } |
| 2557 | else |
| 2558 | next = end; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2559 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) |
| 2560 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2561 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2562 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2563 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2564 | compiler_use_next_block(c, next); |
| 2565 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2566 | } |
| 2567 | } |
| 2568 | compiler_use_next_block(c, end); |
| 2569 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | static int |
| 2573 | compiler_for(struct compiler *c, stmt_ty s) |
| 2574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2577 | start = compiler_new_block(c); |
| 2578 | cleanup = compiler_new_block(c); |
| 2579 | end = compiler_new_block(c); |
| 2580 | if (start == NULL || end == NULL || cleanup == NULL) |
| 2581 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2582 | |
| 2583 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2586 | VISIT(c, expr, s->v.For.iter); |
| 2587 | ADDOP(c, GET_ITER); |
| 2588 | compiler_use_next_block(c, start); |
| 2589 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2590 | VISIT(c, expr, s->v.For.target); |
| 2591 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2592 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2593 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2594 | |
| 2595 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2596 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2598 | compiler_use_next_block(c, end); |
| 2599 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2602 | |
| 2603 | static int |
| 2604 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2605 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2606 | basicblock *start, *except, *end; |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2607 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
| 2608 | return compiler_error(c, "'async for' outside async function"); |
| 2609 | } |
| 2610 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2611 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2612 | except = compiler_new_block(c); |
| 2613 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2614 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2615 | if (start == NULL || except == NULL || end == NULL) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2616 | return 0; |
| 2617 | |
| 2618 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2619 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2620 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2621 | compiler_use_next_block(c, start); |
| 2622 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
| 2623 | return 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2624 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2625 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2626 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2627 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2628 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2629 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2630 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2631 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2632 | /* Success block for __anext__ */ |
| 2633 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2634 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2635 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2636 | |
| 2637 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2638 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2639 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2640 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2641 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2642 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2643 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2644 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2645 | |
| 2646 | compiler_use_next_block(c, end); |
| 2647 | |
| 2648 | return 1; |
| 2649 | } |
| 2650 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2651 | static int |
| 2652 | compiler_while(struct compiler *c, stmt_ty s) |
| 2653 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2655 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2657 | if (constant == 0) { |
| 2658 | if (s->v.While.orelse) |
| 2659 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2660 | return 1; |
| 2661 | } |
| 2662 | loop = compiler_new_block(c); |
| 2663 | end = compiler_new_block(c); |
| 2664 | if (constant == -1) { |
| 2665 | anchor = compiler_new_block(c); |
| 2666 | if (anchor == NULL) |
| 2667 | return 0; |
| 2668 | } |
| 2669 | if (loop == NULL || end == NULL) |
| 2670 | return 0; |
| 2671 | if (s->v.While.orelse) { |
| 2672 | orelse = compiler_new_block(c); |
| 2673 | if (orelse == NULL) |
| 2674 | return 0; |
| 2675 | } |
| 2676 | else |
| 2677 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | compiler_use_next_block(c, loop); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2680 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2681 | return 0; |
| 2682 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2683 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2684 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2685 | } |
| 2686 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2687 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | /* XXX should the two POP instructions be in a separate block |
| 2690 | if there is no else clause ? |
| 2691 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2692 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2693 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2695 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2698 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2699 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2701 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2705 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2706 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2707 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2708 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2709 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2710 | return compiler_error(c, "'return' outside function"); |
| 2711 | if (s->v.Return.value != NULL && |
| 2712 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2713 | { |
| 2714 | return compiler_error( |
| 2715 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2716 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2717 | if (preserve_tos) { |
| 2718 | VISIT(c, expr, s->v.Return.value); |
| 2719 | } |
| 2720 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2721 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2722 | |
| 2723 | if (!compiler_unwind_fblock(c, info, preserve_tos)) |
| 2724 | return 0; |
| 2725 | } |
| 2726 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2727 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2728 | } |
| 2729 | else if (!preserve_tos) { |
| 2730 | VISIT(c, expr, s->v.Return.value); |
| 2731 | } |
| 2732 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2737 | static int |
| 2738 | compiler_break(struct compiler *c) |
| 2739 | { |
| 2740 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2741 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2742 | |
| 2743 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2744 | return 0; |
| 2745 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2746 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit); |
| 2747 | return 1; |
| 2748 | } |
| 2749 | } |
| 2750 | return compiler_error(c, "'break' outside loop"); |
| 2751 | } |
| 2752 | |
| 2753 | static int |
| 2754 | compiler_continue(struct compiler *c) |
| 2755 | { |
| 2756 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2757 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2758 | |
| 2759 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2760 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block); |
| 2761 | return 1; |
| 2762 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2763 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2764 | return 0; |
| 2765 | } |
| 2766 | return compiler_error(c, "'continue' not properly in loop"); |
| 2767 | } |
| 2768 | |
| 2769 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2770 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | |
| 2772 | SETUP_FINALLY L |
| 2773 | <code for body> |
| 2774 | POP_BLOCK |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2775 | BEGIN_FINALLY |
| 2776 | L: |
| 2777 | <code for finalbody> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | END_FINALLY |
| 2779 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2780 | The special instructions use the block stack. Each block |
| 2781 | stack entry contains the instruction that created it (here |
| 2782 | SETUP_FINALLY), the level of the value stack at the time the |
| 2783 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2785 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | Pushes the current value stack level and the label |
| 2787 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2788 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2789 | Pops en entry from the block stack. |
| 2790 | BEGIN_FINALLY |
| 2791 | Pushes NULL onto the value stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2792 | END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2793 | Pops 1 (NULL or int) or 6 entries from the *value* stack and restore |
| 2794 | the raised and the caught exceptions they specify. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2795 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2796 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2797 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2798 | exceptions are pushed onto the value stack (and the exception |
| 2799 | condition is cleared), and the interpreter jumps to the label |
| 2800 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2801 | */ |
| 2802 | |
| 2803 | static int |
| 2804 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2805 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | basicblock *body, *end; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | body = compiler_new_block(c); |
| 2809 | end = compiler_new_block(c); |
| 2810 | if (body == NULL || end == NULL) |
| 2811 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2812 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2813 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2814 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2815 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2816 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2817 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2818 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2819 | if (!compiler_try_except(c, s)) |
| 2820 | return 0; |
| 2821 | } |
| 2822 | else { |
| 2823 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2824 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2826 | ADDOP(c, BEGIN_FINALLY); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | compiler_pop_fblock(c, FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2828 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2829 | /* `finally` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | compiler_use_next_block(c, end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2831 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2832 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2833 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | ADDOP(c, END_FINALLY); |
| 2835 | compiler_pop_fblock(c, FINALLY_END, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2837 | } |
| 2838 | |
| 2839 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2840 | 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] | 2841 | (The contents of the value stack is shown in [], with the top |
| 2842 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2843 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | |
| 2845 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2846 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2847 | [] <code for S> |
| 2848 | [] POP_BLOCK |
| 2849 | [] JUMP_FORWARD L0 |
| 2850 | |
| 2851 | [tb, val, exc] L1: DUP ) |
| 2852 | [tb, val, exc, exc] <evaluate E1> ) |
| 2853 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2854 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2855 | [tb, val, exc] POP |
| 2856 | [tb, val] <assign to V1> (or POP if no V1) |
| 2857 | [tb] POP |
| 2858 | [] <code for S1> |
| 2859 | JUMP_FORWARD L0 |
| 2860 | |
| 2861 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 | .............................etc....................... |
| 2863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2864 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2865 | |
| 2866 | [] L0: <next statement> |
| 2867 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2868 | Of course, parts are not generated if Vi or Ei is not present. |
| 2869 | */ |
| 2870 | static int |
| 2871 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2874 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | body = compiler_new_block(c); |
| 2877 | except = compiler_new_block(c); |
| 2878 | orelse = compiler_new_block(c); |
| 2879 | end = compiler_new_block(c); |
| 2880 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2881 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2882 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2883 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2884 | if (!compiler_push_fblock(c, EXCEPT, body, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2886 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | ADDOP(c, POP_BLOCK); |
| 2888 | compiler_pop_fblock(c, EXCEPT, body); |
| 2889 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2890 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2891 | compiler_use_next_block(c, except); |
| 2892 | for (i = 0; i < n; i++) { |
| 2893 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2894 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 2896 | return compiler_error(c, "default 'except:' must be last"); |
| 2897 | c->u->u_lineno_set = 0; |
| 2898 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 2899 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2900 | except = compiler_new_block(c); |
| 2901 | if (except == NULL) |
| 2902 | return 0; |
| 2903 | if (handler->v.ExceptHandler.type) { |
| 2904 | ADDOP(c, DUP_TOP); |
| 2905 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 2906 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 2907 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 2908 | } |
| 2909 | ADDOP(c, POP_TOP); |
| 2910 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2911 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2912 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2913 | cleanup_end = compiler_new_block(c); |
| 2914 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 2915 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2916 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 2917 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2918 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2919 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 2920 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2922 | /* |
| 2923 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2924 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2925 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2926 | try: |
| 2927 | # body |
| 2928 | finally: |
| 2929 | name = None |
| 2930 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2931 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2933 | /* second try: */ |
| 2934 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 2935 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2936 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2937 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2939 | /* second # body */ |
| 2940 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 2941 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2942 | ADDOP(c, BEGIN_FINALLY); |
| 2943 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2944 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2945 | /* finally: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2946 | compiler_use_next_block(c, cleanup_end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2947 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2948 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2950 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2951 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2952 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2953 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2955 | ADDOP(c, END_FINALLY); |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 2956 | ADDOP(c, POP_EXCEPT); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2957 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | } |
| 2959 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2960 | basicblock *cleanup_body; |
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 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2963 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2964 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2966 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2967 | ADDOP(c, POP_TOP); |
| 2968 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2969 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2970 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2972 | ADDOP(c, POP_EXCEPT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2973 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | } |
| 2975 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2976 | compiler_use_next_block(c, except); |
| 2977 | } |
| 2978 | ADDOP(c, END_FINALLY); |
| 2979 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2980 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2981 | compiler_use_next_block(c, end); |
| 2982 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2986 | compiler_try(struct compiler *c, stmt_ty s) { |
| 2987 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 2988 | return compiler_try_finally(c, s); |
| 2989 | else |
| 2990 | return compiler_try_except(c, s); |
| 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2995 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 2996 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | /* The IMPORT_NAME opcode was already generated. This function |
| 2998 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3000 | 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] | 3001 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3003 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3004 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3005 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3006 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3007 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3009 | while (1) { |
| 3010 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3011 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3012 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3013 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3014 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3015 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3017 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3018 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3019 | if (dot == -1) { |
| 3020 | break; |
| 3021 | } |
| 3022 | ADDOP(c, ROT_TWO); |
| 3023 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3024 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3025 | if (!compiler_nameop(c, asname, Store)) { |
| 3026 | return 0; |
| 3027 | } |
| 3028 | ADDOP(c, POP_TOP); |
| 3029 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | } |
| 3031 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | static int |
| 3035 | compiler_import(struct compiler *c, stmt_ty s) |
| 3036 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3037 | /* The Import node stores a module name like a.b.c as a single |
| 3038 | string. This is convenient for all cases except |
| 3039 | import a.b.c as d |
| 3040 | where we need to parse that string to extract the individual |
| 3041 | module names. |
| 3042 | XXX Perhaps change the representation to make this case simpler? |
| 3043 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3044 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3046 | for (i = 0; i < n; i++) { |
| 3047 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3048 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3049 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3050 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3051 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | if (alias->asname) { |
| 3055 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3056 | if (!r) |
| 3057 | return r; |
| 3058 | } |
| 3059 | else { |
| 3060 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3061 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3062 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3063 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3064 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3065 | if (tmp == NULL) |
| 3066 | return 0; |
| 3067 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3069 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | Py_DECREF(tmp); |
| 3071 | } |
| 3072 | if (!r) |
| 3073 | return r; |
| 3074 | } |
| 3075 | } |
| 3076 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | static int |
| 3080 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3081 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3082 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3083 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3085 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3086 | if (!empty_string) { |
| 3087 | empty_string = PyUnicode_FromString(""); |
| 3088 | if (!empty_string) |
| 3089 | return 0; |
| 3090 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3091 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3092 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3093 | |
| 3094 | names = PyTuple_New(n); |
| 3095 | if (!names) |
| 3096 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3098 | /* build up the names */ |
| 3099 | for (i = 0; i < n; i++) { |
| 3100 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3101 | Py_INCREF(alias->name); |
| 3102 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3103 | } |
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 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3106 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | Py_DECREF(names); |
| 3108 | return compiler_error(c, "from __future__ imports must occur " |
| 3109 | "at the beginning of the file"); |
| 3110 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3111 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | if (s->v.ImportFrom.module) { |
| 3114 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3115 | } |
| 3116 | else { |
| 3117 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3118 | } |
| 3119 | for (i = 0; i < n; i++) { |
| 3120 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3121 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3122 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3123 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3124 | assert(n == 1); |
| 3125 | ADDOP(c, IMPORT_STAR); |
| 3126 | return 1; |
| 3127 | } |
| 3128 | |
| 3129 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3130 | store_name = alias->name; |
| 3131 | if (alias->asname) |
| 3132 | store_name = alias->asname; |
| 3133 | |
| 3134 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3135 | return 0; |
| 3136 | } |
| 3137 | } |
| 3138 | /* remove imported module */ |
| 3139 | ADDOP(c, POP_TOP); |
| 3140 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3141 | } |
| 3142 | |
| 3143 | static int |
| 3144 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | static PyObject *assertion_error = NULL; |
| 3147 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3148 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3149 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3150 | return 1; |
| 3151 | if (assertion_error == NULL) { |
| 3152 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 3153 | if (assertion_error == NULL) |
| 3154 | return 0; |
| 3155 | } |
| 3156 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3157 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3158 | { |
| 3159 | if (!compiler_warn(c, "assertion is always true, " |
| 3160 | "perhaps remove parentheses?")) |
| 3161 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3162 | return 0; |
| 3163 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3165 | end = compiler_new_block(c); |
| 3166 | if (end == NULL) |
| 3167 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3168 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3169 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 3171 | if (s->v.Assert.msg) { |
| 3172 | VISIT(c, expr, s->v.Assert.msg); |
| 3173 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3174 | } |
| 3175 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3176 | compiler_use_next_block(c, end); |
| 3177 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3181 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3182 | { |
| 3183 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3184 | VISIT(c, expr, value); |
| 3185 | ADDOP(c, PRINT_EXPR); |
| 3186 | return 1; |
| 3187 | } |
| 3188 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3189 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3190 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3191 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | VISIT(c, expr, value); |
| 3195 | ADDOP(c, POP_TOP); |
| 3196 | return 1; |
| 3197 | } |
| 3198 | |
| 3199 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3200 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3201 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3202 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 3205 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3206 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3208 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3209 | switch (s->kind) { |
| 3210 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3211 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | case ClassDef_kind: |
| 3213 | return compiler_class(c, s); |
| 3214 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3215 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | case Delete_kind: |
| 3217 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3218 | break; |
| 3219 | case Assign_kind: |
| 3220 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3221 | VISIT(c, expr, s->v.Assign.value); |
| 3222 | for (i = 0; i < n; i++) { |
| 3223 | if (i < n - 1) |
| 3224 | ADDOP(c, DUP_TOP); |
| 3225 | VISIT(c, expr, |
| 3226 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3227 | } |
| 3228 | break; |
| 3229 | case AugAssign_kind: |
| 3230 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3231 | case AnnAssign_kind: |
| 3232 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | case For_kind: |
| 3234 | return compiler_for(c, s); |
| 3235 | case While_kind: |
| 3236 | return compiler_while(c, s); |
| 3237 | case If_kind: |
| 3238 | return compiler_if(c, s); |
| 3239 | case Raise_kind: |
| 3240 | n = 0; |
| 3241 | if (s->v.Raise.exc) { |
| 3242 | VISIT(c, expr, s->v.Raise.exc); |
| 3243 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3244 | if (s->v.Raise.cause) { |
| 3245 | VISIT(c, expr, s->v.Raise.cause); |
| 3246 | n++; |
| 3247 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3248 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3249 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3250 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3251 | case Try_kind: |
| 3252 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | case Assert_kind: |
| 3254 | return compiler_assert(c, s); |
| 3255 | case Import_kind: |
| 3256 | return compiler_import(c, s); |
| 3257 | case ImportFrom_kind: |
| 3258 | return compiler_from_import(c, s); |
| 3259 | case Global_kind: |
| 3260 | case Nonlocal_kind: |
| 3261 | break; |
| 3262 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3263 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | case Pass_kind: |
| 3265 | break; |
| 3266 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3267 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | case Continue_kind: |
| 3269 | return compiler_continue(c); |
| 3270 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3271 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3272 | case AsyncFunctionDef_kind: |
| 3273 | return compiler_function(c, s, 1); |
| 3274 | case AsyncWith_kind: |
| 3275 | return compiler_async_with(c, s, 0); |
| 3276 | case AsyncFor_kind: |
| 3277 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | static int |
| 3284 | unaryop(unaryop_ty op) |
| 3285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3286 | switch (op) { |
| 3287 | case Invert: |
| 3288 | return UNARY_INVERT; |
| 3289 | case Not: |
| 3290 | return UNARY_NOT; |
| 3291 | case UAdd: |
| 3292 | return UNARY_POSITIVE; |
| 3293 | case USub: |
| 3294 | return UNARY_NEGATIVE; |
| 3295 | default: |
| 3296 | PyErr_Format(PyExc_SystemError, |
| 3297 | "unary op %d should not be possible", op); |
| 3298 | return 0; |
| 3299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | static int |
| 3303 | binop(struct compiler *c, operator_ty op) |
| 3304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | switch (op) { |
| 3306 | case Add: |
| 3307 | return BINARY_ADD; |
| 3308 | case Sub: |
| 3309 | return BINARY_SUBTRACT; |
| 3310 | case Mult: |
| 3311 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3312 | case MatMult: |
| 3313 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | case Div: |
| 3315 | return BINARY_TRUE_DIVIDE; |
| 3316 | case Mod: |
| 3317 | return BINARY_MODULO; |
| 3318 | case Pow: |
| 3319 | return BINARY_POWER; |
| 3320 | case LShift: |
| 3321 | return BINARY_LSHIFT; |
| 3322 | case RShift: |
| 3323 | return BINARY_RSHIFT; |
| 3324 | case BitOr: |
| 3325 | return BINARY_OR; |
| 3326 | case BitXor: |
| 3327 | return BINARY_XOR; |
| 3328 | case BitAnd: |
| 3329 | return BINARY_AND; |
| 3330 | case FloorDiv: |
| 3331 | return BINARY_FLOOR_DIVIDE; |
| 3332 | default: |
| 3333 | PyErr_Format(PyExc_SystemError, |
| 3334 | "binary op %d should not be possible", op); |
| 3335 | return 0; |
| 3336 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3340 | inplace_binop(struct compiler *c, operator_ty op) |
| 3341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3342 | switch (op) { |
| 3343 | case Add: |
| 3344 | return INPLACE_ADD; |
| 3345 | case Sub: |
| 3346 | return INPLACE_SUBTRACT; |
| 3347 | case Mult: |
| 3348 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3349 | case MatMult: |
| 3350 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | case Div: |
| 3352 | return INPLACE_TRUE_DIVIDE; |
| 3353 | case Mod: |
| 3354 | return INPLACE_MODULO; |
| 3355 | case Pow: |
| 3356 | return INPLACE_POWER; |
| 3357 | case LShift: |
| 3358 | return INPLACE_LSHIFT; |
| 3359 | case RShift: |
| 3360 | return INPLACE_RSHIFT; |
| 3361 | case BitOr: |
| 3362 | return INPLACE_OR; |
| 3363 | case BitXor: |
| 3364 | return INPLACE_XOR; |
| 3365 | case BitAnd: |
| 3366 | return INPLACE_AND; |
| 3367 | case FloorDiv: |
| 3368 | return INPLACE_FLOOR_DIVIDE; |
| 3369 | default: |
| 3370 | PyErr_Format(PyExc_SystemError, |
| 3371 | "inplace binary op %d should not be possible", op); |
| 3372 | return 0; |
| 3373 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | static int |
| 3377 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3378 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3379 | int op, scope; |
| 3380 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3381 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3382 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3383 | PyObject *dict = c->u->u_names; |
| 3384 | PyObject *mangled; |
| 3385 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3386 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3387 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3388 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3389 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3390 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3391 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3392 | if (!mangled) |
| 3393 | return 0; |
| 3394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3395 | op = 0; |
| 3396 | optype = OP_NAME; |
| 3397 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3398 | switch (scope) { |
| 3399 | case FREE: |
| 3400 | dict = c->u->u_freevars; |
| 3401 | optype = OP_DEREF; |
| 3402 | break; |
| 3403 | case CELL: |
| 3404 | dict = c->u->u_cellvars; |
| 3405 | optype = OP_DEREF; |
| 3406 | break; |
| 3407 | case LOCAL: |
| 3408 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3409 | optype = OP_FAST; |
| 3410 | break; |
| 3411 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3412 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3413 | optype = OP_GLOBAL; |
| 3414 | break; |
| 3415 | case GLOBAL_EXPLICIT: |
| 3416 | optype = OP_GLOBAL; |
| 3417 | break; |
| 3418 | default: |
| 3419 | /* scope can be 0 */ |
| 3420 | break; |
| 3421 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | /* 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] | 3424 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | switch (optype) { |
| 3427 | case OP_DEREF: |
| 3428 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3429 | case Load: |
| 3430 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3431 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3432 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3433 | op = STORE_DEREF; |
| 3434 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3435 | case AugLoad: |
| 3436 | case AugStore: |
| 3437 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3438 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3439 | case Param: |
| 3440 | default: |
| 3441 | PyErr_SetString(PyExc_SystemError, |
| 3442 | "param invalid for deref variable"); |
| 3443 | return 0; |
| 3444 | } |
| 3445 | break; |
| 3446 | case OP_FAST: |
| 3447 | switch (ctx) { |
| 3448 | case Load: op = LOAD_FAST; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3449 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3450 | op = STORE_FAST; |
| 3451 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | case Del: op = DELETE_FAST; break; |
| 3453 | case AugLoad: |
| 3454 | case AugStore: |
| 3455 | break; |
| 3456 | case Param: |
| 3457 | default: |
| 3458 | PyErr_SetString(PyExc_SystemError, |
| 3459 | "param invalid for local variable"); |
| 3460 | return 0; |
| 3461 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3462 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3463 | return 1; |
| 3464 | case OP_GLOBAL: |
| 3465 | switch (ctx) { |
| 3466 | case Load: op = LOAD_GLOBAL; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3467 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3468 | op = STORE_GLOBAL; |
| 3469 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3470 | case Del: op = DELETE_GLOBAL; break; |
| 3471 | case AugLoad: |
| 3472 | case AugStore: |
| 3473 | break; |
| 3474 | case Param: |
| 3475 | default: |
| 3476 | PyErr_SetString(PyExc_SystemError, |
| 3477 | "param invalid for global variable"); |
| 3478 | return 0; |
| 3479 | } |
| 3480 | break; |
| 3481 | case OP_NAME: |
| 3482 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3483 | case Load: op = LOAD_NAME; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3484 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3485 | op = STORE_NAME; |
| 3486 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | case Del: op = DELETE_NAME; break; |
| 3488 | case AugLoad: |
| 3489 | case AugStore: |
| 3490 | break; |
| 3491 | case Param: |
| 3492 | default: |
| 3493 | PyErr_SetString(PyExc_SystemError, |
| 3494 | "param invalid for name variable"); |
| 3495 | return 0; |
| 3496 | } |
| 3497 | break; |
| 3498 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3500 | assert(op); |
| 3501 | arg = compiler_add_o(c, dict, mangled); |
| 3502 | Py_DECREF(mangled); |
| 3503 | if (arg < 0) |
| 3504 | return 0; |
| 3505 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
| 3508 | static int |
| 3509 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3510 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3512 | int jumpi; |
| 3513 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3514 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3516 | assert(e->kind == BoolOp_kind); |
| 3517 | if (e->v.BoolOp.op == And) |
| 3518 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3519 | else |
| 3520 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3521 | end = compiler_new_block(c); |
| 3522 | if (end == NULL) |
| 3523 | return 0; |
| 3524 | s = e->v.BoolOp.values; |
| 3525 | n = asdl_seq_LEN(s) - 1; |
| 3526 | assert(n >= 0); |
| 3527 | for (i = 0; i < n; ++i) { |
| 3528 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3529 | ADDOP_JABS(c, jumpi, end); |
| 3530 | } |
| 3531 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3532 | compiler_use_next_block(c, end); |
| 3533 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
| 3536 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3537 | starunpack_helper(struct compiler *c, asdl_seq *elts, |
| 3538 | int single_op, int inner_op, int outer_op) |
| 3539 | { |
| 3540 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3541 | Py_ssize_t i, nsubitems = 0, nseen = 0; |
| 3542 | for (i = 0; i < n; i++) { |
| 3543 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3544 | if (elt->kind == Starred_kind) { |
| 3545 | if (nseen) { |
| 3546 | ADDOP_I(c, inner_op, nseen); |
| 3547 | nseen = 0; |
| 3548 | nsubitems++; |
| 3549 | } |
| 3550 | VISIT(c, expr, elt->v.Starred.value); |
| 3551 | nsubitems++; |
| 3552 | } |
| 3553 | else { |
| 3554 | VISIT(c, expr, elt); |
| 3555 | nseen++; |
| 3556 | } |
| 3557 | } |
| 3558 | if (nsubitems) { |
| 3559 | if (nseen) { |
| 3560 | ADDOP_I(c, inner_op, nseen); |
| 3561 | nsubitems++; |
| 3562 | } |
| 3563 | ADDOP_I(c, outer_op, nsubitems); |
| 3564 | } |
| 3565 | else |
| 3566 | ADDOP_I(c, single_op, nseen); |
| 3567 | return 1; |
| 3568 | } |
| 3569 | |
| 3570 | static int |
| 3571 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3572 | { |
| 3573 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3574 | Py_ssize_t i; |
| 3575 | int seen_star = 0; |
| 3576 | for (i = 0; i < n; i++) { |
| 3577 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3578 | if (elt->kind == Starred_kind && !seen_star) { |
| 3579 | if ((i >= (1 << 8)) || |
| 3580 | (n-i-1 >= (INT_MAX >> 8))) |
| 3581 | return compiler_error(c, |
| 3582 | "too many expressions in " |
| 3583 | "star-unpacking assignment"); |
| 3584 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3585 | seen_star = 1; |
| 3586 | asdl_seq_SET(elts, i, elt->v.Starred.value); |
| 3587 | } |
| 3588 | else if (elt->kind == Starred_kind) { |
| 3589 | return compiler_error(c, |
| 3590 | "two starred expressions in assignment"); |
| 3591 | } |
| 3592 | } |
| 3593 | if (!seen_star) { |
| 3594 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3595 | } |
| 3596 | VISIT_SEQ(c, expr, elts); |
| 3597 | return 1; |
| 3598 | } |
| 3599 | |
| 3600 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3601 | compiler_list(struct compiler *c, expr_ty e) |
| 3602 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3603 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3604 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3605 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3606 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3607 | else if (e->v.List.ctx == Load) { |
| 3608 | return starunpack_helper(c, elts, |
| 3609 | BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3610 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3611 | else |
| 3612 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3614 | } |
| 3615 | |
| 3616 | static int |
| 3617 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3618 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3619 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3620 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3621 | return assignment_helper(c, elts); |
| 3622 | } |
| 3623 | else if (e->v.Tuple.ctx == Load) { |
| 3624 | return starunpack_helper(c, elts, |
| 3625 | BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK); |
| 3626 | } |
| 3627 | else |
| 3628 | VISIT_SEQ(c, expr, elts); |
| 3629 | return 1; |
| 3630 | } |
| 3631 | |
| 3632 | static int |
| 3633 | compiler_set(struct compiler *c, expr_ty e) |
| 3634 | { |
| 3635 | return starunpack_helper(c, e->v.Set.elts, BUILD_SET, |
| 3636 | BUILD_SET, BUILD_SET_UNPACK); |
| 3637 | } |
| 3638 | |
| 3639 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3640 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3641 | { |
| 3642 | Py_ssize_t i; |
| 3643 | for (i = begin; i < end; i++) { |
| 3644 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3645 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3646 | return 0; |
| 3647 | } |
| 3648 | return 1; |
| 3649 | } |
| 3650 | |
| 3651 | static int |
| 3652 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3653 | { |
| 3654 | Py_ssize_t i, n = end - begin; |
| 3655 | PyObject *keys, *key; |
| 3656 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3657 | for (i = begin; i < end; i++) { |
| 3658 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3659 | } |
| 3660 | keys = PyTuple_New(n); |
| 3661 | if (keys == NULL) { |
| 3662 | return 0; |
| 3663 | } |
| 3664 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3665 | 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] | 3666 | Py_INCREF(key); |
| 3667 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3668 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3669 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3670 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3671 | } |
| 3672 | else { |
| 3673 | for (i = begin; i < end; i++) { |
| 3674 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3675 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3676 | } |
| 3677 | ADDOP_I(c, BUILD_MAP, n); |
| 3678 | } |
| 3679 | return 1; |
| 3680 | } |
| 3681 | |
| 3682 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3683 | compiler_dict(struct compiler *c, expr_ty e) |
| 3684 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3685 | Py_ssize_t i, n, elements; |
| 3686 | int containers; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3687 | int is_unpacking = 0; |
| 3688 | n = asdl_seq_LEN(e->v.Dict.values); |
| 3689 | containers = 0; |
| 3690 | elements = 0; |
| 3691 | for (i = 0; i < n; i++) { |
| 3692 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
| 3693 | if (elements == 0xFFFF || (elements && is_unpacking)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3694 | if (!compiler_subdict(c, e, i - elements, i)) |
| 3695 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3696 | containers++; |
| 3697 | elements = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3699 | if (is_unpacking) { |
| 3700 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3701 | containers++; |
| 3702 | } |
| 3703 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3704 | elements++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3705 | } |
| 3706 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3707 | if (elements || containers == 0) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3708 | if (!compiler_subdict(c, e, n - elements, n)) |
| 3709 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3710 | containers++; |
| 3711 | } |
| 3712 | /* If there is more than one dict, they need to be merged into a new |
| 3713 | * dict. If there is one dict and it's an unpacking, then it needs |
| 3714 | * to be copied into a new dict." */ |
Serhiy Storchaka | 3d85fae | 2016-11-28 20:56:37 +0200 | [diff] [blame] | 3715 | if (containers > 1 || is_unpacking) { |
| 3716 | ADDOP_I(c, BUILD_MAP_UNPACK, containers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3717 | } |
| 3718 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | static int |
| 3722 | compiler_compare(struct compiler *c, expr_ty e) |
| 3723 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3724 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3725 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3726 | if (!check_compare(c, e)) { |
| 3727 | return 0; |
| 3728 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3729 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3730 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3731 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3732 | if (n == 0) { |
| 3733 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 3734 | ADDOP_I(c, COMPARE_OP, |
| 3735 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0)))); |
| 3736 | } |
| 3737 | else { |
| 3738 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | if (cleanup == NULL) |
| 3740 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3741 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3742 | VISIT(c, expr, |
| 3743 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3744 | ADDOP(c, DUP_TOP); |
| 3745 | ADDOP(c, ROT_THREE); |
| 3746 | ADDOP_I(c, COMPARE_OP, |
| 3747 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 3748 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3749 | NEXT_BLOCK(c); |
| 3750 | } |
| 3751 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 3752 | ADDOP_I(c, COMPARE_OP, |
| 3753 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | basicblock *end = compiler_new_block(c); |
| 3755 | if (end == NULL) |
| 3756 | return 0; |
| 3757 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3758 | compiler_use_next_block(c, cleanup); |
| 3759 | ADDOP(c, ROT_TWO); |
| 3760 | ADDOP(c, POP_TOP); |
| 3761 | compiler_use_next_block(c, end); |
| 3762 | } |
| 3763 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3766 | static PyTypeObject * |
| 3767 | infer_type(expr_ty e) |
| 3768 | { |
| 3769 | switch (e->kind) { |
| 3770 | case Tuple_kind: |
| 3771 | return &PyTuple_Type; |
| 3772 | case List_kind: |
| 3773 | case ListComp_kind: |
| 3774 | return &PyList_Type; |
| 3775 | case Dict_kind: |
| 3776 | case DictComp_kind: |
| 3777 | return &PyDict_Type; |
| 3778 | case Set_kind: |
| 3779 | case SetComp_kind: |
| 3780 | return &PySet_Type; |
| 3781 | case GeneratorExp_kind: |
| 3782 | return &PyGen_Type; |
| 3783 | case Lambda_kind: |
| 3784 | return &PyFunction_Type; |
| 3785 | case JoinedStr_kind: |
| 3786 | case FormattedValue_kind: |
| 3787 | return &PyUnicode_Type; |
| 3788 | case Constant_kind: |
| 3789 | return e->v.Constant.value->ob_type; |
| 3790 | default: |
| 3791 | return NULL; |
| 3792 | } |
| 3793 | } |
| 3794 | |
| 3795 | static int |
| 3796 | check_caller(struct compiler *c, expr_ty e) |
| 3797 | { |
| 3798 | switch (e->kind) { |
| 3799 | case Constant_kind: |
| 3800 | case Tuple_kind: |
| 3801 | case List_kind: |
| 3802 | case ListComp_kind: |
| 3803 | case Dict_kind: |
| 3804 | case DictComp_kind: |
| 3805 | case Set_kind: |
| 3806 | case SetComp_kind: |
| 3807 | case GeneratorExp_kind: |
| 3808 | case JoinedStr_kind: |
| 3809 | case FormattedValue_kind: |
| 3810 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3811 | "perhaps you missed a comma?", |
| 3812 | infer_type(e)->tp_name); |
| 3813 | default: |
| 3814 | return 1; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | static int |
| 3819 | check_subscripter(struct compiler *c, expr_ty e) |
| 3820 | { |
| 3821 | PyObject *v; |
| 3822 | |
| 3823 | switch (e->kind) { |
| 3824 | case Constant_kind: |
| 3825 | v = e->v.Constant.value; |
| 3826 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3827 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3828 | PyAnySet_Check(v))) |
| 3829 | { |
| 3830 | return 1; |
| 3831 | } |
| 3832 | /* fall through */ |
| 3833 | case Set_kind: |
| 3834 | case SetComp_kind: |
| 3835 | case GeneratorExp_kind: |
| 3836 | case Lambda_kind: |
| 3837 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3838 | "perhaps you missed a comma?", |
| 3839 | infer_type(e)->tp_name); |
| 3840 | default: |
| 3841 | return 1; |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | static int |
| 3846 | check_index(struct compiler *c, expr_ty e, slice_ty s) |
| 3847 | { |
| 3848 | PyObject *v; |
| 3849 | |
| 3850 | if (s->kind != Index_kind) { |
| 3851 | return 1; |
| 3852 | } |
| 3853 | PyTypeObject *index_type = infer_type(s->v.Index.value); |
| 3854 | if (index_type == NULL |
| 3855 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 3856 | || index_type == &PySlice_Type) { |
| 3857 | return 1; |
| 3858 | } |
| 3859 | |
| 3860 | switch (e->kind) { |
| 3861 | case Constant_kind: |
| 3862 | v = e->v.Constant.value; |
| 3863 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 3864 | return 1; |
| 3865 | } |
| 3866 | /* fall through */ |
| 3867 | case Tuple_kind: |
| 3868 | case List_kind: |
| 3869 | case ListComp_kind: |
| 3870 | case JoinedStr_kind: |
| 3871 | case FormattedValue_kind: |
| 3872 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 3873 | "not %.200s; " |
| 3874 | "perhaps you missed a comma?", |
| 3875 | infer_type(e)->tp_name, |
| 3876 | index_type->tp_name); |
| 3877 | default: |
| 3878 | return 1; |
| 3879 | } |
| 3880 | } |
| 3881 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3882 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3883 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 3884 | { |
| 3885 | Py_ssize_t argsl, i; |
| 3886 | expr_ty meth = e->v.Call.func; |
| 3887 | asdl_seq *args = e->v.Call.args; |
| 3888 | |
| 3889 | /* Check that the call node is an attribute access, and that |
| 3890 | the call doesn't have keyword parameters. */ |
| 3891 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 3892 | asdl_seq_LEN(e->v.Call.keywords)) |
| 3893 | return -1; |
| 3894 | |
| 3895 | /* Check that there are no *varargs types of arguments. */ |
| 3896 | argsl = asdl_seq_LEN(args); |
| 3897 | for (i = 0; i < argsl; i++) { |
| 3898 | expr_ty elt = asdl_seq_GET(args, i); |
| 3899 | if (elt->kind == Starred_kind) { |
| 3900 | return -1; |
| 3901 | } |
| 3902 | } |
| 3903 | |
| 3904 | /* Alright, we can optimize the code. */ |
| 3905 | VISIT(c, expr, meth->v.Attribute.value); |
| 3906 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 3907 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 3908 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 3909 | return 1; |
| 3910 | } |
| 3911 | |
| 3912 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3913 | compiler_call(struct compiler *c, expr_ty e) |
| 3914 | { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3915 | if (maybe_optimize_method_call(c, e) > 0) |
| 3916 | return 1; |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3917 | if (!check_caller(c, e->v.Call.func)) { |
| 3918 | return 0; |
| 3919 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3920 | VISIT(c, expr, e->v.Call.func); |
| 3921 | return compiler_call_helper(c, 0, |
| 3922 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3923 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3926 | static int |
| 3927 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 3928 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3929 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 3930 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 3931 | 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] | 3932 | return 1; |
| 3933 | } |
| 3934 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3935 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3936 | static int |
| 3937 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 3938 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3939 | /* Our oparg encodes 2 pieces of information: the conversion |
| 3940 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3941 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3942 | Convert the conversion char to 2 bits: |
| 3943 | None: 000 0x0 FVC_NONE |
| 3944 | !s : 001 0x1 FVC_STR |
| 3945 | !r : 010 0x2 FVC_REPR |
| 3946 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3947 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3948 | next bit is whether or not we have a format spec: |
| 3949 | yes : 100 0x4 |
| 3950 | no : 000 0x0 |
| 3951 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3952 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3953 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3954 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3955 | /* Evaluate the expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3956 | VISIT(c, expr, e->v.FormattedValue.value); |
| 3957 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3958 | switch (e->v.FormattedValue.conversion) { |
| 3959 | case 's': oparg = FVC_STR; break; |
| 3960 | case 'r': oparg = FVC_REPR; break; |
| 3961 | case 'a': oparg = FVC_ASCII; break; |
| 3962 | case -1: oparg = FVC_NONE; break; |
| 3963 | default: |
| 3964 | PyErr_SetString(PyExc_SystemError, |
| 3965 | "Unrecognized conversion character"); |
| 3966 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3967 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3968 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3969 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3970 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3971 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3972 | } |
| 3973 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3974 | /* And push our opcode and oparg */ |
| 3975 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3976 | return 1; |
| 3977 | } |
| 3978 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3979 | static int |
| 3980 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 3981 | { |
| 3982 | Py_ssize_t i, n = end - begin; |
| 3983 | keyword_ty kw; |
| 3984 | PyObject *keys, *key; |
| 3985 | assert(n > 0); |
| 3986 | if (n > 1) { |
| 3987 | for (i = begin; i < end; i++) { |
| 3988 | kw = asdl_seq_GET(keywords, i); |
| 3989 | VISIT(c, expr, kw->value); |
| 3990 | } |
| 3991 | keys = PyTuple_New(n); |
| 3992 | if (keys == NULL) { |
| 3993 | return 0; |
| 3994 | } |
| 3995 | for (i = begin; i < end; i++) { |
| 3996 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 3997 | Py_INCREF(key); |
| 3998 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3999 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4000 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4001 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4002 | } |
| 4003 | else { |
| 4004 | /* a for loop only executes once */ |
| 4005 | for (i = begin; i < end; i++) { |
| 4006 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4007 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4008 | VISIT(c, expr, kw->value); |
| 4009 | } |
| 4010 | ADDOP_I(c, BUILD_MAP, n); |
| 4011 | } |
| 4012 | return 1; |
| 4013 | } |
| 4014 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4015 | /* shared code between compiler_call and compiler_class */ |
| 4016 | static int |
| 4017 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4018 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4019 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4020 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4021 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4022 | Py_ssize_t i, nseen, nelts, nkwelts; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4023 | int mustdictunpack = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4024 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4025 | /* the number of tuples and dictionaries on the stack */ |
| 4026 | Py_ssize_t nsubargs = 0, nsubkwargs = 0; |
| 4027 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4028 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4029 | nkwelts = asdl_seq_LEN(keywords); |
| 4030 | |
| 4031 | for (i = 0; i < nkwelts; i++) { |
| 4032 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4033 | if (kw->arg == NULL) { |
| 4034 | mustdictunpack = 1; |
| 4035 | break; |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | nseen = n; /* the number of positional arguments on the stack */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4040 | for (i = 0; i < nelts; i++) { |
| 4041 | expr_ty elt = asdl_seq_GET(args, i); |
| 4042 | if (elt->kind == Starred_kind) { |
| 4043 | /* A star-arg. If we've seen positional arguments, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4044 | pack the positional arguments into a tuple. */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4045 | if (nseen) { |
| 4046 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4047 | nseen = 0; |
| 4048 | nsubargs++; |
| 4049 | } |
| 4050 | VISIT(c, expr, elt->v.Starred.value); |
| 4051 | nsubargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4052 | } |
| 4053 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4054 | VISIT(c, expr, elt); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4055 | nseen++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4056 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4057 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4058 | |
| 4059 | /* Same dance again for keyword arguments */ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4060 | if (nsubargs || mustdictunpack) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4061 | if (nseen) { |
| 4062 | /* Pack up any trailing positional arguments. */ |
| 4063 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4064 | nsubargs++; |
| 4065 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4066 | if (nsubargs > 1) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4067 | /* If we ended up with more than one stararg, we need |
| 4068 | to concatenate them into a single sequence. */ |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 4069 | ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4070 | } |
| 4071 | else if (nsubargs == 0) { |
| 4072 | ADDOP_I(c, BUILD_TUPLE, 0); |
| 4073 | } |
| 4074 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4075 | for (i = 0; i < nkwelts; i++) { |
| 4076 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4077 | if (kw->arg == NULL) { |
| 4078 | /* A keyword argument unpacking. */ |
| 4079 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4080 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) |
| 4081 | return 0; |
| 4082 | nsubkwargs++; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4083 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4084 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4085 | VISIT(c, expr, kw->value); |
| 4086 | nsubkwargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4087 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4088 | else { |
| 4089 | nseen++; |
| 4090 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4091 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4092 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4093 | /* Pack up any trailing keyword arguments. */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4094 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4095 | return 0; |
| 4096 | nsubkwargs++; |
| 4097 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4098 | if (nsubkwargs > 1) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4099 | /* Pack it all up */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4100 | ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4101 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4102 | ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); |
| 4103 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4104 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4105 | else if (nkwelts) { |
| 4106 | PyObject *names; |
| 4107 | VISIT_SEQ(c, keyword, keywords); |
| 4108 | names = PyTuple_New(nkwelts); |
| 4109 | if (names == NULL) { |
| 4110 | return 0; |
| 4111 | } |
| 4112 | for (i = 0; i < nkwelts; i++) { |
| 4113 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4114 | Py_INCREF(kw->arg); |
| 4115 | PyTuple_SET_ITEM(names, i, kw->arg); |
| 4116 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4117 | ADDOP_LOAD_CONST_NEW(c, names); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4118 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4119 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4120 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4121 | else { |
| 4122 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4123 | return 1; |
| 4124 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4127 | |
| 4128 | /* List and set comprehensions and generator expressions work by creating a |
| 4129 | nested function to perform the actual iteration. This means that the |
| 4130 | iteration variables don't leak into the current scope. |
| 4131 | The defined function is called immediately following its definition, with the |
| 4132 | result of that call being the result of the expression. |
| 4133 | The LC/SC version returns the populated container, while the GE version is |
| 4134 | flagged in symtable.c as a generator, so it returns the generator object |
| 4135 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4136 | |
| 4137 | Possible cleanups: |
| 4138 | - iterate over the generator sequence instead of using recursion |
| 4139 | */ |
| 4140 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4141 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4142 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4143 | compiler_comprehension_generator(struct compiler *c, |
| 4144 | asdl_seq *generators, int gen_index, |
| 4145 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4146 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4147 | comprehension_ty gen; |
| 4148 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4149 | if (gen->is_async) { |
| 4150 | return compiler_async_comprehension_generator( |
| 4151 | c, generators, gen_index, elt, val, type); |
| 4152 | } else { |
| 4153 | return compiler_sync_comprehension_generator( |
| 4154 | c, generators, gen_index, elt, val, type); |
| 4155 | } |
| 4156 | } |
| 4157 | |
| 4158 | static int |
| 4159 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4160 | asdl_seq *generators, int gen_index, |
| 4161 | expr_ty elt, expr_ty val, int type) |
| 4162 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4163 | /* generate code for the iterator, then each of the ifs, |
| 4164 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4165 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | comprehension_ty gen; |
| 4167 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4168 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4170 | start = compiler_new_block(c); |
| 4171 | skip = compiler_new_block(c); |
| 4172 | if_cleanup = compiler_new_block(c); |
| 4173 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4174 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4175 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4176 | anchor == NULL) |
| 4177 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4178 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4179 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | if (gen_index == 0) { |
| 4182 | /* Receive outermost iter as an implicit argument */ |
| 4183 | c->u->u_argcount = 1; |
| 4184 | ADDOP_I(c, LOAD_FAST, 0); |
| 4185 | } |
| 4186 | else { |
| 4187 | /* Sub-iter - calculate on the fly */ |
| 4188 | VISIT(c, expr, gen->iter); |
| 4189 | ADDOP(c, GET_ITER); |
| 4190 | } |
| 4191 | compiler_use_next_block(c, start); |
| 4192 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4193 | NEXT_BLOCK(c); |
| 4194 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4196 | /* XXX this needs to be cleaned up...a lot! */ |
| 4197 | n = asdl_seq_LEN(gen->ifs); |
| 4198 | for (i = 0; i < n; i++) { |
| 4199 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4200 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4201 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4202 | NEXT_BLOCK(c); |
| 4203 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4204 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4205 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4206 | if (!compiler_comprehension_generator(c, |
| 4207 | generators, gen_index, |
| 4208 | elt, val, type)) |
| 4209 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4210 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4211 | /* only append after the last for generator */ |
| 4212 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4213 | /* comprehension specific code */ |
| 4214 | switch (type) { |
| 4215 | case COMP_GENEXP: |
| 4216 | VISIT(c, expr, elt); |
| 4217 | ADDOP(c, YIELD_VALUE); |
| 4218 | ADDOP(c, POP_TOP); |
| 4219 | break; |
| 4220 | case COMP_LISTCOMP: |
| 4221 | VISIT(c, expr, elt); |
| 4222 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4223 | break; |
| 4224 | case COMP_SETCOMP: |
| 4225 | VISIT(c, expr, elt); |
| 4226 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4227 | break; |
| 4228 | case COMP_DICTCOMP: |
| 4229 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4230 | the same. */ |
| 4231 | VISIT(c, expr, val); |
| 4232 | VISIT(c, expr, elt); |
| 4233 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4234 | break; |
| 4235 | default: |
| 4236 | return 0; |
| 4237 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4239 | compiler_use_next_block(c, skip); |
| 4240 | } |
| 4241 | compiler_use_next_block(c, if_cleanup); |
| 4242 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4243 | compiler_use_next_block(c, anchor); |
| 4244 | |
| 4245 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4249 | compiler_async_comprehension_generator(struct compiler *c, |
| 4250 | asdl_seq *generators, int gen_index, |
| 4251 | expr_ty elt, expr_ty val, int type) |
| 4252 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4253 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4254 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4255 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4256 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4257 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4258 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4259 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4260 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4261 | return 0; |
| 4262 | } |
| 4263 | |
| 4264 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4265 | |
| 4266 | if (gen_index == 0) { |
| 4267 | /* Receive outermost iter as an implicit argument */ |
| 4268 | c->u->u_argcount = 1; |
| 4269 | ADDOP_I(c, LOAD_FAST, 0); |
| 4270 | } |
| 4271 | else { |
| 4272 | /* Sub-iter - calculate on the fly */ |
| 4273 | VISIT(c, expr, gen->iter); |
| 4274 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4277 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4278 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4279 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4280 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4281 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4282 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4283 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4284 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4285 | |
| 4286 | n = asdl_seq_LEN(gen->ifs); |
| 4287 | for (i = 0; i < n; i++) { |
| 4288 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4289 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4290 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4291 | NEXT_BLOCK(c); |
| 4292 | } |
| 4293 | |
| 4294 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4295 | if (!compiler_comprehension_generator(c, |
| 4296 | generators, gen_index, |
| 4297 | elt, val, type)) |
| 4298 | return 0; |
| 4299 | |
| 4300 | /* only append after the last for generator */ |
| 4301 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4302 | /* comprehension specific code */ |
| 4303 | switch (type) { |
| 4304 | case COMP_GENEXP: |
| 4305 | VISIT(c, expr, elt); |
| 4306 | ADDOP(c, YIELD_VALUE); |
| 4307 | ADDOP(c, POP_TOP); |
| 4308 | break; |
| 4309 | case COMP_LISTCOMP: |
| 4310 | VISIT(c, expr, elt); |
| 4311 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4312 | break; |
| 4313 | case COMP_SETCOMP: |
| 4314 | VISIT(c, expr, elt); |
| 4315 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4316 | break; |
| 4317 | case COMP_DICTCOMP: |
| 4318 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4319 | the same. */ |
| 4320 | VISIT(c, expr, val); |
| 4321 | VISIT(c, expr, elt); |
| 4322 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4323 | break; |
| 4324 | default: |
| 4325 | return 0; |
| 4326 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4327 | } |
| 4328 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4329 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4330 | |
| 4331 | compiler_use_next_block(c, except); |
| 4332 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4333 | |
| 4334 | return 1; |
| 4335 | } |
| 4336 | |
| 4337 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4338 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4339 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4340 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4341 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4342 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4343 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4344 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4345 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4346 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4347 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4348 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4349 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4350 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4351 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4352 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4357 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4358 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4359 | compiler_error(c, "asynchronous comprehension outside of " |
| 4360 | "an asynchronous function"); |
| 4361 | goto error_in_scope; |
| 4362 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | if (type != COMP_GENEXP) { |
| 4365 | int op; |
| 4366 | switch (type) { |
| 4367 | case COMP_LISTCOMP: |
| 4368 | op = BUILD_LIST; |
| 4369 | break; |
| 4370 | case COMP_SETCOMP: |
| 4371 | op = BUILD_SET; |
| 4372 | break; |
| 4373 | case COMP_DICTCOMP: |
| 4374 | op = BUILD_MAP; |
| 4375 | break; |
| 4376 | default: |
| 4377 | PyErr_Format(PyExc_SystemError, |
| 4378 | "unknown comprehension type %d", type); |
| 4379 | goto error_in_scope; |
| 4380 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4382 | ADDOP_I(c, op, 0); |
| 4383 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4385 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 4386 | val, type)) |
| 4387 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4389 | if (type != COMP_GENEXP) { |
| 4390 | ADDOP(c, RETURN_VALUE); |
| 4391 | } |
| 4392 | |
| 4393 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4394 | qualname = c->u->u_qualname; |
| 4395 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4396 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4397 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | goto error; |
| 4399 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4400 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4402 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4403 | Py_DECREF(co); |
| 4404 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4405 | VISIT(c, expr, outermost->iter); |
| 4406 | |
| 4407 | if (outermost->is_async) { |
| 4408 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4409 | } else { |
| 4410 | ADDOP(c, GET_ITER); |
| 4411 | } |
| 4412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4414 | |
| 4415 | if (is_async_generator && type != COMP_GENEXP) { |
| 4416 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4417 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4418 | ADDOP(c, YIELD_FROM); |
| 4419 | } |
| 4420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4422 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4423 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4424 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4425 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4426 | Py_XDECREF(co); |
| 4427 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4428 | } |
| 4429 | |
| 4430 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4431 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | static identifier name; |
| 4434 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4435 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | if (!name) |
| 4437 | return 0; |
| 4438 | } |
| 4439 | assert(e->kind == GeneratorExp_kind); |
| 4440 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4441 | e->v.GeneratorExp.generators, |
| 4442 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
| 4445 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4446 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4448 | static identifier name; |
| 4449 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4450 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4451 | if (!name) |
| 4452 | return 0; |
| 4453 | } |
| 4454 | assert(e->kind == ListComp_kind); |
| 4455 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4456 | e->v.ListComp.generators, |
| 4457 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | static int |
| 4461 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4462 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | static identifier name; |
| 4464 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4465 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4466 | if (!name) |
| 4467 | return 0; |
| 4468 | } |
| 4469 | assert(e->kind == SetComp_kind); |
| 4470 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4471 | e->v.SetComp.generators, |
| 4472 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4473 | } |
| 4474 | |
| 4475 | |
| 4476 | static int |
| 4477 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | static identifier name; |
| 4480 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4481 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4482 | if (!name) |
| 4483 | return 0; |
| 4484 | } |
| 4485 | assert(e->kind == DictComp_kind); |
| 4486 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4487 | e->v.DictComp.generators, |
| 4488 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
| 4491 | |
| 4492 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4493 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4494 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4495 | VISIT(c, expr, k->value); |
| 4496 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4499 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4500 | whether they are true or false. |
| 4501 | |
| 4502 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4503 | */ |
| 4504 | |
| 4505 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4506 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4507 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4508 | if (e->kind == Constant_kind) { |
| 4509 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4510 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4511 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4512 | } |
| 4513 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4514 | |
| 4515 | /* |
| 4516 | Implements the async with statement. |
| 4517 | |
| 4518 | The semantics outlined in that PEP are as follows: |
| 4519 | |
| 4520 | async with EXPR as VAR: |
| 4521 | BLOCK |
| 4522 | |
| 4523 | It is implemented roughly as: |
| 4524 | |
| 4525 | context = EXPR |
| 4526 | exit = context.__aexit__ # not calling it |
| 4527 | value = await context.__aenter__() |
| 4528 | try: |
| 4529 | VAR = value # if VAR present in the syntax |
| 4530 | BLOCK |
| 4531 | finally: |
| 4532 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4533 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4534 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4535 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4536 | if not (await exit(*exc)): |
| 4537 | raise |
| 4538 | */ |
| 4539 | static int |
| 4540 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4541 | { |
| 4542 | basicblock *block, *finally; |
| 4543 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4544 | |
| 4545 | assert(s->kind == AsyncWith_kind); |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4546 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
| 4547 | return compiler_error(c, "'async with' outside async function"); |
| 4548 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4549 | |
| 4550 | block = compiler_new_block(c); |
| 4551 | finally = compiler_new_block(c); |
| 4552 | if (!block || !finally) |
| 4553 | return 0; |
| 4554 | |
| 4555 | /* Evaluate EXPR */ |
| 4556 | VISIT(c, expr, item->context_expr); |
| 4557 | |
| 4558 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4559 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4560 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4561 | ADDOP(c, YIELD_FROM); |
| 4562 | |
| 4563 | ADDOP_JREL(c, SETUP_ASYNC_WITH, finally); |
| 4564 | |
| 4565 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4566 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4567 | if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4568 | return 0; |
| 4569 | } |
| 4570 | |
| 4571 | if (item->optional_vars) { |
| 4572 | VISIT(c, expr, item->optional_vars); |
| 4573 | } |
| 4574 | else { |
| 4575 | /* Discard result from context.__aenter__() */ |
| 4576 | ADDOP(c, POP_TOP); |
| 4577 | } |
| 4578 | |
| 4579 | pos++; |
| 4580 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4581 | /* BLOCK code */ |
| 4582 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4583 | else if (!compiler_async_with(c, s, pos)) |
| 4584 | return 0; |
| 4585 | |
| 4586 | /* End of try block; start the finally block */ |
| 4587 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4588 | ADDOP(c, BEGIN_FINALLY); |
| 4589 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4590 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4591 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4592 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4593 | return 0; |
| 4594 | |
| 4595 | /* Finally block starts; context.__exit__ is on the stack under |
| 4596 | the exception or return information. Just issue our magic |
| 4597 | opcode. */ |
| 4598 | ADDOP(c, WITH_CLEANUP_START); |
| 4599 | |
| 4600 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4601 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4602 | ADDOP(c, YIELD_FROM); |
| 4603 | |
| 4604 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 4605 | |
| 4606 | /* Finally block ends. */ |
| 4607 | ADDOP(c, END_FINALLY); |
| 4608 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4609 | return 1; |
| 4610 | } |
| 4611 | |
| 4612 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4613 | /* |
| 4614 | Implements the with statement from PEP 343. |
| 4615 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4616 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4617 | |
| 4618 | with EXPR as VAR: |
| 4619 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4620 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4621 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4623 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4624 | exit = context.__exit__ # not calling it |
| 4625 | value = context.__enter__() |
| 4626 | try: |
| 4627 | VAR = value # if VAR present in the syntax |
| 4628 | BLOCK |
| 4629 | finally: |
| 4630 | if an exception was raised: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4631 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4632 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4633 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4634 | exit(*exc) |
| 4635 | */ |
| 4636 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4637 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4638 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4639 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4640 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4641 | |
| 4642 | assert(s->kind == With_kind); |
| 4643 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4644 | block = compiler_new_block(c); |
| 4645 | finally = compiler_new_block(c); |
| 4646 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4647 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4648 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4649 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4650 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4651 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4652 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4653 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4654 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4655 | if (!compiler_push_fblock(c, WITH, block, finally)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4656 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4659 | if (item->optional_vars) { |
| 4660 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4661 | } |
| 4662 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4663 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4664 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4667 | pos++; |
| 4668 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4669 | /* BLOCK code */ |
| 4670 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4671 | else if (!compiler_with(c, s, pos)) |
| 4672 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4673 | |
| 4674 | /* End of try block; start the finally block */ |
| 4675 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4676 | ADDOP(c, BEGIN_FINALLY); |
| 4677 | compiler_pop_fblock(c, WITH, block); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4678 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4679 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4680 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4681 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4682 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 4683 | /* Finally block starts; context.__exit__ is on the stack under |
| 4684 | the exception or return information. Just issue our magic |
| 4685 | opcode. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4686 | ADDOP(c, WITH_CLEANUP_START); |
| 4687 | ADDOP(c, WITH_CLEANUP_FINISH); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4688 | |
| 4689 | /* Finally block ends. */ |
| 4690 | ADDOP(c, END_FINALLY); |
| 4691 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4692 | return 1; |
| 4693 | } |
| 4694 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4695 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4696 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4698 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4699 | case NamedExpr_kind: |
| 4700 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4701 | ADDOP(c, DUP_TOP); |
| 4702 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4703 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4704 | case BoolOp_kind: |
| 4705 | return compiler_boolop(c, e); |
| 4706 | case BinOp_kind: |
| 4707 | VISIT(c, expr, e->v.BinOp.left); |
| 4708 | VISIT(c, expr, e->v.BinOp.right); |
| 4709 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 4710 | break; |
| 4711 | case UnaryOp_kind: |
| 4712 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4713 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4714 | break; |
| 4715 | case Lambda_kind: |
| 4716 | return compiler_lambda(c, e); |
| 4717 | case IfExp_kind: |
| 4718 | return compiler_ifexp(c, e); |
| 4719 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4720 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4722 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4723 | case GeneratorExp_kind: |
| 4724 | return compiler_genexp(c, e); |
| 4725 | case ListComp_kind: |
| 4726 | return compiler_listcomp(c, e); |
| 4727 | case SetComp_kind: |
| 4728 | return compiler_setcomp(c, e); |
| 4729 | case DictComp_kind: |
| 4730 | return compiler_dictcomp(c, e); |
| 4731 | case Yield_kind: |
| 4732 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4733 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4734 | if (e->v.Yield.value) { |
| 4735 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4736 | } |
| 4737 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4738 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4740 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4742 | case YieldFrom_kind: |
| 4743 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4744 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4745 | |
| 4746 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4747 | return compiler_error(c, "'yield from' inside async function"); |
| 4748 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4749 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4750 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4751 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4752 | ADDOP(c, YIELD_FROM); |
| 4753 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4754 | case Await_kind: |
| 4755 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4756 | return compiler_error(c, "'await' outside function"); |
| 4757 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4758 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
| 4759 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4760 | return compiler_error(c, "'await' outside async function"); |
| 4761 | |
| 4762 | VISIT(c, expr, e->v.Await.value); |
| 4763 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4764 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4765 | ADDOP(c, YIELD_FROM); |
| 4766 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | case Compare_kind: |
| 4768 | return compiler_compare(c, e); |
| 4769 | case Call_kind: |
| 4770 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4771 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4772 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4773 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4774 | case JoinedStr_kind: |
| 4775 | return compiler_joined_str(c, e); |
| 4776 | case FormattedValue_kind: |
| 4777 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4778 | /* The following exprs can be assignment targets. */ |
| 4779 | case Attribute_kind: |
| 4780 | if (e->v.Attribute.ctx != AugStore) |
| 4781 | VISIT(c, expr, e->v.Attribute.value); |
| 4782 | switch (e->v.Attribute.ctx) { |
| 4783 | case AugLoad: |
| 4784 | ADDOP(c, DUP_TOP); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4785 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4786 | case Load: |
| 4787 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4788 | break; |
| 4789 | case AugStore: |
| 4790 | ADDOP(c, ROT_TWO); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4791 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4792 | case Store: |
| 4793 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4794 | break; |
| 4795 | case Del: |
| 4796 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4797 | break; |
| 4798 | case Param: |
| 4799 | default: |
| 4800 | PyErr_SetString(PyExc_SystemError, |
| 4801 | "param invalid in attribute expression"); |
| 4802 | return 0; |
| 4803 | } |
| 4804 | break; |
| 4805 | case Subscript_kind: |
| 4806 | switch (e->v.Subscript.ctx) { |
| 4807 | case AugLoad: |
| 4808 | VISIT(c, expr, e->v.Subscript.value); |
| 4809 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 4810 | break; |
| 4811 | case Load: |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4812 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 4813 | return 0; |
| 4814 | } |
| 4815 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 4816 | return 0; |
| 4817 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4818 | VISIT(c, expr, e->v.Subscript.value); |
| 4819 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 4820 | break; |
| 4821 | case AugStore: |
| 4822 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 4823 | break; |
| 4824 | case Store: |
| 4825 | VISIT(c, expr, e->v.Subscript.value); |
| 4826 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 4827 | break; |
| 4828 | case Del: |
| 4829 | VISIT(c, expr, e->v.Subscript.value); |
| 4830 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 4831 | break; |
| 4832 | case Param: |
| 4833 | default: |
| 4834 | PyErr_SetString(PyExc_SystemError, |
| 4835 | "param invalid in subscript expression"); |
| 4836 | return 0; |
| 4837 | } |
| 4838 | break; |
| 4839 | case Starred_kind: |
| 4840 | switch (e->v.Starred.ctx) { |
| 4841 | case Store: |
| 4842 | /* In all legitimate cases, the Starred node was already replaced |
| 4843 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 4844 | return compiler_error(c, |
| 4845 | "starred assignment target must be in a list or tuple"); |
| 4846 | default: |
| 4847 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4848 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4849 | } |
| 4850 | break; |
| 4851 | case Name_kind: |
| 4852 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 4853 | /* child nodes of List and Tuple will have expr_context set */ |
| 4854 | case List_kind: |
| 4855 | return compiler_list(c, e); |
| 4856 | case Tuple_kind: |
| 4857 | return compiler_tuple(c, e); |
| 4858 | } |
| 4859 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4863 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 4864 | { |
| 4865 | /* If expr e has a different line number than the last expr/stmt, |
| 4866 | set a new line number for the next instruction. |
| 4867 | */ |
| 4868 | int old_lineno = c->u->u_lineno; |
| 4869 | int old_col_offset = c->u->u_col_offset; |
| 4870 | if (e->lineno != c->u->u_lineno) { |
| 4871 | c->u->u_lineno = e->lineno; |
| 4872 | c->u->u_lineno_set = 0; |
| 4873 | } |
| 4874 | /* Updating the column offset is always harmless. */ |
| 4875 | c->u->u_col_offset = e->col_offset; |
| 4876 | |
| 4877 | int res = compiler_visit_expr1(c, e); |
| 4878 | |
| 4879 | if (old_lineno != c->u->u_lineno) { |
| 4880 | c->u->u_lineno = old_lineno; |
| 4881 | c->u->u_lineno_set = 0; |
| 4882 | } |
| 4883 | c->u->u_col_offset = old_col_offset; |
| 4884 | return res; |
| 4885 | } |
| 4886 | |
| 4887 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4888 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 4889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4890 | expr_ty e = s->v.AugAssign.target; |
| 4891 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4893 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4895 | switch (e->kind) { |
| 4896 | case Attribute_kind: |
| 4897 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4898 | AugLoad, e->lineno, e->col_offset, |
| 4899 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4900 | if (auge == NULL) |
| 4901 | return 0; |
| 4902 | VISIT(c, expr, auge); |
| 4903 | VISIT(c, expr, s->v.AugAssign.value); |
| 4904 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4905 | auge->v.Attribute.ctx = AugStore; |
| 4906 | VISIT(c, expr, auge); |
| 4907 | break; |
| 4908 | case Subscript_kind: |
| 4909 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 4910 | AugLoad, e->lineno, e->col_offset, |
| 4911 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4912 | if (auge == NULL) |
| 4913 | return 0; |
| 4914 | VISIT(c, expr, auge); |
| 4915 | VISIT(c, expr, s->v.AugAssign.value); |
| 4916 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4917 | auge->v.Subscript.ctx = AugStore; |
| 4918 | VISIT(c, expr, auge); |
| 4919 | break; |
| 4920 | case Name_kind: |
| 4921 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 4922 | return 0; |
| 4923 | VISIT(c, expr, s->v.AugAssign.value); |
| 4924 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4925 | return compiler_nameop(c, e->v.Name.id, Store); |
| 4926 | default: |
| 4927 | PyErr_Format(PyExc_SystemError, |
| 4928 | "invalid node type (%d) for augmented assignment", |
| 4929 | e->kind); |
| 4930 | return 0; |
| 4931 | } |
| 4932 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 4936 | check_ann_expr(struct compiler *c, expr_ty e) |
| 4937 | { |
| 4938 | VISIT(c, expr, e); |
| 4939 | ADDOP(c, POP_TOP); |
| 4940 | return 1; |
| 4941 | } |
| 4942 | |
| 4943 | static int |
| 4944 | check_annotation(struct compiler *c, stmt_ty s) |
| 4945 | { |
| 4946 | /* Annotations are only evaluated in a module or class. */ |
| 4947 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 4948 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 4949 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 4950 | } |
| 4951 | return 1; |
| 4952 | } |
| 4953 | |
| 4954 | static int |
| 4955 | check_ann_slice(struct compiler *c, slice_ty sl) |
| 4956 | { |
| 4957 | switch(sl->kind) { |
| 4958 | case Index_kind: |
| 4959 | return check_ann_expr(c, sl->v.Index.value); |
| 4960 | case Slice_kind: |
| 4961 | if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { |
| 4962 | return 0; |
| 4963 | } |
| 4964 | if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { |
| 4965 | return 0; |
| 4966 | } |
| 4967 | if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { |
| 4968 | return 0; |
| 4969 | } |
| 4970 | break; |
| 4971 | default: |
| 4972 | PyErr_SetString(PyExc_SystemError, |
| 4973 | "unexpected slice kind"); |
| 4974 | return 0; |
| 4975 | } |
| 4976 | return 1; |
| 4977 | } |
| 4978 | |
| 4979 | static int |
| 4980 | check_ann_subscr(struct compiler *c, slice_ty sl) |
| 4981 | { |
| 4982 | /* We check that everything in a subscript is defined at runtime. */ |
| 4983 | Py_ssize_t i, n; |
| 4984 | |
| 4985 | switch (sl->kind) { |
| 4986 | case Index_kind: |
| 4987 | case Slice_kind: |
| 4988 | if (!check_ann_slice(c, sl)) { |
| 4989 | return 0; |
| 4990 | } |
| 4991 | break; |
| 4992 | case ExtSlice_kind: |
| 4993 | n = asdl_seq_LEN(sl->v.ExtSlice.dims); |
| 4994 | for (i = 0; i < n; i++) { |
| 4995 | slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i); |
| 4996 | switch (subsl->kind) { |
| 4997 | case Index_kind: |
| 4998 | case Slice_kind: |
| 4999 | if (!check_ann_slice(c, subsl)) { |
| 5000 | return 0; |
| 5001 | } |
| 5002 | break; |
| 5003 | case ExtSlice_kind: |
| 5004 | default: |
| 5005 | PyErr_SetString(PyExc_SystemError, |
| 5006 | "extended slice invalid in nested slice"); |
| 5007 | return 0; |
| 5008 | } |
| 5009 | } |
| 5010 | break; |
| 5011 | default: |
| 5012 | PyErr_Format(PyExc_SystemError, |
| 5013 | "invalid subscript kind %d", sl->kind); |
| 5014 | return 0; |
| 5015 | } |
| 5016 | return 1; |
| 5017 | } |
| 5018 | |
| 5019 | static int |
| 5020 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5021 | { |
| 5022 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5023 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5024 | |
| 5025 | assert(s->kind == AnnAssign_kind); |
| 5026 | |
| 5027 | /* We perform the actual assignment first. */ |
| 5028 | if (s->v.AnnAssign.value) { |
| 5029 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5030 | VISIT(c, expr, targ); |
| 5031 | } |
| 5032 | switch (targ->kind) { |
| 5033 | case Name_kind: |
| 5034 | /* If we have a simple name in a module or class, store annotation. */ |
| 5035 | if (s->v.AnnAssign.simple && |
| 5036 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5037 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5038 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5039 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5040 | } |
| 5041 | else { |
| 5042 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5043 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5044 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5045 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5046 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5047 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5048 | } |
| 5049 | break; |
| 5050 | case Attribute_kind: |
| 5051 | if (!s->v.AnnAssign.value && |
| 5052 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5053 | return 0; |
| 5054 | } |
| 5055 | break; |
| 5056 | case Subscript_kind: |
| 5057 | if (!s->v.AnnAssign.value && |
| 5058 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5059 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5060 | return 0; |
| 5061 | } |
| 5062 | break; |
| 5063 | default: |
| 5064 | PyErr_Format(PyExc_SystemError, |
| 5065 | "invalid node type (%d) for annotated assignment", |
| 5066 | targ->kind); |
| 5067 | return 0; |
| 5068 | } |
| 5069 | /* Annotation is evaluated last. */ |
| 5070 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5071 | return 0; |
| 5072 | } |
| 5073 | return 1; |
| 5074 | } |
| 5075 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5076 | /* Raises a SyntaxError and returns 0. |
| 5077 | If something goes wrong, a different exception may be raised. |
| 5078 | */ |
| 5079 | |
| 5080 | static int |
| 5081 | compiler_error(struct compiler *c, const char *errstr) |
| 5082 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5083 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5085 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5086 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5087 | if (!loc) { |
| 5088 | Py_INCREF(Py_None); |
| 5089 | loc = Py_None; |
| 5090 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5091 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5092 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | if (!u) |
| 5094 | goto exit; |
| 5095 | v = Py_BuildValue("(zO)", errstr, u); |
| 5096 | if (!v) |
| 5097 | goto exit; |
| 5098 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5099 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5100 | Py_DECREF(loc); |
| 5101 | Py_XDECREF(u); |
| 5102 | Py_XDECREF(v); |
| 5103 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5104 | } |
| 5105 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5106 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5107 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5108 | and returns 0. |
| 5109 | */ |
| 5110 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5111 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5112 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5113 | va_list vargs; |
| 5114 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5115 | va_start(vargs, format); |
| 5116 | #else |
| 5117 | va_start(vargs); |
| 5118 | #endif |
| 5119 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5120 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5121 | if (msg == NULL) { |
| 5122 | return 0; |
| 5123 | } |
| 5124 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5125 | c->u->u_lineno, NULL, NULL) < 0) |
| 5126 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5127 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5128 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5129 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5130 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5131 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5132 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5133 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5134 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5135 | return 0; |
| 5136 | } |
| 5137 | Py_DECREF(msg); |
| 5138 | return 1; |
| 5139 | } |
| 5140 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5141 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5142 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 5143 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5144 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5147 | /* XXX this code is duplicated */ |
| 5148 | switch (ctx) { |
| 5149 | case AugLoad: /* fall through to Load */ |
| 5150 | case Load: op = BINARY_SUBSCR; break; |
| 5151 | case AugStore:/* fall through to Store */ |
| 5152 | case Store: op = STORE_SUBSCR; break; |
| 5153 | case Del: op = DELETE_SUBSCR; break; |
| 5154 | case Param: |
| 5155 | PyErr_Format(PyExc_SystemError, |
| 5156 | "invalid %s kind %d in subscript\n", |
| 5157 | kind, ctx); |
| 5158 | return 0; |
| 5159 | } |
| 5160 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 5161 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5162 | } |
| 5163 | else if (ctx == AugStore) { |
| 5164 | ADDOP(c, ROT_THREE); |
| 5165 | } |
| 5166 | ADDOP(c, op); |
| 5167 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | static int |
| 5171 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5173 | int n = 2; |
| 5174 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5176 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5177 | if (s->v.Slice.lower) { |
| 5178 | VISIT(c, expr, s->v.Slice.lower); |
| 5179 | } |
| 5180 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5181 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5182 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5184 | if (s->v.Slice.upper) { |
| 5185 | VISIT(c, expr, s->v.Slice.upper); |
| 5186 | } |
| 5187 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5188 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5189 | } |
| 5190 | |
| 5191 | if (s->v.Slice.step) { |
| 5192 | n++; |
| 5193 | VISIT(c, expr, s->v.Slice.step); |
| 5194 | } |
| 5195 | ADDOP_I(c, BUILD_SLICE, n); |
| 5196 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5200 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 5201 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5203 | switch (s->kind) { |
| 5204 | case Slice_kind: |
| 5205 | return compiler_slice(c, s, ctx); |
| 5206 | case Index_kind: |
| 5207 | VISIT(c, expr, s->v.Index.value); |
| 5208 | break; |
| 5209 | case ExtSlice_kind: |
| 5210 | default: |
| 5211 | PyErr_SetString(PyExc_SystemError, |
| 5212 | "extended slice invalid in nested slice"); |
| 5213 | return 0; |
| 5214 | } |
| 5215 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5216 | } |
| 5217 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5218 | static int |
| 5219 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5220 | { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 5221 | const char * kindname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5222 | switch (s->kind) { |
| 5223 | case Index_kind: |
| 5224 | kindname = "index"; |
| 5225 | if (ctx != AugStore) { |
| 5226 | VISIT(c, expr, s->v.Index.value); |
| 5227 | } |
| 5228 | break; |
| 5229 | case Slice_kind: |
| 5230 | kindname = "slice"; |
| 5231 | if (ctx != AugStore) { |
| 5232 | if (!compiler_slice(c, s, ctx)) |
| 5233 | return 0; |
| 5234 | } |
| 5235 | break; |
| 5236 | case ExtSlice_kind: |
| 5237 | kindname = "extended slice"; |
| 5238 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5239 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5240 | for (i = 0; i < n; i++) { |
| 5241 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 5242 | s->v.ExtSlice.dims, i); |
| 5243 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 5244 | return 0; |
| 5245 | } |
| 5246 | ADDOP_I(c, BUILD_TUPLE, n); |
| 5247 | } |
| 5248 | break; |
| 5249 | default: |
| 5250 | PyErr_Format(PyExc_SystemError, |
| 5251 | "invalid subscript kind %d", s->kind); |
| 5252 | return 0; |
| 5253 | } |
| 5254 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5257 | /* End of the compiler section, beginning of the assembler section */ |
| 5258 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5259 | /* do depth-first search of basic block graph, starting with block. |
| 5260 | post records the block indices in post-order. |
| 5261 | |
| 5262 | XXX must handle implicit jumps from one block to next |
| 5263 | */ |
| 5264 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5265 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5266 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5267 | int a_offset; /* offset into bytecode */ |
| 5268 | int a_nblocks; /* number of reachable blocks */ |
| 5269 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 5270 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5271 | int a_lnotab_off; /* offset into lnotab */ |
| 5272 | int a_lineno; /* last lineno of emitted instruction */ |
| 5273 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5274 | }; |
| 5275 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5276 | static void |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5277 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5278 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5279 | int i, j; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5280 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5281 | /* Get rid of recursion for normal control flow. |
| 5282 | Since the number of blocks is limited, unused space in a_postorder |
| 5283 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5284 | blocks. */ |
| 5285 | for (j = end; b && !b->b_seen; b = b->b_next) { |
| 5286 | b->b_seen = 1; |
| 5287 | assert(a->a_nblocks < j); |
| 5288 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5289 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5290 | while (j < end) { |
| 5291 | b = a->a_postorder[j++]; |
| 5292 | for (i = 0; i < b->b_iused; i++) { |
| 5293 | struct instr *instr = &b->b_instr[i]; |
| 5294 | if (instr->i_jrel || instr->i_jabs) |
| 5295 | dfs(c, instr->i_target, a, j); |
| 5296 | } |
| 5297 | assert(a->a_nblocks < j); |
| 5298 | a->a_postorder[a->a_nblocks++] = b; |
| 5299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5300 | } |
| 5301 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5302 | Py_LOCAL_INLINE(void) |
| 5303 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5304 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5305 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5306 | if (b->b_startdepth < depth) { |
| 5307 | assert(b->b_startdepth < 0); |
| 5308 | b->b_startdepth = depth; |
| 5309 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5310 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5311 | } |
| 5312 | |
| 5313 | /* Find the flow path that needs the largest stack. We assume that |
| 5314 | * cycles in the flow graph have no net effect on the stack depth. |
| 5315 | */ |
| 5316 | static int |
| 5317 | stackdepth(struct compiler *c) |
| 5318 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5319 | basicblock *b, *entryblock = NULL; |
| 5320 | basicblock **stack, **sp; |
| 5321 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5322 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5323 | b->b_startdepth = INT_MIN; |
| 5324 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5325 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5326 | } |
| 5327 | if (!entryblock) |
| 5328 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5329 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5330 | if (!stack) { |
| 5331 | PyErr_NoMemory(); |
| 5332 | return -1; |
| 5333 | } |
| 5334 | |
| 5335 | sp = stack; |
| 5336 | stackdepth_push(&sp, entryblock, 0); |
| 5337 | while (sp != stack) { |
| 5338 | b = *--sp; |
| 5339 | int depth = b->b_startdepth; |
| 5340 | assert(depth >= 0); |
| 5341 | basicblock *next = b->b_next; |
| 5342 | for (int i = 0; i < b->b_iused; i++) { |
| 5343 | struct instr *instr = &b->b_instr[i]; |
| 5344 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5345 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5346 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5347 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5348 | } |
| 5349 | int new_depth = depth + effect; |
| 5350 | if (new_depth > maxdepth) { |
| 5351 | maxdepth = new_depth; |
| 5352 | } |
| 5353 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5354 | if (instr->i_jrel || instr->i_jabs) { |
| 5355 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5356 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5357 | int target_depth = depth + effect; |
| 5358 | if (target_depth > maxdepth) { |
| 5359 | maxdepth = target_depth; |
| 5360 | } |
| 5361 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5362 | if (instr->i_opcode == CALL_FINALLY) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5363 | assert(instr->i_target->b_startdepth >= 0); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5364 | assert(instr->i_target->b_startdepth >= target_depth); |
| 5365 | depth = new_depth; |
| 5366 | continue; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5367 | } |
| 5368 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5369 | } |
| 5370 | depth = new_depth; |
| 5371 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5372 | instr->i_opcode == JUMP_FORWARD || |
| 5373 | instr->i_opcode == RETURN_VALUE || |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5374 | instr->i_opcode == RAISE_VARARGS) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5375 | { |
| 5376 | /* remaining code is dead */ |
| 5377 | next = NULL; |
| 5378 | break; |
| 5379 | } |
| 5380 | } |
| 5381 | if (next != NULL) { |
| 5382 | stackdepth_push(&sp, next, depth); |
| 5383 | } |
| 5384 | } |
| 5385 | PyObject_Free(stack); |
| 5386 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5387 | } |
| 5388 | |
| 5389 | static int |
| 5390 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5391 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | memset(a, 0, sizeof(struct assembler)); |
| 5393 | a->a_lineno = firstlineno; |
| 5394 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5395 | if (!a->a_bytecode) |
| 5396 | return 0; |
| 5397 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5398 | if (!a->a_lnotab) |
| 5399 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5400 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5401 | PyErr_NoMemory(); |
| 5402 | return 0; |
| 5403 | } |
| 5404 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 5405 | sizeof(basicblock *) * nblocks); |
| 5406 | if (!a->a_postorder) { |
| 5407 | PyErr_NoMemory(); |
| 5408 | return 0; |
| 5409 | } |
| 5410 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | static void |
| 5414 | assemble_free(struct assembler *a) |
| 5415 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5416 | Py_XDECREF(a->a_bytecode); |
| 5417 | Py_XDECREF(a->a_lnotab); |
| 5418 | if (a->a_postorder) |
| 5419 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5422 | static int |
| 5423 | blocksize(basicblock *b) |
| 5424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5425 | int i; |
| 5426 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5428 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5429 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5433 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5434 | the instruction's bytecode offset and line number. See |
| 5435 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5436 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5437 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5438 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5439 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5440 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5441 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5443 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5444 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5446 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5447 | assert(d_bytecode >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5449 | if(d_bytecode == 0 && d_lineno == 0) |
| 5450 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5452 | if (d_bytecode > 255) { |
| 5453 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5454 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5455 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5456 | if (nbytes >= len) { |
| 5457 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5458 | len = nbytes; |
| 5459 | else if (len <= INT_MAX / 2) |
| 5460 | len *= 2; |
| 5461 | else { |
| 5462 | PyErr_NoMemory(); |
| 5463 | return 0; |
| 5464 | } |
| 5465 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5466 | return 0; |
| 5467 | } |
| 5468 | lnotab = (unsigned char *) |
| 5469 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5470 | for (j = 0; j < ncodes; j++) { |
| 5471 | *lnotab++ = 255; |
| 5472 | *lnotab++ = 0; |
| 5473 | } |
| 5474 | d_bytecode -= ncodes * 255; |
| 5475 | a->a_lnotab_off += ncodes * 2; |
| 5476 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5477 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5478 | |
| 5479 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5480 | int j, nbytes, ncodes, k; |
| 5481 | if (d_lineno < 0) { |
| 5482 | k = -128; |
| 5483 | /* use division on positive numbers */ |
| 5484 | ncodes = (-d_lineno) / 128; |
| 5485 | } |
| 5486 | else { |
| 5487 | k = 127; |
| 5488 | ncodes = d_lineno / 127; |
| 5489 | } |
| 5490 | d_lineno -= ncodes * k; |
| 5491 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5492 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5493 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5494 | if (nbytes >= len) { |
| 5495 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5496 | len = nbytes; |
| 5497 | else if (len <= INT_MAX / 2) |
| 5498 | len *= 2; |
| 5499 | else { |
| 5500 | PyErr_NoMemory(); |
| 5501 | return 0; |
| 5502 | } |
| 5503 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5504 | return 0; |
| 5505 | } |
| 5506 | lnotab = (unsigned char *) |
| 5507 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5508 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5509 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5510 | d_bytecode = 0; |
| 5511 | for (j = 1; j < ncodes; j++) { |
| 5512 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5513 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5514 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5515 | a->a_lnotab_off += ncodes * 2; |
| 5516 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5517 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5519 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5520 | if (a->a_lnotab_off + 2 >= len) { |
| 5521 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5522 | return 0; |
| 5523 | } |
| 5524 | lnotab = (unsigned char *) |
| 5525 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5527 | a->a_lnotab_off += 2; |
| 5528 | if (d_bytecode) { |
| 5529 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5530 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5531 | } |
| 5532 | else { /* First line of a block; def stmt, etc. */ |
| 5533 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5534 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5535 | } |
| 5536 | a->a_lineno = i->i_lineno; |
| 5537 | a->a_lineno_off = a->a_offset; |
| 5538 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5541 | /* assemble_emit() |
| 5542 | Extend the bytecode with a new instruction. |
| 5543 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5544 | */ |
| 5545 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5546 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5547 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5548 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5549 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5551 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5552 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5553 | arg = i->i_oparg; |
| 5554 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5556 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5557 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | if (len > PY_SSIZE_T_MAX / 2) |
| 5559 | return 0; |
| 5560 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5561 | return 0; |
| 5562 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5563 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5565 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5567 | } |
| 5568 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5569 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5570 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5571 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5572 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5573 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5574 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5575 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5576 | /* Compute the size of each block and fixup jump args. |
| 5577 | Replace block pointer with position in bytecode. */ |
| 5578 | do { |
| 5579 | totsize = 0; |
| 5580 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5581 | b = a->a_postorder[i]; |
| 5582 | bsize = blocksize(b); |
| 5583 | b->b_offset = totsize; |
| 5584 | totsize += bsize; |
| 5585 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5586 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5587 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5588 | bsize = b->b_offset; |
| 5589 | for (i = 0; i < b->b_iused; i++) { |
| 5590 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5591 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5592 | /* Relative jumps are computed relative to |
| 5593 | the instruction pointer after fetching |
| 5594 | the jump instruction. |
| 5595 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5596 | bsize += isize; |
| 5597 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5598 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5599 | if (instr->i_jrel) { |
| 5600 | instr->i_oparg -= bsize; |
| 5601 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5602 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5603 | if (instrsize(instr->i_oparg) != isize) { |
| 5604 | extended_arg_recompile = 1; |
| 5605 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5606 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5607 | } |
| 5608 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5610 | /* XXX: This is an awful hack that could hurt performance, but |
| 5611 | on the bright side it should work until we come up |
| 5612 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5614 | The issue is that in the first loop blocksize() is called |
| 5615 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5616 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5617 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5619 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5620 | The only EXTENDED_ARGs that could be popping up are |
| 5621 | ones in jump instructions. So this should converge |
| 5622 | fairly quickly. |
| 5623 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5624 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5627 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5628 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5631 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5633 | tuple = PyTuple_New(size); |
| 5634 | if (tuple == NULL) |
| 5635 | return NULL; |
| 5636 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5637 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5638 | Py_INCREF(k); |
| 5639 | assert((i - offset) < size); |
| 5640 | assert((i - offset) >= 0); |
| 5641 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5642 | } |
| 5643 | return tuple; |
| 5644 | } |
| 5645 | |
| 5646 | static PyObject * |
| 5647 | consts_dict_keys_inorder(PyObject *dict) |
| 5648 | { |
| 5649 | PyObject *consts, *k, *v; |
| 5650 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5651 | |
| 5652 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5653 | if (consts == NULL) |
| 5654 | return NULL; |
| 5655 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5656 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5657 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5658 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5659 | * the object we want is always second. */ |
| 5660 | if (PyTuple_CheckExact(k)) { |
| 5661 | k = PyTuple_GET_ITEM(k, 1); |
| 5662 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5664 | assert(i < size); |
| 5665 | assert(i >= 0); |
| 5666 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5667 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5668 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5671 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5672 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5673 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5675 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5676 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5677 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | if (ste->ste_nested) |
| 5679 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5680 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5681 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5682 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5683 | flags |= CO_COROUTINE; |
| 5684 | if (ste->ste_generator && ste->ste_coroutine) |
| 5685 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5686 | if (ste->ste_varargs) |
| 5687 | flags |= CO_VARARGS; |
| 5688 | if (ste->ste_varkeywords) |
| 5689 | flags |= CO_VARKEYWORDS; |
| 5690 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5693 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5694 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5695 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5698 | // Merge *tuple* with constant cache. |
| 5699 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5700 | static int |
| 5701 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5702 | { |
| 5703 | assert(PyTuple_CheckExact(*tuple)); |
| 5704 | |
| 5705 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5706 | if (key == NULL) { |
| 5707 | return 0; |
| 5708 | } |
| 5709 | |
| 5710 | // t is borrowed reference |
| 5711 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5712 | Py_DECREF(key); |
| 5713 | if (t == NULL) { |
| 5714 | return 0; |
| 5715 | } |
| 5716 | if (t == key) { // tuple is new constant. |
| 5717 | return 1; |
| 5718 | } |
| 5719 | |
| 5720 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5721 | Py_INCREF(u); |
| 5722 | Py_DECREF(*tuple); |
| 5723 | *tuple = u; |
| 5724 | return 1; |
| 5725 | } |
| 5726 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5727 | static PyCodeObject * |
| 5728 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5730 | PyObject *tmp; |
| 5731 | PyCodeObject *co = NULL; |
| 5732 | PyObject *consts = NULL; |
| 5733 | PyObject *names = NULL; |
| 5734 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5735 | PyObject *name = NULL; |
| 5736 | PyObject *freevars = NULL; |
| 5737 | PyObject *cellvars = NULL; |
| 5738 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5739 | Py_ssize_t nlocals; |
| 5740 | int nlocals_int; |
| 5741 | int flags; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5742 | int argcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5743 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5744 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5745 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5746 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5747 | if (!consts || !names || !varnames) |
| 5748 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5750 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5751 | if (!cellvars) |
| 5752 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5753 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5754 | if (!freevars) |
| 5755 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5756 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5757 | if (!merge_const_tuple(c, &names) || |
| 5758 | !merge_const_tuple(c, &varnames) || |
| 5759 | !merge_const_tuple(c, &cellvars) || |
| 5760 | !merge_const_tuple(c, &freevars)) |
| 5761 | { |
| 5762 | goto error; |
| 5763 | } |
| 5764 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5765 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5766 | assert(nlocals < INT_MAX); |
| 5767 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5769 | flags = compute_code_flags(c); |
| 5770 | if (flags < 0) |
| 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 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5774 | if (!bytecode) |
| 5775 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5777 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5778 | if (!tmp) |
| 5779 | goto error; |
| 5780 | Py_DECREF(consts); |
| 5781 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5782 | if (!merge_const_tuple(c, &consts)) { |
| 5783 | goto error; |
| 5784 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5785 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5786 | argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
| 5787 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5788 | maxdepth = stackdepth(c); |
| 5789 | if (maxdepth < 0) { |
| 5790 | goto error; |
| 5791 | } |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5792 | co = PyCode_New(argcount, kwonlyargcount, |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5793 | nlocals_int, maxdepth, flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5794 | bytecode, consts, names, varnames, |
| 5795 | freevars, cellvars, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5796 | c->c_filename, c->u->u_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5797 | c->u->u_firstlineno, |
| 5798 | a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5799 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5800 | Py_XDECREF(consts); |
| 5801 | Py_XDECREF(names); |
| 5802 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5803 | Py_XDECREF(name); |
| 5804 | Py_XDECREF(freevars); |
| 5805 | Py_XDECREF(cellvars); |
| 5806 | Py_XDECREF(bytecode); |
| 5807 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5808 | } |
| 5809 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5810 | |
| 5811 | /* For debugging purposes only */ |
| 5812 | #if 0 |
| 5813 | static void |
| 5814 | dump_instr(const struct instr *i) |
| 5815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5816 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5817 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5818 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5821 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5823 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5824 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5825 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | static void |
| 5829 | dump_basicblock(const basicblock *b) |
| 5830 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5831 | const char *seen = b->b_seen ? "seen " : ""; |
| 5832 | const char *b_return = b->b_return ? "return " : ""; |
| 5833 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5834 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5835 | if (b->b_instr) { |
| 5836 | int i; |
| 5837 | for (i = 0; i < b->b_iused; i++) { |
| 5838 | fprintf(stderr, " [%02d] ", i); |
| 5839 | dump_instr(b->b_instr + i); |
| 5840 | } |
| 5841 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5842 | } |
| 5843 | #endif |
| 5844 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5845 | static PyCodeObject * |
| 5846 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5848 | basicblock *b, *entryblock; |
| 5849 | struct assembler a; |
| 5850 | int i, j, nblocks; |
| 5851 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5852 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5853 | /* Make sure every block that falls off the end returns None. |
| 5854 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5855 | block ends with a jump or return b_next shouldn't set. |
| 5856 | */ |
| 5857 | if (!c->u->u_curblock->b_return) { |
| 5858 | NEXT_BLOCK(c); |
| 5859 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5860 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5861 | ADDOP(c, RETURN_VALUE); |
| 5862 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5864 | nblocks = 0; |
| 5865 | entryblock = NULL; |
| 5866 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5867 | nblocks++; |
| 5868 | entryblock = b; |
| 5869 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5871 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5872 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5873 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5874 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 5875 | else |
| 5876 | c->u->u_firstlineno = 1; |
| 5877 | } |
| 5878 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5879 | goto error; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5880 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5882 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5883 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5885 | /* Emit code in reverse postorder from dfs. */ |
| 5886 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 5887 | b = a.a_postorder[i]; |
| 5888 | for (j = 0; j < b->b_iused; j++) |
| 5889 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5890 | goto error; |
| 5891 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5893 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5894 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5895 | 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] | 5896 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5898 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5899 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5900 | assemble_free(&a); |
| 5901 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5902 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5903 | |
| 5904 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5905 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5906 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 5907 | PyArena *arena) |
| 5908 | { |
| 5909 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 5910 | } |