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