blob: fb8fb523810d27005b355334a7f52ef4b52730b7 [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
Guido van Rossumd8faa362007-04-27 19:54:29 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 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
Nick Coghlan650f0d02007-04-15 12:05:43 +000042#define COMP_GENEXP 0
43#define COMP_LISTCOMP 1
44#define COMP_SETCOMP 2
45
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000047 unsigned i_jabs : 1;
48 unsigned i_jrel : 1;
49 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000071 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000073 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000077 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078} basicblock;
79
80/* fblockinfo tracks the current frame block.
81
Jeremy Hyltone9357b22006-03-01 15:47:05 +000082A frame block is used to handle loops, try/except, and try/finally.
83It's called a frame block to distinguish it from a basic block in the
84compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085*/
86
87enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
88
89struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000090 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 basicblock *fb_block;
92};
93
94/* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
96*/
97struct compiler_unit {
98 PySTEntryObject *u_ste;
99
100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000102 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103 the argument for opcodes that refer to those collections.
104 */
105 PyObject *u_consts; /* all constants */
106 PyObject *u_names; /* all names */
107 PyObject *u_varnames; /* local variables */
108 PyObject *u_cellvars; /* cell variables */
109 PyObject *u_freevars; /* free variables */
110
111 PyObject *u_private; /* for private name mangling */
112
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000113 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000114 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 /* Pointer to the most recently allocated block. By following b_list
116 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
121 int u_nfblocks;
122 struct fblockinfo u_fblock[CO_MAXBLOCKS];
123
124 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000125 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000126 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 has been generated with current lineno */
128};
129
130/* This struct captures the global state of a compilation.
131
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132The u pointer points to the current compilation unit, while units
133for enclosing blocks are stored in c_stack. The u and c_stack are
134managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135*/
136
137struct compiler {
138 const char *c_filename;
139 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141 PyCompilerFlags *c_flags;
142
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000149 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150};
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152static int compiler_enter_scope(struct compiler *, identifier, void *, int);
153static void compiler_free(struct compiler *);
154static basicblock *compiler_new_block(struct compiler *);
155static int compiler_next_instr(struct compiler *, basicblock *);
156static int compiler_addop(struct compiler *, int);
157static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158static int compiler_addop_i(struct compiler *, int, int);
159static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static basicblock *compiler_use_new_block(struct compiler *);
161static int compiler_error(struct compiler *, const char *);
162static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
163
164static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
165static int compiler_visit_stmt(struct compiler *, stmt_ty);
166static int compiler_visit_keyword(struct compiler *, keyword_ty);
167static int compiler_visit_expr(struct compiler *, expr_ty);
168static int compiler_augassign(struct compiler *, stmt_ty);
169static int compiler_visit_slice(struct compiler *, slice_ty,
170 expr_context_ty);
171
172static int compiler_push_fblock(struct compiler *, enum fblocktype,
173 basicblock *);
174static void compiler_pop_fblock(struct compiler *, enum fblocktype,
175 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176/* Returns true if there is a loop on the fblock stack. */
177static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int inplace_binop(struct compiler *, operator_ty);
180static int expr_constant(expr_ty e);
181
Guido van Rossumc2e20742006-02-27 22:32:47 +0000182static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000183static int compiler_call_helper(struct compiler *c, int n,
184 asdl_seq *args,
185 asdl_seq *keywords,
186 expr_ty starargs,
187 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static PyCodeObject *assemble(struct compiler *, int addNone);
190static PyObject *__doc__;
191
192PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000194{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 /* Name mangling: __private becomes _classname__private.
196 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000197 const char *p, *name = PyString_AsString(ident);
198 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 size_t nlen, plen;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000200 if (privateobj == NULL || !PyString_Check(privateobj) ||
201 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000202 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 nlen = strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207 /* Don't mangle __id__ or names with dots.
208
209 The only time a name with a dot can occur is when
210 we are compiling an import statement that has a
211 package name.
212
213 TODO(jhylton): Decide whether we want to support
214 mangling of the module name, e.g. __M.X.
215 */
216 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
217 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000218 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 /* Strip leading underscores from class name */
222 while (*p == '_')
223 p++;
224 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000225 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000227 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 plen = strlen(p);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000229 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
230 if (!ident)
231 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000233 buffer = PyString_AS_STRING(ident);
234 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 strncpy(buffer+1, p, plen);
236 strcpy(buffer+1+plen, name);
237 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000238}
239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240static int
241compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000242{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 c->c_stack = PyList_New(0);
246 if (!c->c_stack)
247 return 0;
248
249 return 1;
250}
251
252PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000253PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000254 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255{
256 struct compiler c;
257 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000258 PyCompilerFlags local_flags;
259 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000261 if (!__doc__) {
262 __doc__ = PyString_InternFromString("__doc__");
263 if (!__doc__)
264 return NULL;
265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266
267 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000270 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 c.c_future = PyFuture_FromAST(mod, filename);
272 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000273 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000275 local_flags.cf_flags = 0;
276 flags = &local_flags;
277 }
278 merged = c.c_future->ff_features | flags->cf_flags;
279 c.c_future->ff_features = merged;
280 flags->cf_flags = merged;
281 c.c_flags = flags;
282 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283
284 c.c_st = PySymtable_Build(mod, filename, c.c_future);
285 if (c.c_st == NULL) {
286 if (!PyErr_Occurred())
287 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000288 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 }
290
291 /* XXX initialize to NULL for now, need to handle */
292 c.c_encoding = NULL;
293
294 co = compiler_mod(&c, mod);
295
Thomas Wouters1175c432006-02-27 22:49:54 +0000296 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000298 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 return co;
300}
301
302PyCodeObject *
303PyNode_Compile(struct _node *n, const char *filename)
304{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000305 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000307 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308 if (!arena)
309 return NULL;
310 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000311 if (mod)
312 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000313 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000314 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000315}
316
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000317static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 if (c->c_st)
321 PySymtable_Free(c->c_st);
322 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000325}
326
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000329{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 PyObject *v, *k;
332 PyObject *dict = PyDict_New();
333 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335 n = PyList_Size(list);
336 for (i = 0; i < n; i++) {
337 v = PyInt_FromLong(i);
338 if (!v) {
339 Py_DECREF(dict);
340 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000341 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000342 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000343 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
345 Py_XDECREF(k);
346 Py_DECREF(v);
347 Py_DECREF(dict);
348 return NULL;
349 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000350 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 return dict;
354}
355
356/* Return new dict containing names from src that match scope(s).
357
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000358src is a symbol table dictionary. If the scope of a name matches
359either scope_type or flag is set, insert it into the new dict. The
360values are integers, starting at offset and increasing by one for
361each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362*/
363
364static PyObject *
365dictbytype(PyObject *src, int scope_type, int flag, int offset)
366{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000367 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 PyObject *k, *v, *dest = PyDict_New();
369
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000370 assert(offset >= 0);
371 if (dest == NULL)
372 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373
374 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000375 /* XXX this should probably be a macro in symtable.h */
376 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000377 scope = (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000379 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
380 PyObject *tuple, *item = PyInt_FromLong(i);
381 if (item == NULL) {
382 Py_DECREF(dest);
383 return NULL;
384 }
385 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000387 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
388 Py_DECREF(item);
389 Py_DECREF(dest);
390 Py_XDECREF(tuple);
391 return NULL;
392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000394 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 }
397 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000398}
399
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400static void
401compiler_unit_check(struct compiler_unit *u)
402{
403 basicblock *block;
404 for (block = u->u_blocks; block != NULL; block = block->b_list) {
405 assert(block != (void *)0xcbcbcbcb);
406 assert(block != (void *)0xfbfbfbfb);
407 assert(block != (void *)0xdbdbdbdb);
408 if (block->b_instr != NULL) {
409 assert(block->b_ialloc > 0);
410 assert(block->b_iused > 0);
411 assert(block->b_ialloc >= block->b_iused);
412 }
413 else {
414 assert (block->b_iused == 0);
415 assert (block->b_ialloc == 0);
416 }
417 }
418}
419
420static void
421compiler_unit_free(struct compiler_unit *u)
422{
423 basicblock *b, *next;
424
425 compiler_unit_check(u);
426 b = u->u_blocks;
427 while (b != NULL) {
428 if (b->b_instr)
429 PyObject_Free((void *)b->b_instr);
430 next = b->b_list;
431 PyObject_Free((void *)b);
432 b = next;
433 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000434 Py_CLEAR(u->u_ste);
435 Py_CLEAR(u->u_name);
436 Py_CLEAR(u->u_consts);
437 Py_CLEAR(u->u_names);
438 Py_CLEAR(u->u_varnames);
439 Py_CLEAR(u->u_freevars);
440 Py_CLEAR(u->u_cellvars);
441 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 PyObject_Free(u);
443}
444
445static int
446compiler_enter_scope(struct compiler *c, identifier name, void *key,
447 int lineno)
448{
449 struct compiler_unit *u;
450
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000452 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000453 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000454 PyErr_NoMemory();
455 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000456 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000459 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460 u->u_ste = PySymtable_Lookup(c->c_st, key);
461 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000462 compiler_unit_free(u);
463 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464 }
465 Py_INCREF(name);
466 u->u_name = name;
467 u->u_varnames = list2dict(u->u_ste->ste_varnames);
468 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000469 if (!u->u_varnames || !u->u_cellvars) {
470 compiler_unit_free(u);
471 return 0;
472 }
473
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000475 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000476 if (!u->u_freevars) {
477 compiler_unit_free(u);
478 return 0;
479 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480
481 u->u_blocks = NULL;
482 u->u_tmpname = 0;
483 u->u_nfblocks = 0;
484 u->u_firstlineno = lineno;
485 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000486 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 u->u_consts = PyDict_New();
488 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000489 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 return 0;
491 }
492 u->u_names = PyDict_New();
493 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000494 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 return 0;
496 }
497
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499
500 /* Push the old compiler_unit on the stack. */
501 if (c->u) {
502 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
504 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000505 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 return 0;
507 }
508 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000509 u->u_private = c->u->u_private;
510 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 }
512 c->u = u;
513
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000514 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000515 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 return 0;
517
518 return 1;
519}
520
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000521static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522compiler_exit_scope(struct compiler *c)
523{
524 int n;
525 PyObject *wrapper;
526
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000527 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 compiler_unit_free(c->u);
529 /* Restore c->u to the parent unit. */
530 n = PyList_GET_SIZE(c->c_stack) - 1;
531 if (n >= 0) {
532 wrapper = PyList_GET_ITEM(c->c_stack, n);
533 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000534 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000535 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000537 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 compiler_unit_check(c->u);
539 }
540 else
541 c->u = NULL;
542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543}
544
Guido van Rossumc2e20742006-02-27 22:32:47 +0000545/* Allocate a new "anonymous" local variable.
546 Used by list comprehensions and with statements.
547*/
548
549static PyObject *
550compiler_new_tmpname(struct compiler *c)
551{
552 char tmpname[256];
553 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
554 return PyString_FromString(tmpname);
555}
556
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557/* Allocate a new block and return a pointer to it.
558 Returns NULL on error.
559*/
560
561static basicblock *
562compiler_new_block(struct compiler *c)
563{
564 basicblock *b;
565 struct compiler_unit *u;
566
567 u = c->u;
568 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000569 if (b == NULL) {
570 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000572 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000574 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 b->b_list = u->u_blocks;
576 u->u_blocks = b;
577 return b;
578}
579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580static basicblock *
581compiler_use_new_block(struct compiler *c)
582{
583 basicblock *block = compiler_new_block(c);
584 if (block == NULL)
585 return NULL;
586 c->u->u_curblock = block;
587 return block;
588}
589
590static basicblock *
591compiler_next_block(struct compiler *c)
592{
593 basicblock *block = compiler_new_block(c);
594 if (block == NULL)
595 return NULL;
596 c->u->u_curblock->b_next = block;
597 c->u->u_curblock = block;
598 return block;
599}
600
601static basicblock *
602compiler_use_next_block(struct compiler *c, basicblock *block)
603{
604 assert(block != NULL);
605 c->u->u_curblock->b_next = block;
606 c->u->u_curblock = block;
607 return block;
608}
609
610/* Returns the offset of the next instruction in the current block's
611 b_instr array. Resizes the b_instr as necessary.
612 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000613*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
615static int
616compiler_next_instr(struct compiler *c, basicblock *b)
617{
618 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000619 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000621 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (b->b_instr == NULL) {
623 PyErr_NoMemory();
624 return -1;
625 }
626 b->b_ialloc = DEFAULT_BLOCK_SIZE;
627 memset((char *)b->b_instr, 0,
628 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632 size_t oldsize, newsize;
633 oldsize = b->b_ialloc * sizeof(struct instr);
634 newsize = oldsize << 1;
635 if (newsize == 0) {
636 PyErr_NoMemory();
637 return -1;
638 }
639 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000641 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 if (tmp == NULL) {
643 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645 }
646 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
648 }
649 return b->b_iused++;
650}
651
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652/* Set the i_lineno member of the instruction at offse off if the
653 line number for the current expression/statement (?) has not
654 already been set. If it has been set, the call has no effect.
655
656 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000657*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659static void
660compiler_set_lineno(struct compiler *c, int off)
661{
662 basicblock *b;
663 if (c->u->u_lineno_set)
664 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000665 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000667 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668}
669
670static int
671opcode_stack_effect(int opcode, int oparg)
672{
673 switch (opcode) {
674 case POP_TOP:
675 return -1;
676 case ROT_TWO:
677 case ROT_THREE:
678 return 0;
679 case DUP_TOP:
680 return 1;
681 case ROT_FOUR:
682 return 0;
683
684 case UNARY_POSITIVE:
685 case UNARY_NEGATIVE:
686 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 case UNARY_INVERT:
688 return 0;
689
Nick Coghlan650f0d02007-04-15 12:05:43 +0000690 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000691 case LIST_APPEND:
692 return -2;
693
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 case BINARY_POWER:
695 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 case BINARY_MODULO:
697 case BINARY_ADD:
698 case BINARY_SUBTRACT:
699 case BINARY_SUBSCR:
700 case BINARY_FLOOR_DIVIDE:
701 case BINARY_TRUE_DIVIDE:
702 return -1;
703 case INPLACE_FLOOR_DIVIDE:
704 case INPLACE_TRUE_DIVIDE:
705 return -1;
706
707 case SLICE+0:
708 return 1;
709 case SLICE+1:
710 return 0;
711 case SLICE+2:
712 return 0;
713 case SLICE+3:
714 return -1;
715
716 case STORE_SLICE+0:
717 return -2;
718 case STORE_SLICE+1:
719 return -3;
720 case STORE_SLICE+2:
721 return -3;
722 case STORE_SLICE+3:
723 return -4;
724
725 case DELETE_SLICE+0:
726 return -1;
727 case DELETE_SLICE+1:
728 return -2;
729 case DELETE_SLICE+2:
730 return -2;
731 case DELETE_SLICE+3:
732 return -3;
733
734 case INPLACE_ADD:
735 case INPLACE_SUBTRACT:
736 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 case INPLACE_MODULO:
738 return -1;
739 case STORE_SUBSCR:
740 return -3;
741 case DELETE_SUBSCR:
742 return -2;
743
744 case BINARY_LSHIFT:
745 case BINARY_RSHIFT:
746 case BINARY_AND:
747 case BINARY_XOR:
748 case BINARY_OR:
749 return -1;
750 case INPLACE_POWER:
751 return -1;
752 case GET_ITER:
753 return 0;
754
755 case PRINT_EXPR:
756 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000757 case LOAD_BUILD_CLASS:
758 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 case INPLACE_LSHIFT:
760 case INPLACE_RSHIFT:
761 case INPLACE_AND:
762 case INPLACE_XOR:
763 case INPLACE_OR:
764 return -1;
765 case BREAK_LOOP:
766 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000767 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000768 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000769 case STORE_LOCALS:
770 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 case RETURN_VALUE:
772 return -1;
773 case IMPORT_STAR:
774 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 case YIELD_VALUE:
776 return 0;
777
778 case POP_BLOCK:
779 return 0;
780 case END_FINALLY:
781 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782
783 case STORE_NAME:
784 return -1;
785 case DELETE_NAME:
786 return 0;
787 case UNPACK_SEQUENCE:
788 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000789 case UNPACK_EX:
790 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 case FOR_ITER:
792 return 1;
793
794 case STORE_ATTR:
795 return -2;
796 case DELETE_ATTR:
797 return -1;
798 case STORE_GLOBAL:
799 return -1;
800 case DELETE_GLOBAL:
801 return 0;
802 case DUP_TOPX:
803 return oparg;
804 case LOAD_CONST:
805 return 1;
806 case LOAD_NAME:
807 return 1;
808 case BUILD_TUPLE:
809 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000810 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 return 1-oparg;
812 case BUILD_MAP:
813 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000814 case MAKE_BYTES:
815 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 case LOAD_ATTR:
817 return 0;
818 case COMPARE_OP:
819 return -1;
820 case IMPORT_NAME:
821 return 0;
822 case IMPORT_FROM:
823 return 1;
824
825 case JUMP_FORWARD:
826 case JUMP_IF_FALSE:
827 case JUMP_IF_TRUE:
828 case JUMP_ABSOLUTE:
829 return 0;
830
831 case LOAD_GLOBAL:
832 return 1;
833
834 case CONTINUE_LOOP:
835 return 0;
836 case SETUP_LOOP:
837 return 0;
838 case SETUP_EXCEPT:
839 case SETUP_FINALLY:
840 return 3; /* actually pushed by an exception */
841
842 case LOAD_FAST:
843 return 1;
844 case STORE_FAST:
845 return -1;
846 case DELETE_FAST:
847 return 0;
848
849 case RAISE_VARARGS:
850 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000851#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 case CALL_FUNCTION:
853 return -NARGS(oparg);
854 case CALL_FUNCTION_VAR:
855 case CALL_FUNCTION_KW:
856 return -NARGS(oparg)-1;
857 case CALL_FUNCTION_VAR_KW:
858 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000860 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000861 case MAKE_CLOSURE:
862 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000863#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 case BUILD_SLICE:
865 if (oparg == 3)
866 return -2;
867 else
868 return -1;
869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 case LOAD_CLOSURE:
871 return 1;
872 case LOAD_DEREF:
873 return 1;
874 case STORE_DEREF:
875 return -1;
876 default:
877 fprintf(stderr, "opcode = %d\n", opcode);
878 Py_FatalError("opcode_stack_effect()");
879
880 }
881 return 0; /* not reachable */
882}
883
884/* Add an opcode with no argument.
885 Returns 0 on failure, 1 on success.
886*/
887
888static int
889compiler_addop(struct compiler *c, int opcode)
890{
891 basicblock *b;
892 struct instr *i;
893 int off;
894 off = compiler_next_instr(c, c->u->u_curblock);
895 if (off < 0)
896 return 0;
897 b = c->u->u_curblock;
898 i = &b->b_instr[off];
899 i->i_opcode = opcode;
900 i->i_hasarg = 0;
901 if (opcode == RETURN_VALUE)
902 b->b_return = 1;
903 compiler_set_lineno(c, off);
904 return 1;
905}
906
907static int
908compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
909{
910 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000911 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000913 /* necessary to make sure types aren't coerced (e.g., int and long) */
914 t = PyTuple_Pack(2, o, o->ob_type);
915 if (t == NULL)
916 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
918 v = PyDict_GetItem(dict, t);
919 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000920 if (PyErr_Occurred())
921 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 arg = PyDict_Size(dict);
923 v = PyInt_FromLong(arg);
924 if (!v) {
925 Py_DECREF(t);
926 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000927 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 if (PyDict_SetItem(dict, t, v) < 0) {
929 Py_DECREF(t);
930 Py_DECREF(v);
931 return -1;
932 }
933 Py_DECREF(v);
934 }
935 else
936 arg = PyInt_AsLong(v);
937 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000938 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
941static int
942compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
943 PyObject *o)
944{
945 int arg = compiler_add_o(c, dict, o);
946 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000947 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 return compiler_addop_i(c, opcode, arg);
949}
950
951static int
952compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000953 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
955 int arg;
956 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
957 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000958 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 arg = compiler_add_o(c, dict, mangled);
960 Py_DECREF(mangled);
961 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000962 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 return compiler_addop_i(c, opcode, arg);
964}
965
966/* Add an opcode with an integer argument.
967 Returns 0 on failure, 1 on success.
968*/
969
970static int
971compiler_addop_i(struct compiler *c, int opcode, int oparg)
972{
973 struct instr *i;
974 int off;
975 off = compiler_next_instr(c, c->u->u_curblock);
976 if (off < 0)
977 return 0;
978 i = &c->u->u_curblock->b_instr[off];
979 i->i_opcode = opcode;
980 i->i_oparg = oparg;
981 i->i_hasarg = 1;
982 compiler_set_lineno(c, off);
983 return 1;
984}
985
986static int
987compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
988{
989 struct instr *i;
990 int off;
991
992 assert(b != NULL);
993 off = compiler_next_instr(c, c->u->u_curblock);
994 if (off < 0)
995 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 i = &c->u->u_curblock->b_instr[off];
997 i->i_opcode = opcode;
998 i->i_target = b;
999 i->i_hasarg = 1;
1000 if (absolute)
1001 i->i_jabs = 1;
1002 else
1003 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 return 1;
1006}
1007
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1009 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 it as the current block. NEXT_BLOCK() also creates an implicit jump
1011 from the current block to the new block.
1012*/
1013
Thomas Wouters89f507f2006-12-13 04:49:30 +00001014/* The returns inside these macros make it impossible to decref objects
1015 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016*/
1017
1018
1019#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001020 if (compiler_use_new_block((C)) == NULL) \
1021 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022}
1023
1024#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001025 if (compiler_next_block((C)) == NULL) \
1026 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027}
1028
1029#define ADDOP(C, OP) { \
1030 if (!compiler_addop((C), (OP))) \
1031 return 0; \
1032}
1033
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001034#define ADDOP_IN_SCOPE(C, OP) { \
1035 if (!compiler_addop((C), (OP))) { \
1036 compiler_exit_scope(c); \
1037 return 0; \
1038 } \
1039}
1040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041#define ADDOP_O(C, OP, O, TYPE) { \
1042 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1043 return 0; \
1044}
1045
1046#define ADDOP_NAME(C, OP, O, TYPE) { \
1047 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1048 return 0; \
1049}
1050
1051#define ADDOP_I(C, OP, O) { \
1052 if (!compiler_addop_i((C), (OP), (O))) \
1053 return 0; \
1054}
1055
1056#define ADDOP_JABS(C, OP, O) { \
1057 if (!compiler_addop_j((C), (OP), (O), 1)) \
1058 return 0; \
1059}
1060
1061#define ADDOP_JREL(C, OP, O) { \
1062 if (!compiler_addop_j((C), (OP), (O), 0)) \
1063 return 0; \
1064}
1065
1066/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1067 the ASDL name to synthesize the name of the C type and the visit function.
1068*/
1069
1070#define VISIT(C, TYPE, V) {\
1071 if (!compiler_visit_ ## TYPE((C), (V))) \
1072 return 0; \
1073}
1074
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075#define VISIT_IN_SCOPE(C, TYPE, V) {\
1076 if (!compiler_visit_ ## TYPE((C), (V))) { \
1077 compiler_exit_scope(c); \
1078 return 0; \
1079 } \
1080}
1081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082#define VISIT_SLICE(C, V, CTX) {\
1083 if (!compiler_visit_slice((C), (V), (CTX))) \
1084 return 0; \
1085}
1086
1087#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001088 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001090 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001091 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 if (!compiler_visit_ ## TYPE((C), elt)) \
1093 return 0; \
1094 } \
1095}
1096
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001097#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001098 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001099 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001100 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001102 if (!compiler_visit_ ## TYPE((C), elt)) { \
1103 compiler_exit_scope(c); \
1104 return 0; \
1105 } \
1106 } \
1107}
1108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109static int
1110compiler_isdocstring(stmt_ty s)
1111{
1112 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001113 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 return s->v.Expr.value->kind == Str_kind;
1115}
1116
1117/* Compile a sequence of statements, checking for a docstring. */
1118
1119static int
1120compiler_body(struct compiler *c, asdl_seq *stmts)
1121{
1122 int i = 0;
1123 stmt_ty st;
1124
1125 if (!asdl_seq_LEN(stmts))
1126 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 if (compiler_isdocstring(st)) {
1129 i = 1;
1130 VISIT(c, expr, st->v.Expr.value);
1131 if (!compiler_nameop(c, __doc__, Store))
1132 return 0;
1133 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001134 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 return 1;
1137}
1138
1139static PyCodeObject *
1140compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001141{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001143 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 static PyObject *module;
1145 if (!module) {
1146 module = PyString_FromString("<module>");
1147 if (!module)
1148 return NULL;
1149 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001150 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1151 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 switch (mod->kind) {
1154 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001155 if (!compiler_body(c, mod->v.Module.body)) {
1156 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001158 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 break;
1160 case Interactive_kind:
1161 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001163 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 break;
1165 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001166 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001167 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 break;
1169 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001170 PyErr_SetString(PyExc_SystemError,
1171 "suite should not be possible");
1172 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001173 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001174 PyErr_Format(PyExc_SystemError,
1175 "module kind %d should not be possible",
1176 mod->kind);
1177 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001178 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 co = assemble(c, addNone);
1180 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001181 return co;
1182}
1183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184/* The test for LOCAL must come before the test for FREE in order to
1185 handle classes where name is both local and free. The local var is
1186 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001187*/
1188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189static int
1190get_ref_type(struct compiler *c, PyObject *name)
1191{
1192 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001193 if (scope == 0) {
1194 char buf[350];
1195 PyOS_snprintf(buf, sizeof(buf),
1196 "unknown scope for %.100s in %.100s(%s) in %s\n"
1197 "symbols: %s\nlocals: %s\nglobals: %s\n",
1198 PyString_AS_STRING(name),
1199 PyString_AS_STRING(c->u->u_name),
1200 PyObject_REPR(c->u->u_ste->ste_id),
1201 c->c_filename,
1202 PyObject_REPR(c->u->u_ste->ste_symbols),
1203 PyObject_REPR(c->u->u_varnames),
1204 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001206 Py_FatalError(buf);
1207 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001208
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001209 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210}
1211
1212static int
1213compiler_lookup_arg(PyObject *dict, PyObject *name)
1214{
1215 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001216 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001218 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001220 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001222 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return PyInt_AS_LONG(v);
1224}
1225
1226static int
1227compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1228{
1229 int i, free = PyCode_GetNumFree(co);
1230 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001231 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1232 ADDOP_I(c, MAKE_FUNCTION, args);
1233 return 1;
1234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 for (i = 0; i < free; ++i) {
1236 /* Bypass com_addop_varname because it will generate
1237 LOAD_DEREF but LOAD_CLOSURE is needed.
1238 */
1239 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1240 int arg, reftype;
1241
1242 /* Special case: If a class contains a method with a
1243 free variable that has the same name as a method,
1244 the name will be considered free *and* local in the
1245 class. It should be handled by the closure, as
1246 well as by the normal name loookup logic.
1247 */
1248 reftype = get_ref_type(c, name);
1249 if (reftype == CELL)
1250 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1251 else /* (reftype == FREE) */
1252 arg = compiler_lookup_arg(c->u->u_freevars, name);
1253 if (arg == -1) {
1254 printf("lookup %s in %s %d %d\n"
1255 "freevars of %s: %s\n",
1256 PyObject_REPR(name),
1257 PyString_AS_STRING(c->u->u_name),
1258 reftype, arg,
1259 PyString_AS_STRING(co->co_name),
1260 PyObject_REPR(co->co_freevars));
1261 Py_FatalError("compiler_make_closure()");
1262 }
1263 ADDOP_I(c, LOAD_CLOSURE, arg);
1264 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001265 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001267 ADDOP_I(c, MAKE_CLOSURE, args);
1268 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269}
1270
1271static int
1272compiler_decorators(struct compiler *c, asdl_seq* decos)
1273{
1274 int i;
1275
1276 if (!decos)
1277 return 1;
1278
1279 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 }
1282 return 1;
1283}
1284
1285static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1287 asdl_seq *kw_defaults)
1288{
1289 int i, default_count = 0;
1290 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1293 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001294 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295 if (!compiler_visit_expr(c, default_)) {
1296 return -1;
1297 }
1298 default_count++;
1299 }
1300 }
1301 return default_count;
1302}
1303
1304static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001305compiler_visit_argannotation(struct compiler *c, identifier id,
1306 expr_ty annotation, PyObject *names)
1307{
1308 if (annotation) {
1309 VISIT(c, expr, annotation);
1310 if (PyList_Append(names, id))
1311 return -1;
1312 }
1313 return 0;
1314}
1315
1316static int
1317compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1318 PyObject *names)
1319{
1320 int i, error;
1321 for (i = 0; i < asdl_seq_LEN(args); i++) {
1322 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001323 error = compiler_visit_argannotation(
1324 c,
1325 arg->arg,
1326 arg->annotation,
1327 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 if (error)
1329 return error;
1330 }
1331 return 0;
1332}
1333
1334static int
1335compiler_visit_annotations(struct compiler *c, arguments_ty args,
1336 expr_ty returns)
1337{
Guido van Rossum0240b922007-02-26 21:23:50 +00001338 /* Push arg annotations and a list of the argument names. Return the #
1339 of items pushed. The expressions are evaluated out-of-order wrt the
1340 source code.
1341
1342 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1343 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 static identifier return_str;
1345 PyObject *names;
1346 int len;
1347 names = PyList_New(0);
1348 if (!names)
1349 return -1;
1350
1351 if (compiler_visit_argannotations(c, args->args, names))
1352 goto error;
1353 if (args->varargannotation &&
1354 compiler_visit_argannotation(c, args->vararg,
1355 args->varargannotation, names))
1356 goto error;
1357 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1358 goto error;
1359 if (args->kwargannotation &&
1360 compiler_visit_argannotation(c, args->kwarg,
1361 args->kwargannotation, names))
1362 goto error;
1363
1364 if (!return_str) {
1365 return_str = PyString_InternFromString("return");
1366 if (!return_str)
1367 goto error;
1368 }
1369 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1370 goto error;
1371 }
1372
1373 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001374 if (len > 65534) {
1375 /* len must fit in 16 bits, and len is incremented below */
1376 PyErr_SetString(PyExc_SyntaxError,
1377 "too many annotations");
1378 goto error;
1379 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 if (len) {
1381 /* convert names to a tuple and place on stack */
1382 PyObject *elt;
1383 int i;
1384 PyObject *s = PyTuple_New(len);
1385 if (!s)
1386 goto error;
1387 for (i = 0; i < len; i++) {
1388 elt = PyList_GET_ITEM(names, i);
1389 Py_INCREF(elt);
1390 PyTuple_SET_ITEM(s, i, elt);
1391 }
1392 ADDOP_O(c, LOAD_CONST, s, consts);
1393 Py_DECREF(s);
1394 len++; /* include the just-pushed tuple */
1395 }
1396 Py_DECREF(names);
1397 return len;
1398
1399error:
1400 Py_DECREF(names);
1401 return -1;
1402}
1403
1404static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405compiler_function(struct compiler *c, stmt_ty s)
1406{
1407 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001408 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 expr_ty returns = s->v.FunctionDef.returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001411 asdl_seq* decos = s->v.FunctionDef.decorators;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001412 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
1416 assert(s->kind == FunctionDef_kind);
1417
1418 if (!compiler_decorators(c, decos))
1419 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 if (args->kwonlyargs) {
1421 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1422 args->kw_defaults);
1423 if (res < 0)
1424 return 0;
1425 kw_default_count = res;
1426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 if (args->defaults)
1428 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001430 if (num_annotations < 0)
1431 return 0;
1432 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001433
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1435 s->lineno))
1436 return 0;
1437
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001438 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001439 docstring = compiler_isdocstring(st);
1440 if (docstring)
1441 first_const = st->v.Expr.value->v.Str.s;
1442 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001443 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001444 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001450 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001452 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1453 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 }
1455 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001456 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 if (co == NULL)
1458 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 arglength = asdl_seq_LEN(args->defaults);
1461 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001462 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001463 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001464 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1468 ADDOP_I(c, CALL_FUNCTION, 1);
1469 }
1470
1471 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1472}
1473
1474static int
1475compiler_class(struct compiler *c, stmt_ty s)
1476{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001477 static PyObject *build_class = NULL;
1478 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001480 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001481 PySTEntryObject *ste;
Guido van Rossum3a383622007-03-21 21:26:58 +00001482 int err;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001484 /* initialize statics */
1485 if (build_class == NULL) {
1486 build_class = PyString_FromString("__build_class__");
1487 if (build_class == NULL)
1488 return 0;
1489 }
1490 if (locals == NULL) {
1491 locals = PyString_FromString("__locals__");
1492 if (locals == NULL)
1493 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001496 /* ultimately generate code for:
1497 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1498 where:
1499 <func> is a function/closure created from the class body
1500 <name> is the class name
1501 <bases> is the positional arguments and *varargs argument
1502 <keywords> is the keyword arguments and **kwds argument
1503 This borrows from compiler_call.
1504 */
1505
1506 /* 0. Create a fake variable named __locals__ */
1507 ste = PySymtable_Lookup(c->c_st, s);
1508 if (ste == NULL)
1509 return 0;
1510 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001511 err = PyList_Append(ste->ste_varnames, locals);
1512 Py_DECREF(ste);
1513 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001514 return 0;
1515
1516 /* 1. compile the class body into a code object */
1517 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1518 return 0;
1519 /* this block represents what we do in the new scope */
1520 {
1521 /* use the class name for name mangling */
1522 Py_INCREF(s->v.ClassDef.name);
1523 c->u->u_private = s->v.ClassDef.name;
1524 /* force it to have one mandatory argument */
1525 c->u->u_argcount = 1;
1526 /* load the first argument ... */
1527 ADDOP_I(c, LOAD_FAST, 0);
1528 /* ... and store it into f_locals */
1529 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1530 /* load __name__ ... */
1531 str = PyString_InternFromString("__name__");
1532 if (!str || !compiler_nameop(c, str, Load)) {
1533 Py_XDECREF(str);
1534 compiler_exit_scope(c);
1535 return 0;
1536 }
1537 Py_DECREF(str);
1538 /* ... and store it as __module__ */
1539 str = PyString_InternFromString("__module__");
1540 if (!str || !compiler_nameop(c, str, Store)) {
1541 Py_XDECREF(str);
1542 compiler_exit_scope(c);
1543 return 0;
1544 }
1545 Py_DECREF(str);
1546 /* compile the body proper */
1547 if (!compiler_body(c, s->v.ClassDef.body)) {
1548 compiler_exit_scope(c);
1549 return 0;
1550 }
1551 /* return None */
1552 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1553 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1554 /* create the code object */
1555 co = assemble(c, 1);
1556 }
1557 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001558 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001559 if (co == NULL)
1560 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001561
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001562 /* 2. load the 'build_class' function */
1563 ADDOP(c, LOAD_BUILD_CLASS);
1564
1565 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001566 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001567 Py_DECREF(co);
1568
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001569 /* 4. load class name */
1570 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1571
1572 /* 5. generate the rest of the code for the call */
1573 if (!compiler_call_helper(c, 2,
1574 s->v.ClassDef.bases,
1575 s->v.ClassDef.keywords,
1576 s->v.ClassDef.starargs,
1577 s->v.ClassDef.kwargs))
1578 return 0;
1579
1580 /* 6. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1582 return 0;
1583 return 1;
1584}
1585
1586static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001587compiler_ifexp(struct compiler *c, expr_ty e)
1588{
1589 basicblock *end, *next;
1590
1591 assert(e->kind == IfExp_kind);
1592 end = compiler_new_block(c);
1593 if (end == NULL)
1594 return 0;
1595 next = compiler_new_block(c);
1596 if (next == NULL)
1597 return 0;
1598 VISIT(c, expr, e->v.IfExp.test);
1599 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1600 ADDOP(c, POP_TOP);
1601 VISIT(c, expr, e->v.IfExp.body);
1602 ADDOP_JREL(c, JUMP_FORWARD, end);
1603 compiler_use_next_block(c, next);
1604 ADDOP(c, POP_TOP);
1605 VISIT(c, expr, e->v.IfExp.orelse);
1606 compiler_use_next_block(c, end);
1607 return 1;
1608}
1609
1610static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611compiler_lambda(struct compiler *c, expr_ty e)
1612{
1613 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001614 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001615 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616 arguments_ty args = e->v.Lambda.args;
1617 assert(e->kind == Lambda_kind);
1618
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001619 if (!name) {
1620 name = PyString_InternFromString("<lambda>");
1621 if (!name)
1622 return 0;
1623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624
Guido van Rossum4f72a782006-10-27 23:31:49 +00001625 if (args->kwonlyargs) {
1626 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1627 args->kw_defaults);
1628 if (res < 0) return 0;
1629 kw_default_count = res;
1630 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 if (args->defaults)
1632 VISIT_SEQ(c, expr, args->defaults);
1633 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1634 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001635
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001637 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001638 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1639 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001640 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001641 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 if (co == NULL)
1643 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644
Guido van Rossum4f72a782006-10-27 23:31:49 +00001645 arglength = asdl_seq_LEN(args->defaults);
1646 arglength |= kw_default_count << 8;
1647 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001648 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649
1650 return 1;
1651}
1652
1653static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654compiler_if(struct compiler *c, stmt_ty s)
1655{
1656 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001657 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658 assert(s->kind == If_kind);
1659 end = compiler_new_block(c);
1660 if (end == NULL)
1661 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001662 next = compiler_new_block(c);
1663 if (next == NULL)
1664 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001665
1666 constant = expr_constant(s->v.If.test);
1667 /* constant = 0: "if 0"
1668 * constant = 1: "if 1", "if 2", ...
1669 * constant = -1: rest */
1670 if (constant == 0) {
1671 if (s->v.If.orelse)
1672 VISIT_SEQ(c, stmt, s->v.If.orelse);
1673 } else if (constant == 1) {
1674 VISIT_SEQ(c, stmt, s->v.If.body);
1675 } else {
1676 VISIT(c, expr, s->v.If.test);
1677 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1678 ADDOP(c, POP_TOP);
1679 VISIT_SEQ(c, stmt, s->v.If.body);
1680 ADDOP_JREL(c, JUMP_FORWARD, end);
1681 compiler_use_next_block(c, next);
1682 ADDOP(c, POP_TOP);
1683 if (s->v.If.orelse)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001684 VISIT_SEQ(c, stmt, s->v.If.orelse);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 compiler_use_next_block(c, end);
1687 return 1;
1688}
1689
1690static int
1691compiler_for(struct compiler *c, stmt_ty s)
1692{
1693 basicblock *start, *cleanup, *end;
1694
1695 start = compiler_new_block(c);
1696 cleanup = compiler_new_block(c);
1697 end = compiler_new_block(c);
1698 if (start == NULL || end == NULL || cleanup == NULL)
1699 return 0;
1700 ADDOP_JREL(c, SETUP_LOOP, end);
1701 if (!compiler_push_fblock(c, LOOP, start))
1702 return 0;
1703 VISIT(c, expr, s->v.For.iter);
1704 ADDOP(c, GET_ITER);
1705 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001706 /* XXX(nnorwitz): is there a better way to handle this?
1707 for loops are special, we want to be able to trace them
1708 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001709 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710 ADDOP_JREL(c, FOR_ITER, cleanup);
1711 VISIT(c, expr, s->v.For.target);
1712 VISIT_SEQ(c, stmt, s->v.For.body);
1713 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1714 compiler_use_next_block(c, cleanup);
1715 ADDOP(c, POP_BLOCK);
1716 compiler_pop_fblock(c, LOOP, start);
1717 VISIT_SEQ(c, stmt, s->v.For.orelse);
1718 compiler_use_next_block(c, end);
1719 return 1;
1720}
1721
1722static int
1723compiler_while(struct compiler *c, stmt_ty s)
1724{
1725 basicblock *loop, *orelse, *end, *anchor = NULL;
1726 int constant = expr_constant(s->v.While.test);
1727
1728 if (constant == 0)
1729 return 1;
1730 loop = compiler_new_block(c);
1731 end = compiler_new_block(c);
1732 if (constant == -1) {
1733 anchor = compiler_new_block(c);
1734 if (anchor == NULL)
1735 return 0;
1736 }
1737 if (loop == NULL || end == NULL)
1738 return 0;
1739 if (s->v.While.orelse) {
1740 orelse = compiler_new_block(c);
1741 if (orelse == NULL)
1742 return 0;
1743 }
1744 else
1745 orelse = NULL;
1746
1747 ADDOP_JREL(c, SETUP_LOOP, end);
1748 compiler_use_next_block(c, loop);
1749 if (!compiler_push_fblock(c, LOOP, loop))
1750 return 0;
1751 if (constant == -1) {
1752 VISIT(c, expr, s->v.While.test);
1753 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1754 ADDOP(c, POP_TOP);
1755 }
1756 VISIT_SEQ(c, stmt, s->v.While.body);
1757 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1758
1759 /* XXX should the two POP instructions be in a separate block
1760 if there is no else clause ?
1761 */
1762
1763 if (constant == -1) {
1764 compiler_use_next_block(c, anchor);
1765 ADDOP(c, POP_TOP);
1766 ADDOP(c, POP_BLOCK);
1767 }
1768 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001769 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 VISIT_SEQ(c, stmt, s->v.While.orelse);
1771 compiler_use_next_block(c, end);
1772
1773 return 1;
1774}
1775
1776static int
1777compiler_continue(struct compiler *c)
1778{
1779 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001780 static const char IN_FINALLY_ERROR_MSG[] =
1781 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 int i;
1783
1784 if (!c->u->u_nfblocks)
1785 return compiler_error(c, LOOP_ERROR_MSG);
1786 i = c->u->u_nfblocks - 1;
1787 switch (c->u->u_fblock[i].fb_type) {
1788 case LOOP:
1789 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1790 break;
1791 case EXCEPT:
1792 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001793 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1794 /* Prevent continue anywhere under a finally
1795 even if hidden in a sub-try or except. */
1796 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1797 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799 if (i == -1)
1800 return compiler_error(c, LOOP_ERROR_MSG);
1801 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1802 break;
1803 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001804 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805 }
1806
1807 return 1;
1808}
1809
1810/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1811
1812 SETUP_FINALLY L
1813 <code for body>
1814 POP_BLOCK
1815 LOAD_CONST <None>
1816 L: <code for finalbody>
1817 END_FINALLY
1818
1819 The special instructions use the block stack. Each block
1820 stack entry contains the instruction that created it (here
1821 SETUP_FINALLY), the level of the value stack at the time the
1822 block stack entry was created, and a label (here L).
1823
1824 SETUP_FINALLY:
1825 Pushes the current value stack level and the label
1826 onto the block stack.
1827 POP_BLOCK:
1828 Pops en entry from the block stack, and pops the value
1829 stack until its level is the same as indicated on the
1830 block stack. (The label is ignored.)
1831 END_FINALLY:
1832 Pops a variable number of entries from the *value* stack
1833 and re-raises the exception they specify. The number of
1834 entries popped depends on the (pseudo) exception type.
1835
1836 The block stack is unwound when an exception is raised:
1837 when a SETUP_FINALLY entry is found, the exception is pushed
1838 onto the value stack (and the exception condition is cleared),
1839 and the interpreter jumps to the label gotten from the block
1840 stack.
1841*/
1842
1843static int
1844compiler_try_finally(struct compiler *c, stmt_ty s)
1845{
1846 basicblock *body, *end;
1847 body = compiler_new_block(c);
1848 end = compiler_new_block(c);
1849 if (body == NULL || end == NULL)
1850 return 0;
1851
1852 ADDOP_JREL(c, SETUP_FINALLY, end);
1853 compiler_use_next_block(c, body);
1854 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1855 return 0;
1856 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1857 ADDOP(c, POP_BLOCK);
1858 compiler_pop_fblock(c, FINALLY_TRY, body);
1859
1860 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1861 compiler_use_next_block(c, end);
1862 if (!compiler_push_fblock(c, FINALLY_END, end))
1863 return 0;
1864 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1865 ADDOP(c, END_FINALLY);
1866 compiler_pop_fblock(c, FINALLY_END, end);
1867
1868 return 1;
1869}
1870
1871/*
1872 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1873 (The contents of the value stack is shown in [], with the top
1874 at the right; 'tb' is trace-back info, 'val' the exception's
1875 associated value, and 'exc' the exception.)
1876
1877 Value stack Label Instruction Argument
1878 [] SETUP_EXCEPT L1
1879 [] <code for S>
1880 [] POP_BLOCK
1881 [] JUMP_FORWARD L0
1882
1883 [tb, val, exc] L1: DUP )
1884 [tb, val, exc, exc] <evaluate E1> )
1885 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1886 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1887 [tb, val, exc, 1] POP )
1888 [tb, val, exc] POP
1889 [tb, val] <assign to V1> (or POP if no V1)
1890 [tb] POP
1891 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001892 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893
1894 [tb, val, exc, 0] L2: POP
1895 [tb, val, exc] DUP
1896 .............................etc.......................
1897
1898 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001899 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900
1901 [] L0: <next statement>
1902
1903 Of course, parts are not generated if Vi or Ei is not present.
1904*/
1905static int
1906compiler_try_except(struct compiler *c, stmt_ty s)
1907{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001908 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 int i, n;
1910
1911 body = compiler_new_block(c);
1912 except = compiler_new_block(c);
1913 orelse = compiler_new_block(c);
1914 end = compiler_new_block(c);
1915 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1916 return 0;
1917 ADDOP_JREL(c, SETUP_EXCEPT, except);
1918 compiler_use_next_block(c, body);
1919 if (!compiler_push_fblock(c, EXCEPT, body))
1920 return 0;
1921 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1922 ADDOP(c, POP_BLOCK);
1923 compiler_pop_fblock(c, EXCEPT, body);
1924 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1925 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1926 compiler_use_next_block(c, except);
1927 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001928 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929 s->v.TryExcept.handlers, i);
1930 if (!handler->type && i < n-1)
1931 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001932 c->u->u_lineno_set = 0;
1933 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 except = compiler_new_block(c);
1935 if (except == NULL)
1936 return 0;
1937 if (handler->type) {
1938 ADDOP(c, DUP_TOP);
1939 VISIT(c, expr, handler->type);
1940 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1941 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1942 ADDOP(c, POP_TOP);
1943 }
1944 ADDOP(c, POP_TOP);
1945 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001946 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001947
1948 cleanup_end = compiler_new_block(c);
1949 cleanup_body = compiler_new_block(c);
1950 if(!(cleanup_end || cleanup_body))
1951 return 0;
1952
Guido van Rossum16be03e2007-01-10 18:51:35 +00001953 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001954 ADDOP(c, POP_TOP);
1955
1956 /*
1957 try:
1958 # body
1959 except type as name:
1960 try:
1961 # body
1962 finally:
1963 name = None
1964 del name
1965 */
1966
1967 /* second try: */
1968 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1969 compiler_use_next_block(c, cleanup_body);
1970 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1971 return 0;
1972
1973 /* second # body */
1974 VISIT_SEQ(c, stmt, handler->body);
1975 ADDOP(c, POP_BLOCK);
1976 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1977
1978 /* finally: */
1979 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1980 compiler_use_next_block(c, cleanup_end);
1981 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1982 return 0;
1983
1984 /* name = None */
1985 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00001986 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001987
Guido van Rossum16be03e2007-01-10 18:51:35 +00001988 /* del name */
1989 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00001990
1991 ADDOP(c, END_FINALLY);
1992 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993 }
1994 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00001995 ADDOP(c, POP_TOP);
1996 ADDOP(c, POP_TOP);
1997 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 ADDOP_JREL(c, JUMP_FORWARD, end);
2000 compiler_use_next_block(c, except);
2001 if (handler->type)
2002 ADDOP(c, POP_TOP);
2003 }
2004 ADDOP(c, END_FINALLY);
2005 compiler_use_next_block(c, orelse);
2006 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2007 compiler_use_next_block(c, end);
2008 return 1;
2009}
2010
2011static int
2012compiler_import_as(struct compiler *c, identifier name, identifier asname)
2013{
2014 /* The IMPORT_NAME opcode was already generated. This function
2015 merely needs to bind the result to a name.
2016
2017 If there is a dot in name, we need to split it and emit a
2018 LOAD_ATTR for each name.
2019 */
2020 const char *src = PyString_AS_STRING(name);
2021 const char *dot = strchr(src, '.');
2022 if (dot) {
2023 /* Consume the base module name to get the first attribute */
2024 src = dot + 1;
2025 while (dot) {
2026 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002027 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002029 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002031 if (!attr)
2032 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002034 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 src = dot + 1;
2036 }
2037 }
2038 return compiler_nameop(c, asname, Store);
2039}
2040
2041static int
2042compiler_import(struct compiler *c, stmt_ty s)
2043{
2044 /* The Import node stores a module name like a.b.c as a single
2045 string. This is convenient for all cases except
2046 import a.b.c as d
2047 where we need to parse that string to extract the individual
2048 module names.
2049 XXX Perhaps change the representation to make this case simpler?
2050 */
2051 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002053 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002054 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002056 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057
Guido van Rossum45aecf42006-03-15 04:58:47 +00002058 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002059 if (level == NULL)
2060 return 0;
2061
2062 ADDOP_O(c, LOAD_CONST, level, consts);
2063 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2065 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2066
2067 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002068 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002069 if (!r)
2070 return r;
2071 }
2072 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 identifier tmp = alias->name;
2074 const char *base = PyString_AS_STRING(alias->name);
2075 char *dot = strchr(base, '.');
2076 if (dot)
2077 tmp = PyString_FromStringAndSize(base,
2078 dot - base);
2079 r = compiler_nameop(c, tmp, Store);
2080 if (dot) {
2081 Py_DECREF(tmp);
2082 }
2083 if (!r)
2084 return r;
2085 }
2086 }
2087 return 1;
2088}
2089
2090static int
2091compiler_from_import(struct compiler *c, stmt_ty s)
2092{
2093 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094
2095 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002096 PyObject *level;
2097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 if (!names)
2099 return 0;
2100
Guido van Rossum45aecf42006-03-15 04:58:47 +00002101 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002102 if (!level) {
2103 Py_DECREF(names);
2104 return 0;
2105 }
2106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 /* build up the names */
2108 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002109 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 Py_INCREF(alias->name);
2111 PyTuple_SET_ITEM(names, i, alias->name);
2112 }
2113
2114 if (s->lineno > c->c_future->ff_lineno) {
2115 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2116 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002117 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 Py_DECREF(names);
2119 return compiler_error(c,
2120 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002121 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122
2123 }
2124 }
2125
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002126 ADDOP_O(c, LOAD_CONST, level, consts);
2127 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002129 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2131 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002132 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 identifier store_name;
2134
2135 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2136 assert(n == 1);
2137 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002138 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 }
2140
2141 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2142 store_name = alias->name;
2143 if (alias->asname)
2144 store_name = alias->asname;
2145
2146 if (!compiler_nameop(c, store_name, Store)) {
2147 Py_DECREF(names);
2148 return 0;
2149 }
2150 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002151 /* remove imported module */
2152 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 return 1;
2154}
2155
2156static int
2157compiler_assert(struct compiler *c, stmt_ty s)
2158{
2159 static PyObject *assertion_error = NULL;
2160 basicblock *end;
2161
2162 if (Py_OptimizeFlag)
2163 return 1;
2164 if (assertion_error == NULL) {
2165 assertion_error = PyString_FromString("AssertionError");
2166 if (assertion_error == NULL)
2167 return 0;
2168 }
2169 VISIT(c, expr, s->v.Assert.test);
2170 end = compiler_new_block(c);
2171 if (end == NULL)
2172 return 0;
2173 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2174 ADDOP(c, POP_TOP);
2175 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2176 if (s->v.Assert.msg) {
2177 VISIT(c, expr, s->v.Assert.msg);
2178 ADDOP_I(c, RAISE_VARARGS, 2);
2179 }
2180 else {
2181 ADDOP_I(c, RAISE_VARARGS, 1);
2182 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002183 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 ADDOP(c, POP_TOP);
2185 return 1;
2186}
2187
2188static int
2189compiler_visit_stmt(struct compiler *c, stmt_ty s)
2190{
2191 int i, n;
2192
Thomas Wouters89f507f2006-12-13 04:49:30 +00002193 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002195 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002196
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002198 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002200 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002202 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 if (c->u->u_ste->ste_type != FunctionBlock)
2204 return compiler_error(c, "'return' outside function");
2205 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 VISIT(c, expr, s->v.Return.value);
2207 }
2208 else
2209 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2210 ADDOP(c, RETURN_VALUE);
2211 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002212 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 VISIT_SEQ(c, expr, s->v.Delete.targets)
2214 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002215 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 n = asdl_seq_LEN(s->v.Assign.targets);
2217 VISIT(c, expr, s->v.Assign.value);
2218 for (i = 0; i < n; i++) {
2219 if (i < n - 1)
2220 ADDOP(c, DUP_TOP);
2221 VISIT(c, expr,
2222 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2223 }
2224 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002225 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002227 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002229 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002231 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002233 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002234 n = 0;
2235 if (s->v.Raise.type) {
2236 VISIT(c, expr, s->v.Raise.type);
2237 n++;
2238 if (s->v.Raise.inst) {
2239 VISIT(c, expr, s->v.Raise.inst);
2240 n++;
2241 if (s->v.Raise.tback) {
2242 VISIT(c, expr, s->v.Raise.tback);
2243 n++;
2244 }
2245 }
2246 }
2247 ADDOP_I(c, RAISE_VARARGS, n);
2248 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002249 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002251 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002253 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002255 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002257 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002259 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002260 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002262 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002264 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 ADDOP(c, PRINT_EXPR);
2266 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002267 else if (s->v.Expr.value->kind != Str_kind &&
2268 s->v.Expr.value->kind != Num_kind) {
2269 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 ADDOP(c, POP_TOP);
2271 }
2272 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002273 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002275 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002276 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 return compiler_error(c, "'break' outside loop");
2278 ADDOP(c, BREAK_LOOP);
2279 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002280 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002282 case With_kind:
2283 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 }
2285 return 1;
2286}
2287
2288static int
2289unaryop(unaryop_ty op)
2290{
2291 switch (op) {
2292 case Invert:
2293 return UNARY_INVERT;
2294 case Not:
2295 return UNARY_NOT;
2296 case UAdd:
2297 return UNARY_POSITIVE;
2298 case USub:
2299 return UNARY_NEGATIVE;
2300 }
2301 return 0;
2302}
2303
2304static int
2305binop(struct compiler *c, operator_ty op)
2306{
2307 switch (op) {
2308 case Add:
2309 return BINARY_ADD;
2310 case Sub:
2311 return BINARY_SUBTRACT;
2312 case Mult:
2313 return BINARY_MULTIPLY;
2314 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002315 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 case Mod:
2317 return BINARY_MODULO;
2318 case Pow:
2319 return BINARY_POWER;
2320 case LShift:
2321 return BINARY_LSHIFT;
2322 case RShift:
2323 return BINARY_RSHIFT;
2324 case BitOr:
2325 return BINARY_OR;
2326 case BitXor:
2327 return BINARY_XOR;
2328 case BitAnd:
2329 return BINARY_AND;
2330 case FloorDiv:
2331 return BINARY_FLOOR_DIVIDE;
2332 }
2333 return 0;
2334}
2335
2336static int
2337cmpop(cmpop_ty op)
2338{
2339 switch (op) {
2340 case Eq:
2341 return PyCmp_EQ;
2342 case NotEq:
2343 return PyCmp_NE;
2344 case Lt:
2345 return PyCmp_LT;
2346 case LtE:
2347 return PyCmp_LE;
2348 case Gt:
2349 return PyCmp_GT;
2350 case GtE:
2351 return PyCmp_GE;
2352 case Is:
2353 return PyCmp_IS;
2354 case IsNot:
2355 return PyCmp_IS_NOT;
2356 case In:
2357 return PyCmp_IN;
2358 case NotIn:
2359 return PyCmp_NOT_IN;
2360 }
2361 return PyCmp_BAD;
2362}
2363
2364static int
2365inplace_binop(struct compiler *c, operator_ty op)
2366{
2367 switch (op) {
2368 case Add:
2369 return INPLACE_ADD;
2370 case Sub:
2371 return INPLACE_SUBTRACT;
2372 case Mult:
2373 return INPLACE_MULTIPLY;
2374 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002375 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 case Mod:
2377 return INPLACE_MODULO;
2378 case Pow:
2379 return INPLACE_POWER;
2380 case LShift:
2381 return INPLACE_LSHIFT;
2382 case RShift:
2383 return INPLACE_RSHIFT;
2384 case BitOr:
2385 return INPLACE_OR;
2386 case BitXor:
2387 return INPLACE_XOR;
2388 case BitAnd:
2389 return INPLACE_AND;
2390 case FloorDiv:
2391 return INPLACE_FLOOR_DIVIDE;
2392 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002393 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002394 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 return 0;
2396}
2397
2398static int
2399compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2400{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002401 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2403
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002404 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002405 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 /* XXX AugStore isn't used anywhere! */
2407
2408 /* First check for assignment to __debug__. Param? */
2409 if ((ctx == Store || ctx == AugStore || ctx == Del)
2410 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2411 return compiler_error(c, "can not assign to __debug__");
2412 }
2413
Guido van Rossumd8faa362007-04-27 19:54:29 +00002414mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002415 if (!mangled)
2416 return 0;
2417
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 op = 0;
2419 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002420 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 switch (scope) {
2422 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002423 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 optype = OP_DEREF;
2425 break;
2426 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002427 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 optype = OP_DEREF;
2429 break;
2430 case LOCAL:
2431 if (c->u->u_ste->ste_type == FunctionBlock)
2432 optype = OP_FAST;
2433 break;
2434 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002435 if (c->u->u_ste->ste_type == FunctionBlock &&
2436 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002437 optype = OP_GLOBAL;
2438 break;
2439 case GLOBAL_EXPLICIT:
2440 optype = OP_GLOBAL;
2441 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002442 default:
2443 /* scope can be 0 */
2444 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 }
2446
2447 /* XXX Leave assert here, but handle __doc__ and the like better */
2448 assert(scope || PyString_AS_STRING(name)[0] == '_');
2449
2450 switch (optype) {
2451 case OP_DEREF:
2452 switch (ctx) {
2453 case Load: op = LOAD_DEREF; break;
2454 case Store: op = STORE_DEREF; break;
2455 case AugLoad:
2456 case AugStore:
2457 break;
2458 case Del:
2459 PyErr_Format(PyExc_SyntaxError,
2460 "can not delete variable '%s' referenced "
2461 "in nested scope",
2462 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002463 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002466 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002467 PyErr_SetString(PyExc_SystemError,
2468 "param invalid for deref variable");
2469 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 }
2471 break;
2472 case OP_FAST:
2473 switch (ctx) {
2474 case Load: op = LOAD_FAST; break;
2475 case Store: op = STORE_FAST; break;
2476 case Del: op = DELETE_FAST; break;
2477 case AugLoad:
2478 case AugStore:
2479 break;
2480 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002481 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002482 PyErr_SetString(PyExc_SystemError,
2483 "param invalid for local variable");
2484 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002486 ADDOP_O(c, op, mangled, varnames);
2487 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 return 1;
2489 case OP_GLOBAL:
2490 switch (ctx) {
2491 case Load: op = LOAD_GLOBAL; break;
2492 case Store: op = STORE_GLOBAL; break;
2493 case Del: op = DELETE_GLOBAL; break;
2494 case AugLoad:
2495 case AugStore:
2496 break;
2497 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002498 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002499 PyErr_SetString(PyExc_SystemError,
2500 "param invalid for global variable");
2501 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002502 }
2503 break;
2504 case OP_NAME:
2505 switch (ctx) {
2506 case Load: op = LOAD_NAME; break;
2507 case Store: op = STORE_NAME; break;
2508 case Del: op = DELETE_NAME; break;
2509 case AugLoad:
2510 case AugStore:
2511 break;
2512 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002513 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002514 PyErr_SetString(PyExc_SystemError,
2515 "param invalid for name variable");
2516 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 }
2518 break;
2519 }
2520
2521 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002522 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002523 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002524 if (arg < 0)
2525 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002526 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527}
2528
2529static int
2530compiler_boolop(struct compiler *c, expr_ty e)
2531{
2532 basicblock *end;
2533 int jumpi, i, n;
2534 asdl_seq *s;
2535
2536 assert(e->kind == BoolOp_kind);
2537 if (e->v.BoolOp.op == And)
2538 jumpi = JUMP_IF_FALSE;
2539 else
2540 jumpi = JUMP_IF_TRUE;
2541 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002542 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 return 0;
2544 s = e->v.BoolOp.values;
2545 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002546 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002548 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 ADDOP_JREL(c, jumpi, end);
2550 ADDOP(c, POP_TOP)
2551 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002552 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 compiler_use_next_block(c, end);
2554 return 1;
2555}
2556
2557static int
2558compiler_list(struct compiler *c, expr_ty e)
2559{
2560 int n = asdl_seq_LEN(e->v.List.elts);
2561 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002562 int i, seen_star = 0;
2563 for (i = 0; i < n; i++) {
2564 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2565 if (elt->kind == Starred_kind && !seen_star) {
2566 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2567 seen_star = 1;
2568 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2569 } else if (elt->kind == Starred_kind) {
2570 return compiler_error(c,
2571 "two starred expressions in assignment");
2572 }
2573 }
2574 if (!seen_star) {
2575 ADDOP_I(c, UNPACK_SEQUENCE, n);
2576 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577 }
2578 VISIT_SEQ(c, expr, e->v.List.elts);
2579 if (e->v.List.ctx == Load) {
2580 ADDOP_I(c, BUILD_LIST, n);
2581 }
2582 return 1;
2583}
2584
2585static int
2586compiler_tuple(struct compiler *c, expr_ty e)
2587{
2588 int n = asdl_seq_LEN(e->v.Tuple.elts);
2589 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002590 int i, seen_star = 0;
2591 for (i = 0; i < n; i++) {
2592 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2593 if (elt->kind == Starred_kind && !seen_star) {
2594 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2595 seen_star = 1;
2596 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2597 } else if (elt->kind == Starred_kind) {
2598 return compiler_error(c,
2599 "two starred expressions in assignment");
2600 }
2601 }
2602 if (!seen_star) {
2603 ADDOP_I(c, UNPACK_SEQUENCE, n);
2604 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 }
2606 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2607 if (e->v.Tuple.ctx == Load) {
2608 ADDOP_I(c, BUILD_TUPLE, n);
2609 }
2610 return 1;
2611}
2612
2613static int
2614compiler_compare(struct compiler *c, expr_ty e)
2615{
2616 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002617 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
2619 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2620 VISIT(c, expr, e->v.Compare.left);
2621 n = asdl_seq_LEN(e->v.Compare.ops);
2622 assert(n > 0);
2623 if (n > 1) {
2624 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002625 if (cleanup == NULL)
2626 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002627 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002628 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 }
2630 for (i = 1; i < n; i++) {
2631 ADDOP(c, DUP_TOP);
2632 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002634 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002635 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2637 NEXT_BLOCK(c);
2638 ADDOP(c, POP_TOP);
2639 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002640 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002641 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002643 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002645 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 if (n > 1) {
2647 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002648 if (end == NULL)
2649 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 ADDOP_JREL(c, JUMP_FORWARD, end);
2651 compiler_use_next_block(c, cleanup);
2652 ADDOP(c, ROT_TWO);
2653 ADDOP(c, POP_TOP);
2654 compiler_use_next_block(c, end);
2655 }
2656 return 1;
2657}
2658
2659static int
2660compiler_call(struct compiler *c, expr_ty e)
2661{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002663 return compiler_call_helper(c, 0,
2664 e->v.Call.args,
2665 e->v.Call.keywords,
2666 e->v.Call.starargs,
2667 e->v.Call.kwargs);
2668}
2669
2670/* shared code between compiler_call and compiler_class */
2671static int
2672compiler_call_helper(struct compiler *c,
2673 int n, /* Args already pushed */
2674 asdl_seq *args,
2675 asdl_seq *keywords,
2676 expr_ty starargs,
2677 expr_ty kwargs)
2678{
2679 int code = 0;
2680
2681 n += asdl_seq_LEN(args);
2682 VISIT_SEQ(c, expr, args);
2683 if (keywords) {
2684 VISIT_SEQ(c, keyword, keywords);
2685 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002687 if (starargs) {
2688 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689 code |= 1;
2690 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002691 if (kwargs) {
2692 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002693 code |= 2;
2694 }
2695 switch (code) {
2696 case 0:
2697 ADDOP_I(c, CALL_FUNCTION, n);
2698 break;
2699 case 1:
2700 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2701 break;
2702 case 2:
2703 ADDOP_I(c, CALL_FUNCTION_KW, n);
2704 break;
2705 case 3:
2706 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2707 break;
2708 }
2709 return 1;
2710}
2711
Nick Coghlan650f0d02007-04-15 12:05:43 +00002712
2713/* List and set comprehensions and generator expressions work by creating a
2714 nested function to perform the actual iteration. This means that the
2715 iteration variables don't leak into the current scope.
2716 The defined function is called immediately following its definition, with the
2717 result of that call being the result of the expression.
2718 The LC/SC version returns the populated container, while the GE version is
2719 flagged in symtable.c as a generator, so it returns the generator object
2720 when the function is called.
2721 This code *knows* that the loop cannot contain break, continue, or return,
2722 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2723
2724 Possible cleanups:
2725 - iterate over the generator sequence instead of using recursion
2726*/
2727
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002729compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2730 asdl_seq *generators, int gen_index,
2731 expr_ty elt, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002732{
2733 /* generate code for the iterator, then each of the ifs,
2734 and then write to the element */
2735
Nick Coghlan650f0d02007-04-15 12:05:43 +00002736 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002738 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739
2740 start = compiler_new_block(c);
2741 skip = compiler_new_block(c);
2742 if_cleanup = compiler_new_block(c);
2743 anchor = compiler_new_block(c);
2744
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002745 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002746 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Nick Coghlan650f0d02007-04-15 12:05:43 +00002749 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002751 if (gen_index == 0) {
2752 /* Receive outermost iter as an implicit argument */
2753 c->u->u_argcount = 1;
2754 ADDOP_I(c, LOAD_FAST, 0);
2755 }
2756 else {
2757 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002758 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759 ADDOP(c, GET_ITER);
2760 }
2761 compiler_use_next_block(c, start);
2762 ADDOP_JREL(c, FOR_ITER, anchor);
2763 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002764 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002766 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002767 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002769 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770 VISIT(c, expr, e);
2771 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2772 NEXT_BLOCK(c);
2773 ADDOP(c, POP_TOP);
2774 }
2775
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002776 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002777 if (!compiler_comprehension_generator(c, tmpname,
2778 generators, gen_index,
2779 elt, type))
2780 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781
Nick Coghlan650f0d02007-04-15 12:05:43 +00002782 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002783 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002784 /* comprehension specific code */
2785 switch (type) {
2786 case COMP_GENEXP:
2787 VISIT(c, expr, elt);
2788 ADDOP(c, YIELD_VALUE);
2789 ADDOP(c, POP_TOP);
2790 break;
2791 case COMP_LISTCOMP:
2792 if (!compiler_nameop(c, tmpname, Load))
2793 return 0;
2794 VISIT(c, expr, elt);
2795 ADDOP(c, LIST_APPEND);
2796 break;
2797 case COMP_SETCOMP:
2798 if (!compiler_nameop(c, tmpname, Load))
2799 return 0;
2800 VISIT(c, expr, elt);
2801 ADDOP(c, SET_ADD);
2802 break;
2803 default:
2804 return 0;
2805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
2807 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 for (i = 0; i < n; i++) {
2810 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002811 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814 ADDOP(c, POP_TOP);
2815 }
2816 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2817 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818
2819 return 1;
2820}
2821
2822static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002823compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2824 asdl_seq *generators, expr_ty elt)
2825{
2826 PyCodeObject *co = NULL;
2827 identifier tmp = NULL;
2828 expr_ty outermost_iter;
2829
2830 outermost_iter = ((comprehension_ty)
2831 asdl_seq_GET(generators, 0))->iter;
2832
2833 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2834 goto error;
2835
2836 if (type != COMP_GENEXP) {
2837 tmp = compiler_new_tmpname(c);
2838 if (!tmp)
2839 goto error_in_scope;
2840
2841 ADDOP_I(c, (type == COMP_LISTCOMP ?
2842 BUILD_LIST : BUILD_SET), 0);
2843 ADDOP(c, DUP_TOP);
2844 if (!compiler_nameop(c, tmp, Store))
2845 goto error_in_scope;
2846 }
2847
2848 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt, type))
2849 goto error_in_scope;
2850
2851 if (type != COMP_GENEXP) {
2852 ADDOP(c, RETURN_VALUE);
2853 }
2854
2855 co = assemble(c, 1);
2856 compiler_exit_scope(c);
2857 if (co == NULL)
2858 goto error;
2859
2860 if (!compiler_make_closure(c, co, 0))
2861 goto error;
2862 Py_DECREF(co);
2863 Py_XDECREF(tmp);
2864
2865 VISIT(c, expr, outermost_iter);
2866 ADDOP(c, GET_ITER);
2867 ADDOP_I(c, CALL_FUNCTION, 1);
2868 return 1;
2869error_in_scope:
2870 compiler_exit_scope(c);
2871error:
2872 Py_XDECREF(co);
2873 Py_XDECREF(tmp);
2874 return 0;
2875}
2876
2877static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878compiler_genexp(struct compiler *c, expr_ty e)
2879{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002880 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002881 if (!name) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002882 name = PyString_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002883 if (!name)
2884 return 0;
2885 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002886 assert(e->kind == GeneratorExp_kind);
2887 return compiler_comprehension(c, e, COMP_GENEXP, name,
2888 e->v.GeneratorExp.generators,
2889 e->v.GeneratorExp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890}
2891
2892static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002893compiler_listcomp(struct compiler *c, expr_ty e)
2894{
2895 static identifier name;
2896 if (!name) {
2897 name = PyString_FromString("<listcomp>");
2898 if (!name)
2899 return 0;
2900 }
2901 assert(e->kind == ListComp_kind);
2902 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2903 e->v.ListComp.generators,
2904 e->v.ListComp.elt);
2905}
2906
2907static int
2908compiler_setcomp(struct compiler *c, expr_ty e)
2909{
2910 static identifier name;
2911 if (!name) {
2912 name = PyString_FromString("<setcomp>");
2913 if (!name)
2914 return 0;
2915 }
2916 assert(e->kind == SetComp_kind);
2917 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2918 e->v.SetComp.generators,
2919 e->v.SetComp.elt);
2920}
2921
2922
2923static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924compiler_visit_keyword(struct compiler *c, keyword_ty k)
2925{
2926 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2927 VISIT(c, expr, k->value);
2928 return 1;
2929}
2930
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002931/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932 whether they are true or false.
2933
2934 Return values: 1 for true, 0 for false, -1 for non-constant.
2935 */
2936
2937static int
2938expr_constant(expr_ty e)
2939{
2940 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002941 case Ellipsis_kind:
2942 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002943 case Num_kind:
2944 return PyObject_IsTrue(e->v.Num.n);
2945 case Str_kind:
2946 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002947 case Name_kind:
2948 /* __debug__ is not assignable, so we can optimize
2949 * it away in if and while statements */
2950 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Guido van Rossumd8faa362007-04-27 19:54:29 +00002951 "__debug__") == 0)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002952 return ! Py_OptimizeFlag;
2953 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954 default:
2955 return -1;
2956 }
2957}
2958
Guido van Rossumc2e20742006-02-27 22:32:47 +00002959/*
2960 Implements the with statement from PEP 343.
2961
2962 The semantics outlined in that PEP are as follows:
2963
2964 with EXPR as VAR:
2965 BLOCK
2966
2967 It is implemented roughly as:
2968
Thomas Wouters477c8d52006-05-27 19:21:47 +00002969 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002970 exit = context.__exit__ # not calling it
2971 value = context.__enter__()
2972 try:
2973 VAR = value # if VAR present in the syntax
2974 BLOCK
2975 finally:
2976 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002977 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002978 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002979 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002980 exit(*exc)
2981 */
2982static int
2983compiler_with(struct compiler *c, stmt_ty s)
2984{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002985 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002986 basicblock *block, *finally;
2987 identifier tmpexit, tmpvalue = NULL;
2988
2989 assert(s->kind == With_kind);
2990
Guido van Rossumc2e20742006-02-27 22:32:47 +00002991 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002992 enter_attr = PyString_InternFromString("__enter__");
2993 if (!enter_attr)
2994 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002995 }
2996 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002997 exit_attr = PyString_InternFromString("__exit__");
2998 if (!exit_attr)
2999 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003000 }
3001
3002 block = compiler_new_block(c);
3003 finally = compiler_new_block(c);
3004 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003005 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003006
3007 /* Create a temporary variable to hold context.__exit__ */
3008 tmpexit = compiler_new_tmpname(c);
3009 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003010 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003011 PyArena_AddPyObject(c->c_arena, tmpexit);
3012
3013 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003014 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003015 We need to do this rather than preserving it on the stack
3016 because SETUP_FINALLY remembers the stack level.
3017 We need to do the assignment *inside* the try/finally
3018 so that context.__exit__() is called when the assignment
3019 fails. But we need to call context.__enter__() *before*
3020 the try/finally so that if it fails we won't call
3021 context.__exit__().
3022 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003023 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003024 if (tmpvalue == NULL)
3025 return 0;
3026 PyArena_AddPyObject(c->c_arena, tmpvalue);
3027 }
3028
Thomas Wouters477c8d52006-05-27 19:21:47 +00003029 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003030 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003031
3032 /* Squirrel away context.__exit__ */
3033 ADDOP(c, DUP_TOP);
3034 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3035 if (!compiler_nameop(c, tmpexit, Store))
3036 return 0;
3037
3038 /* Call context.__enter__() */
3039 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3040 ADDOP_I(c, CALL_FUNCTION, 0);
3041
3042 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003043 /* Store it in tmpvalue */
3044 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003045 return 0;
3046 }
3047 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003048 /* Discard result from context.__enter__() */
3049 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003050 }
3051
3052 /* Start the try block */
3053 ADDOP_JREL(c, SETUP_FINALLY, finally);
3054
3055 compiler_use_next_block(c, block);
3056 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003057 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003058 }
3059
3060 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003061 /* Bind saved result of context.__enter__() to VAR */
3062 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003063 !compiler_nameop(c, tmpvalue, Del))
3064 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003065 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003066 }
3067
3068 /* BLOCK code */
3069 VISIT_SEQ(c, stmt, s->v.With.body);
3070
3071 /* End of try block; start the finally block */
3072 ADDOP(c, POP_BLOCK);
3073 compiler_pop_fblock(c, FINALLY_TRY, block);
3074
3075 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3076 compiler_use_next_block(c, finally);
3077 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003078 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003079
3080 /* Finally block starts; push tmpexit and issue our magic opcode. */
3081 if (!compiler_nameop(c, tmpexit, Load) ||
3082 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003083 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003084 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003085
3086 /* Finally block ends. */
3087 ADDOP(c, END_FINALLY);
3088 compiler_pop_fblock(c, FINALLY_END, finally);
3089 return 1;
3090}
3091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092static int
3093compiler_visit_expr(struct compiler *c, expr_ty e)
3094{
3095 int i, n;
3096
Thomas Wouters89f507f2006-12-13 04:49:30 +00003097 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003098 set a new line number for the next instruction.
3099 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003100 if (e->lineno > c->u->u_lineno) {
3101 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003102 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003103 }
3104 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003105 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003107 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 VISIT(c, expr, e->v.BinOp.left);
3109 VISIT(c, expr, e->v.BinOp.right);
3110 ADDOP(c, binop(c, e->v.BinOp.op));
3111 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003112 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 VISIT(c, expr, e->v.UnaryOp.operand);
3114 ADDOP(c, unaryop(e->v.UnaryOp.op));
3115 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003116 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003118 case IfExp_kind:
3119 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 /* XXX get rid of arg? */
3122 ADDOP_I(c, BUILD_MAP, 0);
3123 n = asdl_seq_LEN(e->v.Dict.values);
3124 /* We must arrange things just right for STORE_SUBSCR.
3125 It wants the stack to look like (value) (dict) (key) */
3126 for (i = 0; i < n; i++) {
3127 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003128 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003129 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003131 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003132 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 ADDOP(c, STORE_SUBSCR);
3134 }
3135 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003136 case Set_kind:
3137 n = asdl_seq_LEN(e->v.Set.elts);
3138 VISIT_SEQ(c, expr, e->v.Set.elts);
3139 ADDOP_I(c, BUILD_SET, n);
3140 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003141 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003143 case ListComp_kind:
3144 return compiler_listcomp(c, e);
3145 case SetComp_kind:
3146 return compiler_setcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 case Yield_kind:
3148 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003149 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 if (e->v.Yield.value) {
3151 VISIT(c, expr, e->v.Yield.value);
3152 }
3153 else {
3154 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3155 }
3156 ADDOP(c, YIELD_VALUE);
3157 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003158 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003160 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003162 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3164 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003165 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3167 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003168 case Bytes_kind:
3169 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3170 ADDOP(c, MAKE_BYTES);
3171 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003172 case Ellipsis_kind:
3173 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3174 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003176 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 if (e->v.Attribute.ctx != AugStore)
3178 VISIT(c, expr, e->v.Attribute.value);
3179 switch (e->v.Attribute.ctx) {
3180 case AugLoad:
3181 ADDOP(c, DUP_TOP);
3182 /* Fall through to load */
3183 case Load:
3184 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3185 break;
3186 case AugStore:
3187 ADDOP(c, ROT_TWO);
3188 /* Fall through to save */
3189 case Store:
3190 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3191 break;
3192 case Del:
3193 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3194 break;
3195 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003196 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003197 PyErr_SetString(PyExc_SystemError,
3198 "param invalid in attribute expression");
3199 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 }
3201 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003202 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 switch (e->v.Subscript.ctx) {
3204 case AugLoad:
3205 VISIT(c, expr, e->v.Subscript.value);
3206 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3207 break;
3208 case Load:
3209 VISIT(c, expr, e->v.Subscript.value);
3210 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3211 break;
3212 case AugStore:
3213 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3214 break;
3215 case Store:
3216 VISIT(c, expr, e->v.Subscript.value);
3217 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3218 break;
3219 case Del:
3220 VISIT(c, expr, e->v.Subscript.value);
3221 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3222 break;
3223 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003224 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003225 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003226 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003227 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 }
3229 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003230 case Starred_kind:
3231 switch (e->v.Starred.ctx) {
3232 case Store:
3233 /* In all legitimate cases, the Starred node was already replaced
3234 * by compiler_list/compiler_tuple. XXX: is that okay? */
3235 return compiler_error(c,
3236 "starred assignment target must be in a list or tuple");
3237 default:
3238 return compiler_error(c,
3239 "can use starred expression only as assignment target");
3240 }
3241 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003242 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003243 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3244 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003245 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003247 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 return compiler_tuple(c, e);
3249 }
3250 return 1;
3251}
3252
3253static int
3254compiler_augassign(struct compiler *c, stmt_ty s)
3255{
3256 expr_ty e = s->v.AugAssign.target;
3257 expr_ty auge;
3258
3259 assert(s->kind == AugAssign_kind);
3260
3261 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003262 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003264 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003265 if (auge == NULL)
3266 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 VISIT(c, expr, auge);
3268 VISIT(c, expr, s->v.AugAssign.value);
3269 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3270 auge->v.Attribute.ctx = AugStore;
3271 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 break;
3273 case Subscript_kind:
3274 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003275 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003276 if (auge == NULL)
3277 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003278 VISIT(c, expr, auge);
3279 VISIT(c, expr, s->v.AugAssign.value);
3280 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003281 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003283 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003284 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003285 if (!compiler_nameop(c, e->v.Name.id, Load))
3286 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287 VISIT(c, expr, s->v.AugAssign.value);
3288 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3289 return compiler_nameop(c, e->v.Name.id, Store);
3290 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003291 PyErr_Format(PyExc_SystemError,
3292 "invalid node type (%d) for augmented assignment",
3293 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003294 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 }
3296 return 1;
3297}
3298
3299static int
3300compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3301{
3302 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003303 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3304 PyErr_SetString(PyExc_SystemError,
3305 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308 f = &c->u->u_fblock[c->u->u_nfblocks++];
3309 f->fb_type = t;
3310 f->fb_block = b;
3311 return 1;
3312}
3313
3314static void
3315compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3316{
3317 struct compiler_unit *u = c->u;
3318 assert(u->u_nfblocks > 0);
3319 u->u_nfblocks--;
3320 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3321 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3322}
3323
Thomas Wouters89f507f2006-12-13 04:49:30 +00003324static int
3325compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003326 int i;
3327 struct compiler_unit *u = c->u;
3328 for (i = 0; i < u->u_nfblocks; ++i) {
3329 if (u->u_fblock[i].fb_type == LOOP)
3330 return 1;
3331 }
3332 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003333}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334/* Raises a SyntaxError and returns 0.
3335 If something goes wrong, a different exception may be raised.
3336*/
3337
3338static int
3339compiler_error(struct compiler *c, const char *errstr)
3340{
3341 PyObject *loc;
3342 PyObject *u = NULL, *v = NULL;
3343
3344 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3345 if (!loc) {
3346 Py_INCREF(Py_None);
3347 loc = Py_None;
3348 }
3349 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3350 Py_None, loc);
3351 if (!u)
3352 goto exit;
3353 v = Py_BuildValue("(zO)", errstr, u);
3354 if (!v)
3355 goto exit;
3356 PyErr_SetObject(PyExc_SyntaxError, v);
3357 exit:
3358 Py_DECREF(loc);
3359 Py_XDECREF(u);
3360 Py_XDECREF(v);
3361 return 0;
3362}
3363
3364static int
3365compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003366 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003368 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003369
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003370 /* XXX this code is duplicated */
3371 switch (ctx) {
3372 case AugLoad: /* fall through to Load */
3373 case Load: op = BINARY_SUBSCR; break;
3374 case AugStore:/* fall through to Store */
3375 case Store: op = STORE_SUBSCR; break;
3376 case Del: op = DELETE_SUBSCR; break;
3377 case Param:
3378 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003379 "invalid %s kind %d in subscript\n",
3380 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003381 return 0;
3382 }
3383 if (ctx == AugLoad) {
3384 ADDOP_I(c, DUP_TOPX, 2);
3385 }
3386 else if (ctx == AugStore) {
3387 ADDOP(c, ROT_THREE);
3388 }
3389 ADDOP(c, op);
3390 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391}
3392
3393static int
3394compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3395{
3396 int n = 2;
3397 assert(s->kind == Slice_kind);
3398
3399 /* only handles the cases where BUILD_SLICE is emitted */
3400 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003401 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402 }
3403 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003404 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003406
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003408 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409 }
3410 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003411 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 }
3413
3414 if (s->v.Slice.step) {
3415 n++;
3416 VISIT(c, expr, s->v.Slice.step);
3417 }
3418 ADDOP_I(c, BUILD_SLICE, n);
3419 return 1;
3420}
3421
3422static int
3423compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3424{
3425 int op = 0, slice_offset = 0, stack_count = 0;
3426
3427 assert(s->v.Slice.step == NULL);
3428 if (s->v.Slice.lower) {
3429 slice_offset++;
3430 stack_count++;
3431 if (ctx != AugStore)
3432 VISIT(c, expr, s->v.Slice.lower);
3433 }
3434 if (s->v.Slice.upper) {
3435 slice_offset += 2;
3436 stack_count++;
3437 if (ctx != AugStore)
3438 VISIT(c, expr, s->v.Slice.upper);
3439 }
3440
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003441 if (ctx == AugLoad) {
3442 switch (stack_count) {
3443 case 0: ADDOP(c, DUP_TOP); break;
3444 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3445 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3446 }
3447 }
3448 else if (ctx == AugStore) {
3449 switch (stack_count) {
3450 case 0: ADDOP(c, ROT_TWO); break;
3451 case 1: ADDOP(c, ROT_THREE); break;
3452 case 2: ADDOP(c, ROT_FOUR); break;
3453 }
3454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455
3456 switch (ctx) {
3457 case AugLoad: /* fall through to Load */
3458 case Load: op = SLICE; break;
3459 case AugStore:/* fall through to Store */
3460 case Store: op = STORE_SLICE; break;
3461 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003462 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003463 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003464 PyErr_SetString(PyExc_SystemError,
3465 "param invalid in simple slice");
3466 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 }
3468
3469 ADDOP(c, op + slice_offset);
3470 return 1;
3471}
3472
3473static int
3474compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3475 expr_context_ty ctx)
3476{
3477 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 case Slice_kind:
3479 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 case Index_kind:
3481 VISIT(c, expr, s->v.Index.value);
3482 break;
3483 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003484 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003485 PyErr_SetString(PyExc_SystemError,
3486 "extended slice invalid in nested slice");
3487 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 }
3489 return 1;
3490}
3491
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492static int
3493compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3494{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003495 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003497 case Index_kind:
3498 kindname = "index";
3499 if (ctx != AugStore) {
3500 VISIT(c, expr, s->v.Index.value);
3501 }
3502 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003503 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003504 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505 if (!s->v.Slice.step)
3506 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003507 if (ctx != AugStore) {
3508 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 return 0;
3510 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003511 break;
3512 case ExtSlice_kind:
3513 kindname = "extended slice";
3514 if (ctx != AugStore) {
3515 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3516 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003517 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003518 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003519 if (!compiler_visit_nested_slice(c, sub, ctx))
3520 return 0;
3521 }
3522 ADDOP_I(c, BUILD_TUPLE, n);
3523 }
3524 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003525 default:
3526 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003527 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003528 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003530 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531}
3532
Thomas Wouters89f507f2006-12-13 04:49:30 +00003533/* End of the compiler section, beginning of the assembler section */
3534
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003535/* do depth-first search of basic block graph, starting with block.
3536 post records the block indices in post-order.
3537
3538 XXX must handle implicit jumps from one block to next
3539*/
3540
Thomas Wouters89f507f2006-12-13 04:49:30 +00003541struct assembler {
3542 PyObject *a_bytecode; /* string containing bytecode */
3543 int a_offset; /* offset into bytecode */
3544 int a_nblocks; /* number of reachable blocks */
3545 basicblock **a_postorder; /* list of blocks in dfs postorder */
3546 PyObject *a_lnotab; /* string containing lnotab */
3547 int a_lnotab_off; /* offset into lnotab */
3548 int a_lineno; /* last lineno of emitted instruction */
3549 int a_lineno_off; /* bytecode offset of last lineno */
3550};
3551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552static void
3553dfs(struct compiler *c, basicblock *b, struct assembler *a)
3554{
3555 int i;
3556 struct instr *instr = NULL;
3557
3558 if (b->b_seen)
3559 return;
3560 b->b_seen = 1;
3561 if (b->b_next != NULL)
3562 dfs(c, b->b_next, a);
3563 for (i = 0; i < b->b_iused; i++) {
3564 instr = &b->b_instr[i];
3565 if (instr->i_jrel || instr->i_jabs)
3566 dfs(c, instr->i_target, a);
3567 }
3568 a->a_postorder[a->a_nblocks++] = b;
3569}
3570
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003571static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3573{
3574 int i;
3575 struct instr *instr;
3576 if (b->b_seen || b->b_startdepth >= depth)
3577 return maxdepth;
3578 b->b_seen = 1;
3579 b->b_startdepth = depth;
3580 for (i = 0; i < b->b_iused; i++) {
3581 instr = &b->b_instr[i];
3582 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3583 if (depth > maxdepth)
3584 maxdepth = depth;
3585 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3586 if (instr->i_jrel || instr->i_jabs) {
3587 maxdepth = stackdepth_walk(c, instr->i_target,
3588 depth, maxdepth);
3589 if (instr->i_opcode == JUMP_ABSOLUTE ||
3590 instr->i_opcode == JUMP_FORWARD) {
3591 goto out; /* remaining code is dead */
3592 }
3593 }
3594 }
3595 if (b->b_next)
3596 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3597out:
3598 b->b_seen = 0;
3599 return maxdepth;
3600}
3601
3602/* Find the flow path that needs the largest stack. We assume that
3603 * cycles in the flow graph have no net effect on the stack depth.
3604 */
3605static int
3606stackdepth(struct compiler *c)
3607{
3608 basicblock *b, *entryblock;
3609 entryblock = NULL;
3610 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3611 b->b_seen = 0;
3612 b->b_startdepth = INT_MIN;
3613 entryblock = b;
3614 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003615 if (!entryblock)
3616 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 return stackdepth_walk(c, entryblock, 0, 0);
3618}
3619
3620static int
3621assemble_init(struct assembler *a, int nblocks, int firstlineno)
3622{
3623 memset(a, 0, sizeof(struct assembler));
3624 a->a_lineno = firstlineno;
3625 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3626 if (!a->a_bytecode)
3627 return 0;
3628 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3629 if (!a->a_lnotab)
3630 return 0;
3631 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003632 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003633 if (!a->a_postorder) {
3634 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637 return 1;
3638}
3639
3640static void
3641assemble_free(struct assembler *a)
3642{
3643 Py_XDECREF(a->a_bytecode);
3644 Py_XDECREF(a->a_lnotab);
3645 if (a->a_postorder)
3646 PyObject_Free(a->a_postorder);
3647}
3648
3649/* Return the size of a basic block in bytes. */
3650
3651static int
3652instrsize(struct instr *instr)
3653{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003654 if (!instr->i_hasarg)
3655 return 1;
3656 if (instr->i_oparg > 0xffff)
3657 return 6;
3658 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659}
3660
3661static int
3662blocksize(basicblock *b)
3663{
3664 int i;
3665 int size = 0;
3666
3667 for (i = 0; i < b->b_iused; i++)
3668 size += instrsize(&b->b_instr[i]);
3669 return size;
3670}
3671
3672/* All about a_lnotab.
3673
3674c_lnotab is an array of unsigned bytes disguised as a Python string.
3675It is used to map bytecode offsets to source code line #s (when needed
3676for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003677
Tim Peters2a7f3842001-06-09 09:26:21 +00003678The array is conceptually a list of
3679 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003680pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003681
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003682 byte code offset source code line number
3683 0 1
3684 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003685 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003686 350 307
3687 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003688
3689The first trick is that these numbers aren't stored, only the increments
3690from one row to the next (this doesn't really work, but it's a start):
3691
3692 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3693
3694The second trick is that an unsigned byte can't hold negative values, or
3695values larger than 255, so (a) there's a deep assumption that byte code
3696offsets and their corresponding line #s both increase monotonically, and (b)
3697if at least one column jumps by more than 255 from one row to the next, more
3698than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003699from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003700part. A user of c_lnotab desiring to find the source line number
3701corresponding to a bytecode address A should do something like this
3702
3703 lineno = addr = 0
3704 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003705 addr += addr_incr
3706 if addr > A:
3707 return lineno
3708 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003709
3710In order for this to work, when the addr field increments by more than 255,
3711the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003712increment is < 256. So, in the example above, assemble_lnotab (it used
3713to be called com_set_lineno) should not (as was actually done until 2.2)
3714expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003715 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003716*/
3717
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003718static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003720{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 int d_bytecode, d_lineno;
3722 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003723 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724
3725 d_bytecode = a->a_offset - a->a_lineno_off;
3726 d_lineno = i->i_lineno - a->a_lineno;
3727
3728 assert(d_bytecode >= 0);
3729 assert(d_lineno >= 0);
3730
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003731 /* XXX(nnorwitz): is there a better way to handle this?
3732 for loops are special, we want to be able to trace them
3733 each time around, so we need to set an extra line number. */
3734 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003735 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003736
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003737 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003738 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 nbytes = a->a_lnotab_off + 2 * ncodes;
3740 len = PyString_GET_SIZE(a->a_lnotab);
3741 if (nbytes >= len) {
3742 if (len * 2 < nbytes)
3743 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003744 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 len *= 2;
3746 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3747 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003748 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003749 lnotab = (unsigned char *)
3750 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003751 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 *lnotab++ = 255;
3753 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 d_bytecode -= ncodes * 255;
3756 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003757 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758 assert(d_bytecode <= 255);
3759 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003760 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003761 nbytes = a->a_lnotab_off + 2 * ncodes;
3762 len = PyString_GET_SIZE(a->a_lnotab);
3763 if (nbytes >= len) {
3764 if (len * 2 < nbytes)
3765 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003766 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 len *= 2;
3768 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3769 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003770 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003771 lnotab = (unsigned char *)
3772 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003774 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003776 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003778 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003779 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 d_lineno -= ncodes * 255;
3781 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003782 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003783
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 len = PyString_GET_SIZE(a->a_lnotab);
3785 if (a->a_lnotab_off + 2 >= len) {
3786 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003787 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003788 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003789 lnotab = (unsigned char *)
3790 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 a->a_lnotab_off += 2;
3793 if (d_bytecode) {
3794 *lnotab++ = d_bytecode;
3795 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003796 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003797 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 *lnotab++ = 0;
3799 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003800 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 a->a_lineno = i->i_lineno;
3802 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003803 return 1;
3804}
3805
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806/* assemble_emit()
3807 Extend the bytecode with a new instruction.
3808 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003809*/
3810
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003811static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003813{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003814 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003815 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 char *code;
3817
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003818 size = instrsize(i);
3819 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003821 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003822 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003824 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 if (a->a_offset + size >= len) {
3826 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003827 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3830 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003831 if (size == 6) {
3832 assert(i->i_hasarg);
3833 *code++ = (char)EXTENDED_ARG;
3834 *code++ = ext & 0xff;
3835 *code++ = ext >> 8;
3836 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003837 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003839 if (i->i_hasarg) {
3840 assert(size == 3 || size == 6);
3841 *code++ = arg & 0xff;
3842 *code++ = arg >> 8;
3843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003845}
3846
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003847static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003849{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003851 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003852 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003853
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 /* Compute the size of each block and fixup jump args.
3855 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003856start:
3857 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003859 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 bsize = blocksize(b);
3861 b->b_offset = totsize;
3862 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003863 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003864 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3866 bsize = b->b_offset;
3867 for (i = 0; i < b->b_iused; i++) {
3868 struct instr *instr = &b->b_instr[i];
3869 /* Relative jumps are computed relative to
3870 the instruction pointer after fetching
3871 the jump instruction.
3872 */
3873 bsize += instrsize(instr);
3874 if (instr->i_jabs)
3875 instr->i_oparg = instr->i_target->b_offset;
3876 else if (instr->i_jrel) {
3877 int delta = instr->i_target->b_offset - bsize;
3878 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003879 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003880 else
3881 continue;
3882 if (instr->i_oparg > 0xffff)
3883 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003884 }
3885 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003886
3887 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003888 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003889 with a better solution.
3890
3891 In the meantime, should the goto be dropped in favor
3892 of a loop?
3893
3894 The issue is that in the first loop blocksize() is called
3895 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003896 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003897 i_oparg is calculated in the second loop above.
3898
3899 So we loop until we stop seeing new EXTENDED_ARGs.
3900 The only EXTENDED_ARGs that could be popping up are
3901 ones in jump instructions. So this should converge
3902 fairly quickly.
3903 */
3904 if (last_extended_arg_count != extended_arg_count) {
3905 last_extended_arg_count = extended_arg_count;
3906 goto start;
3907 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003908}
3909
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003910static PyObject *
3911dict_keys_inorder(PyObject *dict, int offset)
3912{
3913 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003914 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003915
3916 tuple = PyTuple_New(size);
3917 if (tuple == NULL)
3918 return NULL;
3919 while (PyDict_Next(dict, &pos, &k, &v)) {
3920 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003921 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003922 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003923 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003924 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003925 PyTuple_SET_ITEM(tuple, i - offset, k);
3926 }
3927 return tuple;
3928}
3929
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003930static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003931compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003932{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003933 PySTEntryObject *ste = c->u->u_ste;
3934 int flags = 0, n;
3935 if (ste->ste_type != ModuleBlock)
3936 flags |= CO_NEWLOCALS;
3937 if (ste->ste_type == FunctionBlock) {
3938 if (!ste->ste_unoptimized)
3939 flags |= CO_OPTIMIZED;
3940 if (ste->ste_nested)
3941 flags |= CO_NESTED;
3942 if (ste->ste_generator)
3943 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003944 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 if (ste->ste_varargs)
3946 flags |= CO_VARARGS;
3947 if (ste->ste_varkeywords)
3948 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003949 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003950 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003951
3952 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003953 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 n = PyDict_Size(c->u->u_freevars);
3956 if (n < 0)
3957 return -1;
3958 if (n == 0) {
3959 n = PyDict_Size(c->u->u_cellvars);
3960 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003961 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 if (n == 0) {
3963 flags |= CO_NOFREE;
3964 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003965 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003966
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003967 return flags;
3968}
3969
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970static PyCodeObject *
3971makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003972{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 PyObject *tmp;
3974 PyCodeObject *co = NULL;
3975 PyObject *consts = NULL;
3976 PyObject *names = NULL;
3977 PyObject *varnames = NULL;
3978 PyObject *filename = NULL;
3979 PyObject *name = NULL;
3980 PyObject *freevars = NULL;
3981 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003982 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003984
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 tmp = dict_keys_inorder(c->u->u_consts, 0);
3986 if (!tmp)
3987 goto error;
3988 consts = PySequence_List(tmp); /* optimize_code requires a list */
3989 Py_DECREF(tmp);
3990
3991 names = dict_keys_inorder(c->u->u_names, 0);
3992 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3993 if (!consts || !names || !varnames)
3994 goto error;
3995
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003996 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3997 if (!cellvars)
3998 goto error;
3999 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4000 if (!freevars)
4001 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004002 filename = PyString_FromString(c->c_filename);
4003 if (!filename)
4004 goto error;
4005
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004006 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004007 flags = compute_code_flags(c);
4008 if (flags < 0)
4009 goto error;
4010
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004011 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 if (!bytecode)
4013 goto error;
4014
4015 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4016 if (!tmp)
4017 goto error;
4018 Py_DECREF(consts);
4019 consts = tmp;
4020
Guido van Rossum4f72a782006-10-27 23:31:49 +00004021 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4022 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004023 bytecode, consts, names, varnames,
4024 freevars, cellvars,
4025 filename, c->u->u_name,
4026 c->u->u_firstlineno,
4027 a->a_lnotab);
4028 error:
4029 Py_XDECREF(consts);
4030 Py_XDECREF(names);
4031 Py_XDECREF(varnames);
4032 Py_XDECREF(filename);
4033 Py_XDECREF(name);
4034 Py_XDECREF(freevars);
4035 Py_XDECREF(cellvars);
4036 Py_XDECREF(bytecode);
4037 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004038}
4039
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004040
4041/* For debugging purposes only */
4042#if 0
4043static void
4044dump_instr(const struct instr *i)
4045{
4046 const char *jrel = i->i_jrel ? "jrel " : "";
4047 const char *jabs = i->i_jabs ? "jabs " : "";
4048 char arg[128];
4049
4050 *arg = '\0';
4051 if (i->i_hasarg)
4052 sprintf(arg, "arg: %d ", i->i_oparg);
4053
4054 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4055 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4056}
4057
4058static void
4059dump_basicblock(const basicblock *b)
4060{
4061 const char *seen = b->b_seen ? "seen " : "";
4062 const char *b_return = b->b_return ? "return " : "";
4063 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4064 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4065 if (b->b_instr) {
4066 int i;
4067 for (i = 0; i < b->b_iused; i++) {
4068 fprintf(stderr, " [%02d] ", i);
4069 dump_instr(b->b_instr + i);
4070 }
4071 }
4072}
4073#endif
4074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075static PyCodeObject *
4076assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004077{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004078 basicblock *b, *entryblock;
4079 struct assembler a;
4080 int i, j, nblocks;
4081 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083 /* Make sure every block that falls off the end returns None.
4084 XXX NEXT_BLOCK() isn't quite right, because if the last
4085 block ends with a jump or return b_next shouldn't set.
4086 */
4087 if (!c->u->u_curblock->b_return) {
4088 NEXT_BLOCK(c);
4089 if (addNone)
4090 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4091 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004092 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094 nblocks = 0;
4095 entryblock = NULL;
4096 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4097 nblocks++;
4098 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004099 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004100
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004101 /* Set firstlineno if it wasn't explicitly set. */
4102 if (!c->u->u_firstlineno) {
4103 if (entryblock && entryblock->b_instr)
4104 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4105 else
4106 c->u->u_firstlineno = 1;
4107 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4109 goto error;
4110 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004113 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004114
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004115 /* Emit code in reverse postorder from dfs. */
4116 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004117 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 for (j = 0; j < b->b_iused; j++)
4119 if (!assemble_emit(&a, &b->b_instr[j]))
4120 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004121 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4124 goto error;
4125 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4126 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004127
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 co = makecode(c, &a);
4129 error:
4130 assemble_free(&a);
4131 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004132}