blob: 347a19281e663ba9b6dba51beee996c2c7bf551a [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
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 Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Guido van Rossumd8faa362007-04-27 19:54:29 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Nick Coghlan650f0d02007-04-15 12:05:43 +000042#define COMP_GENEXP 0
43#define COMP_LISTCOMP 1
44#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000045#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000048 unsigned i_jabs : 1;
49 unsigned i_jrel : 1;
50 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055};
56
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 /* Each basicblock in a compilation unit is linked via b_list in the
59 reverse order that the block are allocated. b_list points to the next
60 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061 struct basicblock_ *b_list;
62 /* number of instructions used */
63 int b_iused;
64 /* length of instruction array (b_instr) */
65 int b_ialloc;
66 /* pointer to an array of instructions, initially NULL */
67 struct instr *b_instr;
68 /* If b_next is non-NULL, it is a pointer to the next
69 block reached by normal control flow. */
70 struct basicblock_ *b_next;
71 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000072 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000073 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000074 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075 /* depth of stack upon entry of block, computed by stackdepth() */
76 int b_startdepth;
77 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079} basicblock;
80
81/* fblockinfo tracks the current frame block.
82
Jeremy Hyltone9357b22006-03-01 15:47:05 +000083A frame block is used to handle loops, try/except, and try/finally.
84It's called a frame block to distinguish it from a basic block in the
85compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086*/
87
88enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
89
90struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000091 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 basicblock *fb_block;
93};
94
95/* The following items change on entry and exit of code blocks.
96 They must be saved and restored when returning to a block.
97*/
98struct compiler_unit {
99 PySTEntryObject *u_ste;
100
101 PyObject *u_name;
102 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000103 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 the argument for opcodes that refer to those collections.
105 */
106 PyObject *u_consts; /* all constants */
107 PyObject *u_names; /* all names */
108 PyObject *u_varnames; /* local variables */
109 PyObject *u_cellvars; /* cell variables */
110 PyObject *u_freevars; /* free variables */
111
112 PyObject *u_private; /* for private name mangling */
113
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000114 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000115 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 /* Pointer to the most recently allocated block. By following b_list
117 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
122 int u_nfblocks;
123 struct fblockinfo u_fblock[CO_MAXBLOCKS];
124
125 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000126 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000127 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128 has been generated with current lineno */
129};
130
131/* This struct captures the global state of a compilation.
132
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000133The u pointer points to the current compilation unit, while units
134for enclosing blocks are stored in c_stack. The u and c_stack are
135managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136*/
137
138struct compiler {
139 const char *c_filename;
140 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142 PyCompilerFlags *c_flags;
143
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000145 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000150 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151};
152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153static int compiler_enter_scope(struct compiler *, identifier, void *, int);
154static void compiler_free(struct compiler *);
155static basicblock *compiler_new_block(struct compiler *);
156static int compiler_next_instr(struct compiler *, basicblock *);
157static int compiler_addop(struct compiler *, int);
158static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
159static int compiler_addop_i(struct compiler *, int, int);
160static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161static basicblock *compiler_use_new_block(struct compiler *);
162static int compiler_error(struct compiler *, const char *);
163static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
164
165static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
166static int compiler_visit_stmt(struct compiler *, stmt_ty);
167static int compiler_visit_keyword(struct compiler *, keyword_ty);
168static int compiler_visit_expr(struct compiler *, expr_ty);
169static int compiler_augassign(struct compiler *, stmt_ty);
170static int compiler_visit_slice(struct compiler *, slice_ty,
171 expr_context_ty);
172
173static int compiler_push_fblock(struct compiler *, enum fblocktype,
174 basicblock *);
175static void compiler_pop_fblock(struct compiler *, enum fblocktype,
176 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177/* Returns true if there is a loop on the fblock stack. */
178static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179
180static int inplace_binop(struct compiler *, operator_ty);
181static int expr_constant(expr_ty e);
182
Guido van Rossumc2e20742006-02-27 22:32:47 +0000183static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000184static int compiler_call_helper(struct compiler *c, int n,
185 asdl_seq *args,
186 asdl_seq *keywords,
187 expr_ty starargs,
188 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190static PyCodeObject *assemble(struct compiler *, int addNone);
191static PyObject *__doc__;
192
193PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000195{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 /* Name mangling: __private becomes _classname__private.
197 This is independent from how the name is used. */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000198 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
199 Py_UNICODE *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 size_t nlen, plen;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000201 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000202 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000203 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000205 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000206 p = PyUnicode_AS_UNICODE(privateobj);
207 nlen = Py_UNICODE_strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000208 /* Don't mangle __id__ or names with dots.
209
210 The only time a name with a dot can occur is when
211 we are compiling an import statement that has a
212 package name.
213
214 TODO(jhylton): Decide whether we want to support
215 mangling of the module name, e.g. __M.X.
216 */
217 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Martin v. Löwis5b222132007-06-10 09:51:05 +0000218 || Py_UNICODE_strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000219 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 /* Strip leading underscores from class name */
223 while (*p == '_')
224 p++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000225 if (*p == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000226 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000229 plen = Py_UNICODE_strlen(p);
230 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000231 if (!ident)
232 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000234 buffer = PyUnicode_AS_UNICODE(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000235 buffer[0] = '_';
Martin v. Löwis5b222132007-06-10 09:51:05 +0000236 Py_UNICODE_strncpy(buffer+1, p, plen);
237 Py_UNICODE_strcpy(buffer+1+plen, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000239}
240
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241static int
242compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000243{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246 c->c_stack = PyList_New(0);
247 if (!c->c_stack)
248 return 0;
249
250 return 1;
251}
252
253PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000254PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000255 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256{
257 struct compiler c;
258 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000259 PyCompilerFlags local_flags;
260 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000262 if (!__doc__) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000263 __doc__ = PyUnicode_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000264 if (!__doc__)
265 return NULL;
266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
268 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000269 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000271 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 c.c_future = PyFuture_FromAST(mod, filename);
273 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000274 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000276 local_flags.cf_flags = 0;
277 flags = &local_flags;
278 }
279 merged = c.c_future->ff_features | flags->cf_flags;
280 c.c_future->ff_features = merged;
281 flags->cf_flags = merged;
282 c.c_flags = flags;
283 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284
285 c.c_st = PySymtable_Build(mod, filename, c.c_future);
286 if (c.c_st == NULL) {
287 if (!PyErr_Occurred())
288 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000289 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290 }
291
292 /* XXX initialize to NULL for now, need to handle */
293 c.c_encoding = NULL;
294
295 co = compiler_mod(&c, mod);
296
Thomas Wouters1175c432006-02-27 22:49:54 +0000297 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000299 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 return co;
301}
302
303PyCodeObject *
304PyNode_Compile(struct _node *n, const char *filename)
305{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000306 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000308 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309 if (!arena)
310 return NULL;
311 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000312 if (mod)
313 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000314 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000315 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000316}
317
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000318static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000320{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 if (c->c_st)
322 PySymtable_Free(c->c_st);
323 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000326}
327
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000330{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000331 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332 PyObject *v, *k;
333 PyObject *dict = PyDict_New();
334 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336 n = PyList_Size(list);
337 for (i = 0; i < n; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000338 v = PyLong_FromLong(i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 if (!v) {
340 Py_DECREF(dict);
341 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000342 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000343 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
346 Py_XDECREF(k);
347 Py_DECREF(v);
348 Py_DECREF(dict);
349 return NULL;
350 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000351 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 return dict;
355}
356
357/* Return new dict containing names from src that match scope(s).
358
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000359src is a symbol table dictionary. If the scope of a name matches
360either scope_type or flag is set, insert it into the new dict. The
361values are integers, starting at offset and increasing by one for
362each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363*/
364
365static PyObject *
366dictbytype(PyObject *src, int scope_type, int flag, int offset)
367{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000368 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 PyObject *k, *v, *dest = PyDict_New();
370
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000371 assert(offset >= 0);
372 if (dest == NULL)
373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
375 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000376 /* XXX this should probably be a macro in symtable.h */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000377 long vi;
Christian Heimes217cfd12007-12-02 14:31:20 +0000378 assert(PyLong_Check(v));
379 vi = PyLong_AS_LONG(v);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000380 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000382 if (scope == scope_type || vi & flag) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000383 PyObject *tuple, *item = PyLong_FromLong(i);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000384 if (item == NULL) {
385 Py_DECREF(dest);
386 return NULL;
387 }
388 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000389 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000390 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
391 Py_DECREF(item);
392 Py_DECREF(dest);
393 Py_XDECREF(tuple);
394 return NULL;
395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000397 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399 }
400 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000401}
402
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403static void
404compiler_unit_check(struct compiler_unit *u)
405{
406 basicblock *block;
407 for (block = u->u_blocks; block != NULL; block = block->b_list) {
408 assert(block != (void *)0xcbcbcbcb);
409 assert(block != (void *)0xfbfbfbfb);
410 assert(block != (void *)0xdbdbdbdb);
411 if (block->b_instr != NULL) {
412 assert(block->b_ialloc > 0);
413 assert(block->b_iused > 0);
414 assert(block->b_ialloc >= block->b_iused);
415 }
416 else {
417 assert (block->b_iused == 0);
418 assert (block->b_ialloc == 0);
419 }
420 }
421}
422
423static void
424compiler_unit_free(struct compiler_unit *u)
425{
426 basicblock *b, *next;
427
428 compiler_unit_check(u);
429 b = u->u_blocks;
430 while (b != NULL) {
431 if (b->b_instr)
432 PyObject_Free((void *)b->b_instr);
433 next = b->b_list;
434 PyObject_Free((void *)b);
435 b = next;
436 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000437 Py_CLEAR(u->u_ste);
438 Py_CLEAR(u->u_name);
439 Py_CLEAR(u->u_consts);
440 Py_CLEAR(u->u_names);
441 Py_CLEAR(u->u_varnames);
442 Py_CLEAR(u->u_freevars);
443 Py_CLEAR(u->u_cellvars);
444 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 PyObject_Free(u);
446}
447
448static int
449compiler_enter_scope(struct compiler *c, identifier name, void *key,
450 int lineno)
451{
452 struct compiler_unit *u;
453
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000455 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000456 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457 PyErr_NoMemory();
458 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000459 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000460 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000462 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 u->u_ste = PySymtable_Lookup(c->c_st, key);
464 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000465 compiler_unit_free(u);
466 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 }
468 Py_INCREF(name);
469 u->u_name = name;
470 u->u_varnames = list2dict(u->u_ste->ste_varnames);
471 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000472 if (!u->u_varnames || !u->u_cellvars) {
473 compiler_unit_free(u);
474 return 0;
475 }
476
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000478 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000479 if (!u->u_freevars) {
480 compiler_unit_free(u);
481 return 0;
482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483
484 u->u_blocks = NULL;
485 u->u_tmpname = 0;
486 u->u_nfblocks = 0;
487 u->u_firstlineno = lineno;
488 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000489 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 u->u_consts = PyDict_New();
491 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000492 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return 0;
494 }
495 u->u_names = PyDict_New();
496 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000497 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 return 0;
499 }
500
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000501 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502
503 /* Push the old compiler_unit on the stack. */
504 if (c->u) {
505 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
507 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000508 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 return 0;
510 }
511 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000512 u->u_private = c->u->u_private;
513 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 }
515 c->u = u;
516
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000517 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000518 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 return 0;
520
521 return 1;
522}
523
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000524static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525compiler_exit_scope(struct compiler *c)
526{
527 int n;
528 PyObject *wrapper;
529
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000530 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 compiler_unit_free(c->u);
532 /* Restore c->u to the parent unit. */
533 n = PyList_GET_SIZE(c->c_stack) - 1;
534 if (n >= 0) {
535 wrapper = PyList_GET_ITEM(c->c_stack, n);
536 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000537 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000538 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000540 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 compiler_unit_check(c->u);
542 }
543 else
544 c->u = NULL;
545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546}
547
Guido van Rossumc2e20742006-02-27 22:32:47 +0000548/* Allocate a new "anonymous" local variable.
549 Used by list comprehensions and with statements.
550*/
551
552static PyObject *
553compiler_new_tmpname(struct compiler *c)
554{
555 char tmpname[256];
556 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000557 return PyUnicode_FromString(tmpname);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000558}
559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560/* Allocate a new block and return a pointer to it.
561 Returns NULL on error.
562*/
563
564static basicblock *
565compiler_new_block(struct compiler *c)
566{
567 basicblock *b;
568 struct compiler_unit *u;
569
570 u = c->u;
571 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000572 if (b == NULL) {
573 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000575 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000577 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 b->b_list = u->u_blocks;
579 u->u_blocks = b;
580 return b;
581}
582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583static basicblock *
584compiler_use_new_block(struct compiler *c)
585{
586 basicblock *block = compiler_new_block(c);
587 if (block == NULL)
588 return NULL;
589 c->u->u_curblock = block;
590 return block;
591}
592
593static basicblock *
594compiler_next_block(struct compiler *c)
595{
596 basicblock *block = compiler_new_block(c);
597 if (block == NULL)
598 return NULL;
599 c->u->u_curblock->b_next = block;
600 c->u->u_curblock = block;
601 return block;
602}
603
604static basicblock *
605compiler_use_next_block(struct compiler *c, basicblock *block)
606{
607 assert(block != NULL);
608 c->u->u_curblock->b_next = block;
609 c->u->u_curblock = block;
610 return block;
611}
612
613/* Returns the offset of the next instruction in the current block's
614 b_instr array. Resizes the b_instr as necessary.
615 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000616*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617
618static int
619compiler_next_instr(struct compiler *c, basicblock *b)
620{
621 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000622 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000624 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 if (b->b_instr == NULL) {
626 PyErr_NoMemory();
627 return -1;
628 }
629 b->b_ialloc = DEFAULT_BLOCK_SIZE;
630 memset((char *)b->b_instr, 0,
631 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635 size_t oldsize, newsize;
636 oldsize = b->b_ialloc * sizeof(struct instr);
637 newsize = oldsize << 1;
638 if (newsize == 0) {
639 PyErr_NoMemory();
640 return -1;
641 }
642 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000643 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000644 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645 if (tmp == NULL) {
646 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648 }
649 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
651 }
652 return b->b_iused++;
653}
654
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655/* Set the i_lineno member of the instruction at offse off if the
656 line number for the current expression/statement (?) has not
657 already been set. If it has been set, the call has no effect.
658
659 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000660*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662static void
663compiler_set_lineno(struct compiler *c, int off)
664{
665 basicblock *b;
666 if (c->u->u_lineno_set)
667 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000668 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000670 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671}
672
673static int
674opcode_stack_effect(int opcode, int oparg)
675{
676 switch (opcode) {
677 case POP_TOP:
678 return -1;
679 case ROT_TWO:
680 case ROT_THREE:
681 return 0;
682 case DUP_TOP:
683 return 1;
684 case ROT_FOUR:
685 return 0;
686
687 case UNARY_POSITIVE:
688 case UNARY_NEGATIVE:
689 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 case UNARY_INVERT:
691 return 0;
692
Nick Coghlan650f0d02007-04-15 12:05:43 +0000693 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000694 case LIST_APPEND:
695 return -2;
696
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 case BINARY_POWER:
698 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 case BINARY_MODULO:
700 case BINARY_ADD:
701 case BINARY_SUBTRACT:
702 case BINARY_SUBSCR:
703 case BINARY_FLOOR_DIVIDE:
704 case BINARY_TRUE_DIVIDE:
705 return -1;
706 case INPLACE_FLOOR_DIVIDE:
707 case INPLACE_TRUE_DIVIDE:
708 return -1;
709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710 case INPLACE_ADD:
711 case INPLACE_SUBTRACT:
712 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 case INPLACE_MODULO:
714 return -1;
715 case STORE_SUBSCR:
716 return -3;
Christian Heimes99170a52007-12-19 02:07:34 +0000717 case STORE_MAP:
718 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 case DELETE_SUBSCR:
720 return -2;
721
722 case BINARY_LSHIFT:
723 case BINARY_RSHIFT:
724 case BINARY_AND:
725 case BINARY_XOR:
726 case BINARY_OR:
727 return -1;
728 case INPLACE_POWER:
729 return -1;
730 case GET_ITER:
731 return 0;
732
733 case PRINT_EXPR:
734 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000735 case LOAD_BUILD_CLASS:
736 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 case INPLACE_LSHIFT:
738 case INPLACE_RSHIFT:
739 case INPLACE_AND:
740 case INPLACE_XOR:
741 case INPLACE_OR:
742 return -1;
743 case BREAK_LOOP:
744 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000745 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000746 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000747 case STORE_LOCALS:
748 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 case RETURN_VALUE:
750 return -1;
751 case IMPORT_STAR:
752 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 case YIELD_VALUE:
754 return 0;
755
756 case POP_BLOCK:
757 return 0;
758 case END_FINALLY:
759 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
761 case STORE_NAME:
762 return -1;
763 case DELETE_NAME:
764 return 0;
765 case UNPACK_SEQUENCE:
766 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000767 case UNPACK_EX:
768 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 case FOR_ITER:
770 return 1;
771
772 case STORE_ATTR:
773 return -2;
774 case DELETE_ATTR:
775 return -1;
776 case STORE_GLOBAL:
777 return -1;
778 case DELETE_GLOBAL:
779 return 0;
780 case DUP_TOPX:
781 return oparg;
782 case LOAD_CONST:
783 return 1;
784 case LOAD_NAME:
785 return 1;
786 case BUILD_TUPLE:
787 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000788 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 return 1-oparg;
790 case BUILD_MAP:
791 return 1;
792 case LOAD_ATTR:
793 return 0;
794 case COMPARE_OP:
795 return -1;
796 case IMPORT_NAME:
797 return 0;
798 case IMPORT_FROM:
799 return 1;
800
801 case JUMP_FORWARD:
802 case JUMP_IF_FALSE:
803 case JUMP_IF_TRUE:
804 case JUMP_ABSOLUTE:
805 return 0;
806
807 case LOAD_GLOBAL:
808 return 1;
809
810 case CONTINUE_LOOP:
811 return 0;
812 case SETUP_LOOP:
813 return 0;
814 case SETUP_EXCEPT:
815 case SETUP_FINALLY:
816 return 3; /* actually pushed by an exception */
817
818 case LOAD_FAST:
819 return 1;
820 case STORE_FAST:
821 return -1;
822 case DELETE_FAST:
823 return 0;
824
825 case RAISE_VARARGS:
826 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000827#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 case CALL_FUNCTION:
829 return -NARGS(oparg);
830 case CALL_FUNCTION_VAR:
831 case CALL_FUNCTION_KW:
832 return -NARGS(oparg)-1;
833 case CALL_FUNCTION_VAR_KW:
834 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000836 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000837 case MAKE_CLOSURE:
838 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000839#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 case BUILD_SLICE:
841 if (oparg == 3)
842 return -2;
843 else
844 return -1;
845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 case LOAD_CLOSURE:
847 return 1;
848 case LOAD_DEREF:
849 return 1;
850 case STORE_DEREF:
851 return -1;
852 default:
853 fprintf(stderr, "opcode = %d\n", opcode);
854 Py_FatalError("opcode_stack_effect()");
855
856 }
857 return 0; /* not reachable */
858}
859
860/* Add an opcode with no argument.
861 Returns 0 on failure, 1 on success.
862*/
863
864static int
865compiler_addop(struct compiler *c, int opcode)
866{
867 basicblock *b;
868 struct instr *i;
869 int off;
870 off = compiler_next_instr(c, c->u->u_curblock);
871 if (off < 0)
872 return 0;
873 b = c->u->u_curblock;
874 i = &b->b_instr[off];
875 i->i_opcode = opcode;
876 i->i_hasarg = 0;
877 if (opcode == RETURN_VALUE)
878 b->b_return = 1;
879 compiler_set_lineno(c, off);
880 return 1;
881}
882
883static int
884compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
885{
886 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000887 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000889 /* necessary to make sure types aren't coerced (e.g., int and long) */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000890 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
891 if (PyFloat_Check(o)) {
892 double d = PyFloat_AS_DOUBLE(o);
893 unsigned char* p = (unsigned char*) &d;
894 /* all we need is to make the tuple different in either the 0.0
895 * or -0.0 case from all others, just to avoid the "coercion".
896 */
897 if (*p==0 && p[sizeof(double)-1]==0)
898 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
899 else
900 t = PyTuple_Pack(2, o, o->ob_type);
901 } else {
902 t = PyTuple_Pack(2, o, o->ob_type);
903 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000904 if (t == NULL)
905 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906
907 v = PyDict_GetItem(dict, t);
908 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000909 if (PyErr_Occurred())
910 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 arg = PyDict_Size(dict);
Christian Heimes217cfd12007-12-02 14:31:20 +0000912 v = PyLong_FromLong(arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913 if (!v) {
914 Py_DECREF(t);
915 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917 if (PyDict_SetItem(dict, t, v) < 0) {
918 Py_DECREF(t);
919 Py_DECREF(v);
920 return -1;
921 }
922 Py_DECREF(v);
923 }
924 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000925 arg = PyLong_AsLong(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000927 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928}
929
930static int
931compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
932 PyObject *o)
933{
934 int arg = compiler_add_o(c, dict, o);
935 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000936 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 return compiler_addop_i(c, opcode, arg);
938}
939
940static int
941compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000942 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943{
944 int arg;
945 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
946 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000947 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 arg = compiler_add_o(c, dict, mangled);
949 Py_DECREF(mangled);
950 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000951 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 return compiler_addop_i(c, opcode, arg);
953}
954
955/* Add an opcode with an integer argument.
956 Returns 0 on failure, 1 on success.
957*/
958
959static int
960compiler_addop_i(struct compiler *c, int opcode, int oparg)
961{
962 struct instr *i;
963 int off;
964 off = compiler_next_instr(c, c->u->u_curblock);
965 if (off < 0)
966 return 0;
967 i = &c->u->u_curblock->b_instr[off];
968 i->i_opcode = opcode;
969 i->i_oparg = oparg;
970 i->i_hasarg = 1;
971 compiler_set_lineno(c, off);
972 return 1;
973}
974
975static int
976compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
977{
978 struct instr *i;
979 int off;
980
981 assert(b != NULL);
982 off = compiler_next_instr(c, c->u->u_curblock);
983 if (off < 0)
984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 i = &c->u->u_curblock->b_instr[off];
986 i->i_opcode = opcode;
987 i->i_target = b;
988 i->i_hasarg = 1;
989 if (absolute)
990 i->i_jabs = 1;
991 else
992 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000993 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 return 1;
995}
996
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000997/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
998 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 it as the current block. NEXT_BLOCK() also creates an implicit jump
1000 from the current block to the new block.
1001*/
1002
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003/* The returns inside these macros make it impossible to decref objects
1004 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005*/
1006
1007
1008#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 if (compiler_use_new_block((C)) == NULL) \
1010 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001014 if (compiler_next_block((C)) == NULL) \
1015 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016}
1017
1018#define ADDOP(C, OP) { \
1019 if (!compiler_addop((C), (OP))) \
1020 return 0; \
1021}
1022
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001023#define ADDOP_IN_SCOPE(C, OP) { \
1024 if (!compiler_addop((C), (OP))) { \
1025 compiler_exit_scope(c); \
1026 return 0; \
1027 } \
1028}
1029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030#define ADDOP_O(C, OP, O, TYPE) { \
1031 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1032 return 0; \
1033}
1034
1035#define ADDOP_NAME(C, OP, O, TYPE) { \
1036 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1037 return 0; \
1038}
1039
1040#define ADDOP_I(C, OP, O) { \
1041 if (!compiler_addop_i((C), (OP), (O))) \
1042 return 0; \
1043}
1044
1045#define ADDOP_JABS(C, OP, O) { \
1046 if (!compiler_addop_j((C), (OP), (O), 1)) \
1047 return 0; \
1048}
1049
1050#define ADDOP_JREL(C, OP, O) { \
1051 if (!compiler_addop_j((C), (OP), (O), 0)) \
1052 return 0; \
1053}
1054
1055/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1056 the ASDL name to synthesize the name of the C type and the visit function.
1057*/
1058
1059#define VISIT(C, TYPE, V) {\
1060 if (!compiler_visit_ ## TYPE((C), (V))) \
1061 return 0; \
1062}
1063
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001064#define VISIT_IN_SCOPE(C, TYPE, V) {\
1065 if (!compiler_visit_ ## TYPE((C), (V))) { \
1066 compiler_exit_scope(c); \
1067 return 0; \
1068 } \
1069}
1070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071#define VISIT_SLICE(C, V, CTX) {\
1072 if (!compiler_visit_slice((C), (V), (CTX))) \
1073 return 0; \
1074}
1075
1076#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001077 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001079 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 if (!compiler_visit_ ## TYPE((C), elt)) \
1082 return 0; \
1083 } \
1084}
1085
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001086#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001087 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001088 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001089 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001090 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001091 if (!compiler_visit_ ## TYPE((C), elt)) { \
1092 compiler_exit_scope(c); \
1093 return 0; \
1094 } \
1095 } \
1096}
1097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098static int
1099compiler_isdocstring(stmt_ty s)
1100{
1101 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001102 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 return s->v.Expr.value->kind == Str_kind;
1104}
1105
1106/* Compile a sequence of statements, checking for a docstring. */
1107
1108static int
1109compiler_body(struct compiler *c, asdl_seq *stmts)
1110{
1111 int i = 0;
1112 stmt_ty st;
1113
1114 if (!asdl_seq_LEN(stmts))
1115 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001116 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001117 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1118 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 i = 1;
1120 VISIT(c, expr, st->v.Expr.value);
1121 if (!compiler_nameop(c, __doc__, Store))
1122 return 0;
1123 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001124 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 return 1;
1127}
1128
1129static PyCodeObject *
1130compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001131{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001133 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 static PyObject *module;
1135 if (!module) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001136 module = PyUnicode_FromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 if (!module)
1138 return NULL;
1139 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001140 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1141 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001142 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 switch (mod->kind) {
1144 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001145 if (!compiler_body(c, mod->v.Module.body)) {
1146 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001148 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 break;
1150 case Interactive_kind:
1151 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001153 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 break;
1155 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001156 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001157 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 break;
1159 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001160 PyErr_SetString(PyExc_SystemError,
1161 "suite should not be possible");
1162 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001163 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001164 PyErr_Format(PyExc_SystemError,
1165 "module kind %d should not be possible",
1166 mod->kind);
1167 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 co = assemble(c, addNone);
1170 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001171 return co;
1172}
1173
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174/* The test for LOCAL must come before the test for FREE in order to
1175 handle classes where name is both local and free. The local var is
1176 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001177*/
1178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179static int
1180get_ref_type(struct compiler *c, PyObject *name)
1181{
1182 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001183 if (scope == 0) {
1184 char buf[350];
1185 PyOS_snprintf(buf, sizeof(buf),
1186 "unknown scope for %.100s in %.100s(%s) in %s\n"
1187 "symbols: %s\nlocals: %s\nglobals: %s\n",
1188 PyString_AS_STRING(name),
1189 PyString_AS_STRING(c->u->u_name),
1190 PyObject_REPR(c->u->u_ste->ste_id),
1191 c->c_filename,
1192 PyObject_REPR(c->u->u_ste->ste_symbols),
1193 PyObject_REPR(c->u->u_varnames),
1194 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001196 Py_FatalError(buf);
1197 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001198
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001199 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200}
1201
1202static int
1203compiler_lookup_arg(PyObject *dict, PyObject *name)
1204{
1205 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001206 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001208 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001210 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001212 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001213 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214}
1215
1216static int
1217compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1218{
1219 int i, free = PyCode_GetNumFree(co);
1220 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001221 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1222 ADDOP_I(c, MAKE_FUNCTION, args);
1223 return 1;
1224 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 for (i = 0; i < free; ++i) {
1226 /* Bypass com_addop_varname because it will generate
1227 LOAD_DEREF but LOAD_CLOSURE is needed.
1228 */
1229 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1230 int arg, reftype;
1231
1232 /* Special case: If a class contains a method with a
1233 free variable that has the same name as a method,
1234 the name will be considered free *and* local in the
1235 class. It should be handled by the closure, as
1236 well as by the normal name loookup logic.
1237 */
1238 reftype = get_ref_type(c, name);
1239 if (reftype == CELL)
1240 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1241 else /* (reftype == FREE) */
1242 arg = compiler_lookup_arg(c->u->u_freevars, name);
1243 if (arg == -1) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001244 fprintf(stderr,
1245 "lookup %s in %s %d %d\n"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 "freevars of %s: %s\n",
1247 PyObject_REPR(name),
1248 PyString_AS_STRING(c->u->u_name),
1249 reftype, arg,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00001250 PyUnicode_AsString(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 PyObject_REPR(co->co_freevars));
1252 Py_FatalError("compiler_make_closure()");
1253 }
1254 ADDOP_I(c, LOAD_CLOSURE, arg);
1255 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001256 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001258 ADDOP_I(c, MAKE_CLOSURE, args);
1259 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260}
1261
1262static int
1263compiler_decorators(struct compiler *c, asdl_seq* decos)
1264{
1265 int i;
1266
1267 if (!decos)
1268 return 1;
1269
1270 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 }
1273 return 1;
1274}
1275
1276static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1278 asdl_seq *kw_defaults)
1279{
1280 int i, default_count = 0;
1281 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001282 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1284 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001285 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 if (!compiler_visit_expr(c, default_)) {
1287 return -1;
1288 }
1289 default_count++;
1290 }
1291 }
1292 return default_count;
1293}
1294
1295static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001296compiler_visit_argannotation(struct compiler *c, identifier id,
1297 expr_ty annotation, PyObject *names)
1298{
1299 if (annotation) {
1300 VISIT(c, expr, annotation);
1301 if (PyList_Append(names, id))
1302 return -1;
1303 }
1304 return 0;
1305}
1306
1307static int
1308compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1309 PyObject *names)
1310{
1311 int i, error;
1312 for (i = 0; i < asdl_seq_LEN(args); i++) {
1313 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001314 error = compiler_visit_argannotation(
1315 c,
1316 arg->arg,
1317 arg->annotation,
1318 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001319 if (error)
1320 return error;
1321 }
1322 return 0;
1323}
1324
1325static int
1326compiler_visit_annotations(struct compiler *c, arguments_ty args,
1327 expr_ty returns)
1328{
Guido van Rossum0240b922007-02-26 21:23:50 +00001329 /* Push arg annotations and a list of the argument names. Return the #
1330 of items pushed. The expressions are evaluated out-of-order wrt the
1331 source code.
1332
1333 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1334 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001335 static identifier return_str;
1336 PyObject *names;
1337 int len;
1338 names = PyList_New(0);
1339 if (!names)
1340 return -1;
1341
1342 if (compiler_visit_argannotations(c, args->args, names))
1343 goto error;
1344 if (args->varargannotation &&
1345 compiler_visit_argannotation(c, args->vararg,
1346 args->varargannotation, names))
1347 goto error;
1348 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1349 goto error;
1350 if (args->kwargannotation &&
1351 compiler_visit_argannotation(c, args->kwarg,
1352 args->kwargannotation, names))
1353 goto error;
1354
1355 if (!return_str) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001356 return_str = PyUnicode_InternFromString("return");
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 if (!return_str)
1358 goto error;
1359 }
1360 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1361 goto error;
1362 }
1363
1364 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001365 if (len > 65534) {
1366 /* len must fit in 16 bits, and len is incremented below */
1367 PyErr_SetString(PyExc_SyntaxError,
1368 "too many annotations");
1369 goto error;
1370 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001371 if (len) {
1372 /* convert names to a tuple and place on stack */
1373 PyObject *elt;
1374 int i;
1375 PyObject *s = PyTuple_New(len);
1376 if (!s)
1377 goto error;
1378 for (i = 0; i < len; i++) {
1379 elt = PyList_GET_ITEM(names, i);
1380 Py_INCREF(elt);
1381 PyTuple_SET_ITEM(s, i, elt);
1382 }
1383 ADDOP_O(c, LOAD_CONST, s, consts);
1384 Py_DECREF(s);
1385 len++; /* include the just-pushed tuple */
1386 }
1387 Py_DECREF(names);
1388 return len;
1389
1390error:
1391 Py_DECREF(names);
1392 return -1;
1393}
1394
1395static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396compiler_function(struct compiler *c, stmt_ty s)
1397{
1398 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001399 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001402 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001403 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001405 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406
1407 assert(s->kind == FunctionDef_kind);
1408
1409 if (!compiler_decorators(c, decos))
1410 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 if (args->kwonlyargs) {
1412 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1413 args->kw_defaults);
1414 if (res < 0)
1415 return 0;
1416 kw_default_count = res;
1417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 if (args->defaults)
1419 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001420 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001421 if (num_annotations < 0)
1422 return 0;
1423 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1426 s->lineno))
1427 return 0;
1428
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001429 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001430 docstring = compiler_isdocstring(st);
Thomas Woutersce272b62007-09-19 21:19:28 +00001431 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001432 first_const = st->v.Expr.value->v.Str.s;
1433 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001434 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001435 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001439 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001441 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001443 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1444 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 }
1446 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001447 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 if (co == NULL)
1449 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Guido van Rossum4f72a782006-10-27 23:31:49 +00001451 arglength = asdl_seq_LEN(args->defaults);
1452 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001455 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456
Neal Norwitzc1505362006-12-28 06:47:50 +00001457 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1459 ADDOP_I(c, CALL_FUNCTION, 1);
1460 }
1461
1462 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1463}
1464
1465static int
1466compiler_class(struct compiler *c, stmt_ty s)
1467{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001468 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001470 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001471 PySTEntryObject *ste;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001472 int err, i;
1473 asdl_seq* decos = s->v.ClassDef.decorator_list;
1474
1475 if (!compiler_decorators(c, decos))
1476 return 0;
1477
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001478 /* initialize statics */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001479 if (locals == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001480 locals = PyUnicode_FromString("__locals__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001481 if (locals == NULL)
1482 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001485 /* ultimately generate code for:
1486 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1487 where:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001488 <func> is a function/closure created from the class body;
1489 it has a single argument (__locals__) where the dict
1490 (or MutableSequence) representing the locals is passed
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001491 <name> is the class name
1492 <bases> is the positional arguments and *varargs argument
1493 <keywords> is the keyword arguments and **kwds argument
1494 This borrows from compiler_call.
1495 */
1496
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001497 /* 0. Create a fake argument named __locals__ */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001498 ste = PySymtable_Lookup(c->c_st, s);
1499 if (ste == NULL)
1500 return 0;
1501 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001502 err = PyList_Append(ste->ste_varnames, locals);
1503 Py_DECREF(ste);
1504 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001505 return 0;
1506
1507 /* 1. compile the class body into a code object */
1508 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1509 return 0;
1510 /* this block represents what we do in the new scope */
1511 {
1512 /* use the class name for name mangling */
1513 Py_INCREF(s->v.ClassDef.name);
1514 c->u->u_private = s->v.ClassDef.name;
1515 /* force it to have one mandatory argument */
1516 c->u->u_argcount = 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001517 /* load the first argument (__locals__) ... */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001518 ADDOP_I(c, LOAD_FAST, 0);
1519 /* ... and store it into f_locals */
1520 ADDOP_IN_SCOPE(c, STORE_LOCALS);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001521 /* load (global) __name__ ... */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001522 str = PyUnicode_InternFromString("__name__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001523 if (!str || !compiler_nameop(c, str, Load)) {
1524 Py_XDECREF(str);
1525 compiler_exit_scope(c);
1526 return 0;
1527 }
1528 Py_DECREF(str);
1529 /* ... and store it as __module__ */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001530 str = PyUnicode_InternFromString("__module__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001531 if (!str || !compiler_nameop(c, str, Store)) {
1532 Py_XDECREF(str);
1533 compiler_exit_scope(c);
1534 return 0;
1535 }
1536 Py_DECREF(str);
1537 /* compile the body proper */
1538 if (!compiler_body(c, s->v.ClassDef.body)) {
1539 compiler_exit_scope(c);
1540 return 0;
1541 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001542 /* return the (empty) __class__ cell */
1543 str = PyUnicode_InternFromString("__class__");
1544 if (str == NULL) {
1545 compiler_exit_scope(c);
1546 return 0;
1547 }
1548 i = compiler_lookup_arg(c->u->u_cellvars, str);
1549 Py_DECREF(str);
1550 if (i == -1) {
1551 /* This happens when nobody references the cell */
1552 PyErr_Clear();
1553 /* Return None */
1554 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1555 }
1556 else {
1557 /* Return the cell where to store __class__ */
1558 ADDOP_I(c, LOAD_CLOSURE, i);
1559 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001560 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1561 /* create the code object */
1562 co = assemble(c, 1);
1563 }
1564 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001565 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 if (co == NULL)
1567 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001569 /* 2. load the 'build_class' function */
1570 ADDOP(c, LOAD_BUILD_CLASS);
1571
1572 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001573 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001574 Py_DECREF(co);
1575
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001576 /* 4. load class name */
1577 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1578
1579 /* 5. generate the rest of the code for the call */
1580 if (!compiler_call_helper(c, 2,
1581 s->v.ClassDef.bases,
1582 s->v.ClassDef.keywords,
1583 s->v.ClassDef.starargs,
1584 s->v.ClassDef.kwargs))
1585 return 0;
1586
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001587 /* 6. apply decorators */
1588 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1589 ADDOP_I(c, CALL_FUNCTION, 1);
1590 }
1591
1592 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1594 return 0;
1595 return 1;
1596}
1597
1598static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001599compiler_ifexp(struct compiler *c, expr_ty e)
1600{
1601 basicblock *end, *next;
1602
1603 assert(e->kind == IfExp_kind);
1604 end = compiler_new_block(c);
1605 if (end == NULL)
1606 return 0;
1607 next = compiler_new_block(c);
1608 if (next == NULL)
1609 return 0;
1610 VISIT(c, expr, e->v.IfExp.test);
1611 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1612 ADDOP(c, POP_TOP);
1613 VISIT(c, expr, e->v.IfExp.body);
1614 ADDOP_JREL(c, JUMP_FORWARD, end);
1615 compiler_use_next_block(c, next);
1616 ADDOP(c, POP_TOP);
1617 VISIT(c, expr, e->v.IfExp.orelse);
1618 compiler_use_next_block(c, end);
1619 return 1;
1620}
1621
1622static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623compiler_lambda(struct compiler *c, expr_ty e)
1624{
1625 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001626 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001627 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 arguments_ty args = e->v.Lambda.args;
1629 assert(e->kind == Lambda_kind);
1630
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001631 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001632 name = PyUnicode_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001633 if (!name)
1634 return 0;
1635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
Guido van Rossum4f72a782006-10-27 23:31:49 +00001637 if (args->kwonlyargs) {
1638 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1639 args->kw_defaults);
1640 if (res < 0) return 0;
1641 kw_default_count = res;
1642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (args->defaults)
1644 VISIT_SEQ(c, expr, args->defaults);
1645 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1646 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001647
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001649 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001650 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1651 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001653 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 if (co == NULL)
1655 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656
Guido van Rossum4f72a782006-10-27 23:31:49 +00001657 arglength = asdl_seq_LEN(args->defaults);
1658 arglength |= kw_default_count << 8;
1659 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001660 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661
1662 return 1;
1663}
1664
1665static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666compiler_if(struct compiler *c, stmt_ty s)
1667{
1668 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001669 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 assert(s->kind == If_kind);
1671 end = compiler_new_block(c);
1672 if (end == NULL)
1673 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001674 next = compiler_new_block(c);
1675 if (next == NULL)
1676 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001677
1678 constant = expr_constant(s->v.If.test);
1679 /* constant = 0: "if 0"
1680 * constant = 1: "if 1", "if 2", ...
1681 * constant = -1: rest */
1682 if (constant == 0) {
1683 if (s->v.If.orelse)
1684 VISIT_SEQ(c, stmt, s->v.If.orelse);
1685 } else if (constant == 1) {
1686 VISIT_SEQ(c, stmt, s->v.If.body);
1687 } else {
1688 VISIT(c, expr, s->v.If.test);
1689 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1690 ADDOP(c, POP_TOP);
1691 VISIT_SEQ(c, stmt, s->v.If.body);
1692 ADDOP_JREL(c, JUMP_FORWARD, end);
1693 compiler_use_next_block(c, next);
1694 ADDOP(c, POP_TOP);
1695 if (s->v.If.orelse)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001696 VISIT_SEQ(c, stmt, s->v.If.orelse);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001697 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 compiler_use_next_block(c, end);
1699 return 1;
1700}
1701
1702static int
1703compiler_for(struct compiler *c, stmt_ty s)
1704{
1705 basicblock *start, *cleanup, *end;
1706
1707 start = compiler_new_block(c);
1708 cleanup = compiler_new_block(c);
1709 end = compiler_new_block(c);
1710 if (start == NULL || end == NULL || cleanup == NULL)
1711 return 0;
1712 ADDOP_JREL(c, SETUP_LOOP, end);
1713 if (!compiler_push_fblock(c, LOOP, start))
1714 return 0;
1715 VISIT(c, expr, s->v.For.iter);
1716 ADDOP(c, GET_ITER);
1717 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001718 /* XXX(nnorwitz): is there a better way to handle this?
1719 for loops are special, we want to be able to trace them
1720 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001721 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 ADDOP_JREL(c, FOR_ITER, cleanup);
1723 VISIT(c, expr, s->v.For.target);
1724 VISIT_SEQ(c, stmt, s->v.For.body);
1725 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1726 compiler_use_next_block(c, cleanup);
1727 ADDOP(c, POP_BLOCK);
1728 compiler_pop_fblock(c, LOOP, start);
1729 VISIT_SEQ(c, stmt, s->v.For.orelse);
1730 compiler_use_next_block(c, end);
1731 return 1;
1732}
1733
1734static int
1735compiler_while(struct compiler *c, stmt_ty s)
1736{
1737 basicblock *loop, *orelse, *end, *anchor = NULL;
1738 int constant = expr_constant(s->v.While.test);
1739
Christian Heimes969fe572008-01-25 11:23:10 +00001740 if (constant == 0) {
1741 if (s->v.While.orelse)
1742 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 return 1;
Christian Heimes969fe572008-01-25 11:23:10 +00001744 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001745 loop = compiler_new_block(c);
1746 end = compiler_new_block(c);
1747 if (constant == -1) {
1748 anchor = compiler_new_block(c);
1749 if (anchor == NULL)
1750 return 0;
1751 }
1752 if (loop == NULL || end == NULL)
1753 return 0;
1754 if (s->v.While.orelse) {
1755 orelse = compiler_new_block(c);
1756 if (orelse == NULL)
1757 return 0;
1758 }
1759 else
1760 orelse = NULL;
1761
1762 ADDOP_JREL(c, SETUP_LOOP, end);
1763 compiler_use_next_block(c, loop);
1764 if (!compiler_push_fblock(c, LOOP, loop))
1765 return 0;
1766 if (constant == -1) {
1767 VISIT(c, expr, s->v.While.test);
1768 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1769 ADDOP(c, POP_TOP);
1770 }
1771 VISIT_SEQ(c, stmt, s->v.While.body);
1772 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1773
1774 /* XXX should the two POP instructions be in a separate block
1775 if there is no else clause ?
1776 */
1777
1778 if (constant == -1) {
1779 compiler_use_next_block(c, anchor);
1780 ADDOP(c, POP_TOP);
1781 ADDOP(c, POP_BLOCK);
1782 }
1783 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001784 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785 VISIT_SEQ(c, stmt, s->v.While.orelse);
1786 compiler_use_next_block(c, end);
1787
1788 return 1;
1789}
1790
1791static int
1792compiler_continue(struct compiler *c)
1793{
1794 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001795 static const char IN_FINALLY_ERROR_MSG[] =
1796 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 int i;
1798
1799 if (!c->u->u_nfblocks)
1800 return compiler_error(c, LOOP_ERROR_MSG);
1801 i = c->u->u_nfblocks - 1;
1802 switch (c->u->u_fblock[i].fb_type) {
1803 case LOOP:
1804 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1805 break;
1806 case EXCEPT:
1807 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001808 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1809 /* Prevent continue anywhere under a finally
1810 even if hidden in a sub-try or except. */
1811 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1812 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1813 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 if (i == -1)
1815 return compiler_error(c, LOOP_ERROR_MSG);
1816 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1817 break;
1818 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001819 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 }
1821
1822 return 1;
1823}
1824
1825/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1826
1827 SETUP_FINALLY L
1828 <code for body>
1829 POP_BLOCK
1830 LOAD_CONST <None>
1831 L: <code for finalbody>
1832 END_FINALLY
1833
1834 The special instructions use the block stack. Each block
1835 stack entry contains the instruction that created it (here
1836 SETUP_FINALLY), the level of the value stack at the time the
1837 block stack entry was created, and a label (here L).
1838
1839 SETUP_FINALLY:
1840 Pushes the current value stack level and the label
1841 onto the block stack.
1842 POP_BLOCK:
1843 Pops en entry from the block stack, and pops the value
1844 stack until its level is the same as indicated on the
1845 block stack. (The label is ignored.)
1846 END_FINALLY:
1847 Pops a variable number of entries from the *value* stack
1848 and re-raises the exception they specify. The number of
1849 entries popped depends on the (pseudo) exception type.
1850
1851 The block stack is unwound when an exception is raised:
1852 when a SETUP_FINALLY entry is found, the exception is pushed
1853 onto the value stack (and the exception condition is cleared),
1854 and the interpreter jumps to the label gotten from the block
1855 stack.
1856*/
1857
1858static int
1859compiler_try_finally(struct compiler *c, stmt_ty s)
1860{
1861 basicblock *body, *end;
1862 body = compiler_new_block(c);
1863 end = compiler_new_block(c);
1864 if (body == NULL || end == NULL)
1865 return 0;
1866
1867 ADDOP_JREL(c, SETUP_FINALLY, end);
1868 compiler_use_next_block(c, body);
1869 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1870 return 0;
1871 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1872 ADDOP(c, POP_BLOCK);
1873 compiler_pop_fblock(c, FINALLY_TRY, body);
1874
1875 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1876 compiler_use_next_block(c, end);
1877 if (!compiler_push_fblock(c, FINALLY_END, end))
1878 return 0;
1879 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1880 ADDOP(c, END_FINALLY);
1881 compiler_pop_fblock(c, FINALLY_END, end);
1882
1883 return 1;
1884}
1885
1886/*
1887 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1888 (The contents of the value stack is shown in [], with the top
1889 at the right; 'tb' is trace-back info, 'val' the exception's
1890 associated value, and 'exc' the exception.)
1891
1892 Value stack Label Instruction Argument
1893 [] SETUP_EXCEPT L1
1894 [] <code for S>
1895 [] POP_BLOCK
1896 [] JUMP_FORWARD L0
1897
1898 [tb, val, exc] L1: DUP )
1899 [tb, val, exc, exc] <evaluate E1> )
1900 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1901 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1902 [tb, val, exc, 1] POP )
1903 [tb, val, exc] POP
1904 [tb, val] <assign to V1> (or POP if no V1)
1905 [tb] POP
1906 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001907 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908
1909 [tb, val, exc, 0] L2: POP
1910 [tb, val, exc] DUP
1911 .............................etc.......................
1912
1913 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001914 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915
1916 [] L0: <next statement>
1917
1918 Of course, parts are not generated if Vi or Ei is not present.
1919*/
1920static int
1921compiler_try_except(struct compiler *c, stmt_ty s)
1922{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001923 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924 int i, n;
1925
1926 body = compiler_new_block(c);
1927 except = compiler_new_block(c);
1928 orelse = compiler_new_block(c);
1929 end = compiler_new_block(c);
1930 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1931 return 0;
1932 ADDOP_JREL(c, SETUP_EXCEPT, except);
1933 compiler_use_next_block(c, body);
1934 if (!compiler_push_fblock(c, EXCEPT, body))
1935 return 0;
1936 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1937 ADDOP(c, POP_BLOCK);
1938 compiler_pop_fblock(c, EXCEPT, body);
1939 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1940 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1941 compiler_use_next_block(c, except);
1942 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001943 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 s->v.TryExcept.handlers, i);
1945 if (!handler->type && i < n-1)
1946 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001947 c->u->u_lineno_set = 0;
1948 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 except = compiler_new_block(c);
1950 if (except == NULL)
1951 return 0;
1952 if (handler->type) {
1953 ADDOP(c, DUP_TOP);
1954 VISIT(c, expr, handler->type);
1955 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1956 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1957 ADDOP(c, POP_TOP);
1958 }
1959 ADDOP(c, POP_TOP);
1960 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001961 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001962
1963 cleanup_end = compiler_new_block(c);
1964 cleanup_body = compiler_new_block(c);
1965 if(!(cleanup_end || cleanup_body))
1966 return 0;
1967
Guido van Rossum16be03e2007-01-10 18:51:35 +00001968 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001969 ADDOP(c, POP_TOP);
1970
1971 /*
1972 try:
1973 # body
1974 except type as name:
1975 try:
1976 # body
1977 finally:
1978 name = None
1979 del name
1980 */
1981
1982 /* second try: */
1983 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1984 compiler_use_next_block(c, cleanup_body);
1985 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1986 return 0;
1987
1988 /* second # body */
1989 VISIT_SEQ(c, stmt, handler->body);
1990 ADDOP(c, POP_BLOCK);
1991 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1992
1993 /* finally: */
1994 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1995 compiler_use_next_block(c, cleanup_end);
1996 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1997 return 0;
1998
1999 /* name = None */
2000 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00002001 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002002
Guido van Rossum16be03e2007-01-10 18:51:35 +00002003 /* del name */
2004 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002005
2006 ADDOP(c, END_FINALLY);
2007 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 }
2009 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002010 ADDOP(c, POP_TOP);
2011 ADDOP(c, POP_TOP);
2012 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 ADDOP_JREL(c, JUMP_FORWARD, end);
2015 compiler_use_next_block(c, except);
2016 if (handler->type)
2017 ADDOP(c, POP_TOP);
2018 }
2019 ADDOP(c, END_FINALLY);
2020 compiler_use_next_block(c, orelse);
2021 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2022 compiler_use_next_block(c, end);
2023 return 1;
2024}
2025
2026static int
2027compiler_import_as(struct compiler *c, identifier name, identifier asname)
2028{
2029 /* The IMPORT_NAME opcode was already generated. This function
2030 merely needs to bind the result to a name.
2031
2032 If there is a dot in name, we need to split it and emit a
2033 LOAD_ATTR for each name.
2034 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002035 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2036 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 if (dot) {
2038 /* Consume the base module name to get the first attribute */
2039 src = dot + 1;
2040 while (dot) {
2041 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002042 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002043 dot = Py_UNICODE_strchr(src, '.');
2044 attr = PyUnicode_FromUnicode(src,
2045 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002046 if (!attr)
2047 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002049 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 src = dot + 1;
2051 }
2052 }
2053 return compiler_nameop(c, asname, Store);
2054}
2055
2056static int
2057compiler_import(struct compiler *c, stmt_ty s)
2058{
2059 /* The Import node stores a module name like a.b.c as a single
2060 string. This is convenient for all cases except
2061 import a.b.c as d
2062 where we need to parse that string to extract the individual
2063 module names.
2064 XXX Perhaps change the representation to make this case simpler?
2065 */
2066 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002069 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002071 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072
Christian Heimes217cfd12007-12-02 14:31:20 +00002073 level = PyLong_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002074 if (level == NULL)
2075 return 0;
2076
2077 ADDOP_O(c, LOAD_CONST, level, consts);
2078 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2080 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2081
2082 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002083 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002084 if (!r)
2085 return r;
2086 }
2087 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002089 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2090 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002092 tmp = PyUnicode_FromUnicode(base,
2093 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 r = compiler_nameop(c, tmp, Store);
2095 if (dot) {
2096 Py_DECREF(tmp);
2097 }
2098 if (!r)
2099 return r;
2100 }
2101 }
2102 return 1;
2103}
2104
2105static int
2106compiler_from_import(struct compiler *c, stmt_ty s)
2107{
2108 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109
2110 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002111 PyObject *level;
2112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 if (!names)
2114 return 0;
2115
Christian Heimes217cfd12007-12-02 14:31:20 +00002116 level = PyLong_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002117 if (!level) {
2118 Py_DECREF(names);
2119 return 0;
2120 }
2121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 /* build up the names */
2123 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002124 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 Py_INCREF(alias->name);
2126 PyTuple_SET_ITEM(names, i, alias->name);
2127 }
2128
2129 if (s->lineno > c->c_future->ff_lineno) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002130 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2131 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002132 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 Py_DECREF(names);
2134 return compiler_error(c,
2135 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002136 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137
2138 }
2139 }
2140
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002141 ADDOP_O(c, LOAD_CONST, level, consts);
2142 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002144 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2146 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002147 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 identifier store_name;
2149
Martin v. Löwis5b222132007-06-10 09:51:05 +00002150 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 assert(n == 1);
2152 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002153 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 }
2155
2156 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2157 store_name = alias->name;
2158 if (alias->asname)
2159 store_name = alias->asname;
2160
2161 if (!compiler_nameop(c, store_name, Store)) {
2162 Py_DECREF(names);
2163 return 0;
2164 }
2165 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002166 /* remove imported module */
2167 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 return 1;
2169}
2170
2171static int
2172compiler_assert(struct compiler *c, stmt_ty s)
2173{
2174 static PyObject *assertion_error = NULL;
2175 basicblock *end;
2176
2177 if (Py_OptimizeFlag)
2178 return 1;
2179 if (assertion_error == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002180 assertion_error = PyUnicode_FromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 if (assertion_error == NULL)
2182 return 0;
2183 }
2184 VISIT(c, expr, s->v.Assert.test);
2185 end = compiler_new_block(c);
2186 if (end == NULL)
2187 return 0;
2188 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2189 ADDOP(c, POP_TOP);
2190 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2191 if (s->v.Assert.msg) {
2192 VISIT(c, expr, s->v.Assert.msg);
Collin Winter828f04a2007-08-31 00:04:24 +00002193 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 }
Collin Winter828f04a2007-08-31 00:04:24 +00002195 ADDOP_I(c, RAISE_VARARGS, 1);
Neal Norwitz51abbc72005-12-18 07:06:23 +00002196 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 ADDOP(c, POP_TOP);
2198 return 1;
2199}
2200
2201static int
2202compiler_visit_stmt(struct compiler *c, stmt_ty s)
2203{
2204 int i, n;
2205
Thomas Wouters89f507f2006-12-13 04:49:30 +00002206 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002208 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002211 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002213 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002215 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 if (c->u->u_ste->ste_type != FunctionBlock)
2217 return compiler_error(c, "'return' outside function");
2218 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 VISIT(c, expr, s->v.Return.value);
2220 }
2221 else
2222 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2223 ADDOP(c, RETURN_VALUE);
2224 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002225 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 VISIT_SEQ(c, expr, s->v.Delete.targets)
2227 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002228 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 n = asdl_seq_LEN(s->v.Assign.targets);
2230 VISIT(c, expr, s->v.Assign.value);
2231 for (i = 0; i < n; i++) {
2232 if (i < n - 1)
2233 ADDOP(c, DUP_TOP);
2234 VISIT(c, expr,
2235 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2236 }
2237 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002238 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002240 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002242 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002244 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002246 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 n = 0;
Collin Winter828f04a2007-08-31 00:04:24 +00002248 if (s->v.Raise.exc) {
2249 VISIT(c, expr, s->v.Raise.exc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 n++;
Collin Winter828f04a2007-08-31 00:04:24 +00002251 if (s->v.Raise.cause) {
2252 VISIT(c, expr, s->v.Raise.cause);
2253 n++;
2254 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 }
2256 ADDOP_I(c, RAISE_VARARGS, n);
2257 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002258 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002260 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002262 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002264 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002266 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002268 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002269 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002271 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002273 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 ADDOP(c, PRINT_EXPR);
2275 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002276 else if (s->v.Expr.value->kind != Str_kind &&
2277 s->v.Expr.value->kind != Num_kind) {
2278 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 ADDOP(c, POP_TOP);
2280 }
2281 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002282 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002284 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002285 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 return compiler_error(c, "'break' outside loop");
2287 ADDOP(c, BREAK_LOOP);
2288 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002289 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002291 case With_kind:
2292 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 }
2294 return 1;
2295}
2296
2297static int
2298unaryop(unaryop_ty op)
2299{
2300 switch (op) {
2301 case Invert:
2302 return UNARY_INVERT;
2303 case Not:
2304 return UNARY_NOT;
2305 case UAdd:
2306 return UNARY_POSITIVE;
2307 case USub:
2308 return UNARY_NEGATIVE;
2309 }
2310 return 0;
2311}
2312
2313static int
2314binop(struct compiler *c, operator_ty op)
2315{
2316 switch (op) {
2317 case Add:
2318 return BINARY_ADD;
2319 case Sub:
2320 return BINARY_SUBTRACT;
2321 case Mult:
2322 return BINARY_MULTIPLY;
2323 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002324 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 case Mod:
2326 return BINARY_MODULO;
2327 case Pow:
2328 return BINARY_POWER;
2329 case LShift:
2330 return BINARY_LSHIFT;
2331 case RShift:
2332 return BINARY_RSHIFT;
2333 case BitOr:
2334 return BINARY_OR;
2335 case BitXor:
2336 return BINARY_XOR;
2337 case BitAnd:
2338 return BINARY_AND;
2339 case FloorDiv:
2340 return BINARY_FLOOR_DIVIDE;
2341 }
2342 return 0;
2343}
2344
2345static int
2346cmpop(cmpop_ty op)
2347{
2348 switch (op) {
2349 case Eq:
2350 return PyCmp_EQ;
2351 case NotEq:
2352 return PyCmp_NE;
2353 case Lt:
2354 return PyCmp_LT;
2355 case LtE:
2356 return PyCmp_LE;
2357 case Gt:
2358 return PyCmp_GT;
2359 case GtE:
2360 return PyCmp_GE;
2361 case Is:
2362 return PyCmp_IS;
2363 case IsNot:
2364 return PyCmp_IS_NOT;
2365 case In:
2366 return PyCmp_IN;
2367 case NotIn:
2368 return PyCmp_NOT_IN;
2369 }
2370 return PyCmp_BAD;
2371}
2372
2373static int
2374inplace_binop(struct compiler *c, operator_ty op)
2375{
2376 switch (op) {
2377 case Add:
2378 return INPLACE_ADD;
2379 case Sub:
2380 return INPLACE_SUBTRACT;
2381 case Mult:
2382 return INPLACE_MULTIPLY;
2383 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002384 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 case Mod:
2386 return INPLACE_MODULO;
2387 case Pow:
2388 return INPLACE_POWER;
2389 case LShift:
2390 return INPLACE_LSHIFT;
2391 case RShift:
2392 return INPLACE_RSHIFT;
2393 case BitOr:
2394 return INPLACE_OR;
2395 case BitXor:
2396 return INPLACE_XOR;
2397 case BitAnd:
2398 return INPLACE_AND;
2399 case FloorDiv:
2400 return INPLACE_FLOOR_DIVIDE;
2401 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002402 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002403 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 return 0;
2405}
2406
2407static int
2408compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2409{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002410 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002411 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2412
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002413 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002414 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 /* XXX AugStore isn't used anywhere! */
2416
2417 /* First check for assignment to __debug__. Param? */
2418 if ((ctx == Store || ctx == AugStore || ctx == Del)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002419 && !PyUnicode_CompareWithASCIIString(name, "__debug__")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 return compiler_error(c, "can not assign to __debug__");
2421 }
2422
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002423 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002424 if (!mangled)
2425 return 0;
2426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 op = 0;
2428 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002429 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 switch (scope) {
2431 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002432 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 optype = OP_DEREF;
2434 break;
2435 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002436 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 optype = OP_DEREF;
2438 break;
2439 case LOCAL:
2440 if (c->u->u_ste->ste_type == FunctionBlock)
2441 optype = OP_FAST;
2442 break;
2443 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002444 if (c->u->u_ste->ste_type == FunctionBlock &&
2445 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 optype = OP_GLOBAL;
2447 break;
2448 case GLOBAL_EXPLICIT:
2449 optype = OP_GLOBAL;
2450 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002451 default:
2452 /* scope can be 0 */
2453 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 }
2455
2456 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002457 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458
2459 switch (optype) {
2460 case OP_DEREF:
2461 switch (ctx) {
2462 case Load: op = LOAD_DEREF; break;
2463 case Store: op = STORE_DEREF; break;
2464 case AugLoad:
2465 case AugStore:
2466 break;
2467 case Del:
2468 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002469 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002471 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002472 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002475 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002476 PyErr_SetString(PyExc_SystemError,
2477 "param invalid for deref variable");
2478 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 }
2480 break;
2481 case OP_FAST:
2482 switch (ctx) {
2483 case Load: op = LOAD_FAST; break;
2484 case Store: op = STORE_FAST; break;
2485 case Del: op = DELETE_FAST; break;
2486 case AugLoad:
2487 case AugStore:
2488 break;
2489 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002490 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002491 PyErr_SetString(PyExc_SystemError,
2492 "param invalid for local variable");
2493 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002495 ADDOP_O(c, op, mangled, varnames);
2496 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 return 1;
2498 case OP_GLOBAL:
2499 switch (ctx) {
2500 case Load: op = LOAD_GLOBAL; break;
2501 case Store: op = STORE_GLOBAL; break;
2502 case Del: op = DELETE_GLOBAL; break;
2503 case AugLoad:
2504 case AugStore:
2505 break;
2506 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002507 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002508 PyErr_SetString(PyExc_SystemError,
2509 "param invalid for global variable");
2510 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511 }
2512 break;
2513 case OP_NAME:
2514 switch (ctx) {
2515 case Load: op = LOAD_NAME; break;
2516 case Store: op = STORE_NAME; break;
2517 case Del: op = DELETE_NAME; break;
2518 case AugLoad:
2519 case AugStore:
2520 break;
2521 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002522 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002523 PyErr_SetString(PyExc_SystemError,
2524 "param invalid for name variable");
2525 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 }
2527 break;
2528 }
2529
2530 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002531 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002532 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002533 if (arg < 0)
2534 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002535 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536}
2537
2538static int
2539compiler_boolop(struct compiler *c, expr_ty e)
2540{
2541 basicblock *end;
2542 int jumpi, i, n;
2543 asdl_seq *s;
2544
2545 assert(e->kind == BoolOp_kind);
2546 if (e->v.BoolOp.op == And)
2547 jumpi = JUMP_IF_FALSE;
2548 else
2549 jumpi = JUMP_IF_TRUE;
2550 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002551 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 return 0;
2553 s = e->v.BoolOp.values;
2554 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002555 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002557 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 ADDOP_JREL(c, jumpi, end);
2559 ADDOP(c, POP_TOP)
2560 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002561 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 compiler_use_next_block(c, end);
2563 return 1;
2564}
2565
2566static int
2567compiler_list(struct compiler *c, expr_ty e)
2568{
2569 int n = asdl_seq_LEN(e->v.List.elts);
2570 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002571 int i, seen_star = 0;
2572 for (i = 0; i < n; i++) {
2573 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2574 if (elt->kind == Starred_kind && !seen_star) {
2575 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2576 seen_star = 1;
2577 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2578 } else if (elt->kind == Starred_kind) {
2579 return compiler_error(c,
2580 "two starred expressions in assignment");
2581 }
2582 }
2583 if (!seen_star) {
2584 ADDOP_I(c, UNPACK_SEQUENCE, n);
2585 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 }
2587 VISIT_SEQ(c, expr, e->v.List.elts);
2588 if (e->v.List.ctx == Load) {
2589 ADDOP_I(c, BUILD_LIST, n);
2590 }
2591 return 1;
2592}
2593
2594static int
2595compiler_tuple(struct compiler *c, expr_ty e)
2596{
2597 int n = asdl_seq_LEN(e->v.Tuple.elts);
2598 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002599 int i, seen_star = 0;
2600 for (i = 0; i < n; i++) {
2601 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2602 if (elt->kind == Starred_kind && !seen_star) {
2603 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2604 seen_star = 1;
2605 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2606 } else if (elt->kind == Starred_kind) {
2607 return compiler_error(c,
2608 "two starred expressions in assignment");
2609 }
2610 }
2611 if (!seen_star) {
2612 ADDOP_I(c, UNPACK_SEQUENCE, n);
2613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
2615 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2616 if (e->v.Tuple.ctx == Load) {
2617 ADDOP_I(c, BUILD_TUPLE, n);
2618 }
2619 return 1;
2620}
2621
2622static int
2623compiler_compare(struct compiler *c, expr_ty e)
2624{
2625 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002626 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627
2628 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2629 VISIT(c, expr, e->v.Compare.left);
2630 n = asdl_seq_LEN(e->v.Compare.ops);
2631 assert(n > 0);
2632 if (n > 1) {
2633 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002634 if (cleanup == NULL)
2635 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002636 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002637 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 }
2639 for (i = 1; i < n; i++) {
2640 ADDOP(c, DUP_TOP);
2641 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002643 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002644 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2646 NEXT_BLOCK(c);
2647 ADDOP(c, POP_TOP);
2648 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002649 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002650 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002652 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002654 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655 if (n > 1) {
2656 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002657 if (end == NULL)
2658 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 ADDOP_JREL(c, JUMP_FORWARD, end);
2660 compiler_use_next_block(c, cleanup);
2661 ADDOP(c, ROT_TWO);
2662 ADDOP(c, POP_TOP);
2663 compiler_use_next_block(c, end);
2664 }
2665 return 1;
2666}
2667
2668static int
2669compiler_call(struct compiler *c, expr_ty e)
2670{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002672 return compiler_call_helper(c, 0,
2673 e->v.Call.args,
2674 e->v.Call.keywords,
2675 e->v.Call.starargs,
2676 e->v.Call.kwargs);
2677}
2678
2679/* shared code between compiler_call and compiler_class */
2680static int
2681compiler_call_helper(struct compiler *c,
2682 int n, /* Args already pushed */
2683 asdl_seq *args,
2684 asdl_seq *keywords,
2685 expr_ty starargs,
2686 expr_ty kwargs)
2687{
2688 int code = 0;
2689
2690 n += asdl_seq_LEN(args);
2691 VISIT_SEQ(c, expr, args);
2692 if (keywords) {
2693 VISIT_SEQ(c, keyword, keywords);
2694 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002696 if (starargs) {
2697 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 code |= 1;
2699 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002700 if (kwargs) {
2701 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 code |= 2;
2703 }
2704 switch (code) {
2705 case 0:
2706 ADDOP_I(c, CALL_FUNCTION, n);
2707 break;
2708 case 1:
2709 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2710 break;
2711 case 2:
2712 ADDOP_I(c, CALL_FUNCTION_KW, n);
2713 break;
2714 case 3:
2715 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2716 break;
2717 }
2718 return 1;
2719}
2720
Nick Coghlan650f0d02007-04-15 12:05:43 +00002721
2722/* List and set comprehensions and generator expressions work by creating a
2723 nested function to perform the actual iteration. This means that the
2724 iteration variables don't leak into the current scope.
2725 The defined function is called immediately following its definition, with the
2726 result of that call being the result of the expression.
2727 The LC/SC version returns the populated container, while the GE version is
2728 flagged in symtable.c as a generator, so it returns the generator object
2729 when the function is called.
2730 This code *knows* that the loop cannot contain break, continue, or return,
2731 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2732
2733 Possible cleanups:
2734 - iterate over the generator sequence instead of using recursion
2735*/
2736
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002738compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2739 asdl_seq *generators, int gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002740 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741{
2742 /* generate code for the iterator, then each of the ifs,
2743 and then write to the element */
2744
Nick Coghlan650f0d02007-04-15 12:05:43 +00002745 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002747 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
2749 start = compiler_new_block(c);
2750 skip = compiler_new_block(c);
2751 if_cleanup = compiler_new_block(c);
2752 anchor = compiler_new_block(c);
2753
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002754 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002755 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757
Nick Coghlan650f0d02007-04-15 12:05:43 +00002758 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 if (gen_index == 0) {
2761 /* Receive outermost iter as an implicit argument */
2762 c->u->u_argcount = 1;
2763 ADDOP_I(c, LOAD_FAST, 0);
2764 }
2765 else {
2766 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002767 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 ADDOP(c, GET_ITER);
2769 }
2770 compiler_use_next_block(c, start);
2771 ADDOP_JREL(c, FOR_ITER, anchor);
2772 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002773 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002775 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002776 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002778 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 VISIT(c, expr, e);
2780 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2781 NEXT_BLOCK(c);
2782 ADDOP(c, POP_TOP);
2783 }
2784
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002785 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002786 if (!compiler_comprehension_generator(c, tmpname,
2787 generators, gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002788 elt, val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790
Nick Coghlan650f0d02007-04-15 12:05:43 +00002791 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002792 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002793 /* comprehension specific code */
2794 switch (type) {
2795 case COMP_GENEXP:
2796 VISIT(c, expr, elt);
2797 ADDOP(c, YIELD_VALUE);
2798 ADDOP(c, POP_TOP);
2799 break;
2800 case COMP_LISTCOMP:
2801 if (!compiler_nameop(c, tmpname, Load))
2802 return 0;
2803 VISIT(c, expr, elt);
2804 ADDOP(c, LIST_APPEND);
2805 break;
2806 case COMP_SETCOMP:
2807 if (!compiler_nameop(c, tmpname, Load))
2808 return 0;
2809 VISIT(c, expr, elt);
2810 ADDOP(c, SET_ADD);
2811 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002812 case COMP_DICTCOMP:
2813 if (!compiler_nameop(c, tmpname, Load))
2814 return 0;
2815 /* With 'd[k] = v', v is evaluated before k, so we do
2816 the same. STORE_SUBSCR requires (item, map, key),
2817 so we still end up ROTing once. */
2818 VISIT(c, expr, val);
2819 ADDOP(c, ROT_TWO);
2820 VISIT(c, expr, elt);
2821 ADDOP(c, STORE_SUBSCR);
2822 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002823 default:
2824 return 0;
2825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826
2827 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 for (i = 0; i < n; i++) {
2830 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002831 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 ADDOP(c, POP_TOP);
2835 }
2836 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2837 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838
2839 return 1;
2840}
2841
2842static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002843compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002844 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002845{
2846 PyCodeObject *co = NULL;
2847 identifier tmp = NULL;
2848 expr_ty outermost_iter;
2849
2850 outermost_iter = ((comprehension_ty)
2851 asdl_seq_GET(generators, 0))->iter;
2852
2853 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2854 goto error;
2855
2856 if (type != COMP_GENEXP) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002857 int op;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002858 tmp = compiler_new_tmpname(c);
2859 if (!tmp)
2860 goto error_in_scope;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002861 switch (type) {
2862 case COMP_LISTCOMP:
2863 op = BUILD_LIST;
2864 break;
2865 case COMP_SETCOMP:
2866 op = BUILD_SET;
2867 break;
2868 case COMP_DICTCOMP:
2869 op = BUILD_MAP;
2870 break;
2871 default:
2872 PyErr_Format(PyExc_SystemError,
2873 "unknown comprehension type %d", type);
2874 goto error_in_scope;
2875 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002876
Guido van Rossum992d4a32007-07-11 13:09:30 +00002877 ADDOP_I(c, op, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002878 ADDOP(c, DUP_TOP);
2879 if (!compiler_nameop(c, tmp, Store))
2880 goto error_in_scope;
2881 }
2882
Guido van Rossum992d4a32007-07-11 13:09:30 +00002883 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt,
2884 val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002885 goto error_in_scope;
2886
2887 if (type != COMP_GENEXP) {
2888 ADDOP(c, RETURN_VALUE);
2889 }
2890
2891 co = assemble(c, 1);
2892 compiler_exit_scope(c);
2893 if (co == NULL)
2894 goto error;
2895
2896 if (!compiler_make_closure(c, co, 0))
2897 goto error;
2898 Py_DECREF(co);
2899 Py_XDECREF(tmp);
2900
2901 VISIT(c, expr, outermost_iter);
2902 ADDOP(c, GET_ITER);
2903 ADDOP_I(c, CALL_FUNCTION, 1);
2904 return 1;
2905error_in_scope:
2906 compiler_exit_scope(c);
2907error:
2908 Py_XDECREF(co);
2909 Py_XDECREF(tmp);
2910 return 0;
2911}
2912
2913static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914compiler_genexp(struct compiler *c, expr_ty e)
2915{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002916 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002917 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002918 name = PyUnicode_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002919 if (!name)
2920 return 0;
2921 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002922 assert(e->kind == GeneratorExp_kind);
2923 return compiler_comprehension(c, e, COMP_GENEXP, name,
2924 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002925 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926}
2927
2928static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002929compiler_listcomp(struct compiler *c, expr_ty e)
2930{
2931 static identifier name;
2932 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002933 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002934 if (!name)
2935 return 0;
2936 }
2937 assert(e->kind == ListComp_kind);
2938 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2939 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002940 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002941}
2942
2943static int
2944compiler_setcomp(struct compiler *c, expr_ty e)
2945{
2946 static identifier name;
2947 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002948 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002949 if (!name)
2950 return 0;
2951 }
2952 assert(e->kind == SetComp_kind);
2953 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2954 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002955 e->v.SetComp.elt, NULL);
2956}
2957
2958
2959static int
2960compiler_dictcomp(struct compiler *c, expr_ty e)
2961{
2962 static identifier name;
2963 if (!name) {
Neal Norwitz41103bf2007-08-24 23:12:06 +00002964 name = PyUnicode_FromString("<dictcomp>");
Guido van Rossum992d4a32007-07-11 13:09:30 +00002965 if (!name)
2966 return 0;
2967 }
2968 assert(e->kind == DictComp_kind);
2969 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2970 e->v.DictComp.generators,
2971 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002972}
2973
2974
2975static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976compiler_visit_keyword(struct compiler *c, keyword_ty k)
2977{
2978 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2979 VISIT(c, expr, k->value);
2980 return 1;
2981}
2982
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002983/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 whether they are true or false.
2985
2986 Return values: 1 for true, 0 for false, -1 for non-constant.
2987 */
2988
2989static int
2990expr_constant(expr_ty e)
2991{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002992 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002994 case Ellipsis_kind:
2995 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 case Num_kind:
2997 return PyObject_IsTrue(e->v.Num.n);
2998 case Str_kind:
2999 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003000 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003001 /* optimize away names that can't be reassigned */
Guido van Rossumbdbb3952007-06-14 00:28:01 +00003002 id = PyString_AS_STRING(
3003 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003004 if (strcmp(id, "True") == 0) return 1;
3005 if (strcmp(id, "False") == 0) return 0;
3006 if (strcmp(id, "None") == 0) return 0;
3007 if (strcmp(id, "__debug__") == 0)
3008 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003009 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 default:
3011 return -1;
3012 }
3013}
3014
Guido van Rossumc2e20742006-02-27 22:32:47 +00003015/*
3016 Implements the with statement from PEP 343.
3017
3018 The semantics outlined in that PEP are as follows:
3019
3020 with EXPR as VAR:
3021 BLOCK
3022
3023 It is implemented roughly as:
3024
Thomas Wouters477c8d52006-05-27 19:21:47 +00003025 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003026 exit = context.__exit__ # not calling it
3027 value = context.__enter__()
3028 try:
3029 VAR = value # if VAR present in the syntax
3030 BLOCK
3031 finally:
3032 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003033 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003034 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003035 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003036 exit(*exc)
3037 */
3038static int
3039compiler_with(struct compiler *c, stmt_ty s)
3040{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003041 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003042 basicblock *block, *finally;
3043 identifier tmpexit, tmpvalue = NULL;
3044
3045 assert(s->kind == With_kind);
3046
Guido van Rossumc2e20742006-02-27 22:32:47 +00003047 if (!enter_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003048 enter_attr = PyUnicode_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003049 if (!enter_attr)
3050 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003051 }
3052 if (!exit_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003053 exit_attr = PyUnicode_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003054 if (!exit_attr)
3055 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003056 }
3057
3058 block = compiler_new_block(c);
3059 finally = compiler_new_block(c);
3060 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003061 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003062
3063 /* Create a temporary variable to hold context.__exit__ */
3064 tmpexit = compiler_new_tmpname(c);
3065 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003066 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003067 PyArena_AddPyObject(c->c_arena, tmpexit);
3068
3069 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003070 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003071 We need to do this rather than preserving it on the stack
3072 because SETUP_FINALLY remembers the stack level.
3073 We need to do the assignment *inside* the try/finally
3074 so that context.__exit__() is called when the assignment
3075 fails. But we need to call context.__enter__() *before*
3076 the try/finally so that if it fails we won't call
3077 context.__exit__().
3078 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003079 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003080 if (tmpvalue == NULL)
3081 return 0;
3082 PyArena_AddPyObject(c->c_arena, tmpvalue);
3083 }
3084
Thomas Wouters477c8d52006-05-27 19:21:47 +00003085 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003086 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003087
3088 /* Squirrel away context.__exit__ */
3089 ADDOP(c, DUP_TOP);
3090 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3091 if (!compiler_nameop(c, tmpexit, Store))
3092 return 0;
3093
3094 /* Call context.__enter__() */
3095 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3096 ADDOP_I(c, CALL_FUNCTION, 0);
3097
3098 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003099 /* Store it in tmpvalue */
3100 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003101 return 0;
3102 }
3103 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003104 /* Discard result from context.__enter__() */
3105 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003106 }
3107
3108 /* Start the try block */
3109 ADDOP_JREL(c, SETUP_FINALLY, finally);
3110
3111 compiler_use_next_block(c, block);
3112 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003113 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003114 }
3115
3116 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003117 /* Bind saved result of context.__enter__() to VAR */
3118 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003119 !compiler_nameop(c, tmpvalue, Del))
3120 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003121 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003122 }
3123
3124 /* BLOCK code */
3125 VISIT_SEQ(c, stmt, s->v.With.body);
3126
3127 /* End of try block; start the finally block */
3128 ADDOP(c, POP_BLOCK);
3129 compiler_pop_fblock(c, FINALLY_TRY, block);
3130
3131 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3132 compiler_use_next_block(c, finally);
3133 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003134 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003135
3136 /* Finally block starts; push tmpexit and issue our magic opcode. */
3137 if (!compiler_nameop(c, tmpexit, Load) ||
3138 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003139 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003140 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003141
3142 /* Finally block ends. */
3143 ADDOP(c, END_FINALLY);
3144 compiler_pop_fblock(c, FINALLY_END, finally);
3145 return 1;
3146}
3147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148static int
3149compiler_visit_expr(struct compiler *c, expr_ty e)
3150{
3151 int i, n;
3152
Thomas Wouters89f507f2006-12-13 04:49:30 +00003153 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003154 set a new line number for the next instruction.
3155 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 if (e->lineno > c->u->u_lineno) {
3157 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003158 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 }
3160 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003161 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003163 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 VISIT(c, expr, e->v.BinOp.left);
3165 VISIT(c, expr, e->v.BinOp.right);
3166 ADDOP(c, binop(c, e->v.BinOp.op));
3167 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003168 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 VISIT(c, expr, e->v.UnaryOp.operand);
3170 ADDOP(c, unaryop(e->v.UnaryOp.op));
3171 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003172 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003174 case IfExp_kind:
3175 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003176 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 n = asdl_seq_LEN(e->v.Dict.values);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003178 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003180 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003181 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003182 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003183 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Christian Heimes99170a52007-12-19 02:07:34 +00003184 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 }
3186 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003187 case Set_kind:
3188 n = asdl_seq_LEN(e->v.Set.elts);
3189 VISIT_SEQ(c, expr, e->v.Set.elts);
3190 ADDOP_I(c, BUILD_SET, n);
3191 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003192 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003194 case ListComp_kind:
3195 return compiler_listcomp(c, e);
3196 case SetComp_kind:
3197 return compiler_setcomp(c, e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003198 case DictComp_kind:
3199 return compiler_dictcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 case Yield_kind:
3201 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003202 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 if (e->v.Yield.value) {
3204 VISIT(c, expr, e->v.Yield.value);
3205 }
3206 else {
3207 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3208 }
3209 ADDOP(c, YIELD_VALUE);
3210 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003211 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003213 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003215 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3217 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003218 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003219 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3220 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003221 case Bytes_kind:
3222 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
Thomas Wouters00e41de2007-02-23 19:56:57 +00003223 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003224 case Ellipsis_kind:
3225 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3226 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003228 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003229 if (e->v.Attribute.ctx != AugStore)
3230 VISIT(c, expr, e->v.Attribute.value);
3231 switch (e->v.Attribute.ctx) {
3232 case AugLoad:
3233 ADDOP(c, DUP_TOP);
3234 /* Fall through to load */
3235 case Load:
3236 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3237 break;
3238 case AugStore:
3239 ADDOP(c, ROT_TWO);
3240 /* Fall through to save */
3241 case Store:
3242 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3243 break;
3244 case Del:
3245 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3246 break;
3247 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003248 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003249 PyErr_SetString(PyExc_SystemError,
3250 "param invalid in attribute expression");
3251 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 }
3253 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003254 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255 switch (e->v.Subscript.ctx) {
3256 case AugLoad:
3257 VISIT(c, expr, e->v.Subscript.value);
3258 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3259 break;
3260 case Load:
3261 VISIT(c, expr, e->v.Subscript.value);
3262 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3263 break;
3264 case AugStore:
3265 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3266 break;
3267 case Store:
3268 VISIT(c, expr, e->v.Subscript.value);
3269 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3270 break;
3271 case Del:
3272 VISIT(c, expr, e->v.Subscript.value);
3273 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3274 break;
3275 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003276 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003277 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003278 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003279 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 }
3281 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003282 case Starred_kind:
3283 switch (e->v.Starred.ctx) {
3284 case Store:
3285 /* In all legitimate cases, the Starred node was already replaced
3286 * by compiler_list/compiler_tuple. XXX: is that okay? */
3287 return compiler_error(c,
3288 "starred assignment target must be in a list or tuple");
3289 default:
3290 return compiler_error(c,
3291 "can use starred expression only as assignment target");
3292 }
3293 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003294 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3296 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003297 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003299 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 return compiler_tuple(c, e);
3301 }
3302 return 1;
3303}
3304
3305static int
3306compiler_augassign(struct compiler *c, stmt_ty s)
3307{
3308 expr_ty e = s->v.AugAssign.target;
3309 expr_ty auge;
3310
3311 assert(s->kind == AugAssign_kind);
3312
3313 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003314 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003316 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003317 if (auge == NULL)
3318 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319 VISIT(c, expr, auge);
3320 VISIT(c, expr, s->v.AugAssign.value);
3321 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3322 auge->v.Attribute.ctx = AugStore;
3323 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 break;
3325 case Subscript_kind:
3326 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003327 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003328 if (auge == NULL)
3329 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 VISIT(c, expr, auge);
3331 VISIT(c, expr, s->v.AugAssign.value);
3332 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003333 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003335 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003337 if (!compiler_nameop(c, e->v.Name.id, Load))
3338 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339 VISIT(c, expr, s->v.AugAssign.value);
3340 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3341 return compiler_nameop(c, e->v.Name.id, Store);
3342 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003343 PyErr_Format(PyExc_SystemError,
3344 "invalid node type (%d) for augmented assignment",
3345 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003346 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347 }
3348 return 1;
3349}
3350
3351static int
3352compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3353{
3354 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3356 PyErr_SetString(PyExc_SystemError,
3357 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360 f = &c->u->u_fblock[c->u->u_nfblocks++];
3361 f->fb_type = t;
3362 f->fb_block = b;
3363 return 1;
3364}
3365
3366static void
3367compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3368{
3369 struct compiler_unit *u = c->u;
3370 assert(u->u_nfblocks > 0);
3371 u->u_nfblocks--;
3372 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3373 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3374}
3375
Thomas Wouters89f507f2006-12-13 04:49:30 +00003376static int
3377compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003378 int i;
3379 struct compiler_unit *u = c->u;
3380 for (i = 0; i < u->u_nfblocks; ++i) {
3381 if (u->u_fblock[i].fb_type == LOOP)
3382 return 1;
3383 }
3384 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003385}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386/* Raises a SyntaxError and returns 0.
3387 If something goes wrong, a different exception may be raised.
3388*/
3389
3390static int
3391compiler_error(struct compiler *c, const char *errstr)
3392{
3393 PyObject *loc;
3394 PyObject *u = NULL, *v = NULL;
3395
3396 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3397 if (!loc) {
3398 Py_INCREF(Py_None);
3399 loc = Py_None;
3400 }
3401 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3402 Py_None, loc);
3403 if (!u)
3404 goto exit;
3405 v = Py_BuildValue("(zO)", errstr, u);
3406 if (!v)
3407 goto exit;
3408 PyErr_SetObject(PyExc_SyntaxError, v);
3409 exit:
3410 Py_DECREF(loc);
3411 Py_XDECREF(u);
3412 Py_XDECREF(v);
3413 return 0;
3414}
3415
3416static int
3417compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003418 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003420 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003422 /* XXX this code is duplicated */
3423 switch (ctx) {
3424 case AugLoad: /* fall through to Load */
3425 case Load: op = BINARY_SUBSCR; break;
3426 case AugStore:/* fall through to Store */
3427 case Store: op = STORE_SUBSCR; break;
3428 case Del: op = DELETE_SUBSCR; break;
3429 case Param:
3430 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003431 "invalid %s kind %d in subscript\n",
3432 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003433 return 0;
3434 }
3435 if (ctx == AugLoad) {
3436 ADDOP_I(c, DUP_TOPX, 2);
3437 }
3438 else if (ctx == AugStore) {
3439 ADDOP(c, ROT_THREE);
3440 }
3441 ADDOP(c, op);
3442 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443}
3444
3445static int
3446compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3447{
3448 int n = 2;
3449 assert(s->kind == Slice_kind);
3450
3451 /* only handles the cases where BUILD_SLICE is emitted */
3452 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003453 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454 }
3455 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003456 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003460 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 }
3462 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003463 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 }
3465
3466 if (s->v.Slice.step) {
3467 n++;
3468 VISIT(c, expr, s->v.Slice.step);
3469 }
3470 ADDOP_I(c, BUILD_SLICE, n);
3471 return 1;
3472}
3473
3474static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3476 expr_context_ty ctx)
3477{
3478 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 case Slice_kind:
3480 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 case Index_kind:
3482 VISIT(c, expr, s->v.Index.value);
3483 break;
3484 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003485 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003486 PyErr_SetString(PyExc_SystemError,
3487 "extended slice invalid in nested slice");
3488 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 }
3490 return 1;
3491}
3492
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493static int
3494compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3495{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003496 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003498 case Index_kind:
3499 kindname = "index";
3500 if (ctx != AugStore) {
3501 VISIT(c, expr, s->v.Index.value);
3502 }
3503 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003505 kindname = "slice";
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003506 if (ctx != AugStore) {
3507 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 return 0;
3509 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003510 break;
3511 case ExtSlice_kind:
3512 kindname = "extended slice";
3513 if (ctx != AugStore) {
3514 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3515 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003516 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003517 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003518 if (!compiler_visit_nested_slice(c, sub, ctx))
3519 return 0;
3520 }
3521 ADDOP_I(c, BUILD_TUPLE, n);
3522 }
3523 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003524 default:
3525 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003526 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003527 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003529 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530}
3531
Thomas Wouters89f507f2006-12-13 04:49:30 +00003532/* End of the compiler section, beginning of the assembler section */
3533
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534/* do depth-first search of basic block graph, starting with block.
3535 post records the block indices in post-order.
3536
3537 XXX must handle implicit jumps from one block to next
3538*/
3539
Thomas Wouters89f507f2006-12-13 04:49:30 +00003540struct assembler {
3541 PyObject *a_bytecode; /* string containing bytecode */
3542 int a_offset; /* offset into bytecode */
3543 int a_nblocks; /* number of reachable blocks */
3544 basicblock **a_postorder; /* list of blocks in dfs postorder */
3545 PyObject *a_lnotab; /* string containing lnotab */
3546 int a_lnotab_off; /* offset into lnotab */
3547 int a_lineno; /* last lineno of emitted instruction */
3548 int a_lineno_off; /* bytecode offset of last lineno */
3549};
3550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551static void
3552dfs(struct compiler *c, basicblock *b, struct assembler *a)
3553{
3554 int i;
3555 struct instr *instr = NULL;
3556
3557 if (b->b_seen)
3558 return;
3559 b->b_seen = 1;
3560 if (b->b_next != NULL)
3561 dfs(c, b->b_next, a);
3562 for (i = 0; i < b->b_iused; i++) {
3563 instr = &b->b_instr[i];
3564 if (instr->i_jrel || instr->i_jabs)
3565 dfs(c, instr->i_target, a);
3566 }
3567 a->a_postorder[a->a_nblocks++] = b;
3568}
3569
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003570static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3572{
3573 int i;
3574 struct instr *instr;
3575 if (b->b_seen || b->b_startdepth >= depth)
3576 return maxdepth;
3577 b->b_seen = 1;
3578 b->b_startdepth = depth;
3579 for (i = 0; i < b->b_iused; i++) {
3580 instr = &b->b_instr[i];
3581 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3582 if (depth > maxdepth)
3583 maxdepth = depth;
3584 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3585 if (instr->i_jrel || instr->i_jabs) {
3586 maxdepth = stackdepth_walk(c, instr->i_target,
3587 depth, maxdepth);
3588 if (instr->i_opcode == JUMP_ABSOLUTE ||
3589 instr->i_opcode == JUMP_FORWARD) {
3590 goto out; /* remaining code is dead */
3591 }
3592 }
3593 }
3594 if (b->b_next)
3595 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3596out:
3597 b->b_seen = 0;
3598 return maxdepth;
3599}
3600
3601/* Find the flow path that needs the largest stack. We assume that
3602 * cycles in the flow graph have no net effect on the stack depth.
3603 */
3604static int
3605stackdepth(struct compiler *c)
3606{
3607 basicblock *b, *entryblock;
3608 entryblock = NULL;
3609 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3610 b->b_seen = 0;
3611 b->b_startdepth = INT_MIN;
3612 entryblock = b;
3613 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003614 if (!entryblock)
3615 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 return stackdepth_walk(c, entryblock, 0, 0);
3617}
3618
3619static int
3620assemble_init(struct assembler *a, int nblocks, int firstlineno)
3621{
3622 memset(a, 0, sizeof(struct assembler));
3623 a->a_lineno = firstlineno;
3624 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3625 if (!a->a_bytecode)
3626 return 0;
3627 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3628 if (!a->a_lnotab)
3629 return 0;
3630 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003631 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003632 if (!a->a_postorder) {
3633 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 return 1;
3637}
3638
3639static void
3640assemble_free(struct assembler *a)
3641{
3642 Py_XDECREF(a->a_bytecode);
3643 Py_XDECREF(a->a_lnotab);
3644 if (a->a_postorder)
3645 PyObject_Free(a->a_postorder);
3646}
3647
3648/* Return the size of a basic block in bytes. */
3649
3650static int
3651instrsize(struct instr *instr)
3652{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003653 if (!instr->i_hasarg)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003654 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003655 if (instr->i_oparg > 0xffff)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003656 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3657 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658}
3659
3660static int
3661blocksize(basicblock *b)
3662{
3663 int i;
3664 int size = 0;
3665
3666 for (i = 0; i < b->b_iused; i++)
3667 size += instrsize(&b->b_instr[i]);
3668 return size;
3669}
3670
3671/* All about a_lnotab.
3672
3673c_lnotab is an array of unsigned bytes disguised as a Python string.
3674It is used to map bytecode offsets to source code line #s (when needed
3675for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003676
Tim Peters2a7f3842001-06-09 09:26:21 +00003677The array is conceptually a list of
3678 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003679pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003680
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003681 byte code offset source code line number
3682 0 1
3683 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003684 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003685 350 307
3686 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003687
3688The first trick is that these numbers aren't stored, only the increments
3689from one row to the next (this doesn't really work, but it's a start):
3690
3691 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3692
3693The second trick is that an unsigned byte can't hold negative values, or
3694values larger than 255, so (a) there's a deep assumption that byte code
3695offsets and their corresponding line #s both increase monotonically, and (b)
3696if at least one column jumps by more than 255 from one row to the next, more
3697than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003698from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003699part. A user of c_lnotab desiring to find the source line number
3700corresponding to a bytecode address A should do something like this
3701
3702 lineno = addr = 0
3703 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003704 addr += addr_incr
3705 if addr > A:
3706 return lineno
3707 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003708
3709In order for this to work, when the addr field increments by more than 255,
3710the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003711increment is < 256. So, in the example above, assemble_lnotab (it used
3712to be called com_set_lineno) should not (as was actually done until 2.2)
3713expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003714 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003715*/
3716
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003717static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003719{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720 int d_bytecode, d_lineno;
3721 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003722 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723
3724 d_bytecode = a->a_offset - a->a_lineno_off;
3725 d_lineno = i->i_lineno - a->a_lineno;
3726
3727 assert(d_bytecode >= 0);
3728 assert(d_lineno >= 0);
3729
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003730 /* XXX(nnorwitz): is there a better way to handle this?
3731 for loops are special, we want to be able to trace them
3732 each time around, so we need to set an extra line number. */
3733 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003734 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003735
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003737 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 nbytes = a->a_lnotab_off + 2 * ncodes;
3739 len = PyString_GET_SIZE(a->a_lnotab);
3740 if (nbytes >= len) {
3741 if (len * 2 < nbytes)
3742 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003743 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 len *= 2;
3745 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3746 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003747 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003748 lnotab = (unsigned char *)
3749 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003750 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 *lnotab++ = 255;
3752 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 d_bytecode -= ncodes * 255;
3755 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003756 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 assert(d_bytecode <= 255);
3758 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003759 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 nbytes = a->a_lnotab_off + 2 * ncodes;
3761 len = PyString_GET_SIZE(a->a_lnotab);
3762 if (nbytes >= len) {
3763 if (len * 2 < nbytes)
3764 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003765 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 len *= 2;
3767 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3768 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003769 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003770 lnotab = (unsigned char *)
3771 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003773 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003775 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003777 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 d_lineno -= ncodes * 255;
3780 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003781 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003782
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 len = PyString_GET_SIZE(a->a_lnotab);
3784 if (a->a_lnotab_off + 2 >= len) {
3785 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003786 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003787 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003788 lnotab = (unsigned char *)
3789 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003790
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 a->a_lnotab_off += 2;
3792 if (d_bytecode) {
3793 *lnotab++ = d_bytecode;
3794 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003795 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003796 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 *lnotab++ = 0;
3798 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 a->a_lineno = i->i_lineno;
3801 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003802 return 1;
3803}
3804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805/* assemble_emit()
3806 Extend the bytecode with a new instruction.
3807 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003808*/
3809
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003810static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003812{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003813 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003814 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 char *code;
3816
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003817 size = instrsize(i);
3818 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003820 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003821 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003823 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 if (a->a_offset + size >= len) {
3825 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003826 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003827 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3829 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003830 if (size == 6) {
3831 assert(i->i_hasarg);
3832 *code++ = (char)EXTENDED_ARG;
3833 *code++ = ext & 0xff;
3834 *code++ = ext >> 8;
3835 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003838 if (i->i_hasarg) {
3839 assert(size == 3 || size == 6);
3840 *code++ = arg & 0xff;
3841 *code++ = arg >> 8;
3842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003844}
3845
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003846static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003848{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003850 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003851 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 /* Compute the size of each block and fixup jump args.
3854 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003855start:
3856 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003858 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 bsize = blocksize(b);
3860 b->b_offset = totsize;
3861 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003862 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003863 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3865 bsize = b->b_offset;
3866 for (i = 0; i < b->b_iused; i++) {
3867 struct instr *instr = &b->b_instr[i];
3868 /* Relative jumps are computed relative to
3869 the instruction pointer after fetching
3870 the jump instruction.
3871 */
3872 bsize += instrsize(instr);
3873 if (instr->i_jabs)
3874 instr->i_oparg = instr->i_target->b_offset;
3875 else if (instr->i_jrel) {
3876 int delta = instr->i_target->b_offset - bsize;
3877 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003878 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003879 else
3880 continue;
3881 if (instr->i_oparg > 0xffff)
3882 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003883 }
3884 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003885
3886 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003887 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003888 with a better solution.
3889
3890 In the meantime, should the goto be dropped in favor
3891 of a loop?
3892
3893 The issue is that in the first loop blocksize() is called
3894 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003895 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003896 i_oparg is calculated in the second loop above.
3897
3898 So we loop until we stop seeing new EXTENDED_ARGs.
3899 The only EXTENDED_ARGs that could be popping up are
3900 ones in jump instructions. So this should converge
3901 fairly quickly.
3902 */
3903 if (last_extended_arg_count != extended_arg_count) {
3904 last_extended_arg_count = extended_arg_count;
3905 goto start;
3906 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003907}
3908
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003909static PyObject *
3910dict_keys_inorder(PyObject *dict, int offset)
3911{
3912 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003913 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003914
3915 tuple = PyTuple_New(size);
3916 if (tuple == NULL)
3917 return NULL;
3918 while (PyDict_Next(dict, &pos, &k, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003919 i = PyLong_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003920 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003921 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003922 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003923 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003924 PyTuple_SET_ITEM(tuple, i - offset, k);
3925 }
3926 return tuple;
3927}
3928
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003929static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003931{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932 PySTEntryObject *ste = c->u->u_ste;
3933 int flags = 0, n;
3934 if (ste->ste_type != ModuleBlock)
3935 flags |= CO_NEWLOCALS;
3936 if (ste->ste_type == FunctionBlock) {
3937 if (!ste->ste_unoptimized)
3938 flags |= CO_OPTIMIZED;
3939 if (ste->ste_nested)
3940 flags |= CO_NESTED;
3941 if (ste->ste_generator)
3942 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003943 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003944 if (ste->ste_varargs)
3945 flags |= CO_VARARGS;
3946 if (ste->ste_varkeywords)
3947 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003948 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003950
3951 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003952 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 n = PyDict_Size(c->u->u_freevars);
3955 if (n < 0)
3956 return -1;
3957 if (n == 0) {
3958 n = PyDict_Size(c->u->u_cellvars);
3959 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003960 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 if (n == 0) {
3962 flags |= CO_NOFREE;
3963 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003964 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003965
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003966 return flags;
3967}
3968
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969static PyCodeObject *
3970makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003971{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 PyObject *tmp;
3973 PyCodeObject *co = NULL;
3974 PyObject *consts = NULL;
3975 PyObject *names = NULL;
3976 PyObject *varnames = NULL;
3977 PyObject *filename = NULL;
3978 PyObject *name = NULL;
3979 PyObject *freevars = NULL;
3980 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003981 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003984 tmp = dict_keys_inorder(c->u->u_consts, 0);
3985 if (!tmp)
3986 goto error;
3987 consts = PySequence_List(tmp); /* optimize_code requires a list */
3988 Py_DECREF(tmp);
3989
3990 names = dict_keys_inorder(c->u->u_names, 0);
3991 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3992 if (!consts || !names || !varnames)
3993 goto error;
3994
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003995 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3996 if (!cellvars)
3997 goto error;
3998 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3999 if (!freevars)
4000 goto error;
Guido van Rossum00bc0e02007-10-15 02:52:41 +00004001 filename = PyUnicode_DecodeFSDefault(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 if (!filename)
4003 goto error;
4004
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004005 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 flags = compute_code_flags(c);
4007 if (flags < 0)
4008 goto error;
4009
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004010 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 if (!bytecode)
4012 goto error;
4013
4014 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4015 if (!tmp)
4016 goto error;
4017 Py_DECREF(consts);
4018 consts = tmp;
4019
Guido van Rossum4f72a782006-10-27 23:31:49 +00004020 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4021 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 bytecode, consts, names, varnames,
4023 freevars, cellvars,
4024 filename, c->u->u_name,
4025 c->u->u_firstlineno,
4026 a->a_lnotab);
4027 error:
4028 Py_XDECREF(consts);
4029 Py_XDECREF(names);
4030 Py_XDECREF(varnames);
4031 Py_XDECREF(filename);
4032 Py_XDECREF(name);
4033 Py_XDECREF(freevars);
4034 Py_XDECREF(cellvars);
4035 Py_XDECREF(bytecode);
4036 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004037}
4038
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004039
4040/* For debugging purposes only */
4041#if 0
4042static void
4043dump_instr(const struct instr *i)
4044{
4045 const char *jrel = i->i_jrel ? "jrel " : "";
4046 const char *jabs = i->i_jabs ? "jabs " : "";
4047 char arg[128];
4048
4049 *arg = '\0';
4050 if (i->i_hasarg)
4051 sprintf(arg, "arg: %d ", i->i_oparg);
4052
4053 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4054 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4055}
4056
4057static void
4058dump_basicblock(const basicblock *b)
4059{
4060 const char *seen = b->b_seen ? "seen " : "";
4061 const char *b_return = b->b_return ? "return " : "";
4062 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4063 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4064 if (b->b_instr) {
4065 int i;
4066 for (i = 0; i < b->b_iused; i++) {
4067 fprintf(stderr, " [%02d] ", i);
4068 dump_instr(b->b_instr + i);
4069 }
4070 }
4071}
4072#endif
4073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074static PyCodeObject *
4075assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004076{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077 basicblock *b, *entryblock;
4078 struct assembler a;
4079 int i, j, nblocks;
4080 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082 /* Make sure every block that falls off the end returns None.
4083 XXX NEXT_BLOCK() isn't quite right, because if the last
4084 block ends with a jump or return b_next shouldn't set.
4085 */
4086 if (!c->u->u_curblock->b_return) {
4087 NEXT_BLOCK(c);
4088 if (addNone)
4089 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4090 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004091 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093 nblocks = 0;
4094 entryblock = NULL;
4095 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4096 nblocks++;
4097 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004098 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004099
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004100 /* Set firstlineno if it wasn't explicitly set. */
4101 if (!c->u->u_firstlineno) {
4102 if (entryblock && entryblock->b_instr)
4103 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4104 else
4105 c->u->u_firstlineno = 1;
4106 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004107 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4108 goto error;
4109 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004112 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004113
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114 /* Emit code in reverse postorder from dfs. */
4115 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004116 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117 for (j = 0; j < b->b_iused; j++)
4118 if (!assemble_emit(&a, &b->b_instr[j]))
4119 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004120 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4123 goto error;
4124 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4125 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004127 co = makecode(c, &a);
4128 error:
4129 assemble_free(&a);
4130 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004131}