blob: d2374a93f5b558b0de67d64c9f70ced45626c425 [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) {
1958 VISIT(c, expr, handler->name);
1959 }
1960 else {
1961 ADDOP(c, POP_TOP);
1962 }
1963 ADDOP(c, POP_TOP);
1964 VISIT_SEQ(c, stmt, handler->body);
1965 ADDOP_JREL(c, JUMP_FORWARD, end);
1966 compiler_use_next_block(c, except);
1967 if (handler->type)
1968 ADDOP(c, POP_TOP);
1969 }
1970 ADDOP(c, END_FINALLY);
1971 compiler_use_next_block(c, orelse);
1972 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
1973 compiler_use_next_block(c, end);
1974 return 1;
1975}
1976
1977static int
1978compiler_import_as(struct compiler *c, identifier name, identifier asname)
1979{
1980 /* The IMPORT_NAME opcode was already generated. This function
1981 merely needs to bind the result to a name.
1982
1983 If there is a dot in name, we need to split it and emit a
1984 LOAD_ATTR for each name.
1985 */
1986 const char *src = PyString_AS_STRING(name);
1987 const char *dot = strchr(src, '.');
1988 if (dot) {
1989 /* Consume the base module name to get the first attribute */
1990 src = dot + 1;
1991 while (dot) {
1992 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001993 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00001995 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001997 if (!attr)
1998 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002000 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 src = dot + 1;
2002 }
2003 }
2004 return compiler_nameop(c, asname, Store);
2005}
2006
2007static int
2008compiler_import(struct compiler *c, stmt_ty s)
2009{
2010 /* The Import node stores a module name like a.b.c as a single
2011 string. This is convenient for all cases except
2012 import a.b.c as d
2013 where we need to parse that string to extract the individual
2014 module names.
2015 XXX Perhaps change the representation to make this case simpler?
2016 */
2017 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002020 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002022 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023
Guido van Rossum45aecf42006-03-15 04:58:47 +00002024 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002025 if (level == NULL)
2026 return 0;
2027
2028 ADDOP_O(c, LOAD_CONST, level, consts);
2029 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2031 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2032
2033 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002034 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002035 if (!r)
2036 return r;
2037 }
2038 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039 identifier tmp = alias->name;
2040 const char *base = PyString_AS_STRING(alias->name);
2041 char *dot = strchr(base, '.');
2042 if (dot)
2043 tmp = PyString_FromStringAndSize(base,
2044 dot - base);
2045 r = compiler_nameop(c, tmp, Store);
2046 if (dot) {
2047 Py_DECREF(tmp);
2048 }
2049 if (!r)
2050 return r;
2051 }
2052 }
2053 return 1;
2054}
2055
2056static int
2057compiler_from_import(struct compiler *c, stmt_ty s)
2058{
2059 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060
2061 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002062 PyObject *level;
2063
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 if (!names)
2065 return 0;
2066
Guido van Rossum45aecf42006-03-15 04:58:47 +00002067 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002068 if (!level) {
2069 Py_DECREF(names);
2070 return 0;
2071 }
2072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 /* build up the names */
2074 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002075 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 Py_INCREF(alias->name);
2077 PyTuple_SET_ITEM(names, i, alias->name);
2078 }
2079
2080 if (s->lineno > c->c_future->ff_lineno) {
2081 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2082 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002083 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 Py_DECREF(names);
2085 return compiler_error(c,
2086 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002087 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088
2089 }
2090 }
2091
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002092 ADDOP_O(c, LOAD_CONST, level, consts);
2093 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002095 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2097 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002098 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 identifier store_name;
2100
2101 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2102 assert(n == 1);
2103 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002104 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 }
2106
2107 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2108 store_name = alias->name;
2109 if (alias->asname)
2110 store_name = alias->asname;
2111
2112 if (!compiler_nameop(c, store_name, Store)) {
2113 Py_DECREF(names);
2114 return 0;
2115 }
2116 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002117 /* remove imported module */
2118 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 return 1;
2120}
2121
2122static int
2123compiler_assert(struct compiler *c, stmt_ty s)
2124{
2125 static PyObject *assertion_error = NULL;
2126 basicblock *end;
2127
2128 if (Py_OptimizeFlag)
2129 return 1;
2130 if (assertion_error == NULL) {
2131 assertion_error = PyString_FromString("AssertionError");
2132 if (assertion_error == NULL)
2133 return 0;
2134 }
2135 VISIT(c, expr, s->v.Assert.test);
2136 end = compiler_new_block(c);
2137 if (end == NULL)
2138 return 0;
2139 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2140 ADDOP(c, POP_TOP);
2141 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2142 if (s->v.Assert.msg) {
2143 VISIT(c, expr, s->v.Assert.msg);
2144 ADDOP_I(c, RAISE_VARARGS, 2);
2145 }
2146 else {
2147 ADDOP_I(c, RAISE_VARARGS, 1);
2148 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002149 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 ADDOP(c, POP_TOP);
2151 return 1;
2152}
2153
2154static int
2155compiler_visit_stmt(struct compiler *c, stmt_ty s)
2156{
2157 int i, n;
2158
Thomas Wouters89f507f2006-12-13 04:49:30 +00002159 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 c->u->u_lineno = s->lineno;
2161 c->u->u_lineno_set = false;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002162
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002164 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002166 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002168 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 if (c->u->u_ste->ste_type != FunctionBlock)
2170 return compiler_error(c, "'return' outside function");
2171 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 VISIT(c, expr, s->v.Return.value);
2173 }
2174 else
2175 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2176 ADDOP(c, RETURN_VALUE);
2177 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002178 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 VISIT_SEQ(c, expr, s->v.Delete.targets)
2180 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002181 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 n = asdl_seq_LEN(s->v.Assign.targets);
2183 VISIT(c, expr, s->v.Assign.value);
2184 for (i = 0; i < n; i++) {
2185 if (i < n - 1)
2186 ADDOP(c, DUP_TOP);
2187 VISIT(c, expr,
2188 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2189 }
2190 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002191 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002193 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002195 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002197 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002199 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002201 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 n = 0;
2203 if (s->v.Raise.type) {
2204 VISIT(c, expr, s->v.Raise.type);
2205 n++;
2206 if (s->v.Raise.inst) {
2207 VISIT(c, expr, s->v.Raise.inst);
2208 n++;
2209 if (s->v.Raise.tback) {
2210 VISIT(c, expr, s->v.Raise.tback);
2211 n++;
2212 }
2213 }
2214 }
2215 ADDOP_I(c, RAISE_VARARGS, n);
2216 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002217 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002219 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002220 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002221 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002223 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002225 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002227 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002229 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002231 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 ADDOP(c, PRINT_EXPR);
2233 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002234 else if (s->v.Expr.value->kind != Str_kind &&
2235 s->v.Expr.value->kind != Num_kind) {
2236 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237 ADDOP(c, POP_TOP);
2238 }
2239 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002240 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002242 case Break_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002243 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 return compiler_error(c, "'break' outside loop");
2245 ADDOP(c, BREAK_LOOP);
2246 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002247 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002249 case With_kind:
2250 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 }
2252 return 1;
2253}
2254
2255static int
2256unaryop(unaryop_ty op)
2257{
2258 switch (op) {
2259 case Invert:
2260 return UNARY_INVERT;
2261 case Not:
2262 return UNARY_NOT;
2263 case UAdd:
2264 return UNARY_POSITIVE;
2265 case USub:
2266 return UNARY_NEGATIVE;
2267 }
2268 return 0;
2269}
2270
2271static int
2272binop(struct compiler *c, operator_ty op)
2273{
2274 switch (op) {
2275 case Add:
2276 return BINARY_ADD;
2277 case Sub:
2278 return BINARY_SUBTRACT;
2279 case Mult:
2280 return BINARY_MULTIPLY;
2281 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002282 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 case Mod:
2284 return BINARY_MODULO;
2285 case Pow:
2286 return BINARY_POWER;
2287 case LShift:
2288 return BINARY_LSHIFT;
2289 case RShift:
2290 return BINARY_RSHIFT;
2291 case BitOr:
2292 return BINARY_OR;
2293 case BitXor:
2294 return BINARY_XOR;
2295 case BitAnd:
2296 return BINARY_AND;
2297 case FloorDiv:
2298 return BINARY_FLOOR_DIVIDE;
2299 }
2300 return 0;
2301}
2302
2303static int
2304cmpop(cmpop_ty op)
2305{
2306 switch (op) {
2307 case Eq:
2308 return PyCmp_EQ;
2309 case NotEq:
2310 return PyCmp_NE;
2311 case Lt:
2312 return PyCmp_LT;
2313 case LtE:
2314 return PyCmp_LE;
2315 case Gt:
2316 return PyCmp_GT;
2317 case GtE:
2318 return PyCmp_GE;
2319 case Is:
2320 return PyCmp_IS;
2321 case IsNot:
2322 return PyCmp_IS_NOT;
2323 case In:
2324 return PyCmp_IN;
2325 case NotIn:
2326 return PyCmp_NOT_IN;
2327 }
2328 return PyCmp_BAD;
2329}
2330
2331static int
2332inplace_binop(struct compiler *c, operator_ty op)
2333{
2334 switch (op) {
2335 case Add:
2336 return INPLACE_ADD;
2337 case Sub:
2338 return INPLACE_SUBTRACT;
2339 case Mult:
2340 return INPLACE_MULTIPLY;
2341 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002342 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 case Mod:
2344 return INPLACE_MODULO;
2345 case Pow:
2346 return INPLACE_POWER;
2347 case LShift:
2348 return INPLACE_LSHIFT;
2349 case RShift:
2350 return INPLACE_RSHIFT;
2351 case BitOr:
2352 return INPLACE_OR;
2353 case BitXor:
2354 return INPLACE_XOR;
2355 case BitAnd:
2356 return INPLACE_AND;
2357 case FloorDiv:
2358 return INPLACE_FLOOR_DIVIDE;
2359 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002360 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002361 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002362 return 0;
2363}
2364
2365static int
2366compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2367{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002368 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002369 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2370
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002371 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002372 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 /* XXX AugStore isn't used anywhere! */
2374
2375 /* First check for assignment to __debug__. Param? */
2376 if ((ctx == Store || ctx == AugStore || ctx == Del)
2377 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2378 return compiler_error(c, "can not assign to __debug__");
2379 }
2380
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002381 mangled = _Py_Mangle(c->u->u_private, name);
2382 if (!mangled)
2383 return 0;
2384
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002385 op = 0;
2386 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002387 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002388 switch (scope) {
2389 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002390 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 optype = OP_DEREF;
2392 break;
2393 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002394 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 optype = OP_DEREF;
2396 break;
2397 case LOCAL:
2398 if (c->u->u_ste->ste_type == FunctionBlock)
2399 optype = OP_FAST;
2400 break;
2401 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002402 if (c->u->u_ste->ste_type == FunctionBlock &&
2403 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 optype = OP_GLOBAL;
2405 break;
2406 case GLOBAL_EXPLICIT:
2407 optype = OP_GLOBAL;
2408 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002409 default:
2410 /* scope can be 0 */
2411 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 }
2413
2414 /* XXX Leave assert here, but handle __doc__ and the like better */
2415 assert(scope || PyString_AS_STRING(name)[0] == '_');
2416
2417 switch (optype) {
2418 case OP_DEREF:
2419 switch (ctx) {
2420 case Load: op = LOAD_DEREF; break;
2421 case Store: op = STORE_DEREF; break;
2422 case AugLoad:
2423 case AugStore:
2424 break;
2425 case Del:
2426 PyErr_Format(PyExc_SyntaxError,
2427 "can not delete variable '%s' referenced "
2428 "in nested scope",
2429 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002430 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002433 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002434 PyErr_SetString(PyExc_SystemError,
2435 "param invalid for deref variable");
2436 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 }
2438 break;
2439 case OP_FAST:
2440 switch (ctx) {
2441 case Load: op = LOAD_FAST; break;
2442 case Store: op = STORE_FAST; break;
2443 case Del: op = DELETE_FAST; break;
2444 case AugLoad:
2445 case AugStore:
2446 break;
2447 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002448 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002449 PyErr_SetString(PyExc_SystemError,
2450 "param invalid for local variable");
2451 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002453 ADDOP_O(c, op, mangled, varnames);
2454 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 return 1;
2456 case OP_GLOBAL:
2457 switch (ctx) {
2458 case Load: op = LOAD_GLOBAL; break;
2459 case Store: op = STORE_GLOBAL; break;
2460 case Del: op = DELETE_GLOBAL; break;
2461 case AugLoad:
2462 case AugStore:
2463 break;
2464 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002465 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002466 PyErr_SetString(PyExc_SystemError,
2467 "param invalid for global variable");
2468 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 }
2470 break;
2471 case OP_NAME:
2472 switch (ctx) {
2473 case Load: op = LOAD_NAME; break;
2474 case Store: op = STORE_NAME; break;
2475 case Del: op = DELETE_NAME; break;
2476 case AugLoad:
2477 case AugStore:
2478 break;
2479 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002480 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002481 PyErr_SetString(PyExc_SystemError,
2482 "param invalid for name variable");
2483 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 }
2485 break;
2486 }
2487
2488 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002489 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002490 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002491 if (arg < 0)
2492 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002493 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494}
2495
2496static int
2497compiler_boolop(struct compiler *c, expr_ty e)
2498{
2499 basicblock *end;
2500 int jumpi, i, n;
2501 asdl_seq *s;
2502
2503 assert(e->kind == BoolOp_kind);
2504 if (e->v.BoolOp.op == And)
2505 jumpi = JUMP_IF_FALSE;
2506 else
2507 jumpi = JUMP_IF_TRUE;
2508 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002509 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 return 0;
2511 s = e->v.BoolOp.values;
2512 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002513 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002515 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 ADDOP_JREL(c, jumpi, end);
2517 ADDOP(c, POP_TOP)
2518 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002519 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 compiler_use_next_block(c, end);
2521 return 1;
2522}
2523
2524static int
2525compiler_list(struct compiler *c, expr_ty e)
2526{
2527 int n = asdl_seq_LEN(e->v.List.elts);
2528 if (e->v.List.ctx == Store) {
2529 ADDOP_I(c, UNPACK_SEQUENCE, n);
2530 }
2531 VISIT_SEQ(c, expr, e->v.List.elts);
2532 if (e->v.List.ctx == Load) {
2533 ADDOP_I(c, BUILD_LIST, n);
2534 }
2535 return 1;
2536}
2537
2538static int
2539compiler_tuple(struct compiler *c, expr_ty e)
2540{
2541 int n = asdl_seq_LEN(e->v.Tuple.elts);
2542 if (e->v.Tuple.ctx == Store) {
2543 ADDOP_I(c, UNPACK_SEQUENCE, n);
2544 }
2545 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2546 if (e->v.Tuple.ctx == Load) {
2547 ADDOP_I(c, BUILD_TUPLE, n);
2548 }
2549 return 1;
2550}
2551
2552static int
2553compiler_compare(struct compiler *c, expr_ty e)
2554{
2555 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002556 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557
2558 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2559 VISIT(c, expr, e->v.Compare.left);
2560 n = asdl_seq_LEN(e->v.Compare.ops);
2561 assert(n > 0);
2562 if (n > 1) {
2563 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002564 if (cleanup == NULL)
2565 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002566 VISIT(c, expr,
2567 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 }
2569 for (i = 1; i < n; i++) {
2570 ADDOP(c, DUP_TOP);
2571 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002573 cmpop((cmpop_ty)(asdl_seq_GET(
2574 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2576 NEXT_BLOCK(c);
2577 ADDOP(c, POP_TOP);
2578 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002579 VISIT(c, expr,
2580 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002582 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002584 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585 if (n > 1) {
2586 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002587 if (end == NULL)
2588 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 ADDOP_JREL(c, JUMP_FORWARD, end);
2590 compiler_use_next_block(c, cleanup);
2591 ADDOP(c, ROT_TWO);
2592 ADDOP(c, POP_TOP);
2593 compiler_use_next_block(c, end);
2594 }
2595 return 1;
2596}
2597
2598static int
2599compiler_call(struct compiler *c, expr_ty e)
2600{
2601 int n, code = 0;
2602
2603 VISIT(c, expr, e->v.Call.func);
2604 n = asdl_seq_LEN(e->v.Call.args);
2605 VISIT_SEQ(c, expr, e->v.Call.args);
2606 if (e->v.Call.keywords) {
2607 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2608 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2609 }
2610 if (e->v.Call.starargs) {
2611 VISIT(c, expr, e->v.Call.starargs);
2612 code |= 1;
2613 }
2614 if (e->v.Call.kwargs) {
2615 VISIT(c, expr, e->v.Call.kwargs);
2616 code |= 2;
2617 }
2618 switch (code) {
2619 case 0:
2620 ADDOP_I(c, CALL_FUNCTION, n);
2621 break;
2622 case 1:
2623 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2624 break;
2625 case 2:
2626 ADDOP_I(c, CALL_FUNCTION_KW, n);
2627 break;
2628 case 3:
2629 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2630 break;
2631 }
2632 return 1;
2633}
2634
2635static int
2636compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002637 asdl_seq *generators, int gen_index,
2638 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639{
2640 /* generate code for the iterator, then each of the ifs,
2641 and then write to the element */
2642
2643 comprehension_ty l;
2644 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002645 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646
2647 start = compiler_new_block(c);
2648 skip = compiler_new_block(c);
2649 if_cleanup = compiler_new_block(c);
2650 anchor = compiler_new_block(c);
2651
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002652 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2653 anchor == NULL)
2654 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002656 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002657 VISIT(c, expr, l->iter);
2658 ADDOP(c, GET_ITER);
2659 compiler_use_next_block(c, start);
2660 ADDOP_JREL(c, FOR_ITER, anchor);
2661 NEXT_BLOCK(c);
2662 VISIT(c, expr, l->target);
2663
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002664 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 n = asdl_seq_LEN(l->ifs);
2666 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002667 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 VISIT(c, expr, e);
2669 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2670 NEXT_BLOCK(c);
2671 ADDOP(c, POP_TOP);
2672 }
2673
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002674 if (++gen_index < asdl_seq_LEN(generators))
2675 if (!compiler_listcomp_generator(c, tmpname,
2676 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002679 /* only append after the last for generator */
2680 if (gen_index >= asdl_seq_LEN(generators)) {
2681 if (!compiler_nameop(c, tmpname, Load))
2682 return 0;
2683 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002684 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002685
2686 compiler_use_next_block(c, skip);
2687 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 for (i = 0; i < n; i++) {
2689 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002690 if (i == 0)
2691 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 ADDOP(c, POP_TOP);
2693 }
2694 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2695 compiler_use_next_block(c, anchor);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002696 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002698 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 return 0;
2700
2701 return 1;
2702}
2703
2704static int
2705compiler_listcomp(struct compiler *c, expr_ty e)
2706{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002708 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 asdl_seq *generators = e->v.ListComp.generators;
2710
2711 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002712 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 if (!tmp)
2714 return 0;
2715 ADDOP_I(c, BUILD_LIST, 0);
2716 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002717 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002718 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2719 e->v.ListComp.elt);
2720 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002721 return rc;
2722}
2723
2724static int
2725compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002726 asdl_seq *generators, int gen_index,
2727 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728{
2729 /* generate code for the iterator, then each of the ifs,
2730 and then write to the element */
2731
2732 comprehension_ty ge;
2733 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002734 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002735
2736 start = compiler_new_block(c);
2737 skip = compiler_new_block(c);
2738 if_cleanup = compiler_new_block(c);
2739 anchor = compiler_new_block(c);
2740 end = compiler_new_block(c);
2741
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002742 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 anchor == NULL || end == NULL)
2744 return 0;
2745
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002746 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 ADDOP_JREL(c, SETUP_LOOP, end);
2748 if (!compiler_push_fblock(c, LOOP, start))
2749 return 0;
2750
2751 if (gen_index == 0) {
2752 /* Receive outermost iter as an implicit argument */
2753 c->u->u_argcount = 1;
2754 ADDOP_I(c, LOAD_FAST, 0);
2755 }
2756 else {
2757 /* Sub-iter - calculate on the fly */
2758 VISIT(c, expr, ge->iter);
2759 ADDOP(c, GET_ITER);
2760 }
2761 compiler_use_next_block(c, start);
2762 ADDOP_JREL(c, FOR_ITER, anchor);
2763 NEXT_BLOCK(c);
2764 VISIT(c, expr, ge->target);
2765
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002766 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 n = asdl_seq_LEN(ge->ifs);
2768 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002769 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 VISIT(c, expr, e);
2771 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2772 NEXT_BLOCK(c);
2773 ADDOP(c, POP_TOP);
2774 }
2775
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002776 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2778 return 0;
2779
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002780 /* only append after the last 'for' generator */
2781 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782 VISIT(c, expr, elt);
2783 ADDOP(c, YIELD_VALUE);
2784 ADDOP(c, POP_TOP);
2785
2786 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 for (i = 0; i < n; i++) {
2789 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002790 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 compiler_use_next_block(c, if_cleanup);
2792
2793 ADDOP(c, POP_TOP);
2794 }
2795 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2796 compiler_use_next_block(c, anchor);
2797 ADDOP(c, POP_BLOCK);
2798 compiler_pop_fblock(c, LOOP, start);
2799 compiler_use_next_block(c, end);
2800
2801 return 1;
2802}
2803
2804static int
2805compiler_genexp(struct compiler *c, expr_ty e)
2806{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002807 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 PyCodeObject *co;
2809 expr_ty outermost_iter = ((comprehension_ty)
2810 (asdl_seq_GET(e->v.GeneratorExp.generators,
2811 0)))->iter;
2812
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002813 if (!name) {
2814 name = PyString_FromString("<genexpr>");
2815 if (!name)
2816 return 0;
2817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818
2819 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2820 return 0;
2821 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2822 e->v.GeneratorExp.elt);
2823 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002824 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002825 if (co == NULL)
2826 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002828 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002829 Py_DECREF(co);
2830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 VISIT(c, expr, outermost_iter);
2832 ADDOP(c, GET_ITER);
2833 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
2835 return 1;
2836}
2837
2838static int
2839compiler_visit_keyword(struct compiler *c, keyword_ty k)
2840{
2841 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2842 VISIT(c, expr, k->value);
2843 return 1;
2844}
2845
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002846/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002847 whether they are true or false.
2848
2849 Return values: 1 for true, 0 for false, -1 for non-constant.
2850 */
2851
2852static int
2853expr_constant(expr_ty e)
2854{
2855 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002856 case Ellipsis_kind:
2857 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858 case Num_kind:
2859 return PyObject_IsTrue(e->v.Num.n);
2860 case Str_kind:
2861 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002862 case Name_kind:
2863 /* __debug__ is not assignable, so we can optimize
2864 * it away in if and while statements */
2865 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2866 "__debug__") == 0)
2867 return ! Py_OptimizeFlag;
2868 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 default:
2870 return -1;
2871 }
2872}
2873
Guido van Rossumc2e20742006-02-27 22:32:47 +00002874/*
2875 Implements the with statement from PEP 343.
2876
2877 The semantics outlined in that PEP are as follows:
2878
2879 with EXPR as VAR:
2880 BLOCK
2881
2882 It is implemented roughly as:
2883
Thomas Wouters477c8d52006-05-27 19:21:47 +00002884 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002885 exit = context.__exit__ # not calling it
2886 value = context.__enter__()
2887 try:
2888 VAR = value # if VAR present in the syntax
2889 BLOCK
2890 finally:
2891 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002892 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002893 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002894 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002895 exit(*exc)
2896 */
2897static int
2898compiler_with(struct compiler *c, stmt_ty s)
2899{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002900 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002901 basicblock *block, *finally;
2902 identifier tmpexit, tmpvalue = NULL;
2903
2904 assert(s->kind == With_kind);
2905
Guido van Rossumc2e20742006-02-27 22:32:47 +00002906 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002907 enter_attr = PyString_InternFromString("__enter__");
2908 if (!enter_attr)
2909 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002910 }
2911 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002912 exit_attr = PyString_InternFromString("__exit__");
2913 if (!exit_attr)
2914 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002915 }
2916
2917 block = compiler_new_block(c);
2918 finally = compiler_new_block(c);
2919 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002920 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002921
2922 /* Create a temporary variable to hold context.__exit__ */
2923 tmpexit = compiler_new_tmpname(c);
2924 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002925 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002926 PyArena_AddPyObject(c->c_arena, tmpexit);
2927
2928 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002929 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002930 We need to do this rather than preserving it on the stack
2931 because SETUP_FINALLY remembers the stack level.
2932 We need to do the assignment *inside* the try/finally
2933 so that context.__exit__() is called when the assignment
2934 fails. But we need to call context.__enter__() *before*
2935 the try/finally so that if it fails we won't call
2936 context.__exit__().
2937 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002938 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002939 if (tmpvalue == NULL)
2940 return 0;
2941 PyArena_AddPyObject(c->c_arena, tmpvalue);
2942 }
2943
Thomas Wouters477c8d52006-05-27 19:21:47 +00002944 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002945 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002946
2947 /* Squirrel away context.__exit__ */
2948 ADDOP(c, DUP_TOP);
2949 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
2950 if (!compiler_nameop(c, tmpexit, Store))
2951 return 0;
2952
2953 /* Call context.__enter__() */
2954 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2955 ADDOP_I(c, CALL_FUNCTION, 0);
2956
2957 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002958 /* Store it in tmpvalue */
2959 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00002960 return 0;
2961 }
2962 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002963 /* Discard result from context.__enter__() */
2964 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002965 }
2966
2967 /* Start the try block */
2968 ADDOP_JREL(c, SETUP_FINALLY, finally);
2969
2970 compiler_use_next_block(c, block);
2971 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002972 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002973 }
2974
2975 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002976 /* Bind saved result of context.__enter__() to VAR */
2977 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00002978 !compiler_nameop(c, tmpvalue, Del))
2979 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002980 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002981 }
2982
2983 /* BLOCK code */
2984 VISIT_SEQ(c, stmt, s->v.With.body);
2985
2986 /* End of try block; start the finally block */
2987 ADDOP(c, POP_BLOCK);
2988 compiler_pop_fblock(c, FINALLY_TRY, block);
2989
2990 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2991 compiler_use_next_block(c, finally);
2992 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002993 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002994
2995 /* Finally block starts; push tmpexit and issue our magic opcode. */
2996 if (!compiler_nameop(c, tmpexit, Load) ||
2997 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002998 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002999 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003000
3001 /* Finally block ends. */
3002 ADDOP(c, END_FINALLY);
3003 compiler_pop_fblock(c, FINALLY_END, finally);
3004 return 1;
3005}
3006
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007static int
3008compiler_visit_expr(struct compiler *c, expr_ty e)
3009{
3010 int i, n;
3011
Thomas Wouters89f507f2006-12-13 04:49:30 +00003012 /* If expr e has a different line number than the last expr/stmt,
3013 set a new line number for the next instruction.
3014 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 if (e->lineno > c->u->u_lineno) {
3016 c->u->u_lineno = e->lineno;
3017 c->u->u_lineno_set = false;
3018 }
3019 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003020 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003022 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 VISIT(c, expr, e->v.BinOp.left);
3024 VISIT(c, expr, e->v.BinOp.right);
3025 ADDOP(c, binop(c, e->v.BinOp.op));
3026 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003027 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 VISIT(c, expr, e->v.UnaryOp.operand);
3029 ADDOP(c, unaryop(e->v.UnaryOp.op));
3030 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003031 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003033 case IfExp_kind:
3034 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003035 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003036 /* XXX get rid of arg? */
3037 ADDOP_I(c, BUILD_MAP, 0);
3038 n = asdl_seq_LEN(e->v.Dict.values);
3039 /* We must arrange things just right for STORE_SUBSCR.
3040 It wants the stack to look like (value) (dict) (key) */
3041 for (i = 0; i < n; i++) {
3042 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003043 VISIT(c, expr,
3044 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003046 VISIT(c, expr,
3047 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003048 ADDOP(c, STORE_SUBSCR);
3049 }
3050 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003051 case Set_kind:
3052 n = asdl_seq_LEN(e->v.Set.elts);
3053 VISIT_SEQ(c, expr, e->v.Set.elts);
3054 ADDOP_I(c, BUILD_SET, n);
3055 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003056 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003058 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 return compiler_genexp(c, e);
3060 case Yield_kind:
3061 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003062 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003063 if (e->v.Yield.value) {
3064 VISIT(c, expr, e->v.Yield.value);
3065 }
3066 else {
3067 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3068 }
3069 ADDOP(c, YIELD_VALUE);
3070 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003071 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003072 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003073 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003075 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003076 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3077 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003078 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3080 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003081 case Ellipsis_kind:
3082 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3083 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003085 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003086 if (e->v.Attribute.ctx != AugStore)
3087 VISIT(c, expr, e->v.Attribute.value);
3088 switch (e->v.Attribute.ctx) {
3089 case AugLoad:
3090 ADDOP(c, DUP_TOP);
3091 /* Fall through to load */
3092 case Load:
3093 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3094 break;
3095 case AugStore:
3096 ADDOP(c, ROT_TWO);
3097 /* Fall through to save */
3098 case Store:
3099 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3100 break;
3101 case Del:
3102 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3103 break;
3104 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003105 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003106 PyErr_SetString(PyExc_SystemError,
3107 "param invalid in attribute expression");
3108 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 }
3110 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003111 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112 switch (e->v.Subscript.ctx) {
3113 case AugLoad:
3114 VISIT(c, expr, e->v.Subscript.value);
3115 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3116 break;
3117 case Load:
3118 VISIT(c, expr, e->v.Subscript.value);
3119 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3120 break;
3121 case AugStore:
3122 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3123 break;
3124 case Store:
3125 VISIT(c, expr, e->v.Subscript.value);
3126 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3127 break;
3128 case Del:
3129 VISIT(c, expr, e->v.Subscript.value);
3130 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3131 break;
3132 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003133 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003134 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003135 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003136 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 }
3138 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003139 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3141 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003142 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003144 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 return compiler_tuple(c, e);
3146 }
3147 return 1;
3148}
3149
3150static int
3151compiler_augassign(struct compiler *c, stmt_ty s)
3152{
3153 expr_ty e = s->v.AugAssign.target;
3154 expr_ty auge;
3155
3156 assert(s->kind == AugAssign_kind);
3157
3158 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003159 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003161 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003162 if (auge == NULL)
3163 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 VISIT(c, expr, auge);
3165 VISIT(c, expr, s->v.AugAssign.value);
3166 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3167 auge->v.Attribute.ctx = AugStore;
3168 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 break;
3170 case Subscript_kind:
3171 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003172 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003173 if (auge == NULL)
3174 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 VISIT(c, expr, auge);
3176 VISIT(c, expr, s->v.AugAssign.value);
3177 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003178 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003180 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003182 if (!compiler_nameop(c, e->v.Name.id, Load))
3183 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003184 VISIT(c, expr, s->v.AugAssign.value);
3185 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3186 return compiler_nameop(c, e->v.Name.id, Store);
3187 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003188 PyErr_Format(PyExc_SystemError,
3189 "invalid node type (%d) for augmented assignment",
3190 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003191 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003192 }
3193 return 1;
3194}
3195
3196static int
3197compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3198{
3199 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003200 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3201 PyErr_SetString(PyExc_SystemError,
3202 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003204 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 f = &c->u->u_fblock[c->u->u_nfblocks++];
3206 f->fb_type = t;
3207 f->fb_block = b;
3208 return 1;
3209}
3210
3211static void
3212compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3213{
3214 struct compiler_unit *u = c->u;
3215 assert(u->u_nfblocks > 0);
3216 u->u_nfblocks--;
3217 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3218 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3219}
3220
Thomas Wouters89f507f2006-12-13 04:49:30 +00003221static int
3222compiler_in_loop(struct compiler *c) {
3223 int i;
3224 struct compiler_unit *u = c->u;
3225 for (i = 0; i < u->u_nfblocks; ++i) {
3226 if (u->u_fblock[i].fb_type == LOOP)
3227 return 1;
3228 }
3229 return 0;
3230}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231/* Raises a SyntaxError and returns 0.
3232 If something goes wrong, a different exception may be raised.
3233*/
3234
3235static int
3236compiler_error(struct compiler *c, const char *errstr)
3237{
3238 PyObject *loc;
3239 PyObject *u = NULL, *v = NULL;
3240
3241 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3242 if (!loc) {
3243 Py_INCREF(Py_None);
3244 loc = Py_None;
3245 }
3246 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3247 Py_None, loc);
3248 if (!u)
3249 goto exit;
3250 v = Py_BuildValue("(zO)", errstr, u);
3251 if (!v)
3252 goto exit;
3253 PyErr_SetObject(PyExc_SyntaxError, v);
3254 exit:
3255 Py_DECREF(loc);
3256 Py_XDECREF(u);
3257 Py_XDECREF(v);
3258 return 0;
3259}
3260
3261static int
3262compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003263 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003265 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003267 /* XXX this code is duplicated */
3268 switch (ctx) {
3269 case AugLoad: /* fall through to Load */
3270 case Load: op = BINARY_SUBSCR; break;
3271 case AugStore:/* fall through to Store */
3272 case Store: op = STORE_SUBSCR; break;
3273 case Del: op = DELETE_SUBSCR; break;
3274 case Param:
3275 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003276 "invalid %s kind %d in subscript\n",
3277 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003278 return 0;
3279 }
3280 if (ctx == AugLoad) {
3281 ADDOP_I(c, DUP_TOPX, 2);
3282 }
3283 else if (ctx == AugStore) {
3284 ADDOP(c, ROT_THREE);
3285 }
3286 ADDOP(c, op);
3287 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288}
3289
3290static int
3291compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3292{
3293 int n = 2;
3294 assert(s->kind == Slice_kind);
3295
3296 /* only handles the cases where BUILD_SLICE is emitted */
3297 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003298 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 }
3300 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003301 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003303
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003304 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003305 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 }
3307 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003308 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
3310
3311 if (s->v.Slice.step) {
3312 n++;
3313 VISIT(c, expr, s->v.Slice.step);
3314 }
3315 ADDOP_I(c, BUILD_SLICE, n);
3316 return 1;
3317}
3318
3319static int
3320compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3321{
3322 int op = 0, slice_offset = 0, stack_count = 0;
3323
3324 assert(s->v.Slice.step == NULL);
3325 if (s->v.Slice.lower) {
3326 slice_offset++;
3327 stack_count++;
3328 if (ctx != AugStore)
3329 VISIT(c, expr, s->v.Slice.lower);
3330 }
3331 if (s->v.Slice.upper) {
3332 slice_offset += 2;
3333 stack_count++;
3334 if (ctx != AugStore)
3335 VISIT(c, expr, s->v.Slice.upper);
3336 }
3337
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003338 if (ctx == AugLoad) {
3339 switch (stack_count) {
3340 case 0: ADDOP(c, DUP_TOP); break;
3341 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3342 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3343 }
3344 }
3345 else if (ctx == AugStore) {
3346 switch (stack_count) {
3347 case 0: ADDOP(c, ROT_TWO); break;
3348 case 1: ADDOP(c, ROT_THREE); break;
3349 case 2: ADDOP(c, ROT_FOUR); break;
3350 }
3351 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352
3353 switch (ctx) {
3354 case AugLoad: /* fall through to Load */
3355 case Load: op = SLICE; break;
3356 case AugStore:/* fall through to Store */
3357 case Store: op = STORE_SLICE; break;
3358 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003359 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003360 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003361 PyErr_SetString(PyExc_SystemError,
3362 "param invalid in simple slice");
3363 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 }
3365
3366 ADDOP(c, op + slice_offset);
3367 return 1;
3368}
3369
3370static int
3371compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3372 expr_context_ty ctx)
3373{
3374 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375 case Slice_kind:
3376 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 case Index_kind:
3378 VISIT(c, expr, s->v.Index.value);
3379 break;
3380 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003381 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003382 PyErr_SetString(PyExc_SystemError,
3383 "extended slice invalid in nested slice");
3384 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385 }
3386 return 1;
3387}
3388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389static int
3390compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3391{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003392 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003394 case Index_kind:
3395 kindname = "index";
3396 if (ctx != AugStore) {
3397 VISIT(c, expr, s->v.Index.value);
3398 }
3399 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003401 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 if (!s->v.Slice.step)
3403 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003404 if (ctx != AugStore) {
3405 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406 return 0;
3407 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003408 break;
3409 case ExtSlice_kind:
3410 kindname = "extended slice";
3411 if (ctx != AugStore) {
3412 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3413 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003414 slice_ty sub = (slice_ty)asdl_seq_GET(
3415 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003416 if (!compiler_visit_nested_slice(c, sub, ctx))
3417 return 0;
3418 }
3419 ADDOP_I(c, BUILD_TUPLE, n);
3420 }
3421 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003422 default:
3423 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003424 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003425 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003427 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003428}
3429
Thomas Wouters89f507f2006-12-13 04:49:30 +00003430
3431/* End of the compiler section, beginning of the assembler section */
3432
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433/* do depth-first search of basic block graph, starting with block.
3434 post records the block indices in post-order.
3435
3436 XXX must handle implicit jumps from one block to next
3437*/
3438
Thomas Wouters89f507f2006-12-13 04:49:30 +00003439struct assembler {
3440 PyObject *a_bytecode; /* string containing bytecode */
3441 int a_offset; /* offset into bytecode */
3442 int a_nblocks; /* number of reachable blocks */
3443 basicblock **a_postorder; /* list of blocks in dfs postorder */
3444 PyObject *a_lnotab; /* string containing lnotab */
3445 int a_lnotab_off; /* offset into lnotab */
3446 int a_lineno; /* last lineno of emitted instruction */
3447 int a_lineno_off; /* bytecode offset of last lineno */
3448};
3449
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450static void
3451dfs(struct compiler *c, basicblock *b, struct assembler *a)
3452{
3453 int i;
3454 struct instr *instr = NULL;
3455
3456 if (b->b_seen)
3457 return;
3458 b->b_seen = 1;
3459 if (b->b_next != NULL)
3460 dfs(c, b->b_next, a);
3461 for (i = 0; i < b->b_iused; i++) {
3462 instr = &b->b_instr[i];
3463 if (instr->i_jrel || instr->i_jabs)
3464 dfs(c, instr->i_target, a);
3465 }
3466 a->a_postorder[a->a_nblocks++] = b;
3467}
3468
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003469static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3471{
3472 int i;
3473 struct instr *instr;
3474 if (b->b_seen || b->b_startdepth >= depth)
3475 return maxdepth;
3476 b->b_seen = 1;
3477 b->b_startdepth = depth;
3478 for (i = 0; i < b->b_iused; i++) {
3479 instr = &b->b_instr[i];
3480 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3481 if (depth > maxdepth)
3482 maxdepth = depth;
3483 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3484 if (instr->i_jrel || instr->i_jabs) {
3485 maxdepth = stackdepth_walk(c, instr->i_target,
3486 depth, maxdepth);
3487 if (instr->i_opcode == JUMP_ABSOLUTE ||
3488 instr->i_opcode == JUMP_FORWARD) {
3489 goto out; /* remaining code is dead */
3490 }
3491 }
3492 }
3493 if (b->b_next)
3494 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3495out:
3496 b->b_seen = 0;
3497 return maxdepth;
3498}
3499
3500/* Find the flow path that needs the largest stack. We assume that
3501 * cycles in the flow graph have no net effect on the stack depth.
3502 */
3503static int
3504stackdepth(struct compiler *c)
3505{
3506 basicblock *b, *entryblock;
3507 entryblock = NULL;
3508 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3509 b->b_seen = 0;
3510 b->b_startdepth = INT_MIN;
3511 entryblock = b;
3512 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003513 if (!entryblock)
3514 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 return stackdepth_walk(c, entryblock, 0, 0);
3516}
3517
3518static int
3519assemble_init(struct assembler *a, int nblocks, int firstlineno)
3520{
3521 memset(a, 0, sizeof(struct assembler));
3522 a->a_lineno = firstlineno;
3523 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3524 if (!a->a_bytecode)
3525 return 0;
3526 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3527 if (!a->a_lnotab)
3528 return 0;
3529 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003530 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003531 if (!a->a_postorder) {
3532 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003534 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535 return 1;
3536}
3537
3538static void
3539assemble_free(struct assembler *a)
3540{
3541 Py_XDECREF(a->a_bytecode);
3542 Py_XDECREF(a->a_lnotab);
3543 if (a->a_postorder)
3544 PyObject_Free(a->a_postorder);
3545}
3546
3547/* Return the size of a basic block in bytes. */
3548
3549static int
3550instrsize(struct instr *instr)
3551{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003552 if (!instr->i_hasarg)
3553 return 1;
3554 if (instr->i_oparg > 0xffff)
3555 return 6;
3556 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557}
3558
3559static int
3560blocksize(basicblock *b)
3561{
3562 int i;
3563 int size = 0;
3564
3565 for (i = 0; i < b->b_iused; i++)
3566 size += instrsize(&b->b_instr[i]);
3567 return size;
3568}
3569
3570/* All about a_lnotab.
3571
3572c_lnotab is an array of unsigned bytes disguised as a Python string.
3573It is used to map bytecode offsets to source code line #s (when needed
3574for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003575
Tim Peters2a7f3842001-06-09 09:26:21 +00003576The array is conceptually a list of
3577 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003578pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003579
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003580 byte code offset source code line number
3581 0 1
3582 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003583 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003584 350 307
3585 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003586
3587The first trick is that these numbers aren't stored, only the increments
3588from one row to the next (this doesn't really work, but it's a start):
3589
3590 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3591
3592The second trick is that an unsigned byte can't hold negative values, or
3593values larger than 255, so (a) there's a deep assumption that byte code
3594offsets and their corresponding line #s both increase monotonically, and (b)
3595if at least one column jumps by more than 255 from one row to the next, more
3596than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003597from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003598part. A user of c_lnotab desiring to find the source line number
3599corresponding to a bytecode address A should do something like this
3600
3601 lineno = addr = 0
3602 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003603 addr += addr_incr
3604 if addr > A:
3605 return lineno
3606 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003607
3608In order for this to work, when the addr field increments by more than 255,
3609the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003610increment is < 256. So, in the example above, assemble_lnotab (it used
3611to be called com_set_lineno) should not (as was actually done until 2.2)
3612expand 300, 300 to 255, 255, 45, 45,
3613 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003614*/
3615
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003616static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003618{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 int d_bytecode, d_lineno;
3620 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003621 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622
3623 d_bytecode = a->a_offset - a->a_lineno_off;
3624 d_lineno = i->i_lineno - a->a_lineno;
3625
3626 assert(d_bytecode >= 0);
3627 assert(d_lineno >= 0);
3628
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003629 /* XXX(nnorwitz): is there a better way to handle this?
3630 for loops are special, we want to be able to trace them
3631 each time around, so we need to set an extra line number. */
3632 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003633 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003634
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003636 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637 nbytes = a->a_lnotab_off + 2 * ncodes;
3638 len = PyString_GET_SIZE(a->a_lnotab);
3639 if (nbytes >= len) {
3640 if (len * 2 < nbytes)
3641 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003642 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 len *= 2;
3644 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3645 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003646 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003647 lnotab = (unsigned char *)
3648 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003649 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650 *lnotab++ = 255;
3651 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 d_bytecode -= ncodes * 255;
3654 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003656 assert(d_bytecode <= 255);
3657 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003658 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 nbytes = a->a_lnotab_off + 2 * ncodes;
3660 len = PyString_GET_SIZE(a->a_lnotab);
3661 if (nbytes >= len) {
3662 if (len * 2 < nbytes)
3663 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003664 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 len *= 2;
3666 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3667 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003668 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003669 lnotab = (unsigned char *)
3670 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003672 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003674 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003676 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678 d_lineno -= ncodes * 255;
3679 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003680 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003681
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003682 len = PyString_GET_SIZE(a->a_lnotab);
3683 if (a->a_lnotab_off + 2 >= len) {
3684 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003685 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003686 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003687 lnotab = (unsigned char *)
3688 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 a->a_lnotab_off += 2;
3691 if (d_bytecode) {
3692 *lnotab++ = d_bytecode;
3693 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003694 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003695 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 *lnotab++ = 0;
3697 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003698 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 a->a_lineno = i->i_lineno;
3700 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003701 return 1;
3702}
3703
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704/* assemble_emit()
3705 Extend the bytecode with a new instruction.
3706 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003707*/
3708
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003709static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003710assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003711{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003712 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003713 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003714 char *code;
3715
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003716 size = instrsize(i);
3717 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003719 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003720 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003722 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 if (a->a_offset + size >= len) {
3724 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003725 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003726 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3728 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003729 if (size == 6) {
3730 assert(i->i_hasarg);
3731 *code++ = (char)EXTENDED_ARG;
3732 *code++ = ext & 0xff;
3733 *code++ = ext >> 8;
3734 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003737 if (i->i_hasarg) {
3738 assert(size == 3 || size == 6);
3739 *code++ = arg & 0xff;
3740 *code++ = arg >> 8;
3741 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003743}
3744
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003745static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003747{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003749 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003750 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 /* Compute the size of each block and fixup jump args.
3753 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003754start:
3755 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003757 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 bsize = blocksize(b);
3759 b->b_offset = totsize;
3760 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003761 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003762 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3764 bsize = b->b_offset;
3765 for (i = 0; i < b->b_iused; i++) {
3766 struct instr *instr = &b->b_instr[i];
3767 /* Relative jumps are computed relative to
3768 the instruction pointer after fetching
3769 the jump instruction.
3770 */
3771 bsize += instrsize(instr);
3772 if (instr->i_jabs)
3773 instr->i_oparg = instr->i_target->b_offset;
3774 else if (instr->i_jrel) {
3775 int delta = instr->i_target->b_offset - bsize;
3776 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003777 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003778 else
3779 continue;
3780 if (instr->i_oparg > 0xffff)
3781 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003782 }
3783 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003784
3785 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003786 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003787 with a better solution.
3788
3789 In the meantime, should the goto be dropped in favor
3790 of a loop?
3791
3792 The issue is that in the first loop blocksize() is called
3793 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003794 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003795 i_oparg is calculated in the second loop above.
3796
3797 So we loop until we stop seeing new EXTENDED_ARGs.
3798 The only EXTENDED_ARGs that could be popping up are
3799 ones in jump instructions. So this should converge
3800 fairly quickly.
3801 */
3802 if (last_extended_arg_count != extended_arg_count) {
3803 last_extended_arg_count = extended_arg_count;
3804 goto start;
3805 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003806}
3807
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003808static PyObject *
3809dict_keys_inorder(PyObject *dict, int offset)
3810{
3811 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003812 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003813
3814 tuple = PyTuple_New(size);
3815 if (tuple == NULL)
3816 return NULL;
3817 while (PyDict_Next(dict, &pos, &k, &v)) {
3818 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003819 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003820 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003821 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003822 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003823 PyTuple_SET_ITEM(tuple, i - offset, k);
3824 }
3825 return tuple;
3826}
3827
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003828static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003830{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 PySTEntryObject *ste = c->u->u_ste;
3832 int flags = 0, n;
3833 if (ste->ste_type != ModuleBlock)
3834 flags |= CO_NEWLOCALS;
3835 if (ste->ste_type == FunctionBlock) {
3836 if (!ste->ste_unoptimized)
3837 flags |= CO_OPTIMIZED;
3838 if (ste->ste_nested)
3839 flags |= CO_NESTED;
3840 if (ste->ste_generator)
3841 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 if (ste->ste_varargs)
3844 flags |= CO_VARARGS;
3845 if (ste->ste_varkeywords)
3846 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003847 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003849
3850 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003851 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 n = PyDict_Size(c->u->u_freevars);
3854 if (n < 0)
3855 return -1;
3856 if (n == 0) {
3857 n = PyDict_Size(c->u->u_cellvars);
3858 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003859 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 if (n == 0) {
3861 flags |= CO_NOFREE;
3862 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003863 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003864
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003865 return flags;
3866}
3867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868static PyCodeObject *
3869makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003870{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 PyObject *tmp;
3872 PyCodeObject *co = NULL;
3873 PyObject *consts = NULL;
3874 PyObject *names = NULL;
3875 PyObject *varnames = NULL;
3876 PyObject *filename = NULL;
3877 PyObject *name = NULL;
3878 PyObject *freevars = NULL;
3879 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003880 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003882
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883 tmp = dict_keys_inorder(c->u->u_consts, 0);
3884 if (!tmp)
3885 goto error;
3886 consts = PySequence_List(tmp); /* optimize_code requires a list */
3887 Py_DECREF(tmp);
3888
3889 names = dict_keys_inorder(c->u->u_names, 0);
3890 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3891 if (!consts || !names || !varnames)
3892 goto error;
3893
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003894 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3895 if (!cellvars)
3896 goto error;
3897 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3898 if (!freevars)
3899 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003900 filename = PyString_FromString(c->c_filename);
3901 if (!filename)
3902 goto error;
3903
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003904 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905 flags = compute_code_flags(c);
3906 if (flags < 0)
3907 goto error;
3908
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003909 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003910 if (!bytecode)
3911 goto error;
3912
3913 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3914 if (!tmp)
3915 goto error;
3916 Py_DECREF(consts);
3917 consts = tmp;
3918
Guido van Rossum4f72a782006-10-27 23:31:49 +00003919 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3920 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921 bytecode, consts, names, varnames,
3922 freevars, cellvars,
3923 filename, c->u->u_name,
3924 c->u->u_firstlineno,
3925 a->a_lnotab);
3926 error:
3927 Py_XDECREF(consts);
3928 Py_XDECREF(names);
3929 Py_XDECREF(varnames);
3930 Py_XDECREF(filename);
3931 Py_XDECREF(name);
3932 Py_XDECREF(freevars);
3933 Py_XDECREF(cellvars);
3934 Py_XDECREF(bytecode);
3935 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003936}
3937
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003938
3939/* For debugging purposes only */
3940#if 0
3941static void
3942dump_instr(const struct instr *i)
3943{
3944 const char *jrel = i->i_jrel ? "jrel " : "";
3945 const char *jabs = i->i_jabs ? "jabs " : "";
3946 char arg[128];
3947
3948 *arg = '\0';
3949 if (i->i_hasarg)
3950 sprintf(arg, "arg: %d ", i->i_oparg);
3951
3952 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3953 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3954}
3955
3956static void
3957dump_basicblock(const basicblock *b)
3958{
3959 const char *seen = b->b_seen ? "seen " : "";
3960 const char *b_return = b->b_return ? "return " : "";
3961 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3962 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3963 if (b->b_instr) {
3964 int i;
3965 for (i = 0; i < b->b_iused; i++) {
3966 fprintf(stderr, " [%02d] ", i);
3967 dump_instr(b->b_instr + i);
3968 }
3969 }
3970}
3971#endif
3972
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973static PyCodeObject *
3974assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003975{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 basicblock *b, *entryblock;
3977 struct assembler a;
3978 int i, j, nblocks;
3979 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 /* Make sure every block that falls off the end returns None.
3982 XXX NEXT_BLOCK() isn't quite right, because if the last
3983 block ends with a jump or return b_next shouldn't set.
3984 */
3985 if (!c->u->u_curblock->b_return) {
3986 NEXT_BLOCK(c);
3987 if (addNone)
3988 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3989 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003990 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 nblocks = 0;
3993 entryblock = NULL;
3994 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3995 nblocks++;
3996 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003997 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003998
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003999 /* Set firstlineno if it wasn't explicitly set. */
4000 if (!c->u->u_firstlineno) {
4001 if (entryblock && entryblock->b_instr)
4002 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4003 else
4004 c->u->u_firstlineno = 1;
4005 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004006 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4007 goto error;
4008 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004009
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004011 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 /* Emit code in reverse postorder from dfs. */
4014 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004015 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004016 for (j = 0; j < b->b_iused; j++)
4017 if (!assemble_emit(&a, &b->b_instr[j]))
4018 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004019 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004021 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4022 goto error;
4023 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4024 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004026 co = makecode(c, &a);
4027 error:
4028 assemble_free(&a);
4029 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004030}