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