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