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