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