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