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" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 27 | #include "node.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 28 | #include "ast.h" |
| 29 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 30 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 32 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 33 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 34 | #define DEFAULT_BLOCK_SIZE 16 |
| 35 | #define DEFAULT_BLOCKS 8 |
| 36 | #define DEFAULT_CODE_SIZE 128 |
| 37 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 38 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 39 | #define COMP_GENEXP 0 |
| 40 | #define COMP_LISTCOMP 1 |
| 41 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 42 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 43 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 44 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | unsigned i_jabs : 1; |
| 46 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | unsigned char i_opcode; |
| 48 | int i_oparg; |
| 49 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 50 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 53 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 55 | reverse order that the block are allocated. b_list points to the next |
| 56 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | struct basicblock_ *b_list; |
| 58 | /* number of instructions used */ |
| 59 | int b_iused; |
| 60 | /* length of instruction array (b_instr) */ |
| 61 | int b_ialloc; |
| 62 | /* pointer to an array of instructions, initially NULL */ |
| 63 | struct instr *b_instr; |
| 64 | /* If b_next is non-NULL, it is a pointer to the next |
| 65 | block reached by normal control flow. */ |
| 66 | struct basicblock_ *b_next; |
| 67 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 68 | unsigned b_seen : 1; |
| 69 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 70 | unsigned b_return : 1; |
| 71 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 72 | int b_startdepth; |
| 73 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 74 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 75 | } basicblock; |
| 76 | |
| 77 | /* fblockinfo tracks the current frame block. |
| 78 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 79 | A frame block is used to handle loops, try/except, and try/finally. |
| 80 | It's called a frame block to distinguish it from a basic block in the |
| 81 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 84 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END, |
| 85 | WITH, ASYNC_WITH, HANDLER_CLEANUP }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 86 | |
| 87 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | enum fblocktype fb_type; |
| 89 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 90 | /* (optional) type-specific exit or cleanup block */ |
| 91 | basicblock *fb_exit; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 94 | enum { |
| 95 | COMPILER_SCOPE_MODULE, |
| 96 | COMPILER_SCOPE_CLASS, |
| 97 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 98 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 99 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 100 | COMPILER_SCOPE_COMPREHENSION, |
| 101 | }; |
| 102 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 103 | /* The following items change on entry and exit of code blocks. |
| 104 | They must be saved and restored when returning to a block. |
| 105 | */ |
| 106 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 110 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 111 | int u_scope_type; |
| 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | /* The following fields are dicts that map objects to |
| 114 | the index of them in co_XXX. The index is used as |
| 115 | the argument for opcodes that refer to those collections. |
| 116 | */ |
| 117 | PyObject *u_consts; /* all constants */ |
| 118 | PyObject *u_names; /* all names */ |
| 119 | PyObject *u_varnames; /* local variables */ |
| 120 | PyObject *u_cellvars; /* cell variables */ |
| 121 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 124 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 125 | Py_ssize_t u_argcount; /* number of arguments for block */ |
| 126 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 127 | /* Pointer to the most recently allocated block. By following b_list |
| 128 | members, you can reach all early allocated blocks. */ |
| 129 | basicblock *u_blocks; |
| 130 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | int u_nfblocks; |
| 133 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 134 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 135 | int u_firstlineno; /* the first lineno of the block */ |
| 136 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 137 | int u_col_offset; /* the offset of the current stmt */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | int u_lineno_set; /* boolean to indicate whether instr |
| 139 | has been generated with current lineno */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 143 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 144 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 145 | 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] | 146 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 147 | |
| 148 | Note that we don't track recursion levels during compilation - the |
| 149 | task of detecting and rejecting excessive levels of nesting is |
| 150 | handled by the symbol analysis pass. |
| 151 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 152 | */ |
| 153 | |
| 154 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 155 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 156 | struct symtable *c_st; |
| 157 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 158 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 159 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 160 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | int c_interactive; /* true if in interactive mode */ |
| 162 | int c_nestlevel; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | struct compiler_unit *u; /* compiler state for current block */ |
| 165 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 166 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 169 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 170 | static void compiler_free(struct compiler *); |
| 171 | static basicblock *compiler_new_block(struct compiler *); |
| 172 | static int compiler_next_instr(struct compiler *, basicblock *); |
| 173 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 174 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 175 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 176 | static int compiler_error(struct compiler *, const char *); |
| 177 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 178 | |
| 179 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 180 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 181 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 182 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 183 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 184 | static int compiler_annassign(struct compiler *, stmt_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | static int compiler_visit_slice(struct compiler *, slice_ty, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | expr_context_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 188 | static int inplace_binop(struct compiler *, operator_ty); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 189 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 191 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 192 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 193 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 194 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 196 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 197 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 198 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 199 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 200 | static int compiler_sync_comprehension_generator( |
| 201 | struct compiler *c, |
| 202 | asdl_seq *generators, int gen_index, |
| 203 | expr_ty elt, expr_ty val, int type); |
| 204 | |
| 205 | static int compiler_async_comprehension_generator( |
| 206 | struct compiler *c, |
| 207 | asdl_seq *generators, int gen_index, |
| 208 | expr_ty elt, expr_ty val, int type); |
| 209 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 210 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 211 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 212 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 213 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 214 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 215 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 216 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 218 | /* Name mangling: __private becomes _classname__private. |
| 219 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 220 | PyObject *result; |
| 221 | size_t nlen, plen, ipriv; |
| 222 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 224 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 225 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | Py_INCREF(ident); |
| 227 | return ident; |
| 228 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 229 | nlen = PyUnicode_GET_LENGTH(ident); |
| 230 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 231 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | The only time a name with a dot can occur is when |
| 234 | we are compiling an import statement that has a |
| 235 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | TODO(jhylton): Decide whether we want to support |
| 238 | mangling of the module name, e.g. __M.X. |
| 239 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 240 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 241 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 242 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | Py_INCREF(ident); |
| 244 | return ident; /* Don't mangle __whatever__ */ |
| 245 | } |
| 246 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 247 | ipriv = 0; |
| 248 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 249 | ipriv++; |
| 250 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | Py_INCREF(ident); |
| 252 | return ident; /* Don't mangle if class is just underscores */ |
| 253 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 254 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 255 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 256 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 257 | PyErr_SetString(PyExc_OverflowError, |
| 258 | "private identifier too large to be mangled"); |
| 259 | return NULL; |
| 260 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 261 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 262 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 263 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 264 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 265 | |
| 266 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 267 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 268 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 269 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 270 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 271 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 272 | Py_DECREF(result); |
| 273 | return NULL; |
| 274 | } |
| 275 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 276 | Py_DECREF(result); |
| 277 | return NULL; |
| 278 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 279 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 280 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 283 | static int |
| 284 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | memset(c, 0, sizeof(struct compiler)); |
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 | c->c_stack = PyList_New(0); |
| 289 | if (!c->c_stack) |
| 290 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 291 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 296 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 297 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | struct compiler c; |
| 300 | PyCodeObject *co = NULL; |
| 301 | PyCompilerFlags local_flags; |
| 302 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | if (!__doc__) { |
| 305 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 306 | if (!__doc__) |
| 307 | return NULL; |
| 308 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 309 | if (!__annotations__) { |
| 310 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 311 | if (!__annotations__) |
| 312 | return NULL; |
| 313 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | if (!compiler_init(&c)) |
| 315 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 316 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | c.c_filename = filename; |
| 318 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 319 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 320 | if (c.c_future == NULL) |
| 321 | goto finally; |
| 322 | if (!flags) { |
| 323 | local_flags.cf_flags = 0; |
| 324 | flags = &local_flags; |
| 325 | } |
| 326 | merged = c.c_future->ff_features | flags->cf_flags; |
| 327 | c.c_future->ff_features = merged; |
| 328 | flags->cf_flags = merged; |
| 329 | c.c_flags = flags; |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 330 | c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 332 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 333 | if (!_PyAST_Optimize(mod, arena, c.c_optimize)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 334 | goto finally; |
| 335 | } |
| 336 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 337 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | if (c.c_st == NULL) { |
| 339 | if (!PyErr_Occurred()) |
| 340 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 341 | goto finally; |
| 342 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 345 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 346 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | compiler_free(&c); |
| 348 | assert(co || PyErr_Occurred()); |
| 349 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 353 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 354 | int optimize, PyArena *arena) |
| 355 | { |
| 356 | PyObject *filename; |
| 357 | PyCodeObject *co; |
| 358 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 359 | if (filename == NULL) |
| 360 | return NULL; |
| 361 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 362 | Py_DECREF(filename); |
| 363 | return co; |
| 364 | |
| 365 | } |
| 366 | |
| 367 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 368 | PyNode_Compile(struct _node *n, const char *filename) |
| 369 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | PyCodeObject *co = NULL; |
| 371 | mod_ty mod; |
| 372 | PyArena *arena = PyArena_New(); |
| 373 | if (!arena) |
| 374 | return NULL; |
| 375 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 376 | if (mod) |
| 377 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 378 | PyArena_Free(arena); |
| 379 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 382 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 383 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 385 | if (c->c_st) |
| 386 | PySymtable_Free(c->c_st); |
| 387 | if (c->c_future) |
| 388 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 389 | Py_XDECREF(c->c_filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 390 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 393 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 394 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | Py_ssize_t i, n; |
| 397 | PyObject *v, *k; |
| 398 | PyObject *dict = PyDict_New(); |
| 399 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 400 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 401 | n = PyList_Size(list); |
| 402 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 403 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | if (!v) { |
| 405 | Py_DECREF(dict); |
| 406 | return NULL; |
| 407 | } |
| 408 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 409 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | Py_DECREF(v); |
| 411 | Py_DECREF(dict); |
| 412 | return NULL; |
| 413 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | Py_DECREF(v); |
| 415 | } |
| 416 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* Return new dict containing names from src that match scope(s). |
| 420 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 421 | 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] | 422 | 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] | 423 | values are integers, starting at offset and increasing by one for |
| 424 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 425 | */ |
| 426 | |
| 427 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 428 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 429 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 430 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 432 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | assert(offset >= 0); |
| 435 | if (dest == NULL) |
| 436 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 437 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 438 | /* Sort the keys so that we have a deterministic order on the indexes |
| 439 | saved in the returned dictionary. These indexes are used as indexes |
| 440 | into the free and cell var storage. Therefore if they aren't |
| 441 | deterministic, then the generated bytecode is not deterministic. |
| 442 | */ |
| 443 | sorted_keys = PyDict_Keys(src); |
| 444 | if (sorted_keys == NULL) |
| 445 | return NULL; |
| 446 | if (PyList_Sort(sorted_keys) != 0) { |
| 447 | Py_DECREF(sorted_keys); |
| 448 | return NULL; |
| 449 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 450 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 451 | |
| 452 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | /* XXX this should probably be a macro in symtable.h */ |
| 454 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 455 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 456 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 457 | assert(PyLong_Check(v)); |
| 458 | vi = PyLong_AS_LONG(v); |
| 459 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 462 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 464 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 465 | Py_DECREF(dest); |
| 466 | return NULL; |
| 467 | } |
| 468 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 469 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 470 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | Py_DECREF(item); |
| 472 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | return NULL; |
| 474 | } |
| 475 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 478 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 482 | static void |
| 483 | compiler_unit_check(struct compiler_unit *u) |
| 484 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 485 | basicblock *block; |
| 486 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 487 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 488 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 489 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | if (block->b_instr != NULL) { |
| 491 | assert(block->b_ialloc > 0); |
| 492 | assert(block->b_iused > 0); |
| 493 | assert(block->b_ialloc >= block->b_iused); |
| 494 | } |
| 495 | else { |
| 496 | assert (block->b_iused == 0); |
| 497 | assert (block->b_ialloc == 0); |
| 498 | } |
| 499 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | static void |
| 503 | compiler_unit_free(struct compiler_unit *u) |
| 504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | compiler_unit_check(u); |
| 508 | b = u->u_blocks; |
| 509 | while (b != NULL) { |
| 510 | if (b->b_instr) |
| 511 | PyObject_Free((void *)b->b_instr); |
| 512 | next = b->b_list; |
| 513 | PyObject_Free((void *)b); |
| 514 | b = next; |
| 515 | } |
| 516 | Py_CLEAR(u->u_ste); |
| 517 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 518 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | Py_CLEAR(u->u_consts); |
| 520 | Py_CLEAR(u->u_names); |
| 521 | Py_CLEAR(u->u_varnames); |
| 522 | Py_CLEAR(u->u_freevars); |
| 523 | Py_CLEAR(u->u_cellvars); |
| 524 | Py_CLEAR(u->u_private); |
| 525 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 529 | compiler_enter_scope(struct compiler *c, identifier name, |
| 530 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 533 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 534 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 535 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 536 | struct compiler_unit)); |
| 537 | if (!u) { |
| 538 | PyErr_NoMemory(); |
| 539 | return 0; |
| 540 | } |
| 541 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 542 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 543 | u->u_argcount = 0; |
| 544 | u->u_kwonlyargcount = 0; |
| 545 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 546 | if (!u->u_ste) { |
| 547 | compiler_unit_free(u); |
| 548 | return 0; |
| 549 | } |
| 550 | Py_INCREF(name); |
| 551 | u->u_name = name; |
| 552 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 553 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 554 | if (!u->u_varnames || !u->u_cellvars) { |
| 555 | compiler_unit_free(u); |
| 556 | return 0; |
| 557 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 558 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 559 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 560 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 561 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 562 | int res; |
| 563 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 564 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 565 | name = _PyUnicode_FromId(&PyId___class__); |
| 566 | if (!name) { |
| 567 | compiler_unit_free(u); |
| 568 | return 0; |
| 569 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 570 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 571 | if (res < 0) { |
| 572 | compiler_unit_free(u); |
| 573 | return 0; |
| 574 | } |
| 575 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 577 | 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] | 578 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | if (!u->u_freevars) { |
| 580 | compiler_unit_free(u); |
| 581 | return 0; |
| 582 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 584 | u->u_blocks = NULL; |
| 585 | u->u_nfblocks = 0; |
| 586 | u->u_firstlineno = lineno; |
| 587 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 588 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | u->u_lineno_set = 0; |
| 590 | u->u_consts = PyDict_New(); |
| 591 | if (!u->u_consts) { |
| 592 | compiler_unit_free(u); |
| 593 | return 0; |
| 594 | } |
| 595 | u->u_names = PyDict_New(); |
| 596 | if (!u->u_names) { |
| 597 | compiler_unit_free(u); |
| 598 | return 0; |
| 599 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 601 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | /* Push the old compiler_unit on the stack. */ |
| 604 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 605 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 607 | Py_XDECREF(capsule); |
| 608 | compiler_unit_free(u); |
| 609 | return 0; |
| 610 | } |
| 611 | Py_DECREF(capsule); |
| 612 | u->u_private = c->u->u_private; |
| 613 | Py_XINCREF(u->u_private); |
| 614 | } |
| 615 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 616 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 617 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 618 | |
| 619 | block = compiler_new_block(c); |
| 620 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 621 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 622 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 623 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 624 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 625 | if (!compiler_set_qualname(c)) |
| 626 | return 0; |
| 627 | } |
| 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 632 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 633 | compiler_exit_scope(struct compiler *c) |
| 634 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 635 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | c->c_nestlevel--; |
| 639 | compiler_unit_free(c->u); |
| 640 | /* Restore c->u to the parent unit. */ |
| 641 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 642 | if (n >= 0) { |
| 643 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 644 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | assert(c->u); |
| 646 | /* we are deleting from a list so this really shouldn't fail */ |
| 647 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 648 | Py_FatalError("compiler_exit_scope()"); |
| 649 | compiler_unit_check(c->u); |
| 650 | } |
| 651 | else |
| 652 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 653 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 656 | static int |
| 657 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 658 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 659 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 660 | _Py_static_string(dot_locals, ".<locals>"); |
| 661 | Py_ssize_t stack_size; |
| 662 | struct compiler_unit *u = c->u; |
| 663 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 664 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 665 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 666 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 667 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 668 | if (stack_size > 1) { |
| 669 | int scope, force_global = 0; |
| 670 | struct compiler_unit *parent; |
| 671 | PyObject *mangled, *capsule; |
| 672 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 673 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 674 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 675 | assert(parent); |
| 676 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 677 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 678 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 679 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 680 | assert(u->u_name); |
| 681 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 682 | if (!mangled) |
| 683 | return 0; |
| 684 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 685 | Py_DECREF(mangled); |
| 686 | assert(scope != GLOBAL_IMPLICIT); |
| 687 | if (scope == GLOBAL_EXPLICIT) |
| 688 | force_global = 1; |
| 689 | } |
| 690 | |
| 691 | if (!force_global) { |
| 692 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 693 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 694 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 695 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 696 | if (dot_locals_str == NULL) |
| 697 | return 0; |
| 698 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 699 | if (base == NULL) |
| 700 | return 0; |
| 701 | } |
| 702 | else { |
| 703 | Py_INCREF(parent->u_qualname); |
| 704 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 705 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 706 | } |
| 707 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 708 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 709 | if (base != NULL) { |
| 710 | dot_str = _PyUnicode_FromId(&dot); |
| 711 | if (dot_str == NULL) { |
| 712 | Py_DECREF(base); |
| 713 | return 0; |
| 714 | } |
| 715 | name = PyUnicode_Concat(base, dot_str); |
| 716 | Py_DECREF(base); |
| 717 | if (name == NULL) |
| 718 | return 0; |
| 719 | PyUnicode_Append(&name, u->u_name); |
| 720 | if (name == NULL) |
| 721 | return 0; |
| 722 | } |
| 723 | else { |
| 724 | Py_INCREF(u->u_name); |
| 725 | name = u->u_name; |
| 726 | } |
| 727 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 728 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 729 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 732 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 733 | /* Allocate a new block and return a pointer to it. |
| 734 | Returns NULL on error. |
| 735 | */ |
| 736 | |
| 737 | static basicblock * |
| 738 | compiler_new_block(struct compiler *c) |
| 739 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | basicblock *b; |
| 741 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 743 | u = c->u; |
| 744 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 745 | if (b == NULL) { |
| 746 | PyErr_NoMemory(); |
| 747 | return NULL; |
| 748 | } |
| 749 | memset((void *)b, 0, sizeof(basicblock)); |
| 750 | /* Extend the singly linked list of blocks with new block. */ |
| 751 | b->b_list = u->u_blocks; |
| 752 | u->u_blocks = b; |
| 753 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 756 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 757 | compiler_next_block(struct compiler *c) |
| 758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | basicblock *block = compiler_new_block(c); |
| 760 | if (block == NULL) |
| 761 | return NULL; |
| 762 | c->u->u_curblock->b_next = block; |
| 763 | c->u->u_curblock = block; |
| 764 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static basicblock * |
| 768 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 769 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 770 | assert(block != NULL); |
| 771 | c->u->u_curblock->b_next = block; |
| 772 | c->u->u_curblock = block; |
| 773 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | /* Returns the offset of the next instruction in the current block's |
| 777 | b_instr array. Resizes the b_instr as necessary. |
| 778 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 779 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 780 | |
| 781 | static int |
| 782 | compiler_next_instr(struct compiler *c, basicblock *b) |
| 783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | assert(b != NULL); |
| 785 | if (b->b_instr == NULL) { |
| 786 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 787 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 788 | if (b->b_instr == NULL) { |
| 789 | PyErr_NoMemory(); |
| 790 | return -1; |
| 791 | } |
| 792 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 793 | memset((char *)b->b_instr, 0, |
| 794 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 795 | } |
| 796 | else if (b->b_iused == b->b_ialloc) { |
| 797 | struct instr *tmp; |
| 798 | size_t oldsize, newsize; |
| 799 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 800 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 801 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 802 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | PyErr_NoMemory(); |
| 804 | return -1; |
| 805 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 806 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 807 | if (newsize == 0) { |
| 808 | PyErr_NoMemory(); |
| 809 | return -1; |
| 810 | } |
| 811 | b->b_ialloc <<= 1; |
| 812 | tmp = (struct instr *)PyObject_Realloc( |
| 813 | (void *)b->b_instr, newsize); |
| 814 | if (tmp == NULL) { |
| 815 | PyErr_NoMemory(); |
| 816 | return -1; |
| 817 | } |
| 818 | b->b_instr = tmp; |
| 819 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 820 | } |
| 821 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 824 | /* Set the i_lineno member of the instruction at offset off if the |
| 825 | line number for the current expression/statement has not |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 826 | already been set. If it has been set, the call has no effect. |
| 827 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 828 | The line number is reset in the following cases: |
| 829 | - when entering a new scope |
| 830 | - on each statement |
| 831 | - on each expression that start a new line |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 832 | - before the "except" and "finally" clauses |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 833 | - before the "for" and "while" expressions |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 834 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 835 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 836 | static void |
| 837 | compiler_set_lineno(struct compiler *c, int off) |
| 838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 839 | basicblock *b; |
| 840 | if (c->u->u_lineno_set) |
| 841 | return; |
| 842 | c->u->u_lineno_set = 1; |
| 843 | b = c->u->u_curblock; |
| 844 | b->b_instr[off].i_lineno = c->u->u_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 847 | /* Return the stack effect of opcode with argument oparg. |
| 848 | |
| 849 | Some opcodes have different stack effect when jump to the target and |
| 850 | when not jump. The 'jump' parameter specifies the case: |
| 851 | |
| 852 | * 0 -- when not jump |
| 853 | * 1 -- when jump |
| 854 | * -1 -- maximal |
| 855 | */ |
| 856 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 857 | WITH_CLEANUP_FINISH deterministic. */ |
| 858 | static int |
| 859 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 860 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 861 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 862 | case NOP: |
| 863 | case EXTENDED_ARG: |
| 864 | return 0; |
| 865 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 866 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | case POP_TOP: |
| 868 | return -1; |
| 869 | case ROT_TWO: |
| 870 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 871 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | return 0; |
| 873 | case DUP_TOP: |
| 874 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 875 | case DUP_TOP_TWO: |
| 876 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 877 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 878 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 879 | case UNARY_POSITIVE: |
| 880 | case UNARY_NEGATIVE: |
| 881 | case UNARY_NOT: |
| 882 | case UNARY_INVERT: |
| 883 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | case SET_ADD: |
| 886 | case LIST_APPEND: |
| 887 | return -1; |
| 888 | case MAP_ADD: |
| 889 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 890 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 891 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | case BINARY_POWER: |
| 893 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 894 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | case BINARY_MODULO: |
| 896 | case BINARY_ADD: |
| 897 | case BINARY_SUBTRACT: |
| 898 | case BINARY_SUBSCR: |
| 899 | case BINARY_FLOOR_DIVIDE: |
| 900 | case BINARY_TRUE_DIVIDE: |
| 901 | return -1; |
| 902 | case INPLACE_FLOOR_DIVIDE: |
| 903 | case INPLACE_TRUE_DIVIDE: |
| 904 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | case INPLACE_ADD: |
| 907 | case INPLACE_SUBTRACT: |
| 908 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 909 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | case INPLACE_MODULO: |
| 911 | return -1; |
| 912 | case STORE_SUBSCR: |
| 913 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | case DELETE_SUBSCR: |
| 915 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | case BINARY_LSHIFT: |
| 918 | case BINARY_RSHIFT: |
| 919 | case BINARY_AND: |
| 920 | case BINARY_XOR: |
| 921 | case BINARY_OR: |
| 922 | return -1; |
| 923 | case INPLACE_POWER: |
| 924 | return -1; |
| 925 | case GET_ITER: |
| 926 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 927 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 928 | case PRINT_EXPR: |
| 929 | return -1; |
| 930 | case LOAD_BUILD_CLASS: |
| 931 | return 1; |
| 932 | case INPLACE_LSHIFT: |
| 933 | case INPLACE_RSHIFT: |
| 934 | case INPLACE_AND: |
| 935 | case INPLACE_XOR: |
| 936 | case INPLACE_OR: |
| 937 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 939 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 940 | /* 1 in the normal flow. |
| 941 | * Restore the stack position and push 6 values before jumping to |
| 942 | * the handler if an exception be raised. */ |
| 943 | return jump ? 6 : 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 944 | case WITH_CLEANUP_START: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 945 | return 2; /* or 1, depending on TOS */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 946 | case WITH_CLEANUP_FINISH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 947 | /* Pop a variable number of values pushed by WITH_CLEANUP_START |
| 948 | * + __exit__ or __aexit__. */ |
| 949 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | case RETURN_VALUE: |
| 951 | return -1; |
| 952 | case IMPORT_STAR: |
| 953 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 954 | case SETUP_ANNOTATIONS: |
| 955 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | case YIELD_VALUE: |
| 957 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 958 | case YIELD_FROM: |
| 959 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 960 | case POP_BLOCK: |
| 961 | return 0; |
| 962 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 963 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 964 | case END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 965 | case POP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 966 | /* Pop 6 values when an exception was raised. */ |
| 967 | return -6; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | case STORE_NAME: |
| 970 | return -1; |
| 971 | case DELETE_NAME: |
| 972 | return 0; |
| 973 | case UNPACK_SEQUENCE: |
| 974 | return oparg-1; |
| 975 | case UNPACK_EX: |
| 976 | return (oparg&0xFF) + (oparg>>8); |
| 977 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 978 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 979 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | case STORE_ATTR: |
| 982 | return -2; |
| 983 | case DELETE_ATTR: |
| 984 | return -1; |
| 985 | case STORE_GLOBAL: |
| 986 | return -1; |
| 987 | case DELETE_GLOBAL: |
| 988 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 989 | case LOAD_CONST: |
| 990 | return 1; |
| 991 | case LOAD_NAME: |
| 992 | return 1; |
| 993 | case BUILD_TUPLE: |
| 994 | case BUILD_LIST: |
| 995 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 996 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | return 1-oparg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 998 | case BUILD_LIST_UNPACK: |
| 999 | case BUILD_TUPLE_UNPACK: |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 1000 | case BUILD_TUPLE_UNPACK_WITH_CALL: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1001 | case BUILD_SET_UNPACK: |
| 1002 | case BUILD_MAP_UNPACK: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1003 | case BUILD_MAP_UNPACK_WITH_CALL: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1004 | return 1 - oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1006 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1007 | case BUILD_CONST_KEY_MAP: |
| 1008 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | case LOAD_ATTR: |
| 1010 | return 0; |
| 1011 | case COMPARE_OP: |
| 1012 | return -1; |
| 1013 | case IMPORT_NAME: |
| 1014 | return -1; |
| 1015 | case IMPORT_FROM: |
| 1016 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1017 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1018 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | case JUMP_ABSOLUTE: |
| 1021 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1022 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1023 | case JUMP_IF_TRUE_OR_POP: |
| 1024 | case JUMP_IF_FALSE_OR_POP: |
| 1025 | return jump ? 0 : -1; |
| 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case POP_JUMP_IF_FALSE: |
| 1028 | case POP_JUMP_IF_TRUE: |
| 1029 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | case LOAD_GLOBAL: |
| 1032 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1033 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1034 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1036 | /* 0 in the normal flow. |
| 1037 | * Restore the stack position and push 6 values before jumping to |
| 1038 | * the handler if an exception be raised. */ |
| 1039 | return jump ? 6 : 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1040 | case BEGIN_FINALLY: |
| 1041 | /* Actually pushes 1 value, but count 6 for balancing with |
| 1042 | * END_FINALLY and POP_FINALLY. |
| 1043 | * This is the main reason of using this opcode instead of |
| 1044 | * "LOAD_CONST None". */ |
| 1045 | return 6; |
| 1046 | case CALL_FINALLY: |
| 1047 | return jump ? 1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | case LOAD_FAST: |
| 1050 | return 1; |
| 1051 | case STORE_FAST: |
| 1052 | return -1; |
| 1053 | case DELETE_FAST: |
| 1054 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | case RAISE_VARARGS: |
| 1057 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1058 | |
| 1059 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1060 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1061 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1062 | case CALL_METHOD: |
| 1063 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1064 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1065 | return -oparg-1; |
| 1066 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1067 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1068 | case MAKE_FUNCTION: |
| 1069 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1070 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | case BUILD_SLICE: |
| 1072 | if (oparg == 3) |
| 1073 | return -2; |
| 1074 | else |
| 1075 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1076 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1077 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | case LOAD_CLOSURE: |
| 1079 | return 1; |
| 1080 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1081 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1082 | return 1; |
| 1083 | case STORE_DEREF: |
| 1084 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1085 | case DELETE_DEREF: |
| 1086 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1087 | |
| 1088 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1089 | case GET_AWAITABLE: |
| 1090 | return 0; |
| 1091 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1092 | /* 0 in the normal flow. |
| 1093 | * Restore the stack position to the position before the result |
| 1094 | * of __aenter__ and push 6 values before jumping to the handler |
| 1095 | * if an exception be raised. */ |
| 1096 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1097 | case BEFORE_ASYNC_WITH: |
| 1098 | return 1; |
| 1099 | case GET_AITER: |
| 1100 | return 0; |
| 1101 | case GET_ANEXT: |
| 1102 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1103 | case GET_YIELD_FROM_ITER: |
| 1104 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1105 | case END_ASYNC_FOR: |
| 1106 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1107 | case FORMAT_VALUE: |
| 1108 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1109 | else 1->1. */ |
| 1110 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1111 | case LOAD_METHOD: |
| 1112 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1114 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1115 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1116 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1119 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1120 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1121 | { |
| 1122 | return stack_effect(opcode, oparg, jump); |
| 1123 | } |
| 1124 | |
| 1125 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1126 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1127 | { |
| 1128 | return stack_effect(opcode, oparg, -1); |
| 1129 | } |
| 1130 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1131 | /* Add an opcode with no argument. |
| 1132 | Returns 0 on failure, 1 on success. |
| 1133 | */ |
| 1134 | |
| 1135 | static int |
| 1136 | compiler_addop(struct compiler *c, int opcode) |
| 1137 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1138 | basicblock *b; |
| 1139 | struct instr *i; |
| 1140 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1141 | assert(!HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1143 | if (off < 0) |
| 1144 | return 0; |
| 1145 | b = c->u->u_curblock; |
| 1146 | i = &b->b_instr[off]; |
| 1147 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1148 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1149 | if (opcode == RETURN_VALUE) |
| 1150 | b->b_return = 1; |
| 1151 | compiler_set_lineno(c, off); |
| 1152 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1155 | static Py_ssize_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1156 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) |
| 1157 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1158 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1159 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1160 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1161 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1162 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1163 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1165 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1166 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1167 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1169 | return -1; |
| 1170 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1171 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | Py_DECREF(v); |
| 1173 | return -1; |
| 1174 | } |
| 1175 | Py_DECREF(v); |
| 1176 | } |
| 1177 | else |
| 1178 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1179 | return arg; |
| 1180 | } |
| 1181 | |
| 1182 | static Py_ssize_t |
| 1183 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1184 | { |
| 1185 | PyObject *t; |
| 1186 | Py_ssize_t arg; |
| 1187 | |
| 1188 | t = _PyCode_ConstantKey(o); |
| 1189 | if (t == NULL) |
| 1190 | return -1; |
| 1191 | |
| 1192 | arg = compiler_add_o(c, c->u->u_consts, t); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1193 | Py_DECREF(t); |
| 1194 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1198 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1199 | { |
| 1200 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1201 | if (arg < 0) |
| 1202 | return 0; |
| 1203 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1204 | } |
| 1205 | |
| 1206 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1207 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1208 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1209 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1210 | Py_ssize_t arg = compiler_add_o(c, dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1211 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1212 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1213 | return compiler_addop_i(c, opcode, arg); |
| 1214 | } |
| 1215 | |
| 1216 | static int |
| 1217 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1219 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1220 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1221 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1222 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1223 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1224 | arg = compiler_add_o(c, dict, mangled); |
| 1225 | Py_DECREF(mangled); |
| 1226 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1227 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1228 | return compiler_addop_i(c, opcode, arg); |
| 1229 | } |
| 1230 | |
| 1231 | /* Add an opcode with an integer argument. |
| 1232 | Returns 0 on failure, 1 on success. |
| 1233 | */ |
| 1234 | |
| 1235 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1236 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1237 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1238 | struct instr *i; |
| 1239 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1240 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1241 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1242 | it in the C code (like Python/ceval.c). |
| 1243 | |
| 1244 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1245 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1246 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1247 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1248 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1249 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1252 | if (off < 0) |
| 1253 | return 0; |
| 1254 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1255 | i->i_opcode = opcode; |
| 1256 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1257 | compiler_set_lineno(c, off); |
| 1258 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | static int |
| 1262 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1264 | struct instr *i; |
| 1265 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1266 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1267 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1268 | assert(b != NULL); |
| 1269 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1270 | if (off < 0) |
| 1271 | return 0; |
| 1272 | i = &c->u->u_curblock->b_instr[off]; |
| 1273 | i->i_opcode = opcode; |
| 1274 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | if (absolute) |
| 1276 | i->i_jabs = 1; |
| 1277 | else |
| 1278 | i->i_jrel = 1; |
| 1279 | compiler_set_lineno(c, off); |
| 1280 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1283 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1284 | to the new block. |
| 1285 | |
| 1286 | The returns inside this macro make it impossible to decref objects |
| 1287 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1288 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1289 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1290 | if (compiler_next_block((C)) == NULL) \ |
| 1291 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1295 | if (!compiler_addop((C), (OP))) \ |
| 1296 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1297 | } |
| 1298 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1299 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1300 | if (!compiler_addop((C), (OP))) { \ |
| 1301 | compiler_exit_scope(c); \ |
| 1302 | return 0; \ |
| 1303 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1306 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1307 | if (!compiler_addop_load_const((C), (O))) \ |
| 1308 | return 0; \ |
| 1309 | } |
| 1310 | |
| 1311 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1312 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1313 | PyObject *__new_const = (O); \ |
| 1314 | if (__new_const == NULL) { \ |
| 1315 | return 0; \ |
| 1316 | } \ |
| 1317 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1318 | Py_DECREF(__new_const); \ |
| 1319 | return 0; \ |
| 1320 | } \ |
| 1321 | Py_DECREF(__new_const); \ |
| 1322 | } |
| 1323 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1324 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1325 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1326 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1329 | /* Same as ADDOP_O, but steals a reference. */ |
| 1330 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1331 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1332 | Py_DECREF((O)); \ |
| 1333 | return 0; \ |
| 1334 | } \ |
| 1335 | Py_DECREF((O)); \ |
| 1336 | } |
| 1337 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1338 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1340 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1344 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1345 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1350 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1354 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1355 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1359 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1360 | */ |
| 1361 | |
| 1362 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1363 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1364 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1367 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1369 | compiler_exit_scope(c); \ |
| 1370 | return 0; \ |
| 1371 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1374 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1376 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | int _i; \ |
| 1381 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1382 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1383 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1384 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1385 | return 0; \ |
| 1386 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1389 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | int _i; \ |
| 1391 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1392 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1393 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1394 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1395 | compiler_exit_scope(c); \ |
| 1396 | return 0; \ |
| 1397 | } \ |
| 1398 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1401 | /* Search if variable annotations are present statically in a block. */ |
| 1402 | |
| 1403 | static int |
| 1404 | find_ann(asdl_seq *stmts) |
| 1405 | { |
| 1406 | int i, j, res = 0; |
| 1407 | stmt_ty st; |
| 1408 | |
| 1409 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1410 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1411 | switch (st->kind) { |
| 1412 | case AnnAssign_kind: |
| 1413 | return 1; |
| 1414 | case For_kind: |
| 1415 | res = find_ann(st->v.For.body) || |
| 1416 | find_ann(st->v.For.orelse); |
| 1417 | break; |
| 1418 | case AsyncFor_kind: |
| 1419 | res = find_ann(st->v.AsyncFor.body) || |
| 1420 | find_ann(st->v.AsyncFor.orelse); |
| 1421 | break; |
| 1422 | case While_kind: |
| 1423 | res = find_ann(st->v.While.body) || |
| 1424 | find_ann(st->v.While.orelse); |
| 1425 | break; |
| 1426 | case If_kind: |
| 1427 | res = find_ann(st->v.If.body) || |
| 1428 | find_ann(st->v.If.orelse); |
| 1429 | break; |
| 1430 | case With_kind: |
| 1431 | res = find_ann(st->v.With.body); |
| 1432 | break; |
| 1433 | case AsyncWith_kind: |
| 1434 | res = find_ann(st->v.AsyncWith.body); |
| 1435 | break; |
| 1436 | case Try_kind: |
| 1437 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1438 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1439 | st->v.Try.handlers, j); |
| 1440 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1441 | return 1; |
| 1442 | } |
| 1443 | } |
| 1444 | res = find_ann(st->v.Try.body) || |
| 1445 | find_ann(st->v.Try.finalbody) || |
| 1446 | find_ann(st->v.Try.orelse); |
| 1447 | break; |
| 1448 | default: |
| 1449 | res = 0; |
| 1450 | } |
| 1451 | if (res) { |
| 1452 | break; |
| 1453 | } |
| 1454 | } |
| 1455 | return res; |
| 1456 | } |
| 1457 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1458 | /* |
| 1459 | * Frame block handling functions |
| 1460 | */ |
| 1461 | |
| 1462 | static int |
| 1463 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
| 1464 | basicblock *exit) |
| 1465 | { |
| 1466 | struct fblockinfo *f; |
| 1467 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1468 | PyErr_SetString(PyExc_SyntaxError, |
| 1469 | "too many statically nested blocks"); |
| 1470 | return 0; |
| 1471 | } |
| 1472 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1473 | f->fb_type = t; |
| 1474 | f->fb_block = b; |
| 1475 | f->fb_exit = exit; |
| 1476 | return 1; |
| 1477 | } |
| 1478 | |
| 1479 | static void |
| 1480 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1481 | { |
| 1482 | struct compiler_unit *u = c->u; |
| 1483 | assert(u->u_nfblocks > 0); |
| 1484 | u->u_nfblocks--; |
| 1485 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1486 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1487 | } |
| 1488 | |
| 1489 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
| 1490 | * popping the blocks will be restored afterwards. |
| 1491 | */ |
| 1492 | static int |
| 1493 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1494 | int preserve_tos) |
| 1495 | { |
| 1496 | switch (info->fb_type) { |
| 1497 | case WHILE_LOOP: |
| 1498 | return 1; |
| 1499 | |
| 1500 | case FINALLY_END: |
| 1501 | ADDOP_I(c, POP_FINALLY, preserve_tos); |
| 1502 | return 1; |
| 1503 | |
| 1504 | case FOR_LOOP: |
| 1505 | /* Pop the iterator */ |
| 1506 | if (preserve_tos) { |
| 1507 | ADDOP(c, ROT_TWO); |
| 1508 | } |
| 1509 | ADDOP(c, POP_TOP); |
| 1510 | return 1; |
| 1511 | |
| 1512 | case EXCEPT: |
| 1513 | ADDOP(c, POP_BLOCK); |
| 1514 | return 1; |
| 1515 | |
| 1516 | case FINALLY_TRY: |
| 1517 | ADDOP(c, POP_BLOCK); |
| 1518 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1519 | return 1; |
| 1520 | |
| 1521 | case WITH: |
| 1522 | case ASYNC_WITH: |
| 1523 | ADDOP(c, POP_BLOCK); |
| 1524 | if (preserve_tos) { |
| 1525 | ADDOP(c, ROT_TWO); |
| 1526 | } |
| 1527 | ADDOP(c, BEGIN_FINALLY); |
| 1528 | ADDOP(c, WITH_CLEANUP_START); |
| 1529 | if (info->fb_type == ASYNC_WITH) { |
| 1530 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1531 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1532 | ADDOP(c, YIELD_FROM); |
| 1533 | } |
| 1534 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 1535 | ADDOP_I(c, POP_FINALLY, 0); |
| 1536 | return 1; |
| 1537 | |
| 1538 | case HANDLER_CLEANUP: |
| 1539 | if (preserve_tos) { |
| 1540 | ADDOP(c, ROT_FOUR); |
| 1541 | } |
| 1542 | if (info->fb_exit) { |
| 1543 | ADDOP(c, POP_BLOCK); |
| 1544 | ADDOP(c, POP_EXCEPT); |
| 1545 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1546 | } |
| 1547 | else { |
| 1548 | ADDOP(c, POP_EXCEPT); |
| 1549 | } |
| 1550 | return 1; |
| 1551 | } |
| 1552 | Py_UNREACHABLE(); |
| 1553 | } |
| 1554 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1555 | /* Compile a sequence of statements, checking for a docstring |
| 1556 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1557 | |
| 1558 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1559 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1560 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1561 | int i = 0; |
| 1562 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1563 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1564 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1565 | /* Set current line number to the line number of first statement. |
| 1566 | This way line number for SETUP_ANNOTATIONS will always |
| 1567 | coincide with the line number of first "real" statement in module. |
| 1568 | If body is empy, then lineno will be set later in assemble. */ |
| 1569 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && |
| 1570 | !c->u->u_lineno && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1571 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1572 | c->u->u_lineno = st->lineno; |
| 1573 | } |
| 1574 | /* Every annotated class and module should have __annotations__. */ |
| 1575 | if (find_ann(stmts)) { |
| 1576 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1577 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1578 | if (!asdl_seq_LEN(stmts)) |
| 1579 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1580 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1581 | if (c->c_optimize < 2) { |
| 1582 | docstring = _PyAST_GetDocString(stmts); |
| 1583 | if (docstring) { |
| 1584 | i = 1; |
| 1585 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1586 | assert(st->kind == Expr_kind); |
| 1587 | VISIT(c, expr, st->v.Expr.value); |
| 1588 | if (!compiler_nameop(c, __doc__, Store)) |
| 1589 | return 0; |
| 1590 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1591 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1592 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1593 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | static PyCodeObject * |
| 1598 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1599 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1600 | PyCodeObject *co; |
| 1601 | int addNone = 1; |
| 1602 | static PyObject *module; |
| 1603 | if (!module) { |
| 1604 | module = PyUnicode_InternFromString("<module>"); |
| 1605 | if (!module) |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1609 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | return NULL; |
| 1611 | switch (mod->kind) { |
| 1612 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1613 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1614 | compiler_exit_scope(c); |
| 1615 | return 0; |
| 1616 | } |
| 1617 | break; |
| 1618 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1619 | if (find_ann(mod->v.Interactive.body)) { |
| 1620 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1621 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | c->c_interactive = 1; |
| 1623 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1624 | mod->v.Interactive.body); |
| 1625 | break; |
| 1626 | case Expression_kind: |
| 1627 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1628 | addNone = 0; |
| 1629 | break; |
| 1630 | case Suite_kind: |
| 1631 | PyErr_SetString(PyExc_SystemError, |
| 1632 | "suite should not be possible"); |
| 1633 | return 0; |
| 1634 | default: |
| 1635 | PyErr_Format(PyExc_SystemError, |
| 1636 | "module kind %d should not be possible", |
| 1637 | mod->kind); |
| 1638 | return 0; |
| 1639 | } |
| 1640 | co = assemble(c, addNone); |
| 1641 | compiler_exit_scope(c); |
| 1642 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1645 | /* The test for LOCAL must come before the test for FREE in order to |
| 1646 | handle classes where name is both local and free. The local var is |
| 1647 | 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] | 1648 | */ |
| 1649 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1650 | static int |
| 1651 | get_ref_type(struct compiler *c, PyObject *name) |
| 1652 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1653 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1654 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1655 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1656 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1657 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | if (scope == 0) { |
| 1659 | char buf[350]; |
| 1660 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1661 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1662 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1663 | PyUnicode_AsUTF8(name), |
| 1664 | PyUnicode_AsUTF8(c->u->u_name), |
| 1665 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1666 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1667 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1668 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | ); |
| 1670 | Py_FatalError(buf); |
| 1671 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | static int |
| 1677 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1678 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1679 | PyObject *v; |
| 1680 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1681 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1682 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1683 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1687 | 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] | 1688 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1689 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1690 | if (qualname == NULL) |
| 1691 | qualname = co->co_name; |
| 1692 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1693 | if (free) { |
| 1694 | for (i = 0; i < free; ++i) { |
| 1695 | /* Bypass com_addop_varname because it will generate |
| 1696 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1697 | */ |
| 1698 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1699 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1700 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1701 | /* Special case: If a class contains a method with a |
| 1702 | free variable that has the same name as a method, |
| 1703 | the name will be considered free *and* local in the |
| 1704 | class. It should be handled by the closure, as |
| 1705 | well as by the normal name loookup logic. |
| 1706 | */ |
| 1707 | reftype = get_ref_type(c, name); |
| 1708 | if (reftype == CELL) |
| 1709 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1710 | else /* (reftype == FREE) */ |
| 1711 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1712 | if (arg == -1) { |
| 1713 | fprintf(stderr, |
| 1714 | "lookup %s in %s %d %d\n" |
| 1715 | "freevars of %s: %s\n", |
| 1716 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1717 | PyUnicode_AsUTF8(c->u->u_name), |
| 1718 | reftype, arg, |
| 1719 | PyUnicode_AsUTF8(co->co_name), |
| 1720 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1721 | Py_FatalError("compiler_make_closure()"); |
| 1722 | } |
| 1723 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1724 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1725 | flags |= 0x08; |
| 1726 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1727 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1728 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1729 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1730 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1731 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
| 1734 | static int |
| 1735 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | if (!decos) |
| 1740 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1742 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1743 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1744 | } |
| 1745 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1749 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1751 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1752 | /* Push a dict of keyword-only default values. |
| 1753 | |
| 1754 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1755 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1756 | int i; |
| 1757 | PyObject *keys = NULL; |
| 1758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1760 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1761 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1762 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1763 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1764 | if (!mangled) { |
| 1765 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1767 | if (keys == NULL) { |
| 1768 | keys = PyList_New(1); |
| 1769 | if (keys == NULL) { |
| 1770 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1771 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1772 | } |
| 1773 | PyList_SET_ITEM(keys, 0, mangled); |
| 1774 | } |
| 1775 | else { |
| 1776 | int res = PyList_Append(keys, mangled); |
| 1777 | Py_DECREF(mangled); |
| 1778 | if (res == -1) { |
| 1779 | goto error; |
| 1780 | } |
| 1781 | } |
| 1782 | if (!compiler_visit_expr(c, default_)) { |
| 1783 | goto error; |
| 1784 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | } |
| 1786 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1787 | if (keys != NULL) { |
| 1788 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 1789 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 1790 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1791 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1792 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1793 | assert(default_count > 0); |
| 1794 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1795 | } |
| 1796 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1797 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | error: |
| 1801 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1802 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1806 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 1807 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 1808 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1809 | return 1; |
| 1810 | } |
| 1811 | |
| 1812 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1813 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 1814 | expr_ty annotation, PyObject *names) |
| 1815 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1817 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1818 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 1819 | VISIT(c, annexpr, annotation) |
| 1820 | } |
| 1821 | else { |
| 1822 | VISIT(c, expr, annotation); |
| 1823 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 1824 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1825 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1826 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1827 | if (PyList_Append(names, mangled) < 0) { |
| 1828 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1829 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 1830 | } |
| 1831 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1833 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | static int |
| 1837 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 1838 | PyObject *names) |
| 1839 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1840 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1841 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1842 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1843 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | c, |
| 1845 | arg->arg, |
| 1846 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1847 | names)) |
| 1848 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1850 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | static int |
| 1854 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 1855 | expr_ty returns) |
| 1856 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1857 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1858 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1859 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1860 | 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] | 1861 | */ |
| 1862 | static identifier return_str; |
| 1863 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1864 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | names = PyList_New(0); |
| 1866 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 1867 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1868 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1869 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1871 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1872 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1873 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1874 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1875 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1877 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1878 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1879 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | if (!return_str) { |
| 1883 | return_str = PyUnicode_InternFromString("return"); |
| 1884 | if (!return_str) |
| 1885 | goto error; |
| 1886 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 1887 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | goto error; |
| 1889 | } |
| 1890 | |
| 1891 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1893 | PyObject *keytuple = PyList_AsTuple(names); |
| 1894 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1895 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1896 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1897 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1899 | else { |
| 1900 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1901 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1902 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1903 | |
| 1904 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 1906 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1910 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 1911 | { |
| 1912 | VISIT_SEQ(c, expr, args->defaults); |
| 1913 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 1914 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1917 | static Py_ssize_t |
| 1918 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 1919 | { |
| 1920 | Py_ssize_t funcflags = 0; |
| 1921 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1922 | if (!compiler_visit_defaults(c, args)) |
| 1923 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1924 | funcflags |= 0x01; |
| 1925 | } |
| 1926 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1927 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1928 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1929 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1930 | return -1; |
| 1931 | } |
| 1932 | else if (res > 0) { |
| 1933 | funcflags |= 0x02; |
| 1934 | } |
| 1935 | } |
| 1936 | return funcflags; |
| 1937 | } |
| 1938 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1939 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1940 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1942 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1943 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1944 | arguments_ty args; |
| 1945 | expr_ty returns; |
| 1946 | identifier name; |
| 1947 | asdl_seq* decos; |
| 1948 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1949 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1950 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1951 | int scope_type; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1953 | if (is_async) { |
| 1954 | assert(s->kind == AsyncFunctionDef_kind); |
| 1955 | |
| 1956 | args = s->v.AsyncFunctionDef.args; |
| 1957 | returns = s->v.AsyncFunctionDef.returns; |
| 1958 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 1959 | name = s->v.AsyncFunctionDef.name; |
| 1960 | body = s->v.AsyncFunctionDef.body; |
| 1961 | |
| 1962 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 1963 | } else { |
| 1964 | assert(s->kind == FunctionDef_kind); |
| 1965 | |
| 1966 | args = s->v.FunctionDef.args; |
| 1967 | returns = s->v.FunctionDef.returns; |
| 1968 | decos = s->v.FunctionDef.decorator_list; |
| 1969 | name = s->v.FunctionDef.name; |
| 1970 | body = s->v.FunctionDef.body; |
| 1971 | |
| 1972 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 1973 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | if (!compiler_decorators(c, decos)) |
| 1976 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1977 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1978 | funcflags = compiler_default_arguments(c, args); |
| 1979 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1981 | } |
| 1982 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1983 | annotations = compiler_visit_annotations(c, args, returns); |
| 1984 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1985 | return 0; |
| 1986 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1987 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1988 | funcflags |= 0x04; |
| 1989 | } |
| 1990 | |
| 1991 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) { |
| 1992 | return 0; |
| 1993 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1994 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1995 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1996 | if (c->c_optimize < 2) { |
| 1997 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1998 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1999 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | compiler_exit_scope(c); |
| 2001 | return 0; |
| 2002 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2004 | c->u->u_argcount = asdl_seq_LEN(args->args); |
| 2005 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2006 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2007 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2008 | qualname = c->u->u_qualname; |
| 2009 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2010 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2011 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2012 | Py_XDECREF(qualname); |
| 2013 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2014 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2015 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2016 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2017 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2018 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2019 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2021 | /* decorators */ |
| 2022 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2023 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2024 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2025 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2026 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| 2029 | static int |
| 2030 | compiler_class(struct compiler *c, stmt_ty s) |
| 2031 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2032 | PyCodeObject *co; |
| 2033 | PyObject *str; |
| 2034 | int i; |
| 2035 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | if (!compiler_decorators(c, decos)) |
| 2038 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2040 | /* ultimately generate code for: |
| 2041 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2042 | where: |
| 2043 | <func> is a function/closure created from the class body; |
| 2044 | it has a single argument (__locals__) where the dict |
| 2045 | (or MutableSequence) representing the locals is passed |
| 2046 | <name> is the class name |
| 2047 | <bases> is the positional arguments and *varargs argument |
| 2048 | <keywords> is the keyword arguments and **kwds argument |
| 2049 | This borrows from compiler_call. |
| 2050 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2053 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
| 2054 | COMPILER_SCOPE_CLASS, (void *)s, s->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | return 0; |
| 2056 | /* this block represents what we do in the new scope */ |
| 2057 | { |
| 2058 | /* use the class name for name mangling */ |
| 2059 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2060 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | /* load (global) __name__ ... */ |
| 2062 | str = PyUnicode_InternFromString("__name__"); |
| 2063 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2064 | Py_XDECREF(str); |
| 2065 | compiler_exit_scope(c); |
| 2066 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2067 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | Py_DECREF(str); |
| 2069 | /* ... and store it as __module__ */ |
| 2070 | str = PyUnicode_InternFromString("__module__"); |
| 2071 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2072 | Py_XDECREF(str); |
| 2073 | compiler_exit_scope(c); |
| 2074 | return 0; |
| 2075 | } |
| 2076 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2077 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2078 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2079 | str = PyUnicode_InternFromString("__qualname__"); |
| 2080 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2081 | Py_XDECREF(str); |
| 2082 | compiler_exit_scope(c); |
| 2083 | return 0; |
| 2084 | } |
| 2085 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2087 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | compiler_exit_scope(c); |
| 2089 | return 0; |
| 2090 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2091 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2092 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2093 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2094 | str = PyUnicode_InternFromString("__class__"); |
| 2095 | if (str == NULL) { |
| 2096 | compiler_exit_scope(c); |
| 2097 | return 0; |
| 2098 | } |
| 2099 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2100 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2101 | if (i < 0) { |
| 2102 | compiler_exit_scope(c); |
| 2103 | return 0; |
| 2104 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2105 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2108 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2109 | str = PyUnicode_InternFromString("__classcell__"); |
| 2110 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2111 | Py_XDECREF(str); |
| 2112 | compiler_exit_scope(c); |
| 2113 | return 0; |
| 2114 | } |
| 2115 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2116 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2117 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2118 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2119 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2120 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2121 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2122 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | /* create the code object */ |
| 2124 | co = assemble(c, 1); |
| 2125 | } |
| 2126 | /* leave the new scope */ |
| 2127 | compiler_exit_scope(c); |
| 2128 | if (co == NULL) |
| 2129 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2131 | /* 2. load the 'build_class' function */ |
| 2132 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2133 | |
| 2134 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2135 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | Py_DECREF(co); |
| 2137 | |
| 2138 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2139 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2140 | |
| 2141 | /* 5. generate the rest of the code for the call */ |
| 2142 | if (!compiler_call_helper(c, 2, |
| 2143 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2144 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | return 0; |
| 2146 | |
| 2147 | /* 6. apply decorators */ |
| 2148 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2149 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2150 | } |
| 2151 | |
| 2152 | /* 7. store into <name> */ |
| 2153 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2154 | return 0; |
| 2155 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | static int |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2159 | cmpop(cmpop_ty op) |
| 2160 | { |
| 2161 | switch (op) { |
| 2162 | case Eq: |
| 2163 | return PyCmp_EQ; |
| 2164 | case NotEq: |
| 2165 | return PyCmp_NE; |
| 2166 | case Lt: |
| 2167 | return PyCmp_LT; |
| 2168 | case LtE: |
| 2169 | return PyCmp_LE; |
| 2170 | case Gt: |
| 2171 | return PyCmp_GT; |
| 2172 | case GtE: |
| 2173 | return PyCmp_GE; |
| 2174 | case Is: |
| 2175 | return PyCmp_IS; |
| 2176 | case IsNot: |
| 2177 | return PyCmp_IS_NOT; |
| 2178 | case In: |
| 2179 | return PyCmp_IN; |
| 2180 | case NotIn: |
| 2181 | return PyCmp_NOT_IN; |
| 2182 | default: |
| 2183 | return PyCmp_BAD; |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | static int |
| 2188 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2189 | { |
| 2190 | switch (e->kind) { |
| 2191 | case UnaryOp_kind: |
| 2192 | if (e->v.UnaryOp.op == Not) |
| 2193 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2194 | /* fallback to general implementation */ |
| 2195 | break; |
| 2196 | case BoolOp_kind: { |
| 2197 | asdl_seq *s = e->v.BoolOp.values; |
| 2198 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2199 | assert(n >= 0); |
| 2200 | int cond2 = e->v.BoolOp.op == Or; |
| 2201 | basicblock *next2 = next; |
| 2202 | if (!cond2 != !cond) { |
| 2203 | next2 = compiler_new_block(c); |
| 2204 | if (next2 == NULL) |
| 2205 | return 0; |
| 2206 | } |
| 2207 | for (i = 0; i < n; ++i) { |
| 2208 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2209 | return 0; |
| 2210 | } |
| 2211 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2212 | return 0; |
| 2213 | if (next2 != next) |
| 2214 | compiler_use_next_block(c, next2); |
| 2215 | return 1; |
| 2216 | } |
| 2217 | case IfExp_kind: { |
| 2218 | basicblock *end, *next2; |
| 2219 | end = compiler_new_block(c); |
| 2220 | if (end == NULL) |
| 2221 | return 0; |
| 2222 | next2 = compiler_new_block(c); |
| 2223 | if (next2 == NULL) |
| 2224 | return 0; |
| 2225 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2226 | return 0; |
| 2227 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2228 | return 0; |
| 2229 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2230 | compiler_use_next_block(c, next2); |
| 2231 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2232 | return 0; |
| 2233 | compiler_use_next_block(c, end); |
| 2234 | return 1; |
| 2235 | } |
| 2236 | case Compare_kind: { |
| 2237 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2238 | if (n > 0) { |
| 2239 | basicblock *cleanup = compiler_new_block(c); |
| 2240 | if (cleanup == NULL) |
| 2241 | return 0; |
| 2242 | VISIT(c, expr, e->v.Compare.left); |
| 2243 | for (i = 0; i < n; i++) { |
| 2244 | VISIT(c, expr, |
| 2245 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2246 | ADDOP(c, DUP_TOP); |
| 2247 | ADDOP(c, ROT_THREE); |
| 2248 | ADDOP_I(c, COMPARE_OP, |
| 2249 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 2250 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2251 | NEXT_BLOCK(c); |
| 2252 | } |
| 2253 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 2254 | ADDOP_I(c, COMPARE_OP, |
| 2255 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
| 2256 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2257 | basicblock *end = compiler_new_block(c); |
| 2258 | if (end == NULL) |
| 2259 | return 0; |
| 2260 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2261 | compiler_use_next_block(c, cleanup); |
| 2262 | ADDOP(c, POP_TOP); |
| 2263 | if (!cond) { |
| 2264 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2265 | } |
| 2266 | compiler_use_next_block(c, end); |
| 2267 | return 1; |
| 2268 | } |
| 2269 | /* fallback to general implementation */ |
| 2270 | break; |
| 2271 | } |
| 2272 | default: |
| 2273 | /* fallback to general implementation */ |
| 2274 | break; |
| 2275 | } |
| 2276 | |
| 2277 | /* general implementation */ |
| 2278 | VISIT(c, expr, e); |
| 2279 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2280 | return 1; |
| 2281 | } |
| 2282 | |
| 2283 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2284 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | basicblock *end, *next; |
| 2287 | |
| 2288 | assert(e->kind == IfExp_kind); |
| 2289 | end = compiler_new_block(c); |
| 2290 | if (end == NULL) |
| 2291 | return 0; |
| 2292 | next = compiler_new_block(c); |
| 2293 | if (next == NULL) |
| 2294 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2295 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2296 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2297 | VISIT(c, expr, e->v.IfExp.body); |
| 2298 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2299 | compiler_use_next_block(c, next); |
| 2300 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2301 | compiler_use_next_block(c, end); |
| 2302 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2306 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2307 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2308 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2309 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2311 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | arguments_ty args = e->v.Lambda.args; |
| 2313 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2315 | if (!name) { |
| 2316 | name = PyUnicode_InternFromString("<lambda>"); |
| 2317 | if (!name) |
| 2318 | return 0; |
| 2319 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2320 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2321 | funcflags = compiler_default_arguments(c, args); |
| 2322 | if (funcflags == -1) { |
| 2323 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2324 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2325 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2326 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2327 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2328 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | /* Make None the first constant, so the lambda can't have a |
| 2331 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2332 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | c->u->u_argcount = asdl_seq_LEN(args->args); |
| 2336 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2337 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2338 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2339 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2340 | } |
| 2341 | else { |
| 2342 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2343 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2344 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2345 | qualname = c->u->u_qualname; |
| 2346 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2347 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2348 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2349 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2350 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2351 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2352 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2353 | Py_DECREF(co); |
| 2354 | |
| 2355 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2359 | compiler_if(struct compiler *c, stmt_ty s) |
| 2360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2361 | basicblock *end, *next; |
| 2362 | int constant; |
| 2363 | assert(s->kind == If_kind); |
| 2364 | end = compiler_new_block(c); |
| 2365 | if (end == NULL) |
| 2366 | return 0; |
| 2367 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2368 | constant = expr_constant(s->v.If.test); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2369 | /* constant = 0: "if 0" |
| 2370 | * constant = 1: "if 1", "if 2", ... |
| 2371 | * constant = -1: rest */ |
| 2372 | if (constant == 0) { |
| 2373 | if (s->v.If.orelse) |
| 2374 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2375 | } else if (constant == 1) { |
| 2376 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2377 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2378 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | next = compiler_new_block(c); |
| 2380 | if (next == NULL) |
| 2381 | return 0; |
| 2382 | } |
| 2383 | else |
| 2384 | next = end; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2385 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) |
| 2386 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2388 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2389 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2390 | compiler_use_next_block(c, next); |
| 2391 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2392 | } |
| 2393 | } |
| 2394 | compiler_use_next_block(c, end); |
| 2395 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | static int |
| 2399 | compiler_for(struct compiler *c, stmt_ty s) |
| 2400 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2401 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | start = compiler_new_block(c); |
| 2404 | cleanup = compiler_new_block(c); |
| 2405 | end = compiler_new_block(c); |
| 2406 | if (start == NULL || end == NULL || cleanup == NULL) |
| 2407 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2408 | |
| 2409 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2411 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2412 | VISIT(c, expr, s->v.For.iter); |
| 2413 | ADDOP(c, GET_ITER); |
| 2414 | compiler_use_next_block(c, start); |
| 2415 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2416 | VISIT(c, expr, s->v.For.target); |
| 2417 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2418 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2419 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2420 | |
| 2421 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2422 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2423 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2424 | compiler_use_next_block(c, end); |
| 2425 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2428 | |
| 2429 | static int |
| 2430 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2431 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2432 | basicblock *start, *except, *end; |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2433 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
| 2434 | return compiler_error(c, "'async for' outside async function"); |
| 2435 | } |
| 2436 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2437 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2438 | except = compiler_new_block(c); |
| 2439 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2440 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2441 | if (start == NULL || except == NULL || end == NULL) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2442 | return 0; |
| 2443 | |
| 2444 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2445 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2446 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2447 | compiler_use_next_block(c, start); |
| 2448 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
| 2449 | return 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2450 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2451 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2452 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2453 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2454 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2455 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2456 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2457 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2458 | /* Success block for __anext__ */ |
| 2459 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2460 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2461 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2462 | |
| 2463 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2464 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2465 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2466 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2467 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2468 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2469 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2470 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2471 | |
| 2472 | compiler_use_next_block(c, end); |
| 2473 | |
| 2474 | return 1; |
| 2475 | } |
| 2476 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2477 | static int |
| 2478 | compiler_while(struct compiler *c, stmt_ty s) |
| 2479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2481 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2482 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | if (constant == 0) { |
| 2484 | if (s->v.While.orelse) |
| 2485 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2486 | return 1; |
| 2487 | } |
| 2488 | loop = compiler_new_block(c); |
| 2489 | end = compiler_new_block(c); |
| 2490 | if (constant == -1) { |
| 2491 | anchor = compiler_new_block(c); |
| 2492 | if (anchor == NULL) |
| 2493 | return 0; |
| 2494 | } |
| 2495 | if (loop == NULL || end == NULL) |
| 2496 | return 0; |
| 2497 | if (s->v.While.orelse) { |
| 2498 | orelse = compiler_new_block(c); |
| 2499 | if (orelse == NULL) |
| 2500 | return 0; |
| 2501 | } |
| 2502 | else |
| 2503 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2505 | compiler_use_next_block(c, loop); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2506 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | return 0; |
| 2508 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2509 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2510 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2511 | } |
| 2512 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2513 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2515 | /* XXX should the two POP instructions be in a separate block |
| 2516 | if there is no else clause ? |
| 2517 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2518 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2519 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2520 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2521 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2524 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2525 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2527 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2531 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2532 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2533 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2534 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2535 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2536 | return compiler_error(c, "'return' outside function"); |
| 2537 | if (s->v.Return.value != NULL && |
| 2538 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2539 | { |
| 2540 | return compiler_error( |
| 2541 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2542 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2543 | if (preserve_tos) { |
| 2544 | VISIT(c, expr, s->v.Return.value); |
| 2545 | } |
| 2546 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2547 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2548 | |
| 2549 | if (!compiler_unwind_fblock(c, info, preserve_tos)) |
| 2550 | return 0; |
| 2551 | } |
| 2552 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2553 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2554 | } |
| 2555 | else if (!preserve_tos) { |
| 2556 | VISIT(c, expr, s->v.Return.value); |
| 2557 | } |
| 2558 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2560 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2563 | static int |
| 2564 | compiler_break(struct compiler *c) |
| 2565 | { |
| 2566 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2567 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2568 | |
| 2569 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2570 | return 0; |
| 2571 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2572 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit); |
| 2573 | return 1; |
| 2574 | } |
| 2575 | } |
| 2576 | return compiler_error(c, "'break' outside loop"); |
| 2577 | } |
| 2578 | |
| 2579 | static int |
| 2580 | compiler_continue(struct compiler *c) |
| 2581 | { |
| 2582 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2583 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2584 | |
| 2585 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2586 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block); |
| 2587 | return 1; |
| 2588 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2589 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2590 | return 0; |
| 2591 | } |
| 2592 | return compiler_error(c, "'continue' not properly in loop"); |
| 2593 | } |
| 2594 | |
| 2595 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2596 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | |
| 2598 | SETUP_FINALLY L |
| 2599 | <code for body> |
| 2600 | POP_BLOCK |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2601 | BEGIN_FINALLY |
| 2602 | L: |
| 2603 | <code for finalbody> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | END_FINALLY |
| 2605 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2606 | The special instructions use the block stack. Each block |
| 2607 | stack entry contains the instruction that created it (here |
| 2608 | SETUP_FINALLY), the level of the value stack at the time the |
| 2609 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2611 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2612 | Pushes the current value stack level and the label |
| 2613 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2614 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2615 | Pops en entry from the block stack. |
| 2616 | BEGIN_FINALLY |
| 2617 | Pushes NULL onto the value stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2618 | END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2619 | Pops 1 (NULL or int) or 6 entries from the *value* stack and restore |
| 2620 | the raised and the caught exceptions they specify. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2621 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2622 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2623 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2624 | exceptions are pushed onto the value stack (and the exception |
| 2625 | condition is cleared), and the interpreter jumps to the label |
| 2626 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2627 | */ |
| 2628 | |
| 2629 | static int |
| 2630 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2631 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | basicblock *body, *end; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2634 | body = compiler_new_block(c); |
| 2635 | end = compiler_new_block(c); |
| 2636 | if (body == NULL || end == NULL) |
| 2637 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2638 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2639 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2641 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2642 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2643 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2644 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2645 | if (!compiler_try_except(c, s)) |
| 2646 | return 0; |
| 2647 | } |
| 2648 | else { |
| 2649 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2650 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2652 | ADDOP(c, BEGIN_FINALLY); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | compiler_pop_fblock(c, FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2655 | /* `finally` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2656 | compiler_use_next_block(c, end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2657 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2658 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2659 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | ADDOP(c, END_FINALLY); |
| 2661 | compiler_pop_fblock(c, FINALLY_END, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
| 2665 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2666 | 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] | 2667 | (The contents of the value stack is shown in [], with the top |
| 2668 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2669 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2670 | |
| 2671 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2672 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | [] <code for S> |
| 2674 | [] POP_BLOCK |
| 2675 | [] JUMP_FORWARD L0 |
| 2676 | |
| 2677 | [tb, val, exc] L1: DUP ) |
| 2678 | [tb, val, exc, exc] <evaluate E1> ) |
| 2679 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2680 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2681 | [tb, val, exc] POP |
| 2682 | [tb, val] <assign to V1> (or POP if no V1) |
| 2683 | [tb] POP |
| 2684 | [] <code for S1> |
| 2685 | JUMP_FORWARD L0 |
| 2686 | |
| 2687 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2688 | .............................etc....................... |
| 2689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2691 | |
| 2692 | [] L0: <next statement> |
| 2693 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2694 | Of course, parts are not generated if Vi or Ei is not present. |
| 2695 | */ |
| 2696 | static int |
| 2697 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2698 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2700 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | body = compiler_new_block(c); |
| 2703 | except = compiler_new_block(c); |
| 2704 | orelse = compiler_new_block(c); |
| 2705 | end = compiler_new_block(c); |
| 2706 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2707 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2708 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2710 | if (!compiler_push_fblock(c, EXCEPT, body, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2711 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2712 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2713 | ADDOP(c, POP_BLOCK); |
| 2714 | compiler_pop_fblock(c, EXCEPT, body); |
| 2715 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2716 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | compiler_use_next_block(c, except); |
| 2718 | for (i = 0; i < n; i++) { |
| 2719 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2720 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2721 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 2722 | return compiler_error(c, "default 'except:' must be last"); |
| 2723 | c->u->u_lineno_set = 0; |
| 2724 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 2725 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | except = compiler_new_block(c); |
| 2727 | if (except == NULL) |
| 2728 | return 0; |
| 2729 | if (handler->v.ExceptHandler.type) { |
| 2730 | ADDOP(c, DUP_TOP); |
| 2731 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 2732 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 2733 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 2734 | } |
| 2735 | ADDOP(c, POP_TOP); |
| 2736 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2737 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2738 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2739 | cleanup_end = compiler_new_block(c); |
| 2740 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2741 | if (!(cleanup_end || cleanup_body)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2742 | return 0; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2743 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2744 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 2745 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2747 | /* |
| 2748 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2749 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2750 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2751 | try: |
| 2752 | # body |
| 2753 | finally: |
| 2754 | name = None |
| 2755 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2756 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2757 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2758 | /* second try: */ |
| 2759 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 2760 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2761 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2762 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2764 | /* second # body */ |
| 2765 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 2766 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2767 | ADDOP(c, BEGIN_FINALLY); |
| 2768 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2770 | /* finally: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2771 | compiler_use_next_block(c, cleanup_end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2772 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2773 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2775 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2776 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2777 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2778 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2780 | ADDOP(c, END_FINALLY); |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 2781 | ADDOP(c, POP_EXCEPT); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2782 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2783 | } |
| 2784 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2785 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2787 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2788 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2789 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2791 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2792 | ADDOP(c, POP_TOP); |
| 2793 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2794 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2795 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2797 | ADDOP(c, POP_EXCEPT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2798 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | } |
| 2800 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2801 | compiler_use_next_block(c, except); |
| 2802 | } |
| 2803 | ADDOP(c, END_FINALLY); |
| 2804 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2805 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2806 | compiler_use_next_block(c, end); |
| 2807 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2811 | compiler_try(struct compiler *c, stmt_ty s) { |
| 2812 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 2813 | return compiler_try_finally(c, s); |
| 2814 | else |
| 2815 | return compiler_try_except(c, s); |
| 2816 | } |
| 2817 | |
| 2818 | |
| 2819 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2820 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 2821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2822 | /* The IMPORT_NAME opcode was already generated. This function |
| 2823 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2825 | 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] | 2826 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 2828 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 2829 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2830 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2831 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2832 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 2834 | while (1) { |
| 2835 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 2837 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2838 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2839 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 2840 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2842 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 2843 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 2844 | if (dot == -1) { |
| 2845 | break; |
| 2846 | } |
| 2847 | ADDOP(c, ROT_TWO); |
| 2848 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 2850 | if (!compiler_nameop(c, asname, Store)) { |
| 2851 | return 0; |
| 2852 | } |
| 2853 | ADDOP(c, POP_TOP); |
| 2854 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | } |
| 2856 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | static int |
| 2860 | compiler_import(struct compiler *c, stmt_ty s) |
| 2861 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | /* The Import node stores a module name like a.b.c as a single |
| 2863 | string. This is convenient for all cases except |
| 2864 | import a.b.c as d |
| 2865 | where we need to parse that string to extract the individual |
| 2866 | module names. |
| 2867 | XXX Perhaps change the representation to make this case simpler? |
| 2868 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2869 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2870 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2871 | for (i = 0; i < n; i++) { |
| 2872 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 2873 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2874 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2875 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 2876 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2878 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | if (alias->asname) { |
| 2880 | r = compiler_import_as(c, alias->name, alias->asname); |
| 2881 | if (!r) |
| 2882 | return r; |
| 2883 | } |
| 2884 | else { |
| 2885 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2886 | Py_ssize_t dot = PyUnicode_FindChar( |
| 2887 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 2888 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2889 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 2890 | if (tmp == NULL) |
| 2891 | return 0; |
| 2892 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2894 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2895 | Py_DECREF(tmp); |
| 2896 | } |
| 2897 | if (!r) |
| 2898 | return r; |
| 2899 | } |
| 2900 | } |
| 2901 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
| 2904 | static int |
| 2905 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 2906 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2907 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2908 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | if (!empty_string) { |
| 2912 | empty_string = PyUnicode_FromString(""); |
| 2913 | if (!empty_string) |
| 2914 | return 0; |
| 2915 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2916 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2917 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 2918 | |
| 2919 | names = PyTuple_New(n); |
| 2920 | if (!names) |
| 2921 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2923 | /* build up the names */ |
| 2924 | for (i = 0; i < n; i++) { |
| 2925 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 2926 | Py_INCREF(alias->name); |
| 2927 | PyTuple_SET_ITEM(names, i, alias->name); |
| 2928 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2930 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 2931 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | Py_DECREF(names); |
| 2933 | return compiler_error(c, "from __future__ imports must occur " |
| 2934 | "at the beginning of the file"); |
| 2935 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2936 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | if (s->v.ImportFrom.module) { |
| 2939 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 2940 | } |
| 2941 | else { |
| 2942 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 2943 | } |
| 2944 | for (i = 0; i < n; i++) { |
| 2945 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 2946 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2948 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2949 | assert(n == 1); |
| 2950 | ADDOP(c, IMPORT_STAR); |
| 2951 | return 1; |
| 2952 | } |
| 2953 | |
| 2954 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 2955 | store_name = alias->name; |
| 2956 | if (alias->asname) |
| 2957 | store_name = alias->asname; |
| 2958 | |
| 2959 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2960 | return 0; |
| 2961 | } |
| 2962 | } |
| 2963 | /* remove imported module */ |
| 2964 | ADDOP(c, POP_TOP); |
| 2965 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
| 2968 | static int |
| 2969 | compiler_assert(struct compiler *c, stmt_ty s) |
| 2970 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | static PyObject *assertion_error = NULL; |
| 2972 | basicblock *end; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 2973 | PyObject* msg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2974 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 2975 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | return 1; |
| 2977 | if (assertion_error == NULL) { |
| 2978 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 2979 | if (assertion_error == NULL) |
| 2980 | return 0; |
| 2981 | } |
| 2982 | if (s->v.Assert.test->kind == Tuple_kind && |
| 2983 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 2984 | msg = PyUnicode_FromString("assertion is always true, " |
| 2985 | "perhaps remove parentheses?"); |
| 2986 | if (msg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2987 | return 0; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 2988 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
| 2989 | c->c_filename, c->u->u_lineno, |
| 2990 | NULL, NULL) == -1) { |
| 2991 | Py_DECREF(msg); |
| 2992 | return 0; |
| 2993 | } |
| 2994 | Py_DECREF(msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2996 | end = compiler_new_block(c); |
| 2997 | if (end == NULL) |
| 2998 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2999 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3000 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 3002 | if (s->v.Assert.msg) { |
| 3003 | VISIT(c, expr, s->v.Assert.msg); |
| 3004 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3005 | } |
| 3006 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3007 | compiler_use_next_block(c, end); |
| 3008 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3012 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3013 | { |
| 3014 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3015 | VISIT(c, expr, value); |
| 3016 | ADDOP(c, PRINT_EXPR); |
| 3017 | return 1; |
| 3018 | } |
| 3019 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3020 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3021 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3022 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | VISIT(c, expr, value); |
| 3026 | ADDOP(c, POP_TOP); |
| 3027 | return 1; |
| 3028 | } |
| 3029 | |
| 3030 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3031 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3032 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3033 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 3036 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3037 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3038 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | switch (s->kind) { |
| 3041 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3042 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3043 | case ClassDef_kind: |
| 3044 | return compiler_class(c, s); |
| 3045 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3046 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | case Delete_kind: |
| 3048 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3049 | break; |
| 3050 | case Assign_kind: |
| 3051 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3052 | VISIT(c, expr, s->v.Assign.value); |
| 3053 | for (i = 0; i < n; i++) { |
| 3054 | if (i < n - 1) |
| 3055 | ADDOP(c, DUP_TOP); |
| 3056 | VISIT(c, expr, |
| 3057 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3058 | } |
| 3059 | break; |
| 3060 | case AugAssign_kind: |
| 3061 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3062 | case AnnAssign_kind: |
| 3063 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | case For_kind: |
| 3065 | return compiler_for(c, s); |
| 3066 | case While_kind: |
| 3067 | return compiler_while(c, s); |
| 3068 | case If_kind: |
| 3069 | return compiler_if(c, s); |
| 3070 | case Raise_kind: |
| 3071 | n = 0; |
| 3072 | if (s->v.Raise.exc) { |
| 3073 | VISIT(c, expr, s->v.Raise.exc); |
| 3074 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3075 | if (s->v.Raise.cause) { |
| 3076 | VISIT(c, expr, s->v.Raise.cause); |
| 3077 | n++; |
| 3078 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3080 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3082 | case Try_kind: |
| 3083 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | case Assert_kind: |
| 3085 | return compiler_assert(c, s); |
| 3086 | case Import_kind: |
| 3087 | return compiler_import(c, s); |
| 3088 | case ImportFrom_kind: |
| 3089 | return compiler_from_import(c, s); |
| 3090 | case Global_kind: |
| 3091 | case Nonlocal_kind: |
| 3092 | break; |
| 3093 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3094 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3095 | case Pass_kind: |
| 3096 | break; |
| 3097 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3098 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | case Continue_kind: |
| 3100 | return compiler_continue(c); |
| 3101 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3102 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3103 | case AsyncFunctionDef_kind: |
| 3104 | return compiler_function(c, s, 1); |
| 3105 | case AsyncWith_kind: |
| 3106 | return compiler_async_with(c, s, 0); |
| 3107 | case AsyncFor_kind: |
| 3108 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | static int |
| 3115 | unaryop(unaryop_ty op) |
| 3116 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3117 | switch (op) { |
| 3118 | case Invert: |
| 3119 | return UNARY_INVERT; |
| 3120 | case Not: |
| 3121 | return UNARY_NOT; |
| 3122 | case UAdd: |
| 3123 | return UNARY_POSITIVE; |
| 3124 | case USub: |
| 3125 | return UNARY_NEGATIVE; |
| 3126 | default: |
| 3127 | PyErr_Format(PyExc_SystemError, |
| 3128 | "unary op %d should not be possible", op); |
| 3129 | return 0; |
| 3130 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | static int |
| 3134 | binop(struct compiler *c, operator_ty op) |
| 3135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3136 | switch (op) { |
| 3137 | case Add: |
| 3138 | return BINARY_ADD; |
| 3139 | case Sub: |
| 3140 | return BINARY_SUBTRACT; |
| 3141 | case Mult: |
| 3142 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3143 | case MatMult: |
| 3144 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3145 | case Div: |
| 3146 | return BINARY_TRUE_DIVIDE; |
| 3147 | case Mod: |
| 3148 | return BINARY_MODULO; |
| 3149 | case Pow: |
| 3150 | return BINARY_POWER; |
| 3151 | case LShift: |
| 3152 | return BINARY_LSHIFT; |
| 3153 | case RShift: |
| 3154 | return BINARY_RSHIFT; |
| 3155 | case BitOr: |
| 3156 | return BINARY_OR; |
| 3157 | case BitXor: |
| 3158 | return BINARY_XOR; |
| 3159 | case BitAnd: |
| 3160 | return BINARY_AND; |
| 3161 | case FloorDiv: |
| 3162 | return BINARY_FLOOR_DIVIDE; |
| 3163 | default: |
| 3164 | PyErr_Format(PyExc_SystemError, |
| 3165 | "binary op %d should not be possible", op); |
| 3166 | return 0; |
| 3167 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3171 | inplace_binop(struct compiler *c, operator_ty op) |
| 3172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3173 | switch (op) { |
| 3174 | case Add: |
| 3175 | return INPLACE_ADD; |
| 3176 | case Sub: |
| 3177 | return INPLACE_SUBTRACT; |
| 3178 | case Mult: |
| 3179 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3180 | case MatMult: |
| 3181 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3182 | case Div: |
| 3183 | return INPLACE_TRUE_DIVIDE; |
| 3184 | case Mod: |
| 3185 | return INPLACE_MODULO; |
| 3186 | case Pow: |
| 3187 | return INPLACE_POWER; |
| 3188 | case LShift: |
| 3189 | return INPLACE_LSHIFT; |
| 3190 | case RShift: |
| 3191 | return INPLACE_RSHIFT; |
| 3192 | case BitOr: |
| 3193 | return INPLACE_OR; |
| 3194 | case BitXor: |
| 3195 | return INPLACE_XOR; |
| 3196 | case BitAnd: |
| 3197 | return INPLACE_AND; |
| 3198 | case FloorDiv: |
| 3199 | return INPLACE_FLOOR_DIVIDE; |
| 3200 | default: |
| 3201 | PyErr_Format(PyExc_SystemError, |
| 3202 | "inplace binary op %d should not be possible", op); |
| 3203 | return 0; |
| 3204 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
| 3207 | static int |
| 3208 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3209 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3210 | int op, scope; |
| 3211 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3212 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3214 | PyObject *dict = c->u->u_names; |
| 3215 | PyObject *mangled; |
| 3216 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3217 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3218 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3219 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3220 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3221 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3222 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3223 | if (!mangled) |
| 3224 | return 0; |
| 3225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3226 | op = 0; |
| 3227 | optype = OP_NAME; |
| 3228 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3229 | switch (scope) { |
| 3230 | case FREE: |
| 3231 | dict = c->u->u_freevars; |
| 3232 | optype = OP_DEREF; |
| 3233 | break; |
| 3234 | case CELL: |
| 3235 | dict = c->u->u_cellvars; |
| 3236 | optype = OP_DEREF; |
| 3237 | break; |
| 3238 | case LOCAL: |
| 3239 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3240 | optype = OP_FAST; |
| 3241 | break; |
| 3242 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3243 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3244 | optype = OP_GLOBAL; |
| 3245 | break; |
| 3246 | case GLOBAL_EXPLICIT: |
| 3247 | optype = OP_GLOBAL; |
| 3248 | break; |
| 3249 | default: |
| 3250 | /* scope can be 0 */ |
| 3251 | break; |
| 3252 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3254 | /* 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] | 3255 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3256 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | switch (optype) { |
| 3258 | case OP_DEREF: |
| 3259 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3260 | case Load: |
| 3261 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3262 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3263 | case Store: op = STORE_DEREF; break; |
| 3264 | case AugLoad: |
| 3265 | case AugStore: |
| 3266 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3267 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | case Param: |
| 3269 | default: |
| 3270 | PyErr_SetString(PyExc_SystemError, |
| 3271 | "param invalid for deref variable"); |
| 3272 | return 0; |
| 3273 | } |
| 3274 | break; |
| 3275 | case OP_FAST: |
| 3276 | switch (ctx) { |
| 3277 | case Load: op = LOAD_FAST; break; |
| 3278 | case Store: op = STORE_FAST; break; |
| 3279 | case Del: op = DELETE_FAST; break; |
| 3280 | case AugLoad: |
| 3281 | case AugStore: |
| 3282 | break; |
| 3283 | case Param: |
| 3284 | default: |
| 3285 | PyErr_SetString(PyExc_SystemError, |
| 3286 | "param invalid for local variable"); |
| 3287 | return 0; |
| 3288 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3289 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | return 1; |
| 3291 | case OP_GLOBAL: |
| 3292 | switch (ctx) { |
| 3293 | case Load: op = LOAD_GLOBAL; break; |
| 3294 | case Store: op = STORE_GLOBAL; break; |
| 3295 | case Del: op = DELETE_GLOBAL; break; |
| 3296 | case AugLoad: |
| 3297 | case AugStore: |
| 3298 | break; |
| 3299 | case Param: |
| 3300 | default: |
| 3301 | PyErr_SetString(PyExc_SystemError, |
| 3302 | "param invalid for global variable"); |
| 3303 | return 0; |
| 3304 | } |
| 3305 | break; |
| 3306 | case OP_NAME: |
| 3307 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3308 | case Load: op = LOAD_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3309 | case Store: op = STORE_NAME; break; |
| 3310 | case Del: op = DELETE_NAME; break; |
| 3311 | case AugLoad: |
| 3312 | case AugStore: |
| 3313 | break; |
| 3314 | case Param: |
| 3315 | default: |
| 3316 | PyErr_SetString(PyExc_SystemError, |
| 3317 | "param invalid for name variable"); |
| 3318 | return 0; |
| 3319 | } |
| 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 | assert(op); |
| 3324 | arg = compiler_add_o(c, dict, mangled); |
| 3325 | Py_DECREF(mangled); |
| 3326 | if (arg < 0) |
| 3327 | return 0; |
| 3328 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
| 3331 | static int |
| 3332 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3335 | int jumpi; |
| 3336 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3339 | assert(e->kind == BoolOp_kind); |
| 3340 | if (e->v.BoolOp.op == And) |
| 3341 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3342 | else |
| 3343 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3344 | end = compiler_new_block(c); |
| 3345 | if (end == NULL) |
| 3346 | return 0; |
| 3347 | s = e->v.BoolOp.values; |
| 3348 | n = asdl_seq_LEN(s) - 1; |
| 3349 | assert(n >= 0); |
| 3350 | for (i = 0; i < n; ++i) { |
| 3351 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3352 | ADDOP_JABS(c, jumpi, end); |
| 3353 | } |
| 3354 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3355 | compiler_use_next_block(c, end); |
| 3356 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3360 | starunpack_helper(struct compiler *c, asdl_seq *elts, |
| 3361 | int single_op, int inner_op, int outer_op) |
| 3362 | { |
| 3363 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3364 | Py_ssize_t i, nsubitems = 0, nseen = 0; |
| 3365 | for (i = 0; i < n; i++) { |
| 3366 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3367 | if (elt->kind == Starred_kind) { |
| 3368 | if (nseen) { |
| 3369 | ADDOP_I(c, inner_op, nseen); |
| 3370 | nseen = 0; |
| 3371 | nsubitems++; |
| 3372 | } |
| 3373 | VISIT(c, expr, elt->v.Starred.value); |
| 3374 | nsubitems++; |
| 3375 | } |
| 3376 | else { |
| 3377 | VISIT(c, expr, elt); |
| 3378 | nseen++; |
| 3379 | } |
| 3380 | } |
| 3381 | if (nsubitems) { |
| 3382 | if (nseen) { |
| 3383 | ADDOP_I(c, inner_op, nseen); |
| 3384 | nsubitems++; |
| 3385 | } |
| 3386 | ADDOP_I(c, outer_op, nsubitems); |
| 3387 | } |
| 3388 | else |
| 3389 | ADDOP_I(c, single_op, nseen); |
| 3390 | return 1; |
| 3391 | } |
| 3392 | |
| 3393 | static int |
| 3394 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3395 | { |
| 3396 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3397 | Py_ssize_t i; |
| 3398 | int seen_star = 0; |
| 3399 | for (i = 0; i < n; i++) { |
| 3400 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3401 | if (elt->kind == Starred_kind && !seen_star) { |
| 3402 | if ((i >= (1 << 8)) || |
| 3403 | (n-i-1 >= (INT_MAX >> 8))) |
| 3404 | return compiler_error(c, |
| 3405 | "too many expressions in " |
| 3406 | "star-unpacking assignment"); |
| 3407 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3408 | seen_star = 1; |
| 3409 | asdl_seq_SET(elts, i, elt->v.Starred.value); |
| 3410 | } |
| 3411 | else if (elt->kind == Starred_kind) { |
| 3412 | return compiler_error(c, |
| 3413 | "two starred expressions in assignment"); |
| 3414 | } |
| 3415 | } |
| 3416 | if (!seen_star) { |
| 3417 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3418 | } |
| 3419 | VISIT_SEQ(c, expr, elts); |
| 3420 | return 1; |
| 3421 | } |
| 3422 | |
| 3423 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3424 | compiler_list(struct compiler *c, expr_ty e) |
| 3425 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3426 | asdl_seq *elts = e->v.List.elts; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3427 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3428 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3430 | else if (e->v.List.ctx == Load) { |
| 3431 | return starunpack_helper(c, elts, |
| 3432 | BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3433 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3434 | else |
| 3435 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | static int |
| 3440 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3441 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3442 | asdl_seq *elts = e->v.Tuple.elts; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3443 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3444 | return assignment_helper(c, elts); |
| 3445 | } |
| 3446 | else if (e->v.Tuple.ctx == Load) { |
| 3447 | return starunpack_helper(c, elts, |
| 3448 | BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK); |
| 3449 | } |
| 3450 | else |
| 3451 | VISIT_SEQ(c, expr, elts); |
| 3452 | return 1; |
| 3453 | } |
| 3454 | |
| 3455 | static int |
| 3456 | compiler_set(struct compiler *c, expr_ty e) |
| 3457 | { |
| 3458 | return starunpack_helper(c, e->v.Set.elts, BUILD_SET, |
| 3459 | BUILD_SET, BUILD_SET_UNPACK); |
| 3460 | } |
| 3461 | |
| 3462 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3463 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3464 | { |
| 3465 | Py_ssize_t i; |
| 3466 | for (i = begin; i < end; i++) { |
| 3467 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3468 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3469 | return 0; |
| 3470 | } |
| 3471 | return 1; |
| 3472 | } |
| 3473 | |
| 3474 | static int |
| 3475 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3476 | { |
| 3477 | Py_ssize_t i, n = end - begin; |
| 3478 | PyObject *keys, *key; |
| 3479 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3480 | for (i = begin; i < end; i++) { |
| 3481 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3482 | } |
| 3483 | keys = PyTuple_New(n); |
| 3484 | if (keys == NULL) { |
| 3485 | return 0; |
| 3486 | } |
| 3487 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3488 | 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] | 3489 | Py_INCREF(key); |
| 3490 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3491 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3492 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3493 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3494 | } |
| 3495 | else { |
| 3496 | for (i = begin; i < end; i++) { |
| 3497 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3498 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3499 | } |
| 3500 | ADDOP_I(c, BUILD_MAP, n); |
| 3501 | } |
| 3502 | return 1; |
| 3503 | } |
| 3504 | |
| 3505 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3506 | compiler_dict(struct compiler *c, expr_ty e) |
| 3507 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3508 | Py_ssize_t i, n, elements; |
| 3509 | int containers; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3510 | int is_unpacking = 0; |
| 3511 | n = asdl_seq_LEN(e->v.Dict.values); |
| 3512 | containers = 0; |
| 3513 | elements = 0; |
| 3514 | for (i = 0; i < n; i++) { |
| 3515 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
| 3516 | if (elements == 0xFFFF || (elements && is_unpacking)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3517 | if (!compiler_subdict(c, e, i - elements, i)) |
| 3518 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3519 | containers++; |
| 3520 | elements = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3521 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3522 | if (is_unpacking) { |
| 3523 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3524 | containers++; |
| 3525 | } |
| 3526 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3527 | elements++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | } |
| 3529 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3530 | if (elements || containers == 0) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3531 | if (!compiler_subdict(c, e, n - elements, n)) |
| 3532 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3533 | containers++; |
| 3534 | } |
| 3535 | /* If there is more than one dict, they need to be merged into a new |
| 3536 | * dict. If there is one dict and it's an unpacking, then it needs |
| 3537 | * to be copied into a new dict." */ |
Serhiy Storchaka | 3d85fae | 2016-11-28 20:56:37 +0200 | [diff] [blame] | 3538 | if (containers > 1 || is_unpacking) { |
| 3539 | ADDOP_I(c, BUILD_MAP_UNPACK, containers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | } |
| 3541 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
| 3544 | static int |
| 3545 | compiler_compare(struct compiler *c, expr_ty e) |
| 3546 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3547 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3549 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3550 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3551 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3552 | if (n == 0) { |
| 3553 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 3554 | ADDOP_I(c, COMPARE_OP, |
| 3555 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0)))); |
| 3556 | } |
| 3557 | else { |
| 3558 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3559 | if (cleanup == NULL) |
| 3560 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3561 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | VISIT(c, expr, |
| 3563 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3564 | ADDOP(c, DUP_TOP); |
| 3565 | ADDOP(c, ROT_THREE); |
| 3566 | ADDOP_I(c, COMPARE_OP, |
| 3567 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 3568 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3569 | NEXT_BLOCK(c); |
| 3570 | } |
| 3571 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 3572 | ADDOP_I(c, COMPARE_OP, |
| 3573 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3574 | basicblock *end = compiler_new_block(c); |
| 3575 | if (end == NULL) |
| 3576 | return 0; |
| 3577 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3578 | compiler_use_next_block(c, cleanup); |
| 3579 | ADDOP(c, ROT_TWO); |
| 3580 | ADDOP(c, POP_TOP); |
| 3581 | compiler_use_next_block(c, end); |
| 3582 | } |
| 3583 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
| 3586 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3587 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 3588 | { |
| 3589 | Py_ssize_t argsl, i; |
| 3590 | expr_ty meth = e->v.Call.func; |
| 3591 | asdl_seq *args = e->v.Call.args; |
| 3592 | |
| 3593 | /* Check that the call node is an attribute access, and that |
| 3594 | the call doesn't have keyword parameters. */ |
| 3595 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 3596 | asdl_seq_LEN(e->v.Call.keywords)) |
| 3597 | return -1; |
| 3598 | |
| 3599 | /* Check that there are no *varargs types of arguments. */ |
| 3600 | argsl = asdl_seq_LEN(args); |
| 3601 | for (i = 0; i < argsl; i++) { |
| 3602 | expr_ty elt = asdl_seq_GET(args, i); |
| 3603 | if (elt->kind == Starred_kind) { |
| 3604 | return -1; |
| 3605 | } |
| 3606 | } |
| 3607 | |
| 3608 | /* Alright, we can optimize the code. */ |
| 3609 | VISIT(c, expr, meth->v.Attribute.value); |
| 3610 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 3611 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 3612 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 3613 | return 1; |
| 3614 | } |
| 3615 | |
| 3616 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | compiler_call(struct compiler *c, expr_ty e) |
| 3618 | { |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3619 | if (maybe_optimize_method_call(c, e) > 0) |
| 3620 | return 1; |
| 3621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3622 | VISIT(c, expr, e->v.Call.func); |
| 3623 | return compiler_call_helper(c, 0, |
| 3624 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3625 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3626 | } |
| 3627 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3628 | static int |
| 3629 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 3630 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3631 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 3632 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 3633 | 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] | 3634 | return 1; |
| 3635 | } |
| 3636 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3637 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3638 | static int |
| 3639 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 3640 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3641 | /* Our oparg encodes 2 pieces of information: the conversion |
| 3642 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3643 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3644 | Convert the conversion char to 2 bits: |
| 3645 | None: 000 0x0 FVC_NONE |
| 3646 | !s : 001 0x1 FVC_STR |
| 3647 | !r : 010 0x2 FVC_REPR |
| 3648 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3649 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3650 | next bit is whether or not we have a format spec: |
| 3651 | yes : 100 0x4 |
| 3652 | no : 000 0x0 |
| 3653 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3654 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3655 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3656 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3657 | /* Evaluate the expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3658 | VISIT(c, expr, e->v.FormattedValue.value); |
| 3659 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3660 | switch (e->v.FormattedValue.conversion) { |
| 3661 | case 's': oparg = FVC_STR; break; |
| 3662 | case 'r': oparg = FVC_REPR; break; |
| 3663 | case 'a': oparg = FVC_ASCII; break; |
| 3664 | case -1: oparg = FVC_NONE; break; |
| 3665 | default: |
| 3666 | PyErr_SetString(PyExc_SystemError, |
| 3667 | "Unrecognized conversion character"); |
| 3668 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3669 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3670 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3671 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3672 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3673 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3674 | } |
| 3675 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 3676 | /* And push our opcode and oparg */ |
| 3677 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 3678 | return 1; |
| 3679 | } |
| 3680 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3681 | static int |
| 3682 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 3683 | { |
| 3684 | Py_ssize_t i, n = end - begin; |
| 3685 | keyword_ty kw; |
| 3686 | PyObject *keys, *key; |
| 3687 | assert(n > 0); |
| 3688 | if (n > 1) { |
| 3689 | for (i = begin; i < end; i++) { |
| 3690 | kw = asdl_seq_GET(keywords, i); |
| 3691 | VISIT(c, expr, kw->value); |
| 3692 | } |
| 3693 | keys = PyTuple_New(n); |
| 3694 | if (keys == NULL) { |
| 3695 | return 0; |
| 3696 | } |
| 3697 | for (i = begin; i < end; i++) { |
| 3698 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 3699 | Py_INCREF(key); |
| 3700 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3701 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3702 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3703 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3704 | } |
| 3705 | else { |
| 3706 | /* a for loop only executes once */ |
| 3707 | for (i = begin; i < end; i++) { |
| 3708 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3709 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3710 | VISIT(c, expr, kw->value); |
| 3711 | } |
| 3712 | ADDOP_I(c, BUILD_MAP, n); |
| 3713 | } |
| 3714 | return 1; |
| 3715 | } |
| 3716 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3717 | /* shared code between compiler_call and compiler_class */ |
| 3718 | static int |
| 3719 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3720 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3721 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3722 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3723 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3724 | Py_ssize_t i, nseen, nelts, nkwelts; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3725 | int mustdictunpack = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 3726 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3727 | /* the number of tuples and dictionaries on the stack */ |
| 3728 | Py_ssize_t nsubargs = 0, nsubkwargs = 0; |
| 3729 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3730 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3731 | nkwelts = asdl_seq_LEN(keywords); |
| 3732 | |
| 3733 | for (i = 0; i < nkwelts; i++) { |
| 3734 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 3735 | if (kw->arg == NULL) { |
| 3736 | mustdictunpack = 1; |
| 3737 | break; |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | nseen = n; /* the number of positional arguments on the stack */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3742 | for (i = 0; i < nelts; i++) { |
| 3743 | expr_ty elt = asdl_seq_GET(args, i); |
| 3744 | if (elt->kind == Starred_kind) { |
| 3745 | /* A star-arg. If we've seen positional arguments, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3746 | pack the positional arguments into a tuple. */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3747 | if (nseen) { |
| 3748 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 3749 | nseen = 0; |
| 3750 | nsubargs++; |
| 3751 | } |
| 3752 | VISIT(c, expr, elt->v.Starred.value); |
| 3753 | nsubargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3754 | } |
| 3755 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3756 | VISIT(c, expr, elt); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3757 | nseen++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3758 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3760 | |
| 3761 | /* Same dance again for keyword arguments */ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3762 | if (nsubargs || mustdictunpack) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3763 | if (nseen) { |
| 3764 | /* Pack up any trailing positional arguments. */ |
| 3765 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 3766 | nsubargs++; |
| 3767 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3768 | if (nsubargs > 1) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3769 | /* If we ended up with more than one stararg, we need |
| 3770 | to concatenate them into a single sequence. */ |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 3771 | ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3772 | } |
| 3773 | else if (nsubargs == 0) { |
| 3774 | ADDOP_I(c, BUILD_TUPLE, 0); |
| 3775 | } |
| 3776 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 3777 | for (i = 0; i < nkwelts; i++) { |
| 3778 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 3779 | if (kw->arg == NULL) { |
| 3780 | /* A keyword argument unpacking. */ |
| 3781 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3782 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) |
| 3783 | return 0; |
| 3784 | nsubkwargs++; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3785 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3786 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3787 | VISIT(c, expr, kw->value); |
| 3788 | nsubkwargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3789 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3790 | else { |
| 3791 | nseen++; |
| 3792 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3793 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3794 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3795 | /* Pack up any trailing keyword arguments. */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3796 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3797 | return 0; |
| 3798 | nsubkwargs++; |
| 3799 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 3800 | if (nsubkwargs > 1) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3801 | /* Pack it all up */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3802 | ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3803 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3804 | ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); |
| 3805 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3806 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3807 | else if (nkwelts) { |
| 3808 | PyObject *names; |
| 3809 | VISIT_SEQ(c, keyword, keywords); |
| 3810 | names = PyTuple_New(nkwelts); |
| 3811 | if (names == NULL) { |
| 3812 | return 0; |
| 3813 | } |
| 3814 | for (i = 0; i < nkwelts; i++) { |
| 3815 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 3816 | Py_INCREF(kw->arg); |
| 3817 | PyTuple_SET_ITEM(names, i, kw->arg); |
| 3818 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3819 | ADDOP_LOAD_CONST_NEW(c, names); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3820 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 3821 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3822 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 3823 | else { |
| 3824 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 3825 | return 1; |
| 3826 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3829 | |
| 3830 | /* List and set comprehensions and generator expressions work by creating a |
| 3831 | nested function to perform the actual iteration. This means that the |
| 3832 | iteration variables don't leak into the current scope. |
| 3833 | The defined function is called immediately following its definition, with the |
| 3834 | result of that call being the result of the expression. |
| 3835 | The LC/SC version returns the populated container, while the GE version is |
| 3836 | flagged in symtable.c as a generator, so it returns the generator object |
| 3837 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3838 | |
| 3839 | Possible cleanups: |
| 3840 | - iterate over the generator sequence instead of using recursion |
| 3841 | */ |
| 3842 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3843 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3844 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3845 | compiler_comprehension_generator(struct compiler *c, |
| 3846 | asdl_seq *generators, int gen_index, |
| 3847 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3848 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3849 | comprehension_ty gen; |
| 3850 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 3851 | if (gen->is_async) { |
| 3852 | return compiler_async_comprehension_generator( |
| 3853 | c, generators, gen_index, elt, val, type); |
| 3854 | } else { |
| 3855 | return compiler_sync_comprehension_generator( |
| 3856 | c, generators, gen_index, elt, val, type); |
| 3857 | } |
| 3858 | } |
| 3859 | |
| 3860 | static int |
| 3861 | compiler_sync_comprehension_generator(struct compiler *c, |
| 3862 | asdl_seq *generators, int gen_index, |
| 3863 | expr_ty elt, expr_ty val, int type) |
| 3864 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | /* generate code for the iterator, then each of the ifs, |
| 3866 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3868 | comprehension_ty gen; |
| 3869 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3870 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | start = compiler_new_block(c); |
| 3873 | skip = compiler_new_block(c); |
| 3874 | if_cleanup = compiler_new_block(c); |
| 3875 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3876 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3877 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 3878 | anchor == NULL) |
| 3879 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3880 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3881 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3882 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3883 | if (gen_index == 0) { |
| 3884 | /* Receive outermost iter as an implicit argument */ |
| 3885 | c->u->u_argcount = 1; |
| 3886 | ADDOP_I(c, LOAD_FAST, 0); |
| 3887 | } |
| 3888 | else { |
| 3889 | /* Sub-iter - calculate on the fly */ |
| 3890 | VISIT(c, expr, gen->iter); |
| 3891 | ADDOP(c, GET_ITER); |
| 3892 | } |
| 3893 | compiler_use_next_block(c, start); |
| 3894 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 3895 | NEXT_BLOCK(c); |
| 3896 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3898 | /* XXX this needs to be cleaned up...a lot! */ |
| 3899 | n = asdl_seq_LEN(gen->ifs); |
| 3900 | for (i = 0; i < n; i++) { |
| 3901 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3902 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 3903 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3904 | NEXT_BLOCK(c); |
| 3905 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3907 | if (++gen_index < asdl_seq_LEN(generators)) |
| 3908 | if (!compiler_comprehension_generator(c, |
| 3909 | generators, gen_index, |
| 3910 | elt, val, type)) |
| 3911 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3912 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3913 | /* only append after the last for generator */ |
| 3914 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 3915 | /* comprehension specific code */ |
| 3916 | switch (type) { |
| 3917 | case COMP_GENEXP: |
| 3918 | VISIT(c, expr, elt); |
| 3919 | ADDOP(c, YIELD_VALUE); |
| 3920 | ADDOP(c, POP_TOP); |
| 3921 | break; |
| 3922 | case COMP_LISTCOMP: |
| 3923 | VISIT(c, expr, elt); |
| 3924 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 3925 | break; |
| 3926 | case COMP_SETCOMP: |
| 3927 | VISIT(c, expr, elt); |
| 3928 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 3929 | break; |
| 3930 | case COMP_DICTCOMP: |
| 3931 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 3932 | the same. */ |
| 3933 | VISIT(c, expr, val); |
| 3934 | VISIT(c, expr, elt); |
| 3935 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 3936 | break; |
| 3937 | default: |
| 3938 | return 0; |
| 3939 | } |
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 | compiler_use_next_block(c, skip); |
| 3942 | } |
| 3943 | compiler_use_next_block(c, if_cleanup); |
| 3944 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 3945 | compiler_use_next_block(c, anchor); |
| 3946 | |
| 3947 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3951 | compiler_async_comprehension_generator(struct compiler *c, |
| 3952 | asdl_seq *generators, int gen_index, |
| 3953 | expr_ty elt, expr_ty val, int type) |
| 3954 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3955 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 3956 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3957 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 3958 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3959 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3960 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3961 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 3962 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3963 | return 0; |
| 3964 | } |
| 3965 | |
| 3966 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 3967 | |
| 3968 | if (gen_index == 0) { |
| 3969 | /* Receive outermost iter as an implicit argument */ |
| 3970 | c->u->u_argcount = 1; |
| 3971 | ADDOP_I(c, LOAD_FAST, 0); |
| 3972 | } |
| 3973 | else { |
| 3974 | /* Sub-iter - calculate on the fly */ |
| 3975 | VISIT(c, expr, gen->iter); |
| 3976 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3977 | } |
| 3978 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 3979 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3980 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3981 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3982 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3983 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3984 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3985 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 3986 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3987 | |
| 3988 | n = asdl_seq_LEN(gen->ifs); |
| 3989 | for (i = 0; i < n; i++) { |
| 3990 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3991 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 3992 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 3993 | NEXT_BLOCK(c); |
| 3994 | } |
| 3995 | |
| 3996 | if (++gen_index < asdl_seq_LEN(generators)) |
| 3997 | if (!compiler_comprehension_generator(c, |
| 3998 | generators, gen_index, |
| 3999 | elt, val, type)) |
| 4000 | return 0; |
| 4001 | |
| 4002 | /* only append after the last for generator */ |
| 4003 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4004 | /* comprehension specific code */ |
| 4005 | switch (type) { |
| 4006 | case COMP_GENEXP: |
| 4007 | VISIT(c, expr, elt); |
| 4008 | ADDOP(c, YIELD_VALUE); |
| 4009 | ADDOP(c, POP_TOP); |
| 4010 | break; |
| 4011 | case COMP_LISTCOMP: |
| 4012 | VISIT(c, expr, elt); |
| 4013 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4014 | break; |
| 4015 | case COMP_SETCOMP: |
| 4016 | VISIT(c, expr, elt); |
| 4017 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4018 | break; |
| 4019 | case COMP_DICTCOMP: |
| 4020 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 4021 | the same. */ |
| 4022 | VISIT(c, expr, val); |
| 4023 | VISIT(c, expr, elt); |
| 4024 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4025 | break; |
| 4026 | default: |
| 4027 | return 0; |
| 4028 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4029 | } |
| 4030 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4031 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4032 | |
| 4033 | compiler_use_next_block(c, except); |
| 4034 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4035 | |
| 4036 | return 1; |
| 4037 | } |
| 4038 | |
| 4039 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4040 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4041 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4042 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4043 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4044 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4045 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4046 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4047 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4048 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4049 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4050 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4052 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4053 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4054 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4055 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
| 4058 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4059 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4060 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4061 | compiler_error(c, "asynchronous comprehension outside of " |
| 4062 | "an asynchronous function"); |
| 4063 | goto error_in_scope; |
| 4064 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4065 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 | if (type != COMP_GENEXP) { |
| 4067 | int op; |
| 4068 | switch (type) { |
| 4069 | case COMP_LISTCOMP: |
| 4070 | op = BUILD_LIST; |
| 4071 | break; |
| 4072 | case COMP_SETCOMP: |
| 4073 | op = BUILD_SET; |
| 4074 | break; |
| 4075 | case COMP_DICTCOMP: |
| 4076 | op = BUILD_MAP; |
| 4077 | break; |
| 4078 | default: |
| 4079 | PyErr_Format(PyExc_SystemError, |
| 4080 | "unknown comprehension type %d", type); |
| 4081 | goto error_in_scope; |
| 4082 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4084 | ADDOP_I(c, op, 0); |
| 4085 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4086 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4087 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 4088 | val, type)) |
| 4089 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4091 | if (type != COMP_GENEXP) { |
| 4092 | ADDOP(c, RETURN_VALUE); |
| 4093 | } |
| 4094 | |
| 4095 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4096 | qualname = c->u->u_qualname; |
| 4097 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4098 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4099 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4100 | goto error; |
| 4101 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4102 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4103 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4104 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4105 | Py_DECREF(co); |
| 4106 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4107 | VISIT(c, expr, outermost->iter); |
| 4108 | |
| 4109 | if (outermost->is_async) { |
| 4110 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4111 | } else { |
| 4112 | ADDOP(c, GET_ITER); |
| 4113 | } |
| 4114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4115 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4116 | |
| 4117 | if (is_async_generator && type != COMP_GENEXP) { |
| 4118 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4119 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4120 | ADDOP(c, YIELD_FROM); |
| 4121 | } |
| 4122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4123 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4124 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4125 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4126 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4127 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | Py_XDECREF(co); |
| 4129 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4133 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4134 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4135 | static identifier name; |
| 4136 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4137 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4138 | if (!name) |
| 4139 | return 0; |
| 4140 | } |
| 4141 | assert(e->kind == GeneratorExp_kind); |
| 4142 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4143 | e->v.GeneratorExp.generators, |
| 4144 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4145 | } |
| 4146 | |
| 4147 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4148 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4149 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4150 | static identifier name; |
| 4151 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4152 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4153 | if (!name) |
| 4154 | return 0; |
| 4155 | } |
| 4156 | assert(e->kind == ListComp_kind); |
| 4157 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4158 | e->v.ListComp.generators, |
| 4159 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4160 | } |
| 4161 | |
| 4162 | static int |
| 4163 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4165 | static identifier name; |
| 4166 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4167 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4168 | if (!name) |
| 4169 | return 0; |
| 4170 | } |
| 4171 | assert(e->kind == SetComp_kind); |
| 4172 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4173 | e->v.SetComp.generators, |
| 4174 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4175 | } |
| 4176 | |
| 4177 | |
| 4178 | static int |
| 4179 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4181 | static identifier name; |
| 4182 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4183 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4184 | if (!name) |
| 4185 | return 0; |
| 4186 | } |
| 4187 | assert(e->kind == DictComp_kind); |
| 4188 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4189 | e->v.DictComp.generators, |
| 4190 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | |
| 4194 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4195 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4196 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4197 | VISIT(c, expr, k->value); |
| 4198 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4201 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4202 | whether they are true or false. |
| 4203 | |
| 4204 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4205 | */ |
| 4206 | |
| 4207 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4208 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4209 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4210 | if (e->kind == Constant_kind) { |
| 4211 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4212 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4213 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4214 | } |
| 4215 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4216 | |
| 4217 | /* |
| 4218 | Implements the async with statement. |
| 4219 | |
| 4220 | The semantics outlined in that PEP are as follows: |
| 4221 | |
| 4222 | async with EXPR as VAR: |
| 4223 | BLOCK |
| 4224 | |
| 4225 | It is implemented roughly as: |
| 4226 | |
| 4227 | context = EXPR |
| 4228 | exit = context.__aexit__ # not calling it |
| 4229 | value = await context.__aenter__() |
| 4230 | try: |
| 4231 | VAR = value # if VAR present in the syntax |
| 4232 | BLOCK |
| 4233 | finally: |
| 4234 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4235 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4236 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4237 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4238 | if not (await exit(*exc)): |
| 4239 | raise |
| 4240 | */ |
| 4241 | static int |
| 4242 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4243 | { |
| 4244 | basicblock *block, *finally; |
| 4245 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4246 | |
| 4247 | assert(s->kind == AsyncWith_kind); |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4248 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
| 4249 | return compiler_error(c, "'async with' outside async function"); |
| 4250 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4251 | |
| 4252 | block = compiler_new_block(c); |
| 4253 | finally = compiler_new_block(c); |
| 4254 | if (!block || !finally) |
| 4255 | return 0; |
| 4256 | |
| 4257 | /* Evaluate EXPR */ |
| 4258 | VISIT(c, expr, item->context_expr); |
| 4259 | |
| 4260 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4261 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4262 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4263 | ADDOP(c, YIELD_FROM); |
| 4264 | |
| 4265 | ADDOP_JREL(c, SETUP_ASYNC_WITH, finally); |
| 4266 | |
| 4267 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4268 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4269 | if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4270 | return 0; |
| 4271 | } |
| 4272 | |
| 4273 | if (item->optional_vars) { |
| 4274 | VISIT(c, expr, item->optional_vars); |
| 4275 | } |
| 4276 | else { |
| 4277 | /* Discard result from context.__aenter__() */ |
| 4278 | ADDOP(c, POP_TOP); |
| 4279 | } |
| 4280 | |
| 4281 | pos++; |
| 4282 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4283 | /* BLOCK code */ |
| 4284 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4285 | else if (!compiler_async_with(c, s, pos)) |
| 4286 | return 0; |
| 4287 | |
| 4288 | /* End of try block; start the finally block */ |
| 4289 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4290 | ADDOP(c, BEGIN_FINALLY); |
| 4291 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4292 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4293 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4294 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4295 | return 0; |
| 4296 | |
| 4297 | /* Finally block starts; context.__exit__ is on the stack under |
| 4298 | the exception or return information. Just issue our magic |
| 4299 | opcode. */ |
| 4300 | ADDOP(c, WITH_CLEANUP_START); |
| 4301 | |
| 4302 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4303 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4304 | ADDOP(c, YIELD_FROM); |
| 4305 | |
| 4306 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 4307 | |
| 4308 | /* Finally block ends. */ |
| 4309 | ADDOP(c, END_FINALLY); |
| 4310 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4311 | return 1; |
| 4312 | } |
| 4313 | |
| 4314 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4315 | /* |
| 4316 | Implements the with statement from PEP 343. |
| 4317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4318 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4319 | |
| 4320 | with EXPR as VAR: |
| 4321 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4322 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4323 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4324 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4325 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4326 | exit = context.__exit__ # not calling it |
| 4327 | value = context.__enter__() |
| 4328 | try: |
| 4329 | VAR = value # if VAR present in the syntax |
| 4330 | BLOCK |
| 4331 | finally: |
| 4332 | if an exception was raised: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4333 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4334 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4335 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4336 | exit(*exc) |
| 4337 | */ |
| 4338 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4339 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4340 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4341 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4342 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4343 | |
| 4344 | assert(s->kind == With_kind); |
| 4345 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4346 | block = compiler_new_block(c); |
| 4347 | finally = compiler_new_block(c); |
| 4348 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4349 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4350 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4351 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4352 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4353 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4354 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4355 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4356 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4357 | if (!compiler_push_fblock(c, WITH, block, finally)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4358 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4361 | if (item->optional_vars) { |
| 4362 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4363 | } |
| 4364 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4365 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4366 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4369 | pos++; |
| 4370 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4371 | /* BLOCK code */ |
| 4372 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4373 | else if (!compiler_with(c, s, pos)) |
| 4374 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4375 | |
| 4376 | /* End of try block; start the finally block */ |
| 4377 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4378 | ADDOP(c, BEGIN_FINALLY); |
| 4379 | compiler_pop_fblock(c, WITH, block); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4380 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4381 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4382 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4383 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4384 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 4385 | /* Finally block starts; context.__exit__ is on the stack under |
| 4386 | the exception or return information. Just issue our magic |
| 4387 | opcode. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4388 | ADDOP(c, WITH_CLEANUP_START); |
| 4389 | ADDOP(c, WITH_CLEANUP_FINISH); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4390 | |
| 4391 | /* Finally block ends. */ |
| 4392 | ADDOP(c, END_FINALLY); |
| 4393 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4394 | return 1; |
| 4395 | } |
| 4396 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4397 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4398 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | switch (e->kind) { |
| 4401 | case BoolOp_kind: |
| 4402 | return compiler_boolop(c, e); |
| 4403 | case BinOp_kind: |
| 4404 | VISIT(c, expr, e->v.BinOp.left); |
| 4405 | VISIT(c, expr, e->v.BinOp.right); |
| 4406 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 4407 | break; |
| 4408 | case UnaryOp_kind: |
| 4409 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4410 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4411 | break; |
| 4412 | case Lambda_kind: |
| 4413 | return compiler_lambda(c, e); |
| 4414 | case IfExp_kind: |
| 4415 | return compiler_ifexp(c, e); |
| 4416 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4417 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4418 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4419 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4420 | case GeneratorExp_kind: |
| 4421 | return compiler_genexp(c, e); |
| 4422 | case ListComp_kind: |
| 4423 | return compiler_listcomp(c, e); |
| 4424 | case SetComp_kind: |
| 4425 | return compiler_setcomp(c, e); |
| 4426 | case DictComp_kind: |
| 4427 | return compiler_dictcomp(c, e); |
| 4428 | case Yield_kind: |
| 4429 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4430 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4431 | if (e->v.Yield.value) { |
| 4432 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4433 | } |
| 4434 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4435 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4436 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4437 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4438 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4439 | case YieldFrom_kind: |
| 4440 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4441 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4442 | |
| 4443 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4444 | return compiler_error(c, "'yield from' inside async function"); |
| 4445 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4446 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4447 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4448 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4449 | ADDOP(c, YIELD_FROM); |
| 4450 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4451 | case Await_kind: |
| 4452 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4453 | return compiler_error(c, "'await' outside function"); |
| 4454 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4455 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
| 4456 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4457 | return compiler_error(c, "'await' outside async function"); |
| 4458 | |
| 4459 | VISIT(c, expr, e->v.Await.value); |
| 4460 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4461 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4462 | ADDOP(c, YIELD_FROM); |
| 4463 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4464 | case Compare_kind: |
| 4465 | return compiler_compare(c, e); |
| 4466 | case Call_kind: |
| 4467 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4468 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4469 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4470 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4471 | case JoinedStr_kind: |
| 4472 | return compiler_joined_str(c, e); |
| 4473 | case FormattedValue_kind: |
| 4474 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4475 | /* The following exprs can be assignment targets. */ |
| 4476 | case Attribute_kind: |
| 4477 | if (e->v.Attribute.ctx != AugStore) |
| 4478 | VISIT(c, expr, e->v.Attribute.value); |
| 4479 | switch (e->v.Attribute.ctx) { |
| 4480 | case AugLoad: |
| 4481 | ADDOP(c, DUP_TOP); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4482 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4483 | case Load: |
| 4484 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4485 | break; |
| 4486 | case AugStore: |
| 4487 | ADDOP(c, ROT_TWO); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4488 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | case Store: |
| 4490 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4491 | break; |
| 4492 | case Del: |
| 4493 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4494 | break; |
| 4495 | case Param: |
| 4496 | default: |
| 4497 | PyErr_SetString(PyExc_SystemError, |
| 4498 | "param invalid in attribute expression"); |
| 4499 | return 0; |
| 4500 | } |
| 4501 | break; |
| 4502 | case Subscript_kind: |
| 4503 | switch (e->v.Subscript.ctx) { |
| 4504 | case AugLoad: |
| 4505 | VISIT(c, expr, e->v.Subscript.value); |
| 4506 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 4507 | break; |
| 4508 | case Load: |
| 4509 | VISIT(c, expr, e->v.Subscript.value); |
| 4510 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 4511 | break; |
| 4512 | case AugStore: |
| 4513 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 4514 | break; |
| 4515 | case Store: |
| 4516 | VISIT(c, expr, e->v.Subscript.value); |
| 4517 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 4518 | break; |
| 4519 | case Del: |
| 4520 | VISIT(c, expr, e->v.Subscript.value); |
| 4521 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 4522 | break; |
| 4523 | case Param: |
| 4524 | default: |
| 4525 | PyErr_SetString(PyExc_SystemError, |
| 4526 | "param invalid in subscript expression"); |
| 4527 | return 0; |
| 4528 | } |
| 4529 | break; |
| 4530 | case Starred_kind: |
| 4531 | switch (e->v.Starred.ctx) { |
| 4532 | case Store: |
| 4533 | /* In all legitimate cases, the Starred node was already replaced |
| 4534 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 4535 | return compiler_error(c, |
| 4536 | "starred assignment target must be in a list or tuple"); |
| 4537 | default: |
| 4538 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4539 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4540 | } |
| 4541 | break; |
| 4542 | case Name_kind: |
| 4543 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 4544 | /* child nodes of List and Tuple will have expr_context set */ |
| 4545 | case List_kind: |
| 4546 | return compiler_list(c, e); |
| 4547 | case Tuple_kind: |
| 4548 | return compiler_tuple(c, e); |
| 4549 | } |
| 4550 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4554 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 4555 | { |
| 4556 | /* If expr e has a different line number than the last expr/stmt, |
| 4557 | set a new line number for the next instruction. |
| 4558 | */ |
| 4559 | int old_lineno = c->u->u_lineno; |
| 4560 | int old_col_offset = c->u->u_col_offset; |
| 4561 | if (e->lineno != c->u->u_lineno) { |
| 4562 | c->u->u_lineno = e->lineno; |
| 4563 | c->u->u_lineno_set = 0; |
| 4564 | } |
| 4565 | /* Updating the column offset is always harmless. */ |
| 4566 | c->u->u_col_offset = e->col_offset; |
| 4567 | |
| 4568 | int res = compiler_visit_expr1(c, e); |
| 4569 | |
| 4570 | if (old_lineno != c->u->u_lineno) { |
| 4571 | c->u->u_lineno = old_lineno; |
| 4572 | c->u->u_lineno_set = 0; |
| 4573 | } |
| 4574 | c->u->u_col_offset = old_col_offset; |
| 4575 | return res; |
| 4576 | } |
| 4577 | |
| 4578 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4579 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 4580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | expr_ty e = s->v.AugAssign.target; |
| 4582 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4583 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4584 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | switch (e->kind) { |
| 4587 | case Attribute_kind: |
| 4588 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
| 4589 | AugLoad, e->lineno, e->col_offset, c->c_arena); |
| 4590 | if (auge == NULL) |
| 4591 | return 0; |
| 4592 | VISIT(c, expr, auge); |
| 4593 | VISIT(c, expr, s->v.AugAssign.value); |
| 4594 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4595 | auge->v.Attribute.ctx = AugStore; |
| 4596 | VISIT(c, expr, auge); |
| 4597 | break; |
| 4598 | case Subscript_kind: |
| 4599 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
| 4600 | AugLoad, e->lineno, e->col_offset, c->c_arena); |
| 4601 | if (auge == NULL) |
| 4602 | return 0; |
| 4603 | VISIT(c, expr, auge); |
| 4604 | VISIT(c, expr, s->v.AugAssign.value); |
| 4605 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4606 | auge->v.Subscript.ctx = AugStore; |
| 4607 | VISIT(c, expr, auge); |
| 4608 | break; |
| 4609 | case Name_kind: |
| 4610 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 4611 | return 0; |
| 4612 | VISIT(c, expr, s->v.AugAssign.value); |
| 4613 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 4614 | return compiler_nameop(c, e->v.Name.id, Store); |
| 4615 | default: |
| 4616 | PyErr_Format(PyExc_SystemError, |
| 4617 | "invalid node type (%d) for augmented assignment", |
| 4618 | e->kind); |
| 4619 | return 0; |
| 4620 | } |
| 4621 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 4625 | check_ann_expr(struct compiler *c, expr_ty e) |
| 4626 | { |
| 4627 | VISIT(c, expr, e); |
| 4628 | ADDOP(c, POP_TOP); |
| 4629 | return 1; |
| 4630 | } |
| 4631 | |
| 4632 | static int |
| 4633 | check_annotation(struct compiler *c, stmt_ty s) |
| 4634 | { |
| 4635 | /* Annotations are only evaluated in a module or class. */ |
| 4636 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 4637 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 4638 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 4639 | } |
| 4640 | return 1; |
| 4641 | } |
| 4642 | |
| 4643 | static int |
| 4644 | check_ann_slice(struct compiler *c, slice_ty sl) |
| 4645 | { |
| 4646 | switch(sl->kind) { |
| 4647 | case Index_kind: |
| 4648 | return check_ann_expr(c, sl->v.Index.value); |
| 4649 | case Slice_kind: |
| 4650 | if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { |
| 4651 | return 0; |
| 4652 | } |
| 4653 | if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { |
| 4654 | return 0; |
| 4655 | } |
| 4656 | if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { |
| 4657 | return 0; |
| 4658 | } |
| 4659 | break; |
| 4660 | default: |
| 4661 | PyErr_SetString(PyExc_SystemError, |
| 4662 | "unexpected slice kind"); |
| 4663 | return 0; |
| 4664 | } |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
| 4668 | static int |
| 4669 | check_ann_subscr(struct compiler *c, slice_ty sl) |
| 4670 | { |
| 4671 | /* We check that everything in a subscript is defined at runtime. */ |
| 4672 | Py_ssize_t i, n; |
| 4673 | |
| 4674 | switch (sl->kind) { |
| 4675 | case Index_kind: |
| 4676 | case Slice_kind: |
| 4677 | if (!check_ann_slice(c, sl)) { |
| 4678 | return 0; |
| 4679 | } |
| 4680 | break; |
| 4681 | case ExtSlice_kind: |
| 4682 | n = asdl_seq_LEN(sl->v.ExtSlice.dims); |
| 4683 | for (i = 0; i < n; i++) { |
| 4684 | slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i); |
| 4685 | switch (subsl->kind) { |
| 4686 | case Index_kind: |
| 4687 | case Slice_kind: |
| 4688 | if (!check_ann_slice(c, subsl)) { |
| 4689 | return 0; |
| 4690 | } |
| 4691 | break; |
| 4692 | case ExtSlice_kind: |
| 4693 | default: |
| 4694 | PyErr_SetString(PyExc_SystemError, |
| 4695 | "extended slice invalid in nested slice"); |
| 4696 | return 0; |
| 4697 | } |
| 4698 | } |
| 4699 | break; |
| 4700 | default: |
| 4701 | PyErr_Format(PyExc_SystemError, |
| 4702 | "invalid subscript kind %d", sl->kind); |
| 4703 | return 0; |
| 4704 | } |
| 4705 | return 1; |
| 4706 | } |
| 4707 | |
| 4708 | static int |
| 4709 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 4710 | { |
| 4711 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 4712 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 4713 | |
| 4714 | assert(s->kind == AnnAssign_kind); |
| 4715 | |
| 4716 | /* We perform the actual assignment first. */ |
| 4717 | if (s->v.AnnAssign.value) { |
| 4718 | VISIT(c, expr, s->v.AnnAssign.value); |
| 4719 | VISIT(c, expr, targ); |
| 4720 | } |
| 4721 | switch (targ->kind) { |
| 4722 | case Name_kind: |
| 4723 | /* If we have a simple name in a module or class, store annotation. */ |
| 4724 | if (s->v.AnnAssign.simple && |
| 4725 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 4726 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 4727 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 4728 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 4729 | } |
| 4730 | else { |
| 4731 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 4732 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 4733 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 4734 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4735 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 4736 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 4737 | } |
| 4738 | break; |
| 4739 | case Attribute_kind: |
| 4740 | if (!s->v.AnnAssign.value && |
| 4741 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 4742 | return 0; |
| 4743 | } |
| 4744 | break; |
| 4745 | case Subscript_kind: |
| 4746 | if (!s->v.AnnAssign.value && |
| 4747 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 4748 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 4749 | return 0; |
| 4750 | } |
| 4751 | break; |
| 4752 | default: |
| 4753 | PyErr_Format(PyExc_SystemError, |
| 4754 | "invalid node type (%d) for annotated assignment", |
| 4755 | targ->kind); |
| 4756 | return 0; |
| 4757 | } |
| 4758 | /* Annotation is evaluated last. */ |
| 4759 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 4760 | return 0; |
| 4761 | } |
| 4762 | return 1; |
| 4763 | } |
| 4764 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4765 | /* Raises a SyntaxError and returns 0. |
| 4766 | If something goes wrong, a different exception may be raised. |
| 4767 | */ |
| 4768 | |
| 4769 | static int |
| 4770 | compiler_error(struct compiler *c, const char *errstr) |
| 4771 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 4772 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4773 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4774 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 4775 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4776 | if (!loc) { |
| 4777 | Py_INCREF(Py_None); |
| 4778 | loc = Py_None; |
| 4779 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 4780 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 4781 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4782 | if (!u) |
| 4783 | goto exit; |
| 4784 | v = Py_BuildValue("(zO)", errstr, u); |
| 4785 | if (!v) |
| 4786 | goto exit; |
| 4787 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4788 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4789 | Py_DECREF(loc); |
| 4790 | Py_XDECREF(u); |
| 4791 | Py_XDECREF(v); |
| 4792 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4796 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 4797 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4799 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4801 | /* XXX this code is duplicated */ |
| 4802 | switch (ctx) { |
| 4803 | case AugLoad: /* fall through to Load */ |
| 4804 | case Load: op = BINARY_SUBSCR; break; |
| 4805 | case AugStore:/* fall through to Store */ |
| 4806 | case Store: op = STORE_SUBSCR; break; |
| 4807 | case Del: op = DELETE_SUBSCR; break; |
| 4808 | case Param: |
| 4809 | PyErr_Format(PyExc_SystemError, |
| 4810 | "invalid %s kind %d in subscript\n", |
| 4811 | kind, ctx); |
| 4812 | return 0; |
| 4813 | } |
| 4814 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 4815 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4816 | } |
| 4817 | else if (ctx == AugStore) { |
| 4818 | ADDOP(c, ROT_THREE); |
| 4819 | } |
| 4820 | ADDOP(c, op); |
| 4821 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
| 4824 | static int |
| 4825 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 4826 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4827 | int n = 2; |
| 4828 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4830 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 4831 | if (s->v.Slice.lower) { |
| 4832 | VISIT(c, expr, s->v.Slice.lower); |
| 4833 | } |
| 4834 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4835 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4836 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | if (s->v.Slice.upper) { |
| 4839 | VISIT(c, expr, s->v.Slice.upper); |
| 4840 | } |
| 4841 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4842 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4843 | } |
| 4844 | |
| 4845 | if (s->v.Slice.step) { |
| 4846 | n++; |
| 4847 | VISIT(c, expr, s->v.Slice.step); |
| 4848 | } |
| 4849 | ADDOP_I(c, BUILD_SLICE, n); |
| 4850 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4851 | } |
| 4852 | |
| 4853 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4854 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 4855 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4856 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4857 | switch (s->kind) { |
| 4858 | case Slice_kind: |
| 4859 | return compiler_slice(c, s, ctx); |
| 4860 | case Index_kind: |
| 4861 | VISIT(c, expr, s->v.Index.value); |
| 4862 | break; |
| 4863 | case ExtSlice_kind: |
| 4864 | default: |
| 4865 | PyErr_SetString(PyExc_SystemError, |
| 4866 | "extended slice invalid in nested slice"); |
| 4867 | return 0; |
| 4868 | } |
| 4869 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4872 | static int |
| 4873 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 4874 | { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 4875 | const char * kindname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4876 | switch (s->kind) { |
| 4877 | case Index_kind: |
| 4878 | kindname = "index"; |
| 4879 | if (ctx != AugStore) { |
| 4880 | VISIT(c, expr, s->v.Index.value); |
| 4881 | } |
| 4882 | break; |
| 4883 | case Slice_kind: |
| 4884 | kindname = "slice"; |
| 4885 | if (ctx != AugStore) { |
| 4886 | if (!compiler_slice(c, s, ctx)) |
| 4887 | return 0; |
| 4888 | } |
| 4889 | break; |
| 4890 | case ExtSlice_kind: |
| 4891 | kindname = "extended slice"; |
| 4892 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4893 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4894 | for (i = 0; i < n; i++) { |
| 4895 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 4896 | s->v.ExtSlice.dims, i); |
| 4897 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 4898 | return 0; |
| 4899 | } |
| 4900 | ADDOP_I(c, BUILD_TUPLE, n); |
| 4901 | } |
| 4902 | break; |
| 4903 | default: |
| 4904 | PyErr_Format(PyExc_SystemError, |
| 4905 | "invalid subscript kind %d", s->kind); |
| 4906 | return 0; |
| 4907 | } |
| 4908 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4911 | /* End of the compiler section, beginning of the assembler section */ |
| 4912 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4913 | /* do depth-first search of basic block graph, starting with block. |
| 4914 | post records the block indices in post-order. |
| 4915 | |
| 4916 | XXX must handle implicit jumps from one block to next |
| 4917 | */ |
| 4918 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4919 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4920 | PyObject *a_bytecode; /* string containing bytecode */ |
| 4921 | int a_offset; /* offset into bytecode */ |
| 4922 | int a_nblocks; /* number of reachable blocks */ |
| 4923 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 4924 | PyObject *a_lnotab; /* string containing lnotab */ |
| 4925 | int a_lnotab_off; /* offset into lnotab */ |
| 4926 | int a_lineno; /* last lineno of emitted instruction */ |
| 4927 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4928 | }; |
| 4929 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4930 | static void |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4931 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4932 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4933 | int i, j; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4934 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4935 | /* Get rid of recursion for normal control flow. |
| 4936 | Since the number of blocks is limited, unused space in a_postorder |
| 4937 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 4938 | blocks. */ |
| 4939 | for (j = end; b && !b->b_seen; b = b->b_next) { |
| 4940 | b->b_seen = 1; |
| 4941 | assert(a->a_nblocks < j); |
| 4942 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4944 | while (j < end) { |
| 4945 | b = a->a_postorder[j++]; |
| 4946 | for (i = 0; i < b->b_iused; i++) { |
| 4947 | struct instr *instr = &b->b_instr[i]; |
| 4948 | if (instr->i_jrel || instr->i_jabs) |
| 4949 | dfs(c, instr->i_target, a, j); |
| 4950 | } |
| 4951 | assert(a->a_nblocks < j); |
| 4952 | a->a_postorder[a->a_nblocks++] = b; |
| 4953 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4954 | } |
| 4955 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4956 | Py_LOCAL_INLINE(void) |
| 4957 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4958 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4959 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4960 | if (b->b_startdepth < depth) { |
| 4961 | assert(b->b_startdepth < 0); |
| 4962 | b->b_startdepth = depth; |
| 4963 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 4964 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | /* Find the flow path that needs the largest stack. We assume that |
| 4968 | * cycles in the flow graph have no net effect on the stack depth. |
| 4969 | */ |
| 4970 | static int |
| 4971 | stackdepth(struct compiler *c) |
| 4972 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4973 | basicblock *b, *entryblock = NULL; |
| 4974 | basicblock **stack, **sp; |
| 4975 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4976 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4977 | b->b_startdepth = INT_MIN; |
| 4978 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4979 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4980 | } |
| 4981 | if (!entryblock) |
| 4982 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 4983 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 4984 | if (!stack) { |
| 4985 | PyErr_NoMemory(); |
| 4986 | return -1; |
| 4987 | } |
| 4988 | |
| 4989 | sp = stack; |
| 4990 | stackdepth_push(&sp, entryblock, 0); |
| 4991 | while (sp != stack) { |
| 4992 | b = *--sp; |
| 4993 | int depth = b->b_startdepth; |
| 4994 | assert(depth >= 0); |
| 4995 | basicblock *next = b->b_next; |
| 4996 | for (int i = 0; i < b->b_iused; i++) { |
| 4997 | struct instr *instr = &b->b_instr[i]; |
| 4998 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 4999 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5000 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5001 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5002 | } |
| 5003 | int new_depth = depth + effect; |
| 5004 | if (new_depth > maxdepth) { |
| 5005 | maxdepth = new_depth; |
| 5006 | } |
| 5007 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5008 | if (instr->i_jrel || instr->i_jabs) { |
| 5009 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5010 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5011 | int target_depth = depth + effect; |
| 5012 | if (target_depth > maxdepth) { |
| 5013 | maxdepth = target_depth; |
| 5014 | } |
| 5015 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5016 | if (instr->i_opcode == CALL_FINALLY) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5017 | assert(instr->i_target->b_startdepth >= 0); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5018 | assert(instr->i_target->b_startdepth >= target_depth); |
| 5019 | depth = new_depth; |
| 5020 | continue; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5021 | } |
| 5022 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5023 | } |
| 5024 | depth = new_depth; |
| 5025 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5026 | instr->i_opcode == JUMP_FORWARD || |
| 5027 | instr->i_opcode == RETURN_VALUE || |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5028 | instr->i_opcode == RAISE_VARARGS) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5029 | { |
| 5030 | /* remaining code is dead */ |
| 5031 | next = NULL; |
| 5032 | break; |
| 5033 | } |
| 5034 | } |
| 5035 | if (next != NULL) { |
| 5036 | stackdepth_push(&sp, next, depth); |
| 5037 | } |
| 5038 | } |
| 5039 | PyObject_Free(stack); |
| 5040 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5041 | } |
| 5042 | |
| 5043 | static int |
| 5044 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5045 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5046 | memset(a, 0, sizeof(struct assembler)); |
| 5047 | a->a_lineno = firstlineno; |
| 5048 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5049 | if (!a->a_bytecode) |
| 5050 | return 0; |
| 5051 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5052 | if (!a->a_lnotab) |
| 5053 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5054 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5055 | PyErr_NoMemory(); |
| 5056 | return 0; |
| 5057 | } |
| 5058 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 5059 | sizeof(basicblock *) * nblocks); |
| 5060 | if (!a->a_postorder) { |
| 5061 | PyErr_NoMemory(); |
| 5062 | return 0; |
| 5063 | } |
| 5064 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5065 | } |
| 5066 | |
| 5067 | static void |
| 5068 | assemble_free(struct assembler *a) |
| 5069 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5070 | Py_XDECREF(a->a_bytecode); |
| 5071 | Py_XDECREF(a->a_lnotab); |
| 5072 | if (a->a_postorder) |
| 5073 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5074 | } |
| 5075 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5076 | static int |
| 5077 | blocksize(basicblock *b) |
| 5078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5079 | int i; |
| 5080 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5082 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5083 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5085 | } |
| 5086 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5087 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5088 | the instruction's bytecode offset and line number. See |
| 5089 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5090 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5091 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5092 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5094 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5095 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5097 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5098 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5099 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5100 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5101 | assert(d_bytecode >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5103 | if(d_bytecode == 0 && d_lineno == 0) |
| 5104 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5106 | if (d_bytecode > 255) { |
| 5107 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5108 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5109 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5110 | if (nbytes >= len) { |
| 5111 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5112 | len = nbytes; |
| 5113 | else if (len <= INT_MAX / 2) |
| 5114 | len *= 2; |
| 5115 | else { |
| 5116 | PyErr_NoMemory(); |
| 5117 | return 0; |
| 5118 | } |
| 5119 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5120 | return 0; |
| 5121 | } |
| 5122 | lnotab = (unsigned char *) |
| 5123 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5124 | for (j = 0; j < ncodes; j++) { |
| 5125 | *lnotab++ = 255; |
| 5126 | *lnotab++ = 0; |
| 5127 | } |
| 5128 | d_bytecode -= ncodes * 255; |
| 5129 | a->a_lnotab_off += ncodes * 2; |
| 5130 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5131 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5132 | |
| 5133 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5134 | int j, nbytes, ncodes, k; |
| 5135 | if (d_lineno < 0) { |
| 5136 | k = -128; |
| 5137 | /* use division on positive numbers */ |
| 5138 | ncodes = (-d_lineno) / 128; |
| 5139 | } |
| 5140 | else { |
| 5141 | k = 127; |
| 5142 | ncodes = d_lineno / 127; |
| 5143 | } |
| 5144 | d_lineno -= ncodes * k; |
| 5145 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5146 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5147 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5148 | if (nbytes >= len) { |
| 5149 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5150 | len = nbytes; |
| 5151 | else if (len <= INT_MAX / 2) |
| 5152 | len *= 2; |
| 5153 | else { |
| 5154 | PyErr_NoMemory(); |
| 5155 | return 0; |
| 5156 | } |
| 5157 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5158 | return 0; |
| 5159 | } |
| 5160 | lnotab = (unsigned char *) |
| 5161 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5162 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5163 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | d_bytecode = 0; |
| 5165 | for (j = 1; j < ncodes; j++) { |
| 5166 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5167 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5168 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5169 | a->a_lnotab_off += ncodes * 2; |
| 5170 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5171 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5172 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5173 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5174 | if (a->a_lnotab_off + 2 >= len) { |
| 5175 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5176 | return 0; |
| 5177 | } |
| 5178 | lnotab = (unsigned char *) |
| 5179 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5181 | a->a_lnotab_off += 2; |
| 5182 | if (d_bytecode) { |
| 5183 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5184 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5185 | } |
| 5186 | else { /* First line of a block; def stmt, etc. */ |
| 5187 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5188 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5189 | } |
| 5190 | a->a_lineno = i->i_lineno; |
| 5191 | a->a_lineno_off = a->a_offset; |
| 5192 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5193 | } |
| 5194 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5195 | /* assemble_emit() |
| 5196 | Extend the bytecode with a new instruction. |
| 5197 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5198 | */ |
| 5199 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5200 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5201 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5202 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5203 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5205 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5206 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5207 | arg = i->i_oparg; |
| 5208 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5209 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5210 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5211 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5212 | if (len > PY_SSIZE_T_MAX / 2) |
| 5213 | return 0; |
| 5214 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5215 | return 0; |
| 5216 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5217 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5218 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5219 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5223 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5224 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5225 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5226 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5227 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5230 | /* Compute the size of each block and fixup jump args. |
| 5231 | Replace block pointer with position in bytecode. */ |
| 5232 | do { |
| 5233 | totsize = 0; |
| 5234 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5235 | b = a->a_postorder[i]; |
| 5236 | bsize = blocksize(b); |
| 5237 | b->b_offset = totsize; |
| 5238 | totsize += bsize; |
| 5239 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5240 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5241 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5242 | bsize = b->b_offset; |
| 5243 | for (i = 0; i < b->b_iused; i++) { |
| 5244 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5245 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5246 | /* Relative jumps are computed relative to |
| 5247 | the instruction pointer after fetching |
| 5248 | the jump instruction. |
| 5249 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5250 | bsize += isize; |
| 5251 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5252 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5253 | if (instr->i_jrel) { |
| 5254 | instr->i_oparg -= bsize; |
| 5255 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5256 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5257 | if (instrsize(instr->i_oparg) != isize) { |
| 5258 | extended_arg_recompile = 1; |
| 5259 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5260 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5261 | } |
| 5262 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5263 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5264 | /* XXX: This is an awful hack that could hurt performance, but |
| 5265 | on the bright side it should work until we come up |
| 5266 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5268 | The issue is that in the first loop blocksize() is called |
| 5269 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5270 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5271 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5274 | The only EXTENDED_ARGs that could be popping up are |
| 5275 | ones in jump instructions. So this should converge |
| 5276 | fairly quickly. |
| 5277 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5278 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5279 | } |
| 5280 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5281 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5282 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5283 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5284 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5285 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5286 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5287 | tuple = PyTuple_New(size); |
| 5288 | if (tuple == NULL) |
| 5289 | return NULL; |
| 5290 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5291 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5292 | Py_INCREF(k); |
| 5293 | assert((i - offset) < size); |
| 5294 | assert((i - offset) >= 0); |
| 5295 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5296 | } |
| 5297 | return tuple; |
| 5298 | } |
| 5299 | |
| 5300 | static PyObject * |
| 5301 | consts_dict_keys_inorder(PyObject *dict) |
| 5302 | { |
| 5303 | PyObject *consts, *k, *v; |
| 5304 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5305 | |
| 5306 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5307 | if (consts == NULL) |
| 5308 | return NULL; |
| 5309 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5310 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5311 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5312 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5313 | * the object we want is always second. */ |
| 5314 | if (PyTuple_CheckExact(k)) { |
| 5315 | k = PyTuple_GET_ITEM(k, 1); |
| 5316 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5317 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5318 | assert(i < size); |
| 5319 | assert(i >= 0); |
| 5320 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5321 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5322 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5325 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5326 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5327 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5328 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5329 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5330 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5331 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5332 | if (ste->ste_nested) |
| 5333 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5334 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5335 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5336 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5337 | flags |= CO_COROUTINE; |
| 5338 | if (ste->ste_generator && ste->ste_coroutine) |
| 5339 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5340 | if (ste->ste_varargs) |
| 5341 | flags |= CO_VARARGS; |
| 5342 | if (ste->ste_varkeywords) |
| 5343 | flags |= CO_VARKEYWORDS; |
| 5344 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5346 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5347 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5349 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5350 | } |
| 5351 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5352 | static PyCodeObject * |
| 5353 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5355 | PyObject *tmp; |
| 5356 | PyCodeObject *co = NULL; |
| 5357 | PyObject *consts = NULL; |
| 5358 | PyObject *names = NULL; |
| 5359 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5360 | PyObject *name = NULL; |
| 5361 | PyObject *freevars = NULL; |
| 5362 | PyObject *cellvars = NULL; |
| 5363 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5364 | Py_ssize_t nlocals; |
| 5365 | int nlocals_int; |
| 5366 | int flags; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5367 | int argcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5368 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5369 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5370 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5371 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5372 | if (!consts || !names || !varnames) |
| 5373 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5375 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5376 | if (!cellvars) |
| 5377 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5378 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5379 | if (!freevars) |
| 5380 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5381 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5382 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5383 | assert(nlocals < INT_MAX); |
| 5384 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5385 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5386 | flags = compute_code_flags(c); |
| 5387 | if (flags < 0) |
| 5388 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5391 | if (!bytecode) |
| 5392 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5395 | if (!tmp) |
| 5396 | goto error; |
| 5397 | Py_DECREF(consts); |
| 5398 | consts = tmp; |
| 5399 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5400 | argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
| 5401 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5402 | maxdepth = stackdepth(c); |
| 5403 | if (maxdepth < 0) { |
| 5404 | goto error; |
| 5405 | } |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5406 | co = PyCode_New(argcount, kwonlyargcount, |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5407 | nlocals_int, maxdepth, flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5408 | bytecode, consts, names, varnames, |
| 5409 | freevars, cellvars, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5410 | c->c_filename, c->u->u_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | c->u->u_firstlineno, |
| 5412 | a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5413 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5414 | Py_XDECREF(consts); |
| 5415 | Py_XDECREF(names); |
| 5416 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | Py_XDECREF(name); |
| 5418 | Py_XDECREF(freevars); |
| 5419 | Py_XDECREF(cellvars); |
| 5420 | Py_XDECREF(bytecode); |
| 5421 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5422 | } |
| 5423 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5424 | |
| 5425 | /* For debugging purposes only */ |
| 5426 | #if 0 |
| 5427 | static void |
| 5428 | dump_instr(const struct instr *i) |
| 5429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5431 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5432 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5433 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5434 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5435 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5436 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5437 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5438 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5439 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| 5442 | static void |
| 5443 | dump_basicblock(const basicblock *b) |
| 5444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5445 | const char *seen = b->b_seen ? "seen " : ""; |
| 5446 | const char *b_return = b->b_return ? "return " : ""; |
| 5447 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5448 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5449 | if (b->b_instr) { |
| 5450 | int i; |
| 5451 | for (i = 0; i < b->b_iused; i++) { |
| 5452 | fprintf(stderr, " [%02d] ", i); |
| 5453 | dump_instr(b->b_instr + i); |
| 5454 | } |
| 5455 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5456 | } |
| 5457 | #endif |
| 5458 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5459 | static PyCodeObject * |
| 5460 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5462 | basicblock *b, *entryblock; |
| 5463 | struct assembler a; |
| 5464 | int i, j, nblocks; |
| 5465 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5466 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5467 | /* Make sure every block that falls off the end returns None. |
| 5468 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5469 | block ends with a jump or return b_next shouldn't set. |
| 5470 | */ |
| 5471 | if (!c->u->u_curblock->b_return) { |
| 5472 | NEXT_BLOCK(c); |
| 5473 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5474 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5475 | ADDOP(c, RETURN_VALUE); |
| 5476 | } |
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 | nblocks = 0; |
| 5479 | entryblock = NULL; |
| 5480 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5481 | nblocks++; |
| 5482 | entryblock = b; |
| 5483 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5484 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5485 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5486 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5487 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5488 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 5489 | else |
| 5490 | c->u->u_firstlineno = 1; |
| 5491 | } |
| 5492 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5493 | goto error; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5494 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5496 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5497 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5499 | /* Emit code in reverse postorder from dfs. */ |
| 5500 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 5501 | b = a.a_postorder[i]; |
| 5502 | for (j = 0; j < b->b_iused; j++) |
| 5503 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5504 | goto error; |
| 5505 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5507 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5508 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5509 | 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] | 5510 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5513 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5514 | assemble_free(&a); |
| 5515 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5516 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5517 | |
| 5518 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5519 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5520 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 5521 | PyArena *arena) |
| 5522 | { |
| 5523 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 5524 | } |