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