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