blob: ebb19f55e8a4f4729a4c17937617cbbfd0567b81 [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;
789 case FOR_ITER:
790 return 1;
791
792 case STORE_ATTR:
793 return -2;
794 case DELETE_ATTR:
795 return -1;
796 case STORE_GLOBAL:
797 return -1;
798 case DELETE_GLOBAL:
799 return 0;
800 case DUP_TOPX:
801 return oparg;
802 case LOAD_CONST:
803 return 1;
804 case LOAD_NAME:
805 return 1;
806 case BUILD_TUPLE:
807 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000808 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 return 1-oparg;
810 case BUILD_MAP:
811 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000812 case MAKE_BYTES:
813 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814 case LOAD_ATTR:
815 return 0;
816 case COMPARE_OP:
817 return -1;
818 case IMPORT_NAME:
819 return 0;
820 case IMPORT_FROM:
821 return 1;
822
823 case JUMP_FORWARD:
824 case JUMP_IF_FALSE:
825 case JUMP_IF_TRUE:
826 case JUMP_ABSOLUTE:
827 return 0;
828
829 case LOAD_GLOBAL:
830 return 1;
831
832 case CONTINUE_LOOP:
833 return 0;
834 case SETUP_LOOP:
835 return 0;
836 case SETUP_EXCEPT:
837 case SETUP_FINALLY:
838 return 3; /* actually pushed by an exception */
839
840 case LOAD_FAST:
841 return 1;
842 case STORE_FAST:
843 return -1;
844 case DELETE_FAST:
845 return 0;
846
847 case RAISE_VARARGS:
848 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000849#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 case CALL_FUNCTION:
851 return -NARGS(oparg);
852 case CALL_FUNCTION_VAR:
853 case CALL_FUNCTION_KW:
854 return -NARGS(oparg)-1;
855 case CALL_FUNCTION_VAR_KW:
856 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000858 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000859 case MAKE_CLOSURE:
860 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000861#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862 case BUILD_SLICE:
863 if (oparg == 3)
864 return -2;
865 else
866 return -1;
867
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 case LOAD_CLOSURE:
869 return 1;
870 case LOAD_DEREF:
871 return 1;
872 case STORE_DEREF:
873 return -1;
874 default:
875 fprintf(stderr, "opcode = %d\n", opcode);
876 Py_FatalError("opcode_stack_effect()");
877
878 }
879 return 0; /* not reachable */
880}
881
882/* Add an opcode with no argument.
883 Returns 0 on failure, 1 on success.
884*/
885
886static int
887compiler_addop(struct compiler *c, int opcode)
888{
889 basicblock *b;
890 struct instr *i;
891 int off;
892 off = compiler_next_instr(c, c->u->u_curblock);
893 if (off < 0)
894 return 0;
895 b = c->u->u_curblock;
896 i = &b->b_instr[off];
897 i->i_opcode = opcode;
898 i->i_hasarg = 0;
899 if (opcode == RETURN_VALUE)
900 b->b_return = 1;
901 compiler_set_lineno(c, off);
902 return 1;
903}
904
905static int
906compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
907{
908 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000909 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000911 /* necessary to make sure types aren't coerced (e.g., int and long) */
912 t = PyTuple_Pack(2, o, o->ob_type);
913 if (t == NULL)
914 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915
916 v = PyDict_GetItem(dict, t);
917 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000918 if (PyErr_Occurred())
919 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000920 arg = PyDict_Size(dict);
921 v = PyInt_FromLong(arg);
922 if (!v) {
923 Py_DECREF(t);
924 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000925 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 if (PyDict_SetItem(dict, t, v) < 0) {
927 Py_DECREF(t);
928 Py_DECREF(v);
929 return -1;
930 }
931 Py_DECREF(v);
932 }
933 else
934 arg = PyInt_AsLong(v);
935 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000936 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937}
938
939static int
940compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
941 PyObject *o)
942{
943 int arg = compiler_add_o(c, dict, o);
944 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000945 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946 return compiler_addop_i(c, opcode, arg);
947}
948
949static int
950compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000951 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952{
953 int arg;
954 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
955 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000956 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957 arg = compiler_add_o(c, dict, mangled);
958 Py_DECREF(mangled);
959 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000960 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 return compiler_addop_i(c, opcode, arg);
962}
963
964/* Add an opcode with an integer argument.
965 Returns 0 on failure, 1 on success.
966*/
967
968static int
969compiler_addop_i(struct compiler *c, int opcode, int oparg)
970{
971 struct instr *i;
972 int off;
973 off = compiler_next_instr(c, c->u->u_curblock);
974 if (off < 0)
975 return 0;
976 i = &c->u->u_curblock->b_instr[off];
977 i->i_opcode = opcode;
978 i->i_oparg = oparg;
979 i->i_hasarg = 1;
980 compiler_set_lineno(c, off);
981 return 1;
982}
983
984static int
985compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
986{
987 struct instr *i;
988 int off;
989
990 assert(b != NULL);
991 off = compiler_next_instr(c, c->u->u_curblock);
992 if (off < 0)
993 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 i = &c->u->u_curblock->b_instr[off];
995 i->i_opcode = opcode;
996 i->i_target = b;
997 i->i_hasarg = 1;
998 if (absolute)
999 i->i_jabs = 1;
1000 else
1001 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001002 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 return 1;
1004}
1005
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001006/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1007 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 it as the current block. NEXT_BLOCK() also creates an implicit jump
1009 from the current block to the new block.
1010*/
1011
Thomas Wouters89f507f2006-12-13 04:49:30 +00001012/* The returns inside these macros make it impossible to decref objects
1013 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014*/
1015
1016
1017#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001018 if (compiler_use_new_block((C)) == NULL) \
1019 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020}
1021
1022#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001023 if (compiler_next_block((C)) == NULL) \
1024 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025}
1026
1027#define ADDOP(C, OP) { \
1028 if (!compiler_addop((C), (OP))) \
1029 return 0; \
1030}
1031
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001032#define ADDOP_IN_SCOPE(C, OP) { \
1033 if (!compiler_addop((C), (OP))) { \
1034 compiler_exit_scope(c); \
1035 return 0; \
1036 } \
1037}
1038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039#define ADDOP_O(C, OP, O, TYPE) { \
1040 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1041 return 0; \
1042}
1043
1044#define ADDOP_NAME(C, OP, O, TYPE) { \
1045 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1046 return 0; \
1047}
1048
1049#define ADDOP_I(C, OP, O) { \
1050 if (!compiler_addop_i((C), (OP), (O))) \
1051 return 0; \
1052}
1053
1054#define ADDOP_JABS(C, OP, O) { \
1055 if (!compiler_addop_j((C), (OP), (O), 1)) \
1056 return 0; \
1057}
1058
1059#define ADDOP_JREL(C, OP, O) { \
1060 if (!compiler_addop_j((C), (OP), (O), 0)) \
1061 return 0; \
1062}
1063
1064/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1065 the ASDL name to synthesize the name of the C type and the visit function.
1066*/
1067
1068#define VISIT(C, TYPE, V) {\
1069 if (!compiler_visit_ ## TYPE((C), (V))) \
1070 return 0; \
1071}
1072
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001073#define VISIT_IN_SCOPE(C, TYPE, V) {\
1074 if (!compiler_visit_ ## TYPE((C), (V))) { \
1075 compiler_exit_scope(c); \
1076 return 0; \
1077 } \
1078}
1079
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080#define VISIT_SLICE(C, V, CTX) {\
1081 if (!compiler_visit_slice((C), (V), (CTX))) \
1082 return 0; \
1083}
1084
1085#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001086 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001088 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090 if (!compiler_visit_ ## TYPE((C), elt)) \
1091 return 0; \
1092 } \
1093}
1094
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001095#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001096 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001097 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001098 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001099 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001100 if (!compiler_visit_ ## TYPE((C), elt)) { \
1101 compiler_exit_scope(c); \
1102 return 0; \
1103 } \
1104 } \
1105}
1106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107static int
1108compiler_isdocstring(stmt_ty s)
1109{
1110 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001111 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112 return s->v.Expr.value->kind == Str_kind;
1113}
1114
1115/* Compile a sequence of statements, checking for a docstring. */
1116
1117static int
1118compiler_body(struct compiler *c, asdl_seq *stmts)
1119{
1120 int i = 0;
1121 stmt_ty st;
1122
1123 if (!asdl_seq_LEN(stmts))
1124 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (compiler_isdocstring(st)) {
1127 i = 1;
1128 VISIT(c, expr, st->v.Expr.value);
1129 if (!compiler_nameop(c, __doc__, Store))
1130 return 0;
1131 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001132 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 return 1;
1135}
1136
1137static PyCodeObject *
1138compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001139{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001140 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001141 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 static PyObject *module;
1143 if (!module) {
1144 module = PyString_FromString("<module>");
1145 if (!module)
1146 return NULL;
1147 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001148 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1149 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001150 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 switch (mod->kind) {
1152 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001153 if (!compiler_body(c, mod->v.Module.body)) {
1154 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001156 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 break;
1158 case Interactive_kind:
1159 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001160 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001161 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 break;
1163 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001164 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001165 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166 break;
1167 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001168 PyErr_SetString(PyExc_SystemError,
1169 "suite should not be possible");
1170 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001171 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001172 PyErr_Format(PyExc_SystemError,
1173 "module kind %d should not be possible",
1174 mod->kind);
1175 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001176 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 co = assemble(c, addNone);
1178 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001179 return co;
1180}
1181
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182/* The test for LOCAL must come before the test for FREE in order to
1183 handle classes where name is both local and free. The local var is
1184 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001185*/
1186
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187static int
1188get_ref_type(struct compiler *c, PyObject *name)
1189{
1190 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001191 if (scope == 0) {
1192 char buf[350];
1193 PyOS_snprintf(buf, sizeof(buf),
1194 "unknown scope for %.100s in %.100s(%s) in %s\n"
1195 "symbols: %s\nlocals: %s\nglobals: %s\n",
1196 PyString_AS_STRING(name),
1197 PyString_AS_STRING(c->u->u_name),
1198 PyObject_REPR(c->u->u_ste->ste_id),
1199 c->c_filename,
1200 PyObject_REPR(c->u->u_ste->ste_symbols),
1201 PyObject_REPR(c->u->u_varnames),
1202 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001204 Py_FatalError(buf);
1205 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001206
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001207 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208}
1209
1210static int
1211compiler_lookup_arg(PyObject *dict, PyObject *name)
1212{
1213 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001214 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001216 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001218 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001220 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 return PyInt_AS_LONG(v);
1222}
1223
1224static int
1225compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1226{
1227 int i, free = PyCode_GetNumFree(co);
1228 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001229 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1230 ADDOP_I(c, MAKE_FUNCTION, args);
1231 return 1;
1232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001233 for (i = 0; i < free; ++i) {
1234 /* Bypass com_addop_varname because it will generate
1235 LOAD_DEREF but LOAD_CLOSURE is needed.
1236 */
1237 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1238 int arg, reftype;
1239
1240 /* Special case: If a class contains a method with a
1241 free variable that has the same name as a method,
1242 the name will be considered free *and* local in the
1243 class. It should be handled by the closure, as
1244 well as by the normal name loookup logic.
1245 */
1246 reftype = get_ref_type(c, name);
1247 if (reftype == CELL)
1248 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1249 else /* (reftype == FREE) */
1250 arg = compiler_lookup_arg(c->u->u_freevars, name);
1251 if (arg == -1) {
1252 printf("lookup %s in %s %d %d\n"
1253 "freevars of %s: %s\n",
1254 PyObject_REPR(name),
1255 PyString_AS_STRING(c->u->u_name),
1256 reftype, arg,
1257 PyString_AS_STRING(co->co_name),
1258 PyObject_REPR(co->co_freevars));
1259 Py_FatalError("compiler_make_closure()");
1260 }
1261 ADDOP_I(c, LOAD_CLOSURE, arg);
1262 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001263 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001265 ADDOP_I(c, MAKE_CLOSURE, args);
1266 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
1269static int
1270compiler_decorators(struct compiler *c, asdl_seq* decos)
1271{
1272 int i;
1273
1274 if (!decos)
1275 return 1;
1276
1277 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001278 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 }
1280 return 1;
1281}
1282
1283static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001284compiler_unpack_nested(struct compiler *c, asdl_seq *args) {
1285 int i, len;
1286 len = asdl_seq_LEN(args);
1287 ADDOP_I(c, UNPACK_SEQUENCE, len);
1288 for (i = 0; i < len; i++) {
1289 arg_ty elt = (arg_ty)asdl_seq_GET(args, i);
1290 switch (elt->kind) {
1291 case SimpleArg_kind:
1292 if (!compiler_nameop(c, elt->v.SimpleArg.arg, Store))
1293 return 0;
1294 break;
1295 case NestedArgs_kind:
1296 if (!compiler_unpack_nested(c, elt->v.NestedArgs.args))
1297 return 0;
1298 break;
1299 default:
1300 return 0;
1301 }
1302 }
1303 return 1;
1304}
1305
1306static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307compiler_arguments(struct compiler *c, arguments_ty args)
1308{
1309 int i;
1310 int n = asdl_seq_LEN(args->args);
Neal Norwitzc1505362006-12-28 06:47:50 +00001311
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 for (i = 0; i < n; i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001313 arg_ty arg = (arg_ty)asdl_seq_GET(args->args, i);
1314 if (arg->kind == NestedArgs_kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001315 PyObject *id = PyString_FromFormat(".%d", i);
1316 if (id == NULL) {
1317 return 0;
1318 }
1319 if (!compiler_nameop(c, id, Load)) {
1320 Py_DECREF(id);
1321 return 0;
1322 }
1323 Py_DECREF(id);
Neal Norwitzc1505362006-12-28 06:47:50 +00001324 if (!compiler_unpack_nested(c, arg->v.NestedArgs.args))
1325 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 }
1327 }
1328 return 1;
1329}
1330
1331static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001332compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1333 asdl_seq *kw_defaults)
1334{
1335 int i, default_count = 0;
1336 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001337 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1339 if (default_) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001340 ADDOP_O(c, LOAD_CONST, arg->v.SimpleArg.arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001341 if (!compiler_visit_expr(c, default_)) {
1342 return -1;
1343 }
1344 default_count++;
1345 }
1346 }
1347 return default_count;
1348}
1349
1350static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001351compiler_visit_argannotation(struct compiler *c, identifier id,
1352 expr_ty annotation, PyObject *names)
1353{
1354 if (annotation) {
1355 VISIT(c, expr, annotation);
1356 if (PyList_Append(names, id))
1357 return -1;
1358 }
1359 return 0;
1360}
1361
1362static int
1363compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1364 PyObject *names)
1365{
1366 int i, error;
1367 for (i = 0; i < asdl_seq_LEN(args); i++) {
1368 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1369 if (arg->kind == NestedArgs_kind)
1370 error = compiler_visit_argannotations(
1371 c,
1372 arg->v.NestedArgs.args,
1373 names);
1374 else
1375 error = compiler_visit_argannotation(
1376 c,
1377 arg->v.SimpleArg.arg,
1378 arg->v.SimpleArg.annotation,
1379 names);
1380 if (error)
1381 return error;
1382 }
1383 return 0;
1384}
1385
1386static int
1387compiler_visit_annotations(struct compiler *c, arguments_ty args,
1388 expr_ty returns)
1389{
Guido van Rossum0240b922007-02-26 21:23:50 +00001390 /* Push arg annotations and a list of the argument names. Return the #
1391 of items pushed. The expressions are evaluated out-of-order wrt the
1392 source code.
1393
1394 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1395 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001396 static identifier return_str;
1397 PyObject *names;
1398 int len;
1399 names = PyList_New(0);
1400 if (!names)
1401 return -1;
1402
1403 if (compiler_visit_argannotations(c, args->args, names))
1404 goto error;
1405 if (args->varargannotation &&
1406 compiler_visit_argannotation(c, args->vararg,
1407 args->varargannotation, names))
1408 goto error;
1409 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1410 goto error;
1411 if (args->kwargannotation &&
1412 compiler_visit_argannotation(c, args->kwarg,
1413 args->kwargannotation, names))
1414 goto error;
1415
1416 if (!return_str) {
1417 return_str = PyString_InternFromString("return");
1418 if (!return_str)
1419 goto error;
1420 }
1421 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1422 goto error;
1423 }
1424
1425 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001426 if (len > 65534) {
1427 /* len must fit in 16 bits, and len is incremented below */
1428 PyErr_SetString(PyExc_SyntaxError,
1429 "too many annotations");
1430 goto error;
1431 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001432 if (len) {
1433 /* convert names to a tuple and place on stack */
1434 PyObject *elt;
1435 int i;
1436 PyObject *s = PyTuple_New(len);
1437 if (!s)
1438 goto error;
1439 for (i = 0; i < len; i++) {
1440 elt = PyList_GET_ITEM(names, i);
1441 Py_INCREF(elt);
1442 PyTuple_SET_ITEM(s, i, elt);
1443 }
1444 ADDOP_O(c, LOAD_CONST, s, consts);
1445 Py_DECREF(s);
1446 len++; /* include the just-pushed tuple */
1447 }
1448 Py_DECREF(names);
1449 return len;
1450
1451error:
1452 Py_DECREF(names);
1453 return -1;
1454}
1455
1456static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457compiler_function(struct compiler *c, stmt_ty s)
1458{
1459 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001460 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001462 expr_ty returns = s->v.FunctionDef.returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 asdl_seq* decos = s->v.FunctionDef.decorators;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001464 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
1468 assert(s->kind == FunctionDef_kind);
1469
1470 if (!compiler_decorators(c, decos))
1471 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 if (args->kwonlyargs) {
1473 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1474 args->kw_defaults);
1475 if (res < 0)
1476 return 0;
1477 kw_default_count = res;
1478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 if (args->defaults)
1480 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001481 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001482 if (num_annotations < 0)
1483 return 0;
1484 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001485
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1487 s->lineno))
1488 return 0;
1489
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001491 docstring = compiler_isdocstring(st);
1492 if (docstring)
1493 first_const = st->v.Expr.value->v.Str.s;
1494 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001495 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001496 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001499 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500 compiler_arguments(c, args);
1501
1502 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001503 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001505 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001507 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1508 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 }
1510 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001511 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 if (co == NULL)
1513 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514
Guido van Rossum4f72a782006-10-27 23:31:49 +00001515 arglength = asdl_seq_LEN(args->defaults);
1516 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001517 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001518 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001519 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Neal Norwitzc1505362006-12-28 06:47:50 +00001521 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1523 ADDOP_I(c, CALL_FUNCTION, 1);
1524 }
1525
1526 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1527}
1528
1529static int
1530compiler_class(struct compiler *c, stmt_ty s)
1531{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001532 static PyObject *build_class = NULL;
1533 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001535 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001536 PySTEntryObject *ste;
Guido van Rossum3a383622007-03-21 21:26:58 +00001537 int err;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001539 /* initialize statics */
1540 if (build_class == NULL) {
1541 build_class = PyString_FromString("__build_class__");
1542 if (build_class == NULL)
1543 return 0;
1544 }
1545 if (locals == NULL) {
1546 locals = PyString_FromString("__locals__");
1547 if (locals == NULL)
1548 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001550
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001551 /* ultimately generate code for:
1552 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1553 where:
1554 <func> is a function/closure created from the class body
1555 <name> is the class name
1556 <bases> is the positional arguments and *varargs argument
1557 <keywords> is the keyword arguments and **kwds argument
1558 This borrows from compiler_call.
1559 */
1560
1561 /* 0. Create a fake variable named __locals__ */
1562 ste = PySymtable_Lookup(c->c_st, s);
1563 if (ste == NULL)
1564 return 0;
1565 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001566 err = PyList_Append(ste->ste_varnames, locals);
1567 Py_DECREF(ste);
1568 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001569 return 0;
1570
1571 /* 1. compile the class body into a code object */
1572 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1573 return 0;
1574 /* this block represents what we do in the new scope */
1575 {
1576 /* use the class name for name mangling */
1577 Py_INCREF(s->v.ClassDef.name);
1578 c->u->u_private = s->v.ClassDef.name;
1579 /* force it to have one mandatory argument */
1580 c->u->u_argcount = 1;
1581 /* load the first argument ... */
1582 ADDOP_I(c, LOAD_FAST, 0);
1583 /* ... and store it into f_locals */
1584 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1585 /* load __name__ ... */
1586 str = PyString_InternFromString("__name__");
1587 if (!str || !compiler_nameop(c, str, Load)) {
1588 Py_XDECREF(str);
1589 compiler_exit_scope(c);
1590 return 0;
1591 }
1592 Py_DECREF(str);
1593 /* ... and store it as __module__ */
1594 str = PyString_InternFromString("__module__");
1595 if (!str || !compiler_nameop(c, str, Store)) {
1596 Py_XDECREF(str);
1597 compiler_exit_scope(c);
1598 return 0;
1599 }
1600 Py_DECREF(str);
1601 /* compile the body proper */
1602 if (!compiler_body(c, s->v.ClassDef.body)) {
1603 compiler_exit_scope(c);
1604 return 0;
1605 }
1606 /* return None */
1607 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1608 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1609 /* create the code object */
1610 co = assemble(c, 1);
1611 }
1612 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001613 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001614 if (co == NULL)
1615 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001617 /* 2. load the 'build_class' function */
1618 ADDOP(c, LOAD_BUILD_CLASS);
1619
1620 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001621 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001622 Py_DECREF(co);
1623
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001624 /* 4. load class name */
1625 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1626
1627 /* 5. generate the rest of the code for the call */
1628 if (!compiler_call_helper(c, 2,
1629 s->v.ClassDef.bases,
1630 s->v.ClassDef.keywords,
1631 s->v.ClassDef.starargs,
1632 s->v.ClassDef.kwargs))
1633 return 0;
1634
1635 /* 6. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1637 return 0;
1638 return 1;
1639}
1640
1641static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001642compiler_ifexp(struct compiler *c, expr_ty e)
1643{
1644 basicblock *end, *next;
1645
1646 assert(e->kind == IfExp_kind);
1647 end = compiler_new_block(c);
1648 if (end == NULL)
1649 return 0;
1650 next = compiler_new_block(c);
1651 if (next == NULL)
1652 return 0;
1653 VISIT(c, expr, e->v.IfExp.test);
1654 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1655 ADDOP(c, POP_TOP);
1656 VISIT(c, expr, e->v.IfExp.body);
1657 ADDOP_JREL(c, JUMP_FORWARD, end);
1658 compiler_use_next_block(c, next);
1659 ADDOP(c, POP_TOP);
1660 VISIT(c, expr, e->v.IfExp.orelse);
1661 compiler_use_next_block(c, end);
1662 return 1;
1663}
1664
1665static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666compiler_lambda(struct compiler *c, expr_ty e)
1667{
1668 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001669 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001670 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 arguments_ty args = e->v.Lambda.args;
1672 assert(e->kind == Lambda_kind);
1673
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001674 if (!name) {
1675 name = PyString_InternFromString("<lambda>");
1676 if (!name)
1677 return 0;
1678 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679
Guido van Rossum4f72a782006-10-27 23:31:49 +00001680 if (args->kwonlyargs) {
1681 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1682 args->kw_defaults);
1683 if (res < 0) return 0;
1684 kw_default_count = res;
1685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686 if (args->defaults)
1687 VISIT_SEQ(c, expr, args->defaults);
1688 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1689 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001690
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001691 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692 compiler_arguments(c, args);
1693
1694 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001695 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001696 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1697 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001699 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 if (co == NULL)
1701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702
Guido van Rossum4f72a782006-10-27 23:31:49 +00001703 arglength = asdl_seq_LEN(args->defaults);
1704 arglength |= kw_default_count << 8;
1705 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001706 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707
1708 return 1;
1709}
1710
1711static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712compiler_if(struct compiler *c, stmt_ty s)
1713{
1714 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001715 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 assert(s->kind == If_kind);
1717 end = compiler_new_block(c);
1718 if (end == NULL)
1719 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001720 next = compiler_new_block(c);
1721 if (next == NULL)
1722 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001723
1724 constant = expr_constant(s->v.If.test);
1725 /* constant = 0: "if 0"
1726 * constant = 1: "if 1", "if 2", ...
1727 * constant = -1: rest */
1728 if (constant == 0) {
1729 if (s->v.If.orelse)
1730 VISIT_SEQ(c, stmt, s->v.If.orelse);
1731 } else if (constant == 1) {
1732 VISIT_SEQ(c, stmt, s->v.If.body);
1733 } else {
1734 VISIT(c, expr, s->v.If.test);
1735 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1736 ADDOP(c, POP_TOP);
1737 VISIT_SEQ(c, stmt, s->v.If.body);
1738 ADDOP_JREL(c, JUMP_FORWARD, end);
1739 compiler_use_next_block(c, next);
1740 ADDOP(c, POP_TOP);
1741 if (s->v.If.orelse)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001742 VISIT_SEQ(c, stmt, s->v.If.orelse);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001743 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744 compiler_use_next_block(c, end);
1745 return 1;
1746}
1747
1748static int
1749compiler_for(struct compiler *c, stmt_ty s)
1750{
1751 basicblock *start, *cleanup, *end;
1752
1753 start = compiler_new_block(c);
1754 cleanup = compiler_new_block(c);
1755 end = compiler_new_block(c);
1756 if (start == NULL || end == NULL || cleanup == NULL)
1757 return 0;
1758 ADDOP_JREL(c, SETUP_LOOP, end);
1759 if (!compiler_push_fblock(c, LOOP, start))
1760 return 0;
1761 VISIT(c, expr, s->v.For.iter);
1762 ADDOP(c, GET_ITER);
1763 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001764 /* XXX(nnorwitz): is there a better way to handle this?
1765 for loops are special, we want to be able to trace them
1766 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001767 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 ADDOP_JREL(c, FOR_ITER, cleanup);
1769 VISIT(c, expr, s->v.For.target);
1770 VISIT_SEQ(c, stmt, s->v.For.body);
1771 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1772 compiler_use_next_block(c, cleanup);
1773 ADDOP(c, POP_BLOCK);
1774 compiler_pop_fblock(c, LOOP, start);
1775 VISIT_SEQ(c, stmt, s->v.For.orelse);
1776 compiler_use_next_block(c, end);
1777 return 1;
1778}
1779
1780static int
1781compiler_while(struct compiler *c, stmt_ty s)
1782{
1783 basicblock *loop, *orelse, *end, *anchor = NULL;
1784 int constant = expr_constant(s->v.While.test);
1785
1786 if (constant == 0)
1787 return 1;
1788 loop = compiler_new_block(c);
1789 end = compiler_new_block(c);
1790 if (constant == -1) {
1791 anchor = compiler_new_block(c);
1792 if (anchor == NULL)
1793 return 0;
1794 }
1795 if (loop == NULL || end == NULL)
1796 return 0;
1797 if (s->v.While.orelse) {
1798 orelse = compiler_new_block(c);
1799 if (orelse == NULL)
1800 return 0;
1801 }
1802 else
1803 orelse = NULL;
1804
1805 ADDOP_JREL(c, SETUP_LOOP, end);
1806 compiler_use_next_block(c, loop);
1807 if (!compiler_push_fblock(c, LOOP, loop))
1808 return 0;
1809 if (constant == -1) {
1810 VISIT(c, expr, s->v.While.test);
1811 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1812 ADDOP(c, POP_TOP);
1813 }
1814 VISIT_SEQ(c, stmt, s->v.While.body);
1815 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1816
1817 /* XXX should the two POP instructions be in a separate block
1818 if there is no else clause ?
1819 */
1820
1821 if (constant == -1) {
1822 compiler_use_next_block(c, anchor);
1823 ADDOP(c, POP_TOP);
1824 ADDOP(c, POP_BLOCK);
1825 }
1826 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001827 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828 VISIT_SEQ(c, stmt, s->v.While.orelse);
1829 compiler_use_next_block(c, end);
1830
1831 return 1;
1832}
1833
1834static int
1835compiler_continue(struct compiler *c)
1836{
1837 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001838 static const char IN_FINALLY_ERROR_MSG[] =
1839 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001840 int i;
1841
1842 if (!c->u->u_nfblocks)
1843 return compiler_error(c, LOOP_ERROR_MSG);
1844 i = c->u->u_nfblocks - 1;
1845 switch (c->u->u_fblock[i].fb_type) {
1846 case LOOP:
1847 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1848 break;
1849 case EXCEPT:
1850 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001851 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1852 /* Prevent continue anywhere under a finally
1853 even if hidden in a sub-try or except. */
1854 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1855 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1856 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857 if (i == -1)
1858 return compiler_error(c, LOOP_ERROR_MSG);
1859 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1860 break;
1861 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001862 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 }
1864
1865 return 1;
1866}
1867
1868/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1869
1870 SETUP_FINALLY L
1871 <code for body>
1872 POP_BLOCK
1873 LOAD_CONST <None>
1874 L: <code for finalbody>
1875 END_FINALLY
1876
1877 The special instructions use the block stack. Each block
1878 stack entry contains the instruction that created it (here
1879 SETUP_FINALLY), the level of the value stack at the time the
1880 block stack entry was created, and a label (here L).
1881
1882 SETUP_FINALLY:
1883 Pushes the current value stack level and the label
1884 onto the block stack.
1885 POP_BLOCK:
1886 Pops en entry from the block stack, and pops the value
1887 stack until its level is the same as indicated on the
1888 block stack. (The label is ignored.)
1889 END_FINALLY:
1890 Pops a variable number of entries from the *value* stack
1891 and re-raises the exception they specify. The number of
1892 entries popped depends on the (pseudo) exception type.
1893
1894 The block stack is unwound when an exception is raised:
1895 when a SETUP_FINALLY entry is found, the exception is pushed
1896 onto the value stack (and the exception condition is cleared),
1897 and the interpreter jumps to the label gotten from the block
1898 stack.
1899*/
1900
1901static int
1902compiler_try_finally(struct compiler *c, stmt_ty s)
1903{
1904 basicblock *body, *end;
1905 body = compiler_new_block(c);
1906 end = compiler_new_block(c);
1907 if (body == NULL || end == NULL)
1908 return 0;
1909
1910 ADDOP_JREL(c, SETUP_FINALLY, end);
1911 compiler_use_next_block(c, body);
1912 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1913 return 0;
1914 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1915 ADDOP(c, POP_BLOCK);
1916 compiler_pop_fblock(c, FINALLY_TRY, body);
1917
1918 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1919 compiler_use_next_block(c, end);
1920 if (!compiler_push_fblock(c, FINALLY_END, end))
1921 return 0;
1922 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1923 ADDOP(c, END_FINALLY);
1924 compiler_pop_fblock(c, FINALLY_END, end);
1925
1926 return 1;
1927}
1928
1929/*
1930 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1931 (The contents of the value stack is shown in [], with the top
1932 at the right; 'tb' is trace-back info, 'val' the exception's
1933 associated value, and 'exc' the exception.)
1934
1935 Value stack Label Instruction Argument
1936 [] SETUP_EXCEPT L1
1937 [] <code for S>
1938 [] POP_BLOCK
1939 [] JUMP_FORWARD L0
1940
1941 [tb, val, exc] L1: DUP )
1942 [tb, val, exc, exc] <evaluate E1> )
1943 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1944 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1945 [tb, val, exc, 1] POP )
1946 [tb, val, exc] POP
1947 [tb, val] <assign to V1> (or POP if no V1)
1948 [tb] POP
1949 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001950 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951
1952 [tb, val, exc, 0] L2: POP
1953 [tb, val, exc] DUP
1954 .............................etc.......................
1955
1956 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001957 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958
1959 [] L0: <next statement>
1960
1961 Of course, parts are not generated if Vi or Ei is not present.
1962*/
1963static int
1964compiler_try_except(struct compiler *c, stmt_ty s)
1965{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001966 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001967 int i, n;
1968
1969 body = compiler_new_block(c);
1970 except = compiler_new_block(c);
1971 orelse = compiler_new_block(c);
1972 end = compiler_new_block(c);
1973 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1974 return 0;
1975 ADDOP_JREL(c, SETUP_EXCEPT, except);
1976 compiler_use_next_block(c, body);
1977 if (!compiler_push_fblock(c, EXCEPT, body))
1978 return 0;
1979 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1980 ADDOP(c, POP_BLOCK);
1981 compiler_pop_fblock(c, EXCEPT, body);
1982 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1983 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1984 compiler_use_next_block(c, except);
1985 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001986 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 s->v.TryExcept.handlers, i);
1988 if (!handler->type && i < n-1)
1989 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001990 c->u->u_lineno_set = 0;
1991 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 except = compiler_new_block(c);
1993 if (except == NULL)
1994 return 0;
1995 if (handler->type) {
1996 ADDOP(c, DUP_TOP);
1997 VISIT(c, expr, handler->type);
1998 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1999 ADDOP_JREL(c, JUMP_IF_FALSE, except);
2000 ADDOP(c, POP_TOP);
2001 }
2002 ADDOP(c, POP_TOP);
2003 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00002004 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002005
2006 cleanup_end = compiler_new_block(c);
2007 cleanup_body = compiler_new_block(c);
2008 if(!(cleanup_end || cleanup_body))
2009 return 0;
2010
Guido van Rossum16be03e2007-01-10 18:51:35 +00002011 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002012 ADDOP(c, POP_TOP);
2013
2014 /*
2015 try:
2016 # body
2017 except type as name:
2018 try:
2019 # body
2020 finally:
2021 name = None
2022 del name
2023 */
2024
2025 /* second try: */
2026 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2027 compiler_use_next_block(c, cleanup_body);
2028 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2029 return 0;
2030
2031 /* second # body */
2032 VISIT_SEQ(c, stmt, handler->body);
2033 ADDOP(c, POP_BLOCK);
2034 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2035
2036 /* finally: */
2037 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2038 compiler_use_next_block(c, cleanup_end);
2039 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2040 return 0;
2041
2042 /* name = None */
2043 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00002044 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002045
Guido van Rossum16be03e2007-01-10 18:51:35 +00002046 /* del name */
2047 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002048
2049 ADDOP(c, END_FINALLY);
2050 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 }
2052 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002053 ADDOP(c, POP_TOP);
2054 ADDOP(c, POP_TOP);
2055 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 ADDOP_JREL(c, JUMP_FORWARD, end);
2058 compiler_use_next_block(c, except);
2059 if (handler->type)
2060 ADDOP(c, POP_TOP);
2061 }
2062 ADDOP(c, END_FINALLY);
2063 compiler_use_next_block(c, orelse);
2064 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2065 compiler_use_next_block(c, end);
2066 return 1;
2067}
2068
2069static int
2070compiler_import_as(struct compiler *c, identifier name, identifier asname)
2071{
2072 /* The IMPORT_NAME opcode was already generated. This function
2073 merely needs to bind the result to a name.
2074
2075 If there is a dot in name, we need to split it and emit a
2076 LOAD_ATTR for each name.
2077 */
2078 const char *src = PyString_AS_STRING(name);
2079 const char *dot = strchr(src, '.');
2080 if (dot) {
2081 /* Consume the base module name to get the first attribute */
2082 src = dot + 1;
2083 while (dot) {
2084 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002085 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002087 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002089 if (!attr)
2090 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002092 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 src = dot + 1;
2094 }
2095 }
2096 return compiler_nameop(c, asname, Store);
2097}
2098
2099static int
2100compiler_import(struct compiler *c, stmt_ty s)
2101{
2102 /* The Import node stores a module name like a.b.c as a single
2103 string. This is convenient for all cases except
2104 import a.b.c as d
2105 where we need to parse that string to extract the individual
2106 module names.
2107 XXX Perhaps change the representation to make this case simpler?
2108 */
2109 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002112 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002114 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115
Guido van Rossum45aecf42006-03-15 04:58:47 +00002116 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002117 if (level == NULL)
2118 return 0;
2119
2120 ADDOP_O(c, LOAD_CONST, level, consts);
2121 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2123 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2124
2125 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002126 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002127 if (!r)
2128 return r;
2129 }
2130 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 identifier tmp = alias->name;
2132 const char *base = PyString_AS_STRING(alias->name);
2133 char *dot = strchr(base, '.');
2134 if (dot)
2135 tmp = PyString_FromStringAndSize(base,
2136 dot - base);
2137 r = compiler_nameop(c, tmp, Store);
2138 if (dot) {
2139 Py_DECREF(tmp);
2140 }
2141 if (!r)
2142 return r;
2143 }
2144 }
2145 return 1;
2146}
2147
2148static int
2149compiler_from_import(struct compiler *c, stmt_ty s)
2150{
2151 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152
2153 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002154 PyObject *level;
2155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 if (!names)
2157 return 0;
2158
Guido van Rossum45aecf42006-03-15 04:58:47 +00002159 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002160 if (!level) {
2161 Py_DECREF(names);
2162 return 0;
2163 }
2164
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 /* build up the names */
2166 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002167 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 Py_INCREF(alias->name);
2169 PyTuple_SET_ITEM(names, i, alias->name);
2170 }
2171
2172 if (s->lineno > c->c_future->ff_lineno) {
2173 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2174 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002175 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 Py_DECREF(names);
2177 return compiler_error(c,
2178 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002179 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180
2181 }
2182 }
2183
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002184 ADDOP_O(c, LOAD_CONST, level, consts);
2185 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002187 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2189 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002190 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 identifier store_name;
2192
2193 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2194 assert(n == 1);
2195 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002196 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002197 }
2198
2199 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2200 store_name = alias->name;
2201 if (alias->asname)
2202 store_name = alias->asname;
2203
2204 if (!compiler_nameop(c, store_name, Store)) {
2205 Py_DECREF(names);
2206 return 0;
2207 }
2208 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002209 /* remove imported module */
2210 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 return 1;
2212}
2213
2214static int
2215compiler_assert(struct compiler *c, stmt_ty s)
2216{
2217 static PyObject *assertion_error = NULL;
2218 basicblock *end;
2219
2220 if (Py_OptimizeFlag)
2221 return 1;
2222 if (assertion_error == NULL) {
2223 assertion_error = PyString_FromString("AssertionError");
2224 if (assertion_error == NULL)
2225 return 0;
2226 }
2227 VISIT(c, expr, s->v.Assert.test);
2228 end = compiler_new_block(c);
2229 if (end == NULL)
2230 return 0;
2231 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2232 ADDOP(c, POP_TOP);
2233 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2234 if (s->v.Assert.msg) {
2235 VISIT(c, expr, s->v.Assert.msg);
2236 ADDOP_I(c, RAISE_VARARGS, 2);
2237 }
2238 else {
2239 ADDOP_I(c, RAISE_VARARGS, 1);
2240 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002241 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 ADDOP(c, POP_TOP);
2243 return 1;
2244}
2245
2246static int
2247compiler_visit_stmt(struct compiler *c, stmt_ty s)
2248{
2249 int i, n;
2250
Thomas Wouters89f507f2006-12-13 04:49:30 +00002251 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002253 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002254
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002256 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002258 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002260 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 if (c->u->u_ste->ste_type != FunctionBlock)
2262 return compiler_error(c, "'return' outside function");
2263 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 VISIT(c, expr, s->v.Return.value);
2265 }
2266 else
2267 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2268 ADDOP(c, RETURN_VALUE);
2269 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002270 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 VISIT_SEQ(c, expr, s->v.Delete.targets)
2272 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002273 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 n = asdl_seq_LEN(s->v.Assign.targets);
2275 VISIT(c, expr, s->v.Assign.value);
2276 for (i = 0; i < n; i++) {
2277 if (i < n - 1)
2278 ADDOP(c, DUP_TOP);
2279 VISIT(c, expr,
2280 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2281 }
2282 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002283 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002285 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002287 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002289 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002291 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 n = 0;
2293 if (s->v.Raise.type) {
2294 VISIT(c, expr, s->v.Raise.type);
2295 n++;
2296 if (s->v.Raise.inst) {
2297 VISIT(c, expr, s->v.Raise.inst);
2298 n++;
2299 if (s->v.Raise.tback) {
2300 VISIT(c, expr, s->v.Raise.tback);
2301 n++;
2302 }
2303 }
2304 }
2305 ADDOP_I(c, RAISE_VARARGS, n);
2306 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002307 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002309 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002310 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002311 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002313 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002315 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002317 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002318 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002319 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002320 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002322 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 ADDOP(c, PRINT_EXPR);
2324 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002325 else if (s->v.Expr.value->kind != Str_kind &&
2326 s->v.Expr.value->kind != Num_kind) {
2327 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 ADDOP(c, POP_TOP);
2329 }
2330 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002331 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002333 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002334 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 return compiler_error(c, "'break' outside loop");
2336 ADDOP(c, BREAK_LOOP);
2337 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002338 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002340 case With_kind:
2341 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 }
2343 return 1;
2344}
2345
2346static int
2347unaryop(unaryop_ty op)
2348{
2349 switch (op) {
2350 case Invert:
2351 return UNARY_INVERT;
2352 case Not:
2353 return UNARY_NOT;
2354 case UAdd:
2355 return UNARY_POSITIVE;
2356 case USub:
2357 return UNARY_NEGATIVE;
2358 }
2359 return 0;
2360}
2361
2362static int
2363binop(struct compiler *c, operator_ty op)
2364{
2365 switch (op) {
2366 case Add:
2367 return BINARY_ADD;
2368 case Sub:
2369 return BINARY_SUBTRACT;
2370 case Mult:
2371 return BINARY_MULTIPLY;
2372 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002373 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 case Mod:
2375 return BINARY_MODULO;
2376 case Pow:
2377 return BINARY_POWER;
2378 case LShift:
2379 return BINARY_LSHIFT;
2380 case RShift:
2381 return BINARY_RSHIFT;
2382 case BitOr:
2383 return BINARY_OR;
2384 case BitXor:
2385 return BINARY_XOR;
2386 case BitAnd:
2387 return BINARY_AND;
2388 case FloorDiv:
2389 return BINARY_FLOOR_DIVIDE;
2390 }
2391 return 0;
2392}
2393
2394static int
2395cmpop(cmpop_ty op)
2396{
2397 switch (op) {
2398 case Eq:
2399 return PyCmp_EQ;
2400 case NotEq:
2401 return PyCmp_NE;
2402 case Lt:
2403 return PyCmp_LT;
2404 case LtE:
2405 return PyCmp_LE;
2406 case Gt:
2407 return PyCmp_GT;
2408 case GtE:
2409 return PyCmp_GE;
2410 case Is:
2411 return PyCmp_IS;
2412 case IsNot:
2413 return PyCmp_IS_NOT;
2414 case In:
2415 return PyCmp_IN;
2416 case NotIn:
2417 return PyCmp_NOT_IN;
2418 }
2419 return PyCmp_BAD;
2420}
2421
2422static int
2423inplace_binop(struct compiler *c, operator_ty op)
2424{
2425 switch (op) {
2426 case Add:
2427 return INPLACE_ADD;
2428 case Sub:
2429 return INPLACE_SUBTRACT;
2430 case Mult:
2431 return INPLACE_MULTIPLY;
2432 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002433 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 case Mod:
2435 return INPLACE_MODULO;
2436 case Pow:
2437 return INPLACE_POWER;
2438 case LShift:
2439 return INPLACE_LSHIFT;
2440 case RShift:
2441 return INPLACE_RSHIFT;
2442 case BitOr:
2443 return INPLACE_OR;
2444 case BitXor:
2445 return INPLACE_XOR;
2446 case BitAnd:
2447 return INPLACE_AND;
2448 case FloorDiv:
2449 return INPLACE_FLOOR_DIVIDE;
2450 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002451 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002452 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 return 0;
2454}
2455
2456static int
2457compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2458{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002459 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2461
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002462 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002463 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 /* XXX AugStore isn't used anywhere! */
2465
2466 /* First check for assignment to __debug__. Param? */
2467 if ((ctx == Store || ctx == AugStore || ctx == Del)
2468 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2469 return compiler_error(c, "can not assign to __debug__");
2470 }
2471
Guido van Rossumd8faa362007-04-27 19:54:29 +00002472mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002473 if (!mangled)
2474 return 0;
2475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 op = 0;
2477 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002478 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 switch (scope) {
2480 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002481 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 optype = OP_DEREF;
2483 break;
2484 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002485 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 optype = OP_DEREF;
2487 break;
2488 case LOCAL:
2489 if (c->u->u_ste->ste_type == FunctionBlock)
2490 optype = OP_FAST;
2491 break;
2492 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002493 if (c->u->u_ste->ste_type == FunctionBlock &&
2494 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 optype = OP_GLOBAL;
2496 break;
2497 case GLOBAL_EXPLICIT:
2498 optype = OP_GLOBAL;
2499 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002500 default:
2501 /* scope can be 0 */
2502 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002503 }
2504
2505 /* XXX Leave assert here, but handle __doc__ and the like better */
2506 assert(scope || PyString_AS_STRING(name)[0] == '_');
2507
2508 switch (optype) {
2509 case OP_DEREF:
2510 switch (ctx) {
2511 case Load: op = LOAD_DEREF; break;
2512 case Store: op = STORE_DEREF; break;
2513 case AugLoad:
2514 case AugStore:
2515 break;
2516 case Del:
2517 PyErr_Format(PyExc_SyntaxError,
2518 "can not delete variable '%s' referenced "
2519 "in nested scope",
2520 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002521 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002524 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002525 PyErr_SetString(PyExc_SystemError,
2526 "param invalid for deref variable");
2527 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 }
2529 break;
2530 case OP_FAST:
2531 switch (ctx) {
2532 case Load: op = LOAD_FAST; break;
2533 case Store: op = STORE_FAST; break;
2534 case Del: op = DELETE_FAST; break;
2535 case AugLoad:
2536 case AugStore:
2537 break;
2538 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002539 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002540 PyErr_SetString(PyExc_SystemError,
2541 "param invalid for local variable");
2542 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002544 ADDOP_O(c, op, mangled, varnames);
2545 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 return 1;
2547 case OP_GLOBAL:
2548 switch (ctx) {
2549 case Load: op = LOAD_GLOBAL; break;
2550 case Store: op = STORE_GLOBAL; break;
2551 case Del: op = DELETE_GLOBAL; break;
2552 case AugLoad:
2553 case AugStore:
2554 break;
2555 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002556 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002557 PyErr_SetString(PyExc_SystemError,
2558 "param invalid for global variable");
2559 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 }
2561 break;
2562 case OP_NAME:
2563 switch (ctx) {
2564 case Load: op = LOAD_NAME; break;
2565 case Store: op = STORE_NAME; break;
2566 case Del: op = DELETE_NAME; break;
2567 case AugLoad:
2568 case AugStore:
2569 break;
2570 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002571 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002572 PyErr_SetString(PyExc_SystemError,
2573 "param invalid for name variable");
2574 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575 }
2576 break;
2577 }
2578
2579 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002580 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002581 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002582 if (arg < 0)
2583 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002584 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002585}
2586
2587static int
2588compiler_boolop(struct compiler *c, expr_ty e)
2589{
2590 basicblock *end;
2591 int jumpi, i, n;
2592 asdl_seq *s;
2593
2594 assert(e->kind == BoolOp_kind);
2595 if (e->v.BoolOp.op == And)
2596 jumpi = JUMP_IF_FALSE;
2597 else
2598 jumpi = JUMP_IF_TRUE;
2599 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002600 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 return 0;
2602 s = e->v.BoolOp.values;
2603 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002604 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002606 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 ADDOP_JREL(c, jumpi, end);
2608 ADDOP(c, POP_TOP)
2609 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002610 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 compiler_use_next_block(c, end);
2612 return 1;
2613}
2614
2615static int
2616compiler_list(struct compiler *c, expr_ty e)
2617{
2618 int n = asdl_seq_LEN(e->v.List.elts);
2619 if (e->v.List.ctx == Store) {
2620 ADDOP_I(c, UNPACK_SEQUENCE, n);
2621 }
2622 VISIT_SEQ(c, expr, e->v.List.elts);
2623 if (e->v.List.ctx == Load) {
2624 ADDOP_I(c, BUILD_LIST, n);
2625 }
2626 return 1;
2627}
2628
2629static int
2630compiler_tuple(struct compiler *c, expr_ty e)
2631{
2632 int n = asdl_seq_LEN(e->v.Tuple.elts);
2633 if (e->v.Tuple.ctx == Store) {
2634 ADDOP_I(c, UNPACK_SEQUENCE, n);
2635 }
2636 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2637 if (e->v.Tuple.ctx == Load) {
2638 ADDOP_I(c, BUILD_TUPLE, n);
2639 }
2640 return 1;
2641}
2642
2643static int
2644compiler_compare(struct compiler *c, expr_ty e)
2645{
2646 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002647 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648
2649 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2650 VISIT(c, expr, e->v.Compare.left);
2651 n = asdl_seq_LEN(e->v.Compare.ops);
2652 assert(n > 0);
2653 if (n > 1) {
2654 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002655 if (cleanup == NULL)
2656 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002657 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002658 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002659 }
2660 for (i = 1; i < n; i++) {
2661 ADDOP(c, DUP_TOP);
2662 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002664 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002665 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2667 NEXT_BLOCK(c);
2668 ADDOP(c, POP_TOP);
2669 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002670 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002671 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002673 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002675 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676 if (n > 1) {
2677 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002678 if (end == NULL)
2679 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002680 ADDOP_JREL(c, JUMP_FORWARD, end);
2681 compiler_use_next_block(c, cleanup);
2682 ADDOP(c, ROT_TWO);
2683 ADDOP(c, POP_TOP);
2684 compiler_use_next_block(c, end);
2685 }
2686 return 1;
2687}
2688
2689static int
2690compiler_call(struct compiler *c, expr_ty e)
2691{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002693 return compiler_call_helper(c, 0,
2694 e->v.Call.args,
2695 e->v.Call.keywords,
2696 e->v.Call.starargs,
2697 e->v.Call.kwargs);
2698}
2699
2700/* shared code between compiler_call and compiler_class */
2701static int
2702compiler_call_helper(struct compiler *c,
2703 int n, /* Args already pushed */
2704 asdl_seq *args,
2705 asdl_seq *keywords,
2706 expr_ty starargs,
2707 expr_ty kwargs)
2708{
2709 int code = 0;
2710
2711 n += asdl_seq_LEN(args);
2712 VISIT_SEQ(c, expr, args);
2713 if (keywords) {
2714 VISIT_SEQ(c, keyword, keywords);
2715 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002717 if (starargs) {
2718 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 code |= 1;
2720 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002721 if (kwargs) {
2722 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002723 code |= 2;
2724 }
2725 switch (code) {
2726 case 0:
2727 ADDOP_I(c, CALL_FUNCTION, n);
2728 break;
2729 case 1:
2730 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2731 break;
2732 case 2:
2733 ADDOP_I(c, CALL_FUNCTION_KW, n);
2734 break;
2735 case 3:
2736 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2737 break;
2738 }
2739 return 1;
2740}
2741
Nick Coghlan650f0d02007-04-15 12:05:43 +00002742
2743/* List and set comprehensions and generator expressions work by creating a
2744 nested function to perform the actual iteration. This means that the
2745 iteration variables don't leak into the current scope.
2746 The defined function is called immediately following its definition, with the
2747 result of that call being the result of the expression.
2748 The LC/SC version returns the populated container, while the GE version is
2749 flagged in symtable.c as a generator, so it returns the generator object
2750 when the function is called.
2751 This code *knows* that the loop cannot contain break, continue, or return,
2752 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2753
2754 Possible cleanups:
2755 - iterate over the generator sequence instead of using recursion
2756*/
2757
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002759compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2760 asdl_seq *generators, int gen_index,
2761 expr_ty elt, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762{
2763 /* generate code for the iterator, then each of the ifs,
2764 and then write to the element */
2765
Nick Coghlan650f0d02007-04-15 12:05:43 +00002766 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002768 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769
2770 start = compiler_new_block(c);
2771 skip = compiler_new_block(c);
2772 if_cleanup = compiler_new_block(c);
2773 anchor = compiler_new_block(c);
2774
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002775 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002776 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002777 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778
Nick Coghlan650f0d02007-04-15 12:05:43 +00002779 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002781 if (gen_index == 0) {
2782 /* Receive outermost iter as an implicit argument */
2783 c->u->u_argcount = 1;
2784 ADDOP_I(c, LOAD_FAST, 0);
2785 }
2786 else {
2787 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002788 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 ADDOP(c, GET_ITER);
2790 }
2791 compiler_use_next_block(c, start);
2792 ADDOP_JREL(c, FOR_ITER, anchor);
2793 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002794 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002796 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002797 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002799 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 VISIT(c, expr, e);
2801 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2802 NEXT_BLOCK(c);
2803 ADDOP(c, POP_TOP);
2804 }
2805
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002806 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002807 if (!compiler_comprehension_generator(c, tmpname,
2808 generators, gen_index,
2809 elt, type))
2810 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811
Nick Coghlan650f0d02007-04-15 12:05:43 +00002812 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002813 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002814 /* comprehension specific code */
2815 switch (type) {
2816 case COMP_GENEXP:
2817 VISIT(c, expr, elt);
2818 ADDOP(c, YIELD_VALUE);
2819 ADDOP(c, POP_TOP);
2820 break;
2821 case COMP_LISTCOMP:
2822 if (!compiler_nameop(c, tmpname, Load))
2823 return 0;
2824 VISIT(c, expr, elt);
2825 ADDOP(c, LIST_APPEND);
2826 break;
2827 case COMP_SETCOMP:
2828 if (!compiler_nameop(c, tmpname, Load))
2829 return 0;
2830 VISIT(c, expr, elt);
2831 ADDOP(c, SET_ADD);
2832 break;
2833 default:
2834 return 0;
2835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002836
2837 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002838 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 for (i = 0; i < n; i++) {
2840 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002841 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 ADDOP(c, POP_TOP);
2845 }
2846 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2847 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848
2849 return 1;
2850}
2851
2852static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002853compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2854 asdl_seq *generators, expr_ty elt)
2855{
2856 PyCodeObject *co = NULL;
2857 identifier tmp = NULL;
2858 expr_ty outermost_iter;
2859
2860 outermost_iter = ((comprehension_ty)
2861 asdl_seq_GET(generators, 0))->iter;
2862
2863 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2864 goto error;
2865
2866 if (type != COMP_GENEXP) {
2867 tmp = compiler_new_tmpname(c);
2868 if (!tmp)
2869 goto error_in_scope;
2870
2871 ADDOP_I(c, (type == COMP_LISTCOMP ?
2872 BUILD_LIST : BUILD_SET), 0);
2873 ADDOP(c, DUP_TOP);
2874 if (!compiler_nameop(c, tmp, Store))
2875 goto error_in_scope;
2876 }
2877
2878 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt, type))
2879 goto error_in_scope;
2880
2881 if (type != COMP_GENEXP) {
2882 ADDOP(c, RETURN_VALUE);
2883 }
2884
2885 co = assemble(c, 1);
2886 compiler_exit_scope(c);
2887 if (co == NULL)
2888 goto error;
2889
2890 if (!compiler_make_closure(c, co, 0))
2891 goto error;
2892 Py_DECREF(co);
2893 Py_XDECREF(tmp);
2894
2895 VISIT(c, expr, outermost_iter);
2896 ADDOP(c, GET_ITER);
2897 ADDOP_I(c, CALL_FUNCTION, 1);
2898 return 1;
2899error_in_scope:
2900 compiler_exit_scope(c);
2901error:
2902 Py_XDECREF(co);
2903 Py_XDECREF(tmp);
2904 return 0;
2905}
2906
2907static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908compiler_genexp(struct compiler *c, expr_ty e)
2909{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002910 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002911 if (!name) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002912 name = PyString_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002913 if (!name)
2914 return 0;
2915 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002916 assert(e->kind == GeneratorExp_kind);
2917 return compiler_comprehension(c, e, COMP_GENEXP, name,
2918 e->v.GeneratorExp.generators,
2919 e->v.GeneratorExp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002920}
2921
2922static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002923compiler_listcomp(struct compiler *c, expr_ty e)
2924{
2925 static identifier name;
2926 if (!name) {
2927 name = PyString_FromString("<listcomp>");
2928 if (!name)
2929 return 0;
2930 }
2931 assert(e->kind == ListComp_kind);
2932 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2933 e->v.ListComp.generators,
2934 e->v.ListComp.elt);
2935}
2936
2937static int
2938compiler_setcomp(struct compiler *c, expr_ty e)
2939{
2940 static identifier name;
2941 if (!name) {
2942 name = PyString_FromString("<setcomp>");
2943 if (!name)
2944 return 0;
2945 }
2946 assert(e->kind == SetComp_kind);
2947 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2948 e->v.SetComp.generators,
2949 e->v.SetComp.elt);
2950}
2951
2952
2953static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954compiler_visit_keyword(struct compiler *c, keyword_ty k)
2955{
2956 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2957 VISIT(c, expr, k->value);
2958 return 1;
2959}
2960
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002961/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 whether they are true or false.
2963
2964 Return values: 1 for true, 0 for false, -1 for non-constant.
2965 */
2966
2967static int
2968expr_constant(expr_ty e)
2969{
2970 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002971 case Ellipsis_kind:
2972 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973 case Num_kind:
2974 return PyObject_IsTrue(e->v.Num.n);
2975 case Str_kind:
2976 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002977 case Name_kind:
2978 /* __debug__ is not assignable, so we can optimize
2979 * it away in if and while statements */
2980 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Guido van Rossumd8faa362007-04-27 19:54:29 +00002981 "__debug__") == 0)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002982 return ! Py_OptimizeFlag;
2983 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 default:
2985 return -1;
2986 }
2987}
2988
Guido van Rossumc2e20742006-02-27 22:32:47 +00002989/*
2990 Implements the with statement from PEP 343.
2991
2992 The semantics outlined in that PEP are as follows:
2993
2994 with EXPR as VAR:
2995 BLOCK
2996
2997 It is implemented roughly as:
2998
Thomas Wouters477c8d52006-05-27 19:21:47 +00002999 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003000 exit = context.__exit__ # not calling it
3001 value = context.__enter__()
3002 try:
3003 VAR = value # if VAR present in the syntax
3004 BLOCK
3005 finally:
3006 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003007 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003008 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003009 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003010 exit(*exc)
3011 */
3012static int
3013compiler_with(struct compiler *c, stmt_ty s)
3014{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003015 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003016 basicblock *block, *finally;
3017 identifier tmpexit, tmpvalue = NULL;
3018
3019 assert(s->kind == With_kind);
3020
Guido van Rossumc2e20742006-02-27 22:32:47 +00003021 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003022 enter_attr = PyString_InternFromString("__enter__");
3023 if (!enter_attr)
3024 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003025 }
3026 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003027 exit_attr = PyString_InternFromString("__exit__");
3028 if (!exit_attr)
3029 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003030 }
3031
3032 block = compiler_new_block(c);
3033 finally = compiler_new_block(c);
3034 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003035 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003036
3037 /* Create a temporary variable to hold context.__exit__ */
3038 tmpexit = compiler_new_tmpname(c);
3039 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003040 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003041 PyArena_AddPyObject(c->c_arena, tmpexit);
3042
3043 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003044 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003045 We need to do this rather than preserving it on the stack
3046 because SETUP_FINALLY remembers the stack level.
3047 We need to do the assignment *inside* the try/finally
3048 so that context.__exit__() is called when the assignment
3049 fails. But we need to call context.__enter__() *before*
3050 the try/finally so that if it fails we won't call
3051 context.__exit__().
3052 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003053 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003054 if (tmpvalue == NULL)
3055 return 0;
3056 PyArena_AddPyObject(c->c_arena, tmpvalue);
3057 }
3058
Thomas Wouters477c8d52006-05-27 19:21:47 +00003059 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003060 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003061
3062 /* Squirrel away context.__exit__ */
3063 ADDOP(c, DUP_TOP);
3064 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3065 if (!compiler_nameop(c, tmpexit, Store))
3066 return 0;
3067
3068 /* Call context.__enter__() */
3069 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3070 ADDOP_I(c, CALL_FUNCTION, 0);
3071
3072 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003073 /* Store it in tmpvalue */
3074 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003075 return 0;
3076 }
3077 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003078 /* Discard result from context.__enter__() */
3079 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003080 }
3081
3082 /* Start the try block */
3083 ADDOP_JREL(c, SETUP_FINALLY, finally);
3084
3085 compiler_use_next_block(c, block);
3086 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003087 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003088 }
3089
3090 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003091 /* Bind saved result of context.__enter__() to VAR */
3092 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003093 !compiler_nameop(c, tmpvalue, Del))
3094 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003095 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003096 }
3097
3098 /* BLOCK code */
3099 VISIT_SEQ(c, stmt, s->v.With.body);
3100
3101 /* End of try block; start the finally block */
3102 ADDOP(c, POP_BLOCK);
3103 compiler_pop_fblock(c, FINALLY_TRY, block);
3104
3105 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3106 compiler_use_next_block(c, finally);
3107 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003108 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003109
3110 /* Finally block starts; push tmpexit and issue our magic opcode. */
3111 if (!compiler_nameop(c, tmpexit, Load) ||
3112 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003113 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003114 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003115
3116 /* Finally block ends. */
3117 ADDOP(c, END_FINALLY);
3118 compiler_pop_fblock(c, FINALLY_END, finally);
3119 return 1;
3120}
3121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122static int
3123compiler_visit_expr(struct compiler *c, expr_ty e)
3124{
3125 int i, n;
3126
Thomas Wouters89f507f2006-12-13 04:49:30 +00003127 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003128 set a new line number for the next instruction.
3129 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130 if (e->lineno > c->u->u_lineno) {
3131 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003132 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 }
3134 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003135 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003137 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 VISIT(c, expr, e->v.BinOp.left);
3139 VISIT(c, expr, e->v.BinOp.right);
3140 ADDOP(c, binop(c, e->v.BinOp.op));
3141 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003142 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 VISIT(c, expr, e->v.UnaryOp.operand);
3144 ADDOP(c, unaryop(e->v.UnaryOp.op));
3145 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003146 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003148 case IfExp_kind:
3149 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003150 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 /* XXX get rid of arg? */
3152 ADDOP_I(c, BUILD_MAP, 0);
3153 n = asdl_seq_LEN(e->v.Dict.values);
3154 /* We must arrange things just right for STORE_SUBSCR.
3155 It wants the stack to look like (value) (dict) (key) */
3156 for (i = 0; i < n; i++) {
3157 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003158 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003159 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003161 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003162 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003163 ADDOP(c, STORE_SUBSCR);
3164 }
3165 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003166 case Set_kind:
3167 n = asdl_seq_LEN(e->v.Set.elts);
3168 VISIT_SEQ(c, expr, e->v.Set.elts);
3169 ADDOP_I(c, BUILD_SET, n);
3170 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003171 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003173 case ListComp_kind:
3174 return compiler_listcomp(c, e);
3175 case SetComp_kind:
3176 return compiler_setcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003177 case Yield_kind:
3178 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003179 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003180 if (e->v.Yield.value) {
3181 VISIT(c, expr, e->v.Yield.value);
3182 }
3183 else {
3184 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3185 }
3186 ADDOP(c, YIELD_VALUE);
3187 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003188 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003190 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003192 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3194 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003195 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3197 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003198 case Bytes_kind:
3199 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3200 ADDOP(c, MAKE_BYTES);
3201 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003202 case Ellipsis_kind:
3203 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3204 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003206 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003207 if (e->v.Attribute.ctx != AugStore)
3208 VISIT(c, expr, e->v.Attribute.value);
3209 switch (e->v.Attribute.ctx) {
3210 case AugLoad:
3211 ADDOP(c, DUP_TOP);
3212 /* Fall through to load */
3213 case Load:
3214 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3215 break;
3216 case AugStore:
3217 ADDOP(c, ROT_TWO);
3218 /* Fall through to save */
3219 case Store:
3220 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3221 break;
3222 case Del:
3223 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3224 break;
3225 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003226 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003227 PyErr_SetString(PyExc_SystemError,
3228 "param invalid in attribute expression");
3229 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 }
3231 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003232 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 switch (e->v.Subscript.ctx) {
3234 case AugLoad:
3235 VISIT(c, expr, e->v.Subscript.value);
3236 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3237 break;
3238 case Load:
3239 VISIT(c, expr, e->v.Subscript.value);
3240 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3241 break;
3242 case AugStore:
3243 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3244 break;
3245 case Store:
3246 VISIT(c, expr, e->v.Subscript.value);
3247 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3248 break;
3249 case Del:
3250 VISIT(c, expr, e->v.Subscript.value);
3251 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3252 break;
3253 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003254 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003255 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003256 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003257 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 }
3259 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003260 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3262 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003263 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003265 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 return compiler_tuple(c, e);
3267 }
3268 return 1;
3269}
3270
3271static int
3272compiler_augassign(struct compiler *c, stmt_ty s)
3273{
3274 expr_ty e = s->v.AugAssign.target;
3275 expr_ty auge;
3276
3277 assert(s->kind == AugAssign_kind);
3278
3279 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003280 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003282 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003283 if (auge == NULL)
3284 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 VISIT(c, expr, auge);
3286 VISIT(c, expr, s->v.AugAssign.value);
3287 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3288 auge->v.Attribute.ctx = AugStore;
3289 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 break;
3291 case Subscript_kind:
3292 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003293 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003294 if (auge == NULL)
3295 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296 VISIT(c, expr, auge);
3297 VISIT(c, expr, s->v.AugAssign.value);
3298 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003299 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003301 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003303 if (!compiler_nameop(c, e->v.Name.id, Load))
3304 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 VISIT(c, expr, s->v.AugAssign.value);
3306 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3307 return compiler_nameop(c, e->v.Name.id, Store);
3308 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003309 PyErr_Format(PyExc_SystemError,
3310 "invalid node type (%d) for augmented assignment",
3311 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003312 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 }
3314 return 1;
3315}
3316
3317static int
3318compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3319{
3320 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003321 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3322 PyErr_SetString(PyExc_SystemError,
3323 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003325 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326 f = &c->u->u_fblock[c->u->u_nfblocks++];
3327 f->fb_type = t;
3328 f->fb_block = b;
3329 return 1;
3330}
3331
3332static void
3333compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3334{
3335 struct compiler_unit *u = c->u;
3336 assert(u->u_nfblocks > 0);
3337 u->u_nfblocks--;
3338 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3339 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3340}
3341
Thomas Wouters89f507f2006-12-13 04:49:30 +00003342static int
3343compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003344 int i;
3345 struct compiler_unit *u = c->u;
3346 for (i = 0; i < u->u_nfblocks; ++i) {
3347 if (u->u_fblock[i].fb_type == LOOP)
3348 return 1;
3349 }
3350 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003351}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352/* Raises a SyntaxError and returns 0.
3353 If something goes wrong, a different exception may be raised.
3354*/
3355
3356static int
3357compiler_error(struct compiler *c, const char *errstr)
3358{
3359 PyObject *loc;
3360 PyObject *u = NULL, *v = NULL;
3361
3362 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3363 if (!loc) {
3364 Py_INCREF(Py_None);
3365 loc = Py_None;
3366 }
3367 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3368 Py_None, loc);
3369 if (!u)
3370 goto exit;
3371 v = Py_BuildValue("(zO)", errstr, u);
3372 if (!v)
3373 goto exit;
3374 PyErr_SetObject(PyExc_SyntaxError, v);
3375 exit:
3376 Py_DECREF(loc);
3377 Py_XDECREF(u);
3378 Py_XDECREF(v);
3379 return 0;
3380}
3381
3382static int
3383compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003384 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003385{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003386 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003388 /* XXX this code is duplicated */
3389 switch (ctx) {
3390 case AugLoad: /* fall through to Load */
3391 case Load: op = BINARY_SUBSCR; break;
3392 case AugStore:/* fall through to Store */
3393 case Store: op = STORE_SUBSCR; break;
3394 case Del: op = DELETE_SUBSCR; break;
3395 case Param:
3396 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003397 "invalid %s kind %d in subscript\n",
3398 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003399 return 0;
3400 }
3401 if (ctx == AugLoad) {
3402 ADDOP_I(c, DUP_TOPX, 2);
3403 }
3404 else if (ctx == AugStore) {
3405 ADDOP(c, ROT_THREE);
3406 }
3407 ADDOP(c, op);
3408 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003409}
3410
3411static int
3412compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3413{
3414 int n = 2;
3415 assert(s->kind == Slice_kind);
3416
3417 /* only handles the cases where BUILD_SLICE is emitted */
3418 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003419 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
3421 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003422 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003423 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003426 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427 }
3428 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003429 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 }
3431
3432 if (s->v.Slice.step) {
3433 n++;
3434 VISIT(c, expr, s->v.Slice.step);
3435 }
3436 ADDOP_I(c, BUILD_SLICE, n);
3437 return 1;
3438}
3439
3440static int
3441compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3442{
3443 int op = 0, slice_offset = 0, stack_count = 0;
3444
3445 assert(s->v.Slice.step == NULL);
3446 if (s->v.Slice.lower) {
3447 slice_offset++;
3448 stack_count++;
3449 if (ctx != AugStore)
3450 VISIT(c, expr, s->v.Slice.lower);
3451 }
3452 if (s->v.Slice.upper) {
3453 slice_offset += 2;
3454 stack_count++;
3455 if (ctx != AugStore)
3456 VISIT(c, expr, s->v.Slice.upper);
3457 }
3458
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003459 if (ctx == AugLoad) {
3460 switch (stack_count) {
3461 case 0: ADDOP(c, DUP_TOP); break;
3462 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3463 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3464 }
3465 }
3466 else if (ctx == AugStore) {
3467 switch (stack_count) {
3468 case 0: ADDOP(c, ROT_TWO); break;
3469 case 1: ADDOP(c, ROT_THREE); break;
3470 case 2: ADDOP(c, ROT_FOUR); break;
3471 }
3472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473
3474 switch (ctx) {
3475 case AugLoad: /* fall through to Load */
3476 case Load: op = SLICE; break;
3477 case AugStore:/* fall through to Store */
3478 case Store: op = STORE_SLICE; break;
3479 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003480 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003481 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003482 PyErr_SetString(PyExc_SystemError,
3483 "param invalid in simple slice");
3484 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 }
3486
3487 ADDOP(c, op + slice_offset);
3488 return 1;
3489}
3490
3491static int
3492compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3493 expr_context_ty ctx)
3494{
3495 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496 case Slice_kind:
3497 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 case Index_kind:
3499 VISIT(c, expr, s->v.Index.value);
3500 break;
3501 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003502 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003503 PyErr_SetString(PyExc_SystemError,
3504 "extended slice invalid in nested slice");
3505 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 }
3507 return 1;
3508}
3509
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510static int
3511compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3512{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003513 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003514 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003515 case Index_kind:
3516 kindname = "index";
3517 if (ctx != AugStore) {
3518 VISIT(c, expr, s->v.Index.value);
3519 }
3520 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003522 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 if (!s->v.Slice.step)
3524 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003525 if (ctx != AugStore) {
3526 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527 return 0;
3528 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003529 break;
3530 case ExtSlice_kind:
3531 kindname = "extended slice";
3532 if (ctx != AugStore) {
3533 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3534 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003535 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003536 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003537 if (!compiler_visit_nested_slice(c, sub, ctx))
3538 return 0;
3539 }
3540 ADDOP_I(c, BUILD_TUPLE, n);
3541 }
3542 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003543 default:
3544 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003545 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003546 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003548 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549}
3550
Thomas Wouters89f507f2006-12-13 04:49:30 +00003551/* End of the compiler section, beginning of the assembler section */
3552
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553/* do depth-first search of basic block graph, starting with block.
3554 post records the block indices in post-order.
3555
3556 XXX must handle implicit jumps from one block to next
3557*/
3558
Thomas Wouters89f507f2006-12-13 04:49:30 +00003559struct assembler {
3560 PyObject *a_bytecode; /* string containing bytecode */
3561 int a_offset; /* offset into bytecode */
3562 int a_nblocks; /* number of reachable blocks */
3563 basicblock **a_postorder; /* list of blocks in dfs postorder */
3564 PyObject *a_lnotab; /* string containing lnotab */
3565 int a_lnotab_off; /* offset into lnotab */
3566 int a_lineno; /* last lineno of emitted instruction */
3567 int a_lineno_off; /* bytecode offset of last lineno */
3568};
3569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570static void
3571dfs(struct compiler *c, basicblock *b, struct assembler *a)
3572{
3573 int i;
3574 struct instr *instr = NULL;
3575
3576 if (b->b_seen)
3577 return;
3578 b->b_seen = 1;
3579 if (b->b_next != NULL)
3580 dfs(c, b->b_next, a);
3581 for (i = 0; i < b->b_iused; i++) {
3582 instr = &b->b_instr[i];
3583 if (instr->i_jrel || instr->i_jabs)
3584 dfs(c, instr->i_target, a);
3585 }
3586 a->a_postorder[a->a_nblocks++] = b;
3587}
3588
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003589static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3591{
3592 int i;
3593 struct instr *instr;
3594 if (b->b_seen || b->b_startdepth >= depth)
3595 return maxdepth;
3596 b->b_seen = 1;
3597 b->b_startdepth = depth;
3598 for (i = 0; i < b->b_iused; i++) {
3599 instr = &b->b_instr[i];
3600 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3601 if (depth > maxdepth)
3602 maxdepth = depth;
3603 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3604 if (instr->i_jrel || instr->i_jabs) {
3605 maxdepth = stackdepth_walk(c, instr->i_target,
3606 depth, maxdepth);
3607 if (instr->i_opcode == JUMP_ABSOLUTE ||
3608 instr->i_opcode == JUMP_FORWARD) {
3609 goto out; /* remaining code is dead */
3610 }
3611 }
3612 }
3613 if (b->b_next)
3614 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3615out:
3616 b->b_seen = 0;
3617 return maxdepth;
3618}
3619
3620/* Find the flow path that needs the largest stack. We assume that
3621 * cycles in the flow graph have no net effect on the stack depth.
3622 */
3623static int
3624stackdepth(struct compiler *c)
3625{
3626 basicblock *b, *entryblock;
3627 entryblock = NULL;
3628 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3629 b->b_seen = 0;
3630 b->b_startdepth = INT_MIN;
3631 entryblock = b;
3632 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003633 if (!entryblock)
3634 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 return stackdepth_walk(c, entryblock, 0, 0);
3636}
3637
3638static int
3639assemble_init(struct assembler *a, int nblocks, int firstlineno)
3640{
3641 memset(a, 0, sizeof(struct assembler));
3642 a->a_lineno = firstlineno;
3643 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3644 if (!a->a_bytecode)
3645 return 0;
3646 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3647 if (!a->a_lnotab)
3648 return 0;
3649 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003650 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003651 if (!a->a_postorder) {
3652 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003654 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 return 1;
3656}
3657
3658static void
3659assemble_free(struct assembler *a)
3660{
3661 Py_XDECREF(a->a_bytecode);
3662 Py_XDECREF(a->a_lnotab);
3663 if (a->a_postorder)
3664 PyObject_Free(a->a_postorder);
3665}
3666
3667/* Return the size of a basic block in bytes. */
3668
3669static int
3670instrsize(struct instr *instr)
3671{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003672 if (!instr->i_hasarg)
3673 return 1;
3674 if (instr->i_oparg > 0xffff)
3675 return 6;
3676 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677}
3678
3679static int
3680blocksize(basicblock *b)
3681{
3682 int i;
3683 int size = 0;
3684
3685 for (i = 0; i < b->b_iused; i++)
3686 size += instrsize(&b->b_instr[i]);
3687 return size;
3688}
3689
3690/* All about a_lnotab.
3691
3692c_lnotab is an array of unsigned bytes disguised as a Python string.
3693It is used to map bytecode offsets to source code line #s (when needed
3694for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003695
Tim Peters2a7f3842001-06-09 09:26:21 +00003696The array is conceptually a list of
3697 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003698pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003699
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003700 byte code offset source code line number
3701 0 1
3702 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003703 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003704 350 307
3705 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003706
3707The first trick is that these numbers aren't stored, only the increments
3708from one row to the next (this doesn't really work, but it's a start):
3709
3710 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3711
3712The second trick is that an unsigned byte can't hold negative values, or
3713values larger than 255, so (a) there's a deep assumption that byte code
3714offsets and their corresponding line #s both increase monotonically, and (b)
3715if at least one column jumps by more than 255 from one row to the next, more
3716than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003717from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003718part. A user of c_lnotab desiring to find the source line number
3719corresponding to a bytecode address A should do something like this
3720
3721 lineno = addr = 0
3722 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003723 addr += addr_incr
3724 if addr > A:
3725 return lineno
3726 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003727
3728In order for this to work, when the addr field increments by more than 255,
3729the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003730increment is < 256. So, in the example above, assemble_lnotab (it used
3731to be called com_set_lineno) should not (as was actually done until 2.2)
3732expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003733 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003734*/
3735
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003736static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003737assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003738{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 int d_bytecode, d_lineno;
3740 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003741 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003742
3743 d_bytecode = a->a_offset - a->a_lineno_off;
3744 d_lineno = i->i_lineno - a->a_lineno;
3745
3746 assert(d_bytecode >= 0);
3747 assert(d_lineno >= 0);
3748
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003749 /* XXX(nnorwitz): is there a better way to handle this?
3750 for loops are special, we want to be able to trace them
3751 each time around, so we need to set an extra line number. */
3752 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003753 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003754
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003756 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 nbytes = a->a_lnotab_off + 2 * ncodes;
3758 len = PyString_GET_SIZE(a->a_lnotab);
3759 if (nbytes >= len) {
3760 if (len * 2 < nbytes)
3761 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003762 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 len *= 2;
3764 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3765 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003766 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003767 lnotab = (unsigned char *)
3768 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003769 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 *lnotab++ = 255;
3771 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003772 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 d_bytecode -= ncodes * 255;
3774 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003775 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 assert(d_bytecode <= 255);
3777 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003778 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 nbytes = a->a_lnotab_off + 2 * ncodes;
3780 len = PyString_GET_SIZE(a->a_lnotab);
3781 if (nbytes >= len) {
3782 if (len * 2 < nbytes)
3783 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003784 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 len *= 2;
3786 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3787 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003788 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003789 lnotab = (unsigned char *)
3790 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003792 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003793 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003794 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003796 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 d_lineno -= ncodes * 255;
3799 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003800 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003801
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 len = PyString_GET_SIZE(a->a_lnotab);
3803 if (a->a_lnotab_off + 2 >= len) {
3804 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003805 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003806 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003807 lnotab = (unsigned char *)
3808 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 a->a_lnotab_off += 2;
3811 if (d_bytecode) {
3812 *lnotab++ = d_bytecode;
3813 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003814 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003815 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 *lnotab++ = 0;
3817 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 a->a_lineno = i->i_lineno;
3820 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003821 return 1;
3822}
3823
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824/* assemble_emit()
3825 Extend the bytecode with a new instruction.
3826 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003827*/
3828
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003829static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003831{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003832 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003833 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834 char *code;
3835
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003836 size = instrsize(i);
3837 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003839 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003840 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003842 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 if (a->a_offset + size >= len) {
3844 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003845 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3848 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003849 if (size == 6) {
3850 assert(i->i_hasarg);
3851 *code++ = (char)EXTENDED_ARG;
3852 *code++ = ext & 0xff;
3853 *code++ = ext >> 8;
3854 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003857 if (i->i_hasarg) {
3858 assert(size == 3 || size == 6);
3859 *code++ = arg & 0xff;
3860 *code++ = arg >> 8;
3861 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003863}
3864
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003865static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003867{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003869 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003870 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003871
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 /* Compute the size of each block and fixup jump args.
3873 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003874start:
3875 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003877 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 bsize = blocksize(b);
3879 b->b_offset = totsize;
3880 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003881 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003882 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003883 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3884 bsize = b->b_offset;
3885 for (i = 0; i < b->b_iused; i++) {
3886 struct instr *instr = &b->b_instr[i];
3887 /* Relative jumps are computed relative to
3888 the instruction pointer after fetching
3889 the jump instruction.
3890 */
3891 bsize += instrsize(instr);
3892 if (instr->i_jabs)
3893 instr->i_oparg = instr->i_target->b_offset;
3894 else if (instr->i_jrel) {
3895 int delta = instr->i_target->b_offset - bsize;
3896 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003897 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003898 else
3899 continue;
3900 if (instr->i_oparg > 0xffff)
3901 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003902 }
3903 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003904
3905 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003906 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003907 with a better solution.
3908
3909 In the meantime, should the goto be dropped in favor
3910 of a loop?
3911
3912 The issue is that in the first loop blocksize() is called
3913 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003914 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003915 i_oparg is calculated in the second loop above.
3916
3917 So we loop until we stop seeing new EXTENDED_ARGs.
3918 The only EXTENDED_ARGs that could be popping up are
3919 ones in jump instructions. So this should converge
3920 fairly quickly.
3921 */
3922 if (last_extended_arg_count != extended_arg_count) {
3923 last_extended_arg_count = extended_arg_count;
3924 goto start;
3925 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003926}
3927
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003928static PyObject *
3929dict_keys_inorder(PyObject *dict, int offset)
3930{
3931 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003932 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003933
3934 tuple = PyTuple_New(size);
3935 if (tuple == NULL)
3936 return NULL;
3937 while (PyDict_Next(dict, &pos, &k, &v)) {
3938 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003939 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003940 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003941 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003942 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003943 PyTuple_SET_ITEM(tuple, i - offset, k);
3944 }
3945 return tuple;
3946}
3947
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003948static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003949compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003950{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951 PySTEntryObject *ste = c->u->u_ste;
3952 int flags = 0, n;
3953 if (ste->ste_type != ModuleBlock)
3954 flags |= CO_NEWLOCALS;
3955 if (ste->ste_type == FunctionBlock) {
3956 if (!ste->ste_unoptimized)
3957 flags |= CO_OPTIMIZED;
3958 if (ste->ste_nested)
3959 flags |= CO_NESTED;
3960 if (ste->ste_generator)
3961 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003962 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 if (ste->ste_varargs)
3964 flags |= CO_VARARGS;
3965 if (ste->ste_varkeywords)
3966 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003967 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003969
3970 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003971 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003972
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 n = PyDict_Size(c->u->u_freevars);
3974 if (n < 0)
3975 return -1;
3976 if (n == 0) {
3977 n = PyDict_Size(c->u->u_cellvars);
3978 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003979 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 if (n == 0) {
3981 flags |= CO_NOFREE;
3982 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003983 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003984
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003985 return flags;
3986}
3987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988static PyCodeObject *
3989makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003990{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 PyObject *tmp;
3992 PyCodeObject *co = NULL;
3993 PyObject *consts = NULL;
3994 PyObject *names = NULL;
3995 PyObject *varnames = NULL;
3996 PyObject *filename = NULL;
3997 PyObject *name = NULL;
3998 PyObject *freevars = NULL;
3999 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004000 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004002
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 tmp = dict_keys_inorder(c->u->u_consts, 0);
4004 if (!tmp)
4005 goto error;
4006 consts = PySequence_List(tmp); /* optimize_code requires a list */
4007 Py_DECREF(tmp);
4008
4009 names = dict_keys_inorder(c->u->u_names, 0);
4010 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4011 if (!consts || !names || !varnames)
4012 goto error;
4013
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004014 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4015 if (!cellvars)
4016 goto error;
4017 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4018 if (!freevars)
4019 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 filename = PyString_FromString(c->c_filename);
4021 if (!filename)
4022 goto error;
4023
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004024 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004025 flags = compute_code_flags(c);
4026 if (flags < 0)
4027 goto error;
4028
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004029 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 if (!bytecode)
4031 goto error;
4032
4033 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4034 if (!tmp)
4035 goto error;
4036 Py_DECREF(consts);
4037 consts = tmp;
4038
Guido van Rossum4f72a782006-10-27 23:31:49 +00004039 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4040 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041 bytecode, consts, names, varnames,
4042 freevars, cellvars,
4043 filename, c->u->u_name,
4044 c->u->u_firstlineno,
4045 a->a_lnotab);
4046 error:
4047 Py_XDECREF(consts);
4048 Py_XDECREF(names);
4049 Py_XDECREF(varnames);
4050 Py_XDECREF(filename);
4051 Py_XDECREF(name);
4052 Py_XDECREF(freevars);
4053 Py_XDECREF(cellvars);
4054 Py_XDECREF(bytecode);
4055 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004056}
4057
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004058
4059/* For debugging purposes only */
4060#if 0
4061static void
4062dump_instr(const struct instr *i)
4063{
4064 const char *jrel = i->i_jrel ? "jrel " : "";
4065 const char *jabs = i->i_jabs ? "jabs " : "";
4066 char arg[128];
4067
4068 *arg = '\0';
4069 if (i->i_hasarg)
4070 sprintf(arg, "arg: %d ", i->i_oparg);
4071
4072 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4073 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4074}
4075
4076static void
4077dump_basicblock(const basicblock *b)
4078{
4079 const char *seen = b->b_seen ? "seen " : "";
4080 const char *b_return = b->b_return ? "return " : "";
4081 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4082 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4083 if (b->b_instr) {
4084 int i;
4085 for (i = 0; i < b->b_iused; i++) {
4086 fprintf(stderr, " [%02d] ", i);
4087 dump_instr(b->b_instr + i);
4088 }
4089 }
4090}
4091#endif
4092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093static PyCodeObject *
4094assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004095{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004096 basicblock *b, *entryblock;
4097 struct assembler a;
4098 int i, j, nblocks;
4099 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004101 /* Make sure every block that falls off the end returns None.
4102 XXX NEXT_BLOCK() isn't quite right, because if the last
4103 block ends with a jump or return b_next shouldn't set.
4104 */
4105 if (!c->u->u_curblock->b_return) {
4106 NEXT_BLOCK(c);
4107 if (addNone)
4108 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4109 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004110 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112 nblocks = 0;
4113 entryblock = NULL;
4114 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4115 nblocks++;
4116 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004117 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004118
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004119 /* Set firstlineno if it wasn't explicitly set. */
4120 if (!c->u->u_firstlineno) {
4121 if (entryblock && entryblock->b_instr)
4122 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4123 else
4124 c->u->u_firstlineno = 1;
4125 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4127 goto error;
4128 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004131 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 /* Emit code in reverse postorder from dfs. */
4134 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004135 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 for (j = 0; j < b->b_iused; j++)
4137 if (!assemble_emit(&a, &b->b_instr[j]))
4138 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004139 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4142 goto error;
4143 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4144 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146 co = makecode(c, &a);
4147 error:
4148 assemble_free(&a);
4149 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004150}