blob: e3bdaf5a1e99231aa8193c7bd5d4b31c56dfca11 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Jeremy Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 * this file.
12 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000043 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000050};
51
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000067 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000069 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000073 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074} basicblock;
75
76/* fblockinfo tracks the current frame block.
77
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078A frame block is used to handle loops, try/except, and try/finally.
79It's called a frame block to distinguish it from a basic block in the
80compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081*/
82
83enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
84
85struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000086 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 basicblock *fb_block;
88};
89
90/* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
92*/
93struct compiler_unit {
94 PySTEntryObject *u_ste;
95
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +000098 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 the argument for opcodes that refer to those collections.
100 */
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
106
107 PyObject *u_private; /* for private name mangling */
108
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000109 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000110 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111 /* Pointer to the most recently allocated block. By following b_list
112 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116
117 int u_nfblocks;
118 struct fblockinfo u_fblock[CO_MAXBLOCKS];
119
120 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000121 int u_lineno; /* the lineno for the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122 bool u_lineno_set; /* boolean to indicate whether instr
123 has been generated with current lineno */
124};
125
126/* This struct captures the global state of a compilation.
127
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000128The u pointer points to the current compilation unit, while units
129for enclosing blocks are stored in c_stack. The u and c_stack are
130managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131*/
132
133struct compiler {
134 const char *c_filename;
135 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000136 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137 PyCompilerFlags *c_flags;
138
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000142 struct compiler_unit *u; /* compiler state for current block */
143 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000144 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000145 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146};
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148static int compiler_enter_scope(struct compiler *, identifier, void *, int);
149static void compiler_free(struct compiler *);
150static basicblock *compiler_new_block(struct compiler *);
151static int compiler_next_instr(struct compiler *, basicblock *);
152static int compiler_addop(struct compiler *, int);
153static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
154static int compiler_addop_i(struct compiler *, int, int);
155static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156static basicblock *compiler_use_new_block(struct compiler *);
157static int compiler_error(struct compiler *, const char *);
158static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
159
160static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
161static int compiler_visit_stmt(struct compiler *, stmt_ty);
162static int compiler_visit_keyword(struct compiler *, keyword_ty);
163static int compiler_visit_expr(struct compiler *, expr_ty);
164static int compiler_augassign(struct compiler *, stmt_ty);
165static int compiler_visit_slice(struct compiler *, slice_ty,
166 expr_context_ty);
167
168static int compiler_push_fblock(struct compiler *, enum fblocktype,
169 basicblock *);
170static void compiler_pop_fblock(struct compiler *, enum fblocktype,
171 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000172/* Returns true if there is a loop on the fblock stack. */
173static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174
175static int inplace_binop(struct compiler *, operator_ty);
176static int expr_constant(expr_ty e);
177
Guido van Rossumc2e20742006-02-27 22:32:47 +0000178static int compiler_with(struct compiler *, stmt_ty);
179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180static PyCodeObject *assemble(struct compiler *, int addNone);
181static PyObject *__doc__;
182
183PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000185{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186 /* Name mangling: __private becomes _classname__private.
187 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000188 const char *p, *name = PyString_AsString(ident);
189 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190 size_t nlen, plen;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000191 if (privateobj == NULL || !PyString_Check(privateobj) ||
192 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000193 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000195 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 nlen = strlen(name);
198 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000199 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 /* Strip leading underscores from class name */
203 while (*p == '_')
204 p++;
205 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 plen = strlen(p);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
211 if (!ident)
212 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000214 buffer = PyString_AS_STRING(ident);
215 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 strncpy(buffer+1, p, plen);
217 strcpy(buffer+1+plen, name);
218 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000219}
220
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221static int
222compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000223{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000225
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 c->c_stack = PyList_New(0);
227 if (!c->c_stack)
228 return 0;
229
230 return 1;
231}
232
233PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000234PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000235 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236{
237 struct compiler c;
238 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000239 PyCompilerFlags local_flags;
240 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000242 if (!__doc__) {
243 __doc__ = PyString_InternFromString("__doc__");
244 if (!__doc__)
245 return NULL;
246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247
248 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000249 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000251 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 c.c_future = PyFuture_FromAST(mod, filename);
253 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000254 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000256 local_flags.cf_flags = 0;
257 flags = &local_flags;
258 }
259 merged = c.c_future->ff_features | flags->cf_flags;
260 c.c_future->ff_features = merged;
261 flags->cf_flags = merged;
262 c.c_flags = flags;
263 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264
265 c.c_st = PySymtable_Build(mod, filename, c.c_future);
266 if (c.c_st == NULL) {
267 if (!PyErr_Occurred())
268 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000269 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 }
271
272 /* XXX initialize to NULL for now, need to handle */
273 c.c_encoding = NULL;
274
275 co = compiler_mod(&c, mod);
276
Thomas Wouters1175c432006-02-27 22:49:54 +0000277 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000279 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000280 return co;
281}
282
283PyCodeObject *
284PyNode_Compile(struct _node *n, const char *filename)
285{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000286 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000288 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000289 if (!arena)
290 return NULL;
291 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000292 if (mod)
293 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000294 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000295 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000296}
297
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000298static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000300{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 if (c->c_st)
302 PySymtable_Free(c->c_st);
303 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000305 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000306}
307
Guido van Rossum79f25d91997-04-29 20:08:16 +0000308static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000310{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000311 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 PyObject *v, *k;
313 PyObject *dict = PyDict_New();
314 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000315
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000316 n = PyList_Size(list);
317 for (i = 0; i < n; i++) {
318 v = PyInt_FromLong(i);
319 if (!v) {
320 Py_DECREF(dict);
321 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000322 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000323 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
326 Py_XDECREF(k);
327 Py_DECREF(v);
328 Py_DECREF(dict);
329 return NULL;
330 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000331 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000333 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 return dict;
335}
336
337/* Return new dict containing names from src that match scope(s).
338
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000339src is a symbol table dictionary. If the scope of a name matches
340either scope_type or flag is set, insert it into the new dict. The
341values are integers, starting at offset and increasing by one for
342each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343*/
344
345static PyObject *
346dictbytype(PyObject *src, int scope_type, int flag, int offset)
347{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000348 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349 PyObject *k, *v, *dest = PyDict_New();
350
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000351 assert(offset >= 0);
352 if (dest == NULL)
353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354
355 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000356 /* XXX this should probably be a macro in symtable.h */
357 assert(PyInt_Check(v));
358 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000360 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
361 PyObject *tuple, *item = PyInt_FromLong(i);
362 if (item == NULL) {
363 Py_DECREF(dest);
364 return NULL;
365 }
366 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000367 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000368 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
369 Py_DECREF(item);
370 Py_DECREF(dest);
371 Py_XDECREF(tuple);
372 return NULL;
373 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000375 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377 }
378 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000379}
380
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381static void
382compiler_unit_check(struct compiler_unit *u)
383{
384 basicblock *block;
385 for (block = u->u_blocks; block != NULL; block = block->b_list) {
386 assert(block != (void *)0xcbcbcbcb);
387 assert(block != (void *)0xfbfbfbfb);
388 assert(block != (void *)0xdbdbdbdb);
389 if (block->b_instr != NULL) {
390 assert(block->b_ialloc > 0);
391 assert(block->b_iused > 0);
392 assert(block->b_ialloc >= block->b_iused);
393 }
394 else {
395 assert (block->b_iused == 0);
396 assert (block->b_ialloc == 0);
397 }
398 }
399}
400
401static void
402compiler_unit_free(struct compiler_unit *u)
403{
404 basicblock *b, *next;
405
406 compiler_unit_check(u);
407 b = u->u_blocks;
408 while (b != NULL) {
409 if (b->b_instr)
410 PyObject_Free((void *)b->b_instr);
411 next = b->b_list;
412 PyObject_Free((void *)b);
413 b = next;
414 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415 Py_CLEAR(u->u_ste);
416 Py_CLEAR(u->u_name);
417 Py_CLEAR(u->u_consts);
418 Py_CLEAR(u->u_names);
419 Py_CLEAR(u->u_varnames);
420 Py_CLEAR(u->u_freevars);
421 Py_CLEAR(u->u_cellvars);
422 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423 PyObject_Free(u);
424}
425
426static int
427compiler_enter_scope(struct compiler *c, identifier name, void *key,
428 int lineno)
429{
430 struct compiler_unit *u;
431
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
433 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000434 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000435 PyErr_NoMemory();
436 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000437 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000438 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000440 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 u->u_ste = PySymtable_Lookup(c->c_st, key);
442 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000443 compiler_unit_free(u);
444 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 }
446 Py_INCREF(name);
447 u->u_name = name;
448 u->u_varnames = list2dict(u->u_ste->ste_varnames);
449 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450 if (!u->u_varnames || !u->u_cellvars) {
451 compiler_unit_free(u);
452 return 0;
453 }
454
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000456 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000457 if (!u->u_freevars) {
458 compiler_unit_free(u);
459 return 0;
460 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461
462 u->u_blocks = NULL;
463 u->u_tmpname = 0;
464 u->u_nfblocks = 0;
465 u->u_firstlineno = lineno;
466 u->u_lineno = 0;
467 u->u_lineno_set = false;
468 u->u_consts = PyDict_New();
469 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000470 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471 return 0;
472 }
473 u->u_names = PyDict_New();
474 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000475 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 return 0;
477 }
478
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000479 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480
481 /* Push the old compiler_unit on the stack. */
482 if (c->u) {
483 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000484 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
485 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000486 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 return 0;
488 }
489 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000490 u->u_private = c->u->u_private;
491 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 }
493 c->u = u;
494
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000495 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000496 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 return 0;
498
499 return 1;
500}
501
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000502static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503compiler_exit_scope(struct compiler *c)
504{
505 int n;
506 PyObject *wrapper;
507
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000508 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 compiler_unit_free(c->u);
510 /* Restore c->u to the parent unit. */
511 n = PyList_GET_SIZE(c->c_stack) - 1;
512 if (n >= 0) {
513 wrapper = PyList_GET_ITEM(c->c_stack, n);
514 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000515 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000516 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000518 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 compiler_unit_check(c->u);
520 }
521 else
522 c->u = NULL;
523
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524}
525
Guido van Rossumc2e20742006-02-27 22:32:47 +0000526/* Allocate a new "anonymous" local variable.
527 Used by list comprehensions and with statements.
528*/
529
530static PyObject *
531compiler_new_tmpname(struct compiler *c)
532{
533 char tmpname[256];
534 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
535 return PyString_FromString(tmpname);
536}
537
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538/* Allocate a new block and return a pointer to it.
539 Returns NULL on error.
540*/
541
542static basicblock *
543compiler_new_block(struct compiler *c)
544{
545 basicblock *b;
546 struct compiler_unit *u;
547
548 u = c->u;
549 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000550 if (b == NULL) {
551 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000555 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 b->b_list = u->u_blocks;
557 u->u_blocks = b;
558 return b;
559}
560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561static basicblock *
562compiler_use_new_block(struct compiler *c)
563{
564 basicblock *block = compiler_new_block(c);
565 if (block == NULL)
566 return NULL;
567 c->u->u_curblock = block;
568 return block;
569}
570
571static basicblock *
572compiler_next_block(struct compiler *c)
573{
574 basicblock *block = compiler_new_block(c);
575 if (block == NULL)
576 return NULL;
577 c->u->u_curblock->b_next = block;
578 c->u->u_curblock = block;
579 return block;
580}
581
582static basicblock *
583compiler_use_next_block(struct compiler *c, basicblock *block)
584{
585 assert(block != NULL);
586 c->u->u_curblock->b_next = block;
587 c->u->u_curblock = block;
588 return block;
589}
590
591/* Returns the offset of the next instruction in the current block's
592 b_instr array. Resizes the b_instr as necessary.
593 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000594*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
596static int
597compiler_next_instr(struct compiler *c, basicblock *b)
598{
599 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000600 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601 b->b_instr = (struct instr *)PyObject_Malloc(
602 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 if (b->b_instr == NULL) {
604 PyErr_NoMemory();
605 return -1;
606 }
607 b->b_ialloc = DEFAULT_BLOCK_SIZE;
608 memset((char *)b->b_instr, 0,
609 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 size_t oldsize, newsize;
614 oldsize = b->b_ialloc * sizeof(struct instr);
615 newsize = oldsize << 1;
616 if (newsize == 0) {
617 PyErr_NoMemory();
618 return -1;
619 }
620 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621 tmp = (struct instr *)PyObject_Realloc(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 if (tmp == NULL) {
624 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000626 }
627 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
629 }
630 return b->b_iused++;
631}
632
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000633/* Set the i_lineno member of the instruction at offse off if the
634 line number for the current expression/statement (?) has not
635 already been set. If it has been set, the call has no effect.
636
637 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000638*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000639
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000640static void
641compiler_set_lineno(struct compiler *c, int off)
642{
643 basicblock *b;
644 if (c->u->u_lineno_set)
645 return;
646 c->u->u_lineno_set = true;
647 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000648 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649}
650
651static int
652opcode_stack_effect(int opcode, int oparg)
653{
654 switch (opcode) {
655 case POP_TOP:
656 return -1;
657 case ROT_TWO:
658 case ROT_THREE:
659 return 0;
660 case DUP_TOP:
661 return 1;
662 case ROT_FOUR:
663 return 0;
664
665 case UNARY_POSITIVE:
666 case UNARY_NEGATIVE:
667 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 case UNARY_INVERT:
669 return 0;
670
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000671 case LIST_APPEND:
672 return -2;
673
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000674 case BINARY_POWER:
675 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000676 case BINARY_MODULO:
677 case BINARY_ADD:
678 case BINARY_SUBTRACT:
679 case BINARY_SUBSCR:
680 case BINARY_FLOOR_DIVIDE:
681 case BINARY_TRUE_DIVIDE:
682 return -1;
683 case INPLACE_FLOOR_DIVIDE:
684 case INPLACE_TRUE_DIVIDE:
685 return -1;
686
687 case SLICE+0:
688 return 1;
689 case SLICE+1:
690 return 0;
691 case SLICE+2:
692 return 0;
693 case SLICE+3:
694 return -1;
695
696 case STORE_SLICE+0:
697 return -2;
698 case STORE_SLICE+1:
699 return -3;
700 case STORE_SLICE+2:
701 return -3;
702 case STORE_SLICE+3:
703 return -4;
704
705 case DELETE_SLICE+0:
706 return -1;
707 case DELETE_SLICE+1:
708 return -2;
709 case DELETE_SLICE+2:
710 return -2;
711 case DELETE_SLICE+3:
712 return -3;
713
714 case INPLACE_ADD:
715 case INPLACE_SUBTRACT:
716 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717 case INPLACE_MODULO:
718 return -1;
719 case STORE_SUBSCR:
720 return -3;
721 case DELETE_SUBSCR:
722 return -2;
723
724 case BINARY_LSHIFT:
725 case BINARY_RSHIFT:
726 case BINARY_AND:
727 case BINARY_XOR:
728 case BINARY_OR:
729 return -1;
730 case INPLACE_POWER:
731 return -1;
732 case GET_ITER:
733 return 0;
734
735 case PRINT_EXPR:
736 return -1;
737 case PRINT_ITEM:
738 return -1;
739 case PRINT_NEWLINE:
740 return 0;
741 case PRINT_ITEM_TO:
742 return -2;
743 case PRINT_NEWLINE_TO:
744 return -1;
745 case INPLACE_LSHIFT:
746 case INPLACE_RSHIFT:
747 case INPLACE_AND:
748 case INPLACE_XOR:
749 case INPLACE_OR:
750 return -1;
751 case BREAK_LOOP:
752 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000753 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000754 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755 case LOAD_LOCALS:
756 return 1;
757 case RETURN_VALUE:
758 return -1;
759 case IMPORT_STAR:
760 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 case YIELD_VALUE:
762 return 0;
763
764 case POP_BLOCK:
765 return 0;
766 case END_FINALLY:
767 return -1; /* or -2 or -3 if exception occurred */
768 case BUILD_CLASS:
769 return -2;
770
771 case STORE_NAME:
772 return -1;
773 case DELETE_NAME:
774 return 0;
775 case UNPACK_SEQUENCE:
776 return oparg-1;
777 case FOR_ITER:
778 return 1;
779
780 case STORE_ATTR:
781 return -2;
782 case DELETE_ATTR:
783 return -1;
784 case STORE_GLOBAL:
785 return -1;
786 case DELETE_GLOBAL:
787 return 0;
788 case DUP_TOPX:
789 return oparg;
790 case LOAD_CONST:
791 return 1;
792 case LOAD_NAME:
793 return 1;
794 case BUILD_TUPLE:
795 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000796 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797 return 1-oparg;
798 case BUILD_MAP:
799 return 1;
800 case LOAD_ATTR:
801 return 0;
802 case COMPARE_OP:
803 return -1;
804 case IMPORT_NAME:
805 return 0;
806 case IMPORT_FROM:
807 return 1;
808
809 case JUMP_FORWARD:
810 case JUMP_IF_FALSE:
811 case JUMP_IF_TRUE:
812 case JUMP_ABSOLUTE:
813 return 0;
814
815 case LOAD_GLOBAL:
816 return 1;
817
818 case CONTINUE_LOOP:
819 return 0;
820 case SETUP_LOOP:
821 return 0;
822 case SETUP_EXCEPT:
823 case SETUP_FINALLY:
824 return 3; /* actually pushed by an exception */
825
826 case LOAD_FAST:
827 return 1;
828 case STORE_FAST:
829 return -1;
830 case DELETE_FAST:
831 return 0;
832
833 case RAISE_VARARGS:
834 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000835#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000836 case CALL_FUNCTION:
837 return -NARGS(oparg);
838 case CALL_FUNCTION_VAR:
839 case CALL_FUNCTION_KW:
840 return -NARGS(oparg)-1;
841 case CALL_FUNCTION_VAR_KW:
842 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000844 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000845#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 case BUILD_SLICE:
847 if (oparg == 3)
848 return -2;
849 else
850 return -1;
851
852 case MAKE_CLOSURE:
853 return -oparg;
854 case LOAD_CLOSURE:
855 return 1;
856 case LOAD_DEREF:
857 return 1;
858 case STORE_DEREF:
859 return -1;
860 default:
861 fprintf(stderr, "opcode = %d\n", opcode);
862 Py_FatalError("opcode_stack_effect()");
863
864 }
865 return 0; /* not reachable */
866}
867
868/* Add an opcode with no argument.
869 Returns 0 on failure, 1 on success.
870*/
871
872static int
873compiler_addop(struct compiler *c, int opcode)
874{
875 basicblock *b;
876 struct instr *i;
877 int off;
878 off = compiler_next_instr(c, c->u->u_curblock);
879 if (off < 0)
880 return 0;
881 b = c->u->u_curblock;
882 i = &b->b_instr[off];
883 i->i_opcode = opcode;
884 i->i_hasarg = 0;
885 if (opcode == RETURN_VALUE)
886 b->b_return = 1;
887 compiler_set_lineno(c, off);
888 return 1;
889}
890
891static int
892compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
893{
894 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000895 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000896
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000897 /* necessary to make sure types aren't coerced (e.g., int and long) */
898 t = PyTuple_Pack(2, o, o->ob_type);
899 if (t == NULL)
900 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901
902 v = PyDict_GetItem(dict, t);
903 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000904 if (PyErr_Occurred())
905 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906 arg = PyDict_Size(dict);
907 v = PyInt_FromLong(arg);
908 if (!v) {
909 Py_DECREF(t);
910 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000911 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912 if (PyDict_SetItem(dict, t, v) < 0) {
913 Py_DECREF(t);
914 Py_DECREF(v);
915 return -1;
916 }
917 Py_DECREF(v);
918 }
919 else
920 arg = PyInt_AsLong(v);
921 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000922 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923}
924
925static int
926compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
927 PyObject *o)
928{
929 int arg = compiler_add_o(c, dict, o);
930 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000931 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000932 return compiler_addop_i(c, opcode, arg);
933}
934
935static int
936compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000937 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938{
939 int arg;
940 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
941 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000942 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943 arg = compiler_add_o(c, dict, mangled);
944 Py_DECREF(mangled);
945 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000946 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 return compiler_addop_i(c, opcode, arg);
948}
949
950/* Add an opcode with an integer argument.
951 Returns 0 on failure, 1 on success.
952*/
953
954static int
955compiler_addop_i(struct compiler *c, int opcode, int oparg)
956{
957 struct instr *i;
958 int off;
959 off = compiler_next_instr(c, c->u->u_curblock);
960 if (off < 0)
961 return 0;
962 i = &c->u->u_curblock->b_instr[off];
963 i->i_opcode = opcode;
964 i->i_oparg = oparg;
965 i->i_hasarg = 1;
966 compiler_set_lineno(c, off);
967 return 1;
968}
969
970static int
971compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
972{
973 struct instr *i;
974 int off;
975
976 assert(b != NULL);
977 off = compiler_next_instr(c, c->u->u_curblock);
978 if (off < 0)
979 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 i = &c->u->u_curblock->b_instr[off];
981 i->i_opcode = opcode;
982 i->i_target = b;
983 i->i_hasarg = 1;
984 if (absolute)
985 i->i_jabs = 1;
986 else
987 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000988 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989 return 1;
990}
991
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000992/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
993 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 it as the current block. NEXT_BLOCK() also creates an implicit jump
995 from the current block to the new block.
996*/
997
Thomas Wouters89f507f2006-12-13 04:49:30 +0000998/* The returns inside these macros make it impossible to decref objects
999 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000*/
1001
1002
1003#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001004 if (compiler_use_new_block((C)) == NULL) \
1005 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006}
1007
1008#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 if (compiler_next_block((C)) == NULL) \
1010 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013#define ADDOP(C, OP) { \
1014 if (!compiler_addop((C), (OP))) \
1015 return 0; \
1016}
1017
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001018#define ADDOP_IN_SCOPE(C, OP) { \
1019 if (!compiler_addop((C), (OP))) { \
1020 compiler_exit_scope(c); \
1021 return 0; \
1022 } \
1023}
1024
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025#define ADDOP_O(C, OP, O, TYPE) { \
1026 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1027 return 0; \
1028}
1029
1030#define ADDOP_NAME(C, OP, O, TYPE) { \
1031 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1032 return 0; \
1033}
1034
1035#define ADDOP_I(C, OP, O) { \
1036 if (!compiler_addop_i((C), (OP), (O))) \
1037 return 0; \
1038}
1039
1040#define ADDOP_JABS(C, OP, O) { \
1041 if (!compiler_addop_j((C), (OP), (O), 1)) \
1042 return 0; \
1043}
1044
1045#define ADDOP_JREL(C, OP, O) { \
1046 if (!compiler_addop_j((C), (OP), (O), 0)) \
1047 return 0; \
1048}
1049
1050/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1051 the ASDL name to synthesize the name of the C type and the visit function.
1052*/
1053
1054#define VISIT(C, TYPE, V) {\
1055 if (!compiler_visit_ ## TYPE((C), (V))) \
1056 return 0; \
1057}
1058
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001059#define VISIT_IN_SCOPE(C, TYPE, V) {\
1060 if (!compiler_visit_ ## TYPE((C), (V))) { \
1061 compiler_exit_scope(c); \
1062 return 0; \
1063 } \
1064}
1065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066#define VISIT_SLICE(C, V, CTX) {\
1067 if (!compiler_visit_slice((C), (V), (CTX))) \
1068 return 0; \
1069}
1070
1071#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001072 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001074 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001075 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076 if (!compiler_visit_ ## TYPE((C), elt)) \
1077 return 0; \
1078 } \
1079}
1080
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001081#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001082 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001083 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001084 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001086 if (!compiler_visit_ ## TYPE((C), elt)) { \
1087 compiler_exit_scope(c); \
1088 return 0; \
1089 } \
1090 } \
1091}
1092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093static int
1094compiler_isdocstring(stmt_ty s)
1095{
1096 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001097 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 return s->v.Expr.value->kind == Str_kind;
1099}
1100
1101/* Compile a sequence of statements, checking for a docstring. */
1102
1103static int
1104compiler_body(struct compiler *c, asdl_seq *stmts)
1105{
1106 int i = 0;
1107 stmt_ty st;
1108
1109 if (!asdl_seq_LEN(stmts))
1110 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 if (compiler_isdocstring(st)) {
1113 i = 1;
1114 VISIT(c, expr, st->v.Expr.value);
1115 if (!compiler_nameop(c, __doc__, Store))
1116 return 0;
1117 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001118 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120 return 1;
1121}
1122
1123static PyCodeObject *
1124compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001125{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001126 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001127 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 static PyObject *module;
1129 if (!module) {
1130 module = PyString_FromString("<module>");
1131 if (!module)
1132 return NULL;
1133 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001134 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1135 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001136 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 switch (mod->kind) {
1138 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001139 if (!compiler_body(c, mod->v.Module.body)) {
1140 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001142 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 break;
1144 case Interactive_kind:
1145 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 VISIT_SEQ_IN_SCOPE(c, stmt,
1147 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 break;
1149 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001150 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001151 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 break;
1153 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001154 PyErr_SetString(PyExc_SystemError,
1155 "suite should not be possible");
1156 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001157 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001158 PyErr_Format(PyExc_SystemError,
1159 "module kind %d should not be possible",
1160 mod->kind);
1161 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001162 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 co = assemble(c, addNone);
1164 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001165 return co;
1166}
1167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168/* The test for LOCAL must come before the test for FREE in order to
1169 handle classes where name is both local and free. The local var is
1170 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001171*/
1172
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173static int
1174get_ref_type(struct compiler *c, PyObject *name)
1175{
1176 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001177 if (scope == 0) {
1178 char buf[350];
1179 PyOS_snprintf(buf, sizeof(buf),
1180 "unknown scope for %.100s in %.100s(%s) in %s\n"
1181 "symbols: %s\nlocals: %s\nglobals: %s\n",
1182 PyString_AS_STRING(name),
1183 PyString_AS_STRING(c->u->u_name),
1184 PyObject_REPR(c->u->u_ste->ste_id),
1185 c->c_filename,
1186 PyObject_REPR(c->u->u_ste->ste_symbols),
1187 PyObject_REPR(c->u->u_varnames),
1188 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001190 Py_FatalError(buf);
1191 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001192
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001193 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194}
1195
1196static int
1197compiler_lookup_arg(PyObject *dict, PyObject *name)
1198{
1199 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001200 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001202 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001204 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001206 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 return PyInt_AS_LONG(v);
1208}
1209
1210static int
1211compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1212{
1213 int i, free = PyCode_GetNumFree(co);
1214 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001215 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1216 ADDOP_I(c, MAKE_FUNCTION, args);
1217 return 1;
1218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 for (i = 0; i < free; ++i) {
1220 /* Bypass com_addop_varname because it will generate
1221 LOAD_DEREF but LOAD_CLOSURE is needed.
1222 */
1223 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1224 int arg, reftype;
1225
1226 /* Special case: If a class contains a method with a
1227 free variable that has the same name as a method,
1228 the name will be considered free *and* local in the
1229 class. It should be handled by the closure, as
1230 well as by the normal name loookup logic.
1231 */
1232 reftype = get_ref_type(c, name);
1233 if (reftype == CELL)
1234 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1235 else /* (reftype == FREE) */
1236 arg = compiler_lookup_arg(c->u->u_freevars, name);
1237 if (arg == -1) {
1238 printf("lookup %s in %s %d %d\n"
1239 "freevars of %s: %s\n",
1240 PyObject_REPR(name),
1241 PyString_AS_STRING(c->u->u_name),
1242 reftype, arg,
1243 PyString_AS_STRING(co->co_name),
1244 PyObject_REPR(co->co_freevars));
1245 Py_FatalError("compiler_make_closure()");
1246 }
1247 ADDOP_I(c, LOAD_CLOSURE, arg);
1248 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001249 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001251 ADDOP_I(c, MAKE_CLOSURE, args);
1252 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253}
1254
1255static int
1256compiler_decorators(struct compiler *c, asdl_seq* decos)
1257{
1258 int i;
1259
1260 if (!decos)
1261 return 1;
1262
1263 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 }
1266 return 1;
1267}
1268
1269static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001270compiler_unpack_nested(struct compiler *c, asdl_seq *args) {
1271 int i, len;
1272 len = asdl_seq_LEN(args);
1273 ADDOP_I(c, UNPACK_SEQUENCE, len);
1274 for (i = 0; i < len; i++) {
1275 arg_ty elt = (arg_ty)asdl_seq_GET(args, i);
1276 switch (elt->kind) {
1277 case SimpleArg_kind:
1278 if (!compiler_nameop(c, elt->v.SimpleArg.arg, Store))
1279 return 0;
1280 break;
1281 case NestedArgs_kind:
1282 if (!compiler_unpack_nested(c, elt->v.NestedArgs.args))
1283 return 0;
1284 break;
1285 default:
1286 return 0;
1287 }
1288 }
1289 return 1;
1290}
1291
1292static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293compiler_arguments(struct compiler *c, arguments_ty args)
1294{
1295 int i;
1296 int n = asdl_seq_LEN(args->args);
Neal Norwitzc1505362006-12-28 06:47:50 +00001297
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 for (i = 0; i < n; i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001299 arg_ty arg = (arg_ty)asdl_seq_GET(args->args, i);
1300 if (arg->kind == NestedArgs_kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 PyObject *id = PyString_FromFormat(".%d", i);
1302 if (id == NULL) {
1303 return 0;
1304 }
1305 if (!compiler_nameop(c, id, Load)) {
1306 Py_DECREF(id);
1307 return 0;
1308 }
1309 Py_DECREF(id);
Neal Norwitzc1505362006-12-28 06:47:50 +00001310 if (!compiler_unpack_nested(c, arg->v.NestedArgs.args))
1311 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 }
1313 }
1314 return 1;
1315}
1316
1317static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001318compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1319 asdl_seq *kw_defaults)
1320{
1321 int i, default_count = 0;
1322 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001323 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001324 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1325 if (default_) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001326 ADDOP_O(c, LOAD_CONST, arg->v.SimpleArg.arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001327 if (!compiler_visit_expr(c, default_)) {
1328 return -1;
1329 }
1330 default_count++;
1331 }
1332 }
1333 return default_count;
1334}
1335
1336static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001337compiler_visit_argannotation(struct compiler *c, identifier id,
1338 expr_ty annotation, PyObject *names)
1339{
1340 if (annotation) {
1341 VISIT(c, expr, annotation);
1342 if (PyList_Append(names, id))
1343 return -1;
1344 }
1345 return 0;
1346}
1347
1348static int
1349compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1350 PyObject *names)
1351{
1352 int i, error;
1353 for (i = 0; i < asdl_seq_LEN(args); i++) {
1354 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1355 if (arg->kind == NestedArgs_kind)
1356 error = compiler_visit_argannotations(
1357 c,
1358 arg->v.NestedArgs.args,
1359 names);
1360 else
1361 error = compiler_visit_argannotation(
1362 c,
1363 arg->v.SimpleArg.arg,
1364 arg->v.SimpleArg.annotation,
1365 names);
1366 if (error)
1367 return error;
1368 }
1369 return 0;
1370}
1371
1372static int
1373compiler_visit_annotations(struct compiler *c, arguments_ty args,
1374 expr_ty returns)
1375{
1376 /* push arg annotations and a list of the argument names. return the #
1377 of items pushed. this is out-of-order wrt the source code. */
1378 static identifier return_str;
1379 PyObject *names;
1380 int len;
1381 names = PyList_New(0);
1382 if (!names)
1383 return -1;
1384
1385 if (compiler_visit_argannotations(c, args->args, names))
1386 goto error;
1387 if (args->varargannotation &&
1388 compiler_visit_argannotation(c, args->vararg,
1389 args->varargannotation, names))
1390 goto error;
1391 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1392 goto error;
1393 if (args->kwargannotation &&
1394 compiler_visit_argannotation(c, args->kwarg,
1395 args->kwargannotation, names))
1396 goto error;
1397
1398 if (!return_str) {
1399 return_str = PyString_InternFromString("return");
1400 if (!return_str)
1401 goto error;
1402 }
1403 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1404 goto error;
1405 }
1406
1407 len = PyList_GET_SIZE(names);
1408 if (len) {
1409 /* convert names to a tuple and place on stack */
1410 PyObject *elt;
1411 int i;
1412 PyObject *s = PyTuple_New(len);
1413 if (!s)
1414 goto error;
1415 for (i = 0; i < len; i++) {
1416 elt = PyList_GET_ITEM(names, i);
1417 Py_INCREF(elt);
1418 PyTuple_SET_ITEM(s, i, elt);
1419 }
1420 ADDOP_O(c, LOAD_CONST, s, consts);
1421 Py_DECREF(s);
1422 len++; /* include the just-pushed tuple */
1423 }
1424 Py_DECREF(names);
1425 return len;
1426
1427error:
1428 Py_DECREF(names);
1429 return -1;
1430}
1431
1432static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001433compiler_function(struct compiler *c, stmt_ty s)
1434{
1435 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001436 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001438 expr_ty returns = s->v.FunctionDef.returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 asdl_seq* decos = s->v.FunctionDef.decorators;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001440 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001441 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001442 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443
1444 assert(s->kind == FunctionDef_kind);
1445
1446 if (!compiler_decorators(c, decos))
1447 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 if (args->kwonlyargs) {
1449 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1450 args->kw_defaults);
1451 if (res < 0)
1452 return 0;
1453 kw_default_count = res;
1454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 if (args->defaults)
1456 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001457 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1460 s->lineno))
1461 return 0;
1462
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001463 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001464 docstring = compiler_isdocstring(st);
1465 if (docstring)
1466 first_const = st->v.Expr.value->v.Str.s;
1467 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001468 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001469 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001472 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 compiler_arguments(c, args);
1474
1475 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001476 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001478 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001480 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1481 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 }
1483 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001484 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 if (co == NULL)
1486 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487
Guido van Rossum4f72a782006-10-27 23:31:49 +00001488 arglength = asdl_seq_LEN(args->defaults);
1489 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001490 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001491 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001492 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Neal Norwitzc1505362006-12-28 06:47:50 +00001494 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1496 ADDOP_I(c, CALL_FUNCTION, 1);
1497 }
1498
1499 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1500}
1501
1502static int
1503compiler_class(struct compiler *c, stmt_ty s)
1504{
1505 int n;
1506 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001507 PyObject *str;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 /* push class name on stack, needed by BUILD_CLASS */
1509 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1510 /* push the tuple of base classes on the stack */
1511 n = asdl_seq_LEN(s->v.ClassDef.bases);
1512 if (n > 0)
1513 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1514 ADDOP_I(c, BUILD_TUPLE, n);
1515 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1516 s->lineno))
1517 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001518 c->u->u_private = s->v.ClassDef.name;
1519 Py_INCREF(c->u->u_private);
1520 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (!str || !compiler_nameop(c, str, Load)) {
1522 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001523 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001525 }
1526
1527 Py_DECREF(str);
1528 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 if (!str || !compiler_nameop(c, str, Store)) {
1530 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001531 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001533 }
1534 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001536 if (!compiler_body(c, s->v.ClassDef.body)) {
1537 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001541 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1542 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001544 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545 if (co == NULL)
1546 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001547
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001548 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001549 Py_DECREF(co);
1550
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001551 ADDOP_I(c, CALL_FUNCTION, 0);
1552 ADDOP(c, BUILD_CLASS);
1553 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1554 return 0;
1555 return 1;
1556}
1557
1558static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001559compiler_ifexp(struct compiler *c, expr_ty e)
1560{
1561 basicblock *end, *next;
1562
1563 assert(e->kind == IfExp_kind);
1564 end = compiler_new_block(c);
1565 if (end == NULL)
1566 return 0;
1567 next = compiler_new_block(c);
1568 if (next == NULL)
1569 return 0;
1570 VISIT(c, expr, e->v.IfExp.test);
1571 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1572 ADDOP(c, POP_TOP);
1573 VISIT(c, expr, e->v.IfExp.body);
1574 ADDOP_JREL(c, JUMP_FORWARD, end);
1575 compiler_use_next_block(c, next);
1576 ADDOP(c, POP_TOP);
1577 VISIT(c, expr, e->v.IfExp.orelse);
1578 compiler_use_next_block(c, end);
1579 return 1;
1580}
1581
1582static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583compiler_lambda(struct compiler *c, expr_ty e)
1584{
1585 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001586 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001587 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588 arguments_ty args = e->v.Lambda.args;
1589 assert(e->kind == Lambda_kind);
1590
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001591 if (!name) {
1592 name = PyString_InternFromString("<lambda>");
1593 if (!name)
1594 return 0;
1595 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596
Guido van Rossum4f72a782006-10-27 23:31:49 +00001597 if (args->kwonlyargs) {
1598 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1599 args->kw_defaults);
1600 if (res < 0) return 0;
1601 kw_default_count = res;
1602 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603 if (args->defaults)
1604 VISIT_SEQ(c, expr, args->defaults);
1605 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1606 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001607
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001608 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 compiler_arguments(c, args);
1610
1611 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001612 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001613 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1614 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001616 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617 if (co == NULL)
1618 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619
Guido van Rossum4f72a782006-10-27 23:31:49 +00001620 arglength = asdl_seq_LEN(args->defaults);
1621 arglength |= kw_default_count << 8;
1622 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001623 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624
1625 return 1;
1626}
1627
1628static int
1629compiler_print(struct compiler *c, stmt_ty s)
1630{
1631 int i, n;
1632 bool dest;
1633
1634 assert(s->kind == Print_kind);
1635 n = asdl_seq_LEN(s->v.Print.values);
1636 dest = false;
1637 if (s->v.Print.dest) {
1638 VISIT(c, expr, s->v.Print.dest);
1639 dest = true;
1640 }
1641 for (i = 0; i < n; i++) {
1642 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1643 if (dest) {
1644 ADDOP(c, DUP_TOP);
1645 VISIT(c, expr, e);
1646 ADDOP(c, ROT_TWO);
1647 ADDOP(c, PRINT_ITEM_TO);
1648 }
1649 else {
1650 VISIT(c, expr, e);
1651 ADDOP(c, PRINT_ITEM);
1652 }
1653 }
1654 if (s->v.Print.nl) {
1655 if (dest)
1656 ADDOP(c, PRINT_NEWLINE_TO)
1657 else
1658 ADDOP(c, PRINT_NEWLINE)
1659 }
1660 else if (dest)
1661 ADDOP(c, POP_TOP);
1662 return 1;
1663}
1664
1665static int
1666compiler_if(struct compiler *c, stmt_ty s)
1667{
1668 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001669 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 assert(s->kind == If_kind);
1671 end = compiler_new_block(c);
1672 if (end == NULL)
1673 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001674 next = compiler_new_block(c);
1675 if (next == NULL)
1676 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001677
1678 constant = expr_constant(s->v.If.test);
1679 /* constant = 0: "if 0"
1680 * constant = 1: "if 1", "if 2", ...
1681 * constant = -1: rest */
1682 if (constant == 0) {
1683 if (s->v.If.orelse)
1684 VISIT_SEQ(c, stmt, s->v.If.orelse);
1685 } else if (constant == 1) {
1686 VISIT_SEQ(c, stmt, s->v.If.body);
1687 } else {
1688 VISIT(c, expr, s->v.If.test);
1689 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1690 ADDOP(c, POP_TOP);
1691 VISIT_SEQ(c, stmt, s->v.If.body);
1692 ADDOP_JREL(c, JUMP_FORWARD, end);
1693 compiler_use_next_block(c, next);
1694 ADDOP(c, POP_TOP);
1695 if (s->v.If.orelse)
1696 VISIT_SEQ(c, stmt, s->v.If.orelse);
1697 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 compiler_use_next_block(c, end);
1699 return 1;
1700}
1701
1702static int
1703compiler_for(struct compiler *c, stmt_ty s)
1704{
1705 basicblock *start, *cleanup, *end;
1706
1707 start = compiler_new_block(c);
1708 cleanup = compiler_new_block(c);
1709 end = compiler_new_block(c);
1710 if (start == NULL || end == NULL || cleanup == NULL)
1711 return 0;
1712 ADDOP_JREL(c, SETUP_LOOP, end);
1713 if (!compiler_push_fblock(c, LOOP, start))
1714 return 0;
1715 VISIT(c, expr, s->v.For.iter);
1716 ADDOP(c, GET_ITER);
1717 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001718 /* XXX(nnorwitz): is there a better way to handle this?
1719 for loops are special, we want to be able to trace them
1720 each time around, so we need to set an extra line number. */
1721 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 ADDOP_JREL(c, FOR_ITER, cleanup);
1723 VISIT(c, expr, s->v.For.target);
1724 VISIT_SEQ(c, stmt, s->v.For.body);
1725 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1726 compiler_use_next_block(c, cleanup);
1727 ADDOP(c, POP_BLOCK);
1728 compiler_pop_fblock(c, LOOP, start);
1729 VISIT_SEQ(c, stmt, s->v.For.orelse);
1730 compiler_use_next_block(c, end);
1731 return 1;
1732}
1733
1734static int
1735compiler_while(struct compiler *c, stmt_ty s)
1736{
1737 basicblock *loop, *orelse, *end, *anchor = NULL;
1738 int constant = expr_constant(s->v.While.test);
1739
1740 if (constant == 0)
1741 return 1;
1742 loop = compiler_new_block(c);
1743 end = compiler_new_block(c);
1744 if (constant == -1) {
1745 anchor = compiler_new_block(c);
1746 if (anchor == NULL)
1747 return 0;
1748 }
1749 if (loop == NULL || end == NULL)
1750 return 0;
1751 if (s->v.While.orelse) {
1752 orelse = compiler_new_block(c);
1753 if (orelse == NULL)
1754 return 0;
1755 }
1756 else
1757 orelse = NULL;
1758
1759 ADDOP_JREL(c, SETUP_LOOP, end);
1760 compiler_use_next_block(c, loop);
1761 if (!compiler_push_fblock(c, LOOP, loop))
1762 return 0;
1763 if (constant == -1) {
1764 VISIT(c, expr, s->v.While.test);
1765 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1766 ADDOP(c, POP_TOP);
1767 }
1768 VISIT_SEQ(c, stmt, s->v.While.body);
1769 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1770
1771 /* XXX should the two POP instructions be in a separate block
1772 if there is no else clause ?
1773 */
1774
1775 if (constant == -1) {
1776 compiler_use_next_block(c, anchor);
1777 ADDOP(c, POP_TOP);
1778 ADDOP(c, POP_BLOCK);
1779 }
1780 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 VISIT_SEQ(c, stmt, s->v.While.orelse);
1783 compiler_use_next_block(c, end);
1784
1785 return 1;
1786}
1787
1788static int
1789compiler_continue(struct compiler *c)
1790{
1791 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001792 static const char IN_FINALLY_ERROR_MSG[] =
1793 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 int i;
1795
1796 if (!c->u->u_nfblocks)
1797 return compiler_error(c, LOOP_ERROR_MSG);
1798 i = c->u->u_nfblocks - 1;
1799 switch (c->u->u_fblock[i].fb_type) {
1800 case LOOP:
1801 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1802 break;
1803 case EXCEPT:
1804 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001805 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1806 /* Prevent continue anywhere under a finally
1807 even if hidden in a sub-try or except. */
1808 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1809 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1810 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (i == -1)
1812 return compiler_error(c, LOOP_ERROR_MSG);
1813 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1814 break;
1815 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001816 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 }
1818
1819 return 1;
1820}
1821
1822/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1823
1824 SETUP_FINALLY L
1825 <code for body>
1826 POP_BLOCK
1827 LOAD_CONST <None>
1828 L: <code for finalbody>
1829 END_FINALLY
1830
1831 The special instructions use the block stack. Each block
1832 stack entry contains the instruction that created it (here
1833 SETUP_FINALLY), the level of the value stack at the time the
1834 block stack entry was created, and a label (here L).
1835
1836 SETUP_FINALLY:
1837 Pushes the current value stack level and the label
1838 onto the block stack.
1839 POP_BLOCK:
1840 Pops en entry from the block stack, and pops the value
1841 stack until its level is the same as indicated on the
1842 block stack. (The label is ignored.)
1843 END_FINALLY:
1844 Pops a variable number of entries from the *value* stack
1845 and re-raises the exception they specify. The number of
1846 entries popped depends on the (pseudo) exception type.
1847
1848 The block stack is unwound when an exception is raised:
1849 when a SETUP_FINALLY entry is found, the exception is pushed
1850 onto the value stack (and the exception condition is cleared),
1851 and the interpreter jumps to the label gotten from the block
1852 stack.
1853*/
1854
1855static int
1856compiler_try_finally(struct compiler *c, stmt_ty s)
1857{
1858 basicblock *body, *end;
1859 body = compiler_new_block(c);
1860 end = compiler_new_block(c);
1861 if (body == NULL || end == NULL)
1862 return 0;
1863
1864 ADDOP_JREL(c, SETUP_FINALLY, end);
1865 compiler_use_next_block(c, body);
1866 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1867 return 0;
1868 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1869 ADDOP(c, POP_BLOCK);
1870 compiler_pop_fblock(c, FINALLY_TRY, body);
1871
1872 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1873 compiler_use_next_block(c, end);
1874 if (!compiler_push_fblock(c, FINALLY_END, end))
1875 return 0;
1876 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1877 ADDOP(c, END_FINALLY);
1878 compiler_pop_fblock(c, FINALLY_END, end);
1879
1880 return 1;
1881}
1882
1883/*
1884 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1885 (The contents of the value stack is shown in [], with the top
1886 at the right; 'tb' is trace-back info, 'val' the exception's
1887 associated value, and 'exc' the exception.)
1888
1889 Value stack Label Instruction Argument
1890 [] SETUP_EXCEPT L1
1891 [] <code for S>
1892 [] POP_BLOCK
1893 [] JUMP_FORWARD L0
1894
1895 [tb, val, exc] L1: DUP )
1896 [tb, val, exc, exc] <evaluate E1> )
1897 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1898 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1899 [tb, val, exc, 1] POP )
1900 [tb, val, exc] POP
1901 [tb, val] <assign to V1> (or POP if no V1)
1902 [tb] POP
1903 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001904 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
1906 [tb, val, exc, 0] L2: POP
1907 [tb, val, exc] DUP
1908 .............................etc.......................
1909
1910 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001911 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
1913 [] L0: <next statement>
1914
1915 Of course, parts are not generated if Vi or Ei is not present.
1916*/
1917static int
1918compiler_try_except(struct compiler *c, stmt_ty s)
1919{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001920 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 int i, n;
1922
1923 body = compiler_new_block(c);
1924 except = compiler_new_block(c);
1925 orelse = compiler_new_block(c);
1926 end = compiler_new_block(c);
1927 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1928 return 0;
1929 ADDOP_JREL(c, SETUP_EXCEPT, except);
1930 compiler_use_next_block(c, body);
1931 if (!compiler_push_fblock(c, EXCEPT, body))
1932 return 0;
1933 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1934 ADDOP(c, POP_BLOCK);
1935 compiler_pop_fblock(c, EXCEPT, body);
1936 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1937 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1938 compiler_use_next_block(c, except);
1939 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 s->v.TryExcept.handlers, i);
1942 if (!handler->type && i < n-1)
1943 return compiler_error(c, "default 'except:' must be last");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001944 c->u->u_lineno_set = false;
1945 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 except = compiler_new_block(c);
1947 if (except == NULL)
1948 return 0;
1949 if (handler->type) {
1950 ADDOP(c, DUP_TOP);
1951 VISIT(c, expr, handler->type);
1952 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1953 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1954 ADDOP(c, POP_TOP);
1955 }
1956 ADDOP(c, POP_TOP);
1957 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001958 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001959
1960 cleanup_end = compiler_new_block(c);
1961 cleanup_body = compiler_new_block(c);
1962 if(!(cleanup_end || cleanup_body))
1963 return 0;
1964
Guido van Rossum16be03e2007-01-10 18:51:35 +00001965 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001966 ADDOP(c, POP_TOP);
1967
1968 /*
1969 try:
1970 # body
1971 except type as name:
1972 try:
1973 # body
1974 finally:
1975 name = None
1976 del name
1977 */
1978
1979 /* second try: */
1980 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1981 compiler_use_next_block(c, cleanup_body);
1982 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1983 return 0;
1984
1985 /* second # body */
1986 VISIT_SEQ(c, stmt, handler->body);
1987 ADDOP(c, POP_BLOCK);
1988 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1989
1990 /* finally: */
1991 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1992 compiler_use_next_block(c, cleanup_end);
1993 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1994 return 0;
1995
1996 /* name = None */
1997 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00001998 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001999
Guido van Rossum16be03e2007-01-10 18:51:35 +00002000 /* del name */
2001 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002002
2003 ADDOP(c, END_FINALLY);
2004 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
2006 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002007 ADDOP(c, POP_TOP);
2008 ADDOP(c, POP_TOP);
2009 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 ADDOP_JREL(c, JUMP_FORWARD, end);
2012 compiler_use_next_block(c, except);
2013 if (handler->type)
2014 ADDOP(c, POP_TOP);
2015 }
2016 ADDOP(c, END_FINALLY);
2017 compiler_use_next_block(c, orelse);
2018 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2019 compiler_use_next_block(c, end);
2020 return 1;
2021}
2022
2023static int
2024compiler_import_as(struct compiler *c, identifier name, identifier asname)
2025{
2026 /* The IMPORT_NAME opcode was already generated. This function
2027 merely needs to bind the result to a name.
2028
2029 If there is a dot in name, we need to split it and emit a
2030 LOAD_ATTR for each name.
2031 */
2032 const char *src = PyString_AS_STRING(name);
2033 const char *dot = strchr(src, '.');
2034 if (dot) {
2035 /* Consume the base module name to get the first attribute */
2036 src = dot + 1;
2037 while (dot) {
2038 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002039 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002041 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002043 if (!attr)
2044 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002046 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 src = dot + 1;
2048 }
2049 }
2050 return compiler_nameop(c, asname, Store);
2051}
2052
2053static int
2054compiler_import(struct compiler *c, stmt_ty s)
2055{
2056 /* The Import node stores a module name like a.b.c as a single
2057 string. This is convenient for all cases except
2058 import a.b.c as d
2059 where we need to parse that string to extract the individual
2060 module names.
2061 XXX Perhaps change the representation to make this case simpler?
2062 */
2063 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002066 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002068 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069
Guido van Rossum45aecf42006-03-15 04:58:47 +00002070 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002071 if (level == NULL)
2072 return 0;
2073
2074 ADDOP_O(c, LOAD_CONST, level, consts);
2075 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2077 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2078
2079 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002080 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002081 if (!r)
2082 return r;
2083 }
2084 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 identifier tmp = alias->name;
2086 const char *base = PyString_AS_STRING(alias->name);
2087 char *dot = strchr(base, '.');
2088 if (dot)
2089 tmp = PyString_FromStringAndSize(base,
2090 dot - base);
2091 r = compiler_nameop(c, tmp, Store);
2092 if (dot) {
2093 Py_DECREF(tmp);
2094 }
2095 if (!r)
2096 return r;
2097 }
2098 }
2099 return 1;
2100}
2101
2102static int
2103compiler_from_import(struct compiler *c, stmt_ty s)
2104{
2105 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106
2107 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002108 PyObject *level;
2109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 if (!names)
2111 return 0;
2112
Guido van Rossum45aecf42006-03-15 04:58:47 +00002113 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002114 if (!level) {
2115 Py_DECREF(names);
2116 return 0;
2117 }
2118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 /* build up the names */
2120 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002121 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 Py_INCREF(alias->name);
2123 PyTuple_SET_ITEM(names, i, alias->name);
2124 }
2125
2126 if (s->lineno > c->c_future->ff_lineno) {
2127 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2128 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002129 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 Py_DECREF(names);
2131 return compiler_error(c,
2132 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002133 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
2135 }
2136 }
2137
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002138 ADDOP_O(c, LOAD_CONST, level, consts);
2139 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002141 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2143 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002144 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 identifier store_name;
2146
2147 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2148 assert(n == 1);
2149 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002150 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
2152
2153 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2154 store_name = alias->name;
2155 if (alias->asname)
2156 store_name = alias->asname;
2157
2158 if (!compiler_nameop(c, store_name, Store)) {
2159 Py_DECREF(names);
2160 return 0;
2161 }
2162 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002163 /* remove imported module */
2164 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return 1;
2166}
2167
2168static int
2169compiler_assert(struct compiler *c, stmt_ty s)
2170{
2171 static PyObject *assertion_error = NULL;
2172 basicblock *end;
2173
2174 if (Py_OptimizeFlag)
2175 return 1;
2176 if (assertion_error == NULL) {
2177 assertion_error = PyString_FromString("AssertionError");
2178 if (assertion_error == NULL)
2179 return 0;
2180 }
2181 VISIT(c, expr, s->v.Assert.test);
2182 end = compiler_new_block(c);
2183 if (end == NULL)
2184 return 0;
2185 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2186 ADDOP(c, POP_TOP);
2187 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2188 if (s->v.Assert.msg) {
2189 VISIT(c, expr, s->v.Assert.msg);
2190 ADDOP_I(c, RAISE_VARARGS, 2);
2191 }
2192 else {
2193 ADDOP_I(c, RAISE_VARARGS, 1);
2194 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002195 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 ADDOP(c, POP_TOP);
2197 return 1;
2198}
2199
2200static int
2201compiler_visit_stmt(struct compiler *c, stmt_ty s)
2202{
2203 int i, n;
2204
Thomas Wouters89f507f2006-12-13 04:49:30 +00002205 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 c->u->u_lineno = s->lineno;
2207 c->u->u_lineno_set = false;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002210 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002212 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002214 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 if (c->u->u_ste->ste_type != FunctionBlock)
2216 return compiler_error(c, "'return' outside function");
2217 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 VISIT(c, expr, s->v.Return.value);
2219 }
2220 else
2221 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2222 ADDOP(c, RETURN_VALUE);
2223 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002224 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 VISIT_SEQ(c, expr, s->v.Delete.targets)
2226 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002227 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 n = asdl_seq_LEN(s->v.Assign.targets);
2229 VISIT(c, expr, s->v.Assign.value);
2230 for (i = 0; i < n; i++) {
2231 if (i < n - 1)
2232 ADDOP(c, DUP_TOP);
2233 VISIT(c, expr,
2234 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2235 }
2236 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002237 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002239 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002241 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002243 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002245 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002247 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 n = 0;
2249 if (s->v.Raise.type) {
2250 VISIT(c, expr, s->v.Raise.type);
2251 n++;
2252 if (s->v.Raise.inst) {
2253 VISIT(c, expr, s->v.Raise.inst);
2254 n++;
2255 if (s->v.Raise.tback) {
2256 VISIT(c, expr, s->v.Raise.tback);
2257 n++;
2258 }
2259 }
2260 }
2261 ADDOP_I(c, RAISE_VARARGS, n);
2262 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002263 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002265 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002267 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002269 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002271 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002273 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002275 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002277 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 ADDOP(c, PRINT_EXPR);
2279 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002280 else if (s->v.Expr.value->kind != Str_kind &&
2281 s->v.Expr.value->kind != Num_kind) {
2282 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 ADDOP(c, POP_TOP);
2284 }
2285 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002286 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002288 case Break_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002289 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 return compiler_error(c, "'break' outside loop");
2291 ADDOP(c, BREAK_LOOP);
2292 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002293 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002295 case With_kind:
2296 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 }
2298 return 1;
2299}
2300
2301static int
2302unaryop(unaryop_ty op)
2303{
2304 switch (op) {
2305 case Invert:
2306 return UNARY_INVERT;
2307 case Not:
2308 return UNARY_NOT;
2309 case UAdd:
2310 return UNARY_POSITIVE;
2311 case USub:
2312 return UNARY_NEGATIVE;
2313 }
2314 return 0;
2315}
2316
2317static int
2318binop(struct compiler *c, operator_ty op)
2319{
2320 switch (op) {
2321 case Add:
2322 return BINARY_ADD;
2323 case Sub:
2324 return BINARY_SUBTRACT;
2325 case Mult:
2326 return BINARY_MULTIPLY;
2327 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002328 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329 case Mod:
2330 return BINARY_MODULO;
2331 case Pow:
2332 return BINARY_POWER;
2333 case LShift:
2334 return BINARY_LSHIFT;
2335 case RShift:
2336 return BINARY_RSHIFT;
2337 case BitOr:
2338 return BINARY_OR;
2339 case BitXor:
2340 return BINARY_XOR;
2341 case BitAnd:
2342 return BINARY_AND;
2343 case FloorDiv:
2344 return BINARY_FLOOR_DIVIDE;
2345 }
2346 return 0;
2347}
2348
2349static int
2350cmpop(cmpop_ty op)
2351{
2352 switch (op) {
2353 case Eq:
2354 return PyCmp_EQ;
2355 case NotEq:
2356 return PyCmp_NE;
2357 case Lt:
2358 return PyCmp_LT;
2359 case LtE:
2360 return PyCmp_LE;
2361 case Gt:
2362 return PyCmp_GT;
2363 case GtE:
2364 return PyCmp_GE;
2365 case Is:
2366 return PyCmp_IS;
2367 case IsNot:
2368 return PyCmp_IS_NOT;
2369 case In:
2370 return PyCmp_IN;
2371 case NotIn:
2372 return PyCmp_NOT_IN;
2373 }
2374 return PyCmp_BAD;
2375}
2376
2377static int
2378inplace_binop(struct compiler *c, operator_ty op)
2379{
2380 switch (op) {
2381 case Add:
2382 return INPLACE_ADD;
2383 case Sub:
2384 return INPLACE_SUBTRACT;
2385 case Mult:
2386 return INPLACE_MULTIPLY;
2387 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002388 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 case Mod:
2390 return INPLACE_MODULO;
2391 case Pow:
2392 return INPLACE_POWER;
2393 case LShift:
2394 return INPLACE_LSHIFT;
2395 case RShift:
2396 return INPLACE_RSHIFT;
2397 case BitOr:
2398 return INPLACE_OR;
2399 case BitXor:
2400 return INPLACE_XOR;
2401 case BitAnd:
2402 return INPLACE_AND;
2403 case FloorDiv:
2404 return INPLACE_FLOOR_DIVIDE;
2405 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002406 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002407 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 return 0;
2409}
2410
2411static int
2412compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2413{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002414 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2416
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002417 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002418 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419 /* XXX AugStore isn't used anywhere! */
2420
2421 /* First check for assignment to __debug__. Param? */
2422 if ((ctx == Store || ctx == AugStore || ctx == Del)
2423 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2424 return compiler_error(c, "can not assign to __debug__");
2425 }
2426
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002427 mangled = _Py_Mangle(c->u->u_private, name);
2428 if (!mangled)
2429 return 0;
2430
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 op = 0;
2432 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002433 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 switch (scope) {
2435 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002436 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 optype = OP_DEREF;
2438 break;
2439 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002440 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002441 optype = OP_DEREF;
2442 break;
2443 case LOCAL:
2444 if (c->u->u_ste->ste_type == FunctionBlock)
2445 optype = OP_FAST;
2446 break;
2447 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002448 if (c->u->u_ste->ste_type == FunctionBlock &&
2449 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 optype = OP_GLOBAL;
2451 break;
2452 case GLOBAL_EXPLICIT:
2453 optype = OP_GLOBAL;
2454 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002455 default:
2456 /* scope can be 0 */
2457 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 }
2459
2460 /* XXX Leave assert here, but handle __doc__ and the like better */
2461 assert(scope || PyString_AS_STRING(name)[0] == '_');
2462
2463 switch (optype) {
2464 case OP_DEREF:
2465 switch (ctx) {
2466 case Load: op = LOAD_DEREF; break;
2467 case Store: op = STORE_DEREF; break;
2468 case AugLoad:
2469 case AugStore:
2470 break;
2471 case Del:
2472 PyErr_Format(PyExc_SyntaxError,
2473 "can not delete variable '%s' referenced "
2474 "in nested scope",
2475 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002476 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002479 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002480 PyErr_SetString(PyExc_SystemError,
2481 "param invalid for deref variable");
2482 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 }
2484 break;
2485 case OP_FAST:
2486 switch (ctx) {
2487 case Load: op = LOAD_FAST; break;
2488 case Store: op = STORE_FAST; break;
2489 case Del: op = DELETE_FAST; break;
2490 case AugLoad:
2491 case AugStore:
2492 break;
2493 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002494 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002495 PyErr_SetString(PyExc_SystemError,
2496 "param invalid for local variable");
2497 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002499 ADDOP_O(c, op, mangled, varnames);
2500 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 return 1;
2502 case OP_GLOBAL:
2503 switch (ctx) {
2504 case Load: op = LOAD_GLOBAL; break;
2505 case Store: op = STORE_GLOBAL; break;
2506 case Del: op = DELETE_GLOBAL; break;
2507 case AugLoad:
2508 case AugStore:
2509 break;
2510 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002511 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002512 PyErr_SetString(PyExc_SystemError,
2513 "param invalid for global variable");
2514 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002515 }
2516 break;
2517 case OP_NAME:
2518 switch (ctx) {
2519 case Load: op = LOAD_NAME; break;
2520 case Store: op = STORE_NAME; break;
2521 case Del: op = DELETE_NAME; break;
2522 case AugLoad:
2523 case AugStore:
2524 break;
2525 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002526 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002527 PyErr_SetString(PyExc_SystemError,
2528 "param invalid for name variable");
2529 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 }
2531 break;
2532 }
2533
2534 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002535 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002536 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002537 if (arg < 0)
2538 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002539 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002540}
2541
2542static int
2543compiler_boolop(struct compiler *c, expr_ty e)
2544{
2545 basicblock *end;
2546 int jumpi, i, n;
2547 asdl_seq *s;
2548
2549 assert(e->kind == BoolOp_kind);
2550 if (e->v.BoolOp.op == And)
2551 jumpi = JUMP_IF_FALSE;
2552 else
2553 jumpi = JUMP_IF_TRUE;
2554 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002555 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 return 0;
2557 s = e->v.BoolOp.values;
2558 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002559 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002561 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562 ADDOP_JREL(c, jumpi, end);
2563 ADDOP(c, POP_TOP)
2564 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002565 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 compiler_use_next_block(c, end);
2567 return 1;
2568}
2569
2570static int
2571compiler_list(struct compiler *c, expr_ty e)
2572{
2573 int n = asdl_seq_LEN(e->v.List.elts);
2574 if (e->v.List.ctx == Store) {
2575 ADDOP_I(c, UNPACK_SEQUENCE, n);
2576 }
2577 VISIT_SEQ(c, expr, e->v.List.elts);
2578 if (e->v.List.ctx == Load) {
2579 ADDOP_I(c, BUILD_LIST, n);
2580 }
2581 return 1;
2582}
2583
2584static int
2585compiler_tuple(struct compiler *c, expr_ty e)
2586{
2587 int n = asdl_seq_LEN(e->v.Tuple.elts);
2588 if (e->v.Tuple.ctx == Store) {
2589 ADDOP_I(c, UNPACK_SEQUENCE, n);
2590 }
2591 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2592 if (e->v.Tuple.ctx == Load) {
2593 ADDOP_I(c, BUILD_TUPLE, n);
2594 }
2595 return 1;
2596}
2597
2598static int
2599compiler_compare(struct compiler *c, expr_ty e)
2600{
2601 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002602 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603
2604 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2605 VISIT(c, expr, e->v.Compare.left);
2606 n = asdl_seq_LEN(e->v.Compare.ops);
2607 assert(n > 0);
2608 if (n > 1) {
2609 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002610 if (cleanup == NULL)
2611 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002612 VISIT(c, expr,
2613 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
2615 for (i = 1; i < n; i++) {
2616 ADDOP(c, DUP_TOP);
2617 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002619 cmpop((cmpop_ty)(asdl_seq_GET(
2620 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2622 NEXT_BLOCK(c);
2623 ADDOP(c, POP_TOP);
2624 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002625 VISIT(c, expr,
2626 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002628 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002630 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 if (n > 1) {
2632 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002633 if (end == NULL)
2634 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 ADDOP_JREL(c, JUMP_FORWARD, end);
2636 compiler_use_next_block(c, cleanup);
2637 ADDOP(c, ROT_TWO);
2638 ADDOP(c, POP_TOP);
2639 compiler_use_next_block(c, end);
2640 }
2641 return 1;
2642}
2643
2644static int
2645compiler_call(struct compiler *c, expr_ty e)
2646{
2647 int n, code = 0;
2648
2649 VISIT(c, expr, e->v.Call.func);
2650 n = asdl_seq_LEN(e->v.Call.args);
2651 VISIT_SEQ(c, expr, e->v.Call.args);
2652 if (e->v.Call.keywords) {
2653 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2654 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2655 }
2656 if (e->v.Call.starargs) {
2657 VISIT(c, expr, e->v.Call.starargs);
2658 code |= 1;
2659 }
2660 if (e->v.Call.kwargs) {
2661 VISIT(c, expr, e->v.Call.kwargs);
2662 code |= 2;
2663 }
2664 switch (code) {
2665 case 0:
2666 ADDOP_I(c, CALL_FUNCTION, n);
2667 break;
2668 case 1:
2669 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2670 break;
2671 case 2:
2672 ADDOP_I(c, CALL_FUNCTION_KW, n);
2673 break;
2674 case 3:
2675 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2676 break;
2677 }
2678 return 1;
2679}
2680
2681static int
2682compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002683 asdl_seq *generators, int gen_index,
2684 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685{
2686 /* generate code for the iterator, then each of the ifs,
2687 and then write to the element */
2688
2689 comprehension_ty l;
2690 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002691 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
2693 start = compiler_new_block(c);
2694 skip = compiler_new_block(c);
2695 if_cleanup = compiler_new_block(c);
2696 anchor = compiler_new_block(c);
2697
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002698 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2699 anchor == NULL)
2700 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002702 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 VISIT(c, expr, l->iter);
2704 ADDOP(c, GET_ITER);
2705 compiler_use_next_block(c, start);
2706 ADDOP_JREL(c, FOR_ITER, anchor);
2707 NEXT_BLOCK(c);
2708 VISIT(c, expr, l->target);
2709
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002710 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 n = asdl_seq_LEN(l->ifs);
2712 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002713 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 VISIT(c, expr, e);
2715 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2716 NEXT_BLOCK(c);
2717 ADDOP(c, POP_TOP);
2718 }
2719
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002720 if (++gen_index < asdl_seq_LEN(generators))
2721 if (!compiler_listcomp_generator(c, tmpname,
2722 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002725 /* only append after the last for generator */
2726 if (gen_index >= asdl_seq_LEN(generators)) {
2727 if (!compiler_nameop(c, tmpname, Load))
2728 return 0;
2729 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002730 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002731
2732 compiler_use_next_block(c, skip);
2733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 for (i = 0; i < n; i++) {
2735 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002736 if (i == 0)
2737 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 ADDOP(c, POP_TOP);
2739 }
2740 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2741 compiler_use_next_block(c, anchor);
Thomas Wouters89f507f2006-12-13 04:49:30 +00002742 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002744 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 return 0;
2746
2747 return 1;
2748}
2749
2750static int
2751compiler_listcomp(struct compiler *c, expr_ty e)
2752{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002754 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 asdl_seq *generators = e->v.ListComp.generators;
2756
2757 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002758 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 if (!tmp)
2760 return 0;
2761 ADDOP_I(c, BUILD_LIST, 0);
2762 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002764 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2765 e->v.ListComp.elt);
2766 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 return rc;
2768}
2769
2770static int
2771compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002772 asdl_seq *generators, int gen_index,
2773 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774{
2775 /* generate code for the iterator, then each of the ifs,
2776 and then write to the element */
2777
2778 comprehension_ty ge;
2779 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002780 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781
2782 start = compiler_new_block(c);
2783 skip = compiler_new_block(c);
2784 if_cleanup = compiler_new_block(c);
2785 anchor = compiler_new_block(c);
2786 end = compiler_new_block(c);
2787
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002788 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 anchor == NULL || end == NULL)
2790 return 0;
2791
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002792 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 ADDOP_JREL(c, SETUP_LOOP, end);
2794 if (!compiler_push_fblock(c, LOOP, start))
2795 return 0;
2796
2797 if (gen_index == 0) {
2798 /* Receive outermost iter as an implicit argument */
2799 c->u->u_argcount = 1;
2800 ADDOP_I(c, LOAD_FAST, 0);
2801 }
2802 else {
2803 /* Sub-iter - calculate on the fly */
2804 VISIT(c, expr, ge->iter);
2805 ADDOP(c, GET_ITER);
2806 }
2807 compiler_use_next_block(c, start);
2808 ADDOP_JREL(c, FOR_ITER, anchor);
2809 NEXT_BLOCK(c);
2810 VISIT(c, expr, ge->target);
2811
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002812 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 n = asdl_seq_LEN(ge->ifs);
2814 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002815 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 VISIT(c, expr, e);
2817 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2818 NEXT_BLOCK(c);
2819 ADDOP(c, POP_TOP);
2820 }
2821
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002822 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2824 return 0;
2825
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002826 /* only append after the last 'for' generator */
2827 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 VISIT(c, expr, elt);
2829 ADDOP(c, YIELD_VALUE);
2830 ADDOP(c, POP_TOP);
2831
2832 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 for (i = 0; i < n; i++) {
2835 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002836 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 compiler_use_next_block(c, if_cleanup);
2838
2839 ADDOP(c, POP_TOP);
2840 }
2841 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2842 compiler_use_next_block(c, anchor);
2843 ADDOP(c, POP_BLOCK);
2844 compiler_pop_fblock(c, LOOP, start);
2845 compiler_use_next_block(c, end);
2846
2847 return 1;
2848}
2849
2850static int
2851compiler_genexp(struct compiler *c, expr_ty e)
2852{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002853 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854 PyCodeObject *co;
2855 expr_ty outermost_iter = ((comprehension_ty)
2856 (asdl_seq_GET(e->v.GeneratorExp.generators,
2857 0)))->iter;
2858
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002859 if (!name) {
2860 name = PyString_FromString("<genexpr>");
2861 if (!name)
2862 return 0;
2863 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002864
2865 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2866 return 0;
2867 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2868 e->v.GeneratorExp.elt);
2869 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002870 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 if (co == NULL)
2872 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002874 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002875 Py_DECREF(co);
2876
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 VISIT(c, expr, outermost_iter);
2878 ADDOP(c, GET_ITER);
2879 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880
2881 return 1;
2882}
2883
2884static int
2885compiler_visit_keyword(struct compiler *c, keyword_ty k)
2886{
2887 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2888 VISIT(c, expr, k->value);
2889 return 1;
2890}
2891
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002892/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893 whether they are true or false.
2894
2895 Return values: 1 for true, 0 for false, -1 for non-constant.
2896 */
2897
2898static int
2899expr_constant(expr_ty e)
2900{
2901 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002902 case Ellipsis_kind:
2903 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 case Num_kind:
2905 return PyObject_IsTrue(e->v.Num.n);
2906 case Str_kind:
2907 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002908 case Name_kind:
2909 /* __debug__ is not assignable, so we can optimize
2910 * it away in if and while statements */
2911 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2912 "__debug__") == 0)
2913 return ! Py_OptimizeFlag;
2914 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 default:
2916 return -1;
2917 }
2918}
2919
Guido van Rossumc2e20742006-02-27 22:32:47 +00002920/*
2921 Implements the with statement from PEP 343.
2922
2923 The semantics outlined in that PEP are as follows:
2924
2925 with EXPR as VAR:
2926 BLOCK
2927
2928 It is implemented roughly as:
2929
Thomas Wouters477c8d52006-05-27 19:21:47 +00002930 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002931 exit = context.__exit__ # not calling it
2932 value = context.__enter__()
2933 try:
2934 VAR = value # if VAR present in the syntax
2935 BLOCK
2936 finally:
2937 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002938 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002939 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002940 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002941 exit(*exc)
2942 */
2943static int
2944compiler_with(struct compiler *c, stmt_ty s)
2945{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002946 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002947 basicblock *block, *finally;
2948 identifier tmpexit, tmpvalue = NULL;
2949
2950 assert(s->kind == With_kind);
2951
Guido van Rossumc2e20742006-02-27 22:32:47 +00002952 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002953 enter_attr = PyString_InternFromString("__enter__");
2954 if (!enter_attr)
2955 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002956 }
2957 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002958 exit_attr = PyString_InternFromString("__exit__");
2959 if (!exit_attr)
2960 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002961 }
2962
2963 block = compiler_new_block(c);
2964 finally = compiler_new_block(c);
2965 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002966 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002967
2968 /* Create a temporary variable to hold context.__exit__ */
2969 tmpexit = compiler_new_tmpname(c);
2970 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002971 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002972 PyArena_AddPyObject(c->c_arena, tmpexit);
2973
2974 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002975 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002976 We need to do this rather than preserving it on the stack
2977 because SETUP_FINALLY remembers the stack level.
2978 We need to do the assignment *inside* the try/finally
2979 so that context.__exit__() is called when the assignment
2980 fails. But we need to call context.__enter__() *before*
2981 the try/finally so that if it fails we won't call
2982 context.__exit__().
2983 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002984 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002985 if (tmpvalue == NULL)
2986 return 0;
2987 PyArena_AddPyObject(c->c_arena, tmpvalue);
2988 }
2989
Thomas Wouters477c8d52006-05-27 19:21:47 +00002990 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002991 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002992
2993 /* Squirrel away context.__exit__ */
2994 ADDOP(c, DUP_TOP);
2995 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
2996 if (!compiler_nameop(c, tmpexit, Store))
2997 return 0;
2998
2999 /* Call context.__enter__() */
3000 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3001 ADDOP_I(c, CALL_FUNCTION, 0);
3002
3003 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003004 /* Store it in tmpvalue */
3005 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003006 return 0;
3007 }
3008 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003009 /* Discard result from context.__enter__() */
3010 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003011 }
3012
3013 /* Start the try block */
3014 ADDOP_JREL(c, SETUP_FINALLY, finally);
3015
3016 compiler_use_next_block(c, block);
3017 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003018 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003019 }
3020
3021 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003022 /* Bind saved result of context.__enter__() to VAR */
3023 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003024 !compiler_nameop(c, tmpvalue, Del))
3025 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003026 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003027 }
3028
3029 /* BLOCK code */
3030 VISIT_SEQ(c, stmt, s->v.With.body);
3031
3032 /* End of try block; start the finally block */
3033 ADDOP(c, POP_BLOCK);
3034 compiler_pop_fblock(c, FINALLY_TRY, block);
3035
3036 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3037 compiler_use_next_block(c, finally);
3038 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003039 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003040
3041 /* Finally block starts; push tmpexit and issue our magic opcode. */
3042 if (!compiler_nameop(c, tmpexit, Load) ||
3043 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003044 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003045 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003046
3047 /* Finally block ends. */
3048 ADDOP(c, END_FINALLY);
3049 compiler_pop_fblock(c, FINALLY_END, finally);
3050 return 1;
3051}
3052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053static int
3054compiler_visit_expr(struct compiler *c, expr_ty e)
3055{
3056 int i, n;
3057
Thomas Wouters89f507f2006-12-13 04:49:30 +00003058 /* If expr e has a different line number than the last expr/stmt,
3059 set a new line number for the next instruction.
3060 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003061 if (e->lineno > c->u->u_lineno) {
3062 c->u->u_lineno = e->lineno;
3063 c->u->u_lineno_set = false;
3064 }
3065 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003066 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003067 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003068 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003069 VISIT(c, expr, e->v.BinOp.left);
3070 VISIT(c, expr, e->v.BinOp.right);
3071 ADDOP(c, binop(c, e->v.BinOp.op));
3072 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003073 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003074 VISIT(c, expr, e->v.UnaryOp.operand);
3075 ADDOP(c, unaryop(e->v.UnaryOp.op));
3076 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003077 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003078 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003079 case IfExp_kind:
3080 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003081 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 /* XXX get rid of arg? */
3083 ADDOP_I(c, BUILD_MAP, 0);
3084 n = asdl_seq_LEN(e->v.Dict.values);
3085 /* We must arrange things just right for STORE_SUBSCR.
3086 It wants the stack to look like (value) (dict) (key) */
3087 for (i = 0; i < n; i++) {
3088 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003089 VISIT(c, expr,
3090 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003091 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003092 VISIT(c, expr,
3093 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003094 ADDOP(c, STORE_SUBSCR);
3095 }
3096 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003097 case Set_kind:
3098 n = asdl_seq_LEN(e->v.Set.elts);
3099 VISIT_SEQ(c, expr, e->v.Set.elts);
3100 ADDOP_I(c, BUILD_SET, n);
3101 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003102 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003104 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105 return compiler_genexp(c, e);
3106 case Yield_kind:
3107 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003108 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 if (e->v.Yield.value) {
3110 VISIT(c, expr, e->v.Yield.value);
3111 }
3112 else {
3113 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3114 }
3115 ADDOP(c, YIELD_VALUE);
3116 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003117 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003119 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003121 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3123 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003124 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3126 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003127 case Ellipsis_kind:
3128 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3129 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003131 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 if (e->v.Attribute.ctx != AugStore)
3133 VISIT(c, expr, e->v.Attribute.value);
3134 switch (e->v.Attribute.ctx) {
3135 case AugLoad:
3136 ADDOP(c, DUP_TOP);
3137 /* Fall through to load */
3138 case Load:
3139 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3140 break;
3141 case AugStore:
3142 ADDOP(c, ROT_TWO);
3143 /* Fall through to save */
3144 case Store:
3145 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3146 break;
3147 case Del:
3148 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3149 break;
3150 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003151 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003152 PyErr_SetString(PyExc_SystemError,
3153 "param invalid in attribute expression");
3154 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 }
3156 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003157 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 switch (e->v.Subscript.ctx) {
3159 case AugLoad:
3160 VISIT(c, expr, e->v.Subscript.value);
3161 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3162 break;
3163 case Load:
3164 VISIT(c, expr, e->v.Subscript.value);
3165 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3166 break;
3167 case AugStore:
3168 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3169 break;
3170 case Store:
3171 VISIT(c, expr, e->v.Subscript.value);
3172 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3173 break;
3174 case Del:
3175 VISIT(c, expr, e->v.Subscript.value);
3176 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3177 break;
3178 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003179 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003180 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003181 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003182 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 }
3184 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003185 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3187 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003188 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003190 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 return compiler_tuple(c, e);
3192 }
3193 return 1;
3194}
3195
3196static int
3197compiler_augassign(struct compiler *c, stmt_ty s)
3198{
3199 expr_ty e = s->v.AugAssign.target;
3200 expr_ty auge;
3201
3202 assert(s->kind == AugAssign_kind);
3203
3204 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003205 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003207 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003208 if (auge == NULL)
3209 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 VISIT(c, expr, auge);
3211 VISIT(c, expr, s->v.AugAssign.value);
3212 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3213 auge->v.Attribute.ctx = AugStore;
3214 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215 break;
3216 case Subscript_kind:
3217 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003218 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003219 if (auge == NULL)
3220 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003221 VISIT(c, expr, auge);
3222 VISIT(c, expr, s->v.AugAssign.value);
3223 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003224 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003226 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003228 if (!compiler_nameop(c, e->v.Name.id, Load))
3229 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 VISIT(c, expr, s->v.AugAssign.value);
3231 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3232 return compiler_nameop(c, e->v.Name.id, Store);
3233 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003234 PyErr_Format(PyExc_SystemError,
3235 "invalid node type (%d) for augmented assignment",
3236 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003237 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 }
3239 return 1;
3240}
3241
3242static int
3243compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3244{
3245 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003246 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3247 PyErr_SetString(PyExc_SystemError,
3248 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003250 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 f = &c->u->u_fblock[c->u->u_nfblocks++];
3252 f->fb_type = t;
3253 f->fb_block = b;
3254 return 1;
3255}
3256
3257static void
3258compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3259{
3260 struct compiler_unit *u = c->u;
3261 assert(u->u_nfblocks > 0);
3262 u->u_nfblocks--;
3263 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3264 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3265}
3266
Thomas Wouters89f507f2006-12-13 04:49:30 +00003267static int
3268compiler_in_loop(struct compiler *c) {
3269 int i;
3270 struct compiler_unit *u = c->u;
3271 for (i = 0; i < u->u_nfblocks; ++i) {
3272 if (u->u_fblock[i].fb_type == LOOP)
3273 return 1;
3274 }
3275 return 0;
3276}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277/* Raises a SyntaxError and returns 0.
3278 If something goes wrong, a different exception may be raised.
3279*/
3280
3281static int
3282compiler_error(struct compiler *c, const char *errstr)
3283{
3284 PyObject *loc;
3285 PyObject *u = NULL, *v = NULL;
3286
3287 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3288 if (!loc) {
3289 Py_INCREF(Py_None);
3290 loc = Py_None;
3291 }
3292 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3293 Py_None, loc);
3294 if (!u)
3295 goto exit;
3296 v = Py_BuildValue("(zO)", errstr, u);
3297 if (!v)
3298 goto exit;
3299 PyErr_SetObject(PyExc_SyntaxError, v);
3300 exit:
3301 Py_DECREF(loc);
3302 Py_XDECREF(u);
3303 Py_XDECREF(v);
3304 return 0;
3305}
3306
3307static int
3308compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003309 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003311 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003313 /* XXX this code is duplicated */
3314 switch (ctx) {
3315 case AugLoad: /* fall through to Load */
3316 case Load: op = BINARY_SUBSCR; break;
3317 case AugStore:/* fall through to Store */
3318 case Store: op = STORE_SUBSCR; break;
3319 case Del: op = DELETE_SUBSCR; break;
3320 case Param:
3321 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003322 "invalid %s kind %d in subscript\n",
3323 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003324 return 0;
3325 }
3326 if (ctx == AugLoad) {
3327 ADDOP_I(c, DUP_TOPX, 2);
3328 }
3329 else if (ctx == AugStore) {
3330 ADDOP(c, ROT_THREE);
3331 }
3332 ADDOP(c, op);
3333 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334}
3335
3336static int
3337compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3338{
3339 int n = 2;
3340 assert(s->kind == Slice_kind);
3341
3342 /* only handles the cases where BUILD_SLICE is emitted */
3343 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003344 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 }
3346 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003347 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003349
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003351 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 }
3353 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003354 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 }
3356
3357 if (s->v.Slice.step) {
3358 n++;
3359 VISIT(c, expr, s->v.Slice.step);
3360 }
3361 ADDOP_I(c, BUILD_SLICE, n);
3362 return 1;
3363}
3364
3365static int
3366compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3367{
3368 int op = 0, slice_offset = 0, stack_count = 0;
3369
3370 assert(s->v.Slice.step == NULL);
3371 if (s->v.Slice.lower) {
3372 slice_offset++;
3373 stack_count++;
3374 if (ctx != AugStore)
3375 VISIT(c, expr, s->v.Slice.lower);
3376 }
3377 if (s->v.Slice.upper) {
3378 slice_offset += 2;
3379 stack_count++;
3380 if (ctx != AugStore)
3381 VISIT(c, expr, s->v.Slice.upper);
3382 }
3383
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003384 if (ctx == AugLoad) {
3385 switch (stack_count) {
3386 case 0: ADDOP(c, DUP_TOP); break;
3387 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3388 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3389 }
3390 }
3391 else if (ctx == AugStore) {
3392 switch (stack_count) {
3393 case 0: ADDOP(c, ROT_TWO); break;
3394 case 1: ADDOP(c, ROT_THREE); break;
3395 case 2: ADDOP(c, ROT_FOUR); break;
3396 }
3397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398
3399 switch (ctx) {
3400 case AugLoad: /* fall through to Load */
3401 case Load: op = SLICE; break;
3402 case AugStore:/* fall through to Store */
3403 case Store: op = STORE_SLICE; break;
3404 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003405 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003406 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003407 PyErr_SetString(PyExc_SystemError,
3408 "param invalid in simple slice");
3409 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 }
3411
3412 ADDOP(c, op + slice_offset);
3413 return 1;
3414}
3415
3416static int
3417compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3418 expr_context_ty ctx)
3419{
3420 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421 case Slice_kind:
3422 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 case Index_kind:
3424 VISIT(c, expr, s->v.Index.value);
3425 break;
3426 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003427 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003428 PyErr_SetString(PyExc_SystemError,
3429 "extended slice invalid in nested slice");
3430 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003431 }
3432 return 1;
3433}
3434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435static int
3436compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3437{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003438 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003440 case Index_kind:
3441 kindname = "index";
3442 if (ctx != AugStore) {
3443 VISIT(c, expr, s->v.Index.value);
3444 }
3445 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003447 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003448 if (!s->v.Slice.step)
3449 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003450 if (ctx != AugStore) {
3451 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 return 0;
3453 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003454 break;
3455 case ExtSlice_kind:
3456 kindname = "extended slice";
3457 if (ctx != AugStore) {
3458 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3459 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003460 slice_ty sub = (slice_ty)asdl_seq_GET(
3461 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003462 if (!compiler_visit_nested_slice(c, sub, ctx))
3463 return 0;
3464 }
3465 ADDOP_I(c, BUILD_TUPLE, n);
3466 }
3467 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003468 default:
3469 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003470 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003471 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003473 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474}
3475
Thomas Wouters89f507f2006-12-13 04:49:30 +00003476
3477/* End of the compiler section, beginning of the assembler section */
3478
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479/* do depth-first search of basic block graph, starting with block.
3480 post records the block indices in post-order.
3481
3482 XXX must handle implicit jumps from one block to next
3483*/
3484
Thomas Wouters89f507f2006-12-13 04:49:30 +00003485struct assembler {
3486 PyObject *a_bytecode; /* string containing bytecode */
3487 int a_offset; /* offset into bytecode */
3488 int a_nblocks; /* number of reachable blocks */
3489 basicblock **a_postorder; /* list of blocks in dfs postorder */
3490 PyObject *a_lnotab; /* string containing lnotab */
3491 int a_lnotab_off; /* offset into lnotab */
3492 int a_lineno; /* last lineno of emitted instruction */
3493 int a_lineno_off; /* bytecode offset of last lineno */
3494};
3495
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496static void
3497dfs(struct compiler *c, basicblock *b, struct assembler *a)
3498{
3499 int i;
3500 struct instr *instr = NULL;
3501
3502 if (b->b_seen)
3503 return;
3504 b->b_seen = 1;
3505 if (b->b_next != NULL)
3506 dfs(c, b->b_next, a);
3507 for (i = 0; i < b->b_iused; i++) {
3508 instr = &b->b_instr[i];
3509 if (instr->i_jrel || instr->i_jabs)
3510 dfs(c, instr->i_target, a);
3511 }
3512 a->a_postorder[a->a_nblocks++] = b;
3513}
3514
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003515static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3517{
3518 int i;
3519 struct instr *instr;
3520 if (b->b_seen || b->b_startdepth >= depth)
3521 return maxdepth;
3522 b->b_seen = 1;
3523 b->b_startdepth = depth;
3524 for (i = 0; i < b->b_iused; i++) {
3525 instr = &b->b_instr[i];
3526 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3527 if (depth > maxdepth)
3528 maxdepth = depth;
3529 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3530 if (instr->i_jrel || instr->i_jabs) {
3531 maxdepth = stackdepth_walk(c, instr->i_target,
3532 depth, maxdepth);
3533 if (instr->i_opcode == JUMP_ABSOLUTE ||
3534 instr->i_opcode == JUMP_FORWARD) {
3535 goto out; /* remaining code is dead */
3536 }
3537 }
3538 }
3539 if (b->b_next)
3540 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3541out:
3542 b->b_seen = 0;
3543 return maxdepth;
3544}
3545
3546/* Find the flow path that needs the largest stack. We assume that
3547 * cycles in the flow graph have no net effect on the stack depth.
3548 */
3549static int
3550stackdepth(struct compiler *c)
3551{
3552 basicblock *b, *entryblock;
3553 entryblock = NULL;
3554 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3555 b->b_seen = 0;
3556 b->b_startdepth = INT_MIN;
3557 entryblock = b;
3558 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003559 if (!entryblock)
3560 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561 return stackdepth_walk(c, entryblock, 0, 0);
3562}
3563
3564static int
3565assemble_init(struct assembler *a, int nblocks, int firstlineno)
3566{
3567 memset(a, 0, sizeof(struct assembler));
3568 a->a_lineno = firstlineno;
3569 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3570 if (!a->a_bytecode)
3571 return 0;
3572 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3573 if (!a->a_lnotab)
3574 return 0;
3575 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003576 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003577 if (!a->a_postorder) {
3578 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003580 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581 return 1;
3582}
3583
3584static void
3585assemble_free(struct assembler *a)
3586{
3587 Py_XDECREF(a->a_bytecode);
3588 Py_XDECREF(a->a_lnotab);
3589 if (a->a_postorder)
3590 PyObject_Free(a->a_postorder);
3591}
3592
3593/* Return the size of a basic block in bytes. */
3594
3595static int
3596instrsize(struct instr *instr)
3597{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003598 if (!instr->i_hasarg)
3599 return 1;
3600 if (instr->i_oparg > 0xffff)
3601 return 6;
3602 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603}
3604
3605static int
3606blocksize(basicblock *b)
3607{
3608 int i;
3609 int size = 0;
3610
3611 for (i = 0; i < b->b_iused; i++)
3612 size += instrsize(&b->b_instr[i]);
3613 return size;
3614}
3615
3616/* All about a_lnotab.
3617
3618c_lnotab is an array of unsigned bytes disguised as a Python string.
3619It is used to map bytecode offsets to source code line #s (when needed
3620for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003621
Tim Peters2a7f3842001-06-09 09:26:21 +00003622The array is conceptually a list of
3623 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003624pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003625
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003626 byte code offset source code line number
3627 0 1
3628 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003629 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003630 350 307
3631 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003632
3633The first trick is that these numbers aren't stored, only the increments
3634from one row to the next (this doesn't really work, but it's a start):
3635
3636 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3637
3638The second trick is that an unsigned byte can't hold negative values, or
3639values larger than 255, so (a) there's a deep assumption that byte code
3640offsets and their corresponding line #s both increase monotonically, and (b)
3641if at least one column jumps by more than 255 from one row to the next, more
3642than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003643from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003644part. A user of c_lnotab desiring to find the source line number
3645corresponding to a bytecode address A should do something like this
3646
3647 lineno = addr = 0
3648 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003649 addr += addr_incr
3650 if addr > A:
3651 return lineno
3652 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003653
3654In order for this to work, when the addr field increments by more than 255,
3655the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003656increment is < 256. So, in the example above, assemble_lnotab (it used
3657to be called com_set_lineno) should not (as was actually done until 2.2)
3658expand 300, 300 to 255, 255, 45, 45,
3659 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003660*/
3661
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003662static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003664{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 int d_bytecode, d_lineno;
3666 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003667 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668
3669 d_bytecode = a->a_offset - a->a_lineno_off;
3670 d_lineno = i->i_lineno - a->a_lineno;
3671
3672 assert(d_bytecode >= 0);
3673 assert(d_lineno >= 0);
3674
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003675 /* XXX(nnorwitz): is there a better way to handle this?
3676 for loops are special, we want to be able to trace them
3677 each time around, so we need to set an extra line number. */
3678 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003679 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003682 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 nbytes = a->a_lnotab_off + 2 * ncodes;
3684 len = PyString_GET_SIZE(a->a_lnotab);
3685 if (nbytes >= len) {
3686 if (len * 2 < nbytes)
3687 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003688 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 len *= 2;
3690 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3691 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003692 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003693 lnotab = (unsigned char *)
3694 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003695 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 *lnotab++ = 255;
3697 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003698 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 d_bytecode -= ncodes * 255;
3700 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003701 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 assert(d_bytecode <= 255);
3703 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003704 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 nbytes = a->a_lnotab_off + 2 * ncodes;
3706 len = PyString_GET_SIZE(a->a_lnotab);
3707 if (nbytes >= len) {
3708 if (len * 2 < nbytes)
3709 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003710 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 len *= 2;
3712 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3713 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003714 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003715 lnotab = (unsigned char *)
3716 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003718 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003720 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003722 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003723 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 d_lineno -= ncodes * 255;
3725 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003726 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003727
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 len = PyString_GET_SIZE(a->a_lnotab);
3729 if (a->a_lnotab_off + 2 >= len) {
3730 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003731 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003732 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003733 lnotab = (unsigned char *)
3734 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003735
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003736 a->a_lnotab_off += 2;
3737 if (d_bytecode) {
3738 *lnotab++ = d_bytecode;
3739 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003740 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003741 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742 *lnotab++ = 0;
3743 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003744 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 a->a_lineno = i->i_lineno;
3746 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003747 return 1;
3748}
3749
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750/* assemble_emit()
3751 Extend the bytecode with a new instruction.
3752 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003753*/
3754
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003755static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003757{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003758 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003759 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 char *code;
3761
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003762 size = instrsize(i);
3763 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003765 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003766 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003768 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 if (a->a_offset + size >= len) {
3770 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003771 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3774 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003775 if (size == 6) {
3776 assert(i->i_hasarg);
3777 *code++ = (char)EXTENDED_ARG;
3778 *code++ = ext & 0xff;
3779 *code++ = ext >> 8;
3780 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003781 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003783 if (i->i_hasarg) {
3784 assert(size == 3 || size == 6);
3785 *code++ = arg & 0xff;
3786 *code++ = arg >> 8;
3787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003789}
3790
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003791static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003793{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003795 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003796 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003797
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 /* Compute the size of each block and fixup jump args.
3799 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003800start:
3801 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003803 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804 bsize = blocksize(b);
3805 b->b_offset = totsize;
3806 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003807 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003808 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3810 bsize = b->b_offset;
3811 for (i = 0; i < b->b_iused; i++) {
3812 struct instr *instr = &b->b_instr[i];
3813 /* Relative jumps are computed relative to
3814 the instruction pointer after fetching
3815 the jump instruction.
3816 */
3817 bsize += instrsize(instr);
3818 if (instr->i_jabs)
3819 instr->i_oparg = instr->i_target->b_offset;
3820 else if (instr->i_jrel) {
3821 int delta = instr->i_target->b_offset - bsize;
3822 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003823 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003824 else
3825 continue;
3826 if (instr->i_oparg > 0xffff)
3827 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003828 }
3829 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003830
3831 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003832 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003833 with a better solution.
3834
3835 In the meantime, should the goto be dropped in favor
3836 of a loop?
3837
3838 The issue is that in the first loop blocksize() is called
3839 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003840 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003841 i_oparg is calculated in the second loop above.
3842
3843 So we loop until we stop seeing new EXTENDED_ARGs.
3844 The only EXTENDED_ARGs that could be popping up are
3845 ones in jump instructions. So this should converge
3846 fairly quickly.
3847 */
3848 if (last_extended_arg_count != extended_arg_count) {
3849 last_extended_arg_count = extended_arg_count;
3850 goto start;
3851 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003852}
3853
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003854static PyObject *
3855dict_keys_inorder(PyObject *dict, int offset)
3856{
3857 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003858 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003859
3860 tuple = PyTuple_New(size);
3861 if (tuple == NULL)
3862 return NULL;
3863 while (PyDict_Next(dict, &pos, &k, &v)) {
3864 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003865 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003866 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003867 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003868 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003869 PyTuple_SET_ITEM(tuple, i - offset, k);
3870 }
3871 return tuple;
3872}
3873
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003874static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003876{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 PySTEntryObject *ste = c->u->u_ste;
3878 int flags = 0, n;
3879 if (ste->ste_type != ModuleBlock)
3880 flags |= CO_NEWLOCALS;
3881 if (ste->ste_type == FunctionBlock) {
3882 if (!ste->ste_unoptimized)
3883 flags |= CO_OPTIMIZED;
3884 if (ste->ste_nested)
3885 flags |= CO_NESTED;
3886 if (ste->ste_generator)
3887 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003888 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889 if (ste->ste_varargs)
3890 flags |= CO_VARARGS;
3891 if (ste->ste_varkeywords)
3892 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003893 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003894 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003895
3896 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003897 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003898
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003899 n = PyDict_Size(c->u->u_freevars);
3900 if (n < 0)
3901 return -1;
3902 if (n == 0) {
3903 n = PyDict_Size(c->u->u_cellvars);
3904 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003905 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003906 if (n == 0) {
3907 flags |= CO_NOFREE;
3908 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003909 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003910
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003911 return flags;
3912}
3913
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003914static PyCodeObject *
3915makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003916{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 PyObject *tmp;
3918 PyCodeObject *co = NULL;
3919 PyObject *consts = NULL;
3920 PyObject *names = NULL;
3921 PyObject *varnames = NULL;
3922 PyObject *filename = NULL;
3923 PyObject *name = NULL;
3924 PyObject *freevars = NULL;
3925 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003926 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003928
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929 tmp = dict_keys_inorder(c->u->u_consts, 0);
3930 if (!tmp)
3931 goto error;
3932 consts = PySequence_List(tmp); /* optimize_code requires a list */
3933 Py_DECREF(tmp);
3934
3935 names = dict_keys_inorder(c->u->u_names, 0);
3936 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3937 if (!consts || !names || !varnames)
3938 goto error;
3939
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003940 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3941 if (!cellvars)
3942 goto error;
3943 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3944 if (!freevars)
3945 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 filename = PyString_FromString(c->c_filename);
3947 if (!filename)
3948 goto error;
3949
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003950 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951 flags = compute_code_flags(c);
3952 if (flags < 0)
3953 goto error;
3954
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003955 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 if (!bytecode)
3957 goto error;
3958
3959 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3960 if (!tmp)
3961 goto error;
3962 Py_DECREF(consts);
3963 consts = tmp;
3964
Guido van Rossum4f72a782006-10-27 23:31:49 +00003965 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3966 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967 bytecode, consts, names, varnames,
3968 freevars, cellvars,
3969 filename, c->u->u_name,
3970 c->u->u_firstlineno,
3971 a->a_lnotab);
3972 error:
3973 Py_XDECREF(consts);
3974 Py_XDECREF(names);
3975 Py_XDECREF(varnames);
3976 Py_XDECREF(filename);
3977 Py_XDECREF(name);
3978 Py_XDECREF(freevars);
3979 Py_XDECREF(cellvars);
3980 Py_XDECREF(bytecode);
3981 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003982}
3983
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003984
3985/* For debugging purposes only */
3986#if 0
3987static void
3988dump_instr(const struct instr *i)
3989{
3990 const char *jrel = i->i_jrel ? "jrel " : "";
3991 const char *jabs = i->i_jabs ? "jabs " : "";
3992 char arg[128];
3993
3994 *arg = '\0';
3995 if (i->i_hasarg)
3996 sprintf(arg, "arg: %d ", i->i_oparg);
3997
3998 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3999 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4000}
4001
4002static void
4003dump_basicblock(const basicblock *b)
4004{
4005 const char *seen = b->b_seen ? "seen " : "";
4006 const char *b_return = b->b_return ? "return " : "";
4007 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4008 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4009 if (b->b_instr) {
4010 int i;
4011 for (i = 0; i < b->b_iused; i++) {
4012 fprintf(stderr, " [%02d] ", i);
4013 dump_instr(b->b_instr + i);
4014 }
4015 }
4016}
4017#endif
4018
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019static PyCodeObject *
4020assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004021{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 basicblock *b, *entryblock;
4023 struct assembler a;
4024 int i, j, nblocks;
4025 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027 /* Make sure every block that falls off the end returns None.
4028 XXX NEXT_BLOCK() isn't quite right, because if the last
4029 block ends with a jump or return b_next shouldn't set.
4030 */
4031 if (!c->u->u_curblock->b_return) {
4032 NEXT_BLOCK(c);
4033 if (addNone)
4034 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4035 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004036 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004037
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038 nblocks = 0;
4039 entryblock = NULL;
4040 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4041 nblocks++;
4042 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004043 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004044
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004045 /* Set firstlineno if it wasn't explicitly set. */
4046 if (!c->u->u_firstlineno) {
4047 if (entryblock && entryblock->b_instr)
4048 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4049 else
4050 c->u->u_firstlineno = 1;
4051 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004052 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4053 goto error;
4054 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004055
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004057 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059 /* Emit code in reverse postorder from dfs. */
4060 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004061 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004062 for (j = 0; j < b->b_iused; j++)
4063 if (!assemble_emit(&a, &b->b_instr[j]))
4064 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004065 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4068 goto error;
4069 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4070 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004071
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072 co = makecode(c, &a);
4073 error:
4074 assemble_free(&a);
4075 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004076}