blob: 359de587c5971ea12b89e5ea28eb85dd75931116 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Jeremy Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Guido van Rossumd8faa362007-04-27 19:54:29 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Nick Coghlan650f0d02007-04-15 12:05:43 +000042#define COMP_GENEXP 0
43#define COMP_LISTCOMP 1
44#define COMP_SETCOMP 2
45
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000047 unsigned i_jabs : 1;
48 unsigned i_jrel : 1;
49 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000071 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000073 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000077 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078} basicblock;
79
80/* fblockinfo tracks the current frame block.
81
Jeremy Hyltone9357b22006-03-01 15:47:05 +000082A frame block is used to handle loops, try/except, and try/finally.
83It's called a frame block to distinguish it from a basic block in the
84compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085*/
86
87enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
88
89struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000090 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 basicblock *fb_block;
92};
93
94/* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
96*/
97struct compiler_unit {
98 PySTEntryObject *u_ste;
99
100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000102 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103 the argument for opcodes that refer to those collections.
104 */
105 PyObject *u_consts; /* all constants */
106 PyObject *u_names; /* all names */
107 PyObject *u_varnames; /* local variables */
108 PyObject *u_cellvars; /* cell variables */
109 PyObject *u_freevars; /* free variables */
110
111 PyObject *u_private; /* for private name mangling */
112
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000113 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000114 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 /* Pointer to the most recently allocated block. By following b_list
116 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
121 int u_nfblocks;
122 struct fblockinfo u_fblock[CO_MAXBLOCKS];
123
124 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000125 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000126 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 has been generated with current lineno */
128};
129
130/* This struct captures the global state of a compilation.
131
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132The u pointer points to the current compilation unit, while units
133for enclosing blocks are stored in c_stack. The u and c_stack are
134managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135*/
136
137struct compiler {
138 const char *c_filename;
139 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141 PyCompilerFlags *c_flags;
142
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000149 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150};
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152static int compiler_enter_scope(struct compiler *, identifier, void *, int);
153static void compiler_free(struct compiler *);
154static basicblock *compiler_new_block(struct compiler *);
155static int compiler_next_instr(struct compiler *, basicblock *);
156static int compiler_addop(struct compiler *, int);
157static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158static int compiler_addop_i(struct compiler *, int, int);
159static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static basicblock *compiler_use_new_block(struct compiler *);
161static int compiler_error(struct compiler *, const char *);
162static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
163
164static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
165static int compiler_visit_stmt(struct compiler *, stmt_ty);
166static int compiler_visit_keyword(struct compiler *, keyword_ty);
167static int compiler_visit_expr(struct compiler *, expr_ty);
168static int compiler_augassign(struct compiler *, stmt_ty);
169static int compiler_visit_slice(struct compiler *, slice_ty,
170 expr_context_ty);
171
172static int compiler_push_fblock(struct compiler *, enum fblocktype,
173 basicblock *);
174static void compiler_pop_fblock(struct compiler *, enum fblocktype,
175 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176/* Returns true if there is a loop on the fblock stack. */
177static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int inplace_binop(struct compiler *, operator_ty);
180static int expr_constant(expr_ty e);
181
Guido van Rossumc2e20742006-02-27 22:32:47 +0000182static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000183static int compiler_call_helper(struct compiler *c, int n,
184 asdl_seq *args,
185 asdl_seq *keywords,
186 expr_ty starargs,
187 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static PyCodeObject *assemble(struct compiler *, int addNone);
190static PyObject *__doc__;
191
192PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000194{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 /* Name mangling: __private becomes _classname__private.
196 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000197 const char *p, *name = PyString_AsString(ident);
198 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 size_t nlen, plen;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000200 if (privateobj == NULL || !PyString_Check(privateobj) ||
201 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000202 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 nlen = strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207 /* Don't mangle __id__ or names with dots.
208
209 The only time a name with a dot can occur is when
210 we are compiling an import statement that has a
211 package name.
212
213 TODO(jhylton): Decide whether we want to support
214 mangling of the module name, e.g. __M.X.
215 */
216 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
217 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000218 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 /* Strip leading underscores from class name */
222 while (*p == '_')
223 p++;
224 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000225 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000227 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228 plen = strlen(p);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000229 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
230 if (!ident)
231 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000233 buffer = PyString_AS_STRING(ident);
234 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 strncpy(buffer+1, p, plen);
236 strcpy(buffer+1+plen, name);
237 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000238}
239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240static int
241compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000242{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 c->c_stack = PyList_New(0);
246 if (!c->c_stack)
247 return 0;
248
249 return 1;
250}
251
252PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000253PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000254 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255{
256 struct compiler c;
257 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000258 PyCompilerFlags local_flags;
259 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000261 if (!__doc__) {
262 __doc__ = PyString_InternFromString("__doc__");
263 if (!__doc__)
264 return NULL;
265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266
267 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000270 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 c.c_future = PyFuture_FromAST(mod, filename);
272 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000273 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000275 local_flags.cf_flags = 0;
276 flags = &local_flags;
277 }
278 merged = c.c_future->ff_features | flags->cf_flags;
279 c.c_future->ff_features = merged;
280 flags->cf_flags = merged;
281 c.c_flags = flags;
282 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283
284 c.c_st = PySymtable_Build(mod, filename, c.c_future);
285 if (c.c_st == NULL) {
286 if (!PyErr_Occurred())
287 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000288 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 }
290
291 /* XXX initialize to NULL for now, need to handle */
292 c.c_encoding = NULL;
293
294 co = compiler_mod(&c, mod);
295
Thomas Wouters1175c432006-02-27 22:49:54 +0000296 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000298 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 return co;
300}
301
302PyCodeObject *
303PyNode_Compile(struct _node *n, const char *filename)
304{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000305 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000307 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308 if (!arena)
309 return NULL;
310 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000311 if (mod)
312 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000313 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000314 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000315}
316
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000317static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 if (c->c_st)
321 PySymtable_Free(c->c_st);
322 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000325}
326
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000329{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 PyObject *v, *k;
332 PyObject *dict = PyDict_New();
333 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335 n = PyList_Size(list);
336 for (i = 0; i < n; i++) {
337 v = PyInt_FromLong(i);
338 if (!v) {
339 Py_DECREF(dict);
340 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000341 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000342 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000343 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
345 Py_XDECREF(k);
346 Py_DECREF(v);
347 Py_DECREF(dict);
348 return NULL;
349 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000350 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 return dict;
354}
355
356/* Return new dict containing names from src that match scope(s).
357
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000358src is a symbol table dictionary. If the scope of a name matches
359either scope_type or flag is set, insert it into the new dict. The
360values are integers, starting at offset and increasing by one for
361each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362*/
363
364static PyObject *
365dictbytype(PyObject *src, int scope_type, int flag, int offset)
366{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000367 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 PyObject *k, *v, *dest = PyDict_New();
369
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000370 assert(offset >= 0);
371 if (dest == NULL)
372 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373
374 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000375 /* XXX this should probably be a macro in symtable.h */
376 assert(PyInt_Check(v));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000377 scope = (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000379 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
380 PyObject *tuple, *item = PyInt_FromLong(i);
381 if (item == NULL) {
382 Py_DECREF(dest);
383 return NULL;
384 }
385 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000386 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000387 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
388 Py_DECREF(item);
389 Py_DECREF(dest);
390 Py_XDECREF(tuple);
391 return NULL;
392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000394 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 }
397 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000398}
399
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400static void
401compiler_unit_check(struct compiler_unit *u)
402{
403 basicblock *block;
404 for (block = u->u_blocks; block != NULL; block = block->b_list) {
405 assert(block != (void *)0xcbcbcbcb);
406 assert(block != (void *)0xfbfbfbfb);
407 assert(block != (void *)0xdbdbdbdb);
408 if (block->b_instr != NULL) {
409 assert(block->b_ialloc > 0);
410 assert(block->b_iused > 0);
411 assert(block->b_ialloc >= block->b_iused);
412 }
413 else {
414 assert (block->b_iused == 0);
415 assert (block->b_ialloc == 0);
416 }
417 }
418}
419
420static void
421compiler_unit_free(struct compiler_unit *u)
422{
423 basicblock *b, *next;
424
425 compiler_unit_check(u);
426 b = u->u_blocks;
427 while (b != NULL) {
428 if (b->b_instr)
429 PyObject_Free((void *)b->b_instr);
430 next = b->b_list;
431 PyObject_Free((void *)b);
432 b = next;
433 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000434 Py_CLEAR(u->u_ste);
435 Py_CLEAR(u->u_name);
436 Py_CLEAR(u->u_consts);
437 Py_CLEAR(u->u_names);
438 Py_CLEAR(u->u_varnames);
439 Py_CLEAR(u->u_freevars);
440 Py_CLEAR(u->u_cellvars);
441 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 PyObject_Free(u);
443}
444
445static int
446compiler_enter_scope(struct compiler *c, identifier name, void *key,
447 int lineno)
448{
449 struct compiler_unit *u;
450
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000452 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000453 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000454 PyErr_NoMemory();
455 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000456 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000459 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460 u->u_ste = PySymtable_Lookup(c->c_st, key);
461 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000462 compiler_unit_free(u);
463 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464 }
465 Py_INCREF(name);
466 u->u_name = name;
467 u->u_varnames = list2dict(u->u_ste->ste_varnames);
468 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000469 if (!u->u_varnames || !u->u_cellvars) {
470 compiler_unit_free(u);
471 return 0;
472 }
473
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000475 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000476 if (!u->u_freevars) {
477 compiler_unit_free(u);
478 return 0;
479 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480
481 u->u_blocks = NULL;
482 u->u_tmpname = 0;
483 u->u_nfblocks = 0;
484 u->u_firstlineno = lineno;
485 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000486 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487 u->u_consts = PyDict_New();
488 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000489 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 return 0;
491 }
492 u->u_names = PyDict_New();
493 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000494 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 return 0;
496 }
497
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499
500 /* Push the old compiler_unit on the stack. */
501 if (c->u) {
502 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
504 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000505 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 return 0;
507 }
508 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000509 u->u_private = c->u->u_private;
510 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511 }
512 c->u = u;
513
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000514 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000515 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000516 return 0;
517
518 return 1;
519}
520
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000521static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000522compiler_exit_scope(struct compiler *c)
523{
524 int n;
525 PyObject *wrapper;
526
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000527 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 compiler_unit_free(c->u);
529 /* Restore c->u to the parent unit. */
530 n = PyList_GET_SIZE(c->c_stack) - 1;
531 if (n >= 0) {
532 wrapper = PyList_GET_ITEM(c->c_stack, n);
533 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000534 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000535 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000537 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 compiler_unit_check(c->u);
539 }
540 else
541 c->u = NULL;
542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000543}
544
Guido van Rossumc2e20742006-02-27 22:32:47 +0000545/* Allocate a new "anonymous" local variable.
546 Used by list comprehensions and with statements.
547*/
548
549static PyObject *
550compiler_new_tmpname(struct compiler *c)
551{
552 char tmpname[256];
553 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
554 return PyString_FromString(tmpname);
555}
556
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000557/* Allocate a new block and return a pointer to it.
558 Returns NULL on error.
559*/
560
561static basicblock *
562compiler_new_block(struct compiler *c)
563{
564 basicblock *b;
565 struct compiler_unit *u;
566
567 u = c->u;
568 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000569 if (b == NULL) {
570 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000572 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000574 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 b->b_list = u->u_blocks;
576 u->u_blocks = b;
577 return b;
578}
579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580static basicblock *
581compiler_use_new_block(struct compiler *c)
582{
583 basicblock *block = compiler_new_block(c);
584 if (block == NULL)
585 return NULL;
586 c->u->u_curblock = block;
587 return block;
588}
589
590static basicblock *
591compiler_next_block(struct compiler *c)
592{
593 basicblock *block = compiler_new_block(c);
594 if (block == NULL)
595 return NULL;
596 c->u->u_curblock->b_next = block;
597 c->u->u_curblock = block;
598 return block;
599}
600
601static basicblock *
602compiler_use_next_block(struct compiler *c, basicblock *block)
603{
604 assert(block != NULL);
605 c->u->u_curblock->b_next = block;
606 c->u->u_curblock = block;
607 return block;
608}
609
610/* Returns the offset of the next instruction in the current block's
611 b_instr array. Resizes the b_instr as necessary.
612 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000613*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
615static int
616compiler_next_instr(struct compiler *c, basicblock *b)
617{
618 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000619 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000621 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (b->b_instr == NULL) {
623 PyErr_NoMemory();
624 return -1;
625 }
626 b->b_ialloc = DEFAULT_BLOCK_SIZE;
627 memset((char *)b->b_instr, 0,
628 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000630 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000631 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632 size_t oldsize, newsize;
633 oldsize = b->b_ialloc * sizeof(struct instr);
634 newsize = oldsize << 1;
635 if (newsize == 0) {
636 PyErr_NoMemory();
637 return -1;
638 }
639 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000641 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 if (tmp == NULL) {
643 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645 }
646 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
648 }
649 return b->b_iused++;
650}
651
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000652/* Set the i_lineno member of the instruction at offse off if the
653 line number for the current expression/statement (?) has not
654 already been set. If it has been set, the call has no effect.
655
656 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000657*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659static void
660compiler_set_lineno(struct compiler *c, int off)
661{
662 basicblock *b;
663 if (c->u->u_lineno_set)
664 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000665 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000666 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000667 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668}
669
670static int
671opcode_stack_effect(int opcode, int oparg)
672{
673 switch (opcode) {
674 case POP_TOP:
675 return -1;
676 case ROT_TWO:
677 case ROT_THREE:
678 return 0;
679 case DUP_TOP:
680 return 1;
681 case ROT_FOUR:
682 return 0;
683
684 case UNARY_POSITIVE:
685 case UNARY_NEGATIVE:
686 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000687 case UNARY_INVERT:
688 return 0;
689
Nick Coghlan650f0d02007-04-15 12:05:43 +0000690 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000691 case LIST_APPEND:
692 return -2;
693
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694 case BINARY_POWER:
695 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 case BINARY_MODULO:
697 case BINARY_ADD:
698 case BINARY_SUBTRACT:
699 case BINARY_SUBSCR:
700 case BINARY_FLOOR_DIVIDE:
701 case BINARY_TRUE_DIVIDE:
702 return -1;
703 case INPLACE_FLOOR_DIVIDE:
704 case INPLACE_TRUE_DIVIDE:
705 return -1;
706
707 case SLICE+0:
708 return 1;
709 case SLICE+1:
710 return 0;
711 case SLICE+2:
712 return 0;
713 case SLICE+3:
714 return -1;
715
716 case STORE_SLICE+0:
717 return -2;
718 case STORE_SLICE+1:
719 return -3;
720 case STORE_SLICE+2:
721 return -3;
722 case STORE_SLICE+3:
723 return -4;
724
725 case DELETE_SLICE+0:
726 return -1;
727 case DELETE_SLICE+1:
728 return -2;
729 case DELETE_SLICE+2:
730 return -2;
731 case DELETE_SLICE+3:
732 return -3;
733
734 case INPLACE_ADD:
735 case INPLACE_SUBTRACT:
736 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 case INPLACE_MODULO:
738 return -1;
739 case STORE_SUBSCR:
740 return -3;
741 case DELETE_SUBSCR:
742 return -2;
743
744 case BINARY_LSHIFT:
745 case BINARY_RSHIFT:
746 case BINARY_AND:
747 case BINARY_XOR:
748 case BINARY_OR:
749 return -1;
750 case INPLACE_POWER:
751 return -1;
752 case GET_ITER:
753 return 0;
754
755 case PRINT_EXPR:
756 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000757 case LOAD_BUILD_CLASS:
758 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759 case INPLACE_LSHIFT:
760 case INPLACE_RSHIFT:
761 case INPLACE_AND:
762 case INPLACE_XOR:
763 case INPLACE_OR:
764 return -1;
765 case BREAK_LOOP:
766 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000767 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000768 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000769 case STORE_LOCALS:
770 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771 case RETURN_VALUE:
772 return -1;
773 case IMPORT_STAR:
774 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775 case YIELD_VALUE:
776 return 0;
777
778 case POP_BLOCK:
779 return 0;
780 case END_FINALLY:
781 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782
783 case STORE_NAME:
784 return -1;
785 case DELETE_NAME:
786 return 0;
787 case UNPACK_SEQUENCE:
788 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000789 case UNPACK_EX:
790 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791 case FOR_ITER:
792 return 1;
793
794 case STORE_ATTR:
795 return -2;
796 case DELETE_ATTR:
797 return -1;
798 case STORE_GLOBAL:
799 return -1;
800 case DELETE_GLOBAL:
801 return 0;
802 case DUP_TOPX:
803 return oparg;
804 case LOAD_CONST:
805 return 1;
806 case LOAD_NAME:
807 return 1;
808 case BUILD_TUPLE:
809 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000810 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811 return 1-oparg;
812 case BUILD_MAP:
813 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000814 case MAKE_BYTES:
815 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 case LOAD_ATTR:
817 return 0;
818 case COMPARE_OP:
819 return -1;
820 case IMPORT_NAME:
821 return 0;
822 case IMPORT_FROM:
823 return 1;
824
825 case JUMP_FORWARD:
826 case JUMP_IF_FALSE:
827 case JUMP_IF_TRUE:
828 case JUMP_ABSOLUTE:
829 return 0;
830
831 case LOAD_GLOBAL:
832 return 1;
833
834 case CONTINUE_LOOP:
835 return 0;
836 case SETUP_LOOP:
837 return 0;
838 case SETUP_EXCEPT:
839 case SETUP_FINALLY:
840 return 3; /* actually pushed by an exception */
841
842 case LOAD_FAST:
843 return 1;
844 case STORE_FAST:
845 return -1;
846 case DELETE_FAST:
847 return 0;
848
849 case RAISE_VARARGS:
850 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000851#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 case CALL_FUNCTION:
853 return -NARGS(oparg);
854 case CALL_FUNCTION_VAR:
855 case CALL_FUNCTION_KW:
856 return -NARGS(oparg)-1;
857 case CALL_FUNCTION_VAR_KW:
858 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000860 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000861 case MAKE_CLOSURE:
862 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000863#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 case BUILD_SLICE:
865 if (oparg == 3)
866 return -2;
867 else
868 return -1;
869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000870 case LOAD_CLOSURE:
871 return 1;
872 case LOAD_DEREF:
873 return 1;
874 case STORE_DEREF:
875 return -1;
876 default:
877 fprintf(stderr, "opcode = %d\n", opcode);
878 Py_FatalError("opcode_stack_effect()");
879
880 }
881 return 0; /* not reachable */
882}
883
884/* Add an opcode with no argument.
885 Returns 0 on failure, 1 on success.
886*/
887
888static int
889compiler_addop(struct compiler *c, int opcode)
890{
891 basicblock *b;
892 struct instr *i;
893 int off;
894 off = compiler_next_instr(c, c->u->u_curblock);
895 if (off < 0)
896 return 0;
897 b = c->u->u_curblock;
898 i = &b->b_instr[off];
899 i->i_opcode = opcode;
900 i->i_hasarg = 0;
901 if (opcode == RETURN_VALUE)
902 b->b_return = 1;
903 compiler_set_lineno(c, off);
904 return 1;
905}
906
907static int
908compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
909{
910 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000911 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000913 /* necessary to make sure types aren't coerced (e.g., int and long) */
914 t = PyTuple_Pack(2, o, o->ob_type);
915 if (t == NULL)
916 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
918 v = PyDict_GetItem(dict, t);
919 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000920 if (PyErr_Occurred())
921 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922 arg = PyDict_Size(dict);
923 v = PyInt_FromLong(arg);
924 if (!v) {
925 Py_DECREF(t);
926 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000927 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928 if (PyDict_SetItem(dict, t, v) < 0) {
929 Py_DECREF(t);
930 Py_DECREF(v);
931 return -1;
932 }
933 Py_DECREF(v);
934 }
935 else
936 arg = PyInt_AsLong(v);
937 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000938 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
941static int
942compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
943 PyObject *o)
944{
945 int arg = compiler_add_o(c, dict, o);
946 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000947 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 return compiler_addop_i(c, opcode, arg);
949}
950
951static int
952compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000953 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954{
955 int arg;
956 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
957 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000958 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 arg = compiler_add_o(c, dict, mangled);
960 Py_DECREF(mangled);
961 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000962 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963 return compiler_addop_i(c, opcode, arg);
964}
965
966/* Add an opcode with an integer argument.
967 Returns 0 on failure, 1 on success.
968*/
969
970static int
971compiler_addop_i(struct compiler *c, int opcode, int oparg)
972{
973 struct instr *i;
974 int off;
975 off = compiler_next_instr(c, c->u->u_curblock);
976 if (off < 0)
977 return 0;
978 i = &c->u->u_curblock->b_instr[off];
979 i->i_opcode = opcode;
980 i->i_oparg = oparg;
981 i->i_hasarg = 1;
982 compiler_set_lineno(c, off);
983 return 1;
984}
985
986static int
987compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
988{
989 struct instr *i;
990 int off;
991
992 assert(b != NULL);
993 off = compiler_next_instr(c, c->u->u_curblock);
994 if (off < 0)
995 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 i = &c->u->u_curblock->b_instr[off];
997 i->i_opcode = opcode;
998 i->i_target = b;
999 i->i_hasarg = 1;
1000 if (absolute)
1001 i->i_jabs = 1;
1002 else
1003 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001004 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005 return 1;
1006}
1007
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1009 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 it as the current block. NEXT_BLOCK() also creates an implicit jump
1011 from the current block to the new block.
1012*/
1013
Thomas Wouters89f507f2006-12-13 04:49:30 +00001014/* The returns inside these macros make it impossible to decref objects
1015 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016*/
1017
1018
1019#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001020 if (compiler_use_new_block((C)) == NULL) \
1021 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022}
1023
1024#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001025 if (compiler_next_block((C)) == NULL) \
1026 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027}
1028
1029#define ADDOP(C, OP) { \
1030 if (!compiler_addop((C), (OP))) \
1031 return 0; \
1032}
1033
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001034#define ADDOP_IN_SCOPE(C, OP) { \
1035 if (!compiler_addop((C), (OP))) { \
1036 compiler_exit_scope(c); \
1037 return 0; \
1038 } \
1039}
1040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041#define ADDOP_O(C, OP, O, TYPE) { \
1042 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1043 return 0; \
1044}
1045
1046#define ADDOP_NAME(C, OP, O, TYPE) { \
1047 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1048 return 0; \
1049}
1050
1051#define ADDOP_I(C, OP, O) { \
1052 if (!compiler_addop_i((C), (OP), (O))) \
1053 return 0; \
1054}
1055
1056#define ADDOP_JABS(C, OP, O) { \
1057 if (!compiler_addop_j((C), (OP), (O), 1)) \
1058 return 0; \
1059}
1060
1061#define ADDOP_JREL(C, OP, O) { \
1062 if (!compiler_addop_j((C), (OP), (O), 0)) \
1063 return 0; \
1064}
1065
1066/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1067 the ASDL name to synthesize the name of the C type and the visit function.
1068*/
1069
1070#define VISIT(C, TYPE, V) {\
1071 if (!compiler_visit_ ## TYPE((C), (V))) \
1072 return 0; \
1073}
1074
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075#define VISIT_IN_SCOPE(C, TYPE, V) {\
1076 if (!compiler_visit_ ## TYPE((C), (V))) { \
1077 compiler_exit_scope(c); \
1078 return 0; \
1079 } \
1080}
1081
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082#define VISIT_SLICE(C, V, CTX) {\
1083 if (!compiler_visit_slice((C), (V), (CTX))) \
1084 return 0; \
1085}
1086
1087#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001088 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001090 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001091 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092 if (!compiler_visit_ ## TYPE((C), elt)) \
1093 return 0; \
1094 } \
1095}
1096
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001097#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001098 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001099 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001100 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001101 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001102 if (!compiler_visit_ ## TYPE((C), elt)) { \
1103 compiler_exit_scope(c); \
1104 return 0; \
1105 } \
1106 } \
1107}
1108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109static int
1110compiler_isdocstring(stmt_ty s)
1111{
1112 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001113 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114 return s->v.Expr.value->kind == Str_kind;
1115}
1116
1117/* Compile a sequence of statements, checking for a docstring. */
1118
1119static int
1120compiler_body(struct compiler *c, asdl_seq *stmts)
1121{
1122 int i = 0;
1123 stmt_ty st;
1124
1125 if (!asdl_seq_LEN(stmts))
1126 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128 if (compiler_isdocstring(st)) {
1129 i = 1;
1130 VISIT(c, expr, st->v.Expr.value);
1131 if (!compiler_nameop(c, __doc__, Store))
1132 return 0;
1133 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001134 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 return 1;
1137}
1138
1139static PyCodeObject *
1140compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001141{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001142 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001143 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144 static PyObject *module;
1145 if (!module) {
1146 module = PyString_FromString("<module>");
1147 if (!module)
1148 return NULL;
1149 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001150 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1151 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001152 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153 switch (mod->kind) {
1154 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001155 if (!compiler_body(c, mod->v.Module.body)) {
1156 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001158 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159 break;
1160 case Interactive_kind:
1161 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001162 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001163 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 break;
1165 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001166 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001167 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168 break;
1169 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001170 PyErr_SetString(PyExc_SystemError,
1171 "suite should not be possible");
1172 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001173 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001174 PyErr_Format(PyExc_SystemError,
1175 "module kind %d should not be possible",
1176 mod->kind);
1177 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001178 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 co = assemble(c, addNone);
1180 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001181 return co;
1182}
1183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184/* The test for LOCAL must come before the test for FREE in order to
1185 handle classes where name is both local and free. The local var is
1186 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001187*/
1188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189static int
1190get_ref_type(struct compiler *c, PyObject *name)
1191{
1192 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001193 if (scope == 0) {
1194 char buf[350];
1195 PyOS_snprintf(buf, sizeof(buf),
1196 "unknown scope for %.100s in %.100s(%s) in %s\n"
1197 "symbols: %s\nlocals: %s\nglobals: %s\n",
1198 PyString_AS_STRING(name),
1199 PyString_AS_STRING(c->u->u_name),
1200 PyObject_REPR(c->u->u_ste->ste_id),
1201 c->c_filename,
1202 PyObject_REPR(c->u->u_ste->ste_symbols),
1203 PyObject_REPR(c->u->u_varnames),
1204 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001206 Py_FatalError(buf);
1207 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001208
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001209 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210}
1211
1212static int
1213compiler_lookup_arg(PyObject *dict, PyObject *name)
1214{
1215 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001216 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001218 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001220 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001221 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001222 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 return PyInt_AS_LONG(v);
1224}
1225
1226static int
1227compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1228{
1229 int i, free = PyCode_GetNumFree(co);
1230 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001231 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1232 ADDOP_I(c, MAKE_FUNCTION, args);
1233 return 1;
1234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235 for (i = 0; i < free; ++i) {
1236 /* Bypass com_addop_varname because it will generate
1237 LOAD_DEREF but LOAD_CLOSURE is needed.
1238 */
1239 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1240 int arg, reftype;
1241
1242 /* Special case: If a class contains a method with a
1243 free variable that has the same name as a method,
1244 the name will be considered free *and* local in the
1245 class. It should be handled by the closure, as
1246 well as by the normal name loookup logic.
1247 */
1248 reftype = get_ref_type(c, name);
1249 if (reftype == CELL)
1250 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1251 else /* (reftype == FREE) */
1252 arg = compiler_lookup_arg(c->u->u_freevars, name);
1253 if (arg == -1) {
1254 printf("lookup %s in %s %d %d\n"
1255 "freevars of %s: %s\n",
1256 PyObject_REPR(name),
1257 PyString_AS_STRING(c->u->u_name),
1258 reftype, arg,
1259 PyString_AS_STRING(co->co_name),
1260 PyObject_REPR(co->co_freevars));
1261 Py_FatalError("compiler_make_closure()");
1262 }
1263 ADDOP_I(c, LOAD_CLOSURE, arg);
1264 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001265 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001266 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001267 ADDOP_I(c, MAKE_CLOSURE, args);
1268 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269}
1270
1271static int
1272compiler_decorators(struct compiler *c, asdl_seq* decos)
1273{
1274 int i;
1275
1276 if (!decos)
1277 return 1;
1278
1279 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 }
1282 return 1;
1283}
1284
1285static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1287 asdl_seq *kw_defaults)
1288{
1289 int i, default_count = 0;
1290 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001291 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001292 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1293 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001294 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001295 if (!compiler_visit_expr(c, default_)) {
1296 return -1;
1297 }
1298 default_count++;
1299 }
1300 }
1301 return default_count;
1302}
1303
1304static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001305compiler_visit_argannotation(struct compiler *c, identifier id,
1306 expr_ty annotation, PyObject *names)
1307{
1308 if (annotation) {
1309 VISIT(c, expr, annotation);
1310 if (PyList_Append(names, id))
1311 return -1;
1312 }
1313 return 0;
1314}
1315
1316static int
1317compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1318 PyObject *names)
1319{
1320 int i, error;
1321 for (i = 0; i < asdl_seq_LEN(args); i++) {
1322 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001323 error = compiler_visit_argannotation(
1324 c,
1325 arg->arg,
1326 arg->annotation,
1327 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001328 if (error)
1329 return error;
1330 }
1331 return 0;
1332}
1333
1334static int
1335compiler_visit_annotations(struct compiler *c, arguments_ty args,
1336 expr_ty returns)
1337{
Guido van Rossum0240b922007-02-26 21:23:50 +00001338 /* Push arg annotations and a list of the argument names. Return the #
1339 of items pushed. The expressions are evaluated out-of-order wrt the
1340 source code.
1341
1342 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1343 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001344 static identifier return_str;
1345 PyObject *names;
1346 int len;
1347 names = PyList_New(0);
1348 if (!names)
1349 return -1;
1350
1351 if (compiler_visit_argannotations(c, args->args, names))
1352 goto error;
1353 if (args->varargannotation &&
1354 compiler_visit_argannotation(c, args->vararg,
1355 args->varargannotation, names))
1356 goto error;
1357 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1358 goto error;
1359 if (args->kwargannotation &&
1360 compiler_visit_argannotation(c, args->kwarg,
1361 args->kwargannotation, names))
1362 goto error;
1363
1364 if (!return_str) {
1365 return_str = PyString_InternFromString("return");
1366 if (!return_str)
1367 goto error;
1368 }
1369 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1370 goto error;
1371 }
1372
1373 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001374 if (len > 65534) {
1375 /* len must fit in 16 bits, and len is incremented below */
1376 PyErr_SetString(PyExc_SyntaxError,
1377 "too many annotations");
1378 goto error;
1379 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 if (len) {
1381 /* convert names to a tuple and place on stack */
1382 PyObject *elt;
1383 int i;
1384 PyObject *s = PyTuple_New(len);
1385 if (!s)
1386 goto error;
1387 for (i = 0; i < len; i++) {
1388 elt = PyList_GET_ITEM(names, i);
1389 Py_INCREF(elt);
1390 PyTuple_SET_ITEM(s, i, elt);
1391 }
1392 ADDOP_O(c, LOAD_CONST, s, consts);
1393 Py_DECREF(s);
1394 len++; /* include the just-pushed tuple */
1395 }
1396 Py_DECREF(names);
1397 return len;
1398
1399error:
1400 Py_DECREF(names);
1401 return -1;
1402}
1403
1404static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405compiler_function(struct compiler *c, stmt_ty s)
1406{
1407 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001408 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001410 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001411 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001412 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001413 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
1416 assert(s->kind == FunctionDef_kind);
1417
1418 if (!compiler_decorators(c, decos))
1419 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001420 if (args->kwonlyargs) {
1421 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1422 args->kw_defaults);
1423 if (res < 0)
1424 return 0;
1425 kw_default_count = res;
1426 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 if (args->defaults)
1428 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001429 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001430 if (num_annotations < 0)
1431 return 0;
1432 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001433
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1435 s->lineno))
1436 return 0;
1437
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001438 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001439 docstring = compiler_isdocstring(st);
1440 if (docstring)
1441 first_const = st->v.Expr.value->v.Str.s;
1442 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001443 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001444 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001445 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001448 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001450 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001452 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1453 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 }
1455 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001456 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 if (co == NULL)
1458 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Guido van Rossum4f72a782006-10-27 23:31:49 +00001460 arglength = asdl_seq_LEN(args->defaults);
1461 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001462 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001463 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001464 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1468 ADDOP_I(c, CALL_FUNCTION, 1);
1469 }
1470
1471 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1472}
1473
1474static int
1475compiler_class(struct compiler *c, stmt_ty s)
1476{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001477 static PyObject *build_class = NULL;
1478 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001480 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001481 PySTEntryObject *ste;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001482 int err, i;
1483 asdl_seq* decos = s->v.ClassDef.decorator_list;
1484
1485 if (!compiler_decorators(c, decos))
1486 return 0;
1487
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001489 /* initialize statics */
1490 if (build_class == NULL) {
1491 build_class = PyString_FromString("__build_class__");
1492 if (build_class == NULL)
1493 return 0;
1494 }
1495 if (locals == NULL) {
1496 locals = PyString_FromString("__locals__");
1497 if (locals == NULL)
1498 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001499 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001501 /* ultimately generate code for:
1502 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1503 where:
1504 <func> is a function/closure created from the class body
1505 <name> is the class name
1506 <bases> is the positional arguments and *varargs argument
1507 <keywords> is the keyword arguments and **kwds argument
1508 This borrows from compiler_call.
1509 */
1510
1511 /* 0. Create a fake variable named __locals__ */
1512 ste = PySymtable_Lookup(c->c_st, s);
1513 if (ste == NULL)
1514 return 0;
1515 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001516 err = PyList_Append(ste->ste_varnames, locals);
1517 Py_DECREF(ste);
1518 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001519 return 0;
1520
1521 /* 1. compile the class body into a code object */
1522 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1523 return 0;
1524 /* this block represents what we do in the new scope */
1525 {
1526 /* use the class name for name mangling */
1527 Py_INCREF(s->v.ClassDef.name);
1528 c->u->u_private = s->v.ClassDef.name;
1529 /* force it to have one mandatory argument */
1530 c->u->u_argcount = 1;
1531 /* load the first argument ... */
1532 ADDOP_I(c, LOAD_FAST, 0);
1533 /* ... and store it into f_locals */
1534 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1535 /* load __name__ ... */
1536 str = PyString_InternFromString("__name__");
1537 if (!str || !compiler_nameop(c, str, Load)) {
1538 Py_XDECREF(str);
1539 compiler_exit_scope(c);
1540 return 0;
1541 }
1542 Py_DECREF(str);
1543 /* ... and store it as __module__ */
1544 str = PyString_InternFromString("__module__");
1545 if (!str || !compiler_nameop(c, str, Store)) {
1546 Py_XDECREF(str);
1547 compiler_exit_scope(c);
1548 return 0;
1549 }
1550 Py_DECREF(str);
1551 /* compile the body proper */
1552 if (!compiler_body(c, s->v.ClassDef.body)) {
1553 compiler_exit_scope(c);
1554 return 0;
1555 }
1556 /* return None */
1557 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1558 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1559 /* create the code object */
1560 co = assemble(c, 1);
1561 }
1562 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001563 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001564 if (co == NULL)
1565 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001567 /* 2. load the 'build_class' function */
1568 ADDOP(c, LOAD_BUILD_CLASS);
1569
1570 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001571 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001572 Py_DECREF(co);
1573
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001574 /* 4. load class name */
1575 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1576
1577 /* 5. generate the rest of the code for the call */
1578 if (!compiler_call_helper(c, 2,
1579 s->v.ClassDef.bases,
1580 s->v.ClassDef.keywords,
1581 s->v.ClassDef.starargs,
1582 s->v.ClassDef.kwargs))
1583 return 0;
1584
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001585 /* 6. apply decorators */
1586 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1587 ADDOP_I(c, CALL_FUNCTION, 1);
1588 }
1589
1590 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1592 return 0;
1593 return 1;
1594}
1595
1596static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001597compiler_ifexp(struct compiler *c, expr_ty e)
1598{
1599 basicblock *end, *next;
1600
1601 assert(e->kind == IfExp_kind);
1602 end = compiler_new_block(c);
1603 if (end == NULL)
1604 return 0;
1605 next = compiler_new_block(c);
1606 if (next == NULL)
1607 return 0;
1608 VISIT(c, expr, e->v.IfExp.test);
1609 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1610 ADDOP(c, POP_TOP);
1611 VISIT(c, expr, e->v.IfExp.body);
1612 ADDOP_JREL(c, JUMP_FORWARD, end);
1613 compiler_use_next_block(c, next);
1614 ADDOP(c, POP_TOP);
1615 VISIT(c, expr, e->v.IfExp.orelse);
1616 compiler_use_next_block(c, end);
1617 return 1;
1618}
1619
1620static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621compiler_lambda(struct compiler *c, expr_ty e)
1622{
1623 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001624 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001625 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 arguments_ty args = e->v.Lambda.args;
1627 assert(e->kind == Lambda_kind);
1628
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001629 if (!name) {
1630 name = PyString_InternFromString("<lambda>");
1631 if (!name)
1632 return 0;
1633 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634
Guido van Rossum4f72a782006-10-27 23:31:49 +00001635 if (args->kwonlyargs) {
1636 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1637 args->kw_defaults);
1638 if (res < 0) return 0;
1639 kw_default_count = res;
1640 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 if (args->defaults)
1642 VISIT_SEQ(c, expr, args->defaults);
1643 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1644 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001645
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001647 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001648 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1649 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001651 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 if (co == NULL)
1653 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654
Guido van Rossum4f72a782006-10-27 23:31:49 +00001655 arglength = asdl_seq_LEN(args->defaults);
1656 arglength |= kw_default_count << 8;
1657 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001658 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659
1660 return 1;
1661}
1662
1663static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664compiler_if(struct compiler *c, stmt_ty s)
1665{
1666 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001667 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 assert(s->kind == If_kind);
1669 end = compiler_new_block(c);
1670 if (end == NULL)
1671 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001672 next = compiler_new_block(c);
1673 if (next == NULL)
1674 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001675
1676 constant = expr_constant(s->v.If.test);
1677 /* constant = 0: "if 0"
1678 * constant = 1: "if 1", "if 2", ...
1679 * constant = -1: rest */
1680 if (constant == 0) {
1681 if (s->v.If.orelse)
1682 VISIT_SEQ(c, stmt, s->v.If.orelse);
1683 } else if (constant == 1) {
1684 VISIT_SEQ(c, stmt, s->v.If.body);
1685 } else {
1686 VISIT(c, expr, s->v.If.test);
1687 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1688 ADDOP(c, POP_TOP);
1689 VISIT_SEQ(c, stmt, s->v.If.body);
1690 ADDOP_JREL(c, JUMP_FORWARD, end);
1691 compiler_use_next_block(c, next);
1692 ADDOP(c, POP_TOP);
1693 if (s->v.If.orelse)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001694 VISIT_SEQ(c, stmt, s->v.If.orelse);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001695 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696 compiler_use_next_block(c, end);
1697 return 1;
1698}
1699
1700static int
1701compiler_for(struct compiler *c, stmt_ty s)
1702{
1703 basicblock *start, *cleanup, *end;
1704
1705 start = compiler_new_block(c);
1706 cleanup = compiler_new_block(c);
1707 end = compiler_new_block(c);
1708 if (start == NULL || end == NULL || cleanup == NULL)
1709 return 0;
1710 ADDOP_JREL(c, SETUP_LOOP, end);
1711 if (!compiler_push_fblock(c, LOOP, start))
1712 return 0;
1713 VISIT(c, expr, s->v.For.iter);
1714 ADDOP(c, GET_ITER);
1715 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001716 /* XXX(nnorwitz): is there a better way to handle this?
1717 for loops are special, we want to be able to trace them
1718 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001719 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 ADDOP_JREL(c, FOR_ITER, cleanup);
1721 VISIT(c, expr, s->v.For.target);
1722 VISIT_SEQ(c, stmt, s->v.For.body);
1723 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1724 compiler_use_next_block(c, cleanup);
1725 ADDOP(c, POP_BLOCK);
1726 compiler_pop_fblock(c, LOOP, start);
1727 VISIT_SEQ(c, stmt, s->v.For.orelse);
1728 compiler_use_next_block(c, end);
1729 return 1;
1730}
1731
1732static int
1733compiler_while(struct compiler *c, stmt_ty s)
1734{
1735 basicblock *loop, *orelse, *end, *anchor = NULL;
1736 int constant = expr_constant(s->v.While.test);
1737
1738 if (constant == 0)
1739 return 1;
1740 loop = compiler_new_block(c);
1741 end = compiler_new_block(c);
1742 if (constant == -1) {
1743 anchor = compiler_new_block(c);
1744 if (anchor == NULL)
1745 return 0;
1746 }
1747 if (loop == NULL || end == NULL)
1748 return 0;
1749 if (s->v.While.orelse) {
1750 orelse = compiler_new_block(c);
1751 if (orelse == NULL)
1752 return 0;
1753 }
1754 else
1755 orelse = NULL;
1756
1757 ADDOP_JREL(c, SETUP_LOOP, end);
1758 compiler_use_next_block(c, loop);
1759 if (!compiler_push_fblock(c, LOOP, loop))
1760 return 0;
1761 if (constant == -1) {
1762 VISIT(c, expr, s->v.While.test);
1763 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1764 ADDOP(c, POP_TOP);
1765 }
1766 VISIT_SEQ(c, stmt, s->v.While.body);
1767 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1768
1769 /* XXX should the two POP instructions be in a separate block
1770 if there is no else clause ?
1771 */
1772
1773 if (constant == -1) {
1774 compiler_use_next_block(c, anchor);
1775 ADDOP(c, POP_TOP);
1776 ADDOP(c, POP_BLOCK);
1777 }
1778 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001779 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001780 VISIT_SEQ(c, stmt, s->v.While.orelse);
1781 compiler_use_next_block(c, end);
1782
1783 return 1;
1784}
1785
1786static int
1787compiler_continue(struct compiler *c)
1788{
1789 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001790 static const char IN_FINALLY_ERROR_MSG[] =
1791 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 int i;
1793
1794 if (!c->u->u_nfblocks)
1795 return compiler_error(c, LOOP_ERROR_MSG);
1796 i = c->u->u_nfblocks - 1;
1797 switch (c->u->u_fblock[i].fb_type) {
1798 case LOOP:
1799 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1800 break;
1801 case EXCEPT:
1802 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001803 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1804 /* Prevent continue anywhere under a finally
1805 even if hidden in a sub-try or except. */
1806 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1807 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809 if (i == -1)
1810 return compiler_error(c, LOOP_ERROR_MSG);
1811 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1812 break;
1813 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001814 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 }
1816
1817 return 1;
1818}
1819
1820/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1821
1822 SETUP_FINALLY L
1823 <code for body>
1824 POP_BLOCK
1825 LOAD_CONST <None>
1826 L: <code for finalbody>
1827 END_FINALLY
1828
1829 The special instructions use the block stack. Each block
1830 stack entry contains the instruction that created it (here
1831 SETUP_FINALLY), the level of the value stack at the time the
1832 block stack entry was created, and a label (here L).
1833
1834 SETUP_FINALLY:
1835 Pushes the current value stack level and the label
1836 onto the block stack.
1837 POP_BLOCK:
1838 Pops en entry from the block stack, and pops the value
1839 stack until its level is the same as indicated on the
1840 block stack. (The label is ignored.)
1841 END_FINALLY:
1842 Pops a variable number of entries from the *value* stack
1843 and re-raises the exception they specify. The number of
1844 entries popped depends on the (pseudo) exception type.
1845
1846 The block stack is unwound when an exception is raised:
1847 when a SETUP_FINALLY entry is found, the exception is pushed
1848 onto the value stack (and the exception condition is cleared),
1849 and the interpreter jumps to the label gotten from the block
1850 stack.
1851*/
1852
1853static int
1854compiler_try_finally(struct compiler *c, stmt_ty s)
1855{
1856 basicblock *body, *end;
1857 body = compiler_new_block(c);
1858 end = compiler_new_block(c);
1859 if (body == NULL || end == NULL)
1860 return 0;
1861
1862 ADDOP_JREL(c, SETUP_FINALLY, end);
1863 compiler_use_next_block(c, body);
1864 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1865 return 0;
1866 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1867 ADDOP(c, POP_BLOCK);
1868 compiler_pop_fblock(c, FINALLY_TRY, body);
1869
1870 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1871 compiler_use_next_block(c, end);
1872 if (!compiler_push_fblock(c, FINALLY_END, end))
1873 return 0;
1874 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1875 ADDOP(c, END_FINALLY);
1876 compiler_pop_fblock(c, FINALLY_END, end);
1877
1878 return 1;
1879}
1880
1881/*
1882 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1883 (The contents of the value stack is shown in [], with the top
1884 at the right; 'tb' is trace-back info, 'val' the exception's
1885 associated value, and 'exc' the exception.)
1886
1887 Value stack Label Instruction Argument
1888 [] SETUP_EXCEPT L1
1889 [] <code for S>
1890 [] POP_BLOCK
1891 [] JUMP_FORWARD L0
1892
1893 [tb, val, exc] L1: DUP )
1894 [tb, val, exc, exc] <evaluate E1> )
1895 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1896 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1897 [tb, val, exc, 1] POP )
1898 [tb, val, exc] POP
1899 [tb, val] <assign to V1> (or POP if no V1)
1900 [tb] POP
1901 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001902 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903
1904 [tb, val, exc, 0] L2: POP
1905 [tb, val, exc] DUP
1906 .............................etc.......................
1907
1908 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001909 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910
1911 [] L0: <next statement>
1912
1913 Of course, parts are not generated if Vi or Ei is not present.
1914*/
1915static int
1916compiler_try_except(struct compiler *c, stmt_ty s)
1917{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001918 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 int i, n;
1920
1921 body = compiler_new_block(c);
1922 except = compiler_new_block(c);
1923 orelse = compiler_new_block(c);
1924 end = compiler_new_block(c);
1925 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1926 return 0;
1927 ADDOP_JREL(c, SETUP_EXCEPT, except);
1928 compiler_use_next_block(c, body);
1929 if (!compiler_push_fblock(c, EXCEPT, body))
1930 return 0;
1931 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1932 ADDOP(c, POP_BLOCK);
1933 compiler_pop_fblock(c, EXCEPT, body);
1934 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1935 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1936 compiler_use_next_block(c, except);
1937 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001938 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 s->v.TryExcept.handlers, i);
1940 if (!handler->type && i < n-1)
1941 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001942 c->u->u_lineno_set = 0;
1943 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 except = compiler_new_block(c);
1945 if (except == NULL)
1946 return 0;
1947 if (handler->type) {
1948 ADDOP(c, DUP_TOP);
1949 VISIT(c, expr, handler->type);
1950 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1951 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1952 ADDOP(c, POP_TOP);
1953 }
1954 ADDOP(c, POP_TOP);
1955 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001956 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001957
1958 cleanup_end = compiler_new_block(c);
1959 cleanup_body = compiler_new_block(c);
1960 if(!(cleanup_end || cleanup_body))
1961 return 0;
1962
Guido van Rossum16be03e2007-01-10 18:51:35 +00001963 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001964 ADDOP(c, POP_TOP);
1965
1966 /*
1967 try:
1968 # body
1969 except type as name:
1970 try:
1971 # body
1972 finally:
1973 name = None
1974 del name
1975 */
1976
1977 /* second try: */
1978 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1979 compiler_use_next_block(c, cleanup_body);
1980 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1981 return 0;
1982
1983 /* second # body */
1984 VISIT_SEQ(c, stmt, handler->body);
1985 ADDOP(c, POP_BLOCK);
1986 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1987
1988 /* finally: */
1989 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1990 compiler_use_next_block(c, cleanup_end);
1991 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1992 return 0;
1993
1994 /* name = None */
1995 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00001996 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001997
Guido van Rossum16be03e2007-01-10 18:51:35 +00001998 /* del name */
1999 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002000
2001 ADDOP(c, END_FINALLY);
2002 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 }
2004 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002005 ADDOP(c, POP_TOP);
2006 ADDOP(c, POP_TOP);
2007 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 ADDOP_JREL(c, JUMP_FORWARD, end);
2010 compiler_use_next_block(c, except);
2011 if (handler->type)
2012 ADDOP(c, POP_TOP);
2013 }
2014 ADDOP(c, END_FINALLY);
2015 compiler_use_next_block(c, orelse);
2016 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2017 compiler_use_next_block(c, end);
2018 return 1;
2019}
2020
2021static int
2022compiler_import_as(struct compiler *c, identifier name, identifier asname)
2023{
2024 /* The IMPORT_NAME opcode was already generated. This function
2025 merely needs to bind the result to a name.
2026
2027 If there is a dot in name, we need to split it and emit a
2028 LOAD_ATTR for each name.
2029 */
2030 const char *src = PyString_AS_STRING(name);
2031 const char *dot = strchr(src, '.');
2032 if (dot) {
2033 /* Consume the base module name to get the first attribute */
2034 src = dot + 1;
2035 while (dot) {
2036 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002037 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002039 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002041 if (!attr)
2042 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002044 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 src = dot + 1;
2046 }
2047 }
2048 return compiler_nameop(c, asname, Store);
2049}
2050
2051static int
2052compiler_import(struct compiler *c, stmt_ty s)
2053{
2054 /* The Import node stores a module name like a.b.c as a single
2055 string. This is convenient for all cases except
2056 import a.b.c as d
2057 where we need to parse that string to extract the individual
2058 module names.
2059 XXX Perhaps change the representation to make this case simpler?
2060 */
2061 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002064 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002066 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067
Guido van Rossum45aecf42006-03-15 04:58:47 +00002068 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002069 if (level == NULL)
2070 return 0;
2071
2072 ADDOP_O(c, LOAD_CONST, level, consts);
2073 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2075 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2076
2077 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002078 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002079 if (!r)
2080 return r;
2081 }
2082 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 identifier tmp = alias->name;
2084 const char *base = PyString_AS_STRING(alias->name);
2085 char *dot = strchr(base, '.');
2086 if (dot)
2087 tmp = PyString_FromStringAndSize(base,
2088 dot - base);
2089 r = compiler_nameop(c, tmp, Store);
2090 if (dot) {
2091 Py_DECREF(tmp);
2092 }
2093 if (!r)
2094 return r;
2095 }
2096 }
2097 return 1;
2098}
2099
2100static int
2101compiler_from_import(struct compiler *c, stmt_ty s)
2102{
2103 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104
2105 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002106 PyObject *level;
2107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 if (!names)
2109 return 0;
2110
Guido van Rossum45aecf42006-03-15 04:58:47 +00002111 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002112 if (!level) {
2113 Py_DECREF(names);
2114 return 0;
2115 }
2116
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 /* build up the names */
2118 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002119 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 Py_INCREF(alias->name);
2121 PyTuple_SET_ITEM(names, i, alias->name);
2122 }
2123
2124 if (s->lineno > c->c_future->ff_lineno) {
2125 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2126 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002127 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 Py_DECREF(names);
2129 return compiler_error(c,
2130 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002131 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
2133 }
2134 }
2135
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002136 ADDOP_O(c, LOAD_CONST, level, consts);
2137 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002139 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2141 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002142 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 identifier store_name;
2144
2145 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2146 assert(n == 1);
2147 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002148 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 }
2150
2151 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2152 store_name = alias->name;
2153 if (alias->asname)
2154 store_name = alias->asname;
2155
2156 if (!compiler_nameop(c, store_name, Store)) {
2157 Py_DECREF(names);
2158 return 0;
2159 }
2160 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002161 /* remove imported module */
2162 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 return 1;
2164}
2165
2166static int
2167compiler_assert(struct compiler *c, stmt_ty s)
2168{
2169 static PyObject *assertion_error = NULL;
2170 basicblock *end;
2171
2172 if (Py_OptimizeFlag)
2173 return 1;
2174 if (assertion_error == NULL) {
2175 assertion_error = PyString_FromString("AssertionError");
2176 if (assertion_error == NULL)
2177 return 0;
2178 }
2179 VISIT(c, expr, s->v.Assert.test);
2180 end = compiler_new_block(c);
2181 if (end == NULL)
2182 return 0;
2183 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2184 ADDOP(c, POP_TOP);
2185 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2186 if (s->v.Assert.msg) {
2187 VISIT(c, expr, s->v.Assert.msg);
2188 ADDOP_I(c, RAISE_VARARGS, 2);
2189 }
2190 else {
2191 ADDOP_I(c, RAISE_VARARGS, 1);
2192 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002193 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 ADDOP(c, POP_TOP);
2195 return 1;
2196}
2197
2198static int
2199compiler_visit_stmt(struct compiler *c, stmt_ty s)
2200{
2201 int i, n;
2202
Thomas Wouters89f507f2006-12-13 04:49:30 +00002203 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002205 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002208 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002210 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002212 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 if (c->u->u_ste->ste_type != FunctionBlock)
2214 return compiler_error(c, "'return' outside function");
2215 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 VISIT(c, expr, s->v.Return.value);
2217 }
2218 else
2219 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2220 ADDOP(c, RETURN_VALUE);
2221 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002222 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 VISIT_SEQ(c, expr, s->v.Delete.targets)
2224 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002225 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 n = asdl_seq_LEN(s->v.Assign.targets);
2227 VISIT(c, expr, s->v.Assign.value);
2228 for (i = 0; i < n; i++) {
2229 if (i < n - 1)
2230 ADDOP(c, DUP_TOP);
2231 VISIT(c, expr,
2232 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2233 }
2234 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002235 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002237 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002239 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002241 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002243 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 n = 0;
2245 if (s->v.Raise.type) {
2246 VISIT(c, expr, s->v.Raise.type);
2247 n++;
2248 if (s->v.Raise.inst) {
2249 VISIT(c, expr, s->v.Raise.inst);
2250 n++;
2251 if (s->v.Raise.tback) {
2252 VISIT(c, expr, s->v.Raise.tback);
2253 n++;
2254 }
2255 }
2256 }
2257 ADDOP_I(c, RAISE_VARARGS, n);
2258 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002259 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002261 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002263 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002265 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002267 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002269 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002270 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002272 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002274 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 ADDOP(c, PRINT_EXPR);
2276 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002277 else if (s->v.Expr.value->kind != Str_kind &&
2278 s->v.Expr.value->kind != Num_kind) {
2279 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 ADDOP(c, POP_TOP);
2281 }
2282 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002283 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002285 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002286 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 return compiler_error(c, "'break' outside loop");
2288 ADDOP(c, BREAK_LOOP);
2289 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002290 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002292 case With_kind:
2293 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 }
2295 return 1;
2296}
2297
2298static int
2299unaryop(unaryop_ty op)
2300{
2301 switch (op) {
2302 case Invert:
2303 return UNARY_INVERT;
2304 case Not:
2305 return UNARY_NOT;
2306 case UAdd:
2307 return UNARY_POSITIVE;
2308 case USub:
2309 return UNARY_NEGATIVE;
2310 }
2311 return 0;
2312}
2313
2314static int
2315binop(struct compiler *c, operator_ty op)
2316{
2317 switch (op) {
2318 case Add:
2319 return BINARY_ADD;
2320 case Sub:
2321 return BINARY_SUBTRACT;
2322 case Mult:
2323 return BINARY_MULTIPLY;
2324 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002325 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 case Mod:
2327 return BINARY_MODULO;
2328 case Pow:
2329 return BINARY_POWER;
2330 case LShift:
2331 return BINARY_LSHIFT;
2332 case RShift:
2333 return BINARY_RSHIFT;
2334 case BitOr:
2335 return BINARY_OR;
2336 case BitXor:
2337 return BINARY_XOR;
2338 case BitAnd:
2339 return BINARY_AND;
2340 case FloorDiv:
2341 return BINARY_FLOOR_DIVIDE;
2342 }
2343 return 0;
2344}
2345
2346static int
2347cmpop(cmpop_ty op)
2348{
2349 switch (op) {
2350 case Eq:
2351 return PyCmp_EQ;
2352 case NotEq:
2353 return PyCmp_NE;
2354 case Lt:
2355 return PyCmp_LT;
2356 case LtE:
2357 return PyCmp_LE;
2358 case Gt:
2359 return PyCmp_GT;
2360 case GtE:
2361 return PyCmp_GE;
2362 case Is:
2363 return PyCmp_IS;
2364 case IsNot:
2365 return PyCmp_IS_NOT;
2366 case In:
2367 return PyCmp_IN;
2368 case NotIn:
2369 return PyCmp_NOT_IN;
2370 }
2371 return PyCmp_BAD;
2372}
2373
2374static int
2375inplace_binop(struct compiler *c, operator_ty op)
2376{
2377 switch (op) {
2378 case Add:
2379 return INPLACE_ADD;
2380 case Sub:
2381 return INPLACE_SUBTRACT;
2382 case Mult:
2383 return INPLACE_MULTIPLY;
2384 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002385 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 case Mod:
2387 return INPLACE_MODULO;
2388 case Pow:
2389 return INPLACE_POWER;
2390 case LShift:
2391 return INPLACE_LSHIFT;
2392 case RShift:
2393 return INPLACE_RSHIFT;
2394 case BitOr:
2395 return INPLACE_OR;
2396 case BitXor:
2397 return INPLACE_XOR;
2398 case BitAnd:
2399 return INPLACE_AND;
2400 case FloorDiv:
2401 return INPLACE_FLOOR_DIVIDE;
2402 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002403 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002404 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002405 return 0;
2406}
2407
2408static int
2409compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2410{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002411 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2413
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002414 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002415 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 /* XXX AugStore isn't used anywhere! */
2417
2418 /* First check for assignment to __debug__. Param? */
2419 if ((ctx == Store || ctx == AugStore || ctx == Del)
2420 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2421 return compiler_error(c, "can not assign to __debug__");
2422 }
2423
Guido van Rossumd8faa362007-04-27 19:54:29 +00002424mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002425 if (!mangled)
2426 return 0;
2427
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428 op = 0;
2429 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002430 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 switch (scope) {
2432 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002433 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 optype = OP_DEREF;
2435 break;
2436 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002437 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 optype = OP_DEREF;
2439 break;
2440 case LOCAL:
2441 if (c->u->u_ste->ste_type == FunctionBlock)
2442 optype = OP_FAST;
2443 break;
2444 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002445 if (c->u->u_ste->ste_type == FunctionBlock &&
2446 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002447 optype = OP_GLOBAL;
2448 break;
2449 case GLOBAL_EXPLICIT:
2450 optype = OP_GLOBAL;
2451 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002452 default:
2453 /* scope can be 0 */
2454 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 }
2456
2457 /* XXX Leave assert here, but handle __doc__ and the like better */
2458 assert(scope || PyString_AS_STRING(name)[0] == '_');
2459
2460 switch (optype) {
2461 case OP_DEREF:
2462 switch (ctx) {
2463 case Load: op = LOAD_DEREF; break;
2464 case Store: op = STORE_DEREF; break;
2465 case AugLoad:
2466 case AugStore:
2467 break;
2468 case Del:
2469 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002470 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002472 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002473 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002476 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002477 PyErr_SetString(PyExc_SystemError,
2478 "param invalid for deref variable");
2479 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 }
2481 break;
2482 case OP_FAST:
2483 switch (ctx) {
2484 case Load: op = LOAD_FAST; break;
2485 case Store: op = STORE_FAST; break;
2486 case Del: op = DELETE_FAST; break;
2487 case AugLoad:
2488 case AugStore:
2489 break;
2490 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002491 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002492 PyErr_SetString(PyExc_SystemError,
2493 "param invalid for local variable");
2494 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002496 ADDOP_O(c, op, mangled, varnames);
2497 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 return 1;
2499 case OP_GLOBAL:
2500 switch (ctx) {
2501 case Load: op = LOAD_GLOBAL; break;
2502 case Store: op = STORE_GLOBAL; break;
2503 case Del: op = DELETE_GLOBAL; break;
2504 case AugLoad:
2505 case AugStore:
2506 break;
2507 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002508 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002509 PyErr_SetString(PyExc_SystemError,
2510 "param invalid for global variable");
2511 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 }
2513 break;
2514 case OP_NAME:
2515 switch (ctx) {
2516 case Load: op = LOAD_NAME; break;
2517 case Store: op = STORE_NAME; break;
2518 case Del: op = DELETE_NAME; break;
2519 case AugLoad:
2520 case AugStore:
2521 break;
2522 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002523 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002524 PyErr_SetString(PyExc_SystemError,
2525 "param invalid for name variable");
2526 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 }
2528 break;
2529 }
2530
2531 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002532 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002533 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002534 if (arg < 0)
2535 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002536 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537}
2538
2539static int
2540compiler_boolop(struct compiler *c, expr_ty e)
2541{
2542 basicblock *end;
2543 int jumpi, i, n;
2544 asdl_seq *s;
2545
2546 assert(e->kind == BoolOp_kind);
2547 if (e->v.BoolOp.op == And)
2548 jumpi = JUMP_IF_FALSE;
2549 else
2550 jumpi = JUMP_IF_TRUE;
2551 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002552 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 return 0;
2554 s = e->v.BoolOp.values;
2555 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002556 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002558 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 ADDOP_JREL(c, jumpi, end);
2560 ADDOP(c, POP_TOP)
2561 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002562 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 compiler_use_next_block(c, end);
2564 return 1;
2565}
2566
2567static int
2568compiler_list(struct compiler *c, expr_ty e)
2569{
2570 int n = asdl_seq_LEN(e->v.List.elts);
2571 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002572 int i, seen_star = 0;
2573 for (i = 0; i < n; i++) {
2574 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2575 if (elt->kind == Starred_kind && !seen_star) {
2576 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2577 seen_star = 1;
2578 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2579 } else if (elt->kind == Starred_kind) {
2580 return compiler_error(c,
2581 "two starred expressions in assignment");
2582 }
2583 }
2584 if (!seen_star) {
2585 ADDOP_I(c, UNPACK_SEQUENCE, n);
2586 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587 }
2588 VISIT_SEQ(c, expr, e->v.List.elts);
2589 if (e->v.List.ctx == Load) {
2590 ADDOP_I(c, BUILD_LIST, n);
2591 }
2592 return 1;
2593}
2594
2595static int
2596compiler_tuple(struct compiler *c, expr_ty e)
2597{
2598 int n = asdl_seq_LEN(e->v.Tuple.elts);
2599 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002600 int i, seen_star = 0;
2601 for (i = 0; i < n; i++) {
2602 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2603 if (elt->kind == Starred_kind && !seen_star) {
2604 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2605 seen_star = 1;
2606 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2607 } else if (elt->kind == Starred_kind) {
2608 return compiler_error(c,
2609 "two starred expressions in assignment");
2610 }
2611 }
2612 if (!seen_star) {
2613 ADDOP_I(c, UNPACK_SEQUENCE, n);
2614 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 }
2616 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2617 if (e->v.Tuple.ctx == Load) {
2618 ADDOP_I(c, BUILD_TUPLE, n);
2619 }
2620 return 1;
2621}
2622
2623static int
2624compiler_compare(struct compiler *c, expr_ty e)
2625{
2626 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002627 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
2629 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2630 VISIT(c, expr, e->v.Compare.left);
2631 n = asdl_seq_LEN(e->v.Compare.ops);
2632 assert(n > 0);
2633 if (n > 1) {
2634 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002635 if (cleanup == NULL)
2636 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002637 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002638 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 }
2640 for (i = 1; i < n; i++) {
2641 ADDOP(c, DUP_TOP);
2642 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002644 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002645 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2647 NEXT_BLOCK(c);
2648 ADDOP(c, POP_TOP);
2649 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002650 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002651 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002653 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002655 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 if (n > 1) {
2657 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002658 if (end == NULL)
2659 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 ADDOP_JREL(c, JUMP_FORWARD, end);
2661 compiler_use_next_block(c, cleanup);
2662 ADDOP(c, ROT_TWO);
2663 ADDOP(c, POP_TOP);
2664 compiler_use_next_block(c, end);
2665 }
2666 return 1;
2667}
2668
2669static int
2670compiler_call(struct compiler *c, expr_ty e)
2671{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002673 return compiler_call_helper(c, 0,
2674 e->v.Call.args,
2675 e->v.Call.keywords,
2676 e->v.Call.starargs,
2677 e->v.Call.kwargs);
2678}
2679
2680/* shared code between compiler_call and compiler_class */
2681static int
2682compiler_call_helper(struct compiler *c,
2683 int n, /* Args already pushed */
2684 asdl_seq *args,
2685 asdl_seq *keywords,
2686 expr_ty starargs,
2687 expr_ty kwargs)
2688{
2689 int code = 0;
2690
2691 n += asdl_seq_LEN(args);
2692 VISIT_SEQ(c, expr, args);
2693 if (keywords) {
2694 VISIT_SEQ(c, keyword, keywords);
2695 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002697 if (starargs) {
2698 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 code |= 1;
2700 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002701 if (kwargs) {
2702 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 code |= 2;
2704 }
2705 switch (code) {
2706 case 0:
2707 ADDOP_I(c, CALL_FUNCTION, n);
2708 break;
2709 case 1:
2710 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2711 break;
2712 case 2:
2713 ADDOP_I(c, CALL_FUNCTION_KW, n);
2714 break;
2715 case 3:
2716 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2717 break;
2718 }
2719 return 1;
2720}
2721
Nick Coghlan650f0d02007-04-15 12:05:43 +00002722
2723/* List and set comprehensions and generator expressions work by creating a
2724 nested function to perform the actual iteration. This means that the
2725 iteration variables don't leak into the current scope.
2726 The defined function is called immediately following its definition, with the
2727 result of that call being the result of the expression.
2728 The LC/SC version returns the populated container, while the GE version is
2729 flagged in symtable.c as a generator, so it returns the generator object
2730 when the function is called.
2731 This code *knows* that the loop cannot contain break, continue, or return,
2732 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2733
2734 Possible cleanups:
2735 - iterate over the generator sequence instead of using recursion
2736*/
2737
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002739compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2740 asdl_seq *generators, int gen_index,
2741 expr_ty elt, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742{
2743 /* generate code for the iterator, then each of the ifs,
2744 and then write to the element */
2745
Nick Coghlan650f0d02007-04-15 12:05:43 +00002746 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002747 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002748 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
2750 start = compiler_new_block(c);
2751 skip = compiler_new_block(c);
2752 if_cleanup = compiler_new_block(c);
2753 anchor = compiler_new_block(c);
2754
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002755 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002756 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758
Nick Coghlan650f0d02007-04-15 12:05:43 +00002759 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 if (gen_index == 0) {
2762 /* Receive outermost iter as an implicit argument */
2763 c->u->u_argcount = 1;
2764 ADDOP_I(c, LOAD_FAST, 0);
2765 }
2766 else {
2767 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002768 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 ADDOP(c, GET_ITER);
2770 }
2771 compiler_use_next_block(c, start);
2772 ADDOP_JREL(c, FOR_ITER, anchor);
2773 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002774 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002776 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002777 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002779 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002780 VISIT(c, expr, e);
2781 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2782 NEXT_BLOCK(c);
2783 ADDOP(c, POP_TOP);
2784 }
2785
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002786 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002787 if (!compiler_comprehension_generator(c, tmpname,
2788 generators, gen_index,
2789 elt, type))
2790 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002793 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002794 /* comprehension specific code */
2795 switch (type) {
2796 case COMP_GENEXP:
2797 VISIT(c, expr, elt);
2798 ADDOP(c, YIELD_VALUE);
2799 ADDOP(c, POP_TOP);
2800 break;
2801 case COMP_LISTCOMP:
2802 if (!compiler_nameop(c, tmpname, Load))
2803 return 0;
2804 VISIT(c, expr, elt);
2805 ADDOP(c, LIST_APPEND);
2806 break;
2807 case COMP_SETCOMP:
2808 if (!compiler_nameop(c, tmpname, Load))
2809 return 0;
2810 VISIT(c, expr, elt);
2811 ADDOP(c, SET_ADD);
2812 break;
2813 default:
2814 return 0;
2815 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816
2817 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 for (i = 0; i < n; i++) {
2820 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002821 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002823
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002824 ADDOP(c, POP_TOP);
2825 }
2826 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2827 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828
2829 return 1;
2830}
2831
2832static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002833compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2834 asdl_seq *generators, expr_ty elt)
2835{
2836 PyCodeObject *co = NULL;
2837 identifier tmp = NULL;
2838 expr_ty outermost_iter;
2839
2840 outermost_iter = ((comprehension_ty)
2841 asdl_seq_GET(generators, 0))->iter;
2842
2843 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2844 goto error;
2845
2846 if (type != COMP_GENEXP) {
2847 tmp = compiler_new_tmpname(c);
2848 if (!tmp)
2849 goto error_in_scope;
2850
2851 ADDOP_I(c, (type == COMP_LISTCOMP ?
2852 BUILD_LIST : BUILD_SET), 0);
2853 ADDOP(c, DUP_TOP);
2854 if (!compiler_nameop(c, tmp, Store))
2855 goto error_in_scope;
2856 }
2857
2858 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt, type))
2859 goto error_in_scope;
2860
2861 if (type != COMP_GENEXP) {
2862 ADDOP(c, RETURN_VALUE);
2863 }
2864
2865 co = assemble(c, 1);
2866 compiler_exit_scope(c);
2867 if (co == NULL)
2868 goto error;
2869
2870 if (!compiler_make_closure(c, co, 0))
2871 goto error;
2872 Py_DECREF(co);
2873 Py_XDECREF(tmp);
2874
2875 VISIT(c, expr, outermost_iter);
2876 ADDOP(c, GET_ITER);
2877 ADDOP_I(c, CALL_FUNCTION, 1);
2878 return 1;
2879error_in_scope:
2880 compiler_exit_scope(c);
2881error:
2882 Py_XDECREF(co);
2883 Py_XDECREF(tmp);
2884 return 0;
2885}
2886
2887static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888compiler_genexp(struct compiler *c, expr_ty e)
2889{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002890 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002891 if (!name) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002892 name = PyString_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002893 if (!name)
2894 return 0;
2895 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002896 assert(e->kind == GeneratorExp_kind);
2897 return compiler_comprehension(c, e, COMP_GENEXP, name,
2898 e->v.GeneratorExp.generators,
2899 e->v.GeneratorExp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900}
2901
2902static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002903compiler_listcomp(struct compiler *c, expr_ty e)
2904{
2905 static identifier name;
2906 if (!name) {
2907 name = PyString_FromString("<listcomp>");
2908 if (!name)
2909 return 0;
2910 }
2911 assert(e->kind == ListComp_kind);
2912 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2913 e->v.ListComp.generators,
2914 e->v.ListComp.elt);
2915}
2916
2917static int
2918compiler_setcomp(struct compiler *c, expr_ty e)
2919{
2920 static identifier name;
2921 if (!name) {
2922 name = PyString_FromString("<setcomp>");
2923 if (!name)
2924 return 0;
2925 }
2926 assert(e->kind == SetComp_kind);
2927 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2928 e->v.SetComp.generators,
2929 e->v.SetComp.elt);
2930}
2931
2932
2933static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934compiler_visit_keyword(struct compiler *c, keyword_ty k)
2935{
2936 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2937 VISIT(c, expr, k->value);
2938 return 1;
2939}
2940
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002941/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002942 whether they are true or false.
2943
2944 Return values: 1 for true, 0 for false, -1 for non-constant.
2945 */
2946
2947static int
2948expr_constant(expr_ty e)
2949{
2950 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002951 case Ellipsis_kind:
2952 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953 case Num_kind:
2954 return PyObject_IsTrue(e->v.Num.n);
2955 case Str_kind:
2956 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002957 case Name_kind:
2958 /* __debug__ is not assignable, so we can optimize
2959 * it away in if and while statements */
2960 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Guido van Rossumd8faa362007-04-27 19:54:29 +00002961 "__debug__") == 0)
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002962 return ! Py_OptimizeFlag;
2963 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002964 default:
2965 return -1;
2966 }
2967}
2968
Guido van Rossumc2e20742006-02-27 22:32:47 +00002969/*
2970 Implements the with statement from PEP 343.
2971
2972 The semantics outlined in that PEP are as follows:
2973
2974 with EXPR as VAR:
2975 BLOCK
2976
2977 It is implemented roughly as:
2978
Thomas Wouters477c8d52006-05-27 19:21:47 +00002979 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002980 exit = context.__exit__ # not calling it
2981 value = context.__enter__()
2982 try:
2983 VAR = value # if VAR present in the syntax
2984 BLOCK
2985 finally:
2986 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002987 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002988 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002989 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990 exit(*exc)
2991 */
2992static int
2993compiler_with(struct compiler *c, stmt_ty s)
2994{
Thomas Wouters477c8d52006-05-27 19:21:47 +00002995 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002996 basicblock *block, *finally;
2997 identifier tmpexit, tmpvalue = NULL;
2998
2999 assert(s->kind == With_kind);
3000
Guido van Rossumc2e20742006-02-27 22:32:47 +00003001 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003002 enter_attr = PyString_InternFromString("__enter__");
3003 if (!enter_attr)
3004 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003005 }
3006 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003007 exit_attr = PyString_InternFromString("__exit__");
3008 if (!exit_attr)
3009 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003010 }
3011
3012 block = compiler_new_block(c);
3013 finally = compiler_new_block(c);
3014 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003015 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003016
3017 /* Create a temporary variable to hold context.__exit__ */
3018 tmpexit = compiler_new_tmpname(c);
3019 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003020 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003021 PyArena_AddPyObject(c->c_arena, tmpexit);
3022
3023 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003024 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003025 We need to do this rather than preserving it on the stack
3026 because SETUP_FINALLY remembers the stack level.
3027 We need to do the assignment *inside* the try/finally
3028 so that context.__exit__() is called when the assignment
3029 fails. But we need to call context.__enter__() *before*
3030 the try/finally so that if it fails we won't call
3031 context.__exit__().
3032 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003033 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003034 if (tmpvalue == NULL)
3035 return 0;
3036 PyArena_AddPyObject(c->c_arena, tmpvalue);
3037 }
3038
Thomas Wouters477c8d52006-05-27 19:21:47 +00003039 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003040 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003041
3042 /* Squirrel away context.__exit__ */
3043 ADDOP(c, DUP_TOP);
3044 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3045 if (!compiler_nameop(c, tmpexit, Store))
3046 return 0;
3047
3048 /* Call context.__enter__() */
3049 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3050 ADDOP_I(c, CALL_FUNCTION, 0);
3051
3052 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003053 /* Store it in tmpvalue */
3054 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003055 return 0;
3056 }
3057 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003058 /* Discard result from context.__enter__() */
3059 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003060 }
3061
3062 /* Start the try block */
3063 ADDOP_JREL(c, SETUP_FINALLY, finally);
3064
3065 compiler_use_next_block(c, block);
3066 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003067 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003068 }
3069
3070 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003071 /* Bind saved result of context.__enter__() to VAR */
3072 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 !compiler_nameop(c, tmpvalue, Del))
3074 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003075 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003076 }
3077
3078 /* BLOCK code */
3079 VISIT_SEQ(c, stmt, s->v.With.body);
3080
3081 /* End of try block; start the finally block */
3082 ADDOP(c, POP_BLOCK);
3083 compiler_pop_fblock(c, FINALLY_TRY, block);
3084
3085 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3086 compiler_use_next_block(c, finally);
3087 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003088 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089
3090 /* Finally block starts; push tmpexit and issue our magic opcode. */
3091 if (!compiler_nameop(c, tmpexit, Load) ||
3092 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003093 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003094 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003095
3096 /* Finally block ends. */
3097 ADDOP(c, END_FINALLY);
3098 compiler_pop_fblock(c, FINALLY_END, finally);
3099 return 1;
3100}
3101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102static int
3103compiler_visit_expr(struct compiler *c, expr_ty e)
3104{
3105 int i, n;
3106
Thomas Wouters89f507f2006-12-13 04:49:30 +00003107 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003108 set a new line number for the next instruction.
3109 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003110 if (e->lineno > c->u->u_lineno) {
3111 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003112 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 }
3114 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003115 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003117 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003118 VISIT(c, expr, e->v.BinOp.left);
3119 VISIT(c, expr, e->v.BinOp.right);
3120 ADDOP(c, binop(c, e->v.BinOp.op));
3121 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003122 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 VISIT(c, expr, e->v.UnaryOp.operand);
3124 ADDOP(c, unaryop(e->v.UnaryOp.op));
3125 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003126 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003128 case IfExp_kind:
3129 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003130 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 /* XXX get rid of arg? */
3132 ADDOP_I(c, BUILD_MAP, 0);
3133 n = asdl_seq_LEN(e->v.Dict.values);
3134 /* We must arrange things just right for STORE_SUBSCR.
3135 It wants the stack to look like (value) (dict) (key) */
3136 for (i = 0; i < n; i++) {
3137 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003138 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003139 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003141 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003142 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 ADDOP(c, STORE_SUBSCR);
3144 }
3145 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003146 case Set_kind:
3147 n = asdl_seq_LEN(e->v.Set.elts);
3148 VISIT_SEQ(c, expr, e->v.Set.elts);
3149 ADDOP_I(c, BUILD_SET, n);
3150 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003151 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003153 case ListComp_kind:
3154 return compiler_listcomp(c, e);
3155 case SetComp_kind:
3156 return compiler_setcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 case Yield_kind:
3158 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003159 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 if (e->v.Yield.value) {
3161 VISIT(c, expr, e->v.Yield.value);
3162 }
3163 else {
3164 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3165 }
3166 ADDOP(c, YIELD_VALUE);
3167 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003168 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003169 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003170 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003172 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3174 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003175 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3177 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003178 case Bytes_kind:
3179 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3180 ADDOP(c, MAKE_BYTES);
3181 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003182 case Ellipsis_kind:
3183 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3184 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003186 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187 if (e->v.Attribute.ctx != AugStore)
3188 VISIT(c, expr, e->v.Attribute.value);
3189 switch (e->v.Attribute.ctx) {
3190 case AugLoad:
3191 ADDOP(c, DUP_TOP);
3192 /* Fall through to load */
3193 case Load:
3194 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3195 break;
3196 case AugStore:
3197 ADDOP(c, ROT_TWO);
3198 /* Fall through to save */
3199 case Store:
3200 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3201 break;
3202 case Del:
3203 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3204 break;
3205 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003206 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003207 PyErr_SetString(PyExc_SystemError,
3208 "param invalid in attribute expression");
3209 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003210 }
3211 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003212 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 switch (e->v.Subscript.ctx) {
3214 case AugLoad:
3215 VISIT(c, expr, e->v.Subscript.value);
3216 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3217 break;
3218 case Load:
3219 VISIT(c, expr, e->v.Subscript.value);
3220 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3221 break;
3222 case AugStore:
3223 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3224 break;
3225 case Store:
3226 VISIT(c, expr, e->v.Subscript.value);
3227 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3228 break;
3229 case Del:
3230 VISIT(c, expr, e->v.Subscript.value);
3231 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3232 break;
3233 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003234 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003235 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003236 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003237 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 }
3239 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003240 case Starred_kind:
3241 switch (e->v.Starred.ctx) {
3242 case Store:
3243 /* In all legitimate cases, the Starred node was already replaced
3244 * by compiler_list/compiler_tuple. XXX: is that okay? */
3245 return compiler_error(c,
3246 "starred assignment target must be in a list or tuple");
3247 default:
3248 return compiler_error(c,
3249 "can use starred expression only as assignment target");
3250 }
3251 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003252 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3254 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003255 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003257 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 return compiler_tuple(c, e);
3259 }
3260 return 1;
3261}
3262
3263static int
3264compiler_augassign(struct compiler *c, stmt_ty s)
3265{
3266 expr_ty e = s->v.AugAssign.target;
3267 expr_ty auge;
3268
3269 assert(s->kind == AugAssign_kind);
3270
3271 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003272 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003273 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003274 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003275 if (auge == NULL)
3276 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 VISIT(c, expr, auge);
3278 VISIT(c, expr, s->v.AugAssign.value);
3279 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3280 auge->v.Attribute.ctx = AugStore;
3281 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 break;
3283 case Subscript_kind:
3284 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003285 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003286 if (auge == NULL)
3287 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 VISIT(c, expr, auge);
3289 VISIT(c, expr, s->v.AugAssign.value);
3290 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003291 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003293 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003295 if (!compiler_nameop(c, e->v.Name.id, Load))
3296 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 VISIT(c, expr, s->v.AugAssign.value);
3298 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3299 return compiler_nameop(c, e->v.Name.id, Store);
3300 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003301 PyErr_Format(PyExc_SystemError,
3302 "invalid node type (%d) for augmented assignment",
3303 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003304 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003305 }
3306 return 1;
3307}
3308
3309static int
3310compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3311{
3312 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003313 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3314 PyErr_SetString(PyExc_SystemError,
3315 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003317 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318 f = &c->u->u_fblock[c->u->u_nfblocks++];
3319 f->fb_type = t;
3320 f->fb_block = b;
3321 return 1;
3322}
3323
3324static void
3325compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3326{
3327 struct compiler_unit *u = c->u;
3328 assert(u->u_nfblocks > 0);
3329 u->u_nfblocks--;
3330 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3331 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3332}
3333
Thomas Wouters89f507f2006-12-13 04:49:30 +00003334static int
3335compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003336 int i;
3337 struct compiler_unit *u = c->u;
3338 for (i = 0; i < u->u_nfblocks; ++i) {
3339 if (u->u_fblock[i].fb_type == LOOP)
3340 return 1;
3341 }
3342 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003343}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344/* Raises a SyntaxError and returns 0.
3345 If something goes wrong, a different exception may be raised.
3346*/
3347
3348static int
3349compiler_error(struct compiler *c, const char *errstr)
3350{
3351 PyObject *loc;
3352 PyObject *u = NULL, *v = NULL;
3353
3354 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3355 if (!loc) {
3356 Py_INCREF(Py_None);
3357 loc = Py_None;
3358 }
3359 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3360 Py_None, loc);
3361 if (!u)
3362 goto exit;
3363 v = Py_BuildValue("(zO)", errstr, u);
3364 if (!v)
3365 goto exit;
3366 PyErr_SetObject(PyExc_SyntaxError, v);
3367 exit:
3368 Py_DECREF(loc);
3369 Py_XDECREF(u);
3370 Py_XDECREF(v);
3371 return 0;
3372}
3373
3374static int
3375compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003376 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003378 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003380 /* XXX this code is duplicated */
3381 switch (ctx) {
3382 case AugLoad: /* fall through to Load */
3383 case Load: op = BINARY_SUBSCR; break;
3384 case AugStore:/* fall through to Store */
3385 case Store: op = STORE_SUBSCR; break;
3386 case Del: op = DELETE_SUBSCR; break;
3387 case Param:
3388 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003389 "invalid %s kind %d in subscript\n",
3390 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003391 return 0;
3392 }
3393 if (ctx == AugLoad) {
3394 ADDOP_I(c, DUP_TOPX, 2);
3395 }
3396 else if (ctx == AugStore) {
3397 ADDOP(c, ROT_THREE);
3398 }
3399 ADDOP(c, op);
3400 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401}
3402
3403static int
3404compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3405{
3406 int n = 2;
3407 assert(s->kind == Slice_kind);
3408
3409 /* only handles the cases where BUILD_SLICE is emitted */
3410 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003411 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003412 }
3413 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003414 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003416
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003418 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419 }
3420 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003421 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422 }
3423
3424 if (s->v.Slice.step) {
3425 n++;
3426 VISIT(c, expr, s->v.Slice.step);
3427 }
3428 ADDOP_I(c, BUILD_SLICE, n);
3429 return 1;
3430}
3431
3432static int
3433compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3434{
3435 int op = 0, slice_offset = 0, stack_count = 0;
3436
3437 assert(s->v.Slice.step == NULL);
3438 if (s->v.Slice.lower) {
3439 slice_offset++;
3440 stack_count++;
3441 if (ctx != AugStore)
3442 VISIT(c, expr, s->v.Slice.lower);
3443 }
3444 if (s->v.Slice.upper) {
3445 slice_offset += 2;
3446 stack_count++;
3447 if (ctx != AugStore)
3448 VISIT(c, expr, s->v.Slice.upper);
3449 }
3450
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003451 if (ctx == AugLoad) {
3452 switch (stack_count) {
3453 case 0: ADDOP(c, DUP_TOP); break;
3454 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3455 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3456 }
3457 }
3458 else if (ctx == AugStore) {
3459 switch (stack_count) {
3460 case 0: ADDOP(c, ROT_TWO); break;
3461 case 1: ADDOP(c, ROT_THREE); break;
3462 case 2: ADDOP(c, ROT_FOUR); break;
3463 }
3464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465
3466 switch (ctx) {
3467 case AugLoad: /* fall through to Load */
3468 case Load: op = SLICE; break;
3469 case AugStore:/* fall through to Store */
3470 case Store: op = STORE_SLICE; break;
3471 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003472 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003473 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003474 PyErr_SetString(PyExc_SystemError,
3475 "param invalid in simple slice");
3476 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003477 }
3478
3479 ADDOP(c, op + slice_offset);
3480 return 1;
3481}
3482
3483static int
3484compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3485 expr_context_ty ctx)
3486{
3487 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 case Slice_kind:
3489 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 case Index_kind:
3491 VISIT(c, expr, s->v.Index.value);
3492 break;
3493 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003494 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003495 PyErr_SetString(PyExc_SystemError,
3496 "extended slice invalid in nested slice");
3497 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 }
3499 return 1;
3500}
3501
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502static int
3503compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3504{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003505 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003507 case Index_kind:
3508 kindname = "index";
3509 if (ctx != AugStore) {
3510 VISIT(c, expr, s->v.Index.value);
3511 }
3512 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003514 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515 if (!s->v.Slice.step)
3516 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003517 if (ctx != AugStore) {
3518 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 return 0;
3520 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003521 break;
3522 case ExtSlice_kind:
3523 kindname = "extended slice";
3524 if (ctx != AugStore) {
3525 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3526 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003527 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003528 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003529 if (!compiler_visit_nested_slice(c, sub, ctx))
3530 return 0;
3531 }
3532 ADDOP_I(c, BUILD_TUPLE, n);
3533 }
3534 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003535 default:
3536 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003537 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003538 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003540 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003541}
3542
Thomas Wouters89f507f2006-12-13 04:49:30 +00003543/* End of the compiler section, beginning of the assembler section */
3544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545/* do depth-first search of basic block graph, starting with block.
3546 post records the block indices in post-order.
3547
3548 XXX must handle implicit jumps from one block to next
3549*/
3550
Thomas Wouters89f507f2006-12-13 04:49:30 +00003551struct assembler {
3552 PyObject *a_bytecode; /* string containing bytecode */
3553 int a_offset; /* offset into bytecode */
3554 int a_nblocks; /* number of reachable blocks */
3555 basicblock **a_postorder; /* list of blocks in dfs postorder */
3556 PyObject *a_lnotab; /* string containing lnotab */
3557 int a_lnotab_off; /* offset into lnotab */
3558 int a_lineno; /* last lineno of emitted instruction */
3559 int a_lineno_off; /* bytecode offset of last lineno */
3560};
3561
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003562static void
3563dfs(struct compiler *c, basicblock *b, struct assembler *a)
3564{
3565 int i;
3566 struct instr *instr = NULL;
3567
3568 if (b->b_seen)
3569 return;
3570 b->b_seen = 1;
3571 if (b->b_next != NULL)
3572 dfs(c, b->b_next, a);
3573 for (i = 0; i < b->b_iused; i++) {
3574 instr = &b->b_instr[i];
3575 if (instr->i_jrel || instr->i_jabs)
3576 dfs(c, instr->i_target, a);
3577 }
3578 a->a_postorder[a->a_nblocks++] = b;
3579}
3580
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003581static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3583{
3584 int i;
3585 struct instr *instr;
3586 if (b->b_seen || b->b_startdepth >= depth)
3587 return maxdepth;
3588 b->b_seen = 1;
3589 b->b_startdepth = depth;
3590 for (i = 0; i < b->b_iused; i++) {
3591 instr = &b->b_instr[i];
3592 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3593 if (depth > maxdepth)
3594 maxdepth = depth;
3595 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3596 if (instr->i_jrel || instr->i_jabs) {
3597 maxdepth = stackdepth_walk(c, instr->i_target,
3598 depth, maxdepth);
3599 if (instr->i_opcode == JUMP_ABSOLUTE ||
3600 instr->i_opcode == JUMP_FORWARD) {
3601 goto out; /* remaining code is dead */
3602 }
3603 }
3604 }
3605 if (b->b_next)
3606 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3607out:
3608 b->b_seen = 0;
3609 return maxdepth;
3610}
3611
3612/* Find the flow path that needs the largest stack. We assume that
3613 * cycles in the flow graph have no net effect on the stack depth.
3614 */
3615static int
3616stackdepth(struct compiler *c)
3617{
3618 basicblock *b, *entryblock;
3619 entryblock = NULL;
3620 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3621 b->b_seen = 0;
3622 b->b_startdepth = INT_MIN;
3623 entryblock = b;
3624 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003625 if (!entryblock)
3626 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627 return stackdepth_walk(c, entryblock, 0, 0);
3628}
3629
3630static int
3631assemble_init(struct assembler *a, int nblocks, int firstlineno)
3632{
3633 memset(a, 0, sizeof(struct assembler));
3634 a->a_lineno = firstlineno;
3635 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3636 if (!a->a_bytecode)
3637 return 0;
3638 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3639 if (!a->a_lnotab)
3640 return 0;
3641 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003642 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003643 if (!a->a_postorder) {
3644 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003646 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 return 1;
3648}
3649
3650static void
3651assemble_free(struct assembler *a)
3652{
3653 Py_XDECREF(a->a_bytecode);
3654 Py_XDECREF(a->a_lnotab);
3655 if (a->a_postorder)
3656 PyObject_Free(a->a_postorder);
3657}
3658
3659/* Return the size of a basic block in bytes. */
3660
3661static int
3662instrsize(struct instr *instr)
3663{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003664 if (!instr->i_hasarg)
3665 return 1;
3666 if (instr->i_oparg > 0xffff)
3667 return 6;
3668 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669}
3670
3671static int
3672blocksize(basicblock *b)
3673{
3674 int i;
3675 int size = 0;
3676
3677 for (i = 0; i < b->b_iused; i++)
3678 size += instrsize(&b->b_instr[i]);
3679 return size;
3680}
3681
3682/* All about a_lnotab.
3683
3684c_lnotab is an array of unsigned bytes disguised as a Python string.
3685It is used to map bytecode offsets to source code line #s (when needed
3686for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003687
Tim Peters2a7f3842001-06-09 09:26:21 +00003688The array is conceptually a list of
3689 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003690pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003691
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003692 byte code offset source code line number
3693 0 1
3694 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003695 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003696 350 307
3697 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003698
3699The first trick is that these numbers aren't stored, only the increments
3700from one row to the next (this doesn't really work, but it's a start):
3701
3702 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3703
3704The second trick is that an unsigned byte can't hold negative values, or
3705values larger than 255, so (a) there's a deep assumption that byte code
3706offsets and their corresponding line #s both increase monotonically, and (b)
3707if at least one column jumps by more than 255 from one row to the next, more
3708than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003709from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003710part. A user of c_lnotab desiring to find the source line number
3711corresponding to a bytecode address A should do something like this
3712
3713 lineno = addr = 0
3714 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003715 addr += addr_incr
3716 if addr > A:
3717 return lineno
3718 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003719
3720In order for this to work, when the addr field increments by more than 255,
3721the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003722increment is < 256. So, in the example above, assemble_lnotab (it used
3723to be called com_set_lineno) should not (as was actually done until 2.2)
3724expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003725 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003726*/
3727
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003728static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003730{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 int d_bytecode, d_lineno;
3732 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003733 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734
3735 d_bytecode = a->a_offset - a->a_lineno_off;
3736 d_lineno = i->i_lineno - a->a_lineno;
3737
3738 assert(d_bytecode >= 0);
3739 assert(d_lineno >= 0);
3740
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003741 /* XXX(nnorwitz): is there a better way to handle this?
3742 for loops are special, we want to be able to trace them
3743 each time around, so we need to set an extra line number. */
3744 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003745 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003746
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003748 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 nbytes = a->a_lnotab_off + 2 * ncodes;
3750 len = PyString_GET_SIZE(a->a_lnotab);
3751 if (nbytes >= len) {
3752 if (len * 2 < nbytes)
3753 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003754 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 len *= 2;
3756 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3757 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003758 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003759 lnotab = (unsigned char *)
3760 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003761 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003762 *lnotab++ = 255;
3763 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003764 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 d_bytecode -= ncodes * 255;
3766 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003767 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003768 assert(d_bytecode <= 255);
3769 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003770 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 nbytes = a->a_lnotab_off + 2 * ncodes;
3772 len = PyString_GET_SIZE(a->a_lnotab);
3773 if (nbytes >= len) {
3774 if (len * 2 < nbytes)
3775 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003776 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 len *= 2;
3778 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3779 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003780 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003781 lnotab = (unsigned char *)
3782 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003784 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003786 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003787 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003788 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 d_lineno -= ncodes * 255;
3791 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003792 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003793
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 len = PyString_GET_SIZE(a->a_lnotab);
3795 if (a->a_lnotab_off + 2 >= len) {
3796 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003797 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003798 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003799 lnotab = (unsigned char *)
3800 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003801
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802 a->a_lnotab_off += 2;
3803 if (d_bytecode) {
3804 *lnotab++ = d_bytecode;
3805 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003806 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003807 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 *lnotab++ = 0;
3809 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003810 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003811 a->a_lineno = i->i_lineno;
3812 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003813 return 1;
3814}
3815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816/* assemble_emit()
3817 Extend the bytecode with a new instruction.
3818 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003819*/
3820
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003821static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003823{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003824 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003825 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 char *code;
3827
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003828 size = instrsize(i);
3829 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003831 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003834 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 if (a->a_offset + size >= len) {
3836 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003837 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003838 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3840 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003841 if (size == 6) {
3842 assert(i->i_hasarg);
3843 *code++ = (char)EXTENDED_ARG;
3844 *code++ = ext & 0xff;
3845 *code++ = ext >> 8;
3846 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003847 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003849 if (i->i_hasarg) {
3850 assert(size == 3 || size == 6);
3851 *code++ = arg & 0xff;
3852 *code++ = arg >> 8;
3853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003855}
3856
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003857static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003859{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003861 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003862 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864 /* Compute the size of each block and fixup jump args.
3865 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003866start:
3867 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003869 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 bsize = blocksize(b);
3871 b->b_offset = totsize;
3872 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003873 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003874 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3876 bsize = b->b_offset;
3877 for (i = 0; i < b->b_iused; i++) {
3878 struct instr *instr = &b->b_instr[i];
3879 /* Relative jumps are computed relative to
3880 the instruction pointer after fetching
3881 the jump instruction.
3882 */
3883 bsize += instrsize(instr);
3884 if (instr->i_jabs)
3885 instr->i_oparg = instr->i_target->b_offset;
3886 else if (instr->i_jrel) {
3887 int delta = instr->i_target->b_offset - bsize;
3888 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003889 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003890 else
3891 continue;
3892 if (instr->i_oparg > 0xffff)
3893 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003894 }
3895 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003896
3897 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003898 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003899 with a better solution.
3900
3901 In the meantime, should the goto be dropped in favor
3902 of a loop?
3903
3904 The issue is that in the first loop blocksize() is called
3905 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003906 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003907 i_oparg is calculated in the second loop above.
3908
3909 So we loop until we stop seeing new EXTENDED_ARGs.
3910 The only EXTENDED_ARGs that could be popping up are
3911 ones in jump instructions. So this should converge
3912 fairly quickly.
3913 */
3914 if (last_extended_arg_count != extended_arg_count) {
3915 last_extended_arg_count = extended_arg_count;
3916 goto start;
3917 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003918}
3919
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003920static PyObject *
3921dict_keys_inorder(PyObject *dict, int offset)
3922{
3923 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003924 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003925
3926 tuple = PyTuple_New(size);
3927 if (tuple == NULL)
3928 return NULL;
3929 while (PyDict_Next(dict, &pos, &k, &v)) {
3930 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003931 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003932 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003933 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003934 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003935 PyTuple_SET_ITEM(tuple, i - offset, k);
3936 }
3937 return tuple;
3938}
3939
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003940static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003942{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943 PySTEntryObject *ste = c->u->u_ste;
3944 int flags = 0, n;
3945 if (ste->ste_type != ModuleBlock)
3946 flags |= CO_NEWLOCALS;
3947 if (ste->ste_type == FunctionBlock) {
3948 if (!ste->ste_unoptimized)
3949 flags |= CO_OPTIMIZED;
3950 if (ste->ste_nested)
3951 flags |= CO_NESTED;
3952 if (ste->ste_generator)
3953 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003954 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 if (ste->ste_varargs)
3956 flags |= CO_VARARGS;
3957 if (ste->ste_varkeywords)
3958 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003959 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003961
3962 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003963 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003964
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 n = PyDict_Size(c->u->u_freevars);
3966 if (n < 0)
3967 return -1;
3968 if (n == 0) {
3969 n = PyDict_Size(c->u->u_cellvars);
3970 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003971 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 if (n == 0) {
3973 flags |= CO_NOFREE;
3974 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003975 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003976
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003977 return flags;
3978}
3979
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980static PyCodeObject *
3981makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003982{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 PyObject *tmp;
3984 PyCodeObject *co = NULL;
3985 PyObject *consts = NULL;
3986 PyObject *names = NULL;
3987 PyObject *varnames = NULL;
3988 PyObject *filename = NULL;
3989 PyObject *name = NULL;
3990 PyObject *freevars = NULL;
3991 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003992 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003994
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 tmp = dict_keys_inorder(c->u->u_consts, 0);
3996 if (!tmp)
3997 goto error;
3998 consts = PySequence_List(tmp); /* optimize_code requires a list */
3999 Py_DECREF(tmp);
4000
4001 names = dict_keys_inorder(c->u->u_names, 0);
4002 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4003 if (!consts || !names || !varnames)
4004 goto error;
4005
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004006 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4007 if (!cellvars)
4008 goto error;
4009 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4010 if (!freevars)
4011 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 filename = PyString_FromString(c->c_filename);
4013 if (!filename)
4014 goto error;
4015
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004016 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004017 flags = compute_code_flags(c);
4018 if (flags < 0)
4019 goto error;
4020
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004021 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022 if (!bytecode)
4023 goto error;
4024
4025 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4026 if (!tmp)
4027 goto error;
4028 Py_DECREF(consts);
4029 consts = tmp;
4030
Guido van Rossum4f72a782006-10-27 23:31:49 +00004031 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4032 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033 bytecode, consts, names, varnames,
4034 freevars, cellvars,
4035 filename, c->u->u_name,
4036 c->u->u_firstlineno,
4037 a->a_lnotab);
4038 error:
4039 Py_XDECREF(consts);
4040 Py_XDECREF(names);
4041 Py_XDECREF(varnames);
4042 Py_XDECREF(filename);
4043 Py_XDECREF(name);
4044 Py_XDECREF(freevars);
4045 Py_XDECREF(cellvars);
4046 Py_XDECREF(bytecode);
4047 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004048}
4049
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004050
4051/* For debugging purposes only */
4052#if 0
4053static void
4054dump_instr(const struct instr *i)
4055{
4056 const char *jrel = i->i_jrel ? "jrel " : "";
4057 const char *jabs = i->i_jabs ? "jabs " : "";
4058 char arg[128];
4059
4060 *arg = '\0';
4061 if (i->i_hasarg)
4062 sprintf(arg, "arg: %d ", i->i_oparg);
4063
4064 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4065 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4066}
4067
4068static void
4069dump_basicblock(const basicblock *b)
4070{
4071 const char *seen = b->b_seen ? "seen " : "";
4072 const char *b_return = b->b_return ? "return " : "";
4073 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4074 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4075 if (b->b_instr) {
4076 int i;
4077 for (i = 0; i < b->b_iused; i++) {
4078 fprintf(stderr, " [%02d] ", i);
4079 dump_instr(b->b_instr + i);
4080 }
4081 }
4082}
4083#endif
4084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085static PyCodeObject *
4086assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004087{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004088 basicblock *b, *entryblock;
4089 struct assembler a;
4090 int i, j, nblocks;
4091 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093 /* Make sure every block that falls off the end returns None.
4094 XXX NEXT_BLOCK() isn't quite right, because if the last
4095 block ends with a jump or return b_next shouldn't set.
4096 */
4097 if (!c->u->u_curblock->b_return) {
4098 NEXT_BLOCK(c);
4099 if (addNone)
4100 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4101 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004102 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104 nblocks = 0;
4105 entryblock = NULL;
4106 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4107 nblocks++;
4108 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004109 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004110
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004111 /* Set firstlineno if it wasn't explicitly set. */
4112 if (!c->u->u_firstlineno) {
4113 if (entryblock && entryblock->b_instr)
4114 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4115 else
4116 c->u->u_firstlineno = 1;
4117 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4119 goto error;
4120 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004123 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004124
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125 /* Emit code in reverse postorder from dfs. */
4126 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004127 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128 for (j = 0; j < b->b_iused; j++)
4129 if (!assemble_emit(&a, &b->b_instr[j]))
4130 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004131 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004133 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4134 goto error;
4135 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4136 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004137
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004138 co = makecode(c, &a);
4139 error:
4140 assemble_free(&a);
4141 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004142}