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