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