blob: 3a211277bf3b56a69370eb5a7ef3744ccf4b2249 [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++) {
338 v = PyInt_FromLong(i);
339 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;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000378 assert(PyInt_Check(v));
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000379 vi = PyInt_AS_LONG(v);
380 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) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000383 PyObject *tuple, *item = PyInt_FromLong(i);
384 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;
717 case DELETE_SUBSCR:
718 return -2;
719
720 case BINARY_LSHIFT:
721 case BINARY_RSHIFT:
722 case BINARY_AND:
723 case BINARY_XOR:
724 case BINARY_OR:
725 return -1;
726 case INPLACE_POWER:
727 return -1;
728 case GET_ITER:
729 return 0;
730
731 case PRINT_EXPR:
732 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000733 case LOAD_BUILD_CLASS:
734 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735 case INPLACE_LSHIFT:
736 case INPLACE_RSHIFT:
737 case INPLACE_AND:
738 case INPLACE_XOR:
739 case INPLACE_OR:
740 return -1;
741 case BREAK_LOOP:
742 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000743 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000744 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000745 case STORE_LOCALS:
746 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747 case RETURN_VALUE:
748 return -1;
749 case IMPORT_STAR:
750 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000751 case YIELD_VALUE:
752 return 0;
753
754 case POP_BLOCK:
755 return 0;
756 case END_FINALLY:
757 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000758
759 case STORE_NAME:
760 return -1;
761 case DELETE_NAME:
762 return 0;
763 case UNPACK_SEQUENCE:
764 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000765 case UNPACK_EX:
766 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 case FOR_ITER:
768 return 1;
769
770 case STORE_ATTR:
771 return -2;
772 case DELETE_ATTR:
773 return -1;
774 case STORE_GLOBAL:
775 return -1;
776 case DELETE_GLOBAL:
777 return 0;
778 case DUP_TOPX:
779 return oparg;
780 case LOAD_CONST:
781 return 1;
782 case LOAD_NAME:
783 return 1;
784 case BUILD_TUPLE:
785 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000786 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787 return 1-oparg;
788 case BUILD_MAP:
789 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000790 case MAKE_BYTES:
791 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792 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);
912 v = PyInt_FromLong(arg);
913 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
925 arg = PyInt_AsLong(v);
926 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 return PyInt_AS_LONG(v);
1214}
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,
1250 PyString_AS_STRING(co->co_name),
1251 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);
1431 if (docstring)
1432 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
1740 if (constant == 0)
1741 return 1;
1742 loop = compiler_new_block(c);
1743 end = compiler_new_block(c);
1744 if (constant == -1) {
1745 anchor = compiler_new_block(c);
1746 if (anchor == NULL)
1747 return 0;
1748 }
1749 if (loop == NULL || end == NULL)
1750 return 0;
1751 if (s->v.While.orelse) {
1752 orelse = compiler_new_block(c);
1753 if (orelse == NULL)
1754 return 0;
1755 }
1756 else
1757 orelse = NULL;
1758
1759 ADDOP_JREL(c, SETUP_LOOP, end);
1760 compiler_use_next_block(c, loop);
1761 if (!compiler_push_fblock(c, LOOP, loop))
1762 return 0;
1763 if (constant == -1) {
1764 VISIT(c, expr, s->v.While.test);
1765 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1766 ADDOP(c, POP_TOP);
1767 }
1768 VISIT_SEQ(c, stmt, s->v.While.body);
1769 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1770
1771 /* XXX should the two POP instructions be in a separate block
1772 if there is no else clause ?
1773 */
1774
1775 if (constant == -1) {
1776 compiler_use_next_block(c, anchor);
1777 ADDOP(c, POP_TOP);
1778 ADDOP(c, POP_BLOCK);
1779 }
1780 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 VISIT_SEQ(c, stmt, s->v.While.orelse);
1783 compiler_use_next_block(c, end);
1784
1785 return 1;
1786}
1787
1788static int
1789compiler_continue(struct compiler *c)
1790{
1791 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001792 static const char IN_FINALLY_ERROR_MSG[] =
1793 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 int i;
1795
1796 if (!c->u->u_nfblocks)
1797 return compiler_error(c, LOOP_ERROR_MSG);
1798 i = c->u->u_nfblocks - 1;
1799 switch (c->u->u_fblock[i].fb_type) {
1800 case LOOP:
1801 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1802 break;
1803 case EXCEPT:
1804 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001805 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1806 /* Prevent continue anywhere under a finally
1807 even if hidden in a sub-try or except. */
1808 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1809 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1810 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (i == -1)
1812 return compiler_error(c, LOOP_ERROR_MSG);
1813 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1814 break;
1815 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001816 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 }
1818
1819 return 1;
1820}
1821
1822/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1823
1824 SETUP_FINALLY L
1825 <code for body>
1826 POP_BLOCK
1827 LOAD_CONST <None>
1828 L: <code for finalbody>
1829 END_FINALLY
1830
1831 The special instructions use the block stack. Each block
1832 stack entry contains the instruction that created it (here
1833 SETUP_FINALLY), the level of the value stack at the time the
1834 block stack entry was created, and a label (here L).
1835
1836 SETUP_FINALLY:
1837 Pushes the current value stack level and the label
1838 onto the block stack.
1839 POP_BLOCK:
1840 Pops en entry from the block stack, and pops the value
1841 stack until its level is the same as indicated on the
1842 block stack. (The label is ignored.)
1843 END_FINALLY:
1844 Pops a variable number of entries from the *value* stack
1845 and re-raises the exception they specify. The number of
1846 entries popped depends on the (pseudo) exception type.
1847
1848 The block stack is unwound when an exception is raised:
1849 when a SETUP_FINALLY entry is found, the exception is pushed
1850 onto the value stack (and the exception condition is cleared),
1851 and the interpreter jumps to the label gotten from the block
1852 stack.
1853*/
1854
1855static int
1856compiler_try_finally(struct compiler *c, stmt_ty s)
1857{
1858 basicblock *body, *end;
1859 body = compiler_new_block(c);
1860 end = compiler_new_block(c);
1861 if (body == NULL || end == NULL)
1862 return 0;
1863
1864 ADDOP_JREL(c, SETUP_FINALLY, end);
1865 compiler_use_next_block(c, body);
1866 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1867 return 0;
1868 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1869 ADDOP(c, POP_BLOCK);
1870 compiler_pop_fblock(c, FINALLY_TRY, body);
1871
1872 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1873 compiler_use_next_block(c, end);
1874 if (!compiler_push_fblock(c, FINALLY_END, end))
1875 return 0;
1876 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1877 ADDOP(c, END_FINALLY);
1878 compiler_pop_fblock(c, FINALLY_END, end);
1879
1880 return 1;
1881}
1882
1883/*
1884 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1885 (The contents of the value stack is shown in [], with the top
1886 at the right; 'tb' is trace-back info, 'val' the exception's
1887 associated value, and 'exc' the exception.)
1888
1889 Value stack Label Instruction Argument
1890 [] SETUP_EXCEPT L1
1891 [] <code for S>
1892 [] POP_BLOCK
1893 [] JUMP_FORWARD L0
1894
1895 [tb, val, exc] L1: DUP )
1896 [tb, val, exc, exc] <evaluate E1> )
1897 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1898 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1899 [tb, val, exc, 1] POP )
1900 [tb, val, exc] POP
1901 [tb, val] <assign to V1> (or POP if no V1)
1902 [tb] POP
1903 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001904 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
1906 [tb, val, exc, 0] L2: POP
1907 [tb, val, exc] DUP
1908 .............................etc.......................
1909
1910 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001911 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
1913 [] L0: <next statement>
1914
1915 Of course, parts are not generated if Vi or Ei is not present.
1916*/
1917static int
1918compiler_try_except(struct compiler *c, stmt_ty s)
1919{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001920 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 int i, n;
1922
1923 body = compiler_new_block(c);
1924 except = compiler_new_block(c);
1925 orelse = compiler_new_block(c);
1926 end = compiler_new_block(c);
1927 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1928 return 0;
1929 ADDOP_JREL(c, SETUP_EXCEPT, except);
1930 compiler_use_next_block(c, body);
1931 if (!compiler_push_fblock(c, EXCEPT, body))
1932 return 0;
1933 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1934 ADDOP(c, POP_BLOCK);
1935 compiler_pop_fblock(c, EXCEPT, body);
1936 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1937 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1938 compiler_use_next_block(c, except);
1939 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 s->v.TryExcept.handlers, i);
1942 if (!handler->type && i < n-1)
1943 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001944 c->u->u_lineno_set = 0;
1945 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 except = compiler_new_block(c);
1947 if (except == NULL)
1948 return 0;
1949 if (handler->type) {
1950 ADDOP(c, DUP_TOP);
1951 VISIT(c, expr, handler->type);
1952 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1953 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1954 ADDOP(c, POP_TOP);
1955 }
1956 ADDOP(c, POP_TOP);
1957 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001958 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001959
1960 cleanup_end = compiler_new_block(c);
1961 cleanup_body = compiler_new_block(c);
1962 if(!(cleanup_end || cleanup_body))
1963 return 0;
1964
Guido van Rossum16be03e2007-01-10 18:51:35 +00001965 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001966 ADDOP(c, POP_TOP);
1967
1968 /*
1969 try:
1970 # body
1971 except type as name:
1972 try:
1973 # body
1974 finally:
1975 name = None
1976 del name
1977 */
1978
1979 /* second try: */
1980 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1981 compiler_use_next_block(c, cleanup_body);
1982 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1983 return 0;
1984
1985 /* second # body */
1986 VISIT_SEQ(c, stmt, handler->body);
1987 ADDOP(c, POP_BLOCK);
1988 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1989
1990 /* finally: */
1991 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1992 compiler_use_next_block(c, cleanup_end);
1993 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1994 return 0;
1995
1996 /* name = None */
1997 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00001998 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001999
Guido van Rossum16be03e2007-01-10 18:51:35 +00002000 /* del name */
2001 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002002
2003 ADDOP(c, END_FINALLY);
2004 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
2006 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002007 ADDOP(c, POP_TOP);
2008 ADDOP(c, POP_TOP);
2009 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 ADDOP_JREL(c, JUMP_FORWARD, end);
2012 compiler_use_next_block(c, except);
2013 if (handler->type)
2014 ADDOP(c, POP_TOP);
2015 }
2016 ADDOP(c, END_FINALLY);
2017 compiler_use_next_block(c, orelse);
2018 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2019 compiler_use_next_block(c, end);
2020 return 1;
2021}
2022
2023static int
2024compiler_import_as(struct compiler *c, identifier name, identifier asname)
2025{
2026 /* The IMPORT_NAME opcode was already generated. This function
2027 merely needs to bind the result to a name.
2028
2029 If there is a dot in name, we need to split it and emit a
2030 LOAD_ATTR for each name.
2031 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002032 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2033 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 if (dot) {
2035 /* Consume the base module name to get the first attribute */
2036 src = dot + 1;
2037 while (dot) {
2038 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002039 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002040 dot = Py_UNICODE_strchr(src, '.');
2041 attr = PyUnicode_FromUnicode(src,
2042 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002043 if (!attr)
2044 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002046 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 src = dot + 1;
2048 }
2049 }
2050 return compiler_nameop(c, asname, Store);
2051}
2052
2053static int
2054compiler_import(struct compiler *c, stmt_ty s)
2055{
2056 /* The Import node stores a module name like a.b.c as a single
2057 string. This is convenient for all cases except
2058 import a.b.c as d
2059 where we need to parse that string to extract the individual
2060 module names.
2061 XXX Perhaps change the representation to make this case simpler?
2062 */
2063 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002066 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002068 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069
Guido van Rossum45aecf42006-03-15 04:58:47 +00002070 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002071 if (level == NULL)
2072 return 0;
2073
2074 ADDOP_O(c, LOAD_CONST, level, consts);
2075 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2077 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2078
2079 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002080 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002081 if (!r)
2082 return r;
2083 }
2084 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002086 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2087 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002089 tmp = PyUnicode_FromUnicode(base,
2090 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 r = compiler_nameop(c, tmp, Store);
2092 if (dot) {
2093 Py_DECREF(tmp);
2094 }
2095 if (!r)
2096 return r;
2097 }
2098 }
2099 return 1;
2100}
2101
2102static int
2103compiler_from_import(struct compiler *c, stmt_ty s)
2104{
2105 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106
2107 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002108 PyObject *level;
2109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 if (!names)
2111 return 0;
2112
Guido van Rossum45aecf42006-03-15 04:58:47 +00002113 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002114 if (!level) {
2115 Py_DECREF(names);
2116 return 0;
2117 }
2118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 /* build up the names */
2120 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002121 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 Py_INCREF(alias->name);
2123 PyTuple_SET_ITEM(names, i, alias->name);
2124 }
2125
2126 if (s->lineno > c->c_future->ff_lineno) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002127 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2128 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002129 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 Py_DECREF(names);
2131 return compiler_error(c,
2132 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002133 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
2135 }
2136 }
2137
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002138 ADDOP_O(c, LOAD_CONST, level, consts);
2139 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002141 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2143 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002144 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 identifier store_name;
2146
Martin v. Löwis5b222132007-06-10 09:51:05 +00002147 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 assert(n == 1);
2149 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002150 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
2152
2153 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2154 store_name = alias->name;
2155 if (alias->asname)
2156 store_name = alias->asname;
2157
2158 if (!compiler_nameop(c, store_name, Store)) {
2159 Py_DECREF(names);
2160 return 0;
2161 }
2162 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002163 /* remove imported module */
2164 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return 1;
2166}
2167
2168static int
2169compiler_assert(struct compiler *c, stmt_ty s)
2170{
2171 static PyObject *assertion_error = NULL;
2172 basicblock *end;
2173
2174 if (Py_OptimizeFlag)
2175 return 1;
2176 if (assertion_error == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002177 assertion_error = PyUnicode_FromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 if (assertion_error == NULL)
2179 return 0;
2180 }
2181 VISIT(c, expr, s->v.Assert.test);
2182 end = compiler_new_block(c);
2183 if (end == NULL)
2184 return 0;
2185 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2186 ADDOP(c, POP_TOP);
2187 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2188 if (s->v.Assert.msg) {
2189 VISIT(c, expr, s->v.Assert.msg);
2190 ADDOP_I(c, RAISE_VARARGS, 2);
2191 }
2192 else {
2193 ADDOP_I(c, RAISE_VARARGS, 1);
2194 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002195 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 ADDOP(c, POP_TOP);
2197 return 1;
2198}
2199
2200static int
2201compiler_visit_stmt(struct compiler *c, stmt_ty s)
2202{
2203 int i, n;
2204
Thomas Wouters89f507f2006-12-13 04:49:30 +00002205 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002207 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002210 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002212 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002214 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 if (c->u->u_ste->ste_type != FunctionBlock)
2216 return compiler_error(c, "'return' outside function");
2217 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 VISIT(c, expr, s->v.Return.value);
2219 }
2220 else
2221 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2222 ADDOP(c, RETURN_VALUE);
2223 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002224 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 VISIT_SEQ(c, expr, s->v.Delete.targets)
2226 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002227 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 n = asdl_seq_LEN(s->v.Assign.targets);
2229 VISIT(c, expr, s->v.Assign.value);
2230 for (i = 0; i < n; i++) {
2231 if (i < n - 1)
2232 ADDOP(c, DUP_TOP);
2233 VISIT(c, expr,
2234 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2235 }
2236 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002237 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002239 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002241 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002243 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002245 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 n = 0;
2247 if (s->v.Raise.type) {
2248 VISIT(c, expr, s->v.Raise.type);
2249 n++;
2250 if (s->v.Raise.inst) {
2251 VISIT(c, expr, s->v.Raise.inst);
2252 n++;
2253 if (s->v.Raise.tback) {
2254 VISIT(c, expr, s->v.Raise.tback);
2255 n++;
2256 }
2257 }
2258 }
2259 ADDOP_I(c, RAISE_VARARGS, n);
2260 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002261 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002263 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002265 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002267 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002269 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002271 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002272 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002274 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002276 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 ADDOP(c, PRINT_EXPR);
2278 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002279 else if (s->v.Expr.value->kind != Str_kind &&
2280 s->v.Expr.value->kind != Num_kind) {
2281 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 ADDOP(c, POP_TOP);
2283 }
2284 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002285 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002287 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002288 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 return compiler_error(c, "'break' outside loop");
2290 ADDOP(c, BREAK_LOOP);
2291 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002292 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002294 case With_kind:
2295 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 }
2297 return 1;
2298}
2299
2300static int
2301unaryop(unaryop_ty op)
2302{
2303 switch (op) {
2304 case Invert:
2305 return UNARY_INVERT;
2306 case Not:
2307 return UNARY_NOT;
2308 case UAdd:
2309 return UNARY_POSITIVE;
2310 case USub:
2311 return UNARY_NEGATIVE;
2312 }
2313 return 0;
2314}
2315
2316static int
2317binop(struct compiler *c, operator_ty op)
2318{
2319 switch (op) {
2320 case Add:
2321 return BINARY_ADD;
2322 case Sub:
2323 return BINARY_SUBTRACT;
2324 case Mult:
2325 return BINARY_MULTIPLY;
2326 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002327 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 case Mod:
2329 return BINARY_MODULO;
2330 case Pow:
2331 return BINARY_POWER;
2332 case LShift:
2333 return BINARY_LSHIFT;
2334 case RShift:
2335 return BINARY_RSHIFT;
2336 case BitOr:
2337 return BINARY_OR;
2338 case BitXor:
2339 return BINARY_XOR;
2340 case BitAnd:
2341 return BINARY_AND;
2342 case FloorDiv:
2343 return BINARY_FLOOR_DIVIDE;
2344 }
2345 return 0;
2346}
2347
2348static int
2349cmpop(cmpop_ty op)
2350{
2351 switch (op) {
2352 case Eq:
2353 return PyCmp_EQ;
2354 case NotEq:
2355 return PyCmp_NE;
2356 case Lt:
2357 return PyCmp_LT;
2358 case LtE:
2359 return PyCmp_LE;
2360 case Gt:
2361 return PyCmp_GT;
2362 case GtE:
2363 return PyCmp_GE;
2364 case Is:
2365 return PyCmp_IS;
2366 case IsNot:
2367 return PyCmp_IS_NOT;
2368 case In:
2369 return PyCmp_IN;
2370 case NotIn:
2371 return PyCmp_NOT_IN;
2372 }
2373 return PyCmp_BAD;
2374}
2375
2376static int
2377inplace_binop(struct compiler *c, operator_ty op)
2378{
2379 switch (op) {
2380 case Add:
2381 return INPLACE_ADD;
2382 case Sub:
2383 return INPLACE_SUBTRACT;
2384 case Mult:
2385 return INPLACE_MULTIPLY;
2386 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002387 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 case Mod:
2389 return INPLACE_MODULO;
2390 case Pow:
2391 return INPLACE_POWER;
2392 case LShift:
2393 return INPLACE_LSHIFT;
2394 case RShift:
2395 return INPLACE_RSHIFT;
2396 case BitOr:
2397 return INPLACE_OR;
2398 case BitXor:
2399 return INPLACE_XOR;
2400 case BitAnd:
2401 return INPLACE_AND;
2402 case FloorDiv:
2403 return INPLACE_FLOOR_DIVIDE;
2404 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002405 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002406 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 return 0;
2408}
2409
2410static int
2411compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2412{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002413 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2415
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002416 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002417 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 /* XXX AugStore isn't used anywhere! */
2419
2420 /* First check for assignment to __debug__. Param? */
2421 if ((ctx == Store || ctx == AugStore || ctx == Del)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002422 && !PyUnicode_CompareWithASCIIString(name, "__debug__")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002423 return compiler_error(c, "can not assign to __debug__");
2424 }
2425
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002426 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002427 if (!mangled)
2428 return 0;
2429
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 op = 0;
2431 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002432 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 switch (scope) {
2434 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002435 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 optype = OP_DEREF;
2437 break;
2438 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002439 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002440 optype = OP_DEREF;
2441 break;
2442 case LOCAL:
2443 if (c->u->u_ste->ste_type == FunctionBlock)
2444 optype = OP_FAST;
2445 break;
2446 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002447 if (c->u->u_ste->ste_type == FunctionBlock &&
2448 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 optype = OP_GLOBAL;
2450 break;
2451 case GLOBAL_EXPLICIT:
2452 optype = OP_GLOBAL;
2453 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002454 default:
2455 /* scope can be 0 */
2456 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 }
2458
2459 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002460 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461
2462 switch (optype) {
2463 case OP_DEREF:
2464 switch (ctx) {
2465 case Load: op = LOAD_DEREF; break;
2466 case Store: op = STORE_DEREF; break;
2467 case AugLoad:
2468 case AugStore:
2469 break;
2470 case Del:
2471 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002472 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002474 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002475 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002478 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002479 PyErr_SetString(PyExc_SystemError,
2480 "param invalid for deref variable");
2481 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 }
2483 break;
2484 case OP_FAST:
2485 switch (ctx) {
2486 case Load: op = LOAD_FAST; break;
2487 case Store: op = STORE_FAST; break;
2488 case Del: op = DELETE_FAST; break;
2489 case AugLoad:
2490 case AugStore:
2491 break;
2492 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002493 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002494 PyErr_SetString(PyExc_SystemError,
2495 "param invalid for local variable");
2496 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002498 ADDOP_O(c, op, mangled, varnames);
2499 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 return 1;
2501 case OP_GLOBAL:
2502 switch (ctx) {
2503 case Load: op = LOAD_GLOBAL; break;
2504 case Store: op = STORE_GLOBAL; break;
2505 case Del: op = DELETE_GLOBAL; break;
2506 case AugLoad:
2507 case AugStore:
2508 break;
2509 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002510 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002511 PyErr_SetString(PyExc_SystemError,
2512 "param invalid for global variable");
2513 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 }
2515 break;
2516 case OP_NAME:
2517 switch (ctx) {
2518 case Load: op = LOAD_NAME; break;
2519 case Store: op = STORE_NAME; break;
2520 case Del: op = DELETE_NAME; break;
2521 case AugLoad:
2522 case AugStore:
2523 break;
2524 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002525 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002526 PyErr_SetString(PyExc_SystemError,
2527 "param invalid for name variable");
2528 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 }
2530 break;
2531 }
2532
2533 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002534 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002535 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002536 if (arg < 0)
2537 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002538 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539}
2540
2541static int
2542compiler_boolop(struct compiler *c, expr_ty e)
2543{
2544 basicblock *end;
2545 int jumpi, i, n;
2546 asdl_seq *s;
2547
2548 assert(e->kind == BoolOp_kind);
2549 if (e->v.BoolOp.op == And)
2550 jumpi = JUMP_IF_FALSE;
2551 else
2552 jumpi = JUMP_IF_TRUE;
2553 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002554 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 return 0;
2556 s = e->v.BoolOp.values;
2557 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002558 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002560 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 ADDOP_JREL(c, jumpi, end);
2562 ADDOP(c, POP_TOP)
2563 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002564 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 compiler_use_next_block(c, end);
2566 return 1;
2567}
2568
2569static int
2570compiler_list(struct compiler *c, expr_ty e)
2571{
2572 int n = asdl_seq_LEN(e->v.List.elts);
2573 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002574 int i, seen_star = 0;
2575 for (i = 0; i < n; i++) {
2576 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2577 if (elt->kind == Starred_kind && !seen_star) {
2578 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2579 seen_star = 1;
2580 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2581 } else if (elt->kind == Starred_kind) {
2582 return compiler_error(c,
2583 "two starred expressions in assignment");
2584 }
2585 }
2586 if (!seen_star) {
2587 ADDOP_I(c, UNPACK_SEQUENCE, n);
2588 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 }
2590 VISIT_SEQ(c, expr, e->v.List.elts);
2591 if (e->v.List.ctx == Load) {
2592 ADDOP_I(c, BUILD_LIST, n);
2593 }
2594 return 1;
2595}
2596
2597static int
2598compiler_tuple(struct compiler *c, expr_ty e)
2599{
2600 int n = asdl_seq_LEN(e->v.Tuple.elts);
2601 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002602 int i, seen_star = 0;
2603 for (i = 0; i < n; i++) {
2604 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2605 if (elt->kind == Starred_kind && !seen_star) {
2606 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2607 seen_star = 1;
2608 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2609 } else if (elt->kind == Starred_kind) {
2610 return compiler_error(c,
2611 "two starred expressions in assignment");
2612 }
2613 }
2614 if (!seen_star) {
2615 ADDOP_I(c, UNPACK_SEQUENCE, n);
2616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 }
2618 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2619 if (e->v.Tuple.ctx == Load) {
2620 ADDOP_I(c, BUILD_TUPLE, n);
2621 }
2622 return 1;
2623}
2624
2625static int
2626compiler_compare(struct compiler *c, expr_ty e)
2627{
2628 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002629 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
2631 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2632 VISIT(c, expr, e->v.Compare.left);
2633 n = asdl_seq_LEN(e->v.Compare.ops);
2634 assert(n > 0);
2635 if (n > 1) {
2636 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002637 if (cleanup == NULL)
2638 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002640 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
2642 for (i = 1; i < n; i++) {
2643 ADDOP(c, DUP_TOP);
2644 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002646 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002647 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2649 NEXT_BLOCK(c);
2650 ADDOP(c, POP_TOP);
2651 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002652 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002653 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002655 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002657 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 if (n > 1) {
2659 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002660 if (end == NULL)
2661 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 ADDOP_JREL(c, JUMP_FORWARD, end);
2663 compiler_use_next_block(c, cleanup);
2664 ADDOP(c, ROT_TWO);
2665 ADDOP(c, POP_TOP);
2666 compiler_use_next_block(c, end);
2667 }
2668 return 1;
2669}
2670
2671static int
2672compiler_call(struct compiler *c, expr_ty e)
2673{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002675 return compiler_call_helper(c, 0,
2676 e->v.Call.args,
2677 e->v.Call.keywords,
2678 e->v.Call.starargs,
2679 e->v.Call.kwargs);
2680}
2681
2682/* shared code between compiler_call and compiler_class */
2683static int
2684compiler_call_helper(struct compiler *c,
2685 int n, /* Args already pushed */
2686 asdl_seq *args,
2687 asdl_seq *keywords,
2688 expr_ty starargs,
2689 expr_ty kwargs)
2690{
2691 int code = 0;
2692
2693 n += asdl_seq_LEN(args);
2694 VISIT_SEQ(c, expr, args);
2695 if (keywords) {
2696 VISIT_SEQ(c, keyword, keywords);
2697 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002699 if (starargs) {
2700 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 code |= 1;
2702 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002703 if (kwargs) {
2704 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 code |= 2;
2706 }
2707 switch (code) {
2708 case 0:
2709 ADDOP_I(c, CALL_FUNCTION, n);
2710 break;
2711 case 1:
2712 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2713 break;
2714 case 2:
2715 ADDOP_I(c, CALL_FUNCTION_KW, n);
2716 break;
2717 case 3:
2718 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2719 break;
2720 }
2721 return 1;
2722}
2723
Nick Coghlan650f0d02007-04-15 12:05:43 +00002724
2725/* List and set comprehensions and generator expressions work by creating a
2726 nested function to perform the actual iteration. This means that the
2727 iteration variables don't leak into the current scope.
2728 The defined function is called immediately following its definition, with the
2729 result of that call being the result of the expression.
2730 The LC/SC version returns the populated container, while the GE version is
2731 flagged in symtable.c as a generator, so it returns the generator object
2732 when the function is called.
2733 This code *knows* that the loop cannot contain break, continue, or return,
2734 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2735
2736 Possible cleanups:
2737 - iterate over the generator sequence instead of using recursion
2738*/
2739
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002740static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002741compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2742 asdl_seq *generators, int gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002743 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744{
2745 /* generate code for the iterator, then each of the ifs,
2746 and then write to the element */
2747
Nick Coghlan650f0d02007-04-15 12:05:43 +00002748 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002750 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751
2752 start = compiler_new_block(c);
2753 skip = compiler_new_block(c);
2754 if_cleanup = compiler_new_block(c);
2755 anchor = compiler_new_block(c);
2756
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002757 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002758 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760
Nick Coghlan650f0d02007-04-15 12:05:43 +00002761 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 if (gen_index == 0) {
2764 /* Receive outermost iter as an implicit argument */
2765 c->u->u_argcount = 1;
2766 ADDOP_I(c, LOAD_FAST, 0);
2767 }
2768 else {
2769 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002770 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 ADDOP(c, GET_ITER);
2772 }
2773 compiler_use_next_block(c, start);
2774 ADDOP_JREL(c, FOR_ITER, anchor);
2775 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002776 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002778 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002779 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002781 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 VISIT(c, expr, e);
2783 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2784 NEXT_BLOCK(c);
2785 ADDOP(c, POP_TOP);
2786 }
2787
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002788 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 if (!compiler_comprehension_generator(c, tmpname,
2790 generators, gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002791 elt, val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793
Nick Coghlan650f0d02007-04-15 12:05:43 +00002794 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002795 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002796 /* comprehension specific code */
2797 switch (type) {
2798 case COMP_GENEXP:
2799 VISIT(c, expr, elt);
2800 ADDOP(c, YIELD_VALUE);
2801 ADDOP(c, POP_TOP);
2802 break;
2803 case COMP_LISTCOMP:
2804 if (!compiler_nameop(c, tmpname, Load))
2805 return 0;
2806 VISIT(c, expr, elt);
2807 ADDOP(c, LIST_APPEND);
2808 break;
2809 case COMP_SETCOMP:
2810 if (!compiler_nameop(c, tmpname, Load))
2811 return 0;
2812 VISIT(c, expr, elt);
2813 ADDOP(c, SET_ADD);
2814 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002815 case COMP_DICTCOMP:
2816 if (!compiler_nameop(c, tmpname, Load))
2817 return 0;
2818 /* With 'd[k] = v', v is evaluated before k, so we do
2819 the same. STORE_SUBSCR requires (item, map, key),
2820 so we still end up ROTing once. */
2821 VISIT(c, expr, val);
2822 ADDOP(c, ROT_TWO);
2823 VISIT(c, expr, elt);
2824 ADDOP(c, STORE_SUBSCR);
2825 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002826 default:
2827 return 0;
2828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829
2830 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 for (i = 0; i < n; i++) {
2833 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002834 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 ADDOP(c, POP_TOP);
2838 }
2839 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2840 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841
2842 return 1;
2843}
2844
2845static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002846compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002847 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002848{
2849 PyCodeObject *co = NULL;
2850 identifier tmp = NULL;
2851 expr_ty outermost_iter;
2852
2853 outermost_iter = ((comprehension_ty)
2854 asdl_seq_GET(generators, 0))->iter;
2855
2856 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2857 goto error;
2858
2859 if (type != COMP_GENEXP) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002860 int op;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002861 tmp = compiler_new_tmpname(c);
2862 if (!tmp)
2863 goto error_in_scope;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002864 switch (type) {
2865 case COMP_LISTCOMP:
2866 op = BUILD_LIST;
2867 break;
2868 case COMP_SETCOMP:
2869 op = BUILD_SET;
2870 break;
2871 case COMP_DICTCOMP:
2872 op = BUILD_MAP;
2873 break;
2874 default:
2875 PyErr_Format(PyExc_SystemError,
2876 "unknown comprehension type %d", type);
2877 goto error_in_scope;
2878 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002879
Guido van Rossum992d4a32007-07-11 13:09:30 +00002880 ADDOP_I(c, op, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002881 ADDOP(c, DUP_TOP);
2882 if (!compiler_nameop(c, tmp, Store))
2883 goto error_in_scope;
2884 }
2885
Guido van Rossum992d4a32007-07-11 13:09:30 +00002886 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt,
2887 val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002888 goto error_in_scope;
2889
2890 if (type != COMP_GENEXP) {
2891 ADDOP(c, RETURN_VALUE);
2892 }
2893
2894 co = assemble(c, 1);
2895 compiler_exit_scope(c);
2896 if (co == NULL)
2897 goto error;
2898
2899 if (!compiler_make_closure(c, co, 0))
2900 goto error;
2901 Py_DECREF(co);
2902 Py_XDECREF(tmp);
2903
2904 VISIT(c, expr, outermost_iter);
2905 ADDOP(c, GET_ITER);
2906 ADDOP_I(c, CALL_FUNCTION, 1);
2907 return 1;
2908error_in_scope:
2909 compiler_exit_scope(c);
2910error:
2911 Py_XDECREF(co);
2912 Py_XDECREF(tmp);
2913 return 0;
2914}
2915
2916static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917compiler_genexp(struct compiler *c, expr_ty e)
2918{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002919 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002920 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002921 name = PyUnicode_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002922 if (!name)
2923 return 0;
2924 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002925 assert(e->kind == GeneratorExp_kind);
2926 return compiler_comprehension(c, e, COMP_GENEXP, name,
2927 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002928 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929}
2930
2931static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002932compiler_listcomp(struct compiler *c, expr_ty e)
2933{
2934 static identifier name;
2935 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002936 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002937 if (!name)
2938 return 0;
2939 }
2940 assert(e->kind == ListComp_kind);
2941 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2942 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002943 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002944}
2945
2946static int
2947compiler_setcomp(struct compiler *c, expr_ty e)
2948{
2949 static identifier name;
2950 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002951 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002952 if (!name)
2953 return 0;
2954 }
2955 assert(e->kind == SetComp_kind);
2956 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2957 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002958 e->v.SetComp.elt, NULL);
2959}
2960
2961
2962static int
2963compiler_dictcomp(struct compiler *c, expr_ty e)
2964{
2965 static identifier name;
2966 if (!name) {
Neal Norwitz41103bf2007-08-24 23:12:06 +00002967 name = PyUnicode_FromString("<dictcomp>");
Guido van Rossum992d4a32007-07-11 13:09:30 +00002968 if (!name)
2969 return 0;
2970 }
2971 assert(e->kind == DictComp_kind);
2972 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2973 e->v.DictComp.generators,
2974 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002975}
2976
2977
2978static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979compiler_visit_keyword(struct compiler *c, keyword_ty k)
2980{
2981 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2982 VISIT(c, expr, k->value);
2983 return 1;
2984}
2985
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002986/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 whether they are true or false.
2988
2989 Return values: 1 for true, 0 for false, -1 for non-constant.
2990 */
2991
2992static int
2993expr_constant(expr_ty e)
2994{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002995 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002997 case Ellipsis_kind:
2998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002999 case Num_kind:
3000 return PyObject_IsTrue(e->v.Num.n);
3001 case Str_kind:
3002 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003003 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003004 /* optimize away names that can't be reassigned */
Guido van Rossumbdbb3952007-06-14 00:28:01 +00003005 id = PyString_AS_STRING(
3006 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003007 if (strcmp(id, "True") == 0) return 1;
3008 if (strcmp(id, "False") == 0) return 0;
3009 if (strcmp(id, "None") == 0) return 0;
3010 if (strcmp(id, "__debug__") == 0)
3011 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003012 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 default:
3014 return -1;
3015 }
3016}
3017
Guido van Rossumc2e20742006-02-27 22:32:47 +00003018/*
3019 Implements the with statement from PEP 343.
3020
3021 The semantics outlined in that PEP are as follows:
3022
3023 with EXPR as VAR:
3024 BLOCK
3025
3026 It is implemented roughly as:
3027
Thomas Wouters477c8d52006-05-27 19:21:47 +00003028 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003029 exit = context.__exit__ # not calling it
3030 value = context.__enter__()
3031 try:
3032 VAR = value # if VAR present in the syntax
3033 BLOCK
3034 finally:
3035 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003036 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003037 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003038 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003039 exit(*exc)
3040 */
3041static int
3042compiler_with(struct compiler *c, stmt_ty s)
3043{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003044 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003045 basicblock *block, *finally;
3046 identifier tmpexit, tmpvalue = NULL;
3047
3048 assert(s->kind == With_kind);
3049
Guido van Rossumc2e20742006-02-27 22:32:47 +00003050 if (!enter_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003051 enter_attr = PyUnicode_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003052 if (!enter_attr)
3053 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003054 }
3055 if (!exit_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003056 exit_attr = PyUnicode_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003057 if (!exit_attr)
3058 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003059 }
3060
3061 block = compiler_new_block(c);
3062 finally = compiler_new_block(c);
3063 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003064 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065
3066 /* Create a temporary variable to hold context.__exit__ */
3067 tmpexit = compiler_new_tmpname(c);
3068 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003069 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003070 PyArena_AddPyObject(c->c_arena, tmpexit);
3071
3072 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003073 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003074 We need to do this rather than preserving it on the stack
3075 because SETUP_FINALLY remembers the stack level.
3076 We need to do the assignment *inside* the try/finally
3077 so that context.__exit__() is called when the assignment
3078 fails. But we need to call context.__enter__() *before*
3079 the try/finally so that if it fails we won't call
3080 context.__exit__().
3081 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003082 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083 if (tmpvalue == NULL)
3084 return 0;
3085 PyArena_AddPyObject(c->c_arena, tmpvalue);
3086 }
3087
Thomas Wouters477c8d52006-05-27 19:21:47 +00003088 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003090
3091 /* Squirrel away context.__exit__ */
3092 ADDOP(c, DUP_TOP);
3093 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3094 if (!compiler_nameop(c, tmpexit, Store))
3095 return 0;
3096
3097 /* Call context.__enter__() */
3098 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3099 ADDOP_I(c, CALL_FUNCTION, 0);
3100
3101 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003102 /* Store it in tmpvalue */
3103 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003104 return 0;
3105 }
3106 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003107 /* Discard result from context.__enter__() */
3108 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003109 }
3110
3111 /* Start the try block */
3112 ADDOP_JREL(c, SETUP_FINALLY, finally);
3113
3114 compiler_use_next_block(c, block);
3115 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003116 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003117 }
3118
3119 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 /* Bind saved result of context.__enter__() to VAR */
3121 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003122 !compiler_nameop(c, tmpvalue, Del))
3123 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003124 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003125 }
3126
3127 /* BLOCK code */
3128 VISIT_SEQ(c, stmt, s->v.With.body);
3129
3130 /* End of try block; start the finally block */
3131 ADDOP(c, POP_BLOCK);
3132 compiler_pop_fblock(c, FINALLY_TRY, block);
3133
3134 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3135 compiler_use_next_block(c, finally);
3136 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003137 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003138
3139 /* Finally block starts; push tmpexit and issue our magic opcode. */
3140 if (!compiler_nameop(c, tmpexit, Load) ||
3141 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003142 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003143 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003144
3145 /* Finally block ends. */
3146 ADDOP(c, END_FINALLY);
3147 compiler_pop_fblock(c, FINALLY_END, finally);
3148 return 1;
3149}
3150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151static int
3152compiler_visit_expr(struct compiler *c, expr_ty e)
3153{
3154 int i, n;
3155
Thomas Wouters89f507f2006-12-13 04:49:30 +00003156 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003157 set a new line number for the next instruction.
3158 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 if (e->lineno > c->u->u_lineno) {
3160 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003161 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 }
3163 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003164 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003166 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 VISIT(c, expr, e->v.BinOp.left);
3168 VISIT(c, expr, e->v.BinOp.right);
3169 ADDOP(c, binop(c, e->v.BinOp.op));
3170 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003171 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 VISIT(c, expr, e->v.UnaryOp.operand);
3173 ADDOP(c, unaryop(e->v.UnaryOp.op));
3174 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003175 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003177 case IfExp_kind:
3178 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003179 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 /* XXX get rid of arg? */
3181 ADDOP_I(c, BUILD_MAP, 0);
3182 n = asdl_seq_LEN(e->v.Dict.values);
3183 /* We must arrange things just right for STORE_SUBSCR.
3184 It wants the stack to look like (value) (dict) (key) */
3185 for (i = 0; i < n; i++) {
3186 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003187 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003188 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003190 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003191 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 ADDOP(c, STORE_SUBSCR);
3193 }
3194 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003195 case Set_kind:
3196 n = asdl_seq_LEN(e->v.Set.elts);
3197 VISIT_SEQ(c, expr, e->v.Set.elts);
3198 ADDOP_I(c, BUILD_SET, n);
3199 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003200 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003202 case ListComp_kind:
3203 return compiler_listcomp(c, e);
3204 case SetComp_kind:
3205 return compiler_setcomp(c, e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003206 case DictComp_kind:
3207 return compiler_dictcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 case Yield_kind:
3209 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003210 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 if (e->v.Yield.value) {
3212 VISIT(c, expr, e->v.Yield.value);
3213 }
3214 else {
3215 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3216 }
3217 ADDOP(c, YIELD_VALUE);
3218 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003219 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003221 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003223 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3225 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003226 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3228 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003229 case Bytes_kind:
3230 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3231 ADDOP(c, MAKE_BYTES);
3232 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003233 case Ellipsis_kind:
3234 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3235 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003237 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 if (e->v.Attribute.ctx != AugStore)
3239 VISIT(c, expr, e->v.Attribute.value);
3240 switch (e->v.Attribute.ctx) {
3241 case AugLoad:
3242 ADDOP(c, DUP_TOP);
3243 /* Fall through to load */
3244 case Load:
3245 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3246 break;
3247 case AugStore:
3248 ADDOP(c, ROT_TWO);
3249 /* Fall through to save */
3250 case Store:
3251 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3252 break;
3253 case Del:
3254 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3255 break;
3256 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003257 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003258 PyErr_SetString(PyExc_SystemError,
3259 "param invalid in attribute expression");
3260 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 }
3262 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003263 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 switch (e->v.Subscript.ctx) {
3265 case AugLoad:
3266 VISIT(c, expr, e->v.Subscript.value);
3267 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3268 break;
3269 case Load:
3270 VISIT(c, expr, e->v.Subscript.value);
3271 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3272 break;
3273 case AugStore:
3274 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3275 break;
3276 case Store:
3277 VISIT(c, expr, e->v.Subscript.value);
3278 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3279 break;
3280 case Del:
3281 VISIT(c, expr, e->v.Subscript.value);
3282 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3283 break;
3284 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003285 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003286 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003287 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003288 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289 }
3290 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003291 case Starred_kind:
3292 switch (e->v.Starred.ctx) {
3293 case Store:
3294 /* In all legitimate cases, the Starred node was already replaced
3295 * by compiler_list/compiler_tuple. XXX: is that okay? */
3296 return compiler_error(c,
3297 "starred assignment target must be in a list or tuple");
3298 default:
3299 return compiler_error(c,
3300 "can use starred expression only as assignment target");
3301 }
3302 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003303 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3305 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003306 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003308 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 return compiler_tuple(c, e);
3310 }
3311 return 1;
3312}
3313
3314static int
3315compiler_augassign(struct compiler *c, stmt_ty s)
3316{
3317 expr_ty e = s->v.AugAssign.target;
3318 expr_ty auge;
3319
3320 assert(s->kind == AugAssign_kind);
3321
3322 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003323 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003325 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003326 if (auge == NULL)
3327 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003328 VISIT(c, expr, auge);
3329 VISIT(c, expr, s->v.AugAssign.value);
3330 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3331 auge->v.Attribute.ctx = AugStore;
3332 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333 break;
3334 case Subscript_kind:
3335 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003336 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003337 if (auge == NULL)
3338 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339 VISIT(c, expr, auge);
3340 VISIT(c, expr, s->v.AugAssign.value);
3341 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003342 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003344 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003346 if (!compiler_nameop(c, e->v.Name.id, Load))
3347 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 VISIT(c, expr, s->v.AugAssign.value);
3349 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3350 return compiler_nameop(c, e->v.Name.id, Store);
3351 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003352 PyErr_Format(PyExc_SystemError,
3353 "invalid node type (%d) for augmented assignment",
3354 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003355 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
3357 return 1;
3358}
3359
3360static int
3361compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3362{
3363 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3365 PyErr_SetString(PyExc_SystemError,
3366 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003368 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369 f = &c->u->u_fblock[c->u->u_nfblocks++];
3370 f->fb_type = t;
3371 f->fb_block = b;
3372 return 1;
3373}
3374
3375static void
3376compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3377{
3378 struct compiler_unit *u = c->u;
3379 assert(u->u_nfblocks > 0);
3380 u->u_nfblocks--;
3381 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3382 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3383}
3384
Thomas Wouters89f507f2006-12-13 04:49:30 +00003385static int
3386compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003387 int i;
3388 struct compiler_unit *u = c->u;
3389 for (i = 0; i < u->u_nfblocks; ++i) {
3390 if (u->u_fblock[i].fb_type == LOOP)
3391 return 1;
3392 }
3393 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003394}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395/* Raises a SyntaxError and returns 0.
3396 If something goes wrong, a different exception may be raised.
3397*/
3398
3399static int
3400compiler_error(struct compiler *c, const char *errstr)
3401{
3402 PyObject *loc;
3403 PyObject *u = NULL, *v = NULL;
3404
3405 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3406 if (!loc) {
3407 Py_INCREF(Py_None);
3408 loc = Py_None;
3409 }
3410 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3411 Py_None, loc);
3412 if (!u)
3413 goto exit;
3414 v = Py_BuildValue("(zO)", errstr, u);
3415 if (!v)
3416 goto exit;
3417 PyErr_SetObject(PyExc_SyntaxError, v);
3418 exit:
3419 Py_DECREF(loc);
3420 Py_XDECREF(u);
3421 Py_XDECREF(v);
3422 return 0;
3423}
3424
3425static int
3426compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003427 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003429 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003431 /* XXX this code is duplicated */
3432 switch (ctx) {
3433 case AugLoad: /* fall through to Load */
3434 case Load: op = BINARY_SUBSCR; break;
3435 case AugStore:/* fall through to Store */
3436 case Store: op = STORE_SUBSCR; break;
3437 case Del: op = DELETE_SUBSCR; break;
3438 case Param:
3439 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003440 "invalid %s kind %d in subscript\n",
3441 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003442 return 0;
3443 }
3444 if (ctx == AugLoad) {
3445 ADDOP_I(c, DUP_TOPX, 2);
3446 }
3447 else if (ctx == AugStore) {
3448 ADDOP(c, ROT_THREE);
3449 }
3450 ADDOP(c, op);
3451 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452}
3453
3454static int
3455compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3456{
3457 int n = 2;
3458 assert(s->kind == Slice_kind);
3459
3460 /* only handles the cases where BUILD_SLICE is emitted */
3461 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003462 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 }
3464 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003465 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003467
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003469 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 }
3471 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003472 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 }
3474
3475 if (s->v.Slice.step) {
3476 n++;
3477 VISIT(c, expr, s->v.Slice.step);
3478 }
3479 ADDOP_I(c, BUILD_SLICE, n);
3480 return 1;
3481}
3482
3483static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3485 expr_context_ty ctx)
3486{
3487 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 case Slice_kind:
3489 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 case Index_kind:
3491 VISIT(c, expr, s->v.Index.value);
3492 break;
3493 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003494 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003495 PyErr_SetString(PyExc_SystemError,
3496 "extended slice invalid in nested slice");
3497 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 }
3499 return 1;
3500}
3501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502static int
3503compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3504{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003505 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003507 case Index_kind:
3508 kindname = "index";
3509 if (ctx != AugStore) {
3510 VISIT(c, expr, s->v.Index.value);
3511 }
3512 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003514 kindname = "slice";
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003515 if (ctx != AugStore) {
3516 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517 return 0;
3518 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003519 break;
3520 case ExtSlice_kind:
3521 kindname = "extended slice";
3522 if (ctx != AugStore) {
3523 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3524 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003525 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003526 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003527 if (!compiler_visit_nested_slice(c, sub, ctx))
3528 return 0;
3529 }
3530 ADDOP_I(c, BUILD_TUPLE, n);
3531 }
3532 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003533 default:
3534 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003535 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003536 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003538 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539}
3540
Thomas Wouters89f507f2006-12-13 04:49:30 +00003541/* End of the compiler section, beginning of the assembler section */
3542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543/* do depth-first search of basic block graph, starting with block.
3544 post records the block indices in post-order.
3545
3546 XXX must handle implicit jumps from one block to next
3547*/
3548
Thomas Wouters89f507f2006-12-13 04:49:30 +00003549struct assembler {
3550 PyObject *a_bytecode; /* string containing bytecode */
3551 int a_offset; /* offset into bytecode */
3552 int a_nblocks; /* number of reachable blocks */
3553 basicblock **a_postorder; /* list of blocks in dfs postorder */
3554 PyObject *a_lnotab; /* string containing lnotab */
3555 int a_lnotab_off; /* offset into lnotab */
3556 int a_lineno; /* last lineno of emitted instruction */
3557 int a_lineno_off; /* bytecode offset of last lineno */
3558};
3559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560static void
3561dfs(struct compiler *c, basicblock *b, struct assembler *a)
3562{
3563 int i;
3564 struct instr *instr = NULL;
3565
3566 if (b->b_seen)
3567 return;
3568 b->b_seen = 1;
3569 if (b->b_next != NULL)
3570 dfs(c, b->b_next, a);
3571 for (i = 0; i < b->b_iused; i++) {
3572 instr = &b->b_instr[i];
3573 if (instr->i_jrel || instr->i_jabs)
3574 dfs(c, instr->i_target, a);
3575 }
3576 a->a_postorder[a->a_nblocks++] = b;
3577}
3578
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003579static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3581{
3582 int i;
3583 struct instr *instr;
3584 if (b->b_seen || b->b_startdepth >= depth)
3585 return maxdepth;
3586 b->b_seen = 1;
3587 b->b_startdepth = depth;
3588 for (i = 0; i < b->b_iused; i++) {
3589 instr = &b->b_instr[i];
3590 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3591 if (depth > maxdepth)
3592 maxdepth = depth;
3593 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3594 if (instr->i_jrel || instr->i_jabs) {
3595 maxdepth = stackdepth_walk(c, instr->i_target,
3596 depth, maxdepth);
3597 if (instr->i_opcode == JUMP_ABSOLUTE ||
3598 instr->i_opcode == JUMP_FORWARD) {
3599 goto out; /* remaining code is dead */
3600 }
3601 }
3602 }
3603 if (b->b_next)
3604 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3605out:
3606 b->b_seen = 0;
3607 return maxdepth;
3608}
3609
3610/* Find the flow path that needs the largest stack. We assume that
3611 * cycles in the flow graph have no net effect on the stack depth.
3612 */
3613static int
3614stackdepth(struct compiler *c)
3615{
3616 basicblock *b, *entryblock;
3617 entryblock = NULL;
3618 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3619 b->b_seen = 0;
3620 b->b_startdepth = INT_MIN;
3621 entryblock = b;
3622 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003623 if (!entryblock)
3624 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return stackdepth_walk(c, entryblock, 0, 0);
3626}
3627
3628static int
3629assemble_init(struct assembler *a, int nblocks, int firstlineno)
3630{
3631 memset(a, 0, sizeof(struct assembler));
3632 a->a_lineno = firstlineno;
3633 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3634 if (!a->a_bytecode)
3635 return 0;
3636 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3637 if (!a->a_lnotab)
3638 return 0;
3639 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003640 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003641 if (!a->a_postorder) {
3642 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 return 1;
3646}
3647
3648static void
3649assemble_free(struct assembler *a)
3650{
3651 Py_XDECREF(a->a_bytecode);
3652 Py_XDECREF(a->a_lnotab);
3653 if (a->a_postorder)
3654 PyObject_Free(a->a_postorder);
3655}
3656
3657/* Return the size of a basic block in bytes. */
3658
3659static int
3660instrsize(struct instr *instr)
3661{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003662 if (!instr->i_hasarg)
3663 return 1;
3664 if (instr->i_oparg > 0xffff)
3665 return 6;
3666 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667}
3668
3669static int
3670blocksize(basicblock *b)
3671{
3672 int i;
3673 int size = 0;
3674
3675 for (i = 0; i < b->b_iused; i++)
3676 size += instrsize(&b->b_instr[i]);
3677 return size;
3678}
3679
3680/* All about a_lnotab.
3681
3682c_lnotab is an array of unsigned bytes disguised as a Python string.
3683It is used to map bytecode offsets to source code line #s (when needed
3684for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003685
Tim Peters2a7f3842001-06-09 09:26:21 +00003686The array is conceptually a list of
3687 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003688pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003689
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003690 byte code offset source code line number
3691 0 1
3692 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003693 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003694 350 307
3695 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003696
3697The first trick is that these numbers aren't stored, only the increments
3698from one row to the next (this doesn't really work, but it's a start):
3699
3700 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3701
3702The second trick is that an unsigned byte can't hold negative values, or
3703values larger than 255, so (a) there's a deep assumption that byte code
3704offsets and their corresponding line #s both increase monotonically, and (b)
3705if at least one column jumps by more than 255 from one row to the next, more
3706than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003707from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003708part. A user of c_lnotab desiring to find the source line number
3709corresponding to a bytecode address A should do something like this
3710
3711 lineno = addr = 0
3712 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003713 addr += addr_incr
3714 if addr > A:
3715 return lineno
3716 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003717
3718In order for this to work, when the addr field increments by more than 255,
3719the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003720increment is < 256. So, in the example above, assemble_lnotab (it used
3721to be called com_set_lineno) should not (as was actually done until 2.2)
3722expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003723 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003724*/
3725
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003726static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003728{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 int d_bytecode, d_lineno;
3730 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003731 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732
3733 d_bytecode = a->a_offset - a->a_lineno_off;
3734 d_lineno = i->i_lineno - a->a_lineno;
3735
3736 assert(d_bytecode >= 0);
3737 assert(d_lineno >= 0);
3738
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003739 /* XXX(nnorwitz): is there a better way to handle this?
3740 for loops are special, we want to be able to trace them
3741 each time around, so we need to set an extra line number. */
3742 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003743 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003746 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 nbytes = a->a_lnotab_off + 2 * ncodes;
3748 len = PyString_GET_SIZE(a->a_lnotab);
3749 if (nbytes >= len) {
3750 if (len * 2 < nbytes)
3751 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003752 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 len *= 2;
3754 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3755 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003756 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003757 lnotab = (unsigned char *)
3758 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003759 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 *lnotab++ = 255;
3761 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 d_bytecode -= ncodes * 255;
3764 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 assert(d_bytecode <= 255);
3767 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003768 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 nbytes = a->a_lnotab_off + 2 * ncodes;
3770 len = PyString_GET_SIZE(a->a_lnotab);
3771 if (nbytes >= len) {
3772 if (len * 2 < nbytes)
3773 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003774 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 len *= 2;
3776 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3777 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003778 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003779 lnotab = (unsigned char *)
3780 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003782 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003784 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003786 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 d_lineno -= ncodes * 255;
3789 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003790 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 len = PyString_GET_SIZE(a->a_lnotab);
3793 if (a->a_lnotab_off + 2 >= len) {
3794 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003795 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003796 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003797 lnotab = (unsigned char *)
3798 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 a->a_lnotab_off += 2;
3801 if (d_bytecode) {
3802 *lnotab++ = d_bytecode;
3803 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003804 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003805 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 *lnotab++ = 0;
3807 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 a->a_lineno = i->i_lineno;
3810 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003811 return 1;
3812}
3813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814/* assemble_emit()
3815 Extend the bytecode with a new instruction.
3816 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003817*/
3818
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003819static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003821{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003822 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003823 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 char *code;
3825
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003826 size = instrsize(i);
3827 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003829 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003832 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833 if (a->a_offset + size >= len) {
3834 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003835 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3838 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003839 if (size == 6) {
3840 assert(i->i_hasarg);
3841 *code++ = (char)EXTENDED_ARG;
3842 *code++ = ext & 0xff;
3843 *code++ = ext >> 8;
3844 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003847 if (i->i_hasarg) {
3848 assert(size == 3 || size == 6);
3849 *code++ = arg & 0xff;
3850 *code++ = arg >> 8;
3851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003853}
3854
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003855static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003857{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003859 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003860 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003861
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 /* Compute the size of each block and fixup jump args.
3863 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003864start:
3865 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003867 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 bsize = blocksize(b);
3869 b->b_offset = totsize;
3870 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003871 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003872 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3874 bsize = b->b_offset;
3875 for (i = 0; i < b->b_iused; i++) {
3876 struct instr *instr = &b->b_instr[i];
3877 /* Relative jumps are computed relative to
3878 the instruction pointer after fetching
3879 the jump instruction.
3880 */
3881 bsize += instrsize(instr);
3882 if (instr->i_jabs)
3883 instr->i_oparg = instr->i_target->b_offset;
3884 else if (instr->i_jrel) {
3885 int delta = instr->i_target->b_offset - bsize;
3886 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003887 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003888 else
3889 continue;
3890 if (instr->i_oparg > 0xffff)
3891 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003892 }
3893 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003894
3895 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003896 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003897 with a better solution.
3898
3899 In the meantime, should the goto be dropped in favor
3900 of a loop?
3901
3902 The issue is that in the first loop blocksize() is called
3903 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003904 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003905 i_oparg is calculated in the second loop above.
3906
3907 So we loop until we stop seeing new EXTENDED_ARGs.
3908 The only EXTENDED_ARGs that could be popping up are
3909 ones in jump instructions. So this should converge
3910 fairly quickly.
3911 */
3912 if (last_extended_arg_count != extended_arg_count) {
3913 last_extended_arg_count = extended_arg_count;
3914 goto start;
3915 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003916}
3917
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003918static PyObject *
3919dict_keys_inorder(PyObject *dict, int offset)
3920{
3921 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003922 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003923
3924 tuple = PyTuple_New(size);
3925 if (tuple == NULL)
3926 return NULL;
3927 while (PyDict_Next(dict, &pos, &k, &v)) {
3928 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003929 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003930 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003931 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003932 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003933 PyTuple_SET_ITEM(tuple, i - offset, k);
3934 }
3935 return tuple;
3936}
3937
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003938static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003940{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 PySTEntryObject *ste = c->u->u_ste;
3942 int flags = 0, n;
3943 if (ste->ste_type != ModuleBlock)
3944 flags |= CO_NEWLOCALS;
3945 if (ste->ste_type == FunctionBlock) {
3946 if (!ste->ste_unoptimized)
3947 flags |= CO_OPTIMIZED;
3948 if (ste->ste_nested)
3949 flags |= CO_NESTED;
3950 if (ste->ste_generator)
3951 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 if (ste->ste_varargs)
3954 flags |= CO_VARARGS;
3955 if (ste->ste_varkeywords)
3956 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003957 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003959
3960 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003961 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 n = PyDict_Size(c->u->u_freevars);
3964 if (n < 0)
3965 return -1;
3966 if (n == 0) {
3967 n = PyDict_Size(c->u->u_cellvars);
3968 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003969 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970 if (n == 0) {
3971 flags |= CO_NOFREE;
3972 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003973 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003974
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003975 return flags;
3976}
3977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978static PyCodeObject *
3979makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003980{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 PyObject *tmp;
3982 PyCodeObject *co = NULL;
3983 PyObject *consts = NULL;
3984 PyObject *names = NULL;
3985 PyObject *varnames = NULL;
3986 PyObject *filename = NULL;
3987 PyObject *name = NULL;
3988 PyObject *freevars = NULL;
3989 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003990 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 tmp = dict_keys_inorder(c->u->u_consts, 0);
3994 if (!tmp)
3995 goto error;
3996 consts = PySequence_List(tmp); /* optimize_code requires a list */
3997 Py_DECREF(tmp);
3998
3999 names = dict_keys_inorder(c->u->u_names, 0);
4000 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4001 if (!consts || !names || !varnames)
4002 goto error;
4003
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004004 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4005 if (!cellvars)
4006 goto error;
4007 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4008 if (!freevars)
4009 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 filename = PyString_FromString(c->c_filename);
4011 if (!filename)
4012 goto error;
4013
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004014 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 flags = compute_code_flags(c);
4016 if (flags < 0)
4017 goto error;
4018
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004019 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 if (!bytecode)
4021 goto error;
4022
4023 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4024 if (!tmp)
4025 goto error;
4026 Py_DECREF(consts);
4027 consts = tmp;
4028
Guido van Rossum4f72a782006-10-27 23:31:49 +00004029 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4030 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 bytecode, consts, names, varnames,
4032 freevars, cellvars,
4033 filename, c->u->u_name,
4034 c->u->u_firstlineno,
4035 a->a_lnotab);
4036 error:
4037 Py_XDECREF(consts);
4038 Py_XDECREF(names);
4039 Py_XDECREF(varnames);
4040 Py_XDECREF(filename);
4041 Py_XDECREF(name);
4042 Py_XDECREF(freevars);
4043 Py_XDECREF(cellvars);
4044 Py_XDECREF(bytecode);
4045 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004046}
4047
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004048
4049/* For debugging purposes only */
4050#if 0
4051static void
4052dump_instr(const struct instr *i)
4053{
4054 const char *jrel = i->i_jrel ? "jrel " : "";
4055 const char *jabs = i->i_jabs ? "jabs " : "";
4056 char arg[128];
4057
4058 *arg = '\0';
4059 if (i->i_hasarg)
4060 sprintf(arg, "arg: %d ", i->i_oparg);
4061
4062 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4063 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4064}
4065
4066static void
4067dump_basicblock(const basicblock *b)
4068{
4069 const char *seen = b->b_seen ? "seen " : "";
4070 const char *b_return = b->b_return ? "return " : "";
4071 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4072 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4073 if (b->b_instr) {
4074 int i;
4075 for (i = 0; i < b->b_iused; i++) {
4076 fprintf(stderr, " [%02d] ", i);
4077 dump_instr(b->b_instr + i);
4078 }
4079 }
4080}
4081#endif
4082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083static PyCodeObject *
4084assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004085{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086 basicblock *b, *entryblock;
4087 struct assembler a;
4088 int i, j, nblocks;
4089 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 /* Make sure every block that falls off the end returns None.
4092 XXX NEXT_BLOCK() isn't quite right, because if the last
4093 block ends with a jump or return b_next shouldn't set.
4094 */
4095 if (!c->u->u_curblock->b_return) {
4096 NEXT_BLOCK(c);
4097 if (addNone)
4098 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4099 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004100 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102 nblocks = 0;
4103 entryblock = NULL;
4104 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4105 nblocks++;
4106 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004107 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004108
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004109 /* Set firstlineno if it wasn't explicitly set. */
4110 if (!c->u->u_firstlineno) {
4111 if (entryblock && entryblock->b_instr)
4112 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4113 else
4114 c->u->u_firstlineno = 1;
4115 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4117 goto error;
4118 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004121 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 /* Emit code in reverse postorder from dfs. */
4124 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004125 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126 for (j = 0; j < b->b_iused; j++)
4127 if (!assemble_emit(&a, &b->b_instr[j]))
4128 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004129 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4132 goto error;
4133 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4134 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004135
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 co = makecode(c, &a);
4137 error:
4138 assemble_free(&a);
4139 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004140}