blob: 481cc858861255b42b659200b50078954cc55f7d [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 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122 bool u_lineno_set; /* boolean to indicate whether instr
123 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);
179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180static PyCodeObject *assemble(struct compiler *, int addNone);
181static PyObject *__doc__;
182
183PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000185{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186 /* Name mangling: __private becomes _classname__private.
187 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000188 const char *p, *name = PyString_AsString(ident);
189 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190 size_t nlen, plen;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000191 if (privateobj == NULL || !PyString_Check(privateobj) ||
192 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000193 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000195 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 nlen = strlen(name);
198 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000199 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 /* Strip leading underscores from class name */
203 while (*p == '_')
204 p++;
205 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 plen = strlen(p);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
211 if (!ident)
212 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000214 buffer = PyString_AS_STRING(ident);
215 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 strncpy(buffer+1, p, plen);
217 strcpy(buffer+1+plen, name);
218 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000219}
220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221static int
222compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000223{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000225
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 c->c_stack = PyList_New(0);
227 if (!c->c_stack)
228 return 0;
229
230 return 1;
231}
232
233PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000234PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000235 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236{
237 struct compiler c;
238 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000239 PyCompilerFlags local_flags;
240 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000242 if (!__doc__) {
243 __doc__ = PyString_InternFromString("__doc__");
244 if (!__doc__)
245 return NULL;
246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247
248 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000249 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000251 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 c.c_future = PyFuture_FromAST(mod, filename);
253 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000254 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000256 local_flags.cf_flags = 0;
257 flags = &local_flags;
258 }
259 merged = c.c_future->ff_features | flags->cf_flags;
260 c.c_future->ff_features = merged;
261 flags->cf_flags = merged;
262 c.c_flags = flags;
263 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264
265 c.c_st = PySymtable_Build(mod, filename, c.c_future);
266 if (c.c_st == NULL) {
267 if (!PyErr_Occurred())
268 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000269 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 }
271
272 /* XXX initialize to NULL for now, need to handle */
273 c.c_encoding = NULL;
274
275 co = compiler_mod(&c, mod);
276
Thomas Wouters1175c432006-02-27 22:49:54 +0000277 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000279 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 return co;
281}
282
283PyCodeObject *
284PyNode_Compile(struct _node *n, const char *filename)
285{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000286 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000288 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289 if (!arena)
290 return NULL;
291 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000292 if (mod)
293 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000294 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000295 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000296}
297
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000298static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000300{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 if (c->c_st)
302 PySymtable_Free(c->c_st);
303 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000306}
307
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000310{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000311 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 PyObject *v, *k;
313 PyObject *dict = PyDict_New();
314 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000315
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316 n = PyList_Size(list);
317 for (i = 0; i < n; i++) {
318 v = PyInt_FromLong(i);
319 if (!v) {
320 Py_DECREF(dict);
321 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000322 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000323 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
326 Py_XDECREF(k);
327 Py_DECREF(v);
328 Py_DECREF(dict);
329 return NULL;
330 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000331 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 return dict;
335}
336
337/* Return new dict containing names from src that match scope(s).
338
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000339src is a symbol table dictionary. If the scope of a name matches
340either scope_type or flag is set, insert it into the new dict. The
341values are integers, starting at offset and increasing by one for
342each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343*/
344
345static PyObject *
346dictbytype(PyObject *src, int scope_type, int flag, int offset)
347{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000348 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349 PyObject *k, *v, *dest = PyDict_New();
350
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000351 assert(offset >= 0);
352 if (dest == NULL)
353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354
355 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000356 /* XXX this should probably be a macro in symtable.h */
357 assert(PyInt_Check(v));
358 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000360 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
361 PyObject *tuple, *item = PyInt_FromLong(i);
362 if (item == NULL) {
363 Py_DECREF(dest);
364 return NULL;
365 }
366 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000367 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000368 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
369 Py_DECREF(item);
370 Py_DECREF(dest);
371 Py_XDECREF(tuple);
372 return NULL;
373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000375 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377 }
378 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000379}
380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381static void
382compiler_unit_check(struct compiler_unit *u)
383{
384 basicblock *block;
385 for (block = u->u_blocks; block != NULL; block = block->b_list) {
386 assert(block != (void *)0xcbcbcbcb);
387 assert(block != (void *)0xfbfbfbfb);
388 assert(block != (void *)0xdbdbdbdb);
389 if (block->b_instr != NULL) {
390 assert(block->b_ialloc > 0);
391 assert(block->b_iused > 0);
392 assert(block->b_ialloc >= block->b_iused);
393 }
394 else {
395 assert (block->b_iused == 0);
396 assert (block->b_ialloc == 0);
397 }
398 }
399}
400
401static void
402compiler_unit_free(struct compiler_unit *u)
403{
404 basicblock *b, *next;
405
406 compiler_unit_check(u);
407 b = u->u_blocks;
408 while (b != NULL) {
409 if (b->b_instr)
410 PyObject_Free((void *)b->b_instr);
411 next = b->b_list;
412 PyObject_Free((void *)b);
413 b = next;
414 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415 Py_CLEAR(u->u_ste);
416 Py_CLEAR(u->u_name);
417 Py_CLEAR(u->u_consts);
418 Py_CLEAR(u->u_names);
419 Py_CLEAR(u->u_varnames);
420 Py_CLEAR(u->u_freevars);
421 Py_CLEAR(u->u_cellvars);
422 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 PyObject_Free(u);
424}
425
426static int
427compiler_enter_scope(struct compiler *c, identifier name, void *key,
428 int lineno)
429{
430 struct compiler_unit *u;
431
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
433 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000434 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000435 PyErr_NoMemory();
436 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000437 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000438 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000440 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 u->u_ste = PySymtable_Lookup(c->c_st, key);
442 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000443 compiler_unit_free(u);
444 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 }
446 Py_INCREF(name);
447 u->u_name = name;
448 u->u_varnames = list2dict(u->u_ste->ste_varnames);
449 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450 if (!u->u_varnames || !u->u_cellvars) {
451 compiler_unit_free(u);
452 return 0;
453 }
454
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000456 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457 if (!u->u_freevars) {
458 compiler_unit_free(u);
459 return 0;
460 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461
462 u->u_blocks = NULL;
463 u->u_tmpname = 0;
464 u->u_nfblocks = 0;
465 u->u_firstlineno = lineno;
466 u->u_lineno = 0;
467 u->u_lineno_set = false;
468 u->u_consts = PyDict_New();
469 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000470 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471 return 0;
472 }
473 u->u_names = PyDict_New();
474 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000475 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 return 0;
477 }
478
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000479 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480
481 /* Push the old compiler_unit on the stack. */
482 if (c->u) {
483 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000484 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
485 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000486 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return 0;
488 }
489 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000490 u->u_private = c->u->u_private;
491 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 }
493 c->u = u;
494
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000495 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000496 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 return 0;
498
499 return 1;
500}
501
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000502static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503compiler_exit_scope(struct compiler *c)
504{
505 int n;
506 PyObject *wrapper;
507
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000508 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 compiler_unit_free(c->u);
510 /* Restore c->u to the parent unit. */
511 n = PyList_GET_SIZE(c->c_stack) - 1;
512 if (n >= 0) {
513 wrapper = PyList_GET_ITEM(c->c_stack, n);
514 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000515 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000516 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000518 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 compiler_unit_check(c->u);
520 }
521 else
522 c->u = NULL;
523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
Guido van Rossumc2e20742006-02-27 22:32:47 +0000526/* Allocate a new "anonymous" local variable.
527 Used by list comprehensions and with statements.
528*/
529
530static PyObject *
531compiler_new_tmpname(struct compiler *c)
532{
533 char tmpname[256];
534 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
535 return PyString_FromString(tmpname);
536}
537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538/* Allocate a new block and return a pointer to it.
539 Returns NULL on error.
540*/
541
542static basicblock *
543compiler_new_block(struct compiler *c)
544{
545 basicblock *b;
546 struct compiler_unit *u;
547
548 u = c->u;
549 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000550 if (b == NULL) {
551 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 b->b_list = u->u_blocks;
557 u->u_blocks = b;
558 return b;
559}
560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561static basicblock *
562compiler_use_new_block(struct compiler *c)
563{
564 basicblock *block = compiler_new_block(c);
565 if (block == NULL)
566 return NULL;
567 c->u->u_curblock = block;
568 return block;
569}
570
571static basicblock *
572compiler_next_block(struct compiler *c)
573{
574 basicblock *block = compiler_new_block(c);
575 if (block == NULL)
576 return NULL;
577 c->u->u_curblock->b_next = block;
578 c->u->u_curblock = block;
579 return block;
580}
581
582static basicblock *
583compiler_use_next_block(struct compiler *c, basicblock *block)
584{
585 assert(block != NULL);
586 c->u->u_curblock->b_next = block;
587 c->u->u_curblock = block;
588 return block;
589}
590
591/* Returns the offset of the next instruction in the current block's
592 b_instr array. Resizes the b_instr as necessary.
593 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000594*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
596static int
597compiler_next_instr(struct compiler *c, basicblock *b)
598{
599 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000600 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601 b->b_instr = (struct instr *)PyObject_Malloc(
602 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 if (b->b_instr == NULL) {
604 PyErr_NoMemory();
605 return -1;
606 }
607 b->b_ialloc = DEFAULT_BLOCK_SIZE;
608 memset((char *)b->b_instr, 0,
609 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 size_t oldsize, newsize;
614 oldsize = b->b_ialloc * sizeof(struct instr);
615 newsize = oldsize << 1;
616 if (newsize == 0) {
617 PyErr_NoMemory();
618 return -1;
619 }
620 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621 tmp = (struct instr *)PyObject_Realloc(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 if (tmp == NULL) {
624 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 }
627 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
629 }
630 return b->b_iused++;
631}
632
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000633/* Set the i_lineno member of the instruction at offse off if the
634 line number for the current expression/statement (?) has not
635 already been set. If it has been set, the call has no effect.
636
637 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000638*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640static void
641compiler_set_lineno(struct compiler *c, int off)
642{
643 basicblock *b;
644 if (c->u->u_lineno_set)
645 return;
646 c->u->u_lineno_set = true;
647 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000648 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649}
650
651static int
652opcode_stack_effect(int opcode, int oparg)
653{
654 switch (opcode) {
655 case POP_TOP:
656 return -1;
657 case ROT_TWO:
658 case ROT_THREE:
659 return 0;
660 case DUP_TOP:
661 return 1;
662 case ROT_FOUR:
663 return 0;
664
665 case UNARY_POSITIVE:
666 case UNARY_NEGATIVE:
667 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 case UNARY_INVERT:
669 return 0;
670
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000671 case LIST_APPEND:
672 return -2;
673
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 case BINARY_POWER:
675 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 case BINARY_MODULO:
677 case BINARY_ADD:
678 case BINARY_SUBTRACT:
679 case BINARY_SUBSCR:
680 case BINARY_FLOOR_DIVIDE:
681 case BINARY_TRUE_DIVIDE:
682 return -1;
683 case INPLACE_FLOOR_DIVIDE:
684 case INPLACE_TRUE_DIVIDE:
685 return -1;
686
687 case SLICE+0:
688 return 1;
689 case SLICE+1:
690 return 0;
691 case SLICE+2:
692 return 0;
693 case SLICE+3:
694 return -1;
695
696 case STORE_SLICE+0:
697 return -2;
698 case STORE_SLICE+1:
699 return -3;
700 case STORE_SLICE+2:
701 return -3;
702 case STORE_SLICE+3:
703 return -4;
704
705 case DELETE_SLICE+0:
706 return -1;
707 case DELETE_SLICE+1:
708 return -2;
709 case DELETE_SLICE+2:
710 return -2;
711 case DELETE_SLICE+3:
712 return -3;
713
714 case INPLACE_ADD:
715 case INPLACE_SUBTRACT:
716 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 case INPLACE_MODULO:
718 return -1;
719 case STORE_SUBSCR:
720 return -3;
721 case DELETE_SUBSCR:
722 return -2;
723
724 case BINARY_LSHIFT:
725 case BINARY_RSHIFT:
726 case BINARY_AND:
727 case BINARY_XOR:
728 case BINARY_OR:
729 return -1;
730 case INPLACE_POWER:
731 return -1;
732 case GET_ITER:
733 return 0;
734
735 case PRINT_EXPR:
736 return -1;
737 case PRINT_ITEM:
738 return -1;
739 case PRINT_NEWLINE:
740 return 0;
741 case PRINT_ITEM_TO:
742 return -2;
743 case PRINT_NEWLINE_TO:
744 return -1;
745 case INPLACE_LSHIFT:
746 case INPLACE_RSHIFT:
747 case INPLACE_AND:
748 case INPLACE_XOR:
749 case INPLACE_OR:
750 return -1;
751 case BREAK_LOOP:
752 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000753 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000754 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 case LOAD_LOCALS:
756 return 1;
757 case RETURN_VALUE:
758 return -1;
759 case IMPORT_STAR:
760 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 case YIELD_VALUE:
762 return 0;
763
764 case POP_BLOCK:
765 return 0;
766 case END_FINALLY:
767 return -1; /* or -2 or -3 if exception occurred */
768 case BUILD_CLASS:
769 return -2;
770
771 case STORE_NAME:
772 return -1;
773 case DELETE_NAME:
774 return 0;
775 case UNPACK_SEQUENCE:
776 return oparg-1;
777 case FOR_ITER:
778 return 1;
779
780 case STORE_ATTR:
781 return -2;
782 case DELETE_ATTR:
783 return -1;
784 case STORE_GLOBAL:
785 return -1;
786 case DELETE_GLOBAL:
787 return 0;
788 case DUP_TOPX:
789 return oparg;
790 case LOAD_CONST:
791 return 1;
792 case LOAD_NAME:
793 return 1;
794 case BUILD_TUPLE:
795 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000796 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 return 1-oparg;
798 case BUILD_MAP:
799 return 1;
800 case LOAD_ATTR:
801 return 0;
802 case COMPARE_OP:
803 return -1;
804 case IMPORT_NAME:
805 return 0;
806 case IMPORT_FROM:
807 return 1;
808
809 case JUMP_FORWARD:
810 case JUMP_IF_FALSE:
811 case JUMP_IF_TRUE:
812 case JUMP_ABSOLUTE:
813 return 0;
814
815 case LOAD_GLOBAL:
816 return 1;
817
818 case CONTINUE_LOOP:
819 return 0;
820 case SETUP_LOOP:
821 return 0;
822 case SETUP_EXCEPT:
823 case SETUP_FINALLY:
824 return 3; /* actually pushed by an exception */
825
826 case LOAD_FAST:
827 return 1;
828 case STORE_FAST:
829 return -1;
830 case DELETE_FAST:
831 return 0;
832
833 case RAISE_VARARGS:
834 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000835#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 case CALL_FUNCTION:
837 return -NARGS(oparg);
838 case CALL_FUNCTION_VAR:
839 case CALL_FUNCTION_KW:
840 return -NARGS(oparg)-1;
841 case CALL_FUNCTION_VAR_KW:
842 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000844 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000845#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 case BUILD_SLICE:
847 if (oparg == 3)
848 return -2;
849 else
850 return -1;
851
852 case MAKE_CLOSURE:
853 return -oparg;
854 case LOAD_CLOSURE:
855 return 1;
856 case LOAD_DEREF:
857 return 1;
858 case STORE_DEREF:
859 return -1;
860 default:
861 fprintf(stderr, "opcode = %d\n", opcode);
862 Py_FatalError("opcode_stack_effect()");
863
864 }
865 return 0; /* not reachable */
866}
867
868/* Add an opcode with no argument.
869 Returns 0 on failure, 1 on success.
870*/
871
872static int
873compiler_addop(struct compiler *c, int opcode)
874{
875 basicblock *b;
876 struct instr *i;
877 int off;
878 off = compiler_next_instr(c, c->u->u_curblock);
879 if (off < 0)
880 return 0;
881 b = c->u->u_curblock;
882 i = &b->b_instr[off];
883 i->i_opcode = opcode;
884 i->i_hasarg = 0;
885 if (opcode == RETURN_VALUE)
886 b->b_return = 1;
887 compiler_set_lineno(c, off);
888 return 1;
889}
890
891static int
892compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
893{
894 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000895 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000897 /* necessary to make sure types aren't coerced (e.g., int and long) */
898 t = PyTuple_Pack(2, o, o->ob_type);
899 if (t == NULL)
900 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901
902 v = PyDict_GetItem(dict, t);
903 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000904 if (PyErr_Occurred())
905 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 arg = PyDict_Size(dict);
907 v = PyInt_FromLong(arg);
908 if (!v) {
909 Py_DECREF(t);
910 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000911 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 if (PyDict_SetItem(dict, t, v) < 0) {
913 Py_DECREF(t);
914 Py_DECREF(v);
915 return -1;
916 }
917 Py_DECREF(v);
918 }
919 else
920 arg = PyInt_AsLong(v);
921 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000922 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923}
924
925static int
926compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
927 PyObject *o)
928{
929 int arg = compiler_add_o(c, dict, o);
930 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000931 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 return compiler_addop_i(c, opcode, arg);
933}
934
935static int
936compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000937 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938{
939 int arg;
940 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
941 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000942 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 arg = compiler_add_o(c, dict, mangled);
944 Py_DECREF(mangled);
945 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000946 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 return compiler_addop_i(c, opcode, arg);
948}
949
950/* Add an opcode with an integer argument.
951 Returns 0 on failure, 1 on success.
952*/
953
954static int
955compiler_addop_i(struct compiler *c, int opcode, int oparg)
956{
957 struct instr *i;
958 int off;
959 off = compiler_next_instr(c, c->u->u_curblock);
960 if (off < 0)
961 return 0;
962 i = &c->u->u_curblock->b_instr[off];
963 i->i_opcode = opcode;
964 i->i_oparg = oparg;
965 i->i_hasarg = 1;
966 compiler_set_lineno(c, off);
967 return 1;
968}
969
970static int
971compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
972{
973 struct instr *i;
974 int off;
975
976 assert(b != NULL);
977 off = compiler_next_instr(c, c->u->u_curblock);
978 if (off < 0)
979 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 i = &c->u->u_curblock->b_instr[off];
981 i->i_opcode = opcode;
982 i->i_target = b;
983 i->i_hasarg = 1;
984 if (absolute)
985 i->i_jabs = 1;
986 else
987 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000988 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return 1;
990}
991
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000992/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
993 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 it as the current block. NEXT_BLOCK() also creates an implicit jump
995 from the current block to the new block.
996*/
997
Thomas Wouters89f507f2006-12-13 04:49:30 +0000998/* The returns inside these macros make it impossible to decref objects
999 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000*/
1001
1002
1003#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001004 if (compiler_use_new_block((C)) == NULL) \
1005 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006}
1007
1008#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 if (compiler_next_block((C)) == NULL) \
1010 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013#define ADDOP(C, OP) { \
1014 if (!compiler_addop((C), (OP))) \
1015 return 0; \
1016}
1017
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001018#define ADDOP_IN_SCOPE(C, OP) { \
1019 if (!compiler_addop((C), (OP))) { \
1020 compiler_exit_scope(c); \
1021 return 0; \
1022 } \
1023}
1024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025#define ADDOP_O(C, OP, O, TYPE) { \
1026 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1027 return 0; \
1028}
1029
1030#define ADDOP_NAME(C, OP, O, TYPE) { \
1031 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1032 return 0; \
1033}
1034
1035#define ADDOP_I(C, OP, O) { \
1036 if (!compiler_addop_i((C), (OP), (O))) \
1037 return 0; \
1038}
1039
1040#define ADDOP_JABS(C, OP, O) { \
1041 if (!compiler_addop_j((C), (OP), (O), 1)) \
1042 return 0; \
1043}
1044
1045#define ADDOP_JREL(C, OP, O) { \
1046 if (!compiler_addop_j((C), (OP), (O), 0)) \
1047 return 0; \
1048}
1049
1050/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1051 the ASDL name to synthesize the name of the C type and the visit function.
1052*/
1053
1054#define VISIT(C, TYPE, V) {\
1055 if (!compiler_visit_ ## TYPE((C), (V))) \
1056 return 0; \
1057}
1058
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001059#define VISIT_IN_SCOPE(C, TYPE, V) {\
1060 if (!compiler_visit_ ## TYPE((C), (V))) { \
1061 compiler_exit_scope(c); \
1062 return 0; \
1063 } \
1064}
1065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066#define VISIT_SLICE(C, V, CTX) {\
1067 if (!compiler_visit_slice((C), (V), (CTX))) \
1068 return 0; \
1069}
1070
1071#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001072 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001074 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 if (!compiler_visit_ ## TYPE((C), elt)) \
1077 return 0; \
1078 } \
1079}
1080
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001081#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001082 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001083 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001084 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001086 if (!compiler_visit_ ## TYPE((C), elt)) { \
1087 compiler_exit_scope(c); \
1088 return 0; \
1089 } \
1090 } \
1091}
1092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093static int
1094compiler_isdocstring(stmt_ty s)
1095{
1096 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001097 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 return s->v.Expr.value->kind == Str_kind;
1099}
1100
1101/* Compile a sequence of statements, checking for a docstring. */
1102
1103static int
1104compiler_body(struct compiler *c, asdl_seq *stmts)
1105{
1106 int i = 0;
1107 stmt_ty st;
1108
1109 if (!asdl_seq_LEN(stmts))
1110 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 if (compiler_isdocstring(st)) {
1113 i = 1;
1114 VISIT(c, expr, st->v.Expr.value);
1115 if (!compiler_nameop(c, __doc__, Store))
1116 return 0;
1117 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001118 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 return 1;
1121}
1122
1123static PyCodeObject *
1124compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001125{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001127 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 static PyObject *module;
1129 if (!module) {
1130 module = PyString_FromString("<module>");
1131 if (!module)
1132 return NULL;
1133 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001134 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1135 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001136 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 switch (mod->kind) {
1138 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001139 if (!compiler_body(c, mod->v.Module.body)) {
1140 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001142 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 break;
1144 case Interactive_kind:
1145 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 VISIT_SEQ_IN_SCOPE(c, stmt,
1147 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 break;
1149 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001150 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001151 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 break;
1153 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001154 PyErr_SetString(PyExc_SystemError,
1155 "suite should not be possible");
1156 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001157 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001158 PyErr_Format(PyExc_SystemError,
1159 "module kind %d should not be possible",
1160 mod->kind);
1161 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001162 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 co = assemble(c, addNone);
1164 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001165 return co;
1166}
1167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168/* The test for LOCAL must come before the test for FREE in order to
1169 handle classes where name is both local and free. The local var is
1170 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001171*/
1172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173static int
1174get_ref_type(struct compiler *c, PyObject *name)
1175{
1176 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001177 if (scope == 0) {
1178 char buf[350];
1179 PyOS_snprintf(buf, sizeof(buf),
1180 "unknown scope for %.100s in %.100s(%s) in %s\n"
1181 "symbols: %s\nlocals: %s\nglobals: %s\n",
1182 PyString_AS_STRING(name),
1183 PyString_AS_STRING(c->u->u_name),
1184 PyObject_REPR(c->u->u_ste->ste_id),
1185 c->c_filename,
1186 PyObject_REPR(c->u->u_ste->ste_symbols),
1187 PyObject_REPR(c->u->u_varnames),
1188 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001190 Py_FatalError(buf);
1191 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001192
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001193 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
1196static int
1197compiler_lookup_arg(PyObject *dict, PyObject *name)
1198{
1199 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001200 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001202 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001204 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001206 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 return PyInt_AS_LONG(v);
1208}
1209
1210static int
1211compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1212{
1213 int i, free = PyCode_GetNumFree(co);
1214 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001215 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1216 ADDOP_I(c, MAKE_FUNCTION, args);
1217 return 1;
1218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 for (i = 0; i < free; ++i) {
1220 /* Bypass com_addop_varname because it will generate
1221 LOAD_DEREF but LOAD_CLOSURE is needed.
1222 */
1223 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1224 int arg, reftype;
1225
1226 /* Special case: If a class contains a method with a
1227 free variable that has the same name as a method,
1228 the name will be considered free *and* local in the
1229 class. It should be handled by the closure, as
1230 well as by the normal name loookup logic.
1231 */
1232 reftype = get_ref_type(c, name);
1233 if (reftype == CELL)
1234 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1235 else /* (reftype == FREE) */
1236 arg = compiler_lookup_arg(c->u->u_freevars, name);
1237 if (arg == -1) {
1238 printf("lookup %s in %s %d %d\n"
1239 "freevars of %s: %s\n",
1240 PyObject_REPR(name),
1241 PyString_AS_STRING(c->u->u_name),
1242 reftype, arg,
1243 PyString_AS_STRING(co->co_name),
1244 PyObject_REPR(co->co_freevars));
1245 Py_FatalError("compiler_make_closure()");
1246 }
1247 ADDOP_I(c, LOAD_CLOSURE, arg);
1248 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001249 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001251 ADDOP_I(c, MAKE_CLOSURE, args);
1252 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255static int
1256compiler_decorators(struct compiler *c, asdl_seq* decos)
1257{
1258 int i;
1259
1260 if (!decos)
1261 return 1;
1262
1263 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 }
1266 return 1;
1267}
1268
1269static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001270compiler_unpack_nested(struct compiler *c, asdl_seq *args) {
1271 int i, len;
1272 len = asdl_seq_LEN(args);
1273 ADDOP_I(c, UNPACK_SEQUENCE, len);
1274 for (i = 0; i < len; i++) {
1275 arg_ty elt = (arg_ty)asdl_seq_GET(args, i);
1276 switch (elt->kind) {
1277 case SimpleArg_kind:
1278 if (!compiler_nameop(c, elt->v.SimpleArg.arg, Store))
1279 return 0;
1280 break;
1281 case NestedArgs_kind:
1282 if (!compiler_unpack_nested(c, elt->v.NestedArgs.args))
1283 return 0;
1284 break;
1285 default:
1286 return 0;
1287 }
1288 }
1289 return 1;
1290}
1291
1292static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293compiler_arguments(struct compiler *c, arguments_ty args)
1294{
1295 int i;
1296 int n = asdl_seq_LEN(args->args);
Neal Norwitzc1505362006-12-28 06:47:50 +00001297
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 for (i = 0; i < n; i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 arg_ty arg = (arg_ty)asdl_seq_GET(args->args, i);
1300 if (arg->kind == NestedArgs_kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 PyObject *id = PyString_FromFormat(".%d", i);
1302 if (id == NULL) {
1303 return 0;
1304 }
1305 if (!compiler_nameop(c, id, Load)) {
1306 Py_DECREF(id);
1307 return 0;
1308 }
1309 Py_DECREF(id);
Neal Norwitzc1505362006-12-28 06:47:50 +00001310 if (!compiler_unpack_nested(c, arg->v.NestedArgs.args))
1311 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 }
1313 }
1314 return 1;
1315}
1316
1317static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1319 asdl_seq *kw_defaults)
1320{
1321 int i, default_count = 0;
1322 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1325 if (default_) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 ADDOP_O(c, LOAD_CONST, arg->v.SimpleArg.arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 if (!compiler_visit_expr(c, default_)) {
1328 return -1;
1329 }
1330 default_count++;
1331 }
1332 }
1333 return default_count;
1334}
1335
1336static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001337compiler_visit_argannotation(struct compiler *c, identifier id,
1338 expr_ty annotation, PyObject *names)
1339{
1340 if (annotation) {
1341 VISIT(c, expr, annotation);
1342 if (PyList_Append(names, id))
1343 return -1;
1344 }
1345 return 0;
1346}
1347
1348static int
1349compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1350 PyObject *names)
1351{
1352 int i, error;
1353 for (i = 0; i < asdl_seq_LEN(args); i++) {
1354 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1355 if (arg->kind == NestedArgs_kind)
1356 error = compiler_visit_argannotations(
1357 c,
1358 arg->v.NestedArgs.args,
1359 names);
1360 else
1361 error = compiler_visit_argannotation(
1362 c,
1363 arg->v.SimpleArg.arg,
1364 arg->v.SimpleArg.annotation,
1365 names);
1366 if (error)
1367 return error;
1368 }
1369 return 0;
1370}
1371
1372static int
1373compiler_visit_annotations(struct compiler *c, arguments_ty args,
1374 expr_ty returns)
1375{
1376 /* push arg annotations and a list of the argument names. return the #
1377 of items pushed. this is out-of-order wrt the source code. */
1378 static identifier return_str;
1379 PyObject *names;
1380 int len;
1381 names = PyList_New(0);
1382 if (!names)
1383 return -1;
1384
1385 if (compiler_visit_argannotations(c, args->args, names))
1386 goto error;
1387 if (args->varargannotation &&
1388 compiler_visit_argannotation(c, args->vararg,
1389 args->varargannotation, names))
1390 goto error;
1391 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1392 goto error;
1393 if (args->kwargannotation &&
1394 compiler_visit_argannotation(c, args->kwarg,
1395 args->kwargannotation, names))
1396 goto error;
1397
1398 if (!return_str) {
1399 return_str = PyString_InternFromString("return");
1400 if (!return_str)
1401 goto error;
1402 }
1403 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1404 goto error;
1405 }
1406
1407 len = PyList_GET_SIZE(names);
1408 if (len) {
1409 /* convert names to a tuple and place on stack */
1410 PyObject *elt;
1411 int i;
1412 PyObject *s = PyTuple_New(len);
1413 if (!s)
1414 goto error;
1415 for (i = 0; i < len; i++) {
1416 elt = PyList_GET_ITEM(names, i);
1417 Py_INCREF(elt);
1418 PyTuple_SET_ITEM(s, i, elt);
1419 }
1420 ADDOP_O(c, LOAD_CONST, s, consts);
1421 Py_DECREF(s);
1422 len++; /* include the just-pushed tuple */
1423 }
1424 Py_DECREF(names);
1425 return len;
1426
1427error:
1428 Py_DECREF(names);
1429 return -1;
1430}
1431
1432static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433compiler_function(struct compiler *c, stmt_ty s)
1434{
1435 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001436 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001438 expr_ty returns = s->v.FunctionDef.returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 asdl_seq* decos = s->v.FunctionDef.decorators;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001440 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001442 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443
1444 assert(s->kind == FunctionDef_kind);
1445
1446 if (!compiler_decorators(c, decos))
1447 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 if (args->kwonlyargs) {
1449 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1450 args->kw_defaults);
1451 if (res < 0)
1452 return 0;
1453 kw_default_count = res;
1454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 if (args->defaults)
1456 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001457 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1460 s->lineno))
1461 return 0;
1462
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001463 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001464 docstring = compiler_isdocstring(st);
1465 if (docstring)
1466 first_const = st->v.Expr.value->v.Str.s;
1467 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001468 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001469 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001472 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 compiler_arguments(c, args);
1474
1475 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001478 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001480 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1481 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 }
1483 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001484 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 if (co == NULL)
1486 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 arglength = asdl_seq_LEN(args->defaults);
1489 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001490 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001491 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001492 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Neal Norwitzc1505362006-12-28 06:47:50 +00001494 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1496 ADDOP_I(c, CALL_FUNCTION, 1);
1497 }
1498
1499 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1500}
1501
1502static int
1503compiler_class(struct compiler *c, stmt_ty s)
1504{
1505 int n;
1506 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001507 PyObject *str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 /* push class name on stack, needed by BUILD_CLASS */
1509 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1510 /* push the tuple of base classes on the stack */
1511 n = asdl_seq_LEN(s->v.ClassDef.bases);
1512 if (n > 0)
1513 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1514 ADDOP_I(c, BUILD_TUPLE, n);
1515 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1516 s->lineno))
1517 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001518 c->u->u_private = s->v.ClassDef.name;
1519 Py_INCREF(c->u->u_private);
1520 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (!str || !compiler_nameop(c, str, Load)) {
1522 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001523 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001525 }
1526
1527 Py_DECREF(str);
1528 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 if (!str || !compiler_nameop(c, str, Store)) {
1530 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001531 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001533 }
1534 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001536 if (!compiler_body(c, s->v.ClassDef.body)) {
1537 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001541 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1542 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001544 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 if (co == NULL)
1546 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001548 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001549 Py_DECREF(co);
1550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 ADDOP_I(c, CALL_FUNCTION, 0);
1552 ADDOP(c, BUILD_CLASS);
1553 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1554 return 0;
1555 return 1;
1556}
1557
1558static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001559compiler_ifexp(struct compiler *c, expr_ty e)
1560{
1561 basicblock *end, *next;
1562
1563 assert(e->kind == IfExp_kind);
1564 end = compiler_new_block(c);
1565 if (end == NULL)
1566 return 0;
1567 next = compiler_new_block(c);
1568 if (next == NULL)
1569 return 0;
1570 VISIT(c, expr, e->v.IfExp.test);
1571 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1572 ADDOP(c, POP_TOP);
1573 VISIT(c, expr, e->v.IfExp.body);
1574 ADDOP_JREL(c, JUMP_FORWARD, end);
1575 compiler_use_next_block(c, next);
1576 ADDOP(c, POP_TOP);
1577 VISIT(c, expr, e->v.IfExp.orelse);
1578 compiler_use_next_block(c, end);
1579 return 1;
1580}
1581
1582static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583compiler_lambda(struct compiler *c, expr_ty e)
1584{
1585 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001586 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001587 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 arguments_ty args = e->v.Lambda.args;
1589 assert(e->kind == Lambda_kind);
1590
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001591 if (!name) {
1592 name = PyString_InternFromString("<lambda>");
1593 if (!name)
1594 return 0;
1595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596
Guido van Rossum4f72a782006-10-27 23:31:49 +00001597 if (args->kwonlyargs) {
1598 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1599 args->kw_defaults);
1600 if (res < 0) return 0;
1601 kw_default_count = res;
1602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 if (args->defaults)
1604 VISIT_SEQ(c, expr, args->defaults);
1605 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1606 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001607
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001608 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 compiler_arguments(c, args);
1610
1611 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001612 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001613 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1614 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001616 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 if (co == NULL)
1618 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619
Guido van Rossum4f72a782006-10-27 23:31:49 +00001620 arglength = asdl_seq_LEN(args->defaults);
1621 arglength |= kw_default_count << 8;
1622 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001623 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624
1625 return 1;
1626}
1627
1628static int
1629compiler_print(struct compiler *c, stmt_ty s)
1630{
1631 int i, n;
1632 bool dest;
1633
1634 assert(s->kind == Print_kind);
1635 n = asdl_seq_LEN(s->v.Print.values);
1636 dest = false;
1637 if (s->v.Print.dest) {
1638 VISIT(c, expr, s->v.Print.dest);
1639 dest = true;
1640 }
1641 for (i = 0; i < n; i++) {
1642 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1643 if (dest) {
1644 ADDOP(c, DUP_TOP);
1645 VISIT(c, expr, e);
1646 ADDOP(c, ROT_TWO);
1647 ADDOP(c, PRINT_ITEM_TO);
1648 }
1649 else {
1650 VISIT(c, expr, e);
1651 ADDOP(c, PRINT_ITEM);
1652 }
1653 }
1654 if (s->v.Print.nl) {
1655 if (dest)
1656 ADDOP(c, PRINT_NEWLINE_TO)
1657 else
1658 ADDOP(c, PRINT_NEWLINE)
1659 }
1660 else if (dest)
1661 ADDOP(c, POP_TOP);
1662 return 1;
1663}
1664
1665static int
1666compiler_if(struct compiler *c, stmt_ty s)
1667{
1668 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001669 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 assert(s->kind == If_kind);
1671 end = compiler_new_block(c);
1672 if (end == NULL)
1673 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001674 next = compiler_new_block(c);
1675 if (next == NULL)
1676 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001677
1678 constant = expr_constant(s->v.If.test);
1679 /* constant = 0: "if 0"
1680 * constant = 1: "if 1", "if 2", ...
1681 * constant = -1: rest */
1682 if (constant == 0) {
1683 if (s->v.If.orelse)
1684 VISIT_SEQ(c, stmt, s->v.If.orelse);
1685 } else if (constant == 1) {
1686 VISIT_SEQ(c, stmt, s->v.If.body);
1687 } else {
1688 VISIT(c, expr, s->v.If.test);
1689 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1690 ADDOP(c, POP_TOP);
1691 VISIT_SEQ(c, stmt, s->v.If.body);
1692 ADDOP_JREL(c, JUMP_FORWARD, end);
1693 compiler_use_next_block(c, next);
1694 ADDOP(c, POP_TOP);
1695 if (s->v.If.orelse)
1696 VISIT_SEQ(c, stmt, s->v.If.orelse);
1697 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 compiler_use_next_block(c, end);
1699 return 1;
1700}
1701
1702static int
1703compiler_for(struct compiler *c, stmt_ty s)
1704{
1705 basicblock *start, *cleanup, *end;
1706
1707 start = compiler_new_block(c);
1708 cleanup = compiler_new_block(c);
1709 end = compiler_new_block(c);
1710 if (start == NULL || end == NULL || cleanup == NULL)
1711 return 0;
1712 ADDOP_JREL(c, SETUP_LOOP, end);
1713 if (!compiler_push_fblock(c, LOOP, start))
1714 return 0;
1715 VISIT(c, expr, s->v.For.iter);
1716 ADDOP(c, GET_ITER);
1717 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001718 /* XXX(nnorwitz): is there a better way to handle this?
1719 for loops are special, we want to be able to trace them
1720 each time around, so we need to set an extra line number. */
1721 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 ADDOP_JREL(c, FOR_ITER, cleanup);
1723 VISIT(c, expr, s->v.For.target);
1724 VISIT_SEQ(c, stmt, s->v.For.body);
1725 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1726 compiler_use_next_block(c, cleanup);
1727 ADDOP(c, POP_BLOCK);
1728 compiler_pop_fblock(c, LOOP, start);
1729 VISIT_SEQ(c, stmt, s->v.For.orelse);
1730 compiler_use_next_block(c, end);
1731 return 1;
1732}
1733
1734static int
1735compiler_while(struct compiler *c, stmt_ty s)
1736{
1737 basicblock *loop, *orelse, *end, *anchor = NULL;
1738 int constant = expr_constant(s->v.While.test);
1739
1740 if (constant == 0)
1741 return 1;
1742 loop = compiler_new_block(c);
1743 end = compiler_new_block(c);
1744 if (constant == -1) {
1745 anchor = compiler_new_block(c);
1746 if (anchor == NULL)
1747 return 0;
1748 }
1749 if (loop == NULL || end == NULL)
1750 return 0;
1751 if (s->v.While.orelse) {
1752 orelse = compiler_new_block(c);
1753 if (orelse == NULL)
1754 return 0;
1755 }
1756 else
1757 orelse = NULL;
1758
1759 ADDOP_JREL(c, SETUP_LOOP, end);
1760 compiler_use_next_block(c, loop);
1761 if (!compiler_push_fblock(c, LOOP, loop))
1762 return 0;
1763 if (constant == -1) {
1764 VISIT(c, expr, s->v.While.test);
1765 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1766 ADDOP(c, POP_TOP);
1767 }
1768 VISIT_SEQ(c, stmt, s->v.While.body);
1769 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1770
1771 /* XXX should the two POP instructions be in a separate block
1772 if there is no else clause ?
1773 */
1774
1775 if (constant == -1) {
1776 compiler_use_next_block(c, anchor);
1777 ADDOP(c, POP_TOP);
1778 ADDOP(c, POP_BLOCK);
1779 }
1780 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 VISIT_SEQ(c, stmt, s->v.While.orelse);
1783 compiler_use_next_block(c, end);
1784
1785 return 1;
1786}
1787
1788static int
1789compiler_continue(struct compiler *c)
1790{
1791 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001792 static const char IN_FINALLY_ERROR_MSG[] =
1793 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 int i;
1795
1796 if (!c->u->u_nfblocks)
1797 return compiler_error(c, LOOP_ERROR_MSG);
1798 i = c->u->u_nfblocks - 1;
1799 switch (c->u->u_fblock[i].fb_type) {
1800 case LOOP:
1801 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1802 break;
1803 case EXCEPT:
1804 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001805 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1806 /* Prevent continue anywhere under a finally
1807 even if hidden in a sub-try or except. */
1808 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1809 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1810 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (i == -1)
1812 return compiler_error(c, LOOP_ERROR_MSG);
1813 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1814 break;
1815 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001816 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 }
1818
1819 return 1;
1820}
1821
1822/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1823
1824 SETUP_FINALLY L
1825 <code for body>
1826 POP_BLOCK
1827 LOAD_CONST <None>
1828 L: <code for finalbody>
1829 END_FINALLY
1830
1831 The special instructions use the block stack. Each block
1832 stack entry contains the instruction that created it (here
1833 SETUP_FINALLY), the level of the value stack at the time the
1834 block stack entry was created, and a label (here L).
1835
1836 SETUP_FINALLY:
1837 Pushes the current value stack level and the label
1838 onto the block stack.
1839 POP_BLOCK:
1840 Pops en entry from the block stack, and pops the value
1841 stack until its level is the same as indicated on the
1842 block stack. (The label is ignored.)
1843 END_FINALLY:
1844 Pops a variable number of entries from the *value* stack
1845 and re-raises the exception they specify. The number of
1846 entries popped depends on the (pseudo) exception type.
1847
1848 The block stack is unwound when an exception is raised:
1849 when a SETUP_FINALLY entry is found, the exception is pushed
1850 onto the value stack (and the exception condition is cleared),
1851 and the interpreter jumps to the label gotten from the block
1852 stack.
1853*/
1854
1855static int
1856compiler_try_finally(struct compiler *c, stmt_ty s)
1857{
1858 basicblock *body, *end;
1859 body = compiler_new_block(c);
1860 end = compiler_new_block(c);
1861 if (body == NULL || end == NULL)
1862 return 0;
1863
1864 ADDOP_JREL(c, SETUP_FINALLY, end);
1865 compiler_use_next_block(c, body);
1866 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1867 return 0;
1868 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1869 ADDOP(c, POP_BLOCK);
1870 compiler_pop_fblock(c, FINALLY_TRY, body);
1871
1872 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1873 compiler_use_next_block(c, end);
1874 if (!compiler_push_fblock(c, FINALLY_END, end))
1875 return 0;
1876 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1877 ADDOP(c, END_FINALLY);
1878 compiler_pop_fblock(c, FINALLY_END, end);
1879
1880 return 1;
1881}
1882
1883/*
1884 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1885 (The contents of the value stack is shown in [], with the top
1886 at the right; 'tb' is trace-back info, 'val' the exception's
1887 associated value, and 'exc' the exception.)
1888
1889 Value stack Label Instruction Argument
1890 [] SETUP_EXCEPT L1
1891 [] <code for S>
1892 [] POP_BLOCK
1893 [] JUMP_FORWARD L0
1894
1895 [tb, val, exc] L1: DUP )
1896 [tb, val, exc, exc] <evaluate E1> )
1897 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1898 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1899 [tb, val, exc, 1] POP )
1900 [tb, val, exc] POP
1901 [tb, val] <assign to V1> (or POP if no V1)
1902 [tb] POP
1903 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001904 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
1906 [tb, val, exc, 0] L2: POP
1907 [tb, val, exc] DUP
1908 .............................etc.......................
1909
1910 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001911 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
1913 [] L0: <next statement>
1914
1915 Of course, parts are not generated if Vi or Ei is not present.
1916*/
1917static int
1918compiler_try_except(struct compiler *c, stmt_ty s)
1919{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001920 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 int i, n;
1922
1923 body = compiler_new_block(c);
1924 except = compiler_new_block(c);
1925 orelse = compiler_new_block(c);
1926 end = compiler_new_block(c);
1927 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1928 return 0;
1929 ADDOP_JREL(c, SETUP_EXCEPT, except);
1930 compiler_use_next_block(c, body);
1931 if (!compiler_push_fblock(c, EXCEPT, body))
1932 return 0;
1933 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1934 ADDOP(c, POP_BLOCK);
1935 compiler_pop_fblock(c, EXCEPT, body);
1936 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1937 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1938 compiler_use_next_block(c, except);
1939 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 s->v.TryExcept.handlers, i);
1942 if (!handler->type && i < n-1)
1943 return compiler_error(c, "default 'except:' must be last");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001944 c->u->u_lineno_set = false;
1945 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 except = compiler_new_block(c);
1947 if (except == NULL)
1948 return 0;
1949 if (handler->type) {
1950 ADDOP(c, DUP_TOP);
1951 VISIT(c, expr, handler->type);
1952 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1953 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1954 ADDOP(c, POP_TOP);
1955 }
1956 ADDOP(c, POP_TOP);
1957 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001958 basicblock *cleanup_end, *cleanup_body;
1959 expr_context_ty orig_ctx;
1960
1961 assert(handler->name->kind == Name_kind);
1962
1963 cleanup_end = compiler_new_block(c);
1964 cleanup_body = compiler_new_block(c);
1965 if(!(cleanup_end || cleanup_body))
1966 return 0;
1967
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968 VISIT(c, expr, handler->name);
Guido van Rossumb940e112007-01-10 16:19:56 +00001969 ADDOP(c, POP_TOP);
1970
1971 /*
1972 try:
1973 # body
1974 except type as name:
1975 try:
1976 # body
1977 finally:
1978 name = None
1979 del name
1980 */
1981
1982 /* second try: */
1983 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1984 compiler_use_next_block(c, cleanup_body);
1985 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1986 return 0;
1987
1988 /* second # body */
1989 VISIT_SEQ(c, stmt, handler->body);
1990 ADDOP(c, POP_BLOCK);
1991 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1992
1993 /* finally: */
1994 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1995 compiler_use_next_block(c, cleanup_end);
1996 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1997 return 0;
1998
1999 /* name = None */
2000 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2001 orig_ctx = handler->name->v.Name.ctx;
2002 handler->name->v.Name.ctx = Store;
2003 VISIT(c, expr, handler->name);
2004
2005 /* del name */
2006 handler->name->v.Name.ctx = Del;
2007 VISIT(c, expr, handler->name);
2008 handler->name->v.Name.ctx = orig_ctx;
2009
2010 ADDOP(c, END_FINALLY);
2011 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 }
2013 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002014 ADDOP(c, POP_TOP);
2015 ADDOP(c, POP_TOP);
2016 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 ADDOP_JREL(c, JUMP_FORWARD, end);
2019 compiler_use_next_block(c, except);
2020 if (handler->type)
2021 ADDOP(c, POP_TOP);
2022 }
2023 ADDOP(c, END_FINALLY);
2024 compiler_use_next_block(c, orelse);
2025 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2026 compiler_use_next_block(c, end);
2027 return 1;
2028}
2029
2030static int
2031compiler_import_as(struct compiler *c, identifier name, identifier asname)
2032{
2033 /* The IMPORT_NAME opcode was already generated. This function
2034 merely needs to bind the result to a name.
2035
2036 If there is a dot in name, we need to split it and emit a
2037 LOAD_ATTR for each name.
2038 */
2039 const char *src = PyString_AS_STRING(name);
2040 const char *dot = strchr(src, '.');
2041 if (dot) {
2042 /* Consume the base module name to get the first attribute */
2043 src = dot + 1;
2044 while (dot) {
2045 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002046 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002048 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002049 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002050 if (!attr)
2051 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002053 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 src = dot + 1;
2055 }
2056 }
2057 return compiler_nameop(c, asname, Store);
2058}
2059
2060static int
2061compiler_import(struct compiler *c, stmt_ty s)
2062{
2063 /* The Import node stores a module name like a.b.c as a single
2064 string. This is convenient for all cases except
2065 import a.b.c as d
2066 where we need to parse that string to extract the individual
2067 module names.
2068 XXX Perhaps change the representation to make this case simpler?
2069 */
2070 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002072 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002073 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002075 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076
Guido van Rossum45aecf42006-03-15 04:58:47 +00002077 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002078 if (level == NULL)
2079 return 0;
2080
2081 ADDOP_O(c, LOAD_CONST, level, consts);
2082 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2084 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2085
2086 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002087 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002088 if (!r)
2089 return r;
2090 }
2091 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 identifier tmp = alias->name;
2093 const char *base = PyString_AS_STRING(alias->name);
2094 char *dot = strchr(base, '.');
2095 if (dot)
2096 tmp = PyString_FromStringAndSize(base,
2097 dot - base);
2098 r = compiler_nameop(c, tmp, Store);
2099 if (dot) {
2100 Py_DECREF(tmp);
2101 }
2102 if (!r)
2103 return r;
2104 }
2105 }
2106 return 1;
2107}
2108
2109static int
2110compiler_from_import(struct compiler *c, stmt_ty s)
2111{
2112 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113
2114 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002115 PyObject *level;
2116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 if (!names)
2118 return 0;
2119
Guido van Rossum45aecf42006-03-15 04:58:47 +00002120 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002121 if (!level) {
2122 Py_DECREF(names);
2123 return 0;
2124 }
2125
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 /* build up the names */
2127 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002128 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 Py_INCREF(alias->name);
2130 PyTuple_SET_ITEM(names, i, alias->name);
2131 }
2132
2133 if (s->lineno > c->c_future->ff_lineno) {
2134 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2135 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002136 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 Py_DECREF(names);
2138 return compiler_error(c,
2139 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002140 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141
2142 }
2143 }
2144
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002145 ADDOP_O(c, LOAD_CONST, level, consts);
2146 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002148 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2150 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002151 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 identifier store_name;
2153
2154 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2155 assert(n == 1);
2156 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002157 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 }
2159
2160 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2161 store_name = alias->name;
2162 if (alias->asname)
2163 store_name = alias->asname;
2164
2165 if (!compiler_nameop(c, store_name, Store)) {
2166 Py_DECREF(names);
2167 return 0;
2168 }
2169 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002170 /* remove imported module */
2171 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 return 1;
2173}
2174
2175static int
2176compiler_assert(struct compiler *c, stmt_ty s)
2177{
2178 static PyObject *assertion_error = NULL;
2179 basicblock *end;
2180
2181 if (Py_OptimizeFlag)
2182 return 1;
2183 if (assertion_error == NULL) {
2184 assertion_error = PyString_FromString("AssertionError");
2185 if (assertion_error == NULL)
2186 return 0;
2187 }
2188 VISIT(c, expr, s->v.Assert.test);
2189 end = compiler_new_block(c);
2190 if (end == NULL)
2191 return 0;
2192 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2193 ADDOP(c, POP_TOP);
2194 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2195 if (s->v.Assert.msg) {
2196 VISIT(c, expr, s->v.Assert.msg);
2197 ADDOP_I(c, RAISE_VARARGS, 2);
2198 }
2199 else {
2200 ADDOP_I(c, RAISE_VARARGS, 1);
2201 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002202 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 ADDOP(c, POP_TOP);
2204 return 1;
2205}
2206
2207static int
2208compiler_visit_stmt(struct compiler *c, stmt_ty s)
2209{
2210 int i, n;
2211
Thomas Wouters89f507f2006-12-13 04:49:30 +00002212 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 c->u->u_lineno = s->lineno;
2214 c->u->u_lineno_set = false;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002215
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002217 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002219 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002221 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222 if (c->u->u_ste->ste_type != FunctionBlock)
2223 return compiler_error(c, "'return' outside function");
2224 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 VISIT(c, expr, s->v.Return.value);
2226 }
2227 else
2228 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2229 ADDOP(c, RETURN_VALUE);
2230 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002231 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 VISIT_SEQ(c, expr, s->v.Delete.targets)
2233 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002234 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235 n = asdl_seq_LEN(s->v.Assign.targets);
2236 VISIT(c, expr, s->v.Assign.value);
2237 for (i = 0; i < n; i++) {
2238 if (i < n - 1)
2239 ADDOP(c, DUP_TOP);
2240 VISIT(c, expr,
2241 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2242 }
2243 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002244 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002246 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002248 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002250 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002252 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002254 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 n = 0;
2256 if (s->v.Raise.type) {
2257 VISIT(c, expr, s->v.Raise.type);
2258 n++;
2259 if (s->v.Raise.inst) {
2260 VISIT(c, expr, s->v.Raise.inst);
2261 n++;
2262 if (s->v.Raise.tback) {
2263 VISIT(c, expr, s->v.Raise.tback);
2264 n++;
2265 }
2266 }
2267 }
2268 ADDOP_I(c, RAISE_VARARGS, n);
2269 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002270 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002272 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002274 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002276 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002278 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002280 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002282 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002284 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 ADDOP(c, PRINT_EXPR);
2286 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002287 else if (s->v.Expr.value->kind != Str_kind &&
2288 s->v.Expr.value->kind != Num_kind) {
2289 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 ADDOP(c, POP_TOP);
2291 }
2292 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002293 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002295 case Break_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002296 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 return compiler_error(c, "'break' outside loop");
2298 ADDOP(c, BREAK_LOOP);
2299 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002300 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002302 case With_kind:
2303 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 }
2305 return 1;
2306}
2307
2308static int
2309unaryop(unaryop_ty op)
2310{
2311 switch (op) {
2312 case Invert:
2313 return UNARY_INVERT;
2314 case Not:
2315 return UNARY_NOT;
2316 case UAdd:
2317 return UNARY_POSITIVE;
2318 case USub:
2319 return UNARY_NEGATIVE;
2320 }
2321 return 0;
2322}
2323
2324static int
2325binop(struct compiler *c, operator_ty op)
2326{
2327 switch (op) {
2328 case Add:
2329 return BINARY_ADD;
2330 case Sub:
2331 return BINARY_SUBTRACT;
2332 case Mult:
2333 return BINARY_MULTIPLY;
2334 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002335 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 case Mod:
2337 return BINARY_MODULO;
2338 case Pow:
2339 return BINARY_POWER;
2340 case LShift:
2341 return BINARY_LSHIFT;
2342 case RShift:
2343 return BINARY_RSHIFT;
2344 case BitOr:
2345 return BINARY_OR;
2346 case BitXor:
2347 return BINARY_XOR;
2348 case BitAnd:
2349 return BINARY_AND;
2350 case FloorDiv:
2351 return BINARY_FLOOR_DIVIDE;
2352 }
2353 return 0;
2354}
2355
2356static int
2357cmpop(cmpop_ty op)
2358{
2359 switch (op) {
2360 case Eq:
2361 return PyCmp_EQ;
2362 case NotEq:
2363 return PyCmp_NE;
2364 case Lt:
2365 return PyCmp_LT;
2366 case LtE:
2367 return PyCmp_LE;
2368 case Gt:
2369 return PyCmp_GT;
2370 case GtE:
2371 return PyCmp_GE;
2372 case Is:
2373 return PyCmp_IS;
2374 case IsNot:
2375 return PyCmp_IS_NOT;
2376 case In:
2377 return PyCmp_IN;
2378 case NotIn:
2379 return PyCmp_NOT_IN;
2380 }
2381 return PyCmp_BAD;
2382}
2383
2384static int
2385inplace_binop(struct compiler *c, operator_ty op)
2386{
2387 switch (op) {
2388 case Add:
2389 return INPLACE_ADD;
2390 case Sub:
2391 return INPLACE_SUBTRACT;
2392 case Mult:
2393 return INPLACE_MULTIPLY;
2394 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002395 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 case Mod:
2397 return INPLACE_MODULO;
2398 case Pow:
2399 return INPLACE_POWER;
2400 case LShift:
2401 return INPLACE_LSHIFT;
2402 case RShift:
2403 return INPLACE_RSHIFT;
2404 case BitOr:
2405 return INPLACE_OR;
2406 case BitXor:
2407 return INPLACE_XOR;
2408 case BitAnd:
2409 return INPLACE_AND;
2410 case FloorDiv:
2411 return INPLACE_FLOOR_DIVIDE;
2412 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002413 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002414 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 return 0;
2416}
2417
2418static int
2419compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2420{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002421 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002422 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2423
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002424 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002425 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002426 /* XXX AugStore isn't used anywhere! */
2427
2428 /* First check for assignment to __debug__. Param? */
2429 if ((ctx == Store || ctx == AugStore || ctx == Del)
2430 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2431 return compiler_error(c, "can not assign to __debug__");
2432 }
2433
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002434 mangled = _Py_Mangle(c->u->u_private, name);
2435 if (!mangled)
2436 return 0;
2437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 op = 0;
2439 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002440 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 switch (scope) {
2442 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002443 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 optype = OP_DEREF;
2445 break;
2446 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002447 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 optype = OP_DEREF;
2449 break;
2450 case LOCAL:
2451 if (c->u->u_ste->ste_type == FunctionBlock)
2452 optype = OP_FAST;
2453 break;
2454 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002455 if (c->u->u_ste->ste_type == FunctionBlock &&
2456 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002457 optype = OP_GLOBAL;
2458 break;
2459 case GLOBAL_EXPLICIT:
2460 optype = OP_GLOBAL;
2461 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002462 default:
2463 /* scope can be 0 */
2464 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 }
2466
2467 /* XXX Leave assert here, but handle __doc__ and the like better */
2468 assert(scope || PyString_AS_STRING(name)[0] == '_');
2469
2470 switch (optype) {
2471 case OP_DEREF:
2472 switch (ctx) {
2473 case Load: op = LOAD_DEREF; break;
2474 case Store: op = STORE_DEREF; break;
2475 case AugLoad:
2476 case AugStore:
2477 break;
2478 case Del:
2479 PyErr_Format(PyExc_SyntaxError,
2480 "can not delete variable '%s' referenced "
2481 "in nested scope",
2482 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002483 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002486 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002487 PyErr_SetString(PyExc_SystemError,
2488 "param invalid for deref variable");
2489 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 }
2491 break;
2492 case OP_FAST:
2493 switch (ctx) {
2494 case Load: op = LOAD_FAST; break;
2495 case Store: op = STORE_FAST; break;
2496 case Del: op = DELETE_FAST; break;
2497 case AugLoad:
2498 case AugStore:
2499 break;
2500 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002501 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002502 PyErr_SetString(PyExc_SystemError,
2503 "param invalid for local variable");
2504 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002506 ADDOP_O(c, op, mangled, varnames);
2507 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 return 1;
2509 case OP_GLOBAL:
2510 switch (ctx) {
2511 case Load: op = LOAD_GLOBAL; break;
2512 case Store: op = STORE_GLOBAL; break;
2513 case Del: op = DELETE_GLOBAL; break;
2514 case AugLoad:
2515 case AugStore:
2516 break;
2517 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002518 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002519 PyErr_SetString(PyExc_SystemError,
2520 "param invalid for global variable");
2521 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
2523 break;
2524 case OP_NAME:
2525 switch (ctx) {
2526 case Load: op = LOAD_NAME; break;
2527 case Store: op = STORE_NAME; break;
2528 case Del: op = DELETE_NAME; break;
2529 case AugLoad:
2530 case AugStore:
2531 break;
2532 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002533 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002534 PyErr_SetString(PyExc_SystemError,
2535 "param invalid for name variable");
2536 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 }
2538 break;
2539 }
2540
2541 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002542 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002543 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002544 if (arg < 0)
2545 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002546 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547}
2548
2549static int
2550compiler_boolop(struct compiler *c, expr_ty e)
2551{
2552 basicblock *end;
2553 int jumpi, i, n;
2554 asdl_seq *s;
2555
2556 assert(e->kind == BoolOp_kind);
2557 if (e->v.BoolOp.op == And)
2558 jumpi = JUMP_IF_FALSE;
2559 else
2560 jumpi = JUMP_IF_TRUE;
2561 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002562 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 return 0;
2564 s = e->v.BoolOp.values;
2565 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002566 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002568 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 ADDOP_JREL(c, jumpi, end);
2570 ADDOP(c, POP_TOP)
2571 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002572 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002573 compiler_use_next_block(c, end);
2574 return 1;
2575}
2576
2577static int
2578compiler_list(struct compiler *c, expr_ty e)
2579{
2580 int n = asdl_seq_LEN(e->v.List.elts);
2581 if (e->v.List.ctx == Store) {
2582 ADDOP_I(c, UNPACK_SEQUENCE, n);
2583 }
2584 VISIT_SEQ(c, expr, e->v.List.elts);
2585 if (e->v.List.ctx == Load) {
2586 ADDOP_I(c, BUILD_LIST, n);
2587 }
2588 return 1;
2589}
2590
2591static int
2592compiler_tuple(struct compiler *c, expr_ty e)
2593{
2594 int n = asdl_seq_LEN(e->v.Tuple.elts);
2595 if (e->v.Tuple.ctx == Store) {
2596 ADDOP_I(c, UNPACK_SEQUENCE, n);
2597 }
2598 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2599 if (e->v.Tuple.ctx == Load) {
2600 ADDOP_I(c, BUILD_TUPLE, n);
2601 }
2602 return 1;
2603}
2604
2605static int
2606compiler_compare(struct compiler *c, expr_ty e)
2607{
2608 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002609 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610
2611 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2612 VISIT(c, expr, e->v.Compare.left);
2613 n = asdl_seq_LEN(e->v.Compare.ops);
2614 assert(n > 0);
2615 if (n > 1) {
2616 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002617 if (cleanup == NULL)
2618 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002619 VISIT(c, expr,
2620 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 }
2622 for (i = 1; i < n; i++) {
2623 ADDOP(c, DUP_TOP);
2624 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002626 cmpop((cmpop_ty)(asdl_seq_GET(
2627 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2629 NEXT_BLOCK(c);
2630 ADDOP(c, POP_TOP);
2631 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002632 VISIT(c, expr,
2633 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002635 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002637 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 if (n > 1) {
2639 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002640 if (end == NULL)
2641 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 ADDOP_JREL(c, JUMP_FORWARD, end);
2643 compiler_use_next_block(c, cleanup);
2644 ADDOP(c, ROT_TWO);
2645 ADDOP(c, POP_TOP);
2646 compiler_use_next_block(c, end);
2647 }
2648 return 1;
2649}
2650
2651static int
2652compiler_call(struct compiler *c, expr_ty e)
2653{
2654 int n, code = 0;
2655
2656 VISIT(c, expr, e->v.Call.func);
2657 n = asdl_seq_LEN(e->v.Call.args);
2658 VISIT_SEQ(c, expr, e->v.Call.args);
2659 if (e->v.Call.keywords) {
2660 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2661 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2662 }
2663 if (e->v.Call.starargs) {
2664 VISIT(c, expr, e->v.Call.starargs);
2665 code |= 1;
2666 }
2667 if (e->v.Call.kwargs) {
2668 VISIT(c, expr, e->v.Call.kwargs);
2669 code |= 2;
2670 }
2671 switch (code) {
2672 case 0:
2673 ADDOP_I(c, CALL_FUNCTION, n);
2674 break;
2675 case 1:
2676 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2677 break;
2678 case 2:
2679 ADDOP_I(c, CALL_FUNCTION_KW, n);
2680 break;
2681 case 3:
2682 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2683 break;
2684 }
2685 return 1;
2686}
2687
2688static int
2689compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002690 asdl_seq *generators, int gen_index,
2691 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692{
2693 /* generate code for the iterator, then each of the ifs,
2694 and then write to the element */
2695
2696 comprehension_ty l;
2697 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002698 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699
2700 start = compiler_new_block(c);
2701 skip = compiler_new_block(c);
2702 if_cleanup = compiler_new_block(c);
2703 anchor = compiler_new_block(c);
2704
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002705 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2706 anchor == NULL)
2707 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002709 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 VISIT(c, expr, l->iter);
2711 ADDOP(c, GET_ITER);
2712 compiler_use_next_block(c, start);
2713 ADDOP_JREL(c, FOR_ITER, anchor);
2714 NEXT_BLOCK(c);
2715 VISIT(c, expr, l->target);
2716
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002717 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 n = asdl_seq_LEN(l->ifs);
2719 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002720 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 VISIT(c, expr, e);
2722 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2723 NEXT_BLOCK(c);
2724 ADDOP(c, POP_TOP);
2725 }
2726
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002727 if (++gen_index < asdl_seq_LEN(generators))
2728 if (!compiler_listcomp_generator(c, tmpname,
2729 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002732 /* only append after the last for generator */
2733 if (gen_index >= asdl_seq_LEN(generators)) {
2734 if (!compiler_nameop(c, tmpname, Load))
2735 return 0;
2736 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002737 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002738
2739 compiler_use_next_block(c, skip);
2740 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 for (i = 0; i < n; i++) {
2742 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002743 if (i == 0)
2744 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 ADDOP(c, POP_TOP);
2746 }
2747 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2748 compiler_use_next_block(c, anchor);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002749 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002751 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 return 0;
2753
2754 return 1;
2755}
2756
2757static int
2758compiler_listcomp(struct compiler *c, expr_ty e)
2759{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002761 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 asdl_seq *generators = e->v.ListComp.generators;
2763
2764 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002765 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 if (!tmp)
2767 return 0;
2768 ADDOP_I(c, BUILD_LIST, 0);
2769 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002771 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2772 e->v.ListComp.elt);
2773 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 return rc;
2775}
2776
2777static int
2778compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002779 asdl_seq *generators, int gen_index,
2780 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781{
2782 /* generate code for the iterator, then each of the ifs,
2783 and then write to the element */
2784
2785 comprehension_ty ge;
2786 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002787 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788
2789 start = compiler_new_block(c);
2790 skip = compiler_new_block(c);
2791 if_cleanup = compiler_new_block(c);
2792 anchor = compiler_new_block(c);
2793 end = compiler_new_block(c);
2794
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002795 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002796 anchor == NULL || end == NULL)
2797 return 0;
2798
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002799 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 ADDOP_JREL(c, SETUP_LOOP, end);
2801 if (!compiler_push_fblock(c, LOOP, start))
2802 return 0;
2803
2804 if (gen_index == 0) {
2805 /* Receive outermost iter as an implicit argument */
2806 c->u->u_argcount = 1;
2807 ADDOP_I(c, LOAD_FAST, 0);
2808 }
2809 else {
2810 /* Sub-iter - calculate on the fly */
2811 VISIT(c, expr, ge->iter);
2812 ADDOP(c, GET_ITER);
2813 }
2814 compiler_use_next_block(c, start);
2815 ADDOP_JREL(c, FOR_ITER, anchor);
2816 NEXT_BLOCK(c);
2817 VISIT(c, expr, ge->target);
2818
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002819 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 n = asdl_seq_LEN(ge->ifs);
2821 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002822 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 VISIT(c, expr, e);
2824 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2825 NEXT_BLOCK(c);
2826 ADDOP(c, POP_TOP);
2827 }
2828
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002829 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2831 return 0;
2832
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002833 /* only append after the last 'for' generator */
2834 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835 VISIT(c, expr, elt);
2836 ADDOP(c, YIELD_VALUE);
2837 ADDOP(c, POP_TOP);
2838
2839 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 for (i = 0; i < n; i++) {
2842 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002843 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 compiler_use_next_block(c, if_cleanup);
2845
2846 ADDOP(c, POP_TOP);
2847 }
2848 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2849 compiler_use_next_block(c, anchor);
2850 ADDOP(c, POP_BLOCK);
2851 compiler_pop_fblock(c, LOOP, start);
2852 compiler_use_next_block(c, end);
2853
2854 return 1;
2855}
2856
2857static int
2858compiler_genexp(struct compiler *c, expr_ty e)
2859{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002860 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002861 PyCodeObject *co;
2862 expr_ty outermost_iter = ((comprehension_ty)
2863 (asdl_seq_GET(e->v.GeneratorExp.generators,
2864 0)))->iter;
2865
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002866 if (!name) {
2867 name = PyString_FromString("<genexpr>");
2868 if (!name)
2869 return 0;
2870 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
2872 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2873 return 0;
2874 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2875 e->v.GeneratorExp.elt);
2876 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002877 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878 if (co == NULL)
2879 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002881 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002882 Py_DECREF(co);
2883
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 VISIT(c, expr, outermost_iter);
2885 ADDOP(c, GET_ITER);
2886 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887
2888 return 1;
2889}
2890
2891static int
2892compiler_visit_keyword(struct compiler *c, keyword_ty k)
2893{
2894 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2895 VISIT(c, expr, k->value);
2896 return 1;
2897}
2898
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002899/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 whether they are true or false.
2901
2902 Return values: 1 for true, 0 for false, -1 for non-constant.
2903 */
2904
2905static int
2906expr_constant(expr_ty e)
2907{
2908 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002909 case Ellipsis_kind:
2910 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 case Num_kind:
2912 return PyObject_IsTrue(e->v.Num.n);
2913 case Str_kind:
2914 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002915 case Name_kind:
2916 /* __debug__ is not assignable, so we can optimize
2917 * it away in if and while statements */
2918 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2919 "__debug__") == 0)
2920 return ! Py_OptimizeFlag;
2921 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002922 default:
2923 return -1;
2924 }
2925}
2926
Guido van Rossumc2e20742006-02-27 22:32:47 +00002927/*
2928 Implements the with statement from PEP 343.
2929
2930 The semantics outlined in that PEP are as follows:
2931
2932 with EXPR as VAR:
2933 BLOCK
2934
2935 It is implemented roughly as:
2936
Thomas Wouters477c8d52006-05-27 19:21:47 +00002937 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002938 exit = context.__exit__ # not calling it
2939 value = context.__enter__()
2940 try:
2941 VAR = value # if VAR present in the syntax
2942 BLOCK
2943 finally:
2944 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002945 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002946 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002947 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002948 exit(*exc)
2949 */
2950static int
2951compiler_with(struct compiler *c, stmt_ty s)
2952{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002953 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002954 basicblock *block, *finally;
2955 identifier tmpexit, tmpvalue = NULL;
2956
2957 assert(s->kind == With_kind);
2958
Guido van Rossumc2e20742006-02-27 22:32:47 +00002959 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002960 enter_attr = PyString_InternFromString("__enter__");
2961 if (!enter_attr)
2962 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002963 }
2964 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002965 exit_attr = PyString_InternFromString("__exit__");
2966 if (!exit_attr)
2967 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002968 }
2969
2970 block = compiler_new_block(c);
2971 finally = compiler_new_block(c);
2972 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002973 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002974
2975 /* Create a temporary variable to hold context.__exit__ */
2976 tmpexit = compiler_new_tmpname(c);
2977 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002978 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002979 PyArena_AddPyObject(c->c_arena, tmpexit);
2980
2981 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002982 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002983 We need to do this rather than preserving it on the stack
2984 because SETUP_FINALLY remembers the stack level.
2985 We need to do the assignment *inside* the try/finally
2986 so that context.__exit__() is called when the assignment
2987 fails. But we need to call context.__enter__() *before*
2988 the try/finally so that if it fails we won't call
2989 context.__exit__().
2990 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002991 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002992 if (tmpvalue == NULL)
2993 return 0;
2994 PyArena_AddPyObject(c->c_arena, tmpvalue);
2995 }
2996
Thomas Wouters477c8d52006-05-27 19:21:47 +00002997 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002999
3000 /* Squirrel away context.__exit__ */
3001 ADDOP(c, DUP_TOP);
3002 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3003 if (!compiler_nameop(c, tmpexit, Store))
3004 return 0;
3005
3006 /* Call context.__enter__() */
3007 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3008 ADDOP_I(c, CALL_FUNCTION, 0);
3009
3010 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003011 /* Store it in tmpvalue */
3012 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003013 return 0;
3014 }
3015 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003016 /* Discard result from context.__enter__() */
3017 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003018 }
3019
3020 /* Start the try block */
3021 ADDOP_JREL(c, SETUP_FINALLY, finally);
3022
3023 compiler_use_next_block(c, block);
3024 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003025 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003026 }
3027
3028 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003029 /* Bind saved result of context.__enter__() to VAR */
3030 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003031 !compiler_nameop(c, tmpvalue, Del))
3032 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003033 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003034 }
3035
3036 /* BLOCK code */
3037 VISIT_SEQ(c, stmt, s->v.With.body);
3038
3039 /* End of try block; start the finally block */
3040 ADDOP(c, POP_BLOCK);
3041 compiler_pop_fblock(c, FINALLY_TRY, block);
3042
3043 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3044 compiler_use_next_block(c, finally);
3045 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003046 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003047
3048 /* Finally block starts; push tmpexit and issue our magic opcode. */
3049 if (!compiler_nameop(c, tmpexit, Load) ||
3050 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003051 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003052 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003053
3054 /* Finally block ends. */
3055 ADDOP(c, END_FINALLY);
3056 compiler_pop_fblock(c, FINALLY_END, finally);
3057 return 1;
3058}
3059
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060static int
3061compiler_visit_expr(struct compiler *c, expr_ty e)
3062{
3063 int i, n;
3064
Thomas Wouters89f507f2006-12-13 04:49:30 +00003065 /* If expr e has a different line number than the last expr/stmt,
3066 set a new line number for the next instruction.
3067 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 if (e->lineno > c->u->u_lineno) {
3069 c->u->u_lineno = e->lineno;
3070 c->u->u_lineno_set = false;
3071 }
3072 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003073 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003075 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 VISIT(c, expr, e->v.BinOp.left);
3077 VISIT(c, expr, e->v.BinOp.right);
3078 ADDOP(c, binop(c, e->v.BinOp.op));
3079 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003080 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081 VISIT(c, expr, e->v.UnaryOp.operand);
3082 ADDOP(c, unaryop(e->v.UnaryOp.op));
3083 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003084 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003086 case IfExp_kind:
3087 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003088 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003089 /* XXX get rid of arg? */
3090 ADDOP_I(c, BUILD_MAP, 0);
3091 n = asdl_seq_LEN(e->v.Dict.values);
3092 /* We must arrange things just right for STORE_SUBSCR.
3093 It wants the stack to look like (value) (dict) (key) */
3094 for (i = 0; i < n; i++) {
3095 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003096 VISIT(c, expr,
3097 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003098 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003099 VISIT(c, expr,
3100 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101 ADDOP(c, STORE_SUBSCR);
3102 }
3103 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003104 case Set_kind:
3105 n = asdl_seq_LEN(e->v.Set.elts);
3106 VISIT_SEQ(c, expr, e->v.Set.elts);
3107 ADDOP_I(c, BUILD_SET, n);
3108 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003109 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003111 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 return compiler_genexp(c, e);
3113 case Yield_kind:
3114 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003115 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 if (e->v.Yield.value) {
3117 VISIT(c, expr, e->v.Yield.value);
3118 }
3119 else {
3120 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3121 }
3122 ADDOP(c, YIELD_VALUE);
3123 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003124 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003126 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003128 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3130 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003131 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3133 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003134 case Ellipsis_kind:
3135 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3136 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003138 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 if (e->v.Attribute.ctx != AugStore)
3140 VISIT(c, expr, e->v.Attribute.value);
3141 switch (e->v.Attribute.ctx) {
3142 case AugLoad:
3143 ADDOP(c, DUP_TOP);
3144 /* Fall through to load */
3145 case Load:
3146 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3147 break;
3148 case AugStore:
3149 ADDOP(c, ROT_TWO);
3150 /* Fall through to save */
3151 case Store:
3152 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3153 break;
3154 case Del:
3155 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3156 break;
3157 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003158 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003159 PyErr_SetString(PyExc_SystemError,
3160 "param invalid in attribute expression");
3161 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 }
3163 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003164 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165 switch (e->v.Subscript.ctx) {
3166 case AugLoad:
3167 VISIT(c, expr, e->v.Subscript.value);
3168 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3169 break;
3170 case Load:
3171 VISIT(c, expr, e->v.Subscript.value);
3172 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3173 break;
3174 case AugStore:
3175 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3176 break;
3177 case Store:
3178 VISIT(c, expr, e->v.Subscript.value);
3179 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3180 break;
3181 case Del:
3182 VISIT(c, expr, e->v.Subscript.value);
3183 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3184 break;
3185 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003186 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003187 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003188 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003189 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 }
3191 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003192 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3194 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003195 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003197 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 return compiler_tuple(c, e);
3199 }
3200 return 1;
3201}
3202
3203static int
3204compiler_augassign(struct compiler *c, stmt_ty s)
3205{
3206 expr_ty e = s->v.AugAssign.target;
3207 expr_ty auge;
3208
3209 assert(s->kind == AugAssign_kind);
3210
3211 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003212 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003214 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003215 if (auge == NULL)
3216 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217 VISIT(c, expr, auge);
3218 VISIT(c, expr, s->v.AugAssign.value);
3219 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3220 auge->v.Attribute.ctx = AugStore;
3221 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222 break;
3223 case Subscript_kind:
3224 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003225 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003226 if (auge == NULL)
3227 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 VISIT(c, expr, auge);
3229 VISIT(c, expr, s->v.AugAssign.value);
3230 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003231 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003233 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003235 if (!compiler_nameop(c, e->v.Name.id, Load))
3236 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 VISIT(c, expr, s->v.AugAssign.value);
3238 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3239 return compiler_nameop(c, e->v.Name.id, Store);
3240 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003241 PyErr_Format(PyExc_SystemError,
3242 "invalid node type (%d) for augmented assignment",
3243 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003244 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245 }
3246 return 1;
3247}
3248
3249static int
3250compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3251{
3252 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003253 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3254 PyErr_SetString(PyExc_SystemError,
3255 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 f = &c->u->u_fblock[c->u->u_nfblocks++];
3259 f->fb_type = t;
3260 f->fb_block = b;
3261 return 1;
3262}
3263
3264static void
3265compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3266{
3267 struct compiler_unit *u = c->u;
3268 assert(u->u_nfblocks > 0);
3269 u->u_nfblocks--;
3270 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3271 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3272}
3273
Thomas Wouters89f507f2006-12-13 04:49:30 +00003274static int
3275compiler_in_loop(struct compiler *c) {
3276 int i;
3277 struct compiler_unit *u = c->u;
3278 for (i = 0; i < u->u_nfblocks; ++i) {
3279 if (u->u_fblock[i].fb_type == LOOP)
3280 return 1;
3281 }
3282 return 0;
3283}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284/* Raises a SyntaxError and returns 0.
3285 If something goes wrong, a different exception may be raised.
3286*/
3287
3288static int
3289compiler_error(struct compiler *c, const char *errstr)
3290{
3291 PyObject *loc;
3292 PyObject *u = NULL, *v = NULL;
3293
3294 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3295 if (!loc) {
3296 Py_INCREF(Py_None);
3297 loc = Py_None;
3298 }
3299 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3300 Py_None, loc);
3301 if (!u)
3302 goto exit;
3303 v = Py_BuildValue("(zO)", errstr, u);
3304 if (!v)
3305 goto exit;
3306 PyErr_SetObject(PyExc_SyntaxError, v);
3307 exit:
3308 Py_DECREF(loc);
3309 Py_XDECREF(u);
3310 Py_XDECREF(v);
3311 return 0;
3312}
3313
3314static int
3315compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003316 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003318 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003319
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003320 /* XXX this code is duplicated */
3321 switch (ctx) {
3322 case AugLoad: /* fall through to Load */
3323 case Load: op = BINARY_SUBSCR; break;
3324 case AugStore:/* fall through to Store */
3325 case Store: op = STORE_SUBSCR; break;
3326 case Del: op = DELETE_SUBSCR; break;
3327 case Param:
3328 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003329 "invalid %s kind %d in subscript\n",
3330 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003331 return 0;
3332 }
3333 if (ctx == AugLoad) {
3334 ADDOP_I(c, DUP_TOPX, 2);
3335 }
3336 else if (ctx == AugStore) {
3337 ADDOP(c, ROT_THREE);
3338 }
3339 ADDOP(c, op);
3340 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
3343static int
3344compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3345{
3346 int n = 2;
3347 assert(s->kind == Slice_kind);
3348
3349 /* only handles the cases where BUILD_SLICE is emitted */
3350 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003351 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 }
3353 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003354 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003356
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003358 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359 }
3360 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003361 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362 }
3363
3364 if (s->v.Slice.step) {
3365 n++;
3366 VISIT(c, expr, s->v.Slice.step);
3367 }
3368 ADDOP_I(c, BUILD_SLICE, n);
3369 return 1;
3370}
3371
3372static int
3373compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3374{
3375 int op = 0, slice_offset = 0, stack_count = 0;
3376
3377 assert(s->v.Slice.step == NULL);
3378 if (s->v.Slice.lower) {
3379 slice_offset++;
3380 stack_count++;
3381 if (ctx != AugStore)
3382 VISIT(c, expr, s->v.Slice.lower);
3383 }
3384 if (s->v.Slice.upper) {
3385 slice_offset += 2;
3386 stack_count++;
3387 if (ctx != AugStore)
3388 VISIT(c, expr, s->v.Slice.upper);
3389 }
3390
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003391 if (ctx == AugLoad) {
3392 switch (stack_count) {
3393 case 0: ADDOP(c, DUP_TOP); break;
3394 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3395 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3396 }
3397 }
3398 else if (ctx == AugStore) {
3399 switch (stack_count) {
3400 case 0: ADDOP(c, ROT_TWO); break;
3401 case 1: ADDOP(c, ROT_THREE); break;
3402 case 2: ADDOP(c, ROT_FOUR); break;
3403 }
3404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405
3406 switch (ctx) {
3407 case AugLoad: /* fall through to Load */
3408 case Load: op = SLICE; break;
3409 case AugStore:/* fall through to Store */
3410 case Store: op = STORE_SLICE; break;
3411 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003412 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003413 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003414 PyErr_SetString(PyExc_SystemError,
3415 "param invalid in simple slice");
3416 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 }
3418
3419 ADDOP(c, op + slice_offset);
3420 return 1;
3421}
3422
3423static int
3424compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3425 expr_context_ty ctx)
3426{
3427 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428 case Slice_kind:
3429 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 case Index_kind:
3431 VISIT(c, expr, s->v.Index.value);
3432 break;
3433 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003434 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003435 PyErr_SetString(PyExc_SystemError,
3436 "extended slice invalid in nested slice");
3437 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 }
3439 return 1;
3440}
3441
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442static int
3443compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3444{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003445 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003447 case Index_kind:
3448 kindname = "index";
3449 if (ctx != AugStore) {
3450 VISIT(c, expr, s->v.Index.value);
3451 }
3452 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003454 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 if (!s->v.Slice.step)
3456 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003457 if (ctx != AugStore) {
3458 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 return 0;
3460 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003461 break;
3462 case ExtSlice_kind:
3463 kindname = "extended slice";
3464 if (ctx != AugStore) {
3465 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3466 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003467 slice_ty sub = (slice_ty)asdl_seq_GET(
3468 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003469 if (!compiler_visit_nested_slice(c, sub, ctx))
3470 return 0;
3471 }
3472 ADDOP_I(c, BUILD_TUPLE, n);
3473 }
3474 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003475 default:
3476 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003477 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003478 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003480 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481}
3482
Thomas Wouters89f507f2006-12-13 04:49:30 +00003483
3484/* End of the compiler section, beginning of the assembler section */
3485
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486/* do depth-first search of basic block graph, starting with block.
3487 post records the block indices in post-order.
3488
3489 XXX must handle implicit jumps from one block to next
3490*/
3491
Thomas Wouters89f507f2006-12-13 04:49:30 +00003492struct assembler {
3493 PyObject *a_bytecode; /* string containing bytecode */
3494 int a_offset; /* offset into bytecode */
3495 int a_nblocks; /* number of reachable blocks */
3496 basicblock **a_postorder; /* list of blocks in dfs postorder */
3497 PyObject *a_lnotab; /* string containing lnotab */
3498 int a_lnotab_off; /* offset into lnotab */
3499 int a_lineno; /* last lineno of emitted instruction */
3500 int a_lineno_off; /* bytecode offset of last lineno */
3501};
3502
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503static void
3504dfs(struct compiler *c, basicblock *b, struct assembler *a)
3505{
3506 int i;
3507 struct instr *instr = NULL;
3508
3509 if (b->b_seen)
3510 return;
3511 b->b_seen = 1;
3512 if (b->b_next != NULL)
3513 dfs(c, b->b_next, a);
3514 for (i = 0; i < b->b_iused; i++) {
3515 instr = &b->b_instr[i];
3516 if (instr->i_jrel || instr->i_jabs)
3517 dfs(c, instr->i_target, a);
3518 }
3519 a->a_postorder[a->a_nblocks++] = b;
3520}
3521
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003522static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3524{
3525 int i;
3526 struct instr *instr;
3527 if (b->b_seen || b->b_startdepth >= depth)
3528 return maxdepth;
3529 b->b_seen = 1;
3530 b->b_startdepth = depth;
3531 for (i = 0; i < b->b_iused; i++) {
3532 instr = &b->b_instr[i];
3533 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3534 if (depth > maxdepth)
3535 maxdepth = depth;
3536 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3537 if (instr->i_jrel || instr->i_jabs) {
3538 maxdepth = stackdepth_walk(c, instr->i_target,
3539 depth, maxdepth);
3540 if (instr->i_opcode == JUMP_ABSOLUTE ||
3541 instr->i_opcode == JUMP_FORWARD) {
3542 goto out; /* remaining code is dead */
3543 }
3544 }
3545 }
3546 if (b->b_next)
3547 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3548out:
3549 b->b_seen = 0;
3550 return maxdepth;
3551}
3552
3553/* Find the flow path that needs the largest stack. We assume that
3554 * cycles in the flow graph have no net effect on the stack depth.
3555 */
3556static int
3557stackdepth(struct compiler *c)
3558{
3559 basicblock *b, *entryblock;
3560 entryblock = NULL;
3561 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3562 b->b_seen = 0;
3563 b->b_startdepth = INT_MIN;
3564 entryblock = b;
3565 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003566 if (!entryblock)
3567 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568 return stackdepth_walk(c, entryblock, 0, 0);
3569}
3570
3571static int
3572assemble_init(struct assembler *a, int nblocks, int firstlineno)
3573{
3574 memset(a, 0, sizeof(struct assembler));
3575 a->a_lineno = firstlineno;
3576 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3577 if (!a->a_bytecode)
3578 return 0;
3579 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3580 if (!a->a_lnotab)
3581 return 0;
3582 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003583 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003584 if (!a->a_postorder) {
3585 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 return 1;
3589}
3590
3591static void
3592assemble_free(struct assembler *a)
3593{
3594 Py_XDECREF(a->a_bytecode);
3595 Py_XDECREF(a->a_lnotab);
3596 if (a->a_postorder)
3597 PyObject_Free(a->a_postorder);
3598}
3599
3600/* Return the size of a basic block in bytes. */
3601
3602static int
3603instrsize(struct instr *instr)
3604{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003605 if (!instr->i_hasarg)
3606 return 1;
3607 if (instr->i_oparg > 0xffff)
3608 return 6;
3609 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610}
3611
3612static int
3613blocksize(basicblock *b)
3614{
3615 int i;
3616 int size = 0;
3617
3618 for (i = 0; i < b->b_iused; i++)
3619 size += instrsize(&b->b_instr[i]);
3620 return size;
3621}
3622
3623/* All about a_lnotab.
3624
3625c_lnotab is an array of unsigned bytes disguised as a Python string.
3626It is used to map bytecode offsets to source code line #s (when needed
3627for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003628
Tim Peters2a7f3842001-06-09 09:26:21 +00003629The array is conceptually a list of
3630 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003631pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003632
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003633 byte code offset source code line number
3634 0 1
3635 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003636 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003637 350 307
3638 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003639
3640The first trick is that these numbers aren't stored, only the increments
3641from one row to the next (this doesn't really work, but it's a start):
3642
3643 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3644
3645The second trick is that an unsigned byte can't hold negative values, or
3646values larger than 255, so (a) there's a deep assumption that byte code
3647offsets and their corresponding line #s both increase monotonically, and (b)
3648if at least one column jumps by more than 255 from one row to the next, more
3649than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003650from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003651part. A user of c_lnotab desiring to find the source line number
3652corresponding to a bytecode address A should do something like this
3653
3654 lineno = addr = 0
3655 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003656 addr += addr_incr
3657 if addr > A:
3658 return lineno
3659 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003660
3661In order for this to work, when the addr field increments by more than 255,
3662the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003663increment is < 256. So, in the example above, assemble_lnotab (it used
3664to be called com_set_lineno) should not (as was actually done until 2.2)
3665expand 300, 300 to 255, 255, 45, 45,
3666 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003667*/
3668
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003669static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003671{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 int d_bytecode, d_lineno;
3673 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003674 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675
3676 d_bytecode = a->a_offset - a->a_lineno_off;
3677 d_lineno = i->i_lineno - a->a_lineno;
3678
3679 assert(d_bytecode >= 0);
3680 assert(d_lineno >= 0);
3681
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003682 /* XXX(nnorwitz): is there a better way to handle this?
3683 for loops are special, we want to be able to trace them
3684 each time around, so we need to set an extra line number. */
3685 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003686 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003689 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 nbytes = a->a_lnotab_off + 2 * ncodes;
3691 len = PyString_GET_SIZE(a->a_lnotab);
3692 if (nbytes >= len) {
3693 if (len * 2 < nbytes)
3694 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003695 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 len *= 2;
3697 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3698 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003699 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003700 lnotab = (unsigned char *)
3701 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003702 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 *lnotab++ = 255;
3704 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003705 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 d_bytecode -= ncodes * 255;
3707 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 assert(d_bytecode <= 255);
3710 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003711 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 nbytes = a->a_lnotab_off + 2 * ncodes;
3713 len = PyString_GET_SIZE(a->a_lnotab);
3714 if (nbytes >= len) {
3715 if (len * 2 < nbytes)
3716 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003717 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 len *= 2;
3719 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3720 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003721 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003722 lnotab = (unsigned char *)
3723 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003725 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003726 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003727 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003729 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003730 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 d_lineno -= ncodes * 255;
3732 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003733 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003734
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 len = PyString_GET_SIZE(a->a_lnotab);
3736 if (a->a_lnotab_off + 2 >= len) {
3737 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003738 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003739 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003740 lnotab = (unsigned char *)
3741 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003742
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003743 a->a_lnotab_off += 2;
3744 if (d_bytecode) {
3745 *lnotab++ = d_bytecode;
3746 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003747 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003748 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 *lnotab++ = 0;
3750 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003751 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 a->a_lineno = i->i_lineno;
3753 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003754 return 1;
3755}
3756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757/* assemble_emit()
3758 Extend the bytecode with a new instruction.
3759 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003760*/
3761
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003762static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003764{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003765 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003766 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 char *code;
3768
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003769 size = instrsize(i);
3770 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003772 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003775 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 if (a->a_offset + size >= len) {
3777 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003778 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3781 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003782 if (size == 6) {
3783 assert(i->i_hasarg);
3784 *code++ = (char)EXTENDED_ARG;
3785 *code++ = ext & 0xff;
3786 *code++ = ext >> 8;
3787 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003788 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003790 if (i->i_hasarg) {
3791 assert(size == 3 || size == 6);
3792 *code++ = arg & 0xff;
3793 *code++ = arg >> 8;
3794 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003796}
3797
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003798static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003799assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003800{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003802 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003803 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003804
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 /* Compute the size of each block and fixup jump args.
3806 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003807start:
3808 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003810 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811 bsize = blocksize(b);
3812 b->b_offset = totsize;
3813 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003814 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003815 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3817 bsize = b->b_offset;
3818 for (i = 0; i < b->b_iused; i++) {
3819 struct instr *instr = &b->b_instr[i];
3820 /* Relative jumps are computed relative to
3821 the instruction pointer after fetching
3822 the jump instruction.
3823 */
3824 bsize += instrsize(instr);
3825 if (instr->i_jabs)
3826 instr->i_oparg = instr->i_target->b_offset;
3827 else if (instr->i_jrel) {
3828 int delta = instr->i_target->b_offset - bsize;
3829 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003830 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003831 else
3832 continue;
3833 if (instr->i_oparg > 0xffff)
3834 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003835 }
3836 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003837
3838 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003839 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003840 with a better solution.
3841
3842 In the meantime, should the goto be dropped in favor
3843 of a loop?
3844
3845 The issue is that in the first loop blocksize() is called
3846 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003847 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003848 i_oparg is calculated in the second loop above.
3849
3850 So we loop until we stop seeing new EXTENDED_ARGs.
3851 The only EXTENDED_ARGs that could be popping up are
3852 ones in jump instructions. So this should converge
3853 fairly quickly.
3854 */
3855 if (last_extended_arg_count != extended_arg_count) {
3856 last_extended_arg_count = extended_arg_count;
3857 goto start;
3858 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859}
3860
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003861static PyObject *
3862dict_keys_inorder(PyObject *dict, int offset)
3863{
3864 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003865 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003866
3867 tuple = PyTuple_New(size);
3868 if (tuple == NULL)
3869 return NULL;
3870 while (PyDict_Next(dict, &pos, &k, &v)) {
3871 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003872 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003873 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003874 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003875 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003876 PyTuple_SET_ITEM(tuple, i - offset, k);
3877 }
3878 return tuple;
3879}
3880
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003881static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003883{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003884 PySTEntryObject *ste = c->u->u_ste;
3885 int flags = 0, n;
3886 if (ste->ste_type != ModuleBlock)
3887 flags |= CO_NEWLOCALS;
3888 if (ste->ste_type == FunctionBlock) {
3889 if (!ste->ste_unoptimized)
3890 flags |= CO_OPTIMIZED;
3891 if (ste->ste_nested)
3892 flags |= CO_NESTED;
3893 if (ste->ste_generator)
3894 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003895 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003896 if (ste->ste_varargs)
3897 flags |= CO_VARARGS;
3898 if (ste->ste_varkeywords)
3899 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003900 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003902
3903 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003904 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003905
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 n = PyDict_Size(c->u->u_freevars);
3907 if (n < 0)
3908 return -1;
3909 if (n == 0) {
3910 n = PyDict_Size(c->u->u_cellvars);
3911 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003912 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003913 if (n == 0) {
3914 flags |= CO_NOFREE;
3915 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003916 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003917
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003918 return flags;
3919}
3920
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921static PyCodeObject *
3922makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003923{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003924 PyObject *tmp;
3925 PyCodeObject *co = NULL;
3926 PyObject *consts = NULL;
3927 PyObject *names = NULL;
3928 PyObject *varnames = NULL;
3929 PyObject *filename = NULL;
3930 PyObject *name = NULL;
3931 PyObject *freevars = NULL;
3932 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003933 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003934 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003935
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003936 tmp = dict_keys_inorder(c->u->u_consts, 0);
3937 if (!tmp)
3938 goto error;
3939 consts = PySequence_List(tmp); /* optimize_code requires a list */
3940 Py_DECREF(tmp);
3941
3942 names = dict_keys_inorder(c->u->u_names, 0);
3943 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3944 if (!consts || !names || !varnames)
3945 goto error;
3946
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003947 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3948 if (!cellvars)
3949 goto error;
3950 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3951 if (!freevars)
3952 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 filename = PyString_FromString(c->c_filename);
3954 if (!filename)
3955 goto error;
3956
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003957 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 flags = compute_code_flags(c);
3959 if (flags < 0)
3960 goto error;
3961
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003962 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 if (!bytecode)
3964 goto error;
3965
3966 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3967 if (!tmp)
3968 goto error;
3969 Py_DECREF(consts);
3970 consts = tmp;
3971
Guido van Rossum4f72a782006-10-27 23:31:49 +00003972 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3973 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 bytecode, consts, names, varnames,
3975 freevars, cellvars,
3976 filename, c->u->u_name,
3977 c->u->u_firstlineno,
3978 a->a_lnotab);
3979 error:
3980 Py_XDECREF(consts);
3981 Py_XDECREF(names);
3982 Py_XDECREF(varnames);
3983 Py_XDECREF(filename);
3984 Py_XDECREF(name);
3985 Py_XDECREF(freevars);
3986 Py_XDECREF(cellvars);
3987 Py_XDECREF(bytecode);
3988 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003989}
3990
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003991
3992/* For debugging purposes only */
3993#if 0
3994static void
3995dump_instr(const struct instr *i)
3996{
3997 const char *jrel = i->i_jrel ? "jrel " : "";
3998 const char *jabs = i->i_jabs ? "jabs " : "";
3999 char arg[128];
4000
4001 *arg = '\0';
4002 if (i->i_hasarg)
4003 sprintf(arg, "arg: %d ", i->i_oparg);
4004
4005 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4006 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4007}
4008
4009static void
4010dump_basicblock(const basicblock *b)
4011{
4012 const char *seen = b->b_seen ? "seen " : "";
4013 const char *b_return = b->b_return ? "return " : "";
4014 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4015 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4016 if (b->b_instr) {
4017 int i;
4018 for (i = 0; i < b->b_iused; i++) {
4019 fprintf(stderr, " [%02d] ", i);
4020 dump_instr(b->b_instr + i);
4021 }
4022 }
4023}
4024#endif
4025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026static PyCodeObject *
4027assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004028{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004029 basicblock *b, *entryblock;
4030 struct assembler a;
4031 int i, j, nblocks;
4032 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 /* Make sure every block that falls off the end returns None.
4035 XXX NEXT_BLOCK() isn't quite right, because if the last
4036 block ends with a jump or return b_next shouldn't set.
4037 */
4038 if (!c->u->u_curblock->b_return) {
4039 NEXT_BLOCK(c);
4040 if (addNone)
4041 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4042 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004043 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004045 nblocks = 0;
4046 entryblock = NULL;
4047 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4048 nblocks++;
4049 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004050 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004051
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004052 /* Set firstlineno if it wasn't explicitly set. */
4053 if (!c->u->u_firstlineno) {
4054 if (entryblock && entryblock->b_instr)
4055 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4056 else
4057 c->u->u_firstlineno = 1;
4058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4060 goto error;
4061 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004063 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004064 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004066 /* Emit code in reverse postorder from dfs. */
4067 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004068 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 for (j = 0; j < b->b_iused; j++)
4070 if (!assemble_emit(&a, &b->b_instr[j]))
4071 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004072 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4075 goto error;
4076 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4077 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079 co = makecode(c, &a);
4080 error:
4081 assemble_free(&a);
4082 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004083}