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