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