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