blob: 4c22441026eff9a8545f6435b4c7b19e696f476e [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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001523 /* initialize statics */
1524 if (build_class == NULL) {
1525 build_class = PyString_FromString("__build_class__");
1526 if (build_class == NULL)
1527 return 0;
1528 }
1529 if (locals == NULL) {
1530 locals = PyString_FromString("__locals__");
1531 if (locals == NULL)
1532 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001533 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001535 /* ultimately generate code for:
1536 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1537 where:
1538 <func> is a function/closure created from the class body
1539 <name> is the class name
1540 <bases> is the positional arguments and *varargs argument
1541 <keywords> is the keyword arguments and **kwds argument
1542 This borrows from compiler_call.
1543 */
1544
1545 /* 0. Create a fake variable named __locals__ */
1546 ste = PySymtable_Lookup(c->c_st, s);
1547 if (ste == NULL)
1548 return 0;
1549 assert(PyList_Check(ste->ste_varnames));
1550 if (PyList_Append(ste->ste_varnames, locals) < 0)
1551 return 0;
1552
1553 /* 1. compile the class body into a code object */
1554 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1555 return 0;
1556 /* this block represents what we do in the new scope */
1557 {
1558 /* use the class name for name mangling */
1559 Py_INCREF(s->v.ClassDef.name);
1560 c->u->u_private = s->v.ClassDef.name;
1561 /* force it to have one mandatory argument */
1562 c->u->u_argcount = 1;
1563 /* load the first argument ... */
1564 ADDOP_I(c, LOAD_FAST, 0);
1565 /* ... and store it into f_locals */
1566 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1567 /* load __name__ ... */
1568 str = PyString_InternFromString("__name__");
1569 if (!str || !compiler_nameop(c, str, Load)) {
1570 Py_XDECREF(str);
1571 compiler_exit_scope(c);
1572 return 0;
1573 }
1574 Py_DECREF(str);
1575 /* ... and store it as __module__ */
1576 str = PyString_InternFromString("__module__");
1577 if (!str || !compiler_nameop(c, str, Store)) {
1578 Py_XDECREF(str);
1579 compiler_exit_scope(c);
1580 return 0;
1581 }
1582 Py_DECREF(str);
1583 /* compile the body proper */
1584 if (!compiler_body(c, s->v.ClassDef.body)) {
1585 compiler_exit_scope(c);
1586 return 0;
1587 }
1588 /* return None */
1589 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1590 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1591 /* create the code object */
1592 co = assemble(c, 1);
1593 }
1594 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001595 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 if (co == NULL)
1597 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001599 /* 2. load the 'build_class' function */
1600 ADDOP(c, LOAD_BUILD_CLASS);
1601
1602 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001603 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001604 Py_DECREF(co);
1605
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001606 /* 4. load class name */
1607 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1608
1609 /* 5. generate the rest of the code for the call */
1610 if (!compiler_call_helper(c, 2,
1611 s->v.ClassDef.bases,
1612 s->v.ClassDef.keywords,
1613 s->v.ClassDef.starargs,
1614 s->v.ClassDef.kwargs))
1615 return 0;
1616
1617 /* 6. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001618 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1619 return 0;
1620 return 1;
1621}
1622
1623static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001624compiler_ifexp(struct compiler *c, expr_ty e)
1625{
1626 basicblock *end, *next;
1627
1628 assert(e->kind == IfExp_kind);
1629 end = compiler_new_block(c);
1630 if (end == NULL)
1631 return 0;
1632 next = compiler_new_block(c);
1633 if (next == NULL)
1634 return 0;
1635 VISIT(c, expr, e->v.IfExp.test);
1636 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1637 ADDOP(c, POP_TOP);
1638 VISIT(c, expr, e->v.IfExp.body);
1639 ADDOP_JREL(c, JUMP_FORWARD, end);
1640 compiler_use_next_block(c, next);
1641 ADDOP(c, POP_TOP);
1642 VISIT(c, expr, e->v.IfExp.orelse);
1643 compiler_use_next_block(c, end);
1644 return 1;
1645}
1646
1647static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648compiler_lambda(struct compiler *c, expr_ty e)
1649{
1650 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001651 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001652 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 arguments_ty args = e->v.Lambda.args;
1654 assert(e->kind == Lambda_kind);
1655
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001656 if (!name) {
1657 name = PyString_InternFromString("<lambda>");
1658 if (!name)
1659 return 0;
1660 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661
Guido van Rossum4f72a782006-10-27 23:31:49 +00001662 if (args->kwonlyargs) {
1663 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1664 args->kw_defaults);
1665 if (res < 0) return 0;
1666 kw_default_count = res;
1667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 if (args->defaults)
1669 VISIT_SEQ(c, expr, args->defaults);
1670 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1671 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001672
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001673 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674 compiler_arguments(c, args);
1675
1676 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001677 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001678 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1679 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001681 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 if (co == NULL)
1683 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684
Guido van Rossum4f72a782006-10-27 23:31:49 +00001685 arglength = asdl_seq_LEN(args->defaults);
1686 arglength |= kw_default_count << 8;
1687 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001688 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689
1690 return 1;
1691}
1692
1693static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694compiler_if(struct compiler *c, stmt_ty s)
1695{
1696 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001697 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 assert(s->kind == If_kind);
1699 end = compiler_new_block(c);
1700 if (end == NULL)
1701 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001702 next = compiler_new_block(c);
1703 if (next == NULL)
1704 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001705
1706 constant = expr_constant(s->v.If.test);
1707 /* constant = 0: "if 0"
1708 * constant = 1: "if 1", "if 2", ...
1709 * constant = -1: rest */
1710 if (constant == 0) {
1711 if (s->v.If.orelse)
1712 VISIT_SEQ(c, stmt, s->v.If.orelse);
1713 } else if (constant == 1) {
1714 VISIT_SEQ(c, stmt, s->v.If.body);
1715 } else {
1716 VISIT(c, expr, s->v.If.test);
1717 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1718 ADDOP(c, POP_TOP);
1719 VISIT_SEQ(c, stmt, s->v.If.body);
1720 ADDOP_JREL(c, JUMP_FORWARD, end);
1721 compiler_use_next_block(c, next);
1722 ADDOP(c, POP_TOP);
1723 if (s->v.If.orelse)
1724 VISIT_SEQ(c, stmt, s->v.If.orelse);
1725 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 compiler_use_next_block(c, end);
1727 return 1;
1728}
1729
1730static int
1731compiler_for(struct compiler *c, stmt_ty s)
1732{
1733 basicblock *start, *cleanup, *end;
1734
1735 start = compiler_new_block(c);
1736 cleanup = compiler_new_block(c);
1737 end = compiler_new_block(c);
1738 if (start == NULL || end == NULL || cleanup == NULL)
1739 return 0;
1740 ADDOP_JREL(c, SETUP_LOOP, end);
1741 if (!compiler_push_fblock(c, LOOP, start))
1742 return 0;
1743 VISIT(c, expr, s->v.For.iter);
1744 ADDOP(c, GET_ITER);
1745 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001746 /* XXX(nnorwitz): is there a better way to handle this?
1747 for loops are special, we want to be able to trace them
1748 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001749 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750 ADDOP_JREL(c, FOR_ITER, cleanup);
1751 VISIT(c, expr, s->v.For.target);
1752 VISIT_SEQ(c, stmt, s->v.For.body);
1753 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1754 compiler_use_next_block(c, cleanup);
1755 ADDOP(c, POP_BLOCK);
1756 compiler_pop_fblock(c, LOOP, start);
1757 VISIT_SEQ(c, stmt, s->v.For.orelse);
1758 compiler_use_next_block(c, end);
1759 return 1;
1760}
1761
1762static int
1763compiler_while(struct compiler *c, stmt_ty s)
1764{
1765 basicblock *loop, *orelse, *end, *anchor = NULL;
1766 int constant = expr_constant(s->v.While.test);
1767
1768 if (constant == 0)
1769 return 1;
1770 loop = compiler_new_block(c);
1771 end = compiler_new_block(c);
1772 if (constant == -1) {
1773 anchor = compiler_new_block(c);
1774 if (anchor == NULL)
1775 return 0;
1776 }
1777 if (loop == NULL || end == NULL)
1778 return 0;
1779 if (s->v.While.orelse) {
1780 orelse = compiler_new_block(c);
1781 if (orelse == NULL)
1782 return 0;
1783 }
1784 else
1785 orelse = NULL;
1786
1787 ADDOP_JREL(c, SETUP_LOOP, end);
1788 compiler_use_next_block(c, loop);
1789 if (!compiler_push_fblock(c, LOOP, loop))
1790 return 0;
1791 if (constant == -1) {
1792 VISIT(c, expr, s->v.While.test);
1793 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1794 ADDOP(c, POP_TOP);
1795 }
1796 VISIT_SEQ(c, stmt, s->v.While.body);
1797 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1798
1799 /* XXX should the two POP instructions be in a separate block
1800 if there is no else clause ?
1801 */
1802
1803 if (constant == -1) {
1804 compiler_use_next_block(c, anchor);
1805 ADDOP(c, POP_TOP);
1806 ADDOP(c, POP_BLOCK);
1807 }
1808 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001809 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810 VISIT_SEQ(c, stmt, s->v.While.orelse);
1811 compiler_use_next_block(c, end);
1812
1813 return 1;
1814}
1815
1816static int
1817compiler_continue(struct compiler *c)
1818{
1819 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001820 static const char IN_FINALLY_ERROR_MSG[] =
1821 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822 int i;
1823
1824 if (!c->u->u_nfblocks)
1825 return compiler_error(c, LOOP_ERROR_MSG);
1826 i = c->u->u_nfblocks - 1;
1827 switch (c->u->u_fblock[i].fb_type) {
1828 case LOOP:
1829 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1830 break;
1831 case EXCEPT:
1832 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001833 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1834 /* Prevent continue anywhere under a finally
1835 even if hidden in a sub-try or except. */
1836 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1837 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1838 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 if (i == -1)
1840 return compiler_error(c, LOOP_ERROR_MSG);
1841 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1842 break;
1843 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001844 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 }
1846
1847 return 1;
1848}
1849
1850/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1851
1852 SETUP_FINALLY L
1853 <code for body>
1854 POP_BLOCK
1855 LOAD_CONST <None>
1856 L: <code for finalbody>
1857 END_FINALLY
1858
1859 The special instructions use the block stack. Each block
1860 stack entry contains the instruction that created it (here
1861 SETUP_FINALLY), the level of the value stack at the time the
1862 block stack entry was created, and a label (here L).
1863
1864 SETUP_FINALLY:
1865 Pushes the current value stack level and the label
1866 onto the block stack.
1867 POP_BLOCK:
1868 Pops en entry from the block stack, and pops the value
1869 stack until its level is the same as indicated on the
1870 block stack. (The label is ignored.)
1871 END_FINALLY:
1872 Pops a variable number of entries from the *value* stack
1873 and re-raises the exception they specify. The number of
1874 entries popped depends on the (pseudo) exception type.
1875
1876 The block stack is unwound when an exception is raised:
1877 when a SETUP_FINALLY entry is found, the exception is pushed
1878 onto the value stack (and the exception condition is cleared),
1879 and the interpreter jumps to the label gotten from the block
1880 stack.
1881*/
1882
1883static int
1884compiler_try_finally(struct compiler *c, stmt_ty s)
1885{
1886 basicblock *body, *end;
1887 body = compiler_new_block(c);
1888 end = compiler_new_block(c);
1889 if (body == NULL || end == NULL)
1890 return 0;
1891
1892 ADDOP_JREL(c, SETUP_FINALLY, end);
1893 compiler_use_next_block(c, body);
1894 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1895 return 0;
1896 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1897 ADDOP(c, POP_BLOCK);
1898 compiler_pop_fblock(c, FINALLY_TRY, body);
1899
1900 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1901 compiler_use_next_block(c, end);
1902 if (!compiler_push_fblock(c, FINALLY_END, end))
1903 return 0;
1904 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1905 ADDOP(c, END_FINALLY);
1906 compiler_pop_fblock(c, FINALLY_END, end);
1907
1908 return 1;
1909}
1910
1911/*
1912 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1913 (The contents of the value stack is shown in [], with the top
1914 at the right; 'tb' is trace-back info, 'val' the exception's
1915 associated value, and 'exc' the exception.)
1916
1917 Value stack Label Instruction Argument
1918 [] SETUP_EXCEPT L1
1919 [] <code for S>
1920 [] POP_BLOCK
1921 [] JUMP_FORWARD L0
1922
1923 [tb, val, exc] L1: DUP )
1924 [tb, val, exc, exc] <evaluate E1> )
1925 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1926 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1927 [tb, val, exc, 1] POP )
1928 [tb, val, exc] POP
1929 [tb, val] <assign to V1> (or POP if no V1)
1930 [tb] POP
1931 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001932 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933
1934 [tb, val, exc, 0] L2: POP
1935 [tb, val, exc] DUP
1936 .............................etc.......................
1937
1938 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001939 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940
1941 [] L0: <next statement>
1942
1943 Of course, parts are not generated if Vi or Ei is not present.
1944*/
1945static int
1946compiler_try_except(struct compiler *c, stmt_ty s)
1947{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001948 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 int i, n;
1950
1951 body = compiler_new_block(c);
1952 except = compiler_new_block(c);
1953 orelse = compiler_new_block(c);
1954 end = compiler_new_block(c);
1955 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1956 return 0;
1957 ADDOP_JREL(c, SETUP_EXCEPT, except);
1958 compiler_use_next_block(c, body);
1959 if (!compiler_push_fblock(c, EXCEPT, body))
1960 return 0;
1961 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1962 ADDOP(c, POP_BLOCK);
1963 compiler_pop_fblock(c, EXCEPT, body);
1964 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1965 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1966 compiler_use_next_block(c, except);
1967 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001968 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 s->v.TryExcept.handlers, i);
1970 if (!handler->type && i < n-1)
1971 return compiler_error(c, "default 'except:' must be last");
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001972 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001973 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 except = compiler_new_block(c);
1975 if (except == NULL)
1976 return 0;
1977 if (handler->type) {
1978 ADDOP(c, DUP_TOP);
1979 VISIT(c, expr, handler->type);
1980 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1981 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1982 ADDOP(c, POP_TOP);
1983 }
1984 ADDOP(c, POP_TOP);
1985 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001986 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001987
1988 cleanup_end = compiler_new_block(c);
1989 cleanup_body = compiler_new_block(c);
1990 if(!(cleanup_end || cleanup_body))
1991 return 0;
1992
Guido van Rossum16be03e2007-01-10 18:51:35 +00001993 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001994 ADDOP(c, POP_TOP);
1995
1996 /*
1997 try:
1998 # body
1999 except type as name:
2000 try:
2001 # body
2002 finally:
2003 name = None
2004 del name
2005 */
2006
2007 /* second try: */
2008 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2009 compiler_use_next_block(c, cleanup_body);
2010 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2011 return 0;
2012
2013 /* second # body */
2014 VISIT_SEQ(c, stmt, handler->body);
2015 ADDOP(c, POP_BLOCK);
2016 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2017
2018 /* finally: */
2019 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2020 compiler_use_next_block(c, cleanup_end);
2021 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2022 return 0;
2023
2024 /* name = None */
2025 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00002026 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002027
Guido van Rossum16be03e2007-01-10 18:51:35 +00002028 /* del name */
2029 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002030
2031 ADDOP(c, END_FINALLY);
2032 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 }
2034 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002035 ADDOP(c, POP_TOP);
2036 ADDOP(c, POP_TOP);
2037 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 ADDOP_JREL(c, JUMP_FORWARD, end);
2040 compiler_use_next_block(c, except);
2041 if (handler->type)
2042 ADDOP(c, POP_TOP);
2043 }
2044 ADDOP(c, END_FINALLY);
2045 compiler_use_next_block(c, orelse);
2046 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2047 compiler_use_next_block(c, end);
2048 return 1;
2049}
2050
2051static int
2052compiler_import_as(struct compiler *c, identifier name, identifier asname)
2053{
2054 /* The IMPORT_NAME opcode was already generated. This function
2055 merely needs to bind the result to a name.
2056
2057 If there is a dot in name, we need to split it and emit a
2058 LOAD_ATTR for each name.
2059 */
2060 const char *src = PyString_AS_STRING(name);
2061 const char *dot = strchr(src, '.');
2062 if (dot) {
2063 /* Consume the base module name to get the first attribute */
2064 src = dot + 1;
2065 while (dot) {
2066 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002067 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002069 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002071 if (!attr)
2072 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002074 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002075 src = dot + 1;
2076 }
2077 }
2078 return compiler_nameop(c, asname, Store);
2079}
2080
2081static int
2082compiler_import(struct compiler *c, stmt_ty s)
2083{
2084 /* The Import node stores a module name like a.b.c as a single
2085 string. This is convenient for all cases except
2086 import a.b.c as d
2087 where we need to parse that string to extract the individual
2088 module names.
2089 XXX Perhaps change the representation to make this case simpler?
2090 */
2091 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002094 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002096 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097
Guido van Rossum45aecf42006-03-15 04:58:47 +00002098 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002099 if (level == NULL)
2100 return 0;
2101
2102 ADDOP_O(c, LOAD_CONST, level, consts);
2103 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2105 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2106
2107 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002108 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002109 if (!r)
2110 return r;
2111 }
2112 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 identifier tmp = alias->name;
2114 const char *base = PyString_AS_STRING(alias->name);
2115 char *dot = strchr(base, '.');
2116 if (dot)
2117 tmp = PyString_FromStringAndSize(base,
2118 dot - base);
2119 r = compiler_nameop(c, tmp, Store);
2120 if (dot) {
2121 Py_DECREF(tmp);
2122 }
2123 if (!r)
2124 return r;
2125 }
2126 }
2127 return 1;
2128}
2129
2130static int
2131compiler_from_import(struct compiler *c, stmt_ty s)
2132{
2133 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
2135 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002136 PyObject *level;
2137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 if (!names)
2139 return 0;
2140
Guido van Rossum45aecf42006-03-15 04:58:47 +00002141 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002142 if (!level) {
2143 Py_DECREF(names);
2144 return 0;
2145 }
2146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 /* build up the names */
2148 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002149 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 Py_INCREF(alias->name);
2151 PyTuple_SET_ITEM(names, i, alias->name);
2152 }
2153
2154 if (s->lineno > c->c_future->ff_lineno) {
2155 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2156 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002157 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 Py_DECREF(names);
2159 return compiler_error(c,
2160 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002161 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162
2163 }
2164 }
2165
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002166 ADDOP_O(c, LOAD_CONST, level, consts);
2167 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002169 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2171 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002172 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173 identifier store_name;
2174
2175 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2176 assert(n == 1);
2177 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002178 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 }
2180
2181 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2182 store_name = alias->name;
2183 if (alias->asname)
2184 store_name = alias->asname;
2185
2186 if (!compiler_nameop(c, store_name, Store)) {
2187 Py_DECREF(names);
2188 return 0;
2189 }
2190 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002191 /* remove imported module */
2192 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 return 1;
2194}
2195
2196static int
2197compiler_assert(struct compiler *c, stmt_ty s)
2198{
2199 static PyObject *assertion_error = NULL;
2200 basicblock *end;
2201
2202 if (Py_OptimizeFlag)
2203 return 1;
2204 if (assertion_error == NULL) {
2205 assertion_error = PyString_FromString("AssertionError");
2206 if (assertion_error == NULL)
2207 return 0;
2208 }
2209 VISIT(c, expr, s->v.Assert.test);
2210 end = compiler_new_block(c);
2211 if (end == NULL)
2212 return 0;
2213 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2214 ADDOP(c, POP_TOP);
2215 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2216 if (s->v.Assert.msg) {
2217 VISIT(c, expr, s->v.Assert.msg);
2218 ADDOP_I(c, RAISE_VARARGS, 2);
2219 }
2220 else {
2221 ADDOP_I(c, RAISE_VARARGS, 1);
2222 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002223 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 ADDOP(c, POP_TOP);
2225 return 1;
2226}
2227
2228static int
2229compiler_visit_stmt(struct compiler *c, stmt_ty s)
2230{
2231 int i, n;
2232
Thomas Wouters89f507f2006-12-13 04:49:30 +00002233 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002235 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002238 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002240 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002242 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002243 if (c->u->u_ste->ste_type != FunctionBlock)
2244 return compiler_error(c, "'return' outside function");
2245 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 VISIT(c, expr, s->v.Return.value);
2247 }
2248 else
2249 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2250 ADDOP(c, RETURN_VALUE);
2251 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002252 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 VISIT_SEQ(c, expr, s->v.Delete.targets)
2254 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002255 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 n = asdl_seq_LEN(s->v.Assign.targets);
2257 VISIT(c, expr, s->v.Assign.value);
2258 for (i = 0; i < n; i++) {
2259 if (i < n - 1)
2260 ADDOP(c, DUP_TOP);
2261 VISIT(c, expr,
2262 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2263 }
2264 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002265 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002267 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002269 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002271 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002273 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 n = 0;
2275 if (s->v.Raise.type) {
2276 VISIT(c, expr, s->v.Raise.type);
2277 n++;
2278 if (s->v.Raise.inst) {
2279 VISIT(c, expr, s->v.Raise.inst);
2280 n++;
2281 if (s->v.Raise.tback) {
2282 VISIT(c, expr, s->v.Raise.tback);
2283 n++;
2284 }
2285 }
2286 }
2287 ADDOP_I(c, RAISE_VARARGS, n);
2288 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002289 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002291 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002293 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002295 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002297 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002299 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002300 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002302 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002304 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 ADDOP(c, PRINT_EXPR);
2306 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002307 else if (s->v.Expr.value->kind != Str_kind &&
2308 s->v.Expr.value->kind != Num_kind) {
2309 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 ADDOP(c, POP_TOP);
2311 }
2312 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002313 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002315 case Break_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002316 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 return compiler_error(c, "'break' outside loop");
2318 ADDOP(c, BREAK_LOOP);
2319 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002320 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002322 case With_kind:
2323 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 }
2325 return 1;
2326}
2327
2328static int
2329unaryop(unaryop_ty op)
2330{
2331 switch (op) {
2332 case Invert:
2333 return UNARY_INVERT;
2334 case Not:
2335 return UNARY_NOT;
2336 case UAdd:
2337 return UNARY_POSITIVE;
2338 case USub:
2339 return UNARY_NEGATIVE;
2340 }
2341 return 0;
2342}
2343
2344static int
2345binop(struct compiler *c, operator_ty op)
2346{
2347 switch (op) {
2348 case Add:
2349 return BINARY_ADD;
2350 case Sub:
2351 return BINARY_SUBTRACT;
2352 case Mult:
2353 return BINARY_MULTIPLY;
2354 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002355 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 case Mod:
2357 return BINARY_MODULO;
2358 case Pow:
2359 return BINARY_POWER;
2360 case LShift:
2361 return BINARY_LSHIFT;
2362 case RShift:
2363 return BINARY_RSHIFT;
2364 case BitOr:
2365 return BINARY_OR;
2366 case BitXor:
2367 return BINARY_XOR;
2368 case BitAnd:
2369 return BINARY_AND;
2370 case FloorDiv:
2371 return BINARY_FLOOR_DIVIDE;
2372 }
2373 return 0;
2374}
2375
2376static int
2377cmpop(cmpop_ty op)
2378{
2379 switch (op) {
2380 case Eq:
2381 return PyCmp_EQ;
2382 case NotEq:
2383 return PyCmp_NE;
2384 case Lt:
2385 return PyCmp_LT;
2386 case LtE:
2387 return PyCmp_LE;
2388 case Gt:
2389 return PyCmp_GT;
2390 case GtE:
2391 return PyCmp_GE;
2392 case Is:
2393 return PyCmp_IS;
2394 case IsNot:
2395 return PyCmp_IS_NOT;
2396 case In:
2397 return PyCmp_IN;
2398 case NotIn:
2399 return PyCmp_NOT_IN;
2400 }
2401 return PyCmp_BAD;
2402}
2403
2404static int
2405inplace_binop(struct compiler *c, operator_ty op)
2406{
2407 switch (op) {
2408 case Add:
2409 return INPLACE_ADD;
2410 case Sub:
2411 return INPLACE_SUBTRACT;
2412 case Mult:
2413 return INPLACE_MULTIPLY;
2414 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002415 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 case Mod:
2417 return INPLACE_MODULO;
2418 case Pow:
2419 return INPLACE_POWER;
2420 case LShift:
2421 return INPLACE_LSHIFT;
2422 case RShift:
2423 return INPLACE_RSHIFT;
2424 case BitOr:
2425 return INPLACE_OR;
2426 case BitXor:
2427 return INPLACE_XOR;
2428 case BitAnd:
2429 return INPLACE_AND;
2430 case FloorDiv:
2431 return INPLACE_FLOOR_DIVIDE;
2432 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002433 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002434 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 return 0;
2436}
2437
2438static int
2439compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2440{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002441 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2443
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002444 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002445 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 /* XXX AugStore isn't used anywhere! */
2447
2448 /* First check for assignment to __debug__. Param? */
2449 if ((ctx == Store || ctx == AugStore || ctx == Del)
2450 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2451 return compiler_error(c, "can not assign to __debug__");
2452 }
2453
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002454 mangled = _Py_Mangle(c->u->u_private, name);
2455 if (!mangled)
2456 return 0;
2457
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 op = 0;
2459 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002460 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 switch (scope) {
2462 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002463 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 optype = OP_DEREF;
2465 break;
2466 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002467 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 optype = OP_DEREF;
2469 break;
2470 case LOCAL:
2471 if (c->u->u_ste->ste_type == FunctionBlock)
2472 optype = OP_FAST;
2473 break;
2474 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002475 if (c->u->u_ste->ste_type == FunctionBlock &&
2476 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 optype = OP_GLOBAL;
2478 break;
2479 case GLOBAL_EXPLICIT:
2480 optype = OP_GLOBAL;
2481 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002482 default:
2483 /* scope can be 0 */
2484 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 }
2486
2487 /* XXX Leave assert here, but handle __doc__ and the like better */
2488 assert(scope || PyString_AS_STRING(name)[0] == '_');
2489
2490 switch (optype) {
2491 case OP_DEREF:
2492 switch (ctx) {
2493 case Load: op = LOAD_DEREF; break;
2494 case Store: op = STORE_DEREF; break;
2495 case AugLoad:
2496 case AugStore:
2497 break;
2498 case Del:
2499 PyErr_Format(PyExc_SyntaxError,
2500 "can not delete variable '%s' referenced "
2501 "in nested scope",
2502 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002503 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002506 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002507 PyErr_SetString(PyExc_SystemError,
2508 "param invalid for deref variable");
2509 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
2511 break;
2512 case OP_FAST:
2513 switch (ctx) {
2514 case Load: op = LOAD_FAST; break;
2515 case Store: op = STORE_FAST; break;
2516 case Del: op = DELETE_FAST; break;
2517 case AugLoad:
2518 case AugStore:
2519 break;
2520 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002521 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002522 PyErr_SetString(PyExc_SystemError,
2523 "param invalid for local variable");
2524 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002526 ADDOP_O(c, op, mangled, varnames);
2527 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 return 1;
2529 case OP_GLOBAL:
2530 switch (ctx) {
2531 case Load: op = LOAD_GLOBAL; break;
2532 case Store: op = STORE_GLOBAL; break;
2533 case Del: op = DELETE_GLOBAL; break;
2534 case AugLoad:
2535 case AugStore:
2536 break;
2537 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002538 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002539 PyErr_SetString(PyExc_SystemError,
2540 "param invalid for global variable");
2541 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 }
2543 break;
2544 case OP_NAME:
2545 switch (ctx) {
2546 case Load: op = LOAD_NAME; break;
2547 case Store: op = STORE_NAME; break;
2548 case Del: op = DELETE_NAME; break;
2549 case AugLoad:
2550 case AugStore:
2551 break;
2552 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002553 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002554 PyErr_SetString(PyExc_SystemError,
2555 "param invalid for name variable");
2556 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
2558 break;
2559 }
2560
2561 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002562 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002563 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002564 if (arg < 0)
2565 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002566 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567}
2568
2569static int
2570compiler_boolop(struct compiler *c, expr_ty e)
2571{
2572 basicblock *end;
2573 int jumpi, i, n;
2574 asdl_seq *s;
2575
2576 assert(e->kind == BoolOp_kind);
2577 if (e->v.BoolOp.op == And)
2578 jumpi = JUMP_IF_FALSE;
2579 else
2580 jumpi = JUMP_IF_TRUE;
2581 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002582 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 return 0;
2584 s = e->v.BoolOp.values;
2585 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002586 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002588 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 ADDOP_JREL(c, jumpi, end);
2590 ADDOP(c, POP_TOP)
2591 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002592 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002593 compiler_use_next_block(c, end);
2594 return 1;
2595}
2596
2597static int
2598compiler_list(struct compiler *c, expr_ty e)
2599{
2600 int n = asdl_seq_LEN(e->v.List.elts);
2601 if (e->v.List.ctx == Store) {
2602 ADDOP_I(c, UNPACK_SEQUENCE, n);
2603 }
2604 VISIT_SEQ(c, expr, e->v.List.elts);
2605 if (e->v.List.ctx == Load) {
2606 ADDOP_I(c, BUILD_LIST, n);
2607 }
2608 return 1;
2609}
2610
2611static int
2612compiler_tuple(struct compiler *c, expr_ty e)
2613{
2614 int n = asdl_seq_LEN(e->v.Tuple.elts);
2615 if (e->v.Tuple.ctx == Store) {
2616 ADDOP_I(c, UNPACK_SEQUENCE, n);
2617 }
2618 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2619 if (e->v.Tuple.ctx == Load) {
2620 ADDOP_I(c, BUILD_TUPLE, n);
2621 }
2622 return 1;
2623}
2624
2625static int
2626compiler_compare(struct compiler *c, expr_ty e)
2627{
2628 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002629 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
2631 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2632 VISIT(c, expr, e->v.Compare.left);
2633 n = asdl_seq_LEN(e->v.Compare.ops);
2634 assert(n > 0);
2635 if (n > 1) {
2636 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002637 if (cleanup == NULL)
2638 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002639 VISIT(c, expr,
2640 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 }
2642 for (i = 1; i < n; i++) {
2643 ADDOP(c, DUP_TOP);
2644 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002646 cmpop((cmpop_ty)(asdl_seq_GET(
2647 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2649 NEXT_BLOCK(c);
2650 ADDOP(c, POP_TOP);
2651 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002652 VISIT(c, expr,
2653 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002655 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002657 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 if (n > 1) {
2659 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002660 if (end == NULL)
2661 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 ADDOP_JREL(c, JUMP_FORWARD, end);
2663 compiler_use_next_block(c, cleanup);
2664 ADDOP(c, ROT_TWO);
2665 ADDOP(c, POP_TOP);
2666 compiler_use_next_block(c, end);
2667 }
2668 return 1;
2669}
2670
2671static int
2672compiler_call(struct compiler *c, expr_ty e)
2673{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002675 return compiler_call_helper(c, 0,
2676 e->v.Call.args,
2677 e->v.Call.keywords,
2678 e->v.Call.starargs,
2679 e->v.Call.kwargs);
2680}
2681
2682/* shared code between compiler_call and compiler_class */
2683static int
2684compiler_call_helper(struct compiler *c,
2685 int n, /* Args already pushed */
2686 asdl_seq *args,
2687 asdl_seq *keywords,
2688 expr_ty starargs,
2689 expr_ty kwargs)
2690{
2691 int code = 0;
2692
2693 n += asdl_seq_LEN(args);
2694 VISIT_SEQ(c, expr, args);
2695 if (keywords) {
2696 VISIT_SEQ(c, keyword, keywords);
2697 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002699 if (starargs) {
2700 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 code |= 1;
2702 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002703 if (kwargs) {
2704 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002705 code |= 2;
2706 }
2707 switch (code) {
2708 case 0:
2709 ADDOP_I(c, CALL_FUNCTION, n);
2710 break;
2711 case 1:
2712 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2713 break;
2714 case 2:
2715 ADDOP_I(c, CALL_FUNCTION_KW, n);
2716 break;
2717 case 3:
2718 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2719 break;
2720 }
2721 return 1;
2722}
2723
2724static int
2725compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002726 asdl_seq *generators, int gen_index,
2727 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728{
2729 /* generate code for the iterator, then each of the ifs,
2730 and then write to the element */
2731
2732 comprehension_ty l;
2733 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002734 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735
2736 start = compiler_new_block(c);
2737 skip = compiler_new_block(c);
2738 if_cleanup = compiler_new_block(c);
2739 anchor = compiler_new_block(c);
2740
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002741 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2742 anchor == NULL)
2743 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002744
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002745 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 VISIT(c, expr, l->iter);
2747 ADDOP(c, GET_ITER);
2748 compiler_use_next_block(c, start);
2749 ADDOP_JREL(c, FOR_ITER, anchor);
2750 NEXT_BLOCK(c);
2751 VISIT(c, expr, l->target);
2752
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002753 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 n = asdl_seq_LEN(l->ifs);
2755 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002756 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 VISIT(c, expr, e);
2758 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2759 NEXT_BLOCK(c);
2760 ADDOP(c, POP_TOP);
2761 }
2762
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002763 if (++gen_index < asdl_seq_LEN(generators))
2764 if (!compiler_listcomp_generator(c, tmpname,
2765 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002768 /* only append after the last for generator */
2769 if (gen_index >= asdl_seq_LEN(generators)) {
2770 if (!compiler_nameop(c, tmpname, Load))
2771 return 0;
2772 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002773 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002774
2775 compiler_use_next_block(c, skip);
2776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 for (i = 0; i < n; i++) {
2778 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002779 if (i == 0)
2780 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 ADDOP(c, POP_TOP);
2782 }
2783 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2784 compiler_use_next_block(c, anchor);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002785 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002787 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 return 0;
2789
2790 return 1;
2791}
2792
2793static int
2794compiler_listcomp(struct compiler *c, expr_ty e)
2795{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002797 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 asdl_seq *generators = e->v.ListComp.generators;
2799
2800 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002801 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802 if (!tmp)
2803 return 0;
2804 ADDOP_I(c, BUILD_LIST, 0);
2805 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002807 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2808 e->v.ListComp.elt);
2809 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002810 return rc;
2811}
2812
2813static int
2814compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002815 asdl_seq *generators, int gen_index,
2816 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817{
2818 /* generate code for the iterator, then each of the ifs,
2819 and then write to the element */
2820
2821 comprehension_ty ge;
2822 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002823 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824
2825 start = compiler_new_block(c);
2826 skip = compiler_new_block(c);
2827 if_cleanup = compiler_new_block(c);
2828 anchor = compiler_new_block(c);
2829 end = compiler_new_block(c);
2830
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002831 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 anchor == NULL || end == NULL)
2833 return 0;
2834
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002835 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836 ADDOP_JREL(c, SETUP_LOOP, end);
2837 if (!compiler_push_fblock(c, LOOP, start))
2838 return 0;
2839
2840 if (gen_index == 0) {
2841 /* Receive outermost iter as an implicit argument */
2842 c->u->u_argcount = 1;
2843 ADDOP_I(c, LOAD_FAST, 0);
2844 }
2845 else {
2846 /* Sub-iter - calculate on the fly */
2847 VISIT(c, expr, ge->iter);
2848 ADDOP(c, GET_ITER);
2849 }
2850 compiler_use_next_block(c, start);
2851 ADDOP_JREL(c, FOR_ITER, anchor);
2852 NEXT_BLOCK(c);
2853 VISIT(c, expr, ge->target);
2854
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002855 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002856 n = asdl_seq_LEN(ge->ifs);
2857 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002858 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859 VISIT(c, expr, e);
2860 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2861 NEXT_BLOCK(c);
2862 ADDOP(c, POP_TOP);
2863 }
2864
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002865 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2867 return 0;
2868
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002869 /* only append after the last 'for' generator */
2870 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 VISIT(c, expr, elt);
2872 ADDOP(c, YIELD_VALUE);
2873 ADDOP(c, POP_TOP);
2874
2875 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002876 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 for (i = 0; i < n; i++) {
2878 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002879 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 compiler_use_next_block(c, if_cleanup);
2881
2882 ADDOP(c, POP_TOP);
2883 }
2884 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2885 compiler_use_next_block(c, anchor);
2886 ADDOP(c, POP_BLOCK);
2887 compiler_pop_fblock(c, LOOP, start);
2888 compiler_use_next_block(c, end);
2889
2890 return 1;
2891}
2892
2893static int
2894compiler_genexp(struct compiler *c, expr_ty e)
2895{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002896 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 PyCodeObject *co;
2898 expr_ty outermost_iter = ((comprehension_ty)
2899 (asdl_seq_GET(e->v.GeneratorExp.generators,
2900 0)))->iter;
2901
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002902 if (!name) {
2903 name = PyString_FromString("<genexpr>");
2904 if (!name)
2905 return 0;
2906 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002907
2908 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2909 return 0;
2910 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2911 e->v.GeneratorExp.elt);
2912 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002913 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914 if (co == NULL)
2915 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002917 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002918 Py_DECREF(co);
2919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920 VISIT(c, expr, outermost_iter);
2921 ADDOP(c, GET_ITER);
2922 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923
2924 return 1;
2925}
2926
2927static int
2928compiler_visit_keyword(struct compiler *c, keyword_ty k)
2929{
2930 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2931 VISIT(c, expr, k->value);
2932 return 1;
2933}
2934
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002935/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 whether they are true or false.
2937
2938 Return values: 1 for true, 0 for false, -1 for non-constant.
2939 */
2940
2941static int
2942expr_constant(expr_ty e)
2943{
2944 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002945 case Ellipsis_kind:
2946 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002947 case Num_kind:
2948 return PyObject_IsTrue(e->v.Num.n);
2949 case Str_kind:
2950 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002951 case Name_kind:
2952 /* __debug__ is not assignable, so we can optimize
2953 * it away in if and while statements */
2954 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2955 "__debug__") == 0)
2956 return ! Py_OptimizeFlag;
2957 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958 default:
2959 return -1;
2960 }
2961}
2962
Guido van Rossumc2e20742006-02-27 22:32:47 +00002963/*
2964 Implements the with statement from PEP 343.
2965
2966 The semantics outlined in that PEP are as follows:
2967
2968 with EXPR as VAR:
2969 BLOCK
2970
2971 It is implemented roughly as:
2972
Thomas Wouters477c8d52006-05-27 19:21:47 +00002973 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002974 exit = context.__exit__ # not calling it
2975 value = context.__enter__()
2976 try:
2977 VAR = value # if VAR present in the syntax
2978 BLOCK
2979 finally:
2980 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002981 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002982 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002983 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002984 exit(*exc)
2985 */
2986static int
2987compiler_with(struct compiler *c, stmt_ty s)
2988{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002989 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990 basicblock *block, *finally;
2991 identifier tmpexit, tmpvalue = NULL;
2992
2993 assert(s->kind == With_kind);
2994
Guido van Rossumc2e20742006-02-27 22:32:47 +00002995 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002996 enter_attr = PyString_InternFromString("__enter__");
2997 if (!enter_attr)
2998 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002999 }
3000 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003001 exit_attr = PyString_InternFromString("__exit__");
3002 if (!exit_attr)
3003 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003004 }
3005
3006 block = compiler_new_block(c);
3007 finally = compiler_new_block(c);
3008 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003009 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003010
3011 /* Create a temporary variable to hold context.__exit__ */
3012 tmpexit = compiler_new_tmpname(c);
3013 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003014 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003015 PyArena_AddPyObject(c->c_arena, tmpexit);
3016
3017 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003018 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003019 We need to do this rather than preserving it on the stack
3020 because SETUP_FINALLY remembers the stack level.
3021 We need to do the assignment *inside* the try/finally
3022 so that context.__exit__() is called when the assignment
3023 fails. But we need to call context.__enter__() *before*
3024 the try/finally so that if it fails we won't call
3025 context.__exit__().
3026 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003027 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003028 if (tmpvalue == NULL)
3029 return 0;
3030 PyArena_AddPyObject(c->c_arena, tmpvalue);
3031 }
3032
Thomas Wouters477c8d52006-05-27 19:21:47 +00003033 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003034 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003035
3036 /* Squirrel away context.__exit__ */
3037 ADDOP(c, DUP_TOP);
3038 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3039 if (!compiler_nameop(c, tmpexit, Store))
3040 return 0;
3041
3042 /* Call context.__enter__() */
3043 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3044 ADDOP_I(c, CALL_FUNCTION, 0);
3045
3046 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003047 /* Store it in tmpvalue */
3048 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003049 return 0;
3050 }
3051 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003052 /* Discard result from context.__enter__() */
3053 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003054 }
3055
3056 /* Start the try block */
3057 ADDOP_JREL(c, SETUP_FINALLY, finally);
3058
3059 compiler_use_next_block(c, block);
3060 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003061 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003062 }
3063
3064 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003065 /* Bind saved result of context.__enter__() to VAR */
3066 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003067 !compiler_nameop(c, tmpvalue, Del))
3068 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003069 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003070 }
3071
3072 /* BLOCK code */
3073 VISIT_SEQ(c, stmt, s->v.With.body);
3074
3075 /* End of try block; start the finally block */
3076 ADDOP(c, POP_BLOCK);
3077 compiler_pop_fblock(c, FINALLY_TRY, block);
3078
3079 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3080 compiler_use_next_block(c, finally);
3081 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003082 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083
3084 /* Finally block starts; push tmpexit and issue our magic opcode. */
3085 if (!compiler_nameop(c, tmpexit, Load) ||
3086 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003087 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003088 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089
3090 /* Finally block ends. */
3091 ADDOP(c, END_FINALLY);
3092 compiler_pop_fblock(c, FINALLY_END, finally);
3093 return 1;
3094}
3095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096static int
3097compiler_visit_expr(struct compiler *c, expr_ty e)
3098{
3099 int i, n;
3100
Thomas Wouters89f507f2006-12-13 04:49:30 +00003101 /* If expr e has a different line number than the last expr/stmt,
3102 set a new line number for the next instruction.
3103 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003104 if (e->lineno > c->u->u_lineno) {
3105 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003106 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 }
3108 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003109 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003111 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 VISIT(c, expr, e->v.BinOp.left);
3113 VISIT(c, expr, e->v.BinOp.right);
3114 ADDOP(c, binop(c, e->v.BinOp.op));
3115 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003116 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 VISIT(c, expr, e->v.UnaryOp.operand);
3118 ADDOP(c, unaryop(e->v.UnaryOp.op));
3119 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003122 case IfExp_kind:
3123 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003124 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 /* XXX get rid of arg? */
3126 ADDOP_I(c, BUILD_MAP, 0);
3127 n = asdl_seq_LEN(e->v.Dict.values);
3128 /* We must arrange things just right for STORE_SUBSCR.
3129 It wants the stack to look like (value) (dict) (key) */
3130 for (i = 0; i < n; i++) {
3131 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003132 VISIT(c, expr,
3133 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003135 VISIT(c, expr,
3136 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 ADDOP(c, STORE_SUBSCR);
3138 }
3139 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003140 case Set_kind:
3141 n = asdl_seq_LEN(e->v.Set.elts);
3142 VISIT_SEQ(c, expr, e->v.Set.elts);
3143 ADDOP_I(c, BUILD_SET, n);
3144 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003145 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003147 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 return compiler_genexp(c, e);
3149 case Yield_kind:
3150 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003151 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 if (e->v.Yield.value) {
3153 VISIT(c, expr, e->v.Yield.value);
3154 }
3155 else {
3156 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3157 }
3158 ADDOP(c, YIELD_VALUE);
3159 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003160 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003162 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003164 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3166 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003167 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003168 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3169 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003170 case Bytes_kind:
3171 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3172 ADDOP(c, MAKE_BYTES);
3173 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003174 case Ellipsis_kind:
3175 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3176 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003178 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 if (e->v.Attribute.ctx != AugStore)
3180 VISIT(c, expr, e->v.Attribute.value);
3181 switch (e->v.Attribute.ctx) {
3182 case AugLoad:
3183 ADDOP(c, DUP_TOP);
3184 /* Fall through to load */
3185 case Load:
3186 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3187 break;
3188 case AugStore:
3189 ADDOP(c, ROT_TWO);
3190 /* Fall through to save */
3191 case Store:
3192 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3193 break;
3194 case Del:
3195 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3196 break;
3197 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003198 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003199 PyErr_SetString(PyExc_SystemError,
3200 "param invalid in attribute expression");
3201 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202 }
3203 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003204 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 switch (e->v.Subscript.ctx) {
3206 case AugLoad:
3207 VISIT(c, expr, e->v.Subscript.value);
3208 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3209 break;
3210 case Load:
3211 VISIT(c, expr, e->v.Subscript.value);
3212 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3213 break;
3214 case AugStore:
3215 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3216 break;
3217 case Store:
3218 VISIT(c, expr, e->v.Subscript.value);
3219 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3220 break;
3221 case Del:
3222 VISIT(c, expr, e->v.Subscript.value);
3223 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3224 break;
3225 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003226 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003227 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003228 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003229 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 }
3231 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003232 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3234 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003235 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003237 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 return compiler_tuple(c, e);
3239 }
3240 return 1;
3241}
3242
3243static int
3244compiler_augassign(struct compiler *c, stmt_ty s)
3245{
3246 expr_ty e = s->v.AugAssign.target;
3247 expr_ty auge;
3248
3249 assert(s->kind == AugAssign_kind);
3250
3251 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003252 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003254 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003255 if (auge == NULL)
3256 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003257 VISIT(c, expr, auge);
3258 VISIT(c, expr, s->v.AugAssign.value);
3259 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3260 auge->v.Attribute.ctx = AugStore;
3261 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 break;
3263 case Subscript_kind:
3264 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003265 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003266 if (auge == NULL)
3267 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 VISIT(c, expr, auge);
3269 VISIT(c, expr, s->v.AugAssign.value);
3270 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003271 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003273 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003275 if (!compiler_nameop(c, e->v.Name.id, Load))
3276 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 VISIT(c, expr, s->v.AugAssign.value);
3278 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3279 return compiler_nameop(c, e->v.Name.id, Store);
3280 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003281 PyErr_Format(PyExc_SystemError,
3282 "invalid node type (%d) for augmented assignment",
3283 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003284 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 }
3286 return 1;
3287}
3288
3289static int
3290compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3291{
3292 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003293 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3294 PyErr_SetString(PyExc_SystemError,
3295 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 f = &c->u->u_fblock[c->u->u_nfblocks++];
3299 f->fb_type = t;
3300 f->fb_block = b;
3301 return 1;
3302}
3303
3304static void
3305compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3306{
3307 struct compiler_unit *u = c->u;
3308 assert(u->u_nfblocks > 0);
3309 u->u_nfblocks--;
3310 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3311 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3312}
3313
Thomas Wouters89f507f2006-12-13 04:49:30 +00003314static int
3315compiler_in_loop(struct compiler *c) {
3316 int i;
3317 struct compiler_unit *u = c->u;
3318 for (i = 0; i < u->u_nfblocks; ++i) {
3319 if (u->u_fblock[i].fb_type == LOOP)
3320 return 1;
3321 }
3322 return 0;
3323}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324/* Raises a SyntaxError and returns 0.
3325 If something goes wrong, a different exception may be raised.
3326*/
3327
3328static int
3329compiler_error(struct compiler *c, const char *errstr)
3330{
3331 PyObject *loc;
3332 PyObject *u = NULL, *v = NULL;
3333
3334 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3335 if (!loc) {
3336 Py_INCREF(Py_None);
3337 loc = Py_None;
3338 }
3339 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3340 Py_None, loc);
3341 if (!u)
3342 goto exit;
3343 v = Py_BuildValue("(zO)", errstr, u);
3344 if (!v)
3345 goto exit;
3346 PyErr_SetObject(PyExc_SyntaxError, v);
3347 exit:
3348 Py_DECREF(loc);
3349 Py_XDECREF(u);
3350 Py_XDECREF(v);
3351 return 0;
3352}
3353
3354static int
3355compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003356 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003358 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003360 /* XXX this code is duplicated */
3361 switch (ctx) {
3362 case AugLoad: /* fall through to Load */
3363 case Load: op = BINARY_SUBSCR; break;
3364 case AugStore:/* fall through to Store */
3365 case Store: op = STORE_SUBSCR; break;
3366 case Del: op = DELETE_SUBSCR; break;
3367 case Param:
3368 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003369 "invalid %s kind %d in subscript\n",
3370 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003371 return 0;
3372 }
3373 if (ctx == AugLoad) {
3374 ADDOP_I(c, DUP_TOPX, 2);
3375 }
3376 else if (ctx == AugStore) {
3377 ADDOP(c, ROT_THREE);
3378 }
3379 ADDOP(c, op);
3380 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381}
3382
3383static int
3384compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3385{
3386 int n = 2;
3387 assert(s->kind == Slice_kind);
3388
3389 /* only handles the cases where BUILD_SLICE is emitted */
3390 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003391 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392 }
3393 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003394 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003396
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003398 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399 }
3400 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003401 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 }
3403
3404 if (s->v.Slice.step) {
3405 n++;
3406 VISIT(c, expr, s->v.Slice.step);
3407 }
3408 ADDOP_I(c, BUILD_SLICE, n);
3409 return 1;
3410}
3411
3412static int
3413compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3414{
3415 int op = 0, slice_offset = 0, stack_count = 0;
3416
3417 assert(s->v.Slice.step == NULL);
3418 if (s->v.Slice.lower) {
3419 slice_offset++;
3420 stack_count++;
3421 if (ctx != AugStore)
3422 VISIT(c, expr, s->v.Slice.lower);
3423 }
3424 if (s->v.Slice.upper) {
3425 slice_offset += 2;
3426 stack_count++;
3427 if (ctx != AugStore)
3428 VISIT(c, expr, s->v.Slice.upper);
3429 }
3430
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003431 if (ctx == AugLoad) {
3432 switch (stack_count) {
3433 case 0: ADDOP(c, DUP_TOP); break;
3434 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3435 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3436 }
3437 }
3438 else if (ctx == AugStore) {
3439 switch (stack_count) {
3440 case 0: ADDOP(c, ROT_TWO); break;
3441 case 1: ADDOP(c, ROT_THREE); break;
3442 case 2: ADDOP(c, ROT_FOUR); break;
3443 }
3444 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445
3446 switch (ctx) {
3447 case AugLoad: /* fall through to Load */
3448 case Load: op = SLICE; break;
3449 case AugStore:/* fall through to Store */
3450 case Store: op = STORE_SLICE; break;
3451 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003452 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003453 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003454 PyErr_SetString(PyExc_SystemError,
3455 "param invalid in simple slice");
3456 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 }
3458
3459 ADDOP(c, op + slice_offset);
3460 return 1;
3461}
3462
3463static int
3464compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3465 expr_context_ty ctx)
3466{
3467 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 case Slice_kind:
3469 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470 case Index_kind:
3471 VISIT(c, expr, s->v.Index.value);
3472 break;
3473 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003474 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003475 PyErr_SetString(PyExc_SystemError,
3476 "extended slice invalid in nested slice");
3477 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 }
3479 return 1;
3480}
3481
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482static int
3483compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3484{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003485 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003487 case Index_kind:
3488 kindname = "index";
3489 if (ctx != AugStore) {
3490 VISIT(c, expr, s->v.Index.value);
3491 }
3492 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003494 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 if (!s->v.Slice.step)
3496 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003497 if (ctx != AugStore) {
3498 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 return 0;
3500 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003501 break;
3502 case ExtSlice_kind:
3503 kindname = "extended slice";
3504 if (ctx != AugStore) {
3505 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3506 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003507 slice_ty sub = (slice_ty)asdl_seq_GET(
3508 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003509 if (!compiler_visit_nested_slice(c, sub, ctx))
3510 return 0;
3511 }
3512 ADDOP_I(c, BUILD_TUPLE, n);
3513 }
3514 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003515 default:
3516 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003517 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003518 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003520 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521}
3522
Thomas Wouters89f507f2006-12-13 04:49:30 +00003523/* End of the compiler section, beginning of the assembler section */
3524
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525/* do depth-first search of basic block graph, starting with block.
3526 post records the block indices in post-order.
3527
3528 XXX must handle implicit jumps from one block to next
3529*/
3530
Thomas Wouters89f507f2006-12-13 04:49:30 +00003531struct assembler {
3532 PyObject *a_bytecode; /* string containing bytecode */
3533 int a_offset; /* offset into bytecode */
3534 int a_nblocks; /* number of reachable blocks */
3535 basicblock **a_postorder; /* list of blocks in dfs postorder */
3536 PyObject *a_lnotab; /* string containing lnotab */
3537 int a_lnotab_off; /* offset into lnotab */
3538 int a_lineno; /* last lineno of emitted instruction */
3539 int a_lineno_off; /* bytecode offset of last lineno */
3540};
3541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542static void
3543dfs(struct compiler *c, basicblock *b, struct assembler *a)
3544{
3545 int i;
3546 struct instr *instr = NULL;
3547
3548 if (b->b_seen)
3549 return;
3550 b->b_seen = 1;
3551 if (b->b_next != NULL)
3552 dfs(c, b->b_next, a);
3553 for (i = 0; i < b->b_iused; i++) {
3554 instr = &b->b_instr[i];
3555 if (instr->i_jrel || instr->i_jabs)
3556 dfs(c, instr->i_target, a);
3557 }
3558 a->a_postorder[a->a_nblocks++] = b;
3559}
3560
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003561static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3563{
3564 int i;
3565 struct instr *instr;
3566 if (b->b_seen || b->b_startdepth >= depth)
3567 return maxdepth;
3568 b->b_seen = 1;
3569 b->b_startdepth = depth;
3570 for (i = 0; i < b->b_iused; i++) {
3571 instr = &b->b_instr[i];
3572 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3573 if (depth > maxdepth)
3574 maxdepth = depth;
3575 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3576 if (instr->i_jrel || instr->i_jabs) {
3577 maxdepth = stackdepth_walk(c, instr->i_target,
3578 depth, maxdepth);
3579 if (instr->i_opcode == JUMP_ABSOLUTE ||
3580 instr->i_opcode == JUMP_FORWARD) {
3581 goto out; /* remaining code is dead */
3582 }
3583 }
3584 }
3585 if (b->b_next)
3586 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3587out:
3588 b->b_seen = 0;
3589 return maxdepth;
3590}
3591
3592/* Find the flow path that needs the largest stack. We assume that
3593 * cycles in the flow graph have no net effect on the stack depth.
3594 */
3595static int
3596stackdepth(struct compiler *c)
3597{
3598 basicblock *b, *entryblock;
3599 entryblock = NULL;
3600 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3601 b->b_seen = 0;
3602 b->b_startdepth = INT_MIN;
3603 entryblock = b;
3604 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003605 if (!entryblock)
3606 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 return stackdepth_walk(c, entryblock, 0, 0);
3608}
3609
3610static int
3611assemble_init(struct assembler *a, int nblocks, int firstlineno)
3612{
3613 memset(a, 0, sizeof(struct assembler));
3614 a->a_lineno = firstlineno;
3615 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3616 if (!a->a_bytecode)
3617 return 0;
3618 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3619 if (!a->a_lnotab)
3620 return 0;
3621 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003622 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003623 if (!a->a_postorder) {
3624 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003626 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 return 1;
3628}
3629
3630static void
3631assemble_free(struct assembler *a)
3632{
3633 Py_XDECREF(a->a_bytecode);
3634 Py_XDECREF(a->a_lnotab);
3635 if (a->a_postorder)
3636 PyObject_Free(a->a_postorder);
3637}
3638
3639/* Return the size of a basic block in bytes. */
3640
3641static int
3642instrsize(struct instr *instr)
3643{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003644 if (!instr->i_hasarg)
3645 return 1;
3646 if (instr->i_oparg > 0xffff)
3647 return 6;
3648 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649}
3650
3651static int
3652blocksize(basicblock *b)
3653{
3654 int i;
3655 int size = 0;
3656
3657 for (i = 0; i < b->b_iused; i++)
3658 size += instrsize(&b->b_instr[i]);
3659 return size;
3660}
3661
3662/* All about a_lnotab.
3663
3664c_lnotab is an array of unsigned bytes disguised as a Python string.
3665It is used to map bytecode offsets to source code line #s (when needed
3666for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003667
Tim Peters2a7f3842001-06-09 09:26:21 +00003668The array is conceptually a list of
3669 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003670pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003671
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003672 byte code offset source code line number
3673 0 1
3674 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003675 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003676 350 307
3677 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003678
3679The first trick is that these numbers aren't stored, only the increments
3680from one row to the next (this doesn't really work, but it's a start):
3681
3682 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3683
3684The second trick is that an unsigned byte can't hold negative values, or
3685values larger than 255, so (a) there's a deep assumption that byte code
3686offsets and their corresponding line #s both increase monotonically, and (b)
3687if at least one column jumps by more than 255 from one row to the next, more
3688than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003689from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003690part. A user of c_lnotab desiring to find the source line number
3691corresponding to a bytecode address A should do something like this
3692
3693 lineno = addr = 0
3694 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003695 addr += addr_incr
3696 if addr > A:
3697 return lineno
3698 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003699
3700In order for this to work, when the addr field increments by more than 255,
3701the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003702increment is < 256. So, in the example above, assemble_lnotab (it used
3703to be called com_set_lineno) should not (as was actually done until 2.2)
3704expand 300, 300 to 255, 255, 45, 45,
3705 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003706*/
3707
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003708static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003710{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 int d_bytecode, d_lineno;
3712 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003713 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714
3715 d_bytecode = a->a_offset - a->a_lineno_off;
3716 d_lineno = i->i_lineno - a->a_lineno;
3717
3718 assert(d_bytecode >= 0);
3719 assert(d_lineno >= 0);
3720
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003721 /* XXX(nnorwitz): is there a better way to handle this?
3722 for loops are special, we want to be able to trace them
3723 each time around, so we need to set an extra line number. */
3724 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003725 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003726
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003728 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 nbytes = a->a_lnotab_off + 2 * ncodes;
3730 len = PyString_GET_SIZE(a->a_lnotab);
3731 if (nbytes >= len) {
3732 if (len * 2 < nbytes)
3733 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003734 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 len *= 2;
3736 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3737 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003738 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003739 lnotab = (unsigned char *)
3740 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003741 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742 *lnotab++ = 255;
3743 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003744 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 d_bytecode -= ncodes * 255;
3746 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 assert(d_bytecode <= 255);
3749 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003750 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 nbytes = a->a_lnotab_off + 2 * ncodes;
3752 len = PyString_GET_SIZE(a->a_lnotab);
3753 if (nbytes >= len) {
3754 if (len * 2 < nbytes)
3755 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003756 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 len *= 2;
3758 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3759 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003760 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003761 lnotab = (unsigned char *)
3762 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003764 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003766 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003768 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 d_lineno -= ncodes * 255;
3771 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003772 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003773
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 len = PyString_GET_SIZE(a->a_lnotab);
3775 if (a->a_lnotab_off + 2 >= len) {
3776 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003777 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003778 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003779 lnotab = (unsigned char *)
3780 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003781
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 a->a_lnotab_off += 2;
3783 if (d_bytecode) {
3784 *lnotab++ = d_bytecode;
3785 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003786 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003787 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 *lnotab++ = 0;
3789 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 a->a_lineno = i->i_lineno;
3792 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003793 return 1;
3794}
3795
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796/* assemble_emit()
3797 Extend the bytecode with a new instruction.
3798 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003799*/
3800
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003801static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003803{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003804 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003805 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 char *code;
3807
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003808 size = instrsize(i);
3809 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003811 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003814 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 if (a->a_offset + size >= len) {
3816 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003817 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3820 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003821 if (size == 6) {
3822 assert(i->i_hasarg);
3823 *code++ = (char)EXTENDED_ARG;
3824 *code++ = ext & 0xff;
3825 *code++ = ext >> 8;
3826 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003827 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003829 if (i->i_hasarg) {
3830 assert(size == 3 || size == 6);
3831 *code++ = arg & 0xff;
3832 *code++ = arg >> 8;
3833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003835}
3836
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003837static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003839{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003841 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003842 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 /* Compute the size of each block and fixup jump args.
3845 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003846start:
3847 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003849 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 bsize = blocksize(b);
3851 b->b_offset = totsize;
3852 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003853 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003854 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3856 bsize = b->b_offset;
3857 for (i = 0; i < b->b_iused; i++) {
3858 struct instr *instr = &b->b_instr[i];
3859 /* Relative jumps are computed relative to
3860 the instruction pointer after fetching
3861 the jump instruction.
3862 */
3863 bsize += instrsize(instr);
3864 if (instr->i_jabs)
3865 instr->i_oparg = instr->i_target->b_offset;
3866 else if (instr->i_jrel) {
3867 int delta = instr->i_target->b_offset - bsize;
3868 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003869 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003870 else
3871 continue;
3872 if (instr->i_oparg > 0xffff)
3873 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003874 }
3875 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003876
3877 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003878 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003879 with a better solution.
3880
3881 In the meantime, should the goto be dropped in favor
3882 of a loop?
3883
3884 The issue is that in the first loop blocksize() is called
3885 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003886 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003887 i_oparg is calculated in the second loop above.
3888
3889 So we loop until we stop seeing new EXTENDED_ARGs.
3890 The only EXTENDED_ARGs that could be popping up are
3891 ones in jump instructions. So this should converge
3892 fairly quickly.
3893 */
3894 if (last_extended_arg_count != extended_arg_count) {
3895 last_extended_arg_count = extended_arg_count;
3896 goto start;
3897 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003898}
3899
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003900static PyObject *
3901dict_keys_inorder(PyObject *dict, int offset)
3902{
3903 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003904 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003905
3906 tuple = PyTuple_New(size);
3907 if (tuple == NULL)
3908 return NULL;
3909 while (PyDict_Next(dict, &pos, &k, &v)) {
3910 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003911 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003912 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003913 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003914 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003915 PyTuple_SET_ITEM(tuple, i - offset, k);
3916 }
3917 return tuple;
3918}
3919
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003920static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003922{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 PySTEntryObject *ste = c->u->u_ste;
3924 int flags = 0, n;
3925 if (ste->ste_type != ModuleBlock)
3926 flags |= CO_NEWLOCALS;
3927 if (ste->ste_type == FunctionBlock) {
3928 if (!ste->ste_unoptimized)
3929 flags |= CO_OPTIMIZED;
3930 if (ste->ste_nested)
3931 flags |= CO_NESTED;
3932 if (ste->ste_generator)
3933 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003934 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003935 if (ste->ste_varargs)
3936 flags |= CO_VARARGS;
3937 if (ste->ste_varkeywords)
3938 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003939 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003941
3942 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003943 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 n = PyDict_Size(c->u->u_freevars);
3946 if (n < 0)
3947 return -1;
3948 if (n == 0) {
3949 n = PyDict_Size(c->u->u_cellvars);
3950 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003951 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003952 if (n == 0) {
3953 flags |= CO_NOFREE;
3954 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003955 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003956
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003957 return flags;
3958}
3959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960static PyCodeObject *
3961makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003962{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 PyObject *tmp;
3964 PyCodeObject *co = NULL;
3965 PyObject *consts = NULL;
3966 PyObject *names = NULL;
3967 PyObject *varnames = NULL;
3968 PyObject *filename = NULL;
3969 PyObject *name = NULL;
3970 PyObject *freevars = NULL;
3971 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003972 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003974
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 tmp = dict_keys_inorder(c->u->u_consts, 0);
3976 if (!tmp)
3977 goto error;
3978 consts = PySequence_List(tmp); /* optimize_code requires a list */
3979 Py_DECREF(tmp);
3980
3981 names = dict_keys_inorder(c->u->u_names, 0);
3982 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3983 if (!consts || !names || !varnames)
3984 goto error;
3985
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003986 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3987 if (!cellvars)
3988 goto error;
3989 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3990 if (!freevars)
3991 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 filename = PyString_FromString(c->c_filename);
3993 if (!filename)
3994 goto error;
3995
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003996 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997 flags = compute_code_flags(c);
3998 if (flags < 0)
3999 goto error;
4000
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004001 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 if (!bytecode)
4003 goto error;
4004
4005 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4006 if (!tmp)
4007 goto error;
4008 Py_DECREF(consts);
4009 consts = tmp;
4010
Guido van Rossum4f72a782006-10-27 23:31:49 +00004011 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4012 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 bytecode, consts, names, varnames,
4014 freevars, cellvars,
4015 filename, c->u->u_name,
4016 c->u->u_firstlineno,
4017 a->a_lnotab);
4018 error:
4019 Py_XDECREF(consts);
4020 Py_XDECREF(names);
4021 Py_XDECREF(varnames);
4022 Py_XDECREF(filename);
4023 Py_XDECREF(name);
4024 Py_XDECREF(freevars);
4025 Py_XDECREF(cellvars);
4026 Py_XDECREF(bytecode);
4027 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004028}
4029
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004030
4031/* For debugging purposes only */
4032#if 0
4033static void
4034dump_instr(const struct instr *i)
4035{
4036 const char *jrel = i->i_jrel ? "jrel " : "";
4037 const char *jabs = i->i_jabs ? "jabs " : "";
4038 char arg[128];
4039
4040 *arg = '\0';
4041 if (i->i_hasarg)
4042 sprintf(arg, "arg: %d ", i->i_oparg);
4043
4044 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4045 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4046}
4047
4048static void
4049dump_basicblock(const basicblock *b)
4050{
4051 const char *seen = b->b_seen ? "seen " : "";
4052 const char *b_return = b->b_return ? "return " : "";
4053 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4054 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4055 if (b->b_instr) {
4056 int i;
4057 for (i = 0; i < b->b_iused; i++) {
4058 fprintf(stderr, " [%02d] ", i);
4059 dump_instr(b->b_instr + i);
4060 }
4061 }
4062}
4063#endif
4064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004065static PyCodeObject *
4066assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004067{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 basicblock *b, *entryblock;
4069 struct assembler a;
4070 int i, j, nblocks;
4071 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073 /* Make sure every block that falls off the end returns None.
4074 XXX NEXT_BLOCK() isn't quite right, because if the last
4075 block ends with a jump or return b_next shouldn't set.
4076 */
4077 if (!c->u->u_curblock->b_return) {
4078 NEXT_BLOCK(c);
4079 if (addNone)
4080 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4081 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004082 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084 nblocks = 0;
4085 entryblock = NULL;
4086 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4087 nblocks++;
4088 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004089 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004090
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004091 /* Set firstlineno if it wasn't explicitly set. */
4092 if (!c->u->u_firstlineno) {
4093 if (entryblock && entryblock->b_instr)
4094 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4095 else
4096 c->u->u_firstlineno = 1;
4097 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004098 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4099 goto error;
4100 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004103 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004105 /* Emit code in reverse postorder from dfs. */
4106 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004107 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108 for (j = 0; j < b->b_iused; j++)
4109 if (!assemble_emit(&a, &b->b_instr[j]))
4110 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004111 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004112
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004113 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4114 goto error;
4115 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4116 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 co = makecode(c, &a);
4119 error:
4120 assemble_free(&a);
4121 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004122}