blob: a47c8e647603099846c20d4df433cf82cd5ddb75 [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
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 * this file.
12 * 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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000043 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000050};
51
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000067 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000069 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000073 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074} basicblock;
75
76/* fblockinfo tracks the current frame block.
77
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078A frame block is used to handle loops, try/except, and try/finally.
79It's called a frame block to distinguish it from a basic block in the
80compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081*/
82
83enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
84
85struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000086 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 basicblock *fb_block;
88};
89
90/* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
92*/
93struct compiler_unit {
94 PySTEntryObject *u_ste;
95
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +000098 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 the argument for opcodes that refer to those collections.
100 */
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
106
107 PyObject *u_private; /* for private name mangling */
108
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000109 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000110 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111 /* Pointer to the most recently allocated block. By following b_list
112 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116
117 int u_nfblocks;
118 struct fblockinfo u_fblock[CO_MAXBLOCKS];
119
120 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000121 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000122 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123 has been generated with current lineno */
124};
125
126/* This struct captures the global state of a compilation.
127
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000128The u pointer points to the current compilation unit, while units
129for enclosing blocks are stored in c_stack. The u and c_stack are
130managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131*/
132
133struct compiler {
134 const char *c_filename;
135 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000136 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137 PyCompilerFlags *c_flags;
138
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000142 struct compiler_unit *u; /* compiler state for current block */
143 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000144 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000145 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146};
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148static int compiler_enter_scope(struct compiler *, identifier, void *, int);
149static void compiler_free(struct compiler *);
150static basicblock *compiler_new_block(struct compiler *);
151static int compiler_next_instr(struct compiler *, basicblock *);
152static int compiler_addop(struct compiler *, int);
153static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
154static int compiler_addop_i(struct compiler *, int, int);
155static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156static basicblock *compiler_use_new_block(struct compiler *);
157static int compiler_error(struct compiler *, const char *);
158static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
159
160static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
161static int compiler_visit_stmt(struct compiler *, stmt_ty);
162static int compiler_visit_keyword(struct compiler *, keyword_ty);
163static int compiler_visit_expr(struct compiler *, expr_ty);
164static int compiler_augassign(struct compiler *, stmt_ty);
165static int compiler_visit_slice(struct compiler *, slice_ty,
166 expr_context_ty);
167
168static int compiler_push_fblock(struct compiler *, enum fblocktype,
169 basicblock *);
170static void compiler_pop_fblock(struct compiler *, enum fblocktype,
171 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000172/* Returns true if there is a loop on the fblock stack. */
173static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174
175static int inplace_binop(struct compiler *, operator_ty);
176static int expr_constant(expr_ty e);
177
Guido van Rossumc2e20742006-02-27 22:32:47 +0000178static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000179static int compiler_call_helper(struct compiler *c, int n,
180 asdl_seq *args,
181 asdl_seq *keywords,
182 expr_ty starargs,
183 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000184
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185static PyCodeObject *assemble(struct compiler *, int addNone);
186static PyObject *__doc__;
187
188PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000189_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000190{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191 /* Name mangling: __private becomes _classname__private.
192 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000193 const char *p, *name = PyString_AsString(ident);
194 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 size_t nlen, plen;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000196 if (privateobj == NULL || !PyString_Check(privateobj) ||
197 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000198 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000200 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000201 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 nlen = strlen(name);
203 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 /* Strip leading underscores from class name */
208 while (*p == '_')
209 p++;
210 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000211 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214 plen = strlen(p);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000215 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
216 if (!ident)
217 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000219 buffer = PyString_AS_STRING(ident);
220 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 strncpy(buffer+1, p, plen);
222 strcpy(buffer+1+plen, name);
223 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000224}
225
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226static int
227compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000228{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000230
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 c->c_stack = PyList_New(0);
232 if (!c->c_stack)
233 return 0;
234
235 return 1;
236}
237
238PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000239PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000240 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241{
242 struct compiler c;
243 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000244 PyCompilerFlags local_flags;
245 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000247 if (!__doc__) {
248 __doc__ = PyString_InternFromString("__doc__");
249 if (!__doc__)
250 return NULL;
251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252
253 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000254 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000256 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257 c.c_future = PyFuture_FromAST(mod, filename);
258 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000259 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000261 local_flags.cf_flags = 0;
262 flags = &local_flags;
263 }
264 merged = c.c_future->ff_features | flags->cf_flags;
265 c.c_future->ff_features = merged;
266 flags->cf_flags = merged;
267 c.c_flags = flags;
268 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269
270 c.c_st = PySymtable_Build(mod, filename, c.c_future);
271 if (c.c_st == NULL) {
272 if (!PyErr_Occurred())
273 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000274 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 }
276
277 /* XXX initialize to NULL for now, need to handle */
278 c.c_encoding = NULL;
279
280 co = compiler_mod(&c, mod);
281
Thomas Wouters1175c432006-02-27 22:49:54 +0000282 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000284 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285 return co;
286}
287
288PyCodeObject *
289PyNode_Compile(struct _node *n, const char *filename)
290{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000291 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000292 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000293 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294 if (!arena)
295 return NULL;
296 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000297 if (mod)
298 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000299 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000300 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000301}
302
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000303static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000305{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306 if (c->c_st)
307 PySymtable_Free(c->c_st);
308 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000311}
312
Guido van Rossum79f25d91997-04-29 20:08:16 +0000313static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000315{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000316 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317 PyObject *v, *k;
318 PyObject *dict = PyDict_New();
319 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000320
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 n = PyList_Size(list);
322 for (i = 0; i < n; i++) {
323 v = PyInt_FromLong(i);
324 if (!v) {
325 Py_DECREF(dict);
326 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000327 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000328 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
331 Py_XDECREF(k);
332 Py_DECREF(v);
333 Py_DECREF(dict);
334 return NULL;
335 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000336 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 return dict;
340}
341
342/* Return new dict containing names from src that match scope(s).
343
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000344src is a symbol table dictionary. If the scope of a name matches
345either scope_type or flag is set, insert it into the new dict. The
346values are integers, starting at offset and increasing by one for
347each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348*/
349
350static PyObject *
351dictbytype(PyObject *src, int scope_type, int flag, int offset)
352{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000353 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 PyObject *k, *v, *dest = PyDict_New();
355
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000356 assert(offset >= 0);
357 if (dest == NULL)
358 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359
360 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000361 /* XXX this should probably be a macro in symtable.h */
362 assert(PyInt_Check(v));
363 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000365 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
366 PyObject *tuple, *item = PyInt_FromLong(i);
367 if (item == NULL) {
368 Py_DECREF(dest);
369 return NULL;
370 }
371 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000372 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000373 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
374 Py_DECREF(item);
375 Py_DECREF(dest);
376 Py_XDECREF(tuple);
377 return NULL;
378 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000380 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 }
383 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000384}
385
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386static void
387compiler_unit_check(struct compiler_unit *u)
388{
389 basicblock *block;
390 for (block = u->u_blocks; block != NULL; block = block->b_list) {
391 assert(block != (void *)0xcbcbcbcb);
392 assert(block != (void *)0xfbfbfbfb);
393 assert(block != (void *)0xdbdbdbdb);
394 if (block->b_instr != NULL) {
395 assert(block->b_ialloc > 0);
396 assert(block->b_iused > 0);
397 assert(block->b_ialloc >= block->b_iused);
398 }
399 else {
400 assert (block->b_iused == 0);
401 assert (block->b_ialloc == 0);
402 }
403 }
404}
405
406static void
407compiler_unit_free(struct compiler_unit *u)
408{
409 basicblock *b, *next;
410
411 compiler_unit_check(u);
412 b = u->u_blocks;
413 while (b != NULL) {
414 if (b->b_instr)
415 PyObject_Free((void *)b->b_instr);
416 next = b->b_list;
417 PyObject_Free((void *)b);
418 b = next;
419 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000420 Py_CLEAR(u->u_ste);
421 Py_CLEAR(u->u_name);
422 Py_CLEAR(u->u_consts);
423 Py_CLEAR(u->u_names);
424 Py_CLEAR(u->u_varnames);
425 Py_CLEAR(u->u_freevars);
426 Py_CLEAR(u->u_cellvars);
427 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 PyObject_Free(u);
429}
430
431static int
432compiler_enter_scope(struct compiler *c, identifier name, void *key,
433 int lineno)
434{
435 struct compiler_unit *u;
436
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
438 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000439 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000440 PyErr_NoMemory();
441 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000442 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000443 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000445 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 u->u_ste = PySymtable_Lookup(c->c_st, key);
447 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000448 compiler_unit_free(u);
449 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 }
451 Py_INCREF(name);
452 u->u_name = name;
453 u->u_varnames = list2dict(u->u_ste->ste_varnames);
454 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000455 if (!u->u_varnames || !u->u_cellvars) {
456 compiler_unit_free(u);
457 return 0;
458 }
459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000461 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000462 if (!u->u_freevars) {
463 compiler_unit_free(u);
464 return 0;
465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466
467 u->u_blocks = NULL;
468 u->u_tmpname = 0;
469 u->u_nfblocks = 0;
470 u->u_firstlineno = lineno;
471 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000472 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 u->u_consts = PyDict_New();
474 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000475 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 return 0;
477 }
478 u->u_names = PyDict_New();
479 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000480 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000481 return 0;
482 }
483
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000484 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485
486 /* Push the old compiler_unit on the stack. */
487 if (c->u) {
488 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000489 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
490 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000491 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 return 0;
493 }
494 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000495 u->u_private = c->u->u_private;
496 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 }
498 c->u = u;
499
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000500 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000501 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502 return 0;
503
504 return 1;
505}
506
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000507static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508compiler_exit_scope(struct compiler *c)
509{
510 int n;
511 PyObject *wrapper;
512
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000513 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 compiler_unit_free(c->u);
515 /* Restore c->u to the parent unit. */
516 n = PyList_GET_SIZE(c->c_stack) - 1;
517 if (n >= 0) {
518 wrapper = PyList_GET_ITEM(c->c_stack, n);
519 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000520 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000521 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000523 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524 compiler_unit_check(c->u);
525 }
526 else
527 c->u = NULL;
528
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529}
530
Guido van Rossumc2e20742006-02-27 22:32:47 +0000531/* Allocate a new "anonymous" local variable.
532 Used by list comprehensions and with statements.
533*/
534
535static PyObject *
536compiler_new_tmpname(struct compiler *c)
537{
538 char tmpname[256];
539 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
540 return PyString_FromString(tmpname);
541}
542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543/* Allocate a new block and return a pointer to it.
544 Returns NULL on error.
545*/
546
547static basicblock *
548compiler_new_block(struct compiler *c)
549{
550 basicblock *b;
551 struct compiler_unit *u;
552
553 u = c->u;
554 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000555 if (b == NULL) {
556 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000558 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000560 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 b->b_list = u->u_blocks;
562 u->u_blocks = b;
563 return b;
564}
565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566static basicblock *
567compiler_use_new_block(struct compiler *c)
568{
569 basicblock *block = compiler_new_block(c);
570 if (block == NULL)
571 return NULL;
572 c->u->u_curblock = block;
573 return block;
574}
575
576static basicblock *
577compiler_next_block(struct compiler *c)
578{
579 basicblock *block = compiler_new_block(c);
580 if (block == NULL)
581 return NULL;
582 c->u->u_curblock->b_next = block;
583 c->u->u_curblock = block;
584 return block;
585}
586
587static basicblock *
588compiler_use_next_block(struct compiler *c, basicblock *block)
589{
590 assert(block != NULL);
591 c->u->u_curblock->b_next = block;
592 c->u->u_curblock = block;
593 return block;
594}
595
596/* Returns the offset of the next instruction in the current block's
597 b_instr array. Resizes the b_instr as necessary.
598 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000599*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
601static int
602compiler_next_instr(struct compiler *c, basicblock *b)
603{
604 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000605 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000606 b->b_instr = (struct instr *)PyObject_Malloc(
607 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608 if (b->b_instr == NULL) {
609 PyErr_NoMemory();
610 return -1;
611 }
612 b->b_ialloc = DEFAULT_BLOCK_SIZE;
613 memset((char *)b->b_instr, 0,
614 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000615 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 size_t oldsize, newsize;
619 oldsize = b->b_ialloc * sizeof(struct instr);
620 newsize = oldsize << 1;
621 if (newsize == 0) {
622 PyErr_NoMemory();
623 return -1;
624 }
625 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 tmp = (struct instr *)PyObject_Realloc(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628 if (tmp == NULL) {
629 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631 }
632 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
634 }
635 return b->b_iused++;
636}
637
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000638/* Set the i_lineno member of the instruction at offse off if the
639 line number for the current expression/statement (?) has not
640 already been set. If it has been set, the call has no effect.
641
642 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000643*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645static void
646compiler_set_lineno(struct compiler *c, int off)
647{
648 basicblock *b;
649 if (c->u->u_lineno_set)
650 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000651 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000652 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000653 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654}
655
656static int
657opcode_stack_effect(int opcode, int oparg)
658{
659 switch (opcode) {
660 case POP_TOP:
661 return -1;
662 case ROT_TWO:
663 case ROT_THREE:
664 return 0;
665 case DUP_TOP:
666 return 1;
667 case ROT_FOUR:
668 return 0;
669
670 case UNARY_POSITIVE:
671 case UNARY_NEGATIVE:
672 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000673 case UNARY_INVERT:
674 return 0;
675
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000676 case LIST_APPEND:
677 return -2;
678
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679 case BINARY_POWER:
680 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681 case BINARY_MODULO:
682 case BINARY_ADD:
683 case BINARY_SUBTRACT:
684 case BINARY_SUBSCR:
685 case BINARY_FLOOR_DIVIDE:
686 case BINARY_TRUE_DIVIDE:
687 return -1;
688 case INPLACE_FLOOR_DIVIDE:
689 case INPLACE_TRUE_DIVIDE:
690 return -1;
691
692 case SLICE+0:
693 return 1;
694 case SLICE+1:
695 return 0;
696 case SLICE+2:
697 return 0;
698 case SLICE+3:
699 return -1;
700
701 case STORE_SLICE+0:
702 return -2;
703 case STORE_SLICE+1:
704 return -3;
705 case STORE_SLICE+2:
706 return -3;
707 case STORE_SLICE+3:
708 return -4;
709
710 case DELETE_SLICE+0:
711 return -1;
712 case DELETE_SLICE+1:
713 return -2;
714 case DELETE_SLICE+2:
715 return -2;
716 case DELETE_SLICE+3:
717 return -3;
718
719 case INPLACE_ADD:
720 case INPLACE_SUBTRACT:
721 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722 case INPLACE_MODULO:
723 return -1;
724 case STORE_SUBSCR:
725 return -3;
726 case DELETE_SUBSCR:
727 return -2;
728
729 case BINARY_LSHIFT:
730 case BINARY_RSHIFT:
731 case BINARY_AND:
732 case BINARY_XOR:
733 case BINARY_OR:
734 return -1;
735 case INPLACE_POWER:
736 return -1;
737 case GET_ITER:
738 return 0;
739
740 case PRINT_EXPR:
741 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000742 case LOAD_BUILD_CLASS:
743 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000744 case INPLACE_LSHIFT:
745 case INPLACE_RSHIFT:
746 case INPLACE_AND:
747 case INPLACE_XOR:
748 case INPLACE_OR:
749 return -1;
750 case BREAK_LOOP:
751 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000752 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000753 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000754 case STORE_LOCALS:
755 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 case RETURN_VALUE:
757 return -1;
758 case IMPORT_STAR:
759 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760 case YIELD_VALUE:
761 return 0;
762
763 case POP_BLOCK:
764 return 0;
765 case END_FINALLY:
766 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767
768 case STORE_NAME:
769 return -1;
770 case DELETE_NAME:
771 return 0;
772 case UNPACK_SEQUENCE:
773 return oparg-1;
774 case FOR_ITER:
775 return 1;
776
777 case STORE_ATTR:
778 return -2;
779 case DELETE_ATTR:
780 return -1;
781 case STORE_GLOBAL:
782 return -1;
783 case DELETE_GLOBAL:
784 return 0;
785 case DUP_TOPX:
786 return oparg;
787 case LOAD_CONST:
788 return 1;
789 case LOAD_NAME:
790 return 1;
791 case BUILD_TUPLE:
792 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000793 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 return 1-oparg;
795 case BUILD_MAP:
796 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000797 case MAKE_BYTES:
798 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 case LOAD_ATTR:
800 return 0;
801 case COMPARE_OP:
802 return -1;
803 case IMPORT_NAME:
804 return 0;
805 case IMPORT_FROM:
806 return 1;
807
808 case JUMP_FORWARD:
809 case JUMP_IF_FALSE:
810 case JUMP_IF_TRUE:
811 case JUMP_ABSOLUTE:
812 return 0;
813
814 case LOAD_GLOBAL:
815 return 1;
816
817 case CONTINUE_LOOP:
818 return 0;
819 case SETUP_LOOP:
820 return 0;
821 case SETUP_EXCEPT:
822 case SETUP_FINALLY:
823 return 3; /* actually pushed by an exception */
824
825 case LOAD_FAST:
826 return 1;
827 case STORE_FAST:
828 return -1;
829 case DELETE_FAST:
830 return 0;
831
832 case RAISE_VARARGS:
833 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000834#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 case CALL_FUNCTION:
836 return -NARGS(oparg);
837 case CALL_FUNCTION_VAR:
838 case CALL_FUNCTION_KW:
839 return -NARGS(oparg)-1;
840 case CALL_FUNCTION_VAR_KW:
841 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000843 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000844 case MAKE_CLOSURE:
845 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000846#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 case BUILD_SLICE:
848 if (oparg == 3)
849 return -2;
850 else
851 return -1;
852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853 case LOAD_CLOSURE:
854 return 1;
855 case LOAD_DEREF:
856 return 1;
857 case STORE_DEREF:
858 return -1;
859 default:
860 fprintf(stderr, "opcode = %d\n", opcode);
861 Py_FatalError("opcode_stack_effect()");
862
863 }
864 return 0; /* not reachable */
865}
866
867/* Add an opcode with no argument.
868 Returns 0 on failure, 1 on success.
869*/
870
871static int
872compiler_addop(struct compiler *c, int opcode)
873{
874 basicblock *b;
875 struct instr *i;
876 int off;
877 off = compiler_next_instr(c, c->u->u_curblock);
878 if (off < 0)
879 return 0;
880 b = c->u->u_curblock;
881 i = &b->b_instr[off];
882 i->i_opcode = opcode;
883 i->i_hasarg = 0;
884 if (opcode == RETURN_VALUE)
885 b->b_return = 1;
886 compiler_set_lineno(c, off);
887 return 1;
888}
889
890static int
891compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
892{
893 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000894 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000896 /* necessary to make sure types aren't coerced (e.g., int and long) */
897 t = PyTuple_Pack(2, o, o->ob_type);
898 if (t == NULL)
899 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
901 v = PyDict_GetItem(dict, t);
902 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000903 if (PyErr_Occurred())
904 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905 arg = PyDict_Size(dict);
906 v = PyInt_FromLong(arg);
907 if (!v) {
908 Py_DECREF(t);
909 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000910 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 if (PyDict_SetItem(dict, t, v) < 0) {
912 Py_DECREF(t);
913 Py_DECREF(v);
914 return -1;
915 }
916 Py_DECREF(v);
917 }
918 else
919 arg = PyInt_AsLong(v);
920 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000921 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922}
923
924static int
925compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
926 PyObject *o)
927{
928 int arg = compiler_add_o(c, dict, o);
929 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000930 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931 return compiler_addop_i(c, opcode, arg);
932}
933
934static int
935compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000936 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937{
938 int arg;
939 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
940 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000941 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 arg = compiler_add_o(c, dict, mangled);
943 Py_DECREF(mangled);
944 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000945 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 return compiler_addop_i(c, opcode, arg);
947}
948
949/* Add an opcode with an integer argument.
950 Returns 0 on failure, 1 on success.
951*/
952
953static int
954compiler_addop_i(struct compiler *c, int opcode, int oparg)
955{
956 struct instr *i;
957 int off;
958 off = compiler_next_instr(c, c->u->u_curblock);
959 if (off < 0)
960 return 0;
961 i = &c->u->u_curblock->b_instr[off];
962 i->i_opcode = opcode;
963 i->i_oparg = oparg;
964 i->i_hasarg = 1;
965 compiler_set_lineno(c, off);
966 return 1;
967}
968
969static int
970compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
971{
972 struct instr *i;
973 int off;
974
975 assert(b != NULL);
976 off = compiler_next_instr(c, c->u->u_curblock);
977 if (off < 0)
978 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 i = &c->u->u_curblock->b_instr[off];
980 i->i_opcode = opcode;
981 i->i_target = b;
982 i->i_hasarg = 1;
983 if (absolute)
984 i->i_jabs = 1;
985 else
986 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 return 1;
989}
990
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000991/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
992 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 it as the current block. NEXT_BLOCK() also creates an implicit jump
994 from the current block to the new block.
995*/
996
Thomas Wouters89f507f2006-12-13 04:49:30 +0000997/* The returns inside these macros make it impossible to decref objects
998 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999*/
1000
1001
1002#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001003 if (compiler_use_new_block((C)) == NULL) \
1004 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005}
1006
1007#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008 if (compiler_next_block((C)) == NULL) \
1009 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010}
1011
1012#define ADDOP(C, OP) { \
1013 if (!compiler_addop((C), (OP))) \
1014 return 0; \
1015}
1016
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001017#define ADDOP_IN_SCOPE(C, OP) { \
1018 if (!compiler_addop((C), (OP))) { \
1019 compiler_exit_scope(c); \
1020 return 0; \
1021 } \
1022}
1023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024#define ADDOP_O(C, OP, O, TYPE) { \
1025 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1026 return 0; \
1027}
1028
1029#define ADDOP_NAME(C, OP, O, TYPE) { \
1030 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1031 return 0; \
1032}
1033
1034#define ADDOP_I(C, OP, O) { \
1035 if (!compiler_addop_i((C), (OP), (O))) \
1036 return 0; \
1037}
1038
1039#define ADDOP_JABS(C, OP, O) { \
1040 if (!compiler_addop_j((C), (OP), (O), 1)) \
1041 return 0; \
1042}
1043
1044#define ADDOP_JREL(C, OP, O) { \
1045 if (!compiler_addop_j((C), (OP), (O), 0)) \
1046 return 0; \
1047}
1048
1049/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1050 the ASDL name to synthesize the name of the C type and the visit function.
1051*/
1052
1053#define VISIT(C, TYPE, V) {\
1054 if (!compiler_visit_ ## TYPE((C), (V))) \
1055 return 0; \
1056}
1057
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001058#define VISIT_IN_SCOPE(C, TYPE, V) {\
1059 if (!compiler_visit_ ## TYPE((C), (V))) { \
1060 compiler_exit_scope(c); \
1061 return 0; \
1062 } \
1063}
1064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065#define VISIT_SLICE(C, V, CTX) {\
1066 if (!compiler_visit_slice((C), (V), (CTX))) \
1067 return 0; \
1068}
1069
1070#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001071 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001073 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001074 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075 if (!compiler_visit_ ## TYPE((C), elt)) \
1076 return 0; \
1077 } \
1078}
1079
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001080#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001081 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001082 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001083 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001084 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001085 if (!compiler_visit_ ## TYPE((C), elt)) { \
1086 compiler_exit_scope(c); \
1087 return 0; \
1088 } \
1089 } \
1090}
1091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092static int
1093compiler_isdocstring(stmt_ty s)
1094{
1095 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001096 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 return s->v.Expr.value->kind == Str_kind;
1098}
1099
1100/* Compile a sequence of statements, checking for a docstring. */
1101
1102static int
1103compiler_body(struct compiler *c, asdl_seq *stmts)
1104{
1105 int i = 0;
1106 stmt_ty st;
1107
1108 if (!asdl_seq_LEN(stmts))
1109 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001110 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111 if (compiler_isdocstring(st)) {
1112 i = 1;
1113 VISIT(c, expr, st->v.Expr.value);
1114 if (!compiler_nameop(c, __doc__, Store))
1115 return 0;
1116 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001117 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 return 1;
1120}
1121
1122static PyCodeObject *
1123compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001124{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001125 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001126 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127 static PyObject *module;
1128 if (!module) {
1129 module = PyString_FromString("<module>");
1130 if (!module)
1131 return NULL;
1132 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001133 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1134 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001135 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 switch (mod->kind) {
1137 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001138 if (!compiler_body(c, mod->v.Module.body)) {
1139 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001141 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 break;
1143 case Interactive_kind:
1144 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001145 VISIT_SEQ_IN_SCOPE(c, stmt,
1146 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 break;
1148 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001149 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001150 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 break;
1152 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001153 PyErr_SetString(PyExc_SystemError,
1154 "suite should not be possible");
1155 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001156 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001157 PyErr_Format(PyExc_SystemError,
1158 "module kind %d should not be possible",
1159 mod->kind);
1160 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001161 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 co = assemble(c, addNone);
1163 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001164 return co;
1165}
1166
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167/* The test for LOCAL must come before the test for FREE in order to
1168 handle classes where name is both local and free. The local var is
1169 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001170*/
1171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172static int
1173get_ref_type(struct compiler *c, PyObject *name)
1174{
1175 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001176 if (scope == 0) {
1177 char buf[350];
1178 PyOS_snprintf(buf, sizeof(buf),
1179 "unknown scope for %.100s in %.100s(%s) in %s\n"
1180 "symbols: %s\nlocals: %s\nglobals: %s\n",
1181 PyString_AS_STRING(name),
1182 PyString_AS_STRING(c->u->u_name),
1183 PyObject_REPR(c->u->u_ste->ste_id),
1184 c->c_filename,
1185 PyObject_REPR(c->u->u_ste->ste_symbols),
1186 PyObject_REPR(c->u->u_varnames),
1187 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001189 Py_FatalError(buf);
1190 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001191
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001192 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193}
1194
1195static int
1196compiler_lookup_arg(PyObject *dict, PyObject *name)
1197{
1198 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001199 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001201 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001203 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001205 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 return PyInt_AS_LONG(v);
1207}
1208
1209static int
1210compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1211{
1212 int i, free = PyCode_GetNumFree(co);
1213 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001214 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1215 ADDOP_I(c, MAKE_FUNCTION, args);
1216 return 1;
1217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 for (i = 0; i < free; ++i) {
1219 /* Bypass com_addop_varname because it will generate
1220 LOAD_DEREF but LOAD_CLOSURE is needed.
1221 */
1222 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1223 int arg, reftype;
1224
1225 /* Special case: If a class contains a method with a
1226 free variable that has the same name as a method,
1227 the name will be considered free *and* local in the
1228 class. It should be handled by the closure, as
1229 well as by the normal name loookup logic.
1230 */
1231 reftype = get_ref_type(c, name);
1232 if (reftype == CELL)
1233 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1234 else /* (reftype == FREE) */
1235 arg = compiler_lookup_arg(c->u->u_freevars, name);
1236 if (arg == -1) {
1237 printf("lookup %s in %s %d %d\n"
1238 "freevars of %s: %s\n",
1239 PyObject_REPR(name),
1240 PyString_AS_STRING(c->u->u_name),
1241 reftype, arg,
1242 PyString_AS_STRING(co->co_name),
1243 PyObject_REPR(co->co_freevars));
1244 Py_FatalError("compiler_make_closure()");
1245 }
1246 ADDOP_I(c, LOAD_CLOSURE, arg);
1247 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001248 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001250 ADDOP_I(c, MAKE_CLOSURE, args);
1251 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252}
1253
1254static int
1255compiler_decorators(struct compiler *c, asdl_seq* decos)
1256{
1257 int i;
1258
1259 if (!decos)
1260 return 1;
1261
1262 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 }
1265 return 1;
1266}
1267
1268static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001269compiler_unpack_nested(struct compiler *c, asdl_seq *args) {
1270 int i, len;
1271 len = asdl_seq_LEN(args);
1272 ADDOP_I(c, UNPACK_SEQUENCE, len);
1273 for (i = 0; i < len; i++) {
1274 arg_ty elt = (arg_ty)asdl_seq_GET(args, i);
1275 switch (elt->kind) {
1276 case SimpleArg_kind:
1277 if (!compiler_nameop(c, elt->v.SimpleArg.arg, Store))
1278 return 0;
1279 break;
1280 case NestedArgs_kind:
1281 if (!compiler_unpack_nested(c, elt->v.NestedArgs.args))
1282 return 0;
1283 break;
1284 default:
1285 return 0;
1286 }
1287 }
1288 return 1;
1289}
1290
1291static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292compiler_arguments(struct compiler *c, arguments_ty args)
1293{
1294 int i;
1295 int n = asdl_seq_LEN(args->args);
Neal Norwitzc1505362006-12-28 06:47:50 +00001296
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 for (i = 0; i < n; i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001298 arg_ty arg = (arg_ty)asdl_seq_GET(args->args, i);
1299 if (arg->kind == NestedArgs_kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300 PyObject *id = PyString_FromFormat(".%d", i);
1301 if (id == NULL) {
1302 return 0;
1303 }
1304 if (!compiler_nameop(c, id, Load)) {
1305 Py_DECREF(id);
1306 return 0;
1307 }
1308 Py_DECREF(id);
Neal Norwitzc1505362006-12-28 06:47:50 +00001309 if (!compiler_unpack_nested(c, arg->v.NestedArgs.args))
1310 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 }
1312 }
1313 return 1;
1314}
1315
1316static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001317compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1318 asdl_seq *kw_defaults)
1319{
1320 int i, default_count = 0;
1321 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001322 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001323 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1324 if (default_) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001325 ADDOP_O(c, LOAD_CONST, arg->v.SimpleArg.arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001326 if (!compiler_visit_expr(c, default_)) {
1327 return -1;
1328 }
1329 default_count++;
1330 }
1331 }
1332 return default_count;
1333}
1334
1335static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001336compiler_visit_argannotation(struct compiler *c, identifier id,
1337 expr_ty annotation, PyObject *names)
1338{
1339 if (annotation) {
1340 VISIT(c, expr, annotation);
1341 if (PyList_Append(names, id))
1342 return -1;
1343 }
1344 return 0;
1345}
1346
1347static int
1348compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1349 PyObject *names)
1350{
1351 int i, error;
1352 for (i = 0; i < asdl_seq_LEN(args); i++) {
1353 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1354 if (arg->kind == NestedArgs_kind)
1355 error = compiler_visit_argannotations(
1356 c,
1357 arg->v.NestedArgs.args,
1358 names);
1359 else
1360 error = compiler_visit_argannotation(
1361 c,
1362 arg->v.SimpleArg.arg,
1363 arg->v.SimpleArg.annotation,
1364 names);
1365 if (error)
1366 return error;
1367 }
1368 return 0;
1369}
1370
1371static int
1372compiler_visit_annotations(struct compiler *c, arguments_ty args,
1373 expr_ty returns)
1374{
Guido van Rossum0240b922007-02-26 21:23:50 +00001375 /* Push arg annotations and a list of the argument names. Return the #
1376 of items pushed. The expressions are evaluated out-of-order wrt the
1377 source code.
1378
1379 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1380 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001381 static identifier return_str;
1382 PyObject *names;
1383 int len;
1384 names = PyList_New(0);
1385 if (!names)
1386 return -1;
1387
1388 if (compiler_visit_argannotations(c, args->args, names))
1389 goto error;
1390 if (args->varargannotation &&
1391 compiler_visit_argannotation(c, args->vararg,
1392 args->varargannotation, names))
1393 goto error;
1394 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1395 goto error;
1396 if (args->kwargannotation &&
1397 compiler_visit_argannotation(c, args->kwarg,
1398 args->kwargannotation, names))
1399 goto error;
1400
1401 if (!return_str) {
1402 return_str = PyString_InternFromString("return");
1403 if (!return_str)
1404 goto error;
1405 }
1406 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1407 goto error;
1408 }
1409
1410 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001411 if (len > 65534) {
1412 /* len must fit in 16 bits, and len is incremented below */
1413 PyErr_SetString(PyExc_SyntaxError,
1414 "too many annotations");
1415 goto error;
1416 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001417 if (len) {
1418 /* convert names to a tuple and place on stack */
1419 PyObject *elt;
1420 int i;
1421 PyObject *s = PyTuple_New(len);
1422 if (!s)
1423 goto error;
1424 for (i = 0; i < len; i++) {
1425 elt = PyList_GET_ITEM(names, i);
1426 Py_INCREF(elt);
1427 PyTuple_SET_ITEM(s, i, elt);
1428 }
1429 ADDOP_O(c, LOAD_CONST, s, consts);
1430 Py_DECREF(s);
1431 len++; /* include the just-pushed tuple */
1432 }
1433 Py_DECREF(names);
1434 return len;
1435
1436error:
1437 Py_DECREF(names);
1438 return -1;
1439}
1440
1441static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442compiler_function(struct compiler *c, stmt_ty s)
1443{
1444 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001445 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001447 expr_ty returns = s->v.FunctionDef.returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 asdl_seq* decos = s->v.FunctionDef.decorators;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001449 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001451 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452
1453 assert(s->kind == FunctionDef_kind);
1454
1455 if (!compiler_decorators(c, decos))
1456 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001457 if (args->kwonlyargs) {
1458 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1459 args->kw_defaults);
1460 if (res < 0)
1461 return 0;
1462 kw_default_count = res;
1463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 if (args->defaults)
1465 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001467 if (num_annotations < 0)
1468 return 0;
1469 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001470
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1472 s->lineno))
1473 return 0;
1474
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001475 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001476 docstring = compiler_isdocstring(st);
1477 if (docstring)
1478 first_const = st->v.Expr.value->v.Str.s;
1479 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001480 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001481 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001484 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 compiler_arguments(c, args);
1486
1487 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001489 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001490 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001492 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1493 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 }
1495 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001496 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 if (co == NULL)
1498 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 arglength = asdl_seq_LEN(args->defaults);
1501 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001502 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001504 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
Neal Norwitzc1505362006-12-28 06:47:50 +00001506 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1508 ADDOP_I(c, CALL_FUNCTION, 1);
1509 }
1510
1511 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1512}
1513
1514static int
1515compiler_class(struct compiler *c, stmt_ty s)
1516{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001517 static PyObject *build_class = NULL;
1518 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001520 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001521 PySTEntryObject *ste;
Guido van Rossum3a383622007-03-21 21:26:58 +00001522 int err;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001524 /* initialize statics */
1525 if (build_class == NULL) {
1526 build_class = PyString_FromString("__build_class__");
1527 if (build_class == NULL)
1528 return 0;
1529 }
1530 if (locals == NULL) {
1531 locals = PyString_FromString("__locals__");
1532 if (locals == NULL)
1533 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001534 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001536 /* ultimately generate code for:
1537 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1538 where:
1539 <func> is a function/closure created from the class body
1540 <name> is the class name
1541 <bases> is the positional arguments and *varargs argument
1542 <keywords> is the keyword arguments and **kwds argument
1543 This borrows from compiler_call.
1544 */
1545
1546 /* 0. Create a fake variable named __locals__ */
1547 ste = PySymtable_Lookup(c->c_st, s);
1548 if (ste == NULL)
1549 return 0;
1550 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001551 err = PyList_Append(ste->ste_varnames, locals);
1552 Py_DECREF(ste);
1553 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001554 return 0;
1555
1556 /* 1. compile the class body into a code object */
1557 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1558 return 0;
1559 /* this block represents what we do in the new scope */
1560 {
1561 /* use the class name for name mangling */
1562 Py_INCREF(s->v.ClassDef.name);
1563 c->u->u_private = s->v.ClassDef.name;
1564 /* force it to have one mandatory argument */
1565 c->u->u_argcount = 1;
1566 /* load the first argument ... */
1567 ADDOP_I(c, LOAD_FAST, 0);
1568 /* ... and store it into f_locals */
1569 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1570 /* load __name__ ... */
1571 str = PyString_InternFromString("__name__");
1572 if (!str || !compiler_nameop(c, str, Load)) {
1573 Py_XDECREF(str);
1574 compiler_exit_scope(c);
1575 return 0;
1576 }
1577 Py_DECREF(str);
1578 /* ... and store it as __module__ */
1579 str = PyString_InternFromString("__module__");
1580 if (!str || !compiler_nameop(c, str, Store)) {
1581 Py_XDECREF(str);
1582 compiler_exit_scope(c);
1583 return 0;
1584 }
1585 Py_DECREF(str);
1586 /* compile the body proper */
1587 if (!compiler_body(c, s->v.ClassDef.body)) {
1588 compiler_exit_scope(c);
1589 return 0;
1590 }
1591 /* return None */
1592 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1593 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1594 /* create the code object */
1595 co = assemble(c, 1);
1596 }
1597 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001598 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599 if (co == NULL)
1600 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001602 /* 2. load the 'build_class' function */
1603 ADDOP(c, LOAD_BUILD_CLASS);
1604
1605 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001606 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001607 Py_DECREF(co);
1608
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001609 /* 4. load class name */
1610 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1611
1612 /* 5. generate the rest of the code for the call */
1613 if (!compiler_call_helper(c, 2,
1614 s->v.ClassDef.bases,
1615 s->v.ClassDef.keywords,
1616 s->v.ClassDef.starargs,
1617 s->v.ClassDef.kwargs))
1618 return 0;
1619
1620 /* 6. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1622 return 0;
1623 return 1;
1624}
1625
1626static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001627compiler_ifexp(struct compiler *c, expr_ty e)
1628{
1629 basicblock *end, *next;
1630
1631 assert(e->kind == IfExp_kind);
1632 end = compiler_new_block(c);
1633 if (end == NULL)
1634 return 0;
1635 next = compiler_new_block(c);
1636 if (next == NULL)
1637 return 0;
1638 VISIT(c, expr, e->v.IfExp.test);
1639 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1640 ADDOP(c, POP_TOP);
1641 VISIT(c, expr, e->v.IfExp.body);
1642 ADDOP_JREL(c, JUMP_FORWARD, end);
1643 compiler_use_next_block(c, next);
1644 ADDOP(c, POP_TOP);
1645 VISIT(c, expr, e->v.IfExp.orelse);
1646 compiler_use_next_block(c, end);
1647 return 1;
1648}
1649
1650static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651compiler_lambda(struct compiler *c, expr_ty e)
1652{
1653 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001654 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001655 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 arguments_ty args = e->v.Lambda.args;
1657 assert(e->kind == Lambda_kind);
1658
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001659 if (!name) {
1660 name = PyString_InternFromString("<lambda>");
1661 if (!name)
1662 return 0;
1663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664
Guido van Rossum4f72a782006-10-27 23:31:49 +00001665 if (args->kwonlyargs) {
1666 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1667 args->kw_defaults);
1668 if (res < 0) return 0;
1669 kw_default_count = res;
1670 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 if (args->defaults)
1672 VISIT_SEQ(c, expr, args->defaults);
1673 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1674 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001675
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001676 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677 compiler_arguments(c, args);
1678
1679 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001680 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001681 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1682 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001684 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 if (co == NULL)
1686 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687
Guido van Rossum4f72a782006-10-27 23:31:49 +00001688 arglength = asdl_seq_LEN(args->defaults);
1689 arglength |= kw_default_count << 8;
1690 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001691 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692
1693 return 1;
1694}
1695
1696static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697compiler_if(struct compiler *c, stmt_ty s)
1698{
1699 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001700 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 assert(s->kind == If_kind);
1702 end = compiler_new_block(c);
1703 if (end == NULL)
1704 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001705 next = compiler_new_block(c);
1706 if (next == NULL)
1707 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001708
1709 constant = expr_constant(s->v.If.test);
1710 /* constant = 0: "if 0"
1711 * constant = 1: "if 1", "if 2", ...
1712 * constant = -1: rest */
1713 if (constant == 0) {
1714 if (s->v.If.orelse)
1715 VISIT_SEQ(c, stmt, s->v.If.orelse);
1716 } else if (constant == 1) {
1717 VISIT_SEQ(c, stmt, s->v.If.body);
1718 } else {
1719 VISIT(c, expr, s->v.If.test);
1720 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1721 ADDOP(c, POP_TOP);
1722 VISIT_SEQ(c, stmt, s->v.If.body);
1723 ADDOP_JREL(c, JUMP_FORWARD, end);
1724 compiler_use_next_block(c, next);
1725 ADDOP(c, POP_TOP);
1726 if (s->v.If.orelse)
1727 VISIT_SEQ(c, stmt, s->v.If.orelse);
1728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 compiler_use_next_block(c, end);
1730 return 1;
1731}
1732
1733static int
1734compiler_for(struct compiler *c, stmt_ty s)
1735{
1736 basicblock *start, *cleanup, *end;
1737
1738 start = compiler_new_block(c);
1739 cleanup = compiler_new_block(c);
1740 end = compiler_new_block(c);
1741 if (start == NULL || end == NULL || cleanup == NULL)
1742 return 0;
1743 ADDOP_JREL(c, SETUP_LOOP, end);
1744 if (!compiler_push_fblock(c, LOOP, start))
1745 return 0;
1746 VISIT(c, expr, s->v.For.iter);
1747 ADDOP(c, GET_ITER);
1748 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001749 /* XXX(nnorwitz): is there a better way to handle this?
1750 for loops are special, we want to be able to trace them
1751 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001752 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753 ADDOP_JREL(c, FOR_ITER, cleanup);
1754 VISIT(c, expr, s->v.For.target);
1755 VISIT_SEQ(c, stmt, s->v.For.body);
1756 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1757 compiler_use_next_block(c, cleanup);
1758 ADDOP(c, POP_BLOCK);
1759 compiler_pop_fblock(c, LOOP, start);
1760 VISIT_SEQ(c, stmt, s->v.For.orelse);
1761 compiler_use_next_block(c, end);
1762 return 1;
1763}
1764
1765static int
1766compiler_while(struct compiler *c, stmt_ty s)
1767{
1768 basicblock *loop, *orelse, *end, *anchor = NULL;
1769 int constant = expr_constant(s->v.While.test);
1770
1771 if (constant == 0)
1772 return 1;
1773 loop = compiler_new_block(c);
1774 end = compiler_new_block(c);
1775 if (constant == -1) {
1776 anchor = compiler_new_block(c);
1777 if (anchor == NULL)
1778 return 0;
1779 }
1780 if (loop == NULL || end == NULL)
1781 return 0;
1782 if (s->v.While.orelse) {
1783 orelse = compiler_new_block(c);
1784 if (orelse == NULL)
1785 return 0;
1786 }
1787 else
1788 orelse = NULL;
1789
1790 ADDOP_JREL(c, SETUP_LOOP, end);
1791 compiler_use_next_block(c, loop);
1792 if (!compiler_push_fblock(c, LOOP, loop))
1793 return 0;
1794 if (constant == -1) {
1795 VISIT(c, expr, s->v.While.test);
1796 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1797 ADDOP(c, POP_TOP);
1798 }
1799 VISIT_SEQ(c, stmt, s->v.While.body);
1800 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1801
1802 /* XXX should the two POP instructions be in a separate block
1803 if there is no else clause ?
1804 */
1805
1806 if (constant == -1) {
1807 compiler_use_next_block(c, anchor);
1808 ADDOP(c, POP_TOP);
1809 ADDOP(c, POP_BLOCK);
1810 }
1811 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001812 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813 VISIT_SEQ(c, stmt, s->v.While.orelse);
1814 compiler_use_next_block(c, end);
1815
1816 return 1;
1817}
1818
1819static int
1820compiler_continue(struct compiler *c)
1821{
1822 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001823 static const char IN_FINALLY_ERROR_MSG[] =
1824 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001825 int i;
1826
1827 if (!c->u->u_nfblocks)
1828 return compiler_error(c, LOOP_ERROR_MSG);
1829 i = c->u->u_nfblocks - 1;
1830 switch (c->u->u_fblock[i].fb_type) {
1831 case LOOP:
1832 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1833 break;
1834 case EXCEPT:
1835 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001836 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1837 /* Prevent continue anywhere under a finally
1838 even if hidden in a sub-try or except. */
1839 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1840 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001842 if (i == -1)
1843 return compiler_error(c, LOOP_ERROR_MSG);
1844 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1845 break;
1846 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001847 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 }
1849
1850 return 1;
1851}
1852
1853/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1854
1855 SETUP_FINALLY L
1856 <code for body>
1857 POP_BLOCK
1858 LOAD_CONST <None>
1859 L: <code for finalbody>
1860 END_FINALLY
1861
1862 The special instructions use the block stack. Each block
1863 stack entry contains the instruction that created it (here
1864 SETUP_FINALLY), the level of the value stack at the time the
1865 block stack entry was created, and a label (here L).
1866
1867 SETUP_FINALLY:
1868 Pushes the current value stack level and the label
1869 onto the block stack.
1870 POP_BLOCK:
1871 Pops en entry from the block stack, and pops the value
1872 stack until its level is the same as indicated on the
1873 block stack. (The label is ignored.)
1874 END_FINALLY:
1875 Pops a variable number of entries from the *value* stack
1876 and re-raises the exception they specify. The number of
1877 entries popped depends on the (pseudo) exception type.
1878
1879 The block stack is unwound when an exception is raised:
1880 when a SETUP_FINALLY entry is found, the exception is pushed
1881 onto the value stack (and the exception condition is cleared),
1882 and the interpreter jumps to the label gotten from the block
1883 stack.
1884*/
1885
1886static int
1887compiler_try_finally(struct compiler *c, stmt_ty s)
1888{
1889 basicblock *body, *end;
1890 body = compiler_new_block(c);
1891 end = compiler_new_block(c);
1892 if (body == NULL || end == NULL)
1893 return 0;
1894
1895 ADDOP_JREL(c, SETUP_FINALLY, end);
1896 compiler_use_next_block(c, body);
1897 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1898 return 0;
1899 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1900 ADDOP(c, POP_BLOCK);
1901 compiler_pop_fblock(c, FINALLY_TRY, body);
1902
1903 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1904 compiler_use_next_block(c, end);
1905 if (!compiler_push_fblock(c, FINALLY_END, end))
1906 return 0;
1907 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1908 ADDOP(c, END_FINALLY);
1909 compiler_pop_fblock(c, FINALLY_END, end);
1910
1911 return 1;
1912}
1913
1914/*
1915 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1916 (The contents of the value stack is shown in [], with the top
1917 at the right; 'tb' is trace-back info, 'val' the exception's
1918 associated value, and 'exc' the exception.)
1919
1920 Value stack Label Instruction Argument
1921 [] SETUP_EXCEPT L1
1922 [] <code for S>
1923 [] POP_BLOCK
1924 [] JUMP_FORWARD L0
1925
1926 [tb, val, exc] L1: DUP )
1927 [tb, val, exc, exc] <evaluate E1> )
1928 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1929 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1930 [tb, val, exc, 1] POP )
1931 [tb, val, exc] POP
1932 [tb, val] <assign to V1> (or POP if no V1)
1933 [tb] POP
1934 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001935 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936
1937 [tb, val, exc, 0] L2: POP
1938 [tb, val, exc] DUP
1939 .............................etc.......................
1940
1941 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001942 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943
1944 [] L0: <next statement>
1945
1946 Of course, parts are not generated if Vi or Ei is not present.
1947*/
1948static int
1949compiler_try_except(struct compiler *c, stmt_ty s)
1950{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001951 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 int i, n;
1953
1954 body = compiler_new_block(c);
1955 except = compiler_new_block(c);
1956 orelse = compiler_new_block(c);
1957 end = compiler_new_block(c);
1958 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1959 return 0;
1960 ADDOP_JREL(c, SETUP_EXCEPT, except);
1961 compiler_use_next_block(c, body);
1962 if (!compiler_push_fblock(c, EXCEPT, body))
1963 return 0;
1964 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1965 ADDOP(c, POP_BLOCK);
1966 compiler_pop_fblock(c, EXCEPT, body);
1967 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1968 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1969 compiler_use_next_block(c, except);
1970 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001971 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 s->v.TryExcept.handlers, i);
1973 if (!handler->type && i < n-1)
1974 return compiler_error(c, "default 'except:' must be last");
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001975 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001976 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 except = compiler_new_block(c);
1978 if (except == NULL)
1979 return 0;
1980 if (handler->type) {
1981 ADDOP(c, DUP_TOP);
1982 VISIT(c, expr, handler->type);
1983 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1984 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1985 ADDOP(c, POP_TOP);
1986 }
1987 ADDOP(c, POP_TOP);
1988 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001989 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001990
1991 cleanup_end = compiler_new_block(c);
1992 cleanup_body = compiler_new_block(c);
1993 if(!(cleanup_end || cleanup_body))
1994 return 0;
1995
Guido van Rossum16be03e2007-01-10 18:51:35 +00001996 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001997 ADDOP(c, POP_TOP);
1998
1999 /*
2000 try:
2001 # body
2002 except type as name:
2003 try:
2004 # body
2005 finally:
2006 name = None
2007 del name
2008 */
2009
2010 /* second try: */
2011 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2012 compiler_use_next_block(c, cleanup_body);
2013 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2014 return 0;
2015
2016 /* second # body */
2017 VISIT_SEQ(c, stmt, handler->body);
2018 ADDOP(c, POP_BLOCK);
2019 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2020
2021 /* finally: */
2022 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2023 compiler_use_next_block(c, cleanup_end);
2024 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2025 return 0;
2026
2027 /* name = None */
2028 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00002029 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002030
Guido van Rossum16be03e2007-01-10 18:51:35 +00002031 /* del name */
2032 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002033
2034 ADDOP(c, END_FINALLY);
2035 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 }
2037 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002038 ADDOP(c, POP_TOP);
2039 ADDOP(c, POP_TOP);
2040 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 ADDOP_JREL(c, JUMP_FORWARD, end);
2043 compiler_use_next_block(c, except);
2044 if (handler->type)
2045 ADDOP(c, POP_TOP);
2046 }
2047 ADDOP(c, END_FINALLY);
2048 compiler_use_next_block(c, orelse);
2049 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2050 compiler_use_next_block(c, end);
2051 return 1;
2052}
2053
2054static int
2055compiler_import_as(struct compiler *c, identifier name, identifier asname)
2056{
2057 /* The IMPORT_NAME opcode was already generated. This function
2058 merely needs to bind the result to a name.
2059
2060 If there is a dot in name, we need to split it and emit a
2061 LOAD_ATTR for each name.
2062 */
2063 const char *src = PyString_AS_STRING(name);
2064 const char *dot = strchr(src, '.');
2065 if (dot) {
2066 /* Consume the base module name to get the first attribute */
2067 src = dot + 1;
2068 while (dot) {
2069 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002070 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002072 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002074 if (!attr)
2075 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002077 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 src = dot + 1;
2079 }
2080 }
2081 return compiler_nameop(c, asname, Store);
2082}
2083
2084static int
2085compiler_import(struct compiler *c, stmt_ty s)
2086{
2087 /* The Import node stores a module name like a.b.c as a single
2088 string. This is convenient for all cases except
2089 import a.b.c as d
2090 where we need to parse that string to extract the individual
2091 module names.
2092 XXX Perhaps change the representation to make this case simpler?
2093 */
2094 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002097 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002099 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100
Guido van Rossum45aecf42006-03-15 04:58:47 +00002101 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002102 if (level == NULL)
2103 return 0;
2104
2105 ADDOP_O(c, LOAD_CONST, level, consts);
2106 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2108 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2109
2110 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002111 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002112 if (!r)
2113 return r;
2114 }
2115 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 identifier tmp = alias->name;
2117 const char *base = PyString_AS_STRING(alias->name);
2118 char *dot = strchr(base, '.');
2119 if (dot)
2120 tmp = PyString_FromStringAndSize(base,
2121 dot - base);
2122 r = compiler_nameop(c, tmp, Store);
2123 if (dot) {
2124 Py_DECREF(tmp);
2125 }
2126 if (!r)
2127 return r;
2128 }
2129 }
2130 return 1;
2131}
2132
2133static int
2134compiler_from_import(struct compiler *c, stmt_ty s)
2135{
2136 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137
2138 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002139 PyObject *level;
2140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 if (!names)
2142 return 0;
2143
Guido van Rossum45aecf42006-03-15 04:58:47 +00002144 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002145 if (!level) {
2146 Py_DECREF(names);
2147 return 0;
2148 }
2149
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 /* build up the names */
2151 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002152 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 Py_INCREF(alias->name);
2154 PyTuple_SET_ITEM(names, i, alias->name);
2155 }
2156
2157 if (s->lineno > c->c_future->ff_lineno) {
2158 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2159 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002160 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 Py_DECREF(names);
2162 return compiler_error(c,
2163 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002164 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165
2166 }
2167 }
2168
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002169 ADDOP_O(c, LOAD_CONST, level, consts);
2170 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002172 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2174 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002175 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 identifier store_name;
2177
2178 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2179 assert(n == 1);
2180 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002181 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 }
2183
2184 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2185 store_name = alias->name;
2186 if (alias->asname)
2187 store_name = alias->asname;
2188
2189 if (!compiler_nameop(c, store_name, Store)) {
2190 Py_DECREF(names);
2191 return 0;
2192 }
2193 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002194 /* remove imported module */
2195 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 return 1;
2197}
2198
2199static int
2200compiler_assert(struct compiler *c, stmt_ty s)
2201{
2202 static PyObject *assertion_error = NULL;
2203 basicblock *end;
2204
2205 if (Py_OptimizeFlag)
2206 return 1;
2207 if (assertion_error == NULL) {
2208 assertion_error = PyString_FromString("AssertionError");
2209 if (assertion_error == NULL)
2210 return 0;
2211 }
2212 VISIT(c, expr, s->v.Assert.test);
2213 end = compiler_new_block(c);
2214 if (end == NULL)
2215 return 0;
2216 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2217 ADDOP(c, POP_TOP);
2218 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2219 if (s->v.Assert.msg) {
2220 VISIT(c, expr, s->v.Assert.msg);
2221 ADDOP_I(c, RAISE_VARARGS, 2);
2222 }
2223 else {
2224 ADDOP_I(c, RAISE_VARARGS, 1);
2225 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002226 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227 ADDOP(c, POP_TOP);
2228 return 1;
2229}
2230
2231static int
2232compiler_visit_stmt(struct compiler *c, stmt_ty s)
2233{
2234 int i, n;
2235
Thomas Wouters89f507f2006-12-13 04:49:30 +00002236 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002238 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002241 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002243 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002245 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 if (c->u->u_ste->ste_type != FunctionBlock)
2247 return compiler_error(c, "'return' outside function");
2248 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 VISIT(c, expr, s->v.Return.value);
2250 }
2251 else
2252 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2253 ADDOP(c, RETURN_VALUE);
2254 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002255 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 VISIT_SEQ(c, expr, s->v.Delete.targets)
2257 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002258 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 n = asdl_seq_LEN(s->v.Assign.targets);
2260 VISIT(c, expr, s->v.Assign.value);
2261 for (i = 0; i < n; i++) {
2262 if (i < n - 1)
2263 ADDOP(c, DUP_TOP);
2264 VISIT(c, expr,
2265 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2266 }
2267 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002268 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002270 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002272 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002274 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002276 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 n = 0;
2278 if (s->v.Raise.type) {
2279 VISIT(c, expr, s->v.Raise.type);
2280 n++;
2281 if (s->v.Raise.inst) {
2282 VISIT(c, expr, s->v.Raise.inst);
2283 n++;
2284 if (s->v.Raise.tback) {
2285 VISIT(c, expr, s->v.Raise.tback);
2286 n++;
2287 }
2288 }
2289 }
2290 ADDOP_I(c, RAISE_VARARGS, n);
2291 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002292 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002294 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002296 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002298 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002300 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002302 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002303 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002305 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002307 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 ADDOP(c, PRINT_EXPR);
2309 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002310 else if (s->v.Expr.value->kind != Str_kind &&
2311 s->v.Expr.value->kind != Num_kind) {
2312 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 ADDOP(c, POP_TOP);
2314 }
2315 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002316 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002318 case Break_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002319 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 return compiler_error(c, "'break' outside loop");
2321 ADDOP(c, BREAK_LOOP);
2322 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002323 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002325 case With_kind:
2326 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 }
2328 return 1;
2329}
2330
2331static int
2332unaryop(unaryop_ty op)
2333{
2334 switch (op) {
2335 case Invert:
2336 return UNARY_INVERT;
2337 case Not:
2338 return UNARY_NOT;
2339 case UAdd:
2340 return UNARY_POSITIVE;
2341 case USub:
2342 return UNARY_NEGATIVE;
2343 }
2344 return 0;
2345}
2346
2347static int
2348binop(struct compiler *c, operator_ty op)
2349{
2350 switch (op) {
2351 case Add:
2352 return BINARY_ADD;
2353 case Sub:
2354 return BINARY_SUBTRACT;
2355 case Mult:
2356 return BINARY_MULTIPLY;
2357 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002358 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 case Mod:
2360 return BINARY_MODULO;
2361 case Pow:
2362 return BINARY_POWER;
2363 case LShift:
2364 return BINARY_LSHIFT;
2365 case RShift:
2366 return BINARY_RSHIFT;
2367 case BitOr:
2368 return BINARY_OR;
2369 case BitXor:
2370 return BINARY_XOR;
2371 case BitAnd:
2372 return BINARY_AND;
2373 case FloorDiv:
2374 return BINARY_FLOOR_DIVIDE;
2375 }
2376 return 0;
2377}
2378
2379static int
2380cmpop(cmpop_ty op)
2381{
2382 switch (op) {
2383 case Eq:
2384 return PyCmp_EQ;
2385 case NotEq:
2386 return PyCmp_NE;
2387 case Lt:
2388 return PyCmp_LT;
2389 case LtE:
2390 return PyCmp_LE;
2391 case Gt:
2392 return PyCmp_GT;
2393 case GtE:
2394 return PyCmp_GE;
2395 case Is:
2396 return PyCmp_IS;
2397 case IsNot:
2398 return PyCmp_IS_NOT;
2399 case In:
2400 return PyCmp_IN;
2401 case NotIn:
2402 return PyCmp_NOT_IN;
2403 }
2404 return PyCmp_BAD;
2405}
2406
2407static int
2408inplace_binop(struct compiler *c, operator_ty op)
2409{
2410 switch (op) {
2411 case Add:
2412 return INPLACE_ADD;
2413 case Sub:
2414 return INPLACE_SUBTRACT;
2415 case Mult:
2416 return INPLACE_MULTIPLY;
2417 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002418 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419 case Mod:
2420 return INPLACE_MODULO;
2421 case Pow:
2422 return INPLACE_POWER;
2423 case LShift:
2424 return INPLACE_LSHIFT;
2425 case RShift:
2426 return INPLACE_RSHIFT;
2427 case BitOr:
2428 return INPLACE_OR;
2429 case BitXor:
2430 return INPLACE_XOR;
2431 case BitAnd:
2432 return INPLACE_AND;
2433 case FloorDiv:
2434 return INPLACE_FLOOR_DIVIDE;
2435 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002436 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002437 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 return 0;
2439}
2440
2441static int
2442compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2443{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002444 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2446
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002447 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002448 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 /* XXX AugStore isn't used anywhere! */
2450
2451 /* First check for assignment to __debug__. Param? */
2452 if ((ctx == Store || ctx == AugStore || ctx == Del)
2453 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2454 return compiler_error(c, "can not assign to __debug__");
2455 }
2456
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002457 mangled = _Py_Mangle(c->u->u_private, name);
2458 if (!mangled)
2459 return 0;
2460
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 op = 0;
2462 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002463 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 switch (scope) {
2465 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002466 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 optype = OP_DEREF;
2468 break;
2469 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002470 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 optype = OP_DEREF;
2472 break;
2473 case LOCAL:
2474 if (c->u->u_ste->ste_type == FunctionBlock)
2475 optype = OP_FAST;
2476 break;
2477 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002478 if (c->u->u_ste->ste_type == FunctionBlock &&
2479 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 optype = OP_GLOBAL;
2481 break;
2482 case GLOBAL_EXPLICIT:
2483 optype = OP_GLOBAL;
2484 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002485 default:
2486 /* scope can be 0 */
2487 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 }
2489
2490 /* XXX Leave assert here, but handle __doc__ and the like better */
2491 assert(scope || PyString_AS_STRING(name)[0] == '_');
2492
2493 switch (optype) {
2494 case OP_DEREF:
2495 switch (ctx) {
2496 case Load: op = LOAD_DEREF; break;
2497 case Store: op = STORE_DEREF; break;
2498 case AugLoad:
2499 case AugStore:
2500 break;
2501 case Del:
2502 PyErr_Format(PyExc_SyntaxError,
2503 "can not delete variable '%s' referenced "
2504 "in nested scope",
2505 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002506 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002509 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002510 PyErr_SetString(PyExc_SystemError,
2511 "param invalid for deref variable");
2512 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 }
2514 break;
2515 case OP_FAST:
2516 switch (ctx) {
2517 case Load: op = LOAD_FAST; break;
2518 case Store: op = STORE_FAST; break;
2519 case Del: op = DELETE_FAST; break;
2520 case AugLoad:
2521 case AugStore:
2522 break;
2523 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002524 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002525 PyErr_SetString(PyExc_SystemError,
2526 "param invalid for local variable");
2527 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002529 ADDOP_O(c, op, mangled, varnames);
2530 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 return 1;
2532 case OP_GLOBAL:
2533 switch (ctx) {
2534 case Load: op = LOAD_GLOBAL; break;
2535 case Store: op = STORE_GLOBAL; break;
2536 case Del: op = DELETE_GLOBAL; break;
2537 case AugLoad:
2538 case AugStore:
2539 break;
2540 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002541 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002542 PyErr_SetString(PyExc_SystemError,
2543 "param invalid for global variable");
2544 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 }
2546 break;
2547 case OP_NAME:
2548 switch (ctx) {
2549 case Load: op = LOAD_NAME; break;
2550 case Store: op = STORE_NAME; break;
2551 case Del: op = DELETE_NAME; break;
2552 case AugLoad:
2553 case AugStore:
2554 break;
2555 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002556 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002557 PyErr_SetString(PyExc_SystemError,
2558 "param invalid for name variable");
2559 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 }
2561 break;
2562 }
2563
2564 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002565 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002566 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002567 if (arg < 0)
2568 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002569 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570}
2571
2572static int
2573compiler_boolop(struct compiler *c, expr_ty e)
2574{
2575 basicblock *end;
2576 int jumpi, i, n;
2577 asdl_seq *s;
2578
2579 assert(e->kind == BoolOp_kind);
2580 if (e->v.BoolOp.op == And)
2581 jumpi = JUMP_IF_FALSE;
2582 else
2583 jumpi = JUMP_IF_TRUE;
2584 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002585 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002586 return 0;
2587 s = e->v.BoolOp.values;
2588 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002589 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002590 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002591 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592 ADDOP_JREL(c, jumpi, end);
2593 ADDOP(c, POP_TOP)
2594 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002595 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596 compiler_use_next_block(c, end);
2597 return 1;
2598}
2599
2600static int
2601compiler_list(struct compiler *c, expr_ty e)
2602{
2603 int n = asdl_seq_LEN(e->v.List.elts);
2604 if (e->v.List.ctx == Store) {
2605 ADDOP_I(c, UNPACK_SEQUENCE, n);
2606 }
2607 VISIT_SEQ(c, expr, e->v.List.elts);
2608 if (e->v.List.ctx == Load) {
2609 ADDOP_I(c, BUILD_LIST, n);
2610 }
2611 return 1;
2612}
2613
2614static int
2615compiler_tuple(struct compiler *c, expr_ty e)
2616{
2617 int n = asdl_seq_LEN(e->v.Tuple.elts);
2618 if (e->v.Tuple.ctx == Store) {
2619 ADDOP_I(c, UNPACK_SEQUENCE, n);
2620 }
2621 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2622 if (e->v.Tuple.ctx == Load) {
2623 ADDOP_I(c, BUILD_TUPLE, n);
2624 }
2625 return 1;
2626}
2627
2628static int
2629compiler_compare(struct compiler *c, expr_ty e)
2630{
2631 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002632 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
2634 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2635 VISIT(c, expr, e->v.Compare.left);
2636 n = asdl_seq_LEN(e->v.Compare.ops);
2637 assert(n > 0);
2638 if (n > 1) {
2639 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002640 if (cleanup == NULL)
2641 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002642 VISIT(c, expr,
2643 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 }
2645 for (i = 1; i < n; i++) {
2646 ADDOP(c, DUP_TOP);
2647 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002649 cmpop((cmpop_ty)(asdl_seq_GET(
2650 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2652 NEXT_BLOCK(c);
2653 ADDOP(c, POP_TOP);
2654 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002655 VISIT(c, expr,
2656 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002658 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002660 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 if (n > 1) {
2662 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002663 if (end == NULL)
2664 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 ADDOP_JREL(c, JUMP_FORWARD, end);
2666 compiler_use_next_block(c, cleanup);
2667 ADDOP(c, ROT_TWO);
2668 ADDOP(c, POP_TOP);
2669 compiler_use_next_block(c, end);
2670 }
2671 return 1;
2672}
2673
2674static int
2675compiler_call(struct compiler *c, expr_ty e)
2676{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002678 return compiler_call_helper(c, 0,
2679 e->v.Call.args,
2680 e->v.Call.keywords,
2681 e->v.Call.starargs,
2682 e->v.Call.kwargs);
2683}
2684
2685/* shared code between compiler_call and compiler_class */
2686static int
2687compiler_call_helper(struct compiler *c,
2688 int n, /* Args already pushed */
2689 asdl_seq *args,
2690 asdl_seq *keywords,
2691 expr_ty starargs,
2692 expr_ty kwargs)
2693{
2694 int code = 0;
2695
2696 n += asdl_seq_LEN(args);
2697 VISIT_SEQ(c, expr, args);
2698 if (keywords) {
2699 VISIT_SEQ(c, keyword, keywords);
2700 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002702 if (starargs) {
2703 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 code |= 1;
2705 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002706 if (kwargs) {
2707 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 code |= 2;
2709 }
2710 switch (code) {
2711 case 0:
2712 ADDOP_I(c, CALL_FUNCTION, n);
2713 break;
2714 case 1:
2715 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2716 break;
2717 case 2:
2718 ADDOP_I(c, CALL_FUNCTION_KW, n);
2719 break;
2720 case 3:
2721 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2722 break;
2723 }
2724 return 1;
2725}
2726
2727static int
2728compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002729 asdl_seq *generators, int gen_index,
2730 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731{
2732 /* generate code for the iterator, then each of the ifs,
2733 and then write to the element */
2734
2735 comprehension_ty l;
2736 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002737 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738
2739 start = compiler_new_block(c);
2740 skip = compiler_new_block(c);
2741 if_cleanup = compiler_new_block(c);
2742 anchor = compiler_new_block(c);
2743
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002744 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2745 anchor == NULL)
2746 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002748 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 VISIT(c, expr, l->iter);
2750 ADDOP(c, GET_ITER);
2751 compiler_use_next_block(c, start);
2752 ADDOP_JREL(c, FOR_ITER, anchor);
2753 NEXT_BLOCK(c);
2754 VISIT(c, expr, l->target);
2755
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002756 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 n = asdl_seq_LEN(l->ifs);
2758 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002759 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 VISIT(c, expr, e);
2761 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2762 NEXT_BLOCK(c);
2763 ADDOP(c, POP_TOP);
2764 }
2765
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002766 if (++gen_index < asdl_seq_LEN(generators))
2767 if (!compiler_listcomp_generator(c, tmpname,
2768 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002771 /* only append after the last for generator */
2772 if (gen_index >= asdl_seq_LEN(generators)) {
2773 if (!compiler_nameop(c, tmpname, Load))
2774 return 0;
2775 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002776 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002777
2778 compiler_use_next_block(c, skip);
2779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 for (i = 0; i < n; i++) {
2781 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002782 if (i == 0)
2783 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 ADDOP(c, POP_TOP);
2785 }
2786 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2787 compiler_use_next_block(c, anchor);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002788 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002790 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 return 0;
2792
2793 return 1;
2794}
2795
2796static int
2797compiler_listcomp(struct compiler *c, expr_ty e)
2798{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002799 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002800 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801 asdl_seq *generators = e->v.ListComp.generators;
2802
2803 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002804 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805 if (!tmp)
2806 return 0;
2807 ADDOP_I(c, BUILD_LIST, 0);
2808 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002810 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2811 e->v.ListComp.elt);
2812 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 return rc;
2814}
2815
2816static int
2817compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002818 asdl_seq *generators, int gen_index,
2819 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820{
2821 /* generate code for the iterator, then each of the ifs,
2822 and then write to the element */
2823
2824 comprehension_ty ge;
2825 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002826 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
2828 start = compiler_new_block(c);
2829 skip = compiler_new_block(c);
2830 if_cleanup = compiler_new_block(c);
2831 anchor = compiler_new_block(c);
2832 end = compiler_new_block(c);
2833
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002834 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 anchor == NULL || end == NULL)
2836 return 0;
2837
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002838 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 ADDOP_JREL(c, SETUP_LOOP, end);
2840 if (!compiler_push_fblock(c, LOOP, start))
2841 return 0;
2842
2843 if (gen_index == 0) {
2844 /* Receive outermost iter as an implicit argument */
2845 c->u->u_argcount = 1;
2846 ADDOP_I(c, LOAD_FAST, 0);
2847 }
2848 else {
2849 /* Sub-iter - calculate on the fly */
2850 VISIT(c, expr, ge->iter);
2851 ADDOP(c, GET_ITER);
2852 }
2853 compiler_use_next_block(c, start);
2854 ADDOP_JREL(c, FOR_ITER, anchor);
2855 NEXT_BLOCK(c);
2856 VISIT(c, expr, ge->target);
2857
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002858 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 n = asdl_seq_LEN(ge->ifs);
2860 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002861 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002862 VISIT(c, expr, e);
2863 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2864 NEXT_BLOCK(c);
2865 ADDOP(c, POP_TOP);
2866 }
2867
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002868 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2870 return 0;
2871
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002872 /* only append after the last 'for' generator */
2873 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 VISIT(c, expr, elt);
2875 ADDOP(c, YIELD_VALUE);
2876 ADDOP(c, POP_TOP);
2877
2878 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 for (i = 0; i < n; i++) {
2881 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002882 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 compiler_use_next_block(c, if_cleanup);
2884
2885 ADDOP(c, POP_TOP);
2886 }
2887 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2888 compiler_use_next_block(c, anchor);
2889 ADDOP(c, POP_BLOCK);
2890 compiler_pop_fblock(c, LOOP, start);
2891 compiler_use_next_block(c, end);
2892
2893 return 1;
2894}
2895
2896static int
2897compiler_genexp(struct compiler *c, expr_ty e)
2898{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002899 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 PyCodeObject *co;
2901 expr_ty outermost_iter = ((comprehension_ty)
2902 (asdl_seq_GET(e->v.GeneratorExp.generators,
2903 0)))->iter;
2904
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002905 if (!name) {
2906 name = PyString_FromString("<genexpr>");
2907 if (!name)
2908 return 0;
2909 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910
2911 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2912 return 0;
2913 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2914 e->v.GeneratorExp.elt);
2915 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002916 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002917 if (co == NULL)
2918 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002920 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002921 Py_DECREF(co);
2922
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 VISIT(c, expr, outermost_iter);
2924 ADDOP(c, GET_ITER);
2925 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926
2927 return 1;
2928}
2929
2930static int
2931compiler_visit_keyword(struct compiler *c, keyword_ty k)
2932{
2933 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2934 VISIT(c, expr, k->value);
2935 return 1;
2936}
2937
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002938/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 whether they are true or false.
2940
2941 Return values: 1 for true, 0 for false, -1 for non-constant.
2942 */
2943
2944static int
2945expr_constant(expr_ty e)
2946{
2947 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002948 case Ellipsis_kind:
2949 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950 case Num_kind:
2951 return PyObject_IsTrue(e->v.Num.n);
2952 case Str_kind:
2953 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002954 case Name_kind:
2955 /* __debug__ is not assignable, so we can optimize
2956 * it away in if and while statements */
2957 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2958 "__debug__") == 0)
2959 return ! Py_OptimizeFlag;
2960 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961 default:
2962 return -1;
2963 }
2964}
2965
Guido van Rossumc2e20742006-02-27 22:32:47 +00002966/*
2967 Implements the with statement from PEP 343.
2968
2969 The semantics outlined in that PEP are as follows:
2970
2971 with EXPR as VAR:
2972 BLOCK
2973
2974 It is implemented roughly as:
2975
Thomas Wouters477c8d52006-05-27 19:21:47 +00002976 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002977 exit = context.__exit__ # not calling it
2978 value = context.__enter__()
2979 try:
2980 VAR = value # if VAR present in the syntax
2981 BLOCK
2982 finally:
2983 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002984 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002985 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002986 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002987 exit(*exc)
2988 */
2989static int
2990compiler_with(struct compiler *c, stmt_ty s)
2991{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002992 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002993 basicblock *block, *finally;
2994 identifier tmpexit, tmpvalue = NULL;
2995
2996 assert(s->kind == With_kind);
2997
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002999 enter_attr = PyString_InternFromString("__enter__");
3000 if (!enter_attr)
3001 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003002 }
3003 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003004 exit_attr = PyString_InternFromString("__exit__");
3005 if (!exit_attr)
3006 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003007 }
3008
3009 block = compiler_new_block(c);
3010 finally = compiler_new_block(c);
3011 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003012 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003013
3014 /* Create a temporary variable to hold context.__exit__ */
3015 tmpexit = compiler_new_tmpname(c);
3016 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003017 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003018 PyArena_AddPyObject(c->c_arena, tmpexit);
3019
3020 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003021 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003022 We need to do this rather than preserving it on the stack
3023 because SETUP_FINALLY remembers the stack level.
3024 We need to do the assignment *inside* the try/finally
3025 so that context.__exit__() is called when the assignment
3026 fails. But we need to call context.__enter__() *before*
3027 the try/finally so that if it fails we won't call
3028 context.__exit__().
3029 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003031 if (tmpvalue == NULL)
3032 return 0;
3033 PyArena_AddPyObject(c->c_arena, tmpvalue);
3034 }
3035
Thomas Wouters477c8d52006-05-27 19:21:47 +00003036 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003037 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003038
3039 /* Squirrel away context.__exit__ */
3040 ADDOP(c, DUP_TOP);
3041 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3042 if (!compiler_nameop(c, tmpexit, Store))
3043 return 0;
3044
3045 /* Call context.__enter__() */
3046 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3047 ADDOP_I(c, CALL_FUNCTION, 0);
3048
3049 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003050 /* Store it in tmpvalue */
3051 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003052 return 0;
3053 }
3054 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003055 /* Discard result from context.__enter__() */
3056 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003057 }
3058
3059 /* Start the try block */
3060 ADDOP_JREL(c, SETUP_FINALLY, finally);
3061
3062 compiler_use_next_block(c, block);
3063 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003064 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065 }
3066
3067 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003068 /* Bind saved result of context.__enter__() to VAR */
3069 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003070 !compiler_nameop(c, tmpvalue, Del))
3071 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003072 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 }
3074
3075 /* BLOCK code */
3076 VISIT_SEQ(c, stmt, s->v.With.body);
3077
3078 /* End of try block; start the finally block */
3079 ADDOP(c, POP_BLOCK);
3080 compiler_pop_fblock(c, FINALLY_TRY, block);
3081
3082 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3083 compiler_use_next_block(c, finally);
3084 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003085 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003086
3087 /* Finally block starts; push tmpexit and issue our magic opcode. */
3088 if (!compiler_nameop(c, tmpexit, Load) ||
3089 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003090 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003091 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003092
3093 /* Finally block ends. */
3094 ADDOP(c, END_FINALLY);
3095 compiler_pop_fblock(c, FINALLY_END, finally);
3096 return 1;
3097}
3098
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003099static int
3100compiler_visit_expr(struct compiler *c, expr_ty e)
3101{
3102 int i, n;
3103
Thomas Wouters89f507f2006-12-13 04:49:30 +00003104 /* If expr e has a different line number than the last expr/stmt,
3105 set a new line number for the next instruction.
3106 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 if (e->lineno > c->u->u_lineno) {
3108 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003109 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 }
3111 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003112 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003114 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003115 VISIT(c, expr, e->v.BinOp.left);
3116 VISIT(c, expr, e->v.BinOp.right);
3117 ADDOP(c, binop(c, e->v.BinOp.op));
3118 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003119 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 VISIT(c, expr, e->v.UnaryOp.operand);
3121 ADDOP(c, unaryop(e->v.UnaryOp.op));
3122 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003123 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003125 case IfExp_kind:
3126 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003127 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 /* XXX get rid of arg? */
3129 ADDOP_I(c, BUILD_MAP, 0);
3130 n = asdl_seq_LEN(e->v.Dict.values);
3131 /* We must arrange things just right for STORE_SUBSCR.
3132 It wants the stack to look like (value) (dict) (key) */
3133 for (i = 0; i < n; i++) {
3134 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003135 VISIT(c, expr,
3136 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138 VISIT(c, expr,
3139 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 ADDOP(c, STORE_SUBSCR);
3141 }
3142 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003143 case Set_kind:
3144 n = asdl_seq_LEN(e->v.Set.elts);
3145 VISIT_SEQ(c, expr, e->v.Set.elts);
3146 ADDOP_I(c, BUILD_SET, n);
3147 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003148 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003150 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 return compiler_genexp(c, e);
3152 case Yield_kind:
3153 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003154 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 if (e->v.Yield.value) {
3156 VISIT(c, expr, e->v.Yield.value);
3157 }
3158 else {
3159 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3160 }
3161 ADDOP(c, YIELD_VALUE);
3162 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003163 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003165 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003167 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3169 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003170 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3172 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003173 case Bytes_kind:
3174 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3175 ADDOP(c, MAKE_BYTES);
3176 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003177 case Ellipsis_kind:
3178 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3179 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003181 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 if (e->v.Attribute.ctx != AugStore)
3183 VISIT(c, expr, e->v.Attribute.value);
3184 switch (e->v.Attribute.ctx) {
3185 case AugLoad:
3186 ADDOP(c, DUP_TOP);
3187 /* Fall through to load */
3188 case Load:
3189 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3190 break;
3191 case AugStore:
3192 ADDOP(c, ROT_TWO);
3193 /* Fall through to save */
3194 case Store:
3195 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3196 break;
3197 case Del:
3198 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3199 break;
3200 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003201 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003202 PyErr_SetString(PyExc_SystemError,
3203 "param invalid in attribute expression");
3204 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 }
3206 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003207 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 switch (e->v.Subscript.ctx) {
3209 case AugLoad:
3210 VISIT(c, expr, e->v.Subscript.value);
3211 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3212 break;
3213 case Load:
3214 VISIT(c, expr, e->v.Subscript.value);
3215 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3216 break;
3217 case AugStore:
3218 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3219 break;
3220 case Store:
3221 VISIT(c, expr, e->v.Subscript.value);
3222 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3223 break;
3224 case Del:
3225 VISIT(c, expr, e->v.Subscript.value);
3226 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3227 break;
3228 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003229 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003230 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003231 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003232 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 }
3234 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003235 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3237 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003238 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003239 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003240 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 return compiler_tuple(c, e);
3242 }
3243 return 1;
3244}
3245
3246static int
3247compiler_augassign(struct compiler *c, stmt_ty s)
3248{
3249 expr_ty e = s->v.AugAssign.target;
3250 expr_ty auge;
3251
3252 assert(s->kind == AugAssign_kind);
3253
3254 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003255 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003257 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003258 if (auge == NULL)
3259 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 VISIT(c, expr, auge);
3261 VISIT(c, expr, s->v.AugAssign.value);
3262 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3263 auge->v.Attribute.ctx = AugStore;
3264 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265 break;
3266 case Subscript_kind:
3267 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003268 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003269 if (auge == NULL)
3270 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 VISIT(c, expr, auge);
3272 VISIT(c, expr, s->v.AugAssign.value);
3273 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003274 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003276 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003278 if (!compiler_nameop(c, e->v.Name.id, Load))
3279 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 VISIT(c, expr, s->v.AugAssign.value);
3281 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3282 return compiler_nameop(c, e->v.Name.id, Store);
3283 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003284 PyErr_Format(PyExc_SystemError,
3285 "invalid node type (%d) for augmented assignment",
3286 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003287 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 }
3289 return 1;
3290}
3291
3292static int
3293compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3294{
3295 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003296 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3297 PyErr_SetString(PyExc_SystemError,
3298 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 f = &c->u->u_fblock[c->u->u_nfblocks++];
3302 f->fb_type = t;
3303 f->fb_block = b;
3304 return 1;
3305}
3306
3307static void
3308compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3309{
3310 struct compiler_unit *u = c->u;
3311 assert(u->u_nfblocks > 0);
3312 u->u_nfblocks--;
3313 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3314 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3315}
3316
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317static int
3318compiler_in_loop(struct compiler *c) {
3319 int i;
3320 struct compiler_unit *u = c->u;
3321 for (i = 0; i < u->u_nfblocks; ++i) {
3322 if (u->u_fblock[i].fb_type == LOOP)
3323 return 1;
3324 }
3325 return 0;
3326}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327/* Raises a SyntaxError and returns 0.
3328 If something goes wrong, a different exception may be raised.
3329*/
3330
3331static int
3332compiler_error(struct compiler *c, const char *errstr)
3333{
3334 PyObject *loc;
3335 PyObject *u = NULL, *v = NULL;
3336
3337 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3338 if (!loc) {
3339 Py_INCREF(Py_None);
3340 loc = Py_None;
3341 }
3342 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3343 Py_None, loc);
3344 if (!u)
3345 goto exit;
3346 v = Py_BuildValue("(zO)", errstr, u);
3347 if (!v)
3348 goto exit;
3349 PyErr_SetObject(PyExc_SyntaxError, v);
3350 exit:
3351 Py_DECREF(loc);
3352 Py_XDECREF(u);
3353 Py_XDECREF(v);
3354 return 0;
3355}
3356
3357static int
3358compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003359 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003361 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003363 /* XXX this code is duplicated */
3364 switch (ctx) {
3365 case AugLoad: /* fall through to Load */
3366 case Load: op = BINARY_SUBSCR; break;
3367 case AugStore:/* fall through to Store */
3368 case Store: op = STORE_SUBSCR; break;
3369 case Del: op = DELETE_SUBSCR; break;
3370 case Param:
3371 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003372 "invalid %s kind %d in subscript\n",
3373 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003374 return 0;
3375 }
3376 if (ctx == AugLoad) {
3377 ADDOP_I(c, DUP_TOPX, 2);
3378 }
3379 else if (ctx == AugStore) {
3380 ADDOP(c, ROT_THREE);
3381 }
3382 ADDOP(c, op);
3383 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384}
3385
3386static int
3387compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3388{
3389 int n = 2;
3390 assert(s->kind == Slice_kind);
3391
3392 /* only handles the cases where BUILD_SLICE is emitted */
3393 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003394 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395 }
3396 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003397 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003399
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003401 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 }
3403 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003404 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 }
3406
3407 if (s->v.Slice.step) {
3408 n++;
3409 VISIT(c, expr, s->v.Slice.step);
3410 }
3411 ADDOP_I(c, BUILD_SLICE, n);
3412 return 1;
3413}
3414
3415static int
3416compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3417{
3418 int op = 0, slice_offset = 0, stack_count = 0;
3419
3420 assert(s->v.Slice.step == NULL);
3421 if (s->v.Slice.lower) {
3422 slice_offset++;
3423 stack_count++;
3424 if (ctx != AugStore)
3425 VISIT(c, expr, s->v.Slice.lower);
3426 }
3427 if (s->v.Slice.upper) {
3428 slice_offset += 2;
3429 stack_count++;
3430 if (ctx != AugStore)
3431 VISIT(c, expr, s->v.Slice.upper);
3432 }
3433
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003434 if (ctx == AugLoad) {
3435 switch (stack_count) {
3436 case 0: ADDOP(c, DUP_TOP); break;
3437 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3438 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3439 }
3440 }
3441 else if (ctx == AugStore) {
3442 switch (stack_count) {
3443 case 0: ADDOP(c, ROT_TWO); break;
3444 case 1: ADDOP(c, ROT_THREE); break;
3445 case 2: ADDOP(c, ROT_FOUR); break;
3446 }
3447 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448
3449 switch (ctx) {
3450 case AugLoad: /* fall through to Load */
3451 case Load: op = SLICE; break;
3452 case AugStore:/* fall through to Store */
3453 case Store: op = STORE_SLICE; break;
3454 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003455 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003456 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003457 PyErr_SetString(PyExc_SystemError,
3458 "param invalid in simple slice");
3459 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 }
3461
3462 ADDOP(c, op + slice_offset);
3463 return 1;
3464}
3465
3466static int
3467compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3468 expr_context_ty ctx)
3469{
3470 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 case Slice_kind:
3472 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 case Index_kind:
3474 VISIT(c, expr, s->v.Index.value);
3475 break;
3476 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003477 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003478 PyErr_SetString(PyExc_SystemError,
3479 "extended slice invalid in nested slice");
3480 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 }
3482 return 1;
3483}
3484
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485static int
3486compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3487{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003488 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003490 case Index_kind:
3491 kindname = "index";
3492 if (ctx != AugStore) {
3493 VISIT(c, expr, s->v.Index.value);
3494 }
3495 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003497 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 if (!s->v.Slice.step)
3499 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003500 if (ctx != AugStore) {
3501 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 return 0;
3503 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003504 break;
3505 case ExtSlice_kind:
3506 kindname = "extended slice";
3507 if (ctx != AugStore) {
3508 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3509 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003510 slice_ty sub = (slice_ty)asdl_seq_GET(
3511 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003512 if (!compiler_visit_nested_slice(c, sub, ctx))
3513 return 0;
3514 }
3515 ADDOP_I(c, BUILD_TUPLE, n);
3516 }
3517 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003518 default:
3519 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003520 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003521 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003523 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
Thomas Wouters89f507f2006-12-13 04:49:30 +00003526/* End of the compiler section, beginning of the assembler section */
3527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528/* do depth-first search of basic block graph, starting with block.
3529 post records the block indices in post-order.
3530
3531 XXX must handle implicit jumps from one block to next
3532*/
3533
Thomas Wouters89f507f2006-12-13 04:49:30 +00003534struct assembler {
3535 PyObject *a_bytecode; /* string containing bytecode */
3536 int a_offset; /* offset into bytecode */
3537 int a_nblocks; /* number of reachable blocks */
3538 basicblock **a_postorder; /* list of blocks in dfs postorder */
3539 PyObject *a_lnotab; /* string containing lnotab */
3540 int a_lnotab_off; /* offset into lnotab */
3541 int a_lineno; /* last lineno of emitted instruction */
3542 int a_lineno_off; /* bytecode offset of last lineno */
3543};
3544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545static void
3546dfs(struct compiler *c, basicblock *b, struct assembler *a)
3547{
3548 int i;
3549 struct instr *instr = NULL;
3550
3551 if (b->b_seen)
3552 return;
3553 b->b_seen = 1;
3554 if (b->b_next != NULL)
3555 dfs(c, b->b_next, a);
3556 for (i = 0; i < b->b_iused; i++) {
3557 instr = &b->b_instr[i];
3558 if (instr->i_jrel || instr->i_jabs)
3559 dfs(c, instr->i_target, a);
3560 }
3561 a->a_postorder[a->a_nblocks++] = b;
3562}
3563
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003564static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3566{
3567 int i;
3568 struct instr *instr;
3569 if (b->b_seen || b->b_startdepth >= depth)
3570 return maxdepth;
3571 b->b_seen = 1;
3572 b->b_startdepth = depth;
3573 for (i = 0; i < b->b_iused; i++) {
3574 instr = &b->b_instr[i];
3575 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3576 if (depth > maxdepth)
3577 maxdepth = depth;
3578 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3579 if (instr->i_jrel || instr->i_jabs) {
3580 maxdepth = stackdepth_walk(c, instr->i_target,
3581 depth, maxdepth);
3582 if (instr->i_opcode == JUMP_ABSOLUTE ||
3583 instr->i_opcode == JUMP_FORWARD) {
3584 goto out; /* remaining code is dead */
3585 }
3586 }
3587 }
3588 if (b->b_next)
3589 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3590out:
3591 b->b_seen = 0;
3592 return maxdepth;
3593}
3594
3595/* Find the flow path that needs the largest stack. We assume that
3596 * cycles in the flow graph have no net effect on the stack depth.
3597 */
3598static int
3599stackdepth(struct compiler *c)
3600{
3601 basicblock *b, *entryblock;
3602 entryblock = NULL;
3603 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3604 b->b_seen = 0;
3605 b->b_startdepth = INT_MIN;
3606 entryblock = b;
3607 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003608 if (!entryblock)
3609 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 return stackdepth_walk(c, entryblock, 0, 0);
3611}
3612
3613static int
3614assemble_init(struct assembler *a, int nblocks, int firstlineno)
3615{
3616 memset(a, 0, sizeof(struct assembler));
3617 a->a_lineno = firstlineno;
3618 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3619 if (!a->a_bytecode)
3620 return 0;
3621 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3622 if (!a->a_lnotab)
3623 return 0;
3624 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003625 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003626 if (!a->a_postorder) {
3627 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 return 1;
3631}
3632
3633static void
3634assemble_free(struct assembler *a)
3635{
3636 Py_XDECREF(a->a_bytecode);
3637 Py_XDECREF(a->a_lnotab);
3638 if (a->a_postorder)
3639 PyObject_Free(a->a_postorder);
3640}
3641
3642/* Return the size of a basic block in bytes. */
3643
3644static int
3645instrsize(struct instr *instr)
3646{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003647 if (!instr->i_hasarg)
3648 return 1;
3649 if (instr->i_oparg > 0xffff)
3650 return 6;
3651 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652}
3653
3654static int
3655blocksize(basicblock *b)
3656{
3657 int i;
3658 int size = 0;
3659
3660 for (i = 0; i < b->b_iused; i++)
3661 size += instrsize(&b->b_instr[i]);
3662 return size;
3663}
3664
3665/* All about a_lnotab.
3666
3667c_lnotab is an array of unsigned bytes disguised as a Python string.
3668It is used to map bytecode offsets to source code line #s (when needed
3669for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003670
Tim Peters2a7f3842001-06-09 09:26:21 +00003671The array is conceptually a list of
3672 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003673pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003674
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003675 byte code offset source code line number
3676 0 1
3677 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003678 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003679 350 307
3680 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003681
3682The first trick is that these numbers aren't stored, only the increments
3683from one row to the next (this doesn't really work, but it's a start):
3684
3685 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3686
3687The second trick is that an unsigned byte can't hold negative values, or
3688values larger than 255, so (a) there's a deep assumption that byte code
3689offsets and their corresponding line #s both increase monotonically, and (b)
3690if at least one column jumps by more than 255 from one row to the next, more
3691than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003692from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003693part. A user of c_lnotab desiring to find the source line number
3694corresponding to a bytecode address A should do something like this
3695
3696 lineno = addr = 0
3697 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003698 addr += addr_incr
3699 if addr > A:
3700 return lineno
3701 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003702
3703In order for this to work, when the addr field increments by more than 255,
3704the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003705increment is < 256. So, in the example above, assemble_lnotab (it used
3706to be called com_set_lineno) should not (as was actually done until 2.2)
3707expand 300, 300 to 255, 255, 45, 45,
3708 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003709*/
3710
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003711static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003713{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 int d_bytecode, d_lineno;
3715 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003716 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717
3718 d_bytecode = a->a_offset - a->a_lineno_off;
3719 d_lineno = i->i_lineno - a->a_lineno;
3720
3721 assert(d_bytecode >= 0);
3722 assert(d_lineno >= 0);
3723
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003724 /* XXX(nnorwitz): is there a better way to handle this?
3725 for loops are special, we want to be able to trace them
3726 each time around, so we need to set an extra line number. */
3727 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003728 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003729
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003731 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 nbytes = a->a_lnotab_off + 2 * ncodes;
3733 len = PyString_GET_SIZE(a->a_lnotab);
3734 if (nbytes >= len) {
3735 if (len * 2 < nbytes)
3736 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003737 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 len *= 2;
3739 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3740 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003741 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003742 lnotab = (unsigned char *)
3743 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003744 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 *lnotab++ = 255;
3746 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 d_bytecode -= ncodes * 255;
3749 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 assert(d_bytecode <= 255);
3752 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003753 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 nbytes = a->a_lnotab_off + 2 * ncodes;
3755 len = PyString_GET_SIZE(a->a_lnotab);
3756 if (nbytes >= len) {
3757 if (len * 2 < nbytes)
3758 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003759 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 len *= 2;
3761 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3762 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003763 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003764 lnotab = (unsigned char *)
3765 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003767 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003769 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003771 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 d_lineno -= ncodes * 255;
3774 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003775 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003776
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 len = PyString_GET_SIZE(a->a_lnotab);
3778 if (a->a_lnotab_off + 2 >= len) {
3779 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003780 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003781 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003782 lnotab = (unsigned char *)
3783 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003784
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 a->a_lnotab_off += 2;
3786 if (d_bytecode) {
3787 *lnotab++ = d_bytecode;
3788 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003789 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003790 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 *lnotab++ = 0;
3792 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003793 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 a->a_lineno = i->i_lineno;
3795 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003796 return 1;
3797}
3798
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799/* assemble_emit()
3800 Extend the bytecode with a new instruction.
3801 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003802*/
3803
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003804static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003806{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003807 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003808 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 char *code;
3810
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003811 size = instrsize(i);
3812 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003814 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003817 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 if (a->a_offset + size >= len) {
3819 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003820 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003821 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3823 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003824 if (size == 6) {
3825 assert(i->i_hasarg);
3826 *code++ = (char)EXTENDED_ARG;
3827 *code++ = ext & 0xff;
3828 *code++ = ext >> 8;
3829 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003832 if (i->i_hasarg) {
3833 assert(size == 3 || size == 6);
3834 *code++ = arg & 0xff;
3835 *code++ = arg >> 8;
3836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003838}
3839
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003840static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003842{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003844 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003845 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847 /* Compute the size of each block and fixup jump args.
3848 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003849start:
3850 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003852 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 bsize = blocksize(b);
3854 b->b_offset = totsize;
3855 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003856 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003857 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3859 bsize = b->b_offset;
3860 for (i = 0; i < b->b_iused; i++) {
3861 struct instr *instr = &b->b_instr[i];
3862 /* Relative jumps are computed relative to
3863 the instruction pointer after fetching
3864 the jump instruction.
3865 */
3866 bsize += instrsize(instr);
3867 if (instr->i_jabs)
3868 instr->i_oparg = instr->i_target->b_offset;
3869 else if (instr->i_jrel) {
3870 int delta = instr->i_target->b_offset - bsize;
3871 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003872 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003873 else
3874 continue;
3875 if (instr->i_oparg > 0xffff)
3876 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003877 }
3878 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003879
3880 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003881 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003882 with a better solution.
3883
3884 In the meantime, should the goto be dropped in favor
3885 of a loop?
3886
3887 The issue is that in the first loop blocksize() is called
3888 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003889 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003890 i_oparg is calculated in the second loop above.
3891
3892 So we loop until we stop seeing new EXTENDED_ARGs.
3893 The only EXTENDED_ARGs that could be popping up are
3894 ones in jump instructions. So this should converge
3895 fairly quickly.
3896 */
3897 if (last_extended_arg_count != extended_arg_count) {
3898 last_extended_arg_count = extended_arg_count;
3899 goto start;
3900 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003901}
3902
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003903static PyObject *
3904dict_keys_inorder(PyObject *dict, int offset)
3905{
3906 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003907 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003908
3909 tuple = PyTuple_New(size);
3910 if (tuple == NULL)
3911 return NULL;
3912 while (PyDict_Next(dict, &pos, &k, &v)) {
3913 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003914 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003915 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003916 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003917 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003918 PyTuple_SET_ITEM(tuple, i - offset, k);
3919 }
3920 return tuple;
3921}
3922
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003923static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003925{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926 PySTEntryObject *ste = c->u->u_ste;
3927 int flags = 0, n;
3928 if (ste->ste_type != ModuleBlock)
3929 flags |= CO_NEWLOCALS;
3930 if (ste->ste_type == FunctionBlock) {
3931 if (!ste->ste_unoptimized)
3932 flags |= CO_OPTIMIZED;
3933 if (ste->ste_nested)
3934 flags |= CO_NESTED;
3935 if (ste->ste_generator)
3936 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003937 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938 if (ste->ste_varargs)
3939 flags |= CO_VARARGS;
3940 if (ste->ste_varkeywords)
3941 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003942 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003944
3945 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003946 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948 n = PyDict_Size(c->u->u_freevars);
3949 if (n < 0)
3950 return -1;
3951 if (n == 0) {
3952 n = PyDict_Size(c->u->u_cellvars);
3953 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003954 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 if (n == 0) {
3956 flags |= CO_NOFREE;
3957 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003958 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003959
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003960 return flags;
3961}
3962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963static PyCodeObject *
3964makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003965{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966 PyObject *tmp;
3967 PyCodeObject *co = NULL;
3968 PyObject *consts = NULL;
3969 PyObject *names = NULL;
3970 PyObject *varnames = NULL;
3971 PyObject *filename = NULL;
3972 PyObject *name = NULL;
3973 PyObject *freevars = NULL;
3974 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003975 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 tmp = dict_keys_inorder(c->u->u_consts, 0);
3979 if (!tmp)
3980 goto error;
3981 consts = PySequence_List(tmp); /* optimize_code requires a list */
3982 Py_DECREF(tmp);
3983
3984 names = dict_keys_inorder(c->u->u_names, 0);
3985 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3986 if (!consts || !names || !varnames)
3987 goto error;
3988
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003989 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3990 if (!cellvars)
3991 goto error;
3992 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3993 if (!freevars)
3994 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 filename = PyString_FromString(c->c_filename);
3996 if (!filename)
3997 goto error;
3998
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003999 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004000 flags = compute_code_flags(c);
4001 if (flags < 0)
4002 goto error;
4003
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004004 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004005 if (!bytecode)
4006 goto error;
4007
4008 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4009 if (!tmp)
4010 goto error;
4011 Py_DECREF(consts);
4012 consts = tmp;
4013
Guido van Rossum4f72a782006-10-27 23:31:49 +00004014 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4015 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 bytecode, consts, names, varnames,
4017 freevars, cellvars,
4018 filename, c->u->u_name,
4019 c->u->u_firstlineno,
4020 a->a_lnotab);
4021 error:
4022 Py_XDECREF(consts);
4023 Py_XDECREF(names);
4024 Py_XDECREF(varnames);
4025 Py_XDECREF(filename);
4026 Py_XDECREF(name);
4027 Py_XDECREF(freevars);
4028 Py_XDECREF(cellvars);
4029 Py_XDECREF(bytecode);
4030 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004031}
4032
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004033
4034/* For debugging purposes only */
4035#if 0
4036static void
4037dump_instr(const struct instr *i)
4038{
4039 const char *jrel = i->i_jrel ? "jrel " : "";
4040 const char *jabs = i->i_jabs ? "jabs " : "";
4041 char arg[128];
4042
4043 *arg = '\0';
4044 if (i->i_hasarg)
4045 sprintf(arg, "arg: %d ", i->i_oparg);
4046
4047 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4048 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4049}
4050
4051static void
4052dump_basicblock(const basicblock *b)
4053{
4054 const char *seen = b->b_seen ? "seen " : "";
4055 const char *b_return = b->b_return ? "return " : "";
4056 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4057 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4058 if (b->b_instr) {
4059 int i;
4060 for (i = 0; i < b->b_iused; i++) {
4061 fprintf(stderr, " [%02d] ", i);
4062 dump_instr(b->b_instr + i);
4063 }
4064 }
4065}
4066#endif
4067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068static PyCodeObject *
4069assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004070{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 basicblock *b, *entryblock;
4072 struct assembler a;
4073 int i, j, nblocks;
4074 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004076 /* Make sure every block that falls off the end returns None.
4077 XXX NEXT_BLOCK() isn't quite right, because if the last
4078 block ends with a jump or return b_next shouldn't set.
4079 */
4080 if (!c->u->u_curblock->b_return) {
4081 NEXT_BLOCK(c);
4082 if (addNone)
4083 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4084 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004085 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004087 nblocks = 0;
4088 entryblock = NULL;
4089 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4090 nblocks++;
4091 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004092 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004093
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004094 /* Set firstlineno if it wasn't explicitly set. */
4095 if (!c->u->u_firstlineno) {
4096 if (entryblock && entryblock->b_instr)
4097 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4098 else
4099 c->u->u_firstlineno = 1;
4100 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004101 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4102 goto error;
4103 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004105 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004106 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108 /* Emit code in reverse postorder from dfs. */
4109 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004110 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111 for (j = 0; j < b->b_iused; j++)
4112 if (!assemble_emit(&a, &b->b_instr[j]))
4113 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004114 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4117 goto error;
4118 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4119 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004121 co = makecode(c, &a);
4122 error:
4123 assemble_free(&a);
4124 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004125}