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" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 8e793d9 | 1997-03-03 19:13:14 +0000 | [diff] [blame] | 33 | int Py_OptimizeFlag = 0; |
| 34 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 35 | #define DEFAULT_BLOCK_SIZE 16 |
| 36 | #define DEFAULT_BLOCKS 8 |
| 37 | #define DEFAULT_CODE_SIZE 128 |
| 38 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 39 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 40 | #define COMP_GENEXP 0 |
| 41 | #define COMP_LISTCOMP 1 |
| 42 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 43 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 44 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 45 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | unsigned i_jabs : 1; |
| 47 | unsigned i_jrel : 1; |
| 48 | unsigned i_hasarg : 1; |
| 49 | unsigned char i_opcode; |
| 50 | int i_oparg; |
| 51 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 52 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 55 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 56 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 57 | reverse order that the block are allocated. b_list points to the next |
| 58 | 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] | 59 | struct basicblock_ *b_list; |
| 60 | /* number of instructions used */ |
| 61 | int b_iused; |
| 62 | /* length of instruction array (b_instr) */ |
| 63 | int b_ialloc; |
| 64 | /* pointer to an array of instructions, initially NULL */ |
| 65 | struct instr *b_instr; |
| 66 | /* If b_next is non-NULL, it is a pointer to the next |
| 67 | block reached by normal control flow. */ |
| 68 | struct basicblock_ *b_next; |
| 69 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 70 | unsigned b_seen : 1; |
| 71 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 72 | unsigned b_return : 1; |
| 73 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 74 | int b_startdepth; |
| 75 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 76 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 77 | } basicblock; |
| 78 | |
| 79 | /* fblockinfo tracks the current frame block. |
| 80 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 81 | A frame block is used to handle loops, try/except, and try/finally. |
| 82 | It's called a frame block to distinguish it from a basic block in the |
| 83 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 84 | */ |
| 85 | |
| 86 | enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END }; |
| 87 | |
| 88 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | enum fblocktype fb_type; |
| 90 | basicblock *fb_block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 93 | enum { |
| 94 | COMPILER_SCOPE_MODULE, |
| 95 | COMPILER_SCOPE_CLASS, |
| 96 | COMPILER_SCOPE_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 97 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 98 | COMPILER_SCOPE_COMPREHENSION, |
| 99 | }; |
| 100 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 101 | /* The following items change on entry and exit of code blocks. |
| 102 | They must be saved and restored when returning to a block. |
| 103 | */ |
| 104 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 108 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 109 | int u_scope_type; |
| 110 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | /* The following fields are dicts that map objects to |
| 112 | the index of them in co_XXX. The index is used as |
| 113 | the argument for opcodes that refer to those collections. |
| 114 | */ |
| 115 | PyObject *u_consts; /* all constants */ |
| 116 | PyObject *u_names; /* all names */ |
| 117 | PyObject *u_varnames; /* local variables */ |
| 118 | PyObject *u_cellvars; /* cell variables */ |
| 119 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | int u_argcount; /* number of arguments for block */ |
| 124 | int u_kwonlyargcount; /* number of keyword only arguments for block */ |
| 125 | /* Pointer to the most recently allocated block. By following b_list |
| 126 | members, you can reach all early allocated blocks. */ |
| 127 | basicblock *u_blocks; |
| 128 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 129 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 130 | int u_nfblocks; |
| 131 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | int u_firstlineno; /* the first lineno of the block */ |
| 134 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 135 | int u_col_offset; /* the offset of the current stmt */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | int u_lineno_set; /* boolean to indicate whether instr |
| 137 | has been generated with current lineno */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 141 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 142 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | 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] | 144 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 145 | |
| 146 | Note that we don't track recursion levels during compilation - the |
| 147 | task of detecting and rejecting excessive levels of nesting is |
| 148 | handled by the symbol analysis pass. |
| 149 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 150 | */ |
| 151 | |
| 152 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 153 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | struct symtable *c_st; |
| 155 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 156 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 157 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 158 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | int c_interactive; /* true if in interactive mode */ |
| 160 | int c_nestlevel; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | struct compiler_unit *u; /* compiler state for current block */ |
| 163 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 164 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 167 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 168 | static void compiler_free(struct compiler *); |
| 169 | static basicblock *compiler_new_block(struct compiler *); |
| 170 | static int compiler_next_instr(struct compiler *, basicblock *); |
| 171 | static int compiler_addop(struct compiler *, int); |
| 172 | static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 173 | static int compiler_addop_i(struct compiler *, Py_ssize_t, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 174 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 175 | static basicblock *compiler_use_new_block(struct compiler *); |
| 176 | static int compiler_error(struct compiler *, const char *); |
| 177 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 178 | |
| 179 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 180 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 181 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 182 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 183 | static int compiler_augassign(struct compiler *, stmt_ty); |
| 184 | static int compiler_visit_slice(struct compiler *, slice_ty, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | expr_context_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 186 | |
| 187 | static int compiler_push_fblock(struct compiler *, enum fblocktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 189 | static void compiler_pop_fblock(struct compiler *, enum fblocktype, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 190 | basicblock *); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 191 | /* Returns true if there is a loop on the fblock stack. */ |
| 192 | static int compiler_in_loop(struct compiler *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 193 | |
| 194 | static int inplace_binop(struct compiler *, operator_ty); |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 195 | static int expr_constant(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 197 | static int compiler_with(struct compiler *, stmt_ty, int); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 198 | static int compiler_call_helper(struct compiler *c, Py_ssize_t n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 199 | asdl_seq *args, |
| 200 | asdl_seq *keywords, |
| 201 | expr_ty starargs, |
| 202 | expr_ty kwargs); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 203 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 204 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 205 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 206 | static PyCodeObject *assemble(struct compiler *, int addNone); |
| 207 | static PyObject *__doc__; |
| 208 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 209 | #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit" |
| 210 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 211 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 212 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 214 | /* Name mangling: __private becomes _classname__private. |
| 215 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 216 | PyObject *result; |
| 217 | size_t nlen, plen, ipriv; |
| 218 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 220 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 221 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | Py_INCREF(ident); |
| 223 | return ident; |
| 224 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 225 | nlen = PyUnicode_GET_LENGTH(ident); |
| 226 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 228 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | The only time a name with a dot can occur is when |
| 230 | we are compiling an import statement that has a |
| 231 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | TODO(jhylton): Decide whether we want to support |
| 234 | mangling of the module name, e.g. __M.X. |
| 235 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 236 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 237 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 238 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | Py_INCREF(ident); |
| 240 | return ident; /* Don't mangle __whatever__ */ |
| 241 | } |
| 242 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 243 | ipriv = 0; |
| 244 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 245 | ipriv++; |
| 246 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | Py_INCREF(ident); |
| 248 | return ident; /* Don't mangle if class is just underscores */ |
| 249 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 250 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 252 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 253 | PyErr_SetString(PyExc_OverflowError, |
| 254 | "private identifier too large to be mangled"); |
| 255 | return NULL; |
| 256 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 257 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 258 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 259 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 260 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 261 | |
| 262 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 263 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 265 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 266 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 267 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 268 | Py_DECREF(result); |
| 269 | return NULL; |
| 270 | } |
| 271 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 272 | Py_DECREF(result); |
| 273 | return NULL; |
| 274 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 275 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 276 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 279 | static int |
| 280 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 281 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 282 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | c->c_stack = PyList_New(0); |
| 285 | if (!c->c_stack) |
| 286 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 292 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 293 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | struct compiler c; |
| 296 | PyCodeObject *co = NULL; |
| 297 | PyCompilerFlags local_flags; |
| 298 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 300 | if (!__doc__) { |
| 301 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 302 | if (!__doc__) |
| 303 | return NULL; |
| 304 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | if (!compiler_init(&c)) |
| 307 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 308 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | c.c_filename = filename; |
| 310 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 311 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | if (c.c_future == NULL) |
| 313 | goto finally; |
| 314 | if (!flags) { |
| 315 | local_flags.cf_flags = 0; |
| 316 | flags = &local_flags; |
| 317 | } |
| 318 | merged = c.c_future->ff_features | flags->cf_flags; |
| 319 | c.c_future->ff_features = merged; |
| 320 | flags->cf_flags = merged; |
| 321 | c.c_flags = flags; |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 322 | c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 323 | c.c_nestlevel = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 324 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 325 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (c.c_st == NULL) { |
| 327 | if (!PyErr_Occurred()) |
| 328 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 329 | goto finally; |
| 330 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 333 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 334 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | compiler_free(&c); |
| 336 | assert(co || PyErr_Occurred()); |
| 337 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 341 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 342 | int optimize, PyArena *arena) |
| 343 | { |
| 344 | PyObject *filename; |
| 345 | PyCodeObject *co; |
| 346 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 347 | if (filename == NULL) |
| 348 | return NULL; |
| 349 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 350 | Py_DECREF(filename); |
| 351 | return co; |
| 352 | |
| 353 | } |
| 354 | |
| 355 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | PyNode_Compile(struct _node *n, const char *filename) |
| 357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | PyCodeObject *co = NULL; |
| 359 | mod_ty mod; |
| 360 | PyArena *arena = PyArena_New(); |
| 361 | if (!arena) |
| 362 | return NULL; |
| 363 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 364 | if (mod) |
| 365 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 366 | PyArena_Free(arena); |
| 367 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 370 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 371 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 372 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | if (c->c_st) |
| 374 | PySymtable_Free(c->c_st); |
| 375 | if (c->c_future) |
| 376 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 377 | Py_XDECREF(c->c_filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 381 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 382 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 383 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | Py_ssize_t i, n; |
| 385 | PyObject *v, *k; |
| 386 | PyObject *dict = PyDict_New(); |
| 387 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | n = PyList_Size(list); |
| 390 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 391 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | if (!v) { |
| 393 | Py_DECREF(dict); |
| 394 | return NULL; |
| 395 | } |
| 396 | k = PyList_GET_ITEM(list, i); |
| 397 | k = PyTuple_Pack(2, k, k->ob_type); |
| 398 | if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { |
| 399 | Py_XDECREF(k); |
| 400 | Py_DECREF(v); |
| 401 | Py_DECREF(dict); |
| 402 | return NULL; |
| 403 | } |
| 404 | Py_DECREF(k); |
| 405 | Py_DECREF(v); |
| 406 | } |
| 407 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* Return new dict containing names from src that match scope(s). |
| 411 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 412 | 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] | 413 | 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] | 414 | values are integers, starting at offset and increasing by one for |
| 415 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 416 | */ |
| 417 | |
| 418 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 419 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 420 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 421 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 423 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 424 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | assert(offset >= 0); |
| 426 | if (dest == NULL) |
| 427 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 428 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 429 | /* Sort the keys so that we have a deterministic order on the indexes |
| 430 | saved in the returned dictionary. These indexes are used as indexes |
| 431 | into the free and cell var storage. Therefore if they aren't |
| 432 | deterministic, then the generated bytecode is not deterministic. |
| 433 | */ |
| 434 | sorted_keys = PyDict_Keys(src); |
| 435 | if (sorted_keys == NULL) |
| 436 | return NULL; |
| 437 | if (PyList_Sort(sorted_keys) != 0) { |
| 438 | Py_DECREF(sorted_keys); |
| 439 | return NULL; |
| 440 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 441 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 442 | |
| 443 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 444 | /* XXX this should probably be a macro in symtable.h */ |
| 445 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 446 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 447 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 448 | assert(PyLong_Check(v)); |
| 449 | vi = PyLong_AS_LONG(v); |
| 450 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | if (scope == scope_type || vi & flag) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 453 | PyObject *tuple, *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 455 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | Py_DECREF(dest); |
| 457 | return NULL; |
| 458 | } |
| 459 | i++; |
| 460 | tuple = PyTuple_Pack(2, k, k->ob_type); |
| 461 | if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 462 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | Py_DECREF(item); |
| 464 | Py_DECREF(dest); |
| 465 | Py_XDECREF(tuple); |
| 466 | return NULL; |
| 467 | } |
| 468 | Py_DECREF(item); |
| 469 | Py_DECREF(tuple); |
| 470 | } |
| 471 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 472 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 476 | static void |
| 477 | compiler_unit_check(struct compiler_unit *u) |
| 478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | basicblock *block; |
| 480 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
| 481 | assert((void *)block != (void *)0xcbcbcbcb); |
| 482 | assert((void *)block != (void *)0xfbfbfbfb); |
| 483 | assert((void *)block != (void *)0xdbdbdbdb); |
| 484 | if (block->b_instr != NULL) { |
| 485 | assert(block->b_ialloc > 0); |
| 486 | assert(block->b_iused > 0); |
| 487 | assert(block->b_ialloc >= block->b_iused); |
| 488 | } |
| 489 | else { |
| 490 | assert (block->b_iused == 0); |
| 491 | assert (block->b_ialloc == 0); |
| 492 | } |
| 493 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static void |
| 497 | compiler_unit_free(struct compiler_unit *u) |
| 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 501 | compiler_unit_check(u); |
| 502 | b = u->u_blocks; |
| 503 | while (b != NULL) { |
| 504 | if (b->b_instr) |
| 505 | PyObject_Free((void *)b->b_instr); |
| 506 | next = b->b_list; |
| 507 | PyObject_Free((void *)b); |
| 508 | b = next; |
| 509 | } |
| 510 | Py_CLEAR(u->u_ste); |
| 511 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 512 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 513 | Py_CLEAR(u->u_consts); |
| 514 | Py_CLEAR(u->u_names); |
| 515 | Py_CLEAR(u->u_varnames); |
| 516 | Py_CLEAR(u->u_freevars); |
| 517 | Py_CLEAR(u->u_cellvars); |
| 518 | Py_CLEAR(u->u_private); |
| 519 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 523 | compiler_enter_scope(struct compiler *c, identifier name, |
| 524 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 525 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 526 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 529 | struct compiler_unit)); |
| 530 | if (!u) { |
| 531 | PyErr_NoMemory(); |
| 532 | return 0; |
| 533 | } |
| 534 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 535 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | u->u_argcount = 0; |
| 537 | u->u_kwonlyargcount = 0; |
| 538 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 539 | if (!u->u_ste) { |
| 540 | compiler_unit_free(u); |
| 541 | return 0; |
| 542 | } |
| 543 | Py_INCREF(name); |
| 544 | u->u_name = name; |
| 545 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 546 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 547 | if (!u->u_varnames || !u->u_cellvars) { |
| 548 | compiler_unit_free(u); |
| 549 | return 0; |
| 550 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 551 | if (u->u_ste->ste_needs_class_closure) { |
| 552 | /* Cook up a implicit __class__ cell. */ |
| 553 | _Py_IDENTIFIER(__class__); |
| 554 | PyObject *tuple, *name, *zero; |
| 555 | int res; |
| 556 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
| 557 | assert(PyDict_Size(u->u_cellvars) == 0); |
| 558 | name = _PyUnicode_FromId(&PyId___class__); |
| 559 | if (!name) { |
| 560 | compiler_unit_free(u); |
| 561 | return 0; |
| 562 | } |
| 563 | tuple = PyTuple_Pack(2, name, Py_TYPE(name)); |
| 564 | if (!tuple) { |
| 565 | compiler_unit_free(u); |
| 566 | return 0; |
| 567 | } |
| 568 | zero = PyLong_FromLong(0); |
| 569 | if (!zero) { |
| 570 | Py_DECREF(tuple); |
| 571 | compiler_unit_free(u); |
| 572 | return 0; |
| 573 | } |
| 574 | res = PyDict_SetItem(u->u_cellvars, tuple, zero); |
| 575 | Py_DECREF(tuple); |
| 576 | Py_DECREF(zero); |
| 577 | if (res < 0) { |
| 578 | compiler_unit_free(u); |
| 579 | return 0; |
| 580 | } |
| 581 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
| 584 | PyDict_Size(u->u_cellvars)); |
| 585 | if (!u->u_freevars) { |
| 586 | compiler_unit_free(u); |
| 587 | return 0; |
| 588 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 590 | u->u_blocks = NULL; |
| 591 | u->u_nfblocks = 0; |
| 592 | u->u_firstlineno = lineno; |
| 593 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 594 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | u->u_lineno_set = 0; |
| 596 | u->u_consts = PyDict_New(); |
| 597 | if (!u->u_consts) { |
| 598 | compiler_unit_free(u); |
| 599 | return 0; |
| 600 | } |
| 601 | u->u_names = PyDict_New(); |
| 602 | if (!u->u_names) { |
| 603 | compiler_unit_free(u); |
| 604 | return 0; |
| 605 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 607 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 608 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 609 | /* Push the old compiler_unit on the stack. */ |
| 610 | if (c->u) { |
| 611 | PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL); |
| 612 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 613 | Py_XDECREF(capsule); |
| 614 | compiler_unit_free(u); |
| 615 | return 0; |
| 616 | } |
| 617 | Py_DECREF(capsule); |
| 618 | u->u_private = c->u->u_private; |
| 619 | Py_XINCREF(u->u_private); |
| 620 | } |
| 621 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | c->c_nestlevel++; |
| 624 | if (compiler_use_new_block(c) == NULL) |
| 625 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 626 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 627 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 628 | if (!compiler_set_qualname(c)) |
| 629 | return 0; |
| 630 | } |
| 631 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 635 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 636 | compiler_exit_scope(struct compiler *c) |
| 637 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 638 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 639 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 641 | c->c_nestlevel--; |
| 642 | compiler_unit_free(c->u); |
| 643 | /* Restore c->u to the parent unit. */ |
| 644 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 645 | if (n >= 0) { |
| 646 | capsule = PyList_GET_ITEM(c->c_stack, n); |
| 647 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT); |
| 648 | assert(c->u); |
| 649 | /* we are deleting from a list so this really shouldn't fail */ |
| 650 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 651 | Py_FatalError("compiler_exit_scope()"); |
| 652 | compiler_unit_check(c->u); |
| 653 | } |
| 654 | else |
| 655 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 659 | static int |
| 660 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 661 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 662 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 663 | _Py_static_string(dot_locals, ".<locals>"); |
| 664 | Py_ssize_t stack_size; |
| 665 | struct compiler_unit *u = c->u; |
| 666 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 667 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 668 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 669 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 670 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 671 | if (stack_size > 1) { |
| 672 | int scope, force_global = 0; |
| 673 | struct compiler_unit *parent; |
| 674 | PyObject *mangled, *capsule; |
| 675 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 676 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 677 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT); |
| 678 | assert(parent); |
| 679 | |
| 680 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 681 | assert(u->u_name); |
| 682 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 683 | if (!mangled) |
| 684 | return 0; |
| 685 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 686 | Py_DECREF(mangled); |
| 687 | assert(scope != GLOBAL_IMPLICIT); |
| 688 | if (scope == GLOBAL_EXPLICIT) |
| 689 | force_global = 1; |
| 690 | } |
| 691 | |
| 692 | if (!force_global) { |
| 693 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 694 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 695 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 696 | if (dot_locals_str == NULL) |
| 697 | return 0; |
| 698 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 699 | if (base == NULL) |
| 700 | return 0; |
| 701 | } |
| 702 | else { |
| 703 | Py_INCREF(parent->u_qualname); |
| 704 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 705 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 706 | } |
| 707 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 708 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 709 | if (base != NULL) { |
| 710 | dot_str = _PyUnicode_FromId(&dot); |
| 711 | if (dot_str == NULL) { |
| 712 | Py_DECREF(base); |
| 713 | return 0; |
| 714 | } |
| 715 | name = PyUnicode_Concat(base, dot_str); |
| 716 | Py_DECREF(base); |
| 717 | if (name == NULL) |
| 718 | return 0; |
| 719 | PyUnicode_Append(&name, u->u_name); |
| 720 | if (name == NULL) |
| 721 | return 0; |
| 722 | } |
| 723 | else { |
| 724 | Py_INCREF(u->u_name); |
| 725 | name = u->u_name; |
| 726 | } |
| 727 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 728 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 729 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 732 | /* Allocate a new block and return a pointer to it. |
| 733 | Returns NULL on error. |
| 734 | */ |
| 735 | |
| 736 | static basicblock * |
| 737 | compiler_new_block(struct compiler *c) |
| 738 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | basicblock *b; |
| 740 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 742 | u = c->u; |
| 743 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 744 | if (b == NULL) { |
| 745 | PyErr_NoMemory(); |
| 746 | return NULL; |
| 747 | } |
| 748 | memset((void *)b, 0, sizeof(basicblock)); |
| 749 | /* Extend the singly linked list of blocks with new block. */ |
| 750 | b->b_list = u->u_blocks; |
| 751 | u->u_blocks = b; |
| 752 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 755 | static basicblock * |
| 756 | compiler_use_new_block(struct compiler *c) |
| 757 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | basicblock *block = compiler_new_block(c); |
| 759 | if (block == NULL) |
| 760 | return NULL; |
| 761 | c->u->u_curblock = block; |
| 762 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static basicblock * |
| 766 | compiler_next_block(struct compiler *c) |
| 767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | basicblock *block = compiler_new_block(c); |
| 769 | if (block == NULL) |
| 770 | return NULL; |
| 771 | c->u->u_curblock->b_next = block; |
| 772 | c->u->u_curblock = block; |
| 773 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | static basicblock * |
| 777 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 779 | assert(block != NULL); |
| 780 | c->u->u_curblock->b_next = block; |
| 781 | c->u->u_curblock = block; |
| 782 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /* Returns the offset of the next instruction in the current block's |
| 786 | b_instr array. Resizes the b_instr as necessary. |
| 787 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 788 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 789 | |
| 790 | static int |
| 791 | compiler_next_instr(struct compiler *c, basicblock *b) |
| 792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | assert(b != NULL); |
| 794 | if (b->b_instr == NULL) { |
| 795 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 796 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 797 | if (b->b_instr == NULL) { |
| 798 | PyErr_NoMemory(); |
| 799 | return -1; |
| 800 | } |
| 801 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 802 | memset((char *)b->b_instr, 0, |
| 803 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 804 | } |
| 805 | else if (b->b_iused == b->b_ialloc) { |
| 806 | struct instr *tmp; |
| 807 | size_t oldsize, newsize; |
| 808 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 809 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 810 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | if (oldsize > (PY_SIZE_MAX >> 1)) { |
| 812 | PyErr_NoMemory(); |
| 813 | return -1; |
| 814 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 816 | if (newsize == 0) { |
| 817 | PyErr_NoMemory(); |
| 818 | return -1; |
| 819 | } |
| 820 | b->b_ialloc <<= 1; |
| 821 | tmp = (struct instr *)PyObject_Realloc( |
| 822 | (void *)b->b_instr, newsize); |
| 823 | if (tmp == NULL) { |
| 824 | PyErr_NoMemory(); |
| 825 | return -1; |
| 826 | } |
| 827 | b->b_instr = tmp; |
| 828 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 829 | } |
| 830 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 833 | /* Set the i_lineno member of the instruction at offset off if the |
| 834 | line number for the current expression/statement has not |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 835 | already been set. If it has been set, the call has no effect. |
| 836 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 837 | The line number is reset in the following cases: |
| 838 | - when entering a new scope |
| 839 | - on each statement |
| 840 | - on each expression that start a new line |
| 841 | - before the "except" clause |
| 842 | - before the "for" and "while" expressions |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 843 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 844 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 845 | static void |
| 846 | compiler_set_lineno(struct compiler *c, int off) |
| 847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 848 | basicblock *b; |
| 849 | if (c->u->u_lineno_set) |
| 850 | return; |
| 851 | c->u->u_lineno_set = 1; |
| 852 | b = c->u->u_curblock; |
| 853 | b->b_instr[off].i_lineno = c->u->u_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static int |
| 857 | opcode_stack_effect(int opcode, int oparg) |
| 858 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 859 | switch (opcode) { |
| 860 | case POP_TOP: |
| 861 | return -1; |
| 862 | case ROT_TWO: |
| 863 | case ROT_THREE: |
| 864 | return 0; |
| 865 | case DUP_TOP: |
| 866 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 867 | case DUP_TOP_TWO: |
| 868 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 870 | case UNARY_POSITIVE: |
| 871 | case UNARY_NEGATIVE: |
| 872 | case UNARY_NOT: |
| 873 | case UNARY_INVERT: |
| 874 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | case SET_ADD: |
| 877 | case LIST_APPEND: |
| 878 | return -1; |
| 879 | case MAP_ADD: |
| 880 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | case BINARY_POWER: |
| 883 | case BINARY_MULTIPLY: |
| 884 | case BINARY_MODULO: |
| 885 | case BINARY_ADD: |
| 886 | case BINARY_SUBTRACT: |
| 887 | case BINARY_SUBSCR: |
| 888 | case BINARY_FLOOR_DIVIDE: |
| 889 | case BINARY_TRUE_DIVIDE: |
| 890 | return -1; |
| 891 | case INPLACE_FLOOR_DIVIDE: |
| 892 | case INPLACE_TRUE_DIVIDE: |
| 893 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 895 | case INPLACE_ADD: |
| 896 | case INPLACE_SUBTRACT: |
| 897 | case INPLACE_MULTIPLY: |
| 898 | case INPLACE_MODULO: |
| 899 | return -1; |
| 900 | case STORE_SUBSCR: |
| 901 | return -3; |
| 902 | case STORE_MAP: |
| 903 | return -2; |
| 904 | case DELETE_SUBSCR: |
| 905 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | case BINARY_LSHIFT: |
| 908 | case BINARY_RSHIFT: |
| 909 | case BINARY_AND: |
| 910 | case BINARY_XOR: |
| 911 | case BINARY_OR: |
| 912 | return -1; |
| 913 | case INPLACE_POWER: |
| 914 | return -1; |
| 915 | case GET_ITER: |
| 916 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | case PRINT_EXPR: |
| 919 | return -1; |
| 920 | case LOAD_BUILD_CLASS: |
| 921 | return 1; |
| 922 | case INPLACE_LSHIFT: |
| 923 | case INPLACE_RSHIFT: |
| 924 | case INPLACE_AND: |
| 925 | case INPLACE_XOR: |
| 926 | case INPLACE_OR: |
| 927 | return -1; |
| 928 | case BREAK_LOOP: |
| 929 | return 0; |
| 930 | case SETUP_WITH: |
| 931 | return 7; |
| 932 | case WITH_CLEANUP: |
| 933 | return -1; /* XXX Sometimes more */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | case RETURN_VALUE: |
| 935 | return -1; |
| 936 | case IMPORT_STAR: |
| 937 | return -1; |
| 938 | case YIELD_VALUE: |
| 939 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 940 | case YIELD_FROM: |
| 941 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | case POP_BLOCK: |
| 943 | return 0; |
| 944 | case POP_EXCEPT: |
| 945 | return 0; /* -3 except if bad bytecode */ |
| 946 | case END_FINALLY: |
| 947 | return -1; /* or -2 or -3 if exception occurred */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 949 | case STORE_NAME: |
| 950 | return -1; |
| 951 | case DELETE_NAME: |
| 952 | return 0; |
| 953 | case UNPACK_SEQUENCE: |
| 954 | return oparg-1; |
| 955 | case UNPACK_EX: |
| 956 | return (oparg&0xFF) + (oparg>>8); |
| 957 | case FOR_ITER: |
| 958 | return 1; /* or -1, at end of iterator */ |
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_ATTR: |
| 961 | return -2; |
| 962 | case DELETE_ATTR: |
| 963 | return -1; |
| 964 | case STORE_GLOBAL: |
| 965 | return -1; |
| 966 | case DELETE_GLOBAL: |
| 967 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 968 | case LOAD_CONST: |
| 969 | return 1; |
| 970 | case LOAD_NAME: |
| 971 | return 1; |
| 972 | case BUILD_TUPLE: |
| 973 | case BUILD_LIST: |
| 974 | case BUILD_SET: |
| 975 | return 1-oparg; |
| 976 | case BUILD_MAP: |
| 977 | return 1; |
| 978 | case LOAD_ATTR: |
| 979 | return 0; |
| 980 | case COMPARE_OP: |
| 981 | return -1; |
| 982 | case IMPORT_NAME: |
| 983 | return -1; |
| 984 | case IMPORT_FROM: |
| 985 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 986 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | case JUMP_FORWARD: |
| 988 | case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */ |
| 989 | case JUMP_IF_FALSE_OR_POP: /* "" */ |
| 990 | case JUMP_ABSOLUTE: |
| 991 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 992 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 993 | case POP_JUMP_IF_FALSE: |
| 994 | case POP_JUMP_IF_TRUE: |
| 995 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 997 | case LOAD_GLOBAL: |
| 998 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | case CONTINUE_LOOP: |
| 1001 | return 0; |
| 1002 | case SETUP_LOOP: |
| 1003 | return 0; |
| 1004 | case SETUP_EXCEPT: |
| 1005 | case SETUP_FINALLY: |
| 1006 | return 6; /* can push 3 values for the new exception |
| 1007 | + 3 others for the previous exception state */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | case LOAD_FAST: |
| 1010 | return 1; |
| 1011 | case STORE_FAST: |
| 1012 | return -1; |
| 1013 | case DELETE_FAST: |
| 1014 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | case RAISE_VARARGS: |
| 1017 | return -oparg; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1018 | #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | case CALL_FUNCTION: |
| 1020 | return -NARGS(oparg); |
| 1021 | case CALL_FUNCTION_VAR: |
| 1022 | case CALL_FUNCTION_KW: |
| 1023 | return -NARGS(oparg)-1; |
| 1024 | case CALL_FUNCTION_VAR_KW: |
| 1025 | return -NARGS(oparg)-2; |
| 1026 | case MAKE_FUNCTION: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1027 | return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | case MAKE_CLOSURE: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1029 | return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1030 | #undef NARGS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | case BUILD_SLICE: |
| 1032 | if (oparg == 3) |
| 1033 | return -2; |
| 1034 | else |
| 1035 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | case LOAD_CLOSURE: |
| 1038 | return 1; |
| 1039 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1040 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | return 1; |
| 1042 | case STORE_DEREF: |
| 1043 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1044 | case DELETE_DEREF: |
| 1045 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | default: |
| 1047 | fprintf(stderr, "opcode = %d\n", opcode); |
| 1048 | Py_FatalError("opcode_stack_effect()"); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | } |
| 1051 | return 0; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* Add an opcode with no argument. |
| 1055 | Returns 0 on failure, 1 on success. |
| 1056 | */ |
| 1057 | |
| 1058 | static int |
| 1059 | compiler_addop(struct compiler *c, int opcode) |
| 1060 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | basicblock *b; |
| 1062 | struct instr *i; |
| 1063 | int off; |
| 1064 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1065 | if (off < 0) |
| 1066 | return 0; |
| 1067 | b = c->u->u_curblock; |
| 1068 | i = &b->b_instr[off]; |
| 1069 | i->i_opcode = opcode; |
| 1070 | i->i_hasarg = 0; |
| 1071 | if (opcode == RETURN_VALUE) |
| 1072 | b->b_return = 1; |
| 1073 | compiler_set_lineno(c, off); |
| 1074 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | static int |
| 1078 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) |
| 1079 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1080 | PyObject *t, *v; |
| 1081 | Py_ssize_t arg; |
| 1082 | double d; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1083 | |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1084 | /* necessary to make sure types aren't coerced (e.g., float and complex) */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */ |
| 1086 | if (PyFloat_Check(o)) { |
| 1087 | d = PyFloat_AS_DOUBLE(o); |
| 1088 | /* all we need is to make the tuple different in either the 0.0 |
| 1089 | * or -0.0 case from all others, just to avoid the "coercion". |
| 1090 | */ |
| 1091 | if (d == 0.0 && copysign(1.0, d) < 0.0) |
| 1092 | t = PyTuple_Pack(3, o, o->ob_type, Py_None); |
| 1093 | else |
| 1094 | t = PyTuple_Pack(2, o, o->ob_type); |
| 1095 | } |
| 1096 | else if (PyComplex_Check(o)) { |
| 1097 | Py_complex z; |
| 1098 | int real_negzero, imag_negzero; |
| 1099 | /* For the complex case we must make complex(x, 0.) |
| 1100 | different from complex(x, -0.) and complex(0., y) |
| 1101 | different from complex(-0., y), for any x and y. |
| 1102 | All four complex zeros must be distinguished.*/ |
| 1103 | z = PyComplex_AsCComplex(o); |
| 1104 | real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0; |
| 1105 | imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0; |
| 1106 | if (real_negzero && imag_negzero) { |
| 1107 | t = PyTuple_Pack(5, o, o->ob_type, |
| 1108 | Py_None, Py_None, Py_None); |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 1109 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1110 | else if (imag_negzero) { |
| 1111 | t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None); |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 1112 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1113 | else if (real_negzero) { |
| 1114 | t = PyTuple_Pack(3, o, o->ob_type, Py_None); |
| 1115 | } |
| 1116 | else { |
| 1117 | t = PyTuple_Pack(2, o, o->ob_type); |
| 1118 | } |
| 1119 | } |
| 1120 | else { |
| 1121 | t = PyTuple_Pack(2, o, o->ob_type); |
| 1122 | } |
| 1123 | if (t == NULL) |
| 1124 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1125 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | v = PyDict_GetItem(dict, t); |
| 1127 | if (!v) { |
| 1128 | if (PyErr_Occurred()) |
| 1129 | return -1; |
| 1130 | arg = PyDict_Size(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1131 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | if (!v) { |
| 1133 | Py_DECREF(t); |
| 1134 | return -1; |
| 1135 | } |
| 1136 | if (PyDict_SetItem(dict, t, v) < 0) { |
| 1137 | Py_DECREF(t); |
| 1138 | Py_DECREF(v); |
| 1139 | return -1; |
| 1140 | } |
| 1141 | Py_DECREF(v); |
| 1142 | } |
| 1143 | else |
| 1144 | arg = PyLong_AsLong(v); |
| 1145 | Py_DECREF(t); |
| 1146 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | static int |
| 1150 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1152 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1153 | Py_ssize_t arg = compiler_add_o(c, dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1154 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1155 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1156 | return compiler_addop_i(c, opcode, arg); |
| 1157 | } |
| 1158 | |
| 1159 | static int |
| 1160 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1163 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1165 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1166 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1167 | arg = compiler_add_o(c, dict, mangled); |
| 1168 | Py_DECREF(mangled); |
| 1169 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1170 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | return compiler_addop_i(c, opcode, arg); |
| 1172 | } |
| 1173 | |
| 1174 | /* Add an opcode with an integer argument. |
| 1175 | Returns 0 on failure, 1 on success. |
| 1176 | */ |
| 1177 | |
| 1178 | static int |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1179 | compiler_addop_i(struct compiler *c, Py_ssize_t opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | struct instr *i; |
| 1182 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1183 | |
| 1184 | /* Integer arguments are limit to 16-bit. There is an extension for 32-bit |
| 1185 | integer arguments. */ |
| 1186 | assert(INT32_MIN <= opcode); |
| 1187 | assert(opcode <= INT32_MAX); |
| 1188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1190 | if (off < 0) |
| 1191 | return 0; |
| 1192 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1193 | i->i_opcode = Py_SAFE_DOWNCAST(opcode, Py_ssize_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | i->i_oparg = oparg; |
| 1195 | i->i_hasarg = 1; |
| 1196 | compiler_set_lineno(c, off); |
| 1197 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | static int |
| 1201 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1202 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1203 | struct instr *i; |
| 1204 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1205 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | assert(b != NULL); |
| 1207 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1208 | if (off < 0) |
| 1209 | return 0; |
| 1210 | i = &c->u->u_curblock->b_instr[off]; |
| 1211 | i->i_opcode = opcode; |
| 1212 | i->i_target = b; |
| 1213 | i->i_hasarg = 1; |
| 1214 | if (absolute) |
| 1215 | i->i_jabs = 1; |
| 1216 | else |
| 1217 | i->i_jrel = 1; |
| 1218 | compiler_set_lineno(c, off); |
| 1219 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd |
| 1223 | like to find better names.) NEW_BLOCK() creates a new block and sets |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1224 | it as the current block. NEXT_BLOCK() also creates an implicit jump |
| 1225 | from the current block to the new block. |
| 1226 | */ |
| 1227 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1228 | /* The returns inside these macros make it impossible to decref objects |
| 1229 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1230 | */ |
| 1231 | |
| 1232 | |
| 1233 | #define NEW_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | if (compiler_use_new_block((C)) == NULL) \ |
| 1235 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1239 | if (compiler_next_block((C)) == NULL) \ |
| 1240 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1244 | if (!compiler_addop((C), (OP))) \ |
| 1245 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1248 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | if (!compiler_addop((C), (OP))) { \ |
| 1250 | compiler_exit_scope(c); \ |
| 1251 | return 0; \ |
| 1252 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1255 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1256 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1257 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
| 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; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1328 | return s->v.Expr.value->kind == Str_kind; |
| 1329 | } |
| 1330 | |
| 1331 | /* Compile a sequence of statements, checking for a docstring. */ |
| 1332 | |
| 1333 | static int |
| 1334 | compiler_body(struct compiler *c, asdl_seq *stmts) |
| 1335 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | int i = 0; |
| 1337 | stmt_ty st; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1338 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | if (!asdl_seq_LEN(stmts)) |
| 1340 | return 1; |
| 1341 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1342 | if (compiler_isdocstring(st) && c->c_optimize < 2) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | /* don't generate docstrings if -OO */ |
| 1344 | i = 1; |
| 1345 | VISIT(c, expr, st->v.Expr.value); |
| 1346 | if (!compiler_nameop(c, __doc__, Store)) |
| 1347 | return 0; |
| 1348 | } |
| 1349 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1350 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
| 1351 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | static PyCodeObject * |
| 1355 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1357 | PyCodeObject *co; |
| 1358 | int addNone = 1; |
| 1359 | static PyObject *module; |
| 1360 | if (!module) { |
| 1361 | module = PyUnicode_InternFromString("<module>"); |
| 1362 | if (!module) |
| 1363 | return NULL; |
| 1364 | } |
| 1365 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1366 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | return NULL; |
| 1368 | switch (mod->kind) { |
| 1369 | case Module_kind: |
| 1370 | if (!compiler_body(c, mod->v.Module.body)) { |
| 1371 | compiler_exit_scope(c); |
| 1372 | return 0; |
| 1373 | } |
| 1374 | break; |
| 1375 | case Interactive_kind: |
| 1376 | c->c_interactive = 1; |
| 1377 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1378 | mod->v.Interactive.body); |
| 1379 | break; |
| 1380 | case Expression_kind: |
| 1381 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1382 | addNone = 0; |
| 1383 | break; |
| 1384 | case Suite_kind: |
| 1385 | PyErr_SetString(PyExc_SystemError, |
| 1386 | "suite should not be possible"); |
| 1387 | return 0; |
| 1388 | default: |
| 1389 | PyErr_Format(PyExc_SystemError, |
| 1390 | "module kind %d should not be possible", |
| 1391 | mod->kind); |
| 1392 | return 0; |
| 1393 | } |
| 1394 | co = assemble(c, addNone); |
| 1395 | compiler_exit_scope(c); |
| 1396 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1399 | /* The test for LOCAL must come before the test for FREE in order to |
| 1400 | handle classes where name is both local and free. The local var is |
| 1401 | 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] | 1402 | */ |
| 1403 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1404 | static int |
| 1405 | get_ref_type(struct compiler *c, PyObject *name) |
| 1406 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1407 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1408 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
| 1409 | !PyUnicode_CompareWithASCIIString(name, "__class__")) |
| 1410 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1411 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | if (scope == 0) { |
| 1413 | char buf[350]; |
| 1414 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1415 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1416 | "symbols: %s\nlocals: %s\nglobals: %s", |
| 1417 | PyBytes_AS_STRING(name), |
| 1418 | PyBytes_AS_STRING(c->u->u_name), |
| 1419 | PyObject_REPR(c->u->u_ste->ste_id), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1420 | PyObject_REPR(c->u->u_ste->ste_symbols), |
| 1421 | PyObject_REPR(c->u->u_varnames), |
| 1422 | PyObject_REPR(c->u->u_names) |
| 1423 | ); |
| 1424 | Py_FatalError(buf); |
| 1425 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | static int |
| 1431 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1432 | { |
| 1433 | PyObject *k, *v; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1434 | k = PyTuple_Pack(2, name, name->ob_type); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1435 | if (k == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1436 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1437 | v = PyDict_GetItem(dict, k); |
Neal Norwitz | 3715c3e | 2005-11-24 22:09:18 +0000 | [diff] [blame] | 1438 | Py_DECREF(k); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1439 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1440 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1441 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1445 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1446 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1447 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1448 | if (qualname == NULL) |
| 1449 | qualname = co->co_name; |
| 1450 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | if (free == 0) { |
| 1452 | ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1453 | ADDOP_O(c, LOAD_CONST, qualname, consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | ADDOP_I(c, MAKE_FUNCTION, args); |
| 1455 | return 1; |
| 1456 | } |
| 1457 | for (i = 0; i < free; ++i) { |
| 1458 | /* Bypass com_addop_varname because it will generate |
| 1459 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1460 | */ |
| 1461 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1462 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1463 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1464 | /* Special case: If a class contains a method with a |
| 1465 | free variable that has the same name as a method, |
| 1466 | the name will be considered free *and* local in the |
| 1467 | class. It should be handled by the closure, as |
| 1468 | well as by the normal name loookup logic. |
| 1469 | */ |
| 1470 | reftype = get_ref_type(c, name); |
| 1471 | if (reftype == CELL) |
| 1472 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1473 | else /* (reftype == FREE) */ |
| 1474 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1475 | if (arg == -1) { |
| 1476 | fprintf(stderr, |
| 1477 | "lookup %s in %s %d %d\n" |
| 1478 | "freevars of %s: %s\n", |
| 1479 | PyObject_REPR(name), |
| 1480 | PyBytes_AS_STRING(c->u->u_name), |
| 1481 | reftype, arg, |
| 1482 | _PyUnicode_AsString(co->co_name), |
| 1483 | PyObject_REPR(co->co_freevars)); |
| 1484 | Py_FatalError("compiler_make_closure()"); |
| 1485 | } |
| 1486 | ADDOP_I(c, LOAD_CLOSURE, arg); |
| 1487 | } |
| 1488 | ADDOP_I(c, BUILD_TUPLE, free); |
| 1489 | ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1490 | ADDOP_O(c, LOAD_CONST, qualname, consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | ADDOP_I(c, MAKE_CLOSURE, args); |
| 1492 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | static int |
| 1496 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1500 | if (!decos) |
| 1501 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1502 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1503 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1504 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1505 | } |
| 1506 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| 1509 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1510 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1512 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | int i, default_count = 0; |
| 1514 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1515 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1516 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1517 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1518 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
| 1519 | if (!mangled) |
| 1520 | return -1; |
| 1521 | ADDOP_O(c, LOAD_CONST, mangled, consts); |
| 1522 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (!compiler_visit_expr(c, default_)) { |
| 1524 | return -1; |
| 1525 | } |
| 1526 | default_count++; |
| 1527 | } |
| 1528 | } |
| 1529 | return default_count; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1533 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 1534 | expr_ty annotation, PyObject *names) |
| 1535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1536 | if (annotation) { |
| 1537 | VISIT(c, expr, annotation); |
| 1538 | if (PyList_Append(names, id)) |
| 1539 | return -1; |
| 1540 | } |
| 1541 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static int |
| 1545 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 1546 | PyObject *names) |
| 1547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1548 | int i, error; |
| 1549 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1550 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
| 1551 | error = compiler_visit_argannotation( |
| 1552 | c, |
| 1553 | arg->arg, |
| 1554 | arg->annotation, |
| 1555 | names); |
| 1556 | if (error) |
| 1557 | return error; |
| 1558 | } |
| 1559 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | static int |
| 1563 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 1564 | expr_ty returns) |
| 1565 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1566 | /* Push arg annotations and a list of the argument names. Return the # |
| 1567 | of items pushed. The expressions are evaluated out-of-order wrt the |
| 1568 | source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | More than 2^16-1 annotations is a SyntaxError. Returns -1 on error. |
| 1571 | */ |
| 1572 | static identifier return_str; |
| 1573 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1574 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1575 | names = PyList_New(0); |
| 1576 | if (!names) |
| 1577 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | if (compiler_visit_argannotations(c, args->args, names)) |
| 1580 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1581 | if (args->vararg && args->vararg->annotation && |
| 1582 | compiler_visit_argannotation(c, args->vararg->arg, |
| 1583 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1584 | goto error; |
| 1585 | if (compiler_visit_argannotations(c, args->kwonlyargs, names)) |
| 1586 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1587 | if (args->kwarg && args->kwarg->annotation && |
| 1588 | compiler_visit_argannotation(c, args->kwarg->arg, |
| 1589 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1590 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1591 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1592 | if (!return_str) { |
| 1593 | return_str = PyUnicode_InternFromString("return"); |
| 1594 | if (!return_str) |
| 1595 | goto error; |
| 1596 | } |
| 1597 | if (compiler_visit_argannotation(c, return_str, returns, names)) { |
| 1598 | goto error; |
| 1599 | } |
| 1600 | |
| 1601 | len = PyList_GET_SIZE(names); |
| 1602 | if (len > 65534) { |
| 1603 | /* len must fit in 16 bits, and len is incremented below */ |
| 1604 | PyErr_SetString(PyExc_SyntaxError, |
| 1605 | "too many annotations"); |
| 1606 | goto error; |
| 1607 | } |
| 1608 | if (len) { |
| 1609 | /* convert names to a tuple and place on stack */ |
| 1610 | PyObject *elt; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1611 | Py_ssize_t i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1612 | PyObject *s = PyTuple_New(len); |
| 1613 | if (!s) |
| 1614 | goto error; |
| 1615 | for (i = 0; i < len; i++) { |
| 1616 | elt = PyList_GET_ITEM(names, i); |
| 1617 | Py_INCREF(elt); |
| 1618 | PyTuple_SET_ITEM(s, i, elt); |
| 1619 | } |
| 1620 | ADDOP_O(c, LOAD_CONST, s, consts); |
| 1621 | Py_DECREF(s); |
| 1622 | len++; /* include the just-pushed tuple */ |
| 1623 | } |
| 1624 | Py_DECREF(names); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1625 | |
| 1626 | /* We just checked that len <= 65535, see above */ |
| 1627 | return Py_SAFE_DOWNCAST(len, Py_ssize_t, int); |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1628 | |
| 1629 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | Py_DECREF(names); |
| 1631 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1635 | compiler_function(struct compiler *c, stmt_ty s) |
| 1636 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1638 | PyObject *qualname, *first_const = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | arguments_ty args = s->v.FunctionDef.args; |
| 1640 | expr_ty returns = s->v.FunctionDef.returns; |
| 1641 | asdl_seq* decos = s->v.FunctionDef.decorator_list; |
| 1642 | stmt_ty st; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1643 | Py_ssize_t i, n, arglength; |
| 1644 | int docstring, kw_default_count = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1645 | int num_annotations; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | assert(s->kind == FunctionDef_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1649 | if (!compiler_decorators(c, decos)) |
| 1650 | return 0; |
Benjamin Peterson | 1ef876c | 2013-02-10 09:29:59 -0500 | [diff] [blame] | 1651 | if (args->defaults) |
| 1652 | VISIT_SEQ(c, expr, args->defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1653 | if (args->kwonlyargs) { |
| 1654 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
| 1655 | args->kw_defaults); |
| 1656 | if (res < 0) |
| 1657 | return 0; |
| 1658 | kw_default_count = res; |
| 1659 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | num_annotations = compiler_visit_annotations(c, args, returns); |
| 1661 | if (num_annotations < 0) |
| 1662 | return 0; |
| 1663 | assert((num_annotations & 0xFFFF) == num_annotations); |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1665 | if (!compiler_enter_scope(c, s->v.FunctionDef.name, |
| 1666 | COMPILER_SCOPE_FUNCTION, (void *)s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1667 | s->lineno)) |
| 1668 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); |
| 1671 | docstring = compiler_isdocstring(st); |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1672 | if (docstring && c->c_optimize < 2) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | first_const = st->v.Expr.value->v.Str.s; |
| 1674 | if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { |
| 1675 | compiler_exit_scope(c); |
| 1676 | return 0; |
| 1677 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1678 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1679 | c->u->u_argcount = asdl_seq_LEN(args->args); |
| 1680 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 1681 | n = asdl_seq_LEN(s->v.FunctionDef.body); |
| 1682 | /* if there was a docstring, we need to skip the first statement */ |
| 1683 | for (i = docstring; i < n; i++) { |
| 1684 | st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); |
| 1685 | VISIT_IN_SCOPE(c, stmt, st); |
| 1686 | } |
| 1687 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 1688 | qualname = c->u->u_qualname; |
| 1689 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1690 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 1691 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1692 | Py_XDECREF(qualname); |
| 1693 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1694 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1695 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | arglength = asdl_seq_LEN(args->defaults); |
| 1698 | arglength |= kw_default_count << 8; |
| 1699 | arglength |= num_annotations << 16; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1700 | compiler_make_closure(c, co, arglength, qualname); |
| 1701 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1702 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | /* decorators */ |
| 1705 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1706 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 1707 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1709 | return compiler_nameop(c, s->v.FunctionDef.name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | static int |
| 1713 | compiler_class(struct compiler *c, stmt_ty s) |
| 1714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | PyCodeObject *co; |
| 1716 | PyObject *str; |
| 1717 | int i; |
| 1718 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | if (!compiler_decorators(c, decos)) |
| 1721 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | /* ultimately generate code for: |
| 1724 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 1725 | where: |
| 1726 | <func> is a function/closure created from the class body; |
| 1727 | it has a single argument (__locals__) where the dict |
| 1728 | (or MutableSequence) representing the locals is passed |
| 1729 | <name> is the class name |
| 1730 | <bases> is the positional arguments and *varargs argument |
| 1731 | <keywords> is the keyword arguments and **kwds argument |
| 1732 | This borrows from compiler_call. |
| 1733 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1736 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
| 1737 | COMPILER_SCOPE_CLASS, (void *)s, s->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | return 0; |
| 1739 | /* this block represents what we do in the new scope */ |
| 1740 | { |
| 1741 | /* use the class name for name mangling */ |
| 1742 | Py_INCREF(s->v.ClassDef.name); |
| 1743 | Py_XDECREF(c->u->u_private); |
| 1744 | c->u->u_private = s->v.ClassDef.name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1745 | /* load (global) __name__ ... */ |
| 1746 | str = PyUnicode_InternFromString("__name__"); |
| 1747 | if (!str || !compiler_nameop(c, str, Load)) { |
| 1748 | Py_XDECREF(str); |
| 1749 | compiler_exit_scope(c); |
| 1750 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1751 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | Py_DECREF(str); |
| 1753 | /* ... and store it as __module__ */ |
| 1754 | str = PyUnicode_InternFromString("__module__"); |
| 1755 | if (!str || !compiler_nameop(c, str, Store)) { |
| 1756 | Py_XDECREF(str); |
| 1757 | compiler_exit_scope(c); |
| 1758 | return 0; |
| 1759 | } |
| 1760 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 1761 | assert(c->u->u_qualname); |
| 1762 | ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1763 | str = PyUnicode_InternFromString("__qualname__"); |
| 1764 | if (!str || !compiler_nameop(c, str, Store)) { |
| 1765 | Py_XDECREF(str); |
| 1766 | compiler_exit_scope(c); |
| 1767 | return 0; |
| 1768 | } |
| 1769 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | /* compile the body proper */ |
| 1771 | if (!compiler_body(c, s->v.ClassDef.body)) { |
| 1772 | compiler_exit_scope(c); |
| 1773 | return 0; |
| 1774 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1775 | if (c->u->u_ste->ste_needs_class_closure) { |
| 1776 | /* return the (empty) __class__ cell */ |
| 1777 | str = PyUnicode_InternFromString("__class__"); |
| 1778 | if (str == NULL) { |
| 1779 | compiler_exit_scope(c); |
| 1780 | return 0; |
| 1781 | } |
| 1782 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 1783 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 1784 | if (i < 0) { |
| 1785 | compiler_exit_scope(c); |
| 1786 | return 0; |
| 1787 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1788 | assert(i == 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1789 | /* Return the cell where to store __class__ */ |
| 1790 | ADDOP_I(c, LOAD_CLOSURE, i); |
| 1791 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1792 | else { |
| 1793 | assert(PyDict_Size(c->u->u_cellvars) == 0); |
| 1794 | /* This happens when nobody references the cell. Return None. */ |
| 1795 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1796 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
| 1798 | /* create the code object */ |
| 1799 | co = assemble(c, 1); |
| 1800 | } |
| 1801 | /* leave the new scope */ |
| 1802 | compiler_exit_scope(c); |
| 1803 | if (co == NULL) |
| 1804 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 1805 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | /* 2. load the 'build_class' function */ |
| 1807 | ADDOP(c, LOAD_BUILD_CLASS); |
| 1808 | |
| 1809 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1810 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | Py_DECREF(co); |
| 1812 | |
| 1813 | /* 4. load class name */ |
| 1814 | ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); |
| 1815 | |
| 1816 | /* 5. generate the rest of the code for the call */ |
| 1817 | if (!compiler_call_helper(c, 2, |
| 1818 | s->v.ClassDef.bases, |
| 1819 | s->v.ClassDef.keywords, |
| 1820 | s->v.ClassDef.starargs, |
| 1821 | s->v.ClassDef.kwargs)) |
| 1822 | return 0; |
| 1823 | |
| 1824 | /* 6. apply decorators */ |
| 1825 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1826 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 1827 | } |
| 1828 | |
| 1829 | /* 7. store into <name> */ |
| 1830 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 1831 | return 0; |
| 1832 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
| 1835 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1836 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 1837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | basicblock *end, *next; |
| 1839 | |
| 1840 | assert(e->kind == IfExp_kind); |
| 1841 | end = compiler_new_block(c); |
| 1842 | if (end == NULL) |
| 1843 | return 0; |
| 1844 | next = compiler_new_block(c); |
| 1845 | if (next == NULL) |
| 1846 | return 0; |
| 1847 | VISIT(c, expr, e->v.IfExp.test); |
| 1848 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); |
| 1849 | VISIT(c, expr, e->v.IfExp.body); |
| 1850 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 1851 | compiler_use_next_block(c, next); |
| 1852 | VISIT(c, expr, e->v.IfExp.orelse); |
| 1853 | compiler_use_next_block(c, end); |
| 1854 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1858 | compiler_lambda(struct compiler *c, expr_ty e) |
| 1859 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1860 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1861 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1862 | static identifier name; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 1863 | int kw_default_count = 0; |
| 1864 | Py_ssize_t arglength; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1865 | arguments_ty args = e->v.Lambda.args; |
| 1866 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1868 | if (!name) { |
| 1869 | name = PyUnicode_InternFromString("<lambda>"); |
| 1870 | if (!name) |
| 1871 | return 0; |
| 1872 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1873 | |
Benjamin Peterson | 419d9a8 | 2013-02-10 09:48:22 -0500 | [diff] [blame] | 1874 | if (args->defaults) |
| 1875 | VISIT_SEQ(c, expr, args->defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | if (args->kwonlyargs) { |
| 1877 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
| 1878 | args->kw_defaults); |
| 1879 | if (res < 0) return 0; |
| 1880 | kw_default_count = res; |
| 1881 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 1882 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1883 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1884 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 1885 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | /* Make None the first constant, so the lambda can't have a |
| 1887 | docstring. */ |
| 1888 | if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) |
| 1889 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1891 | c->u->u_argcount = asdl_seq_LEN(args->args); |
| 1892 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 1893 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 1894 | if (c->u->u_ste->ste_generator) { |
| 1895 | ADDOP_IN_SCOPE(c, POP_TOP); |
| 1896 | } |
| 1897 | else { |
| 1898 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
| 1899 | } |
| 1900 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 1901 | qualname = c->u->u_qualname; |
| 1902 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 1904 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1907 | arglength = asdl_seq_LEN(args->defaults); |
| 1908 | arglength |= kw_default_count << 8; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1909 | compiler_make_closure(c, co, arglength, qualname); |
| 1910 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | Py_DECREF(co); |
| 1912 | |
| 1913 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1917 | compiler_if(struct compiler *c, stmt_ty s) |
| 1918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | basicblock *end, *next; |
| 1920 | int constant; |
| 1921 | assert(s->kind == If_kind); |
| 1922 | end = compiler_new_block(c); |
| 1923 | if (end == NULL) |
| 1924 | return 0; |
| 1925 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1926 | constant = expr_constant(c, s->v.If.test); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | /* constant = 0: "if 0" |
| 1928 | * constant = 1: "if 1", "if 2", ... |
| 1929 | * constant = -1: rest */ |
| 1930 | if (constant == 0) { |
| 1931 | if (s->v.If.orelse) |
| 1932 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 1933 | } else if (constant == 1) { |
| 1934 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 1935 | } else { |
| 1936 | if (s->v.If.orelse) { |
| 1937 | next = compiler_new_block(c); |
| 1938 | if (next == NULL) |
| 1939 | return 0; |
| 1940 | } |
| 1941 | else |
| 1942 | next = end; |
| 1943 | VISIT(c, expr, s->v.If.test); |
| 1944 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, next); |
| 1945 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 1946 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 1947 | if (s->v.If.orelse) { |
| 1948 | compiler_use_next_block(c, next); |
| 1949 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 1950 | } |
| 1951 | } |
| 1952 | compiler_use_next_block(c, end); |
| 1953 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | static int |
| 1957 | compiler_for(struct compiler *c, stmt_ty s) |
| 1958 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1961 | start = compiler_new_block(c); |
| 1962 | cleanup = compiler_new_block(c); |
| 1963 | end = compiler_new_block(c); |
| 1964 | if (start == NULL || end == NULL || cleanup == NULL) |
| 1965 | return 0; |
| 1966 | ADDOP_JREL(c, SETUP_LOOP, end); |
| 1967 | if (!compiler_push_fblock(c, LOOP, start)) |
| 1968 | return 0; |
| 1969 | VISIT(c, expr, s->v.For.iter); |
| 1970 | ADDOP(c, GET_ITER); |
| 1971 | compiler_use_next_block(c, start); |
| 1972 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 1973 | VISIT(c, expr, s->v.For.target); |
| 1974 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 1975 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 1976 | compiler_use_next_block(c, cleanup); |
| 1977 | ADDOP(c, POP_BLOCK); |
| 1978 | compiler_pop_fblock(c, LOOP, start); |
| 1979 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 1980 | compiler_use_next_block(c, end); |
| 1981 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static int |
| 1985 | compiler_while(struct compiler *c, stmt_ty s) |
| 1986 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1987 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 1988 | int constant = expr_constant(c, s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | if (constant == 0) { |
| 1991 | if (s->v.While.orelse) |
| 1992 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 1993 | return 1; |
| 1994 | } |
| 1995 | loop = compiler_new_block(c); |
| 1996 | end = compiler_new_block(c); |
| 1997 | if (constant == -1) { |
| 1998 | anchor = compiler_new_block(c); |
| 1999 | if (anchor == NULL) |
| 2000 | return 0; |
| 2001 | } |
| 2002 | if (loop == NULL || end == NULL) |
| 2003 | return 0; |
| 2004 | if (s->v.While.orelse) { |
| 2005 | orelse = compiler_new_block(c); |
| 2006 | if (orelse == NULL) |
| 2007 | return 0; |
| 2008 | } |
| 2009 | else |
| 2010 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | ADDOP_JREL(c, SETUP_LOOP, end); |
| 2013 | compiler_use_next_block(c, loop); |
| 2014 | if (!compiler_push_fblock(c, LOOP, loop)) |
| 2015 | return 0; |
| 2016 | if (constant == -1) { |
| 2017 | VISIT(c, expr, s->v.While.test); |
| 2018 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor); |
| 2019 | } |
| 2020 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2021 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | /* XXX should the two POP instructions be in a separate block |
| 2024 | if there is no else clause ? |
| 2025 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2027 | if (constant == -1) { |
| 2028 | compiler_use_next_block(c, anchor); |
| 2029 | ADDOP(c, POP_BLOCK); |
| 2030 | } |
| 2031 | compiler_pop_fblock(c, LOOP, loop); |
| 2032 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2033 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2034 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | static int |
| 2040 | compiler_continue(struct compiler *c) |
| 2041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop"; |
| 2043 | static const char IN_FINALLY_ERROR_MSG[] = |
| 2044 | "'continue' not supported inside 'finally' clause"; |
| 2045 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2047 | if (!c->u->u_nfblocks) |
| 2048 | return compiler_error(c, LOOP_ERROR_MSG); |
| 2049 | i = c->u->u_nfblocks - 1; |
| 2050 | switch (c->u->u_fblock[i].fb_type) { |
| 2051 | case LOOP: |
| 2052 | ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block); |
| 2053 | break; |
| 2054 | case EXCEPT: |
| 2055 | case FINALLY_TRY: |
| 2056 | while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) { |
| 2057 | /* Prevent continue anywhere under a finally |
| 2058 | even if hidden in a sub-try or except. */ |
| 2059 | if (c->u->u_fblock[i].fb_type == FINALLY_END) |
| 2060 | return compiler_error(c, IN_FINALLY_ERROR_MSG); |
| 2061 | } |
| 2062 | if (i == -1) |
| 2063 | return compiler_error(c, LOOP_ERROR_MSG); |
| 2064 | ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block); |
| 2065 | break; |
| 2066 | case FINALLY_END: |
| 2067 | return compiler_error(c, IN_FINALLY_ERROR_MSG); |
| 2068 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | |
| 2075 | SETUP_FINALLY L |
| 2076 | <code for body> |
| 2077 | POP_BLOCK |
| 2078 | LOAD_CONST <None> |
| 2079 | L: <code for finalbody> |
| 2080 | END_FINALLY |
| 2081 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2082 | The special instructions use the block stack. Each block |
| 2083 | stack entry contains the instruction that created it (here |
| 2084 | SETUP_FINALLY), the level of the value stack at the time the |
| 2085 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2086 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2087 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2088 | Pushes the current value stack level and the label |
| 2089 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2090 | POP_BLOCK: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | Pops en entry from the block stack, and pops the value |
| 2092 | stack until its level is the same as indicated on the |
| 2093 | block stack. (The label is ignored.) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2094 | END_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2095 | Pops a variable number of entries from the *value* stack |
| 2096 | and re-raises the exception they specify. The number of |
| 2097 | entries popped depends on the (pseudo) exception type. |
| 2098 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2099 | The block stack is unwound when an exception is raised: |
| 2100 | when a SETUP_FINALLY entry is found, the exception is pushed |
| 2101 | onto the value stack (and the exception condition is cleared), |
| 2102 | and the interpreter jumps to the label gotten from the block |
| 2103 | stack. |
| 2104 | */ |
| 2105 | |
| 2106 | static int |
| 2107 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2109 | basicblock *body, *end; |
| 2110 | body = compiler_new_block(c); |
| 2111 | end = compiler_new_block(c); |
| 2112 | if (body == NULL || end == NULL) |
| 2113 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2115 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2116 | compiler_use_next_block(c, body); |
| 2117 | if (!compiler_push_fblock(c, FINALLY_TRY, body)) |
| 2118 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2119 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2120 | if (!compiler_try_except(c, s)) |
| 2121 | return 0; |
| 2122 | } |
| 2123 | else { |
| 2124 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2125 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2126 | ADDOP(c, POP_BLOCK); |
| 2127 | compiler_pop_fblock(c, FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2129 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 2130 | compiler_use_next_block(c, end); |
| 2131 | if (!compiler_push_fblock(c, FINALLY_END, end)) |
| 2132 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2133 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | ADDOP(c, END_FINALLY); |
| 2135 | compiler_pop_fblock(c, FINALLY_END, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2136 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2137 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
| 2140 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2141 | 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] | 2142 | (The contents of the value stack is shown in [], with the top |
| 2143 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2144 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2145 | |
| 2146 | Value stack Label Instruction Argument |
| 2147 | [] SETUP_EXCEPT L1 |
| 2148 | [] <code for S> |
| 2149 | [] POP_BLOCK |
| 2150 | [] JUMP_FORWARD L0 |
| 2151 | |
| 2152 | [tb, val, exc] L1: DUP ) |
| 2153 | [tb, val, exc, exc] <evaluate E1> ) |
| 2154 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2155 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2156 | [tb, val, exc] POP |
| 2157 | [tb, val] <assign to V1> (or POP if no V1) |
| 2158 | [tb] POP |
| 2159 | [] <code for S1> |
| 2160 | JUMP_FORWARD L0 |
| 2161 | |
| 2162 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2163 | .............................etc....................... |
| 2164 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2165 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2166 | |
| 2167 | [] L0: <next statement> |
| 2168 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2169 | Of course, parts are not generated if Vi or Ei is not present. |
| 2170 | */ |
| 2171 | static int |
| 2172 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2174 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2175 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2176 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2177 | body = compiler_new_block(c); |
| 2178 | except = compiler_new_block(c); |
| 2179 | orelse = compiler_new_block(c); |
| 2180 | end = compiler_new_block(c); |
| 2181 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2182 | return 0; |
| 2183 | ADDOP_JREL(c, SETUP_EXCEPT, except); |
| 2184 | compiler_use_next_block(c, body); |
| 2185 | if (!compiler_push_fblock(c, EXCEPT, body)) |
| 2186 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2187 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2188 | ADDOP(c, POP_BLOCK); |
| 2189 | compiler_pop_fblock(c, EXCEPT, body); |
| 2190 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2191 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | compiler_use_next_block(c, except); |
| 2193 | for (i = 0; i < n; i++) { |
| 2194 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2195 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 2197 | return compiler_error(c, "default 'except:' must be last"); |
| 2198 | c->u->u_lineno_set = 0; |
| 2199 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 2200 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2201 | except = compiler_new_block(c); |
| 2202 | if (except == NULL) |
| 2203 | return 0; |
| 2204 | if (handler->v.ExceptHandler.type) { |
| 2205 | ADDOP(c, DUP_TOP); |
| 2206 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 2207 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 2208 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 2209 | } |
| 2210 | ADDOP(c, POP_TOP); |
| 2211 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2212 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2213 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2214 | cleanup_end = compiler_new_block(c); |
| 2215 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2216 | if (!(cleanup_end || cleanup_body)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2217 | return 0; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2218 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2219 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 2220 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2221 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2222 | /* |
| 2223 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2224 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2225 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 2226 | try: |
| 2227 | # body |
| 2228 | finally: |
| 2229 | name = None |
| 2230 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2231 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2233 | /* second try: */ |
| 2234 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 2235 | compiler_use_next_block(c, cleanup_body); |
| 2236 | if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) |
| 2237 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2239 | /* second # body */ |
| 2240 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 2241 | ADDOP(c, POP_BLOCK); |
| 2242 | ADDOP(c, POP_EXCEPT); |
| 2243 | compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2245 | /* finally: */ |
| 2246 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 2247 | compiler_use_next_block(c, cleanup_end); |
| 2248 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end)) |
| 2249 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2251 | /* name = None */ |
| 2252 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 2253 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2255 | /* del name */ |
| 2256 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2257 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2258 | ADDOP(c, END_FINALLY); |
| 2259 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2260 | } |
| 2261 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2262 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2263 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2264 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 2265 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2266 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2267 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 2268 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2269 | ADDOP(c, POP_TOP); |
| 2270 | compiler_use_next_block(c, cleanup_body); |
| 2271 | if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) |
| 2272 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2273 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 2274 | ADDOP(c, POP_EXCEPT); |
| 2275 | compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | } |
| 2277 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2278 | compiler_use_next_block(c, except); |
| 2279 | } |
| 2280 | ADDOP(c, END_FINALLY); |
| 2281 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2282 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | compiler_use_next_block(c, end); |
| 2284 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2288 | compiler_try(struct compiler *c, stmt_ty s) { |
| 2289 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 2290 | return compiler_try_finally(c, s); |
| 2291 | else |
| 2292 | return compiler_try_except(c, s); |
| 2293 | } |
| 2294 | |
| 2295 | |
| 2296 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2297 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 2298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2299 | /* The IMPORT_NAME opcode was already generated. This function |
| 2300 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | If there is a dot in name, we need to split it and emit a |
| 2303 | LOAD_ATTR for each name. |
| 2304 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2305 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, |
| 2306 | PyUnicode_GET_LENGTH(name), 1); |
| 2307 | if (dot == -2) |
| 2308 | return -1; |
| 2309 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | /* Consume the base module name to get the first attribute */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2311 | Py_ssize_t pos = dot + 1; |
| 2312 | while (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2313 | PyObject *attr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2314 | dot = PyUnicode_FindChar(name, '.', pos, |
| 2315 | PyUnicode_GET_LENGTH(name), 1); |
| 2316 | if (dot == -2) |
| 2317 | return -1; |
| 2318 | attr = PyUnicode_Substring(name, pos, |
| 2319 | (dot != -1) ? dot : |
| 2320 | PyUnicode_GET_LENGTH(name)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | if (!attr) |
| 2322 | return -1; |
| 2323 | ADDOP_O(c, LOAD_ATTR, attr, names); |
| 2324 | Py_DECREF(attr); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2325 | pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | static int |
| 2332 | compiler_import(struct compiler *c, stmt_ty s) |
| 2333 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | /* The Import node stores a module name like a.b.c as a single |
| 2335 | string. This is convenient for all cases except |
| 2336 | import a.b.c as d |
| 2337 | where we need to parse that string to extract the individual |
| 2338 | module names. |
| 2339 | XXX Perhaps change the representation to make this case simpler? |
| 2340 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2341 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | for (i = 0; i < n; i++) { |
| 2344 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 2345 | int r; |
| 2346 | PyObject *level; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | level = PyLong_FromLong(0); |
| 2349 | if (level == NULL) |
| 2350 | return 0; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2352 | ADDOP_O(c, LOAD_CONST, level, consts); |
| 2353 | Py_DECREF(level); |
| 2354 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 2355 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2356 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | if (alias->asname) { |
| 2358 | r = compiler_import_as(c, alias->name, alias->asname); |
| 2359 | if (!r) |
| 2360 | return r; |
| 2361 | } |
| 2362 | else { |
| 2363 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2364 | Py_ssize_t dot = PyUnicode_FindChar( |
| 2365 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 2366 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2367 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 2368 | if (tmp == NULL) |
| 2369 | return 0; |
| 2370 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2371 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2372 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2373 | Py_DECREF(tmp); |
| 2374 | } |
| 2375 | if (!r) |
| 2376 | return r; |
| 2377 | } |
| 2378 | } |
| 2379 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | static int |
| 2383 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 2384 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2385 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | PyObject *names = PyTuple_New(n); |
| 2388 | PyObject *level; |
| 2389 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 2390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | if (!empty_string) { |
| 2392 | empty_string = PyUnicode_FromString(""); |
| 2393 | if (!empty_string) |
| 2394 | return 0; |
| 2395 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | if (!names) |
| 2398 | return 0; |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 2399 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2400 | level = PyLong_FromLong(s->v.ImportFrom.level); |
| 2401 | if (!level) { |
| 2402 | Py_DECREF(names); |
| 2403 | return 0; |
| 2404 | } |
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 | /* build up the names */ |
| 2407 | for (i = 0; i < n; i++) { |
| 2408 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 2409 | Py_INCREF(alias->name); |
| 2410 | PyTuple_SET_ITEM(names, i, alias->name); |
| 2411 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
| 2414 | !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) { |
| 2415 | Py_DECREF(level); |
| 2416 | Py_DECREF(names); |
| 2417 | return compiler_error(c, "from __future__ imports must occur " |
| 2418 | "at the beginning of the file"); |
| 2419 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2420 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2421 | ADDOP_O(c, LOAD_CONST, level, consts); |
| 2422 | Py_DECREF(level); |
| 2423 | ADDOP_O(c, LOAD_CONST, names, consts); |
| 2424 | Py_DECREF(names); |
| 2425 | if (s->v.ImportFrom.module) { |
| 2426 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 2427 | } |
| 2428 | else { |
| 2429 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 2430 | } |
| 2431 | for (i = 0; i < n; i++) { |
| 2432 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 2433 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2434 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2435 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2436 | assert(n == 1); |
| 2437 | ADDOP(c, IMPORT_STAR); |
| 2438 | return 1; |
| 2439 | } |
| 2440 | |
| 2441 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 2442 | store_name = alias->name; |
| 2443 | if (alias->asname) |
| 2444 | store_name = alias->asname; |
| 2445 | |
| 2446 | if (!compiler_nameop(c, store_name, Store)) { |
| 2447 | Py_DECREF(names); |
| 2448 | return 0; |
| 2449 | } |
| 2450 | } |
| 2451 | /* remove imported module */ |
| 2452 | ADDOP(c, POP_TOP); |
| 2453 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | static int |
| 2457 | compiler_assert(struct compiler *c, stmt_ty s) |
| 2458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2459 | static PyObject *assertion_error = NULL; |
| 2460 | basicblock *end; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 2461 | PyObject* msg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2462 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 2463 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | return 1; |
| 2465 | if (assertion_error == NULL) { |
| 2466 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 2467 | if (assertion_error == NULL) |
| 2468 | return 0; |
| 2469 | } |
| 2470 | if (s->v.Assert.test->kind == Tuple_kind && |
| 2471 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 2472 | msg = PyUnicode_FromString("assertion is always true, " |
| 2473 | "perhaps remove parentheses?"); |
| 2474 | if (msg == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2475 | return 0; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 2476 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, |
| 2477 | c->c_filename, c->u->u_lineno, |
| 2478 | NULL, NULL) == -1) { |
| 2479 | Py_DECREF(msg); |
| 2480 | return 0; |
| 2481 | } |
| 2482 | Py_DECREF(msg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2483 | } |
| 2484 | VISIT(c, expr, s->v.Assert.test); |
| 2485 | end = compiler_new_block(c); |
| 2486 | if (end == NULL) |
| 2487 | return 0; |
| 2488 | ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); |
| 2489 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 2490 | if (s->v.Assert.msg) { |
| 2491 | VISIT(c, expr, s->v.Assert.msg); |
| 2492 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2493 | } |
| 2494 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 2495 | compiler_use_next_block(c, end); |
| 2496 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | static int |
| 2500 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 2501 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2502 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 2505 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 2506 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2507 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2509 | switch (s->kind) { |
| 2510 | case FunctionDef_kind: |
| 2511 | return compiler_function(c, s); |
| 2512 | case ClassDef_kind: |
| 2513 | return compiler_class(c, s); |
| 2514 | case Return_kind: |
| 2515 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2516 | return compiler_error(c, "'return' outside function"); |
| 2517 | if (s->v.Return.value) { |
| 2518 | VISIT(c, expr, s->v.Return.value); |
| 2519 | } |
| 2520 | else |
| 2521 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 2522 | ADDOP(c, RETURN_VALUE); |
| 2523 | break; |
| 2524 | case Delete_kind: |
| 2525 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 2526 | break; |
| 2527 | case Assign_kind: |
| 2528 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 2529 | VISIT(c, expr, s->v.Assign.value); |
| 2530 | for (i = 0; i < n; i++) { |
| 2531 | if (i < n - 1) |
| 2532 | ADDOP(c, DUP_TOP); |
| 2533 | VISIT(c, expr, |
| 2534 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 2535 | } |
| 2536 | break; |
| 2537 | case AugAssign_kind: |
| 2538 | return compiler_augassign(c, s); |
| 2539 | case For_kind: |
| 2540 | return compiler_for(c, s); |
| 2541 | case While_kind: |
| 2542 | return compiler_while(c, s); |
| 2543 | case If_kind: |
| 2544 | return compiler_if(c, s); |
| 2545 | case Raise_kind: |
| 2546 | n = 0; |
| 2547 | if (s->v.Raise.exc) { |
| 2548 | VISIT(c, expr, s->v.Raise.exc); |
| 2549 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2550 | if (s->v.Raise.cause) { |
| 2551 | VISIT(c, expr, s->v.Raise.cause); |
| 2552 | n++; |
| 2553 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2555 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2557 | case Try_kind: |
| 2558 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | case Assert_kind: |
| 2560 | return compiler_assert(c, s); |
| 2561 | case Import_kind: |
| 2562 | return compiler_import(c, s); |
| 2563 | case ImportFrom_kind: |
| 2564 | return compiler_from_import(c, s); |
| 2565 | case Global_kind: |
| 2566 | case Nonlocal_kind: |
| 2567 | break; |
| 2568 | case Expr_kind: |
| 2569 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 2570 | VISIT(c, expr, s->v.Expr.value); |
| 2571 | ADDOP(c, PRINT_EXPR); |
| 2572 | } |
| 2573 | else if (s->v.Expr.value->kind != Str_kind && |
| 2574 | s->v.Expr.value->kind != Num_kind) { |
| 2575 | VISIT(c, expr, s->v.Expr.value); |
| 2576 | ADDOP(c, POP_TOP); |
| 2577 | } |
| 2578 | break; |
| 2579 | case Pass_kind: |
| 2580 | break; |
| 2581 | case Break_kind: |
| 2582 | if (!compiler_in_loop(c)) |
| 2583 | return compiler_error(c, "'break' outside loop"); |
| 2584 | ADDOP(c, BREAK_LOOP); |
| 2585 | break; |
| 2586 | case Continue_kind: |
| 2587 | return compiler_continue(c); |
| 2588 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 2589 | return compiler_with(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | } |
| 2591 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | static int |
| 2595 | unaryop(unaryop_ty op) |
| 2596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2597 | switch (op) { |
| 2598 | case Invert: |
| 2599 | return UNARY_INVERT; |
| 2600 | case Not: |
| 2601 | return UNARY_NOT; |
| 2602 | case UAdd: |
| 2603 | return UNARY_POSITIVE; |
| 2604 | case USub: |
| 2605 | return UNARY_NEGATIVE; |
| 2606 | default: |
| 2607 | PyErr_Format(PyExc_SystemError, |
| 2608 | "unary op %d should not be possible", op); |
| 2609 | return 0; |
| 2610 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | static int |
| 2614 | binop(struct compiler *c, operator_ty op) |
| 2615 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2616 | switch (op) { |
| 2617 | case Add: |
| 2618 | return BINARY_ADD; |
| 2619 | case Sub: |
| 2620 | return BINARY_SUBTRACT; |
| 2621 | case Mult: |
| 2622 | return BINARY_MULTIPLY; |
| 2623 | case Div: |
| 2624 | return BINARY_TRUE_DIVIDE; |
| 2625 | case Mod: |
| 2626 | return BINARY_MODULO; |
| 2627 | case Pow: |
| 2628 | return BINARY_POWER; |
| 2629 | case LShift: |
| 2630 | return BINARY_LSHIFT; |
| 2631 | case RShift: |
| 2632 | return BINARY_RSHIFT; |
| 2633 | case BitOr: |
| 2634 | return BINARY_OR; |
| 2635 | case BitXor: |
| 2636 | return BINARY_XOR; |
| 2637 | case BitAnd: |
| 2638 | return BINARY_AND; |
| 2639 | case FloorDiv: |
| 2640 | return BINARY_FLOOR_DIVIDE; |
| 2641 | default: |
| 2642 | PyErr_Format(PyExc_SystemError, |
| 2643 | "binary op %d should not be possible", op); |
| 2644 | return 0; |
| 2645 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
| 2648 | static int |
| 2649 | cmpop(cmpop_ty op) |
| 2650 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2651 | switch (op) { |
| 2652 | case Eq: |
| 2653 | return PyCmp_EQ; |
| 2654 | case NotEq: |
| 2655 | return PyCmp_NE; |
| 2656 | case Lt: |
| 2657 | return PyCmp_LT; |
| 2658 | case LtE: |
| 2659 | return PyCmp_LE; |
| 2660 | case Gt: |
| 2661 | return PyCmp_GT; |
| 2662 | case GtE: |
| 2663 | return PyCmp_GE; |
| 2664 | case Is: |
| 2665 | return PyCmp_IS; |
| 2666 | case IsNot: |
| 2667 | return PyCmp_IS_NOT; |
| 2668 | case In: |
| 2669 | return PyCmp_IN; |
| 2670 | case NotIn: |
| 2671 | return PyCmp_NOT_IN; |
| 2672 | default: |
| 2673 | return PyCmp_BAD; |
| 2674 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2675 | } |
| 2676 | |
| 2677 | static int |
| 2678 | inplace_binop(struct compiler *c, operator_ty op) |
| 2679 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | switch (op) { |
| 2681 | case Add: |
| 2682 | return INPLACE_ADD; |
| 2683 | case Sub: |
| 2684 | return INPLACE_SUBTRACT; |
| 2685 | case Mult: |
| 2686 | return INPLACE_MULTIPLY; |
| 2687 | case Div: |
| 2688 | return INPLACE_TRUE_DIVIDE; |
| 2689 | case Mod: |
| 2690 | return INPLACE_MODULO; |
| 2691 | case Pow: |
| 2692 | return INPLACE_POWER; |
| 2693 | case LShift: |
| 2694 | return INPLACE_LSHIFT; |
| 2695 | case RShift: |
| 2696 | return INPLACE_RSHIFT; |
| 2697 | case BitOr: |
| 2698 | return INPLACE_OR; |
| 2699 | case BitXor: |
| 2700 | return INPLACE_XOR; |
| 2701 | case BitAnd: |
| 2702 | return INPLACE_AND; |
| 2703 | case FloorDiv: |
| 2704 | return INPLACE_FLOOR_DIVIDE; |
| 2705 | default: |
| 2706 | PyErr_Format(PyExc_SystemError, |
| 2707 | "inplace binary op %d should not be possible", op); |
| 2708 | return 0; |
| 2709 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | static int |
| 2713 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2714 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2715 | int op, scope; |
| 2716 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2717 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2719 | PyObject *dict = c->u->u_names; |
| 2720 | PyObject *mangled; |
| 2721 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2723 | mangled = _Py_Mangle(c->u->u_private, name); |
| 2724 | if (!mangled) |
| 2725 | return 0; |
Neil Schemenauer | 8b528b2 | 2005-10-23 18:37:42 +0000 | [diff] [blame] | 2726 | |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 2727 | assert(PyUnicode_CompareWithASCIIString(name, "None") && |
| 2728 | PyUnicode_CompareWithASCIIString(name, "True") && |
| 2729 | PyUnicode_CompareWithASCIIString(name, "False")); |
| 2730 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2731 | op = 0; |
| 2732 | optype = OP_NAME; |
| 2733 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 2734 | switch (scope) { |
| 2735 | case FREE: |
| 2736 | dict = c->u->u_freevars; |
| 2737 | optype = OP_DEREF; |
| 2738 | break; |
| 2739 | case CELL: |
| 2740 | dict = c->u->u_cellvars; |
| 2741 | optype = OP_DEREF; |
| 2742 | break; |
| 2743 | case LOCAL: |
| 2744 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 2745 | optype = OP_FAST; |
| 2746 | break; |
| 2747 | case GLOBAL_IMPLICIT: |
| 2748 | if (c->u->u_ste->ste_type == FunctionBlock && |
| 2749 | !c->u->u_ste->ste_unoptimized) |
| 2750 | optype = OP_GLOBAL; |
| 2751 | break; |
| 2752 | case GLOBAL_EXPLICIT: |
| 2753 | optype = OP_GLOBAL; |
| 2754 | break; |
| 2755 | default: |
| 2756 | /* scope can be 0 */ |
| 2757 | break; |
| 2758 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2759 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2760 | /* 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] | 2761 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | switch (optype) { |
| 2764 | case OP_DEREF: |
| 2765 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 2766 | case Load: |
| 2767 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 2768 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | case Store: op = STORE_DEREF; break; |
| 2770 | case AugLoad: |
| 2771 | case AugStore: |
| 2772 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 2773 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | case Param: |
| 2775 | default: |
| 2776 | PyErr_SetString(PyExc_SystemError, |
| 2777 | "param invalid for deref variable"); |
| 2778 | return 0; |
| 2779 | } |
| 2780 | break; |
| 2781 | case OP_FAST: |
| 2782 | switch (ctx) { |
| 2783 | case Load: op = LOAD_FAST; break; |
| 2784 | case Store: op = STORE_FAST; break; |
| 2785 | case Del: op = DELETE_FAST; break; |
| 2786 | case AugLoad: |
| 2787 | case AugStore: |
| 2788 | break; |
| 2789 | case Param: |
| 2790 | default: |
| 2791 | PyErr_SetString(PyExc_SystemError, |
| 2792 | "param invalid for local variable"); |
| 2793 | return 0; |
| 2794 | } |
| 2795 | ADDOP_O(c, op, mangled, varnames); |
| 2796 | Py_DECREF(mangled); |
| 2797 | return 1; |
| 2798 | case OP_GLOBAL: |
| 2799 | switch (ctx) { |
| 2800 | case Load: op = LOAD_GLOBAL; break; |
| 2801 | case Store: op = STORE_GLOBAL; break; |
| 2802 | case Del: op = DELETE_GLOBAL; break; |
| 2803 | case AugLoad: |
| 2804 | case AugStore: |
| 2805 | break; |
| 2806 | case Param: |
| 2807 | default: |
| 2808 | PyErr_SetString(PyExc_SystemError, |
| 2809 | "param invalid for global variable"); |
| 2810 | return 0; |
| 2811 | } |
| 2812 | break; |
| 2813 | case OP_NAME: |
| 2814 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 2815 | case Load: op = LOAD_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2816 | case Store: op = STORE_NAME; break; |
| 2817 | case Del: op = DELETE_NAME; break; |
| 2818 | case AugLoad: |
| 2819 | case AugStore: |
| 2820 | break; |
| 2821 | case Param: |
| 2822 | default: |
| 2823 | PyErr_SetString(PyExc_SystemError, |
| 2824 | "param invalid for name variable"); |
| 2825 | return 0; |
| 2826 | } |
| 2827 | break; |
| 2828 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | assert(op); |
| 2831 | arg = compiler_add_o(c, dict, mangled); |
| 2832 | Py_DECREF(mangled); |
| 2833 | if (arg < 0) |
| 2834 | return 0; |
| 2835 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2836 | } |
| 2837 | |
| 2838 | static int |
| 2839 | compiler_boolop(struct compiler *c, expr_ty e) |
| 2840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2841 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2842 | int jumpi; |
| 2843 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2844 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | assert(e->kind == BoolOp_kind); |
| 2847 | if (e->v.BoolOp.op == And) |
| 2848 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 2849 | else |
| 2850 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 2851 | end = compiler_new_block(c); |
| 2852 | if (end == NULL) |
| 2853 | return 0; |
| 2854 | s = e->v.BoolOp.values; |
| 2855 | n = asdl_seq_LEN(s) - 1; |
| 2856 | assert(n >= 0); |
| 2857 | for (i = 0; i < n; ++i) { |
| 2858 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 2859 | ADDOP_JABS(c, jumpi, end); |
| 2860 | } |
| 2861 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 2862 | compiler_use_next_block(c, end); |
| 2863 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | static int |
| 2867 | compiler_list(struct compiler *c, expr_ty e) |
| 2868 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2869 | Py_ssize_t n = asdl_seq_LEN(e->v.List.elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | if (e->v.List.ctx == Store) { |
| 2871 | int i, seen_star = 0; |
| 2872 | for (i = 0; i < n; i++) { |
| 2873 | expr_ty elt = asdl_seq_GET(e->v.List.elts, i); |
| 2874 | if (elt->kind == Starred_kind && !seen_star) { |
| 2875 | if ((i >= (1 << 8)) || |
| 2876 | (n-i-1 >= (INT_MAX >> 8))) |
| 2877 | return compiler_error(c, |
| 2878 | "too many expressions in " |
| 2879 | "star-unpacking assignment"); |
| 2880 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 2881 | seen_star = 1; |
| 2882 | asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value); |
| 2883 | } else if (elt->kind == Starred_kind) { |
| 2884 | return compiler_error(c, |
| 2885 | "two starred expressions in assignment"); |
| 2886 | } |
| 2887 | } |
| 2888 | if (!seen_star) { |
| 2889 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 2890 | } |
| 2891 | } |
| 2892 | VISIT_SEQ(c, expr, e->v.List.elts); |
| 2893 | if (e->v.List.ctx == Load) { |
| 2894 | ADDOP_I(c, BUILD_LIST, n); |
| 2895 | } |
| 2896 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | static int |
| 2900 | compiler_tuple(struct compiler *c, expr_ty e) |
| 2901 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2902 | Py_ssize_t n = asdl_seq_LEN(e->v.Tuple.elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2903 | if (e->v.Tuple.ctx == Store) { |
| 2904 | int i, seen_star = 0; |
| 2905 | for (i = 0; i < n; i++) { |
| 2906 | expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i); |
| 2907 | if (elt->kind == Starred_kind && !seen_star) { |
| 2908 | if ((i >= (1 << 8)) || |
| 2909 | (n-i-1 >= (INT_MAX >> 8))) |
| 2910 | return compiler_error(c, |
| 2911 | "too many expressions in " |
| 2912 | "star-unpacking assignment"); |
| 2913 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 2914 | seen_star = 1; |
| 2915 | asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value); |
| 2916 | } else if (elt->kind == Starred_kind) { |
| 2917 | return compiler_error(c, |
| 2918 | "two starred expressions in assignment"); |
| 2919 | } |
| 2920 | } |
| 2921 | if (!seen_star) { |
| 2922 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 2923 | } |
| 2924 | } |
| 2925 | VISIT_SEQ(c, expr, e->v.Tuple.elts); |
| 2926 | if (e->v.Tuple.ctx == Load) { |
| 2927 | ADDOP_I(c, BUILD_TUPLE, n); |
| 2928 | } |
| 2929 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | static int |
| 2933 | compiler_compare(struct compiler *c, expr_ty e) |
| 2934 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2935 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | basicblock *cleanup = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2938 | /* XXX the logic can be cleaned up for 1 or multiple comparisons */ |
| 2939 | VISIT(c, expr, e->v.Compare.left); |
| 2940 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2941 | assert(n > 0); |
| 2942 | if (n > 1) { |
| 2943 | cleanup = compiler_new_block(c); |
| 2944 | if (cleanup == NULL) |
| 2945 | return 0; |
| 2946 | VISIT(c, expr, |
| 2947 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 2948 | } |
| 2949 | for (i = 1; i < n; i++) { |
| 2950 | ADDOP(c, DUP_TOP); |
| 2951 | ADDOP(c, ROT_THREE); |
| 2952 | ADDOP_I(c, COMPARE_OP, |
| 2953 | cmpop((cmpop_ty)(asdl_seq_GET( |
| 2954 | e->v.Compare.ops, i - 1)))); |
| 2955 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 2956 | NEXT_BLOCK(c); |
| 2957 | if (i < (n - 1)) |
| 2958 | VISIT(c, expr, |
| 2959 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2960 | } |
| 2961 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1)); |
| 2962 | ADDOP_I(c, COMPARE_OP, |
| 2963 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1)))); |
| 2964 | if (n > 1) { |
| 2965 | basicblock *end = compiler_new_block(c); |
| 2966 | if (end == NULL) |
| 2967 | return 0; |
| 2968 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2969 | compiler_use_next_block(c, cleanup); |
| 2970 | ADDOP(c, ROT_TWO); |
| 2971 | ADDOP(c, POP_TOP); |
| 2972 | compiler_use_next_block(c, end); |
| 2973 | } |
| 2974 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2975 | } |
| 2976 | |
| 2977 | static int |
| 2978 | compiler_call(struct compiler *c, expr_ty e) |
| 2979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2980 | VISIT(c, expr, e->v.Call.func); |
| 2981 | return compiler_call_helper(c, 0, |
| 2982 | e->v.Call.args, |
| 2983 | e->v.Call.keywords, |
| 2984 | e->v.Call.starargs, |
| 2985 | e->v.Call.kwargs); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
| 2988 | /* shared code between compiler_call and compiler_class */ |
| 2989 | static int |
| 2990 | compiler_call_helper(struct compiler *c, |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 2991 | Py_ssize_t n, /* Args already pushed */ |
| 2992 | asdl_seq *args, |
| 2993 | asdl_seq *keywords, |
| 2994 | expr_ty starargs, |
| 2995 | expr_ty kwargs) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2996 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | int code = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | n += asdl_seq_LEN(args); |
| 3000 | VISIT_SEQ(c, expr, args); |
| 3001 | if (keywords) { |
| 3002 | VISIT_SEQ(c, keyword, keywords); |
| 3003 | n |= asdl_seq_LEN(keywords) << 8; |
| 3004 | } |
| 3005 | if (starargs) { |
| 3006 | VISIT(c, expr, starargs); |
| 3007 | code |= 1; |
| 3008 | } |
| 3009 | if (kwargs) { |
| 3010 | VISIT(c, expr, kwargs); |
| 3011 | code |= 2; |
| 3012 | } |
| 3013 | switch (code) { |
| 3014 | case 0: |
| 3015 | ADDOP_I(c, CALL_FUNCTION, n); |
| 3016 | break; |
| 3017 | case 1: |
| 3018 | ADDOP_I(c, CALL_FUNCTION_VAR, n); |
| 3019 | break; |
| 3020 | case 2: |
| 3021 | ADDOP_I(c, CALL_FUNCTION_KW, n); |
| 3022 | break; |
| 3023 | case 3: |
| 3024 | ADDOP_I(c, CALL_FUNCTION_VAR_KW, n); |
| 3025 | break; |
| 3026 | } |
| 3027 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3030 | |
| 3031 | /* List and set comprehensions and generator expressions work by creating a |
| 3032 | nested function to perform the actual iteration. This means that the |
| 3033 | iteration variables don't leak into the current scope. |
| 3034 | The defined function is called immediately following its definition, with the |
| 3035 | result of that call being the result of the expression. |
| 3036 | The LC/SC version returns the populated container, while the GE version is |
| 3037 | flagged in symtable.c as a generator, so it returns the generator object |
| 3038 | when the function is called. |
| 3039 | This code *knows* that the loop cannot contain break, continue, or return, |
| 3040 | so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops. |
| 3041 | |
| 3042 | Possible cleanups: |
| 3043 | - iterate over the generator sequence instead of using recursion |
| 3044 | */ |
| 3045 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3046 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | compiler_comprehension_generator(struct compiler *c, |
| 3048 | asdl_seq *generators, int gen_index, |
| 3049 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3050 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3051 | /* generate code for the iterator, then each of the ifs, |
| 3052 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3053 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | comprehension_ty gen; |
| 3055 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 3056 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3057 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3058 | start = compiler_new_block(c); |
| 3059 | skip = compiler_new_block(c); |
| 3060 | if_cleanup = compiler_new_block(c); |
| 3061 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 3064 | anchor == NULL) |
| 3065 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3068 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | if (gen_index == 0) { |
| 3070 | /* Receive outermost iter as an implicit argument */ |
| 3071 | c->u->u_argcount = 1; |
| 3072 | ADDOP_I(c, LOAD_FAST, 0); |
| 3073 | } |
| 3074 | else { |
| 3075 | /* Sub-iter - calculate on the fly */ |
| 3076 | VISIT(c, expr, gen->iter); |
| 3077 | ADDOP(c, GET_ITER); |
| 3078 | } |
| 3079 | compiler_use_next_block(c, start); |
| 3080 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 3081 | NEXT_BLOCK(c); |
| 3082 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3083 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | /* XXX this needs to be cleaned up...a lot! */ |
| 3085 | n = asdl_seq_LEN(gen->ifs); |
| 3086 | for (i = 0; i < n; i++) { |
| 3087 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
| 3088 | VISIT(c, expr, e); |
| 3089 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup); |
| 3090 | NEXT_BLOCK(c); |
| 3091 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3092 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | if (++gen_index < asdl_seq_LEN(generators)) |
| 3094 | if (!compiler_comprehension_generator(c, |
| 3095 | generators, gen_index, |
| 3096 | elt, val, type)) |
| 3097 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3098 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | /* only append after the last for generator */ |
| 3100 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 3101 | /* comprehension specific code */ |
| 3102 | switch (type) { |
| 3103 | case COMP_GENEXP: |
| 3104 | VISIT(c, expr, elt); |
| 3105 | ADDOP(c, YIELD_VALUE); |
| 3106 | ADDOP(c, POP_TOP); |
| 3107 | break; |
| 3108 | case COMP_LISTCOMP: |
| 3109 | VISIT(c, expr, elt); |
| 3110 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 3111 | break; |
| 3112 | case COMP_SETCOMP: |
| 3113 | VISIT(c, expr, elt); |
| 3114 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 3115 | break; |
| 3116 | case COMP_DICTCOMP: |
| 3117 | /* With 'd[k] = v', v is evaluated before k, so we do |
| 3118 | the same. */ |
| 3119 | VISIT(c, expr, val); |
| 3120 | VISIT(c, expr, elt); |
| 3121 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 3122 | break; |
| 3123 | default: |
| 3124 | return 0; |
| 3125 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | compiler_use_next_block(c, skip); |
| 3128 | } |
| 3129 | compiler_use_next_block(c, if_cleanup); |
| 3130 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 3131 | compiler_use_next_block(c, anchor); |
| 3132 | |
| 3133 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
| 3136 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3137 | compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | asdl_seq *generators, expr_ty elt, expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3139 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | PyCodeObject *co = NULL; |
| 3141 | expr_ty outermost_iter; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3142 | PyObject *qualname = NULL; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3144 | outermost_iter = ((comprehension_ty) |
| 3145 | asdl_seq_GET(generators, 0))->iter; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3146 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3147 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 3148 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3149 | goto error; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | if (type != COMP_GENEXP) { |
| 3152 | int op; |
| 3153 | switch (type) { |
| 3154 | case COMP_LISTCOMP: |
| 3155 | op = BUILD_LIST; |
| 3156 | break; |
| 3157 | case COMP_SETCOMP: |
| 3158 | op = BUILD_SET; |
| 3159 | break; |
| 3160 | case COMP_DICTCOMP: |
| 3161 | op = BUILD_MAP; |
| 3162 | break; |
| 3163 | default: |
| 3164 | PyErr_Format(PyExc_SystemError, |
| 3165 | "unknown comprehension type %d", type); |
| 3166 | goto error_in_scope; |
| 3167 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | ADDOP_I(c, op, 0); |
| 3170 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 3173 | val, type)) |
| 3174 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3176 | if (type != COMP_GENEXP) { |
| 3177 | ADDOP(c, RETURN_VALUE); |
| 3178 | } |
| 3179 | |
| 3180 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 3181 | qualname = c->u->u_qualname; |
| 3182 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 3184 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3185 | goto error; |
| 3186 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3187 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3189 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3190 | Py_DECREF(co); |
| 3191 | |
| 3192 | VISIT(c, expr, outermost_iter); |
| 3193 | ADDOP(c, GET_ITER); |
| 3194 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3195 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3196 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3198 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 3199 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | Py_XDECREF(co); |
| 3201 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3202 | } |
| 3203 | |
| 3204 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3205 | compiler_genexp(struct compiler *c, expr_ty e) |
| 3206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3207 | static identifier name; |
| 3208 | if (!name) { |
| 3209 | name = PyUnicode_FromString("<genexpr>"); |
| 3210 | if (!name) |
| 3211 | return 0; |
| 3212 | } |
| 3213 | assert(e->kind == GeneratorExp_kind); |
| 3214 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 3215 | e->v.GeneratorExp.generators, |
| 3216 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
| 3219 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3220 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 3221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | static identifier name; |
| 3223 | if (!name) { |
| 3224 | name = PyUnicode_FromString("<listcomp>"); |
| 3225 | if (!name) |
| 3226 | return 0; |
| 3227 | } |
| 3228 | assert(e->kind == ListComp_kind); |
| 3229 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 3230 | e->v.ListComp.generators, |
| 3231 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | static int |
| 3235 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 3236 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | static identifier name; |
| 3238 | if (!name) { |
| 3239 | name = PyUnicode_FromString("<setcomp>"); |
| 3240 | if (!name) |
| 3241 | return 0; |
| 3242 | } |
| 3243 | assert(e->kind == SetComp_kind); |
| 3244 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 3245 | e->v.SetComp.generators, |
| 3246 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | |
| 3250 | static int |
| 3251 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 3252 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | static identifier name; |
| 3254 | if (!name) { |
| 3255 | name = PyUnicode_FromString("<dictcomp>"); |
| 3256 | if (!name) |
| 3257 | return 0; |
| 3258 | } |
| 3259 | assert(e->kind == DictComp_kind); |
| 3260 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 3261 | e->v.DictComp.generators, |
| 3262 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | |
| 3266 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3267 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 3268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3269 | ADDOP_O(c, LOAD_CONST, k->arg, consts); |
| 3270 | VISIT(c, expr, k->value); |
| 3271 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3275 | whether they are true or false. |
| 3276 | |
| 3277 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 3278 | */ |
| 3279 | |
| 3280 | static int |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3281 | expr_constant(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3282 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | char *id; |
| 3284 | switch (e->kind) { |
| 3285 | case Ellipsis_kind: |
| 3286 | return 1; |
| 3287 | case Num_kind: |
| 3288 | return PyObject_IsTrue(e->v.Num.n); |
| 3289 | case Str_kind: |
| 3290 | return PyObject_IsTrue(e->v.Str.s); |
| 3291 | case Name_kind: |
| 3292 | /* optimize away names that can't be reassigned */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3293 | id = PyUnicode_AsUTF8(e->v.Name.id); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 3294 | if (id && strcmp(id, "__debug__") == 0) |
| 3295 | return !c->c_optimize; |
| 3296 | return -1; |
| 3297 | case NameConstant_kind: { |
| 3298 | PyObject *o = e->v.NameConstant.value; |
| 3299 | if (o == Py_None) |
| 3300 | return 0; |
| 3301 | else if (o == Py_True) |
| 3302 | return 1; |
| 3303 | else if (o == Py_False) |
| 3304 | return 0; |
| 3305 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | default: |
| 3307 | return -1; |
| 3308 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3311 | /* |
| 3312 | Implements the with statement from PEP 343. |
| 3313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3315 | |
| 3316 | with EXPR as VAR: |
| 3317 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3318 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3319 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3320 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3321 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3322 | exit = context.__exit__ # not calling it |
| 3323 | value = context.__enter__() |
| 3324 | try: |
| 3325 | VAR = value # if VAR present in the syntax |
| 3326 | BLOCK |
| 3327 | finally: |
| 3328 | if an exception was raised: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3329 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3330 | else: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3332 | exit(*exc) |
| 3333 | */ |
| 3334 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3335 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3336 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3337 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3338 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3339 | |
| 3340 | assert(s->kind == With_kind); |
| 3341 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3342 | block = compiler_new_block(c); |
| 3343 | finally = compiler_new_block(c); |
| 3344 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 3345 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3346 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3347 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3348 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3349 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3350 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3351 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3352 | compiler_use_next_block(c, block); |
| 3353 | if (!compiler_push_fblock(c, FINALLY_TRY, block)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 3354 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3357 | if (item->optional_vars) { |
| 3358 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 3359 | } |
| 3360 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 3362 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3365 | pos++; |
| 3366 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 3367 | /* BLOCK code */ |
| 3368 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 3369 | else if (!compiler_with(c, s, pos)) |
| 3370 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3371 | |
| 3372 | /* End of try block; start the finally block */ |
| 3373 | ADDOP(c, POP_BLOCK); |
| 3374 | compiler_pop_fblock(c, FINALLY_TRY, block); |
| 3375 | |
| 3376 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 3377 | compiler_use_next_block(c, finally); |
| 3378 | if (!compiler_push_fblock(c, FINALLY_END, finally)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 3379 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3380 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 3381 | /* Finally block starts; context.__exit__ is on the stack under |
| 3382 | the exception or return information. Just issue our magic |
| 3383 | opcode. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3384 | ADDOP(c, WITH_CLEANUP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 3385 | |
| 3386 | /* Finally block ends. */ |
| 3387 | ADDOP(c, END_FINALLY); |
| 3388 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 3389 | return 1; |
| 3390 | } |
| 3391 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3392 | static int |
| 3393 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 3394 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 3395 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3396 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3397 | /* If expr e has a different line number than the last expr/stmt, |
| 3398 | set a new line number for the next instruction. |
| 3399 | */ |
| 3400 | if (e->lineno > c->u->u_lineno) { |
| 3401 | c->u->u_lineno = e->lineno; |
| 3402 | c->u->u_lineno_set = 0; |
| 3403 | } |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3404 | /* Updating the column offset is always harmless. */ |
| 3405 | c->u->u_col_offset = e->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | switch (e->kind) { |
| 3407 | case BoolOp_kind: |
| 3408 | return compiler_boolop(c, e); |
| 3409 | case BinOp_kind: |
| 3410 | VISIT(c, expr, e->v.BinOp.left); |
| 3411 | VISIT(c, expr, e->v.BinOp.right); |
| 3412 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 3413 | break; |
| 3414 | case UnaryOp_kind: |
| 3415 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 3416 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 3417 | break; |
| 3418 | case Lambda_kind: |
| 3419 | return compiler_lambda(c, e); |
| 3420 | case IfExp_kind: |
| 3421 | return compiler_ifexp(c, e); |
| 3422 | case Dict_kind: |
| 3423 | n = asdl_seq_LEN(e->v.Dict.values); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 3424 | /* BUILD_MAP parameter is only used to preallocate the dictionary, |
| 3425 | it doesn't need to be exact */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n)); |
| 3427 | for (i = 0; i < n; i++) { |
| 3428 | VISIT(c, expr, |
| 3429 | (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3430 | VISIT(c, expr, |
| 3431 | (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3432 | ADDOP(c, STORE_MAP); |
| 3433 | } |
| 3434 | break; |
| 3435 | case Set_kind: |
| 3436 | n = asdl_seq_LEN(e->v.Set.elts); |
| 3437 | VISIT_SEQ(c, expr, e->v.Set.elts); |
| 3438 | ADDOP_I(c, BUILD_SET, n); |
| 3439 | break; |
| 3440 | case GeneratorExp_kind: |
| 3441 | return compiler_genexp(c, e); |
| 3442 | case ListComp_kind: |
| 3443 | return compiler_listcomp(c, e); |
| 3444 | case SetComp_kind: |
| 3445 | return compiler_setcomp(c, e); |
| 3446 | case DictComp_kind: |
| 3447 | return compiler_dictcomp(c, e); |
| 3448 | case Yield_kind: |
| 3449 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 3450 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 3451 | if (e->v.Yield.value) { |
| 3452 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | } |
| 3454 | else { |
| 3455 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 3456 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 3457 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3458 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 3459 | case YieldFrom_kind: |
| 3460 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 3461 | return compiler_error(c, "'yield' outside function"); |
| 3462 | VISIT(c, expr, e->v.YieldFrom.value); |
| 3463 | ADDOP(c, GET_ITER); |
| 3464 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 3465 | ADDOP(c, YIELD_FROM); |
| 3466 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3467 | case Compare_kind: |
| 3468 | return compiler_compare(c, e); |
| 3469 | case Call_kind: |
| 3470 | return compiler_call(c, e); |
| 3471 | case Num_kind: |
| 3472 | ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); |
| 3473 | break; |
| 3474 | case Str_kind: |
| 3475 | ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); |
| 3476 | break; |
| 3477 | case Bytes_kind: |
| 3478 | ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts); |
| 3479 | break; |
| 3480 | case Ellipsis_kind: |
| 3481 | ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); |
| 3482 | break; |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 3483 | case NameConstant_kind: |
| 3484 | ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts); |
| 3485 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3486 | /* The following exprs can be assignment targets. */ |
| 3487 | case Attribute_kind: |
| 3488 | if (e->v.Attribute.ctx != AugStore) |
| 3489 | VISIT(c, expr, e->v.Attribute.value); |
| 3490 | switch (e->v.Attribute.ctx) { |
| 3491 | case AugLoad: |
| 3492 | ADDOP(c, DUP_TOP); |
| 3493 | /* Fall through to load */ |
| 3494 | case Load: |
| 3495 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 3496 | break; |
| 3497 | case AugStore: |
| 3498 | ADDOP(c, ROT_TWO); |
| 3499 | /* Fall through to save */ |
| 3500 | case Store: |
| 3501 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 3502 | break; |
| 3503 | case Del: |
| 3504 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 3505 | break; |
| 3506 | case Param: |
| 3507 | default: |
| 3508 | PyErr_SetString(PyExc_SystemError, |
| 3509 | "param invalid in attribute expression"); |
| 3510 | return 0; |
| 3511 | } |
| 3512 | break; |
| 3513 | case Subscript_kind: |
| 3514 | switch (e->v.Subscript.ctx) { |
| 3515 | case AugLoad: |
| 3516 | VISIT(c, expr, e->v.Subscript.value); |
| 3517 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 3518 | break; |
| 3519 | case Load: |
| 3520 | VISIT(c, expr, e->v.Subscript.value); |
| 3521 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 3522 | break; |
| 3523 | case AugStore: |
| 3524 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 3525 | break; |
| 3526 | case Store: |
| 3527 | VISIT(c, expr, e->v.Subscript.value); |
| 3528 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 3529 | break; |
| 3530 | case Del: |
| 3531 | VISIT(c, expr, e->v.Subscript.value); |
| 3532 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 3533 | break; |
| 3534 | case Param: |
| 3535 | default: |
| 3536 | PyErr_SetString(PyExc_SystemError, |
| 3537 | "param invalid in subscript expression"); |
| 3538 | return 0; |
| 3539 | } |
| 3540 | break; |
| 3541 | case Starred_kind: |
| 3542 | switch (e->v.Starred.ctx) { |
| 3543 | case Store: |
| 3544 | /* In all legitimate cases, the Starred node was already replaced |
| 3545 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 3546 | return compiler_error(c, |
| 3547 | "starred assignment target must be in a list or tuple"); |
| 3548 | default: |
| 3549 | return compiler_error(c, |
| 3550 | "can use starred expression only as assignment target"); |
| 3551 | } |
| 3552 | break; |
| 3553 | case Name_kind: |
| 3554 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 3555 | /* child nodes of List and Tuple will have expr_context set */ |
| 3556 | case List_kind: |
| 3557 | return compiler_list(c, e); |
| 3558 | case Tuple_kind: |
| 3559 | return compiler_tuple(c, e); |
| 3560 | } |
| 3561 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3562 | } |
| 3563 | |
| 3564 | static int |
| 3565 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 3566 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3567 | expr_ty e = s->v.AugAssign.target; |
| 3568 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | switch (e->kind) { |
| 3573 | case Attribute_kind: |
| 3574 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
| 3575 | AugLoad, e->lineno, e->col_offset, c->c_arena); |
| 3576 | if (auge == NULL) |
| 3577 | return 0; |
| 3578 | VISIT(c, expr, auge); |
| 3579 | VISIT(c, expr, s->v.AugAssign.value); |
| 3580 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 3581 | auge->v.Attribute.ctx = AugStore; |
| 3582 | VISIT(c, expr, auge); |
| 3583 | break; |
| 3584 | case Subscript_kind: |
| 3585 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
| 3586 | AugLoad, e->lineno, e->col_offset, c->c_arena); |
| 3587 | if (auge == NULL) |
| 3588 | return 0; |
| 3589 | VISIT(c, expr, auge); |
| 3590 | VISIT(c, expr, s->v.AugAssign.value); |
| 3591 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 3592 | auge->v.Subscript.ctx = AugStore; |
| 3593 | VISIT(c, expr, auge); |
| 3594 | break; |
| 3595 | case Name_kind: |
| 3596 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 3597 | return 0; |
| 3598 | VISIT(c, expr, s->v.AugAssign.value); |
| 3599 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 3600 | return compiler_nameop(c, e->v.Name.id, Store); |
| 3601 | default: |
| 3602 | PyErr_Format(PyExc_SystemError, |
| 3603 | "invalid node type (%d) for augmented assignment", |
| 3604 | e->kind); |
| 3605 | return 0; |
| 3606 | } |
| 3607 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | static int |
| 3611 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 3612 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3613 | struct fblockinfo *f; |
| 3614 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 3615 | PyErr_SetString(PyExc_SystemError, |
| 3616 | "too many statically nested blocks"); |
| 3617 | return 0; |
| 3618 | } |
| 3619 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 3620 | f->fb_type = t; |
| 3621 | f->fb_block = b; |
| 3622 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | static void |
| 3626 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 3627 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3628 | struct compiler_unit *u = c->u; |
| 3629 | assert(u->u_nfblocks > 0); |
| 3630 | u->u_nfblocks--; |
| 3631 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 3632 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3635 | static int |
| 3636 | compiler_in_loop(struct compiler *c) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | int i; |
| 3638 | struct compiler_unit *u = c->u; |
| 3639 | for (i = 0; i < u->u_nfblocks; ++i) { |
| 3640 | if (u->u_fblock[i].fb_type == LOOP) |
| 3641 | return 1; |
| 3642 | } |
| 3643 | return 0; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3644 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3645 | /* Raises a SyntaxError and returns 0. |
| 3646 | If something goes wrong, a different exception may be raised. |
| 3647 | */ |
| 3648 | |
| 3649 | static int |
| 3650 | compiler_error(struct compiler *c, const char *errstr) |
| 3651 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 3652 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3654 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3655 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3656 | if (!loc) { |
| 3657 | Py_INCREF(Py_None); |
| 3658 | loc = Py_None; |
| 3659 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3660 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3661 | c->u->u_col_offset, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3662 | if (!u) |
| 3663 | goto exit; |
| 3664 | v = Py_BuildValue("(zO)", errstr, u); |
| 3665 | if (!v) |
| 3666 | goto exit; |
| 3667 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3668 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | Py_DECREF(loc); |
| 3670 | Py_XDECREF(u); |
| 3671 | Py_XDECREF(v); |
| 3672 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3673 | } |
| 3674 | |
| 3675 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3676 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 3677 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3678 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3679 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | /* XXX this code is duplicated */ |
| 3682 | switch (ctx) { |
| 3683 | case AugLoad: /* fall through to Load */ |
| 3684 | case Load: op = BINARY_SUBSCR; break; |
| 3685 | case AugStore:/* fall through to Store */ |
| 3686 | case Store: op = STORE_SUBSCR; break; |
| 3687 | case Del: op = DELETE_SUBSCR; break; |
| 3688 | case Param: |
| 3689 | PyErr_Format(PyExc_SystemError, |
| 3690 | "invalid %s kind %d in subscript\n", |
| 3691 | kind, ctx); |
| 3692 | return 0; |
| 3693 | } |
| 3694 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 3695 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3696 | } |
| 3697 | else if (ctx == AugStore) { |
| 3698 | ADDOP(c, ROT_THREE); |
| 3699 | } |
| 3700 | ADDOP(c, op); |
| 3701 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | static int |
| 3705 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 3706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3707 | int n = 2; |
| 3708 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3709 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3710 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 3711 | if (s->v.Slice.lower) { |
| 3712 | VISIT(c, expr, s->v.Slice.lower); |
| 3713 | } |
| 3714 | else { |
| 3715 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 3716 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3718 | if (s->v.Slice.upper) { |
| 3719 | VISIT(c, expr, s->v.Slice.upper); |
| 3720 | } |
| 3721 | else { |
| 3722 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 3723 | } |
| 3724 | |
| 3725 | if (s->v.Slice.step) { |
| 3726 | n++; |
| 3727 | VISIT(c, expr, s->v.Slice.step); |
| 3728 | } |
| 3729 | ADDOP_I(c, BUILD_SLICE, n); |
| 3730 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 3735 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3737 | switch (s->kind) { |
| 3738 | case Slice_kind: |
| 3739 | return compiler_slice(c, s, ctx); |
| 3740 | case Index_kind: |
| 3741 | VISIT(c, expr, s->v.Index.value); |
| 3742 | break; |
| 3743 | case ExtSlice_kind: |
| 3744 | default: |
| 3745 | PyErr_SetString(PyExc_SystemError, |
| 3746 | "extended slice invalid in nested slice"); |
| 3747 | return 0; |
| 3748 | } |
| 3749 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3752 | static int |
| 3753 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 3754 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3755 | char * kindname = NULL; |
| 3756 | switch (s->kind) { |
| 3757 | case Index_kind: |
| 3758 | kindname = "index"; |
| 3759 | if (ctx != AugStore) { |
| 3760 | VISIT(c, expr, s->v.Index.value); |
| 3761 | } |
| 3762 | break; |
| 3763 | case Slice_kind: |
| 3764 | kindname = "slice"; |
| 3765 | if (ctx != AugStore) { |
| 3766 | if (!compiler_slice(c, s, ctx)) |
| 3767 | return 0; |
| 3768 | } |
| 3769 | break; |
| 3770 | case ExtSlice_kind: |
| 3771 | kindname = "extended slice"; |
| 3772 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 3773 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3774 | for (i = 0; i < n; i++) { |
| 3775 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 3776 | s->v.ExtSlice.dims, i); |
| 3777 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 3778 | return 0; |
| 3779 | } |
| 3780 | ADDOP_I(c, BUILD_TUPLE, n); |
| 3781 | } |
| 3782 | break; |
| 3783 | default: |
| 3784 | PyErr_Format(PyExc_SystemError, |
| 3785 | "invalid subscript kind %d", s->kind); |
| 3786 | return 0; |
| 3787 | } |
| 3788 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3791 | /* End of the compiler section, beginning of the assembler section */ |
| 3792 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3793 | /* do depth-first search of basic block graph, starting with block. |
| 3794 | post records the block indices in post-order. |
| 3795 | |
| 3796 | XXX must handle implicit jumps from one block to next |
| 3797 | */ |
| 3798 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3799 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3800 | PyObject *a_bytecode; /* string containing bytecode */ |
| 3801 | int a_offset; /* offset into bytecode */ |
| 3802 | int a_nblocks; /* number of reachable blocks */ |
| 3803 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 3804 | PyObject *a_lnotab; /* string containing lnotab */ |
| 3805 | int a_lnotab_off; /* offset into lnotab */ |
| 3806 | int a_lineno; /* last lineno of emitted instruction */ |
| 3807 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3808 | }; |
| 3809 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3810 | static void |
| 3811 | dfs(struct compiler *c, basicblock *b, struct assembler *a) |
| 3812 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | int i; |
| 3814 | struct instr *instr = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3815 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | if (b->b_seen) |
| 3817 | return; |
| 3818 | b->b_seen = 1; |
| 3819 | if (b->b_next != NULL) |
| 3820 | dfs(c, b->b_next, a); |
| 3821 | for (i = 0; i < b->b_iused; i++) { |
| 3822 | instr = &b->b_instr[i]; |
| 3823 | if (instr->i_jrel || instr->i_jabs) |
| 3824 | dfs(c, instr->i_target, a); |
| 3825 | } |
| 3826 | a->a_postorder[a->a_nblocks++] = b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
Neal Norwitz | 2744c6c | 2005-11-13 01:08:38 +0000 | [diff] [blame] | 3829 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3830 | stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth) |
| 3831 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3832 | int i, target_depth; |
| 3833 | struct instr *instr; |
| 3834 | if (b->b_seen || b->b_startdepth >= depth) |
| 3835 | return maxdepth; |
| 3836 | b->b_seen = 1; |
| 3837 | b->b_startdepth = depth; |
| 3838 | for (i = 0; i < b->b_iused; i++) { |
| 3839 | instr = &b->b_instr[i]; |
| 3840 | depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg); |
| 3841 | if (depth > maxdepth) |
| 3842 | maxdepth = depth; |
| 3843 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 3844 | if (instr->i_jrel || instr->i_jabs) { |
| 3845 | target_depth = depth; |
| 3846 | if (instr->i_opcode == FOR_ITER) { |
| 3847 | target_depth = depth-2; |
| 3848 | } else if (instr->i_opcode == SETUP_FINALLY || |
| 3849 | instr->i_opcode == SETUP_EXCEPT) { |
| 3850 | target_depth = depth+3; |
| 3851 | if (target_depth > maxdepth) |
| 3852 | maxdepth = target_depth; |
| 3853 | } |
| 3854 | maxdepth = stackdepth_walk(c, instr->i_target, |
| 3855 | target_depth, maxdepth); |
| 3856 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 3857 | instr->i_opcode == JUMP_FORWARD) { |
| 3858 | goto out; /* remaining code is dead */ |
| 3859 | } |
| 3860 | } |
| 3861 | } |
| 3862 | if (b->b_next) |
| 3863 | maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3864 | out: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3865 | b->b_seen = 0; |
| 3866 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | /* Find the flow path that needs the largest stack. We assume that |
| 3870 | * cycles in the flow graph have no net effect on the stack depth. |
| 3871 | */ |
| 3872 | static int |
| 3873 | stackdepth(struct compiler *c) |
| 3874 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3875 | basicblock *b, *entryblock; |
| 3876 | entryblock = NULL; |
| 3877 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 3878 | b->b_seen = 0; |
| 3879 | b->b_startdepth = INT_MIN; |
| 3880 | entryblock = b; |
| 3881 | } |
| 3882 | if (!entryblock) |
| 3883 | return 0; |
| 3884 | return stackdepth_walk(c, entryblock, 0, 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | static int |
| 3888 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 3889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3890 | memset(a, 0, sizeof(struct assembler)); |
| 3891 | a->a_lineno = firstlineno; |
| 3892 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 3893 | if (!a->a_bytecode) |
| 3894 | return 0; |
| 3895 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 3896 | if (!a->a_lnotab) |
| 3897 | return 0; |
| 3898 | if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) { |
| 3899 | PyErr_NoMemory(); |
| 3900 | return 0; |
| 3901 | } |
| 3902 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 3903 | sizeof(basicblock *) * nblocks); |
| 3904 | if (!a->a_postorder) { |
| 3905 | PyErr_NoMemory(); |
| 3906 | return 0; |
| 3907 | } |
| 3908 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | static void |
| 3912 | assemble_free(struct assembler *a) |
| 3913 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3914 | Py_XDECREF(a->a_bytecode); |
| 3915 | Py_XDECREF(a->a_lnotab); |
| 3916 | if (a->a_postorder) |
| 3917 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3918 | } |
| 3919 | |
| 3920 | /* Return the size of a basic block in bytes. */ |
| 3921 | |
| 3922 | static int |
| 3923 | instrsize(struct instr *instr) |
| 3924 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3925 | if (!instr->i_hasarg) |
| 3926 | return 1; /* 1 byte for the opcode*/ |
| 3927 | if (instr->i_oparg > 0xffff) |
| 3928 | return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */ |
| 3929 | return 3; /* 1 (opcode) + 2 (oparg) */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | static int |
| 3933 | blocksize(basicblock *b) |
| 3934 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3935 | int i; |
| 3936 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3938 | for (i = 0; i < b->b_iused; i++) |
| 3939 | size += instrsize(&b->b_instr[i]); |
| 3940 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3941 | } |
| 3942 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 3943 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 3944 | the instruction's bytecode offset and line number. See |
| 3945 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 3946 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 3947 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3948 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 3949 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3950 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 3951 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3954 | d_bytecode = a->a_offset - a->a_lineno_off; |
| 3955 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3956 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3957 | assert(d_bytecode >= 0); |
| 3958 | assert(d_lineno >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3959 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3960 | if(d_bytecode == 0 && d_lineno == 0) |
| 3961 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 3962 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3963 | if (d_bytecode > 255) { |
| 3964 | int j, nbytes, ncodes = d_bytecode / 255; |
| 3965 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 3966 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 3967 | if (nbytes >= len) { |
| 3968 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 3969 | len = nbytes; |
| 3970 | else if (len <= INT_MAX / 2) |
| 3971 | len *= 2; |
| 3972 | else { |
| 3973 | PyErr_NoMemory(); |
| 3974 | return 0; |
| 3975 | } |
| 3976 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 3977 | return 0; |
| 3978 | } |
| 3979 | lnotab = (unsigned char *) |
| 3980 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 3981 | for (j = 0; j < ncodes; j++) { |
| 3982 | *lnotab++ = 255; |
| 3983 | *lnotab++ = 0; |
| 3984 | } |
| 3985 | d_bytecode -= ncodes * 255; |
| 3986 | a->a_lnotab_off += ncodes * 2; |
| 3987 | } |
| 3988 | assert(d_bytecode <= 255); |
| 3989 | if (d_lineno > 255) { |
| 3990 | int j, nbytes, ncodes = d_lineno / 255; |
| 3991 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 3992 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 3993 | if (nbytes >= len) { |
| 3994 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 3995 | len = nbytes; |
| 3996 | else if (len <= INT_MAX / 2) |
| 3997 | len *= 2; |
| 3998 | else { |
| 3999 | PyErr_NoMemory(); |
| 4000 | return 0; |
| 4001 | } |
| 4002 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 4003 | return 0; |
| 4004 | } |
| 4005 | lnotab = (unsigned char *) |
| 4006 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 4007 | *lnotab++ = d_bytecode; |
| 4008 | *lnotab++ = 255; |
| 4009 | d_bytecode = 0; |
| 4010 | for (j = 1; j < ncodes; j++) { |
| 4011 | *lnotab++ = 0; |
| 4012 | *lnotab++ = 255; |
| 4013 | } |
| 4014 | d_lineno -= ncodes * 255; |
| 4015 | a->a_lnotab_off += ncodes * 2; |
| 4016 | } |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4018 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 4019 | if (a->a_lnotab_off + 2 >= len) { |
| 4020 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 4021 | return 0; |
| 4022 | } |
| 4023 | lnotab = (unsigned char *) |
| 4024 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 4025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4026 | a->a_lnotab_off += 2; |
| 4027 | if (d_bytecode) { |
| 4028 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 4029 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4030 | } |
| 4031 | else { /* First line of a block; def stmt, etc. */ |
| 4032 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 4033 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4034 | } |
| 4035 | a->a_lineno = i->i_lineno; |
| 4036 | a->a_lineno_off = a->a_offset; |
| 4037 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4040 | /* assemble_emit() |
| 4041 | Extend the bytecode with a new instruction. |
| 4042 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 4043 | */ |
| 4044 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4045 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4046 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 4047 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4048 | int size, arg = 0, ext = 0; |
| 4049 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
| 4050 | char *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4052 | size = instrsize(i); |
| 4053 | if (i->i_hasarg) { |
| 4054 | arg = i->i_oparg; |
| 4055 | ext = arg >> 16; |
| 4056 | } |
| 4057 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 4058 | return 0; |
| 4059 | if (a->a_offset + size >= len) { |
| 4060 | if (len > PY_SSIZE_T_MAX / 2) |
| 4061 | return 0; |
| 4062 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 4063 | return 0; |
| 4064 | } |
| 4065 | code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
| 4066 | a->a_offset += size; |
| 4067 | if (size == 6) { |
| 4068 | assert(i->i_hasarg); |
| 4069 | *code++ = (char)EXTENDED_ARG; |
| 4070 | *code++ = ext & 0xff; |
| 4071 | *code++ = ext >> 8; |
| 4072 | arg &= 0xffff; |
| 4073 | } |
| 4074 | *code++ = i->i_opcode; |
| 4075 | if (i->i_hasarg) { |
| 4076 | assert(size == 3 || size == 6); |
| 4077 | *code++ = arg & 0xff; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 4078 | *code++ = arg >> 8; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4079 | } |
| 4080 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 4083 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4084 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 4085 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4086 | basicblock *b; |
| 4087 | int bsize, totsize, extended_arg_count = 0, last_extended_arg_count; |
| 4088 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 4089 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4090 | /* Compute the size of each block and fixup jump args. |
| 4091 | Replace block pointer with position in bytecode. */ |
| 4092 | do { |
| 4093 | totsize = 0; |
| 4094 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 4095 | b = a->a_postorder[i]; |
| 4096 | bsize = blocksize(b); |
| 4097 | b->b_offset = totsize; |
| 4098 | totsize += bsize; |
| 4099 | } |
| 4100 | last_extended_arg_count = extended_arg_count; |
| 4101 | extended_arg_count = 0; |
| 4102 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 4103 | bsize = b->b_offset; |
| 4104 | for (i = 0; i < b->b_iused; i++) { |
| 4105 | struct instr *instr = &b->b_instr[i]; |
| 4106 | /* Relative jumps are computed relative to |
| 4107 | the instruction pointer after fetching |
| 4108 | the jump instruction. |
| 4109 | */ |
| 4110 | bsize += instrsize(instr); |
| 4111 | if (instr->i_jabs) |
| 4112 | instr->i_oparg = instr->i_target->b_offset; |
| 4113 | else if (instr->i_jrel) { |
| 4114 | int delta = instr->i_target->b_offset - bsize; |
| 4115 | instr->i_oparg = delta; |
| 4116 | } |
| 4117 | else |
| 4118 | continue; |
| 4119 | if (instr->i_oparg > 0xffff) |
| 4120 | extended_arg_count++; |
| 4121 | } |
| 4122 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 4123 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4124 | /* XXX: This is an awful hack that could hurt performance, but |
| 4125 | on the bright side it should work until we come up |
| 4126 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 4127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4128 | The issue is that in the first loop blocksize() is called |
| 4129 | which calls instrsize() which requires i_oparg be set |
| 4130 | appropriately. There is a bootstrap problem because |
| 4131 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 4132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4133 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 4134 | The only EXTENDED_ARGs that could be popping up are |
| 4135 | ones in jump instructions. So this should converge |
| 4136 | fairly quickly. |
| 4137 | */ |
| 4138 | } while (last_extended_arg_count != extended_arg_count); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4141 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4142 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4144 | PyObject *tuple, *k, *v; |
| 4145 | Py_ssize_t i, pos = 0, size = PyDict_Size(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | tuple = PyTuple_New(size); |
| 4148 | if (tuple == NULL) |
| 4149 | return NULL; |
| 4150 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 4151 | i = PyLong_AS_LONG(v); |
| 4152 | /* The keys of the dictionary are tuples. (see compiler_add_o) |
| 4153 | The object we want is always first, though. */ |
| 4154 | k = PyTuple_GET_ITEM(k, 0); |
| 4155 | Py_INCREF(k); |
| 4156 | assert((i - offset) < size); |
| 4157 | assert((i - offset) >= 0); |
| 4158 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 4159 | } |
| 4160 | return tuple; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4161 | } |
| 4162 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4163 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4164 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4166 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4167 | int flags = 0; |
| 4168 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4169 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | e8e1459 | 2013-05-16 14:37:25 -0500 | [diff] [blame] | 4170 | flags |= CO_NEWLOCALS; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4171 | if (!ste->ste_unoptimized) |
| 4172 | flags |= CO_OPTIMIZED; |
| 4173 | if (ste->ste_nested) |
| 4174 | flags |= CO_NESTED; |
| 4175 | if (ste->ste_generator) |
| 4176 | flags |= CO_GENERATOR; |
| 4177 | if (ste->ste_varargs) |
| 4178 | flags |= CO_VARARGS; |
| 4179 | if (ste->ste_varkeywords) |
| 4180 | flags |= CO_VARKEYWORDS; |
| 4181 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 4182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4183 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 4184 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 4185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | n = PyDict_Size(c->u->u_freevars); |
| 4187 | if (n < 0) |
| 4188 | return -1; |
| 4189 | if (n == 0) { |
| 4190 | n = PyDict_Size(c->u->u_cellvars); |
| 4191 | if (n < 0) |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4192 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | if (n == 0) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4194 | flags |= CO_NOFREE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4195 | } |
| 4196 | } |
Jeremy Hylton | d7f393e | 2001-02-12 16:01:03 +0000 | [diff] [blame] | 4197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4198 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4201 | static PyCodeObject * |
| 4202 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4203 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4204 | PyObject *tmp; |
| 4205 | PyCodeObject *co = NULL; |
| 4206 | PyObject *consts = NULL; |
| 4207 | PyObject *names = NULL; |
| 4208 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4209 | PyObject *name = NULL; |
| 4210 | PyObject *freevars = NULL; |
| 4211 | PyObject *cellvars = NULL; |
| 4212 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4213 | Py_ssize_t nlocals; |
| 4214 | int nlocals_int; |
| 4215 | int flags; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4216 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4217 | tmp = dict_keys_inorder(c->u->u_consts, 0); |
| 4218 | if (!tmp) |
| 4219 | goto error; |
| 4220 | consts = PySequence_List(tmp); /* optimize_code requires a list */ |
| 4221 | Py_DECREF(tmp); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4223 | names = dict_keys_inorder(c->u->u_names, 0); |
| 4224 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 4225 | if (!consts || !names || !varnames) |
| 4226 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4227 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4228 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 4229 | if (!cellvars) |
| 4230 | goto error; |
| 4231 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); |
| 4232 | if (!freevars) |
| 4233 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | nlocals = PyDict_Size(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4236 | assert(nlocals < INT_MAX); |
| 4237 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 4238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4239 | flags = compute_code_flags(c); |
| 4240 | if (flags < 0) |
| 4241 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 4244 | if (!bytecode) |
| 4245 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4246 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4247 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 4248 | if (!tmp) |
| 4249 | goto error; |
| 4250 | Py_DECREF(consts); |
| 4251 | consts = tmp; |
| 4252 | |
| 4253 | co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame^] | 4254 | nlocals_int, stackdepth(c), flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4255 | bytecode, consts, names, varnames, |
| 4256 | freevars, cellvars, |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 4257 | c->c_filename, c->u->u_name, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | c->u->u_firstlineno, |
| 4259 | a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4260 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4261 | Py_XDECREF(consts); |
| 4262 | Py_XDECREF(names); |
| 4263 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | Py_XDECREF(name); |
| 4265 | Py_XDECREF(freevars); |
| 4266 | Py_XDECREF(cellvars); |
| 4267 | Py_XDECREF(bytecode); |
| 4268 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4269 | } |
| 4270 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4271 | |
| 4272 | /* For debugging purposes only */ |
| 4273 | #if 0 |
| 4274 | static void |
| 4275 | dump_instr(const struct instr *i) |
| 4276 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4277 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 4278 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 4279 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | *arg = '\0'; |
| 4282 | if (i->i_hasarg) |
| 4283 | sprintf(arg, "arg: %d ", i->i_oparg); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 4286 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | static void |
| 4290 | dump_basicblock(const basicblock *b) |
| 4291 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4292 | const char *seen = b->b_seen ? "seen " : ""; |
| 4293 | const char *b_return = b->b_return ? "return " : ""; |
| 4294 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 4295 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 4296 | if (b->b_instr) { |
| 4297 | int i; |
| 4298 | for (i = 0; i < b->b_iused; i++) { |
| 4299 | fprintf(stderr, " [%02d] ", i); |
| 4300 | dump_instr(b->b_instr + i); |
| 4301 | } |
| 4302 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4303 | } |
| 4304 | #endif |
| 4305 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4306 | static PyCodeObject * |
| 4307 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4309 | basicblock *b, *entryblock; |
| 4310 | struct assembler a; |
| 4311 | int i, j, nblocks; |
| 4312 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 4313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | /* Make sure every block that falls off the end returns None. |
| 4315 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 4316 | block ends with a jump or return b_next shouldn't set. |
| 4317 | */ |
| 4318 | if (!c->u->u_curblock->b_return) { |
| 4319 | NEXT_BLOCK(c); |
| 4320 | if (addNone) |
| 4321 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4322 | ADDOP(c, RETURN_VALUE); |
| 4323 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | nblocks = 0; |
| 4326 | entryblock = NULL; |
| 4327 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 4328 | nblocks++; |
| 4329 | entryblock = b; |
| 4330 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4332 | /* Set firstlineno if it wasn't explicitly set. */ |
| 4333 | if (!c->u->u_firstlineno) { |
| 4334 | if (entryblock && entryblock->b_instr) |
| 4335 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 4336 | else |
| 4337 | c->u->u_firstlineno = 1; |
| 4338 | } |
| 4339 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 4340 | goto error; |
| 4341 | dfs(c, entryblock, &a); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4343 | /* Can't modify the bytecode after computing jump offsets. */ |
| 4344 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4346 | /* Emit code in reverse postorder from dfs. */ |
| 4347 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 4348 | b = a.a_postorder[i]; |
| 4349 | for (j = 0; j < b->b_iused; j++) |
| 4350 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 4351 | goto error; |
| 4352 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 4353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 4355 | goto error; |
| 4356 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0) |
| 4357 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4359 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4360 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4361 | assemble_free(&a); |
| 4362 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 4363 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 4364 | |
| 4365 | #undef PyAST_Compile |
| 4366 | PyAPI_FUNC(PyCodeObject *) |
| 4367 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 4368 | PyArena *arena) |
| 4369 | { |
| 4370 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 4371 | } |
| 4372 | |