blob: f41312accadb632c683aa97ae92e36cea6231a96 [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.
Neal Norwitzf733a012006-10-29 18:30:10 +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
Jeremy Hylton819de6c2007-02-27 16:13:23 +000011 * this file.
Neal Norwitzf733a012006-10-29 18:30:10 +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
Neal Norwitzf733a012006-10-29 18:30:10 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000043 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000050};
51
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052typedef struct basicblock_ {
Jeremy Hylton12603c42006-04-01 16:18:02 +000053 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000067 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000069 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000073 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074} basicblock;
75
76/* fblockinfo tracks the current frame block.
77
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078A frame block is used to handle loops, try/except, and try/finally.
79It's called a frame block to distinguish it from a basic block in the
80compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081*/
82
83enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
84
85struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000086 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 basicblock *fb_block;
88};
89
90/* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
92*/
93struct compiler_unit {
94 PySTEntryObject *u_ste;
95
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +000098 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 the argument for opcodes that refer to those collections.
100 */
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
106
107 PyObject *u_private; /* for private name mangling */
108
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000109 int u_argcount; /* number of arguments for block */
Neal Norwitzf733a012006-10-29 18:30:10 +0000110 /* Pointer to the most recently allocated block. By following b_list
111 members, you can reach all early allocated blocks. */
Jeremy Hylton12603c42006-04-01 16:18:02 +0000112 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 basicblock *u_curblock; /* pointer to current block */
Neal Norwitzf733a012006-10-29 18:30:10 +0000114 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115
116 int u_nfblocks;
117 struct fblockinfo u_fblock[CO_MAXBLOCKS];
118
119 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000120 int u_lineno; /* the lineno for the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121 bool u_lineno_set; /* boolean to indicate whether instr
122 has been generated with current lineno */
123};
124
125/* This struct captures the global state of a compilation.
126
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000127The u pointer points to the current compilation unit, while units
128for enclosing blocks are stored in c_stack. The u and c_stack are
129managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000130*/
131
132struct compiler {
133 const char *c_filename;
134 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000135 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136 PyCompilerFlags *c_flags;
137
Neal Norwitz4ffedad2006-08-04 04:58:47 +0000138 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000139 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141 struct compiler_unit *u; /* compiler state for current block */
142 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145};
146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147static int compiler_enter_scope(struct compiler *, identifier, void *, int);
148static void compiler_free(struct compiler *);
149static basicblock *compiler_new_block(struct compiler *);
150static int compiler_next_instr(struct compiler *, basicblock *);
151static int compiler_addop(struct compiler *, int);
152static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
153static int compiler_addop_i(struct compiler *, int, int);
154static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000155static basicblock *compiler_use_new_block(struct compiler *);
156static int compiler_error(struct compiler *, const char *);
157static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
158
159static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
160static int compiler_visit_stmt(struct compiler *, stmt_ty);
161static int compiler_visit_keyword(struct compiler *, keyword_ty);
162static int compiler_visit_expr(struct compiler *, expr_ty);
163static int compiler_augassign(struct compiler *, stmt_ty);
164static int compiler_visit_slice(struct compiler *, slice_ty,
165 expr_context_ty);
166
167static int compiler_push_fblock(struct compiler *, enum fblocktype,
168 basicblock *);
169static void compiler_pop_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
Jeremy Hylton82271f12006-10-04 02:24:52 +0000171/* Returns true if there is a loop on the fblock stack. */
172static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173
174static int inplace_binop(struct compiler *, operator_ty);
175static int expr_constant(expr_ty e);
176
Guido van Rossumc2e20742006-02-27 22:32:47 +0000177static int compiler_with(struct compiler *, stmt_ty);
178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static PyCodeObject *assemble(struct compiler *, int addNone);
180static PyObject *__doc__;
181
182PyObject *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000183_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000184{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185 /* Name mangling: __private becomes _classname__private.
186 This is independent from how the name is used. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000187 const char *p, *name = PyString_AsString(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000188 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189 size_t nlen, plen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000190 if (privateobj == NULL || !PyString_Check(privateobj) ||
Neal Norwitz84167d02006-08-12 01:45:47 +0000191 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000192 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000194 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000195 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 nlen = strlen(name);
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000197 /* Don't mangle __id__ or names with dots.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000198
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000199 The only time a name with a dot can occur is when
200 we are compiling an import statement that has a
201 package name.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000202
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000203 TODO(jhylton): Decide whether we want to support
204 mangling of the module name, e.g. __M.X.
205 */
Jeremy Hylton37075c52007-02-27 01:01:59 +0000206 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000207 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 /* Strip leading underscores from class name */
212 while (*p == '_')
213 p++;
214 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000215 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 plen = strlen(p);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000219
220 assert(1 <= PY_SSIZE_T_MAX - nlen);
221 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
222
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000223 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000224 if (!ident)
225 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000227 buffer = PyString_AS_STRING(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 strncpy(buffer+1, p, plen);
230 strcpy(buffer+1+plen, name);
231 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000232}
233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234static int
235compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000236{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 c->c_stack = PyList_New(0);
240 if (!c->c_stack)
241 return 0;
242
243 return 1;
244}
245
246PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000248 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249{
250 struct compiler c;
251 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000252 PyCompilerFlags local_flags;
253 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000255 if (!__doc__) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000256 __doc__ = PyString_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000257 if (!__doc__)
258 return NULL;
259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260
261 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000262 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000264 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 c.c_future = PyFuture_FromAST(mod, filename);
266 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000267 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000269 local_flags.cf_flags = 0;
270 flags = &local_flags;
271 }
272 merged = c.c_future->ff_features | flags->cf_flags;
273 c.c_future->ff_features = merged;
274 flags->cf_flags = merged;
275 c.c_flags = flags;
276 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277
278 c.c_st = PySymtable_Build(mod, filename, c.c_future);
279 if (c.c_st == NULL) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000282 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 }
284
285 /* XXX initialize to NULL for now, need to handle */
286 c.c_encoding = NULL;
287
288 co = compiler_mod(&c, mod);
289
Thomas Wouters1175c432006-02-27 22:49:54 +0000290 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000292 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293 return co;
294}
295
296PyCodeObject *
297PyNode_Compile(struct _node *n, const char *filename)
298{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000299 PyCodeObject *co = NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000300 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000301 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000302 if (!arena)
303 return NULL;
304 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000305 if (mod)
306 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000307 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000308 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000309}
310
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000311static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000313{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 if (c->c_st)
315 PySymtable_Free(c->c_st);
316 if (c->c_future)
Neal Norwitz14bc4e42006-04-10 06:57:06 +0000317 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319}
320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000323{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000324 Py_ssize_t i, n;
Georg Brandl5c170fd2006-03-17 19:03:25 +0000325 PyObject *v, *k;
326 PyObject *dict = PyDict_New();
327 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000328
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329 n = PyList_Size(list);
330 for (i = 0; i < n; i++) {
331 v = PyInt_FromLong(i);
332 if (!v) {
333 Py_DECREF(dict);
334 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000335 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000336 k = PyList_GET_ITEM(list, i);
Georg Brandl7784f122006-05-26 20:04:44 +0000337 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
339 Py_XDECREF(k);
340 Py_DECREF(v);
341 Py_DECREF(dict);
342 return NULL;
343 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000344 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000346 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 return dict;
348}
349
350/* Return new dict containing names from src that match scope(s).
351
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000352src is a symbol table dictionary. If the scope of a name matches
353either scope_type or flag is set, insert it into the new dict. The
354values are integers, starting at offset and increasing by one for
355each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356*/
357
358static PyObject *
359dictbytype(PyObject *src, int scope_type, int flag, int offset)
360{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000361 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362 PyObject *k, *v, *dest = PyDict_New();
363
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000364 assert(offset >= 0);
365 if (dest == NULL)
366 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367
368 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000369 /* XXX this should probably be a macro in symtable.h */
370 assert(PyInt_Check(v));
371 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000373 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
374 PyObject *tuple, *item = PyInt_FromLong(i);
375 if (item == NULL) {
376 Py_DECREF(dest);
377 return NULL;
378 }
379 i++;
Georg Brandl7784f122006-05-26 20:04:44 +0000380 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000381 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
382 Py_DECREF(item);
383 Py_DECREF(dest);
384 Py_XDECREF(tuple);
385 return NULL;
386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000388 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390 }
391 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000392}
393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394static void
395compiler_unit_check(struct compiler_unit *u)
396{
397 basicblock *block;
398 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Brett Cannona6c41bc2008-02-07 07:47:31 +0000399 assert((void *)block != (void *)0xcbcbcbcb);
400 assert((void *)block != (void *)0xfbfbfbfb);
401 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402 if (block->b_instr != NULL) {
403 assert(block->b_ialloc > 0);
404 assert(block->b_iused > 0);
405 assert(block->b_ialloc >= block->b_iused);
406 }
407 else {
408 assert (block->b_iused == 0);
409 assert (block->b_ialloc == 0);
410 }
411 }
412}
413
414static void
415compiler_unit_free(struct compiler_unit *u)
416{
417 basicblock *b, *next;
418
419 compiler_unit_check(u);
420 b = u->u_blocks;
421 while (b != NULL) {
422 if (b->b_instr)
423 PyObject_Free((void *)b->b_instr);
424 next = b->b_list;
425 PyObject_Free((void *)b);
426 b = next;
427 }
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000428 Py_CLEAR(u->u_ste);
429 Py_CLEAR(u->u_name);
430 Py_CLEAR(u->u_consts);
431 Py_CLEAR(u->u_names);
432 Py_CLEAR(u->u_varnames);
433 Py_CLEAR(u->u_freevars);
434 Py_CLEAR(u->u_cellvars);
435 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 PyObject_Free(u);
437}
438
439static int
440compiler_enter_scope(struct compiler *c, identifier name, void *key,
441 int lineno)
442{
443 struct compiler_unit *u;
444
Anthony Baxter7b782b62006-04-11 12:01:56 +0000445 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000446 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000447 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000448 PyErr_NoMemory();
449 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000450 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000451 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 u->u_argcount = 0;
453 u->u_ste = PySymtable_Lookup(c->c_st, key);
454 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000455 compiler_unit_free(u);
456 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457 }
458 Py_INCREF(name);
459 u->u_name = name;
460 u->u_varnames = list2dict(u->u_ste->ste_varnames);
461 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000462 if (!u->u_varnames || !u->u_cellvars) {
463 compiler_unit_free(u);
464 return 0;
465 }
466
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000468 PyDict_Size(u->u_cellvars));
Neal Norwitzd12bd012006-07-21 07:59:47 +0000469 if (!u->u_freevars) {
470 compiler_unit_free(u);
471 return 0;
472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
474 u->u_blocks = NULL;
475 u->u_tmpname = 0;
476 u->u_nfblocks = 0;
477 u->u_firstlineno = lineno;
478 u->u_lineno = 0;
479 u->u_lineno_set = false;
480 u->u_consts = PyDict_New();
481 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000482 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 return 0;
484 }
485 u->u_names = PyDict_New();
486 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000487 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 return 0;
489 }
490
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000491 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
493 /* Push the old compiler_unit on the stack. */
494 if (c->u) {
495 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000496 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
497 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 return 0;
500 }
501 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000502 u->u_private = c->u->u_private;
503 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 }
505 c->u = u;
506
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000507 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000508 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 return 0;
510
511 return 1;
512}
513
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000514static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515compiler_exit_scope(struct compiler *c)
516{
517 int n;
518 PyObject *wrapper;
519
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000520 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 compiler_unit_free(c->u);
522 /* Restore c->u to the parent unit. */
523 n = PyList_GET_SIZE(c->c_stack) - 1;
524 if (n >= 0) {
525 wrapper = PyList_GET_ITEM(c->c_stack, n);
526 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neal Norwitz87557cd2006-08-21 18:01:30 +0000527 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000528 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000530 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 compiler_unit_check(c->u);
532 }
533 else
534 c->u = NULL;
535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536}
537
Guido van Rossumc2e20742006-02-27 22:32:47 +0000538/* Allocate a new "anonymous" local variable.
539 Used by list comprehensions and with statements.
540*/
541
542static PyObject *
543compiler_new_tmpname(struct compiler *c)
544{
545 char tmpname[256];
546 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000547 return PyString_FromString(tmpname);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000548}
549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550/* Allocate a new block and return a pointer to it.
551 Returns NULL on error.
552*/
553
554static basicblock *
555compiler_new_block(struct compiler *c)
556{
557 basicblock *b;
558 struct compiler_unit *u;
559
560 u = c->u;
561 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000562 if (b == NULL) {
563 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000565 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 memset((void *)b, 0, sizeof(basicblock));
Neal Norwitzf733a012006-10-29 18:30:10 +0000567 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 b->b_list = u->u_blocks;
569 u->u_blocks = b;
570 return b;
571}
572
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573static basicblock *
574compiler_use_new_block(struct compiler *c)
575{
576 basicblock *block = compiler_new_block(c);
577 if (block == NULL)
578 return NULL;
579 c->u->u_curblock = block;
580 return block;
581}
582
583static basicblock *
584compiler_next_block(struct compiler *c)
585{
586 basicblock *block = compiler_new_block(c);
587 if (block == NULL)
588 return NULL;
589 c->u->u_curblock->b_next = block;
590 c->u->u_curblock = block;
591 return block;
592}
593
594static basicblock *
595compiler_use_next_block(struct compiler *c, basicblock *block)
596{
597 assert(block != NULL);
598 c->u->u_curblock->b_next = block;
599 c->u->u_curblock = block;
600 return block;
601}
602
603/* Returns the offset of the next instruction in the current block's
604 b_instr array. Resizes the b_instr as necessary.
605 Returns -1 on failure.
Neal Norwitzf733a012006-10-29 18:30:10 +0000606*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
608static int
609compiler_next_instr(struct compiler *c, basicblock *b)
610{
611 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000612 if (b->b_instr == NULL) {
Anthony Baxter7b782b62006-04-11 12:01:56 +0000613 b->b_instr = (struct instr *)PyObject_Malloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000614 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 if (b->b_instr == NULL) {
616 PyErr_NoMemory();
617 return -1;
618 }
619 b->b_ialloc = DEFAULT_BLOCK_SIZE;
620 memset((char *)b->b_instr, 0,
621 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 else if (b->b_iused == b->b_ialloc) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000624 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 size_t oldsize, newsize;
626 oldsize = b->b_ialloc * sizeof(struct instr);
627 newsize = oldsize << 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000628
629 if (oldsize > (PY_SIZE_MAX >> 1)) {
630 PyErr_NoMemory();
631 return -1;
632 }
633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 if (newsize == 0) {
635 PyErr_NoMemory();
636 return -1;
637 }
638 b->b_ialloc <<= 1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000639 tmp = (struct instr *)PyObject_Realloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000640 (void *)b->b_instr, newsize);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000641 if (tmp == NULL) {
642 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 return -1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000644 }
645 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
647 }
648 return b->b_iused++;
649}
650
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000651/* Set the i_lineno member of the instruction at offset off if the
652 line number for the current expression/statement has not
Jeremy Hylton12603c42006-04-01 16:18:02 +0000653 already been set. If it has been set, the call has no effect.
654
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000655 The line number is reset in the following cases:
656 - when entering a new scope
657 - on each statement
658 - on each expression that start a new line
659 - before the "except" clause
660 - before the "for" and "while" expressions
Neal Norwitzf733a012006-10-29 18:30:10 +0000661*/
Jeremy Hylton12603c42006-04-01 16:18:02 +0000662
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663static void
664compiler_set_lineno(struct compiler *c, int off)
665{
666 basicblock *b;
667 if (c->u->u_lineno_set)
668 return;
669 c->u->u_lineno_set = true;
670 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000671 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672}
673
674static int
675opcode_stack_effect(int opcode, int oparg)
676{
677 switch (opcode) {
678 case POP_TOP:
679 return -1;
680 case ROT_TWO:
681 case ROT_THREE:
682 return 0;
683 case DUP_TOP:
684 return 1;
685 case ROT_FOUR:
686 return 0;
687
688 case UNARY_POSITIVE:
689 case UNARY_NEGATIVE:
690 case UNARY_NOT:
691 case UNARY_CONVERT:
692 case UNARY_INVERT:
693 return 0;
694
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000695 case LIST_APPEND:
696 return -2;
697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 case BINARY_POWER:
699 case BINARY_MULTIPLY:
700 case BINARY_DIVIDE:
701 case BINARY_MODULO:
702 case BINARY_ADD:
703 case BINARY_SUBTRACT:
704 case BINARY_SUBSCR:
705 case BINARY_FLOOR_DIVIDE:
706 case BINARY_TRUE_DIVIDE:
707 return -1;
708 case INPLACE_FLOOR_DIVIDE:
709 case INPLACE_TRUE_DIVIDE:
710 return -1;
711
712 case SLICE+0:
713 return 1;
714 case SLICE+1:
715 return 0;
716 case SLICE+2:
717 return 0;
718 case SLICE+3:
719 return -1;
720
721 case STORE_SLICE+0:
722 return -2;
723 case STORE_SLICE+1:
724 return -3;
725 case STORE_SLICE+2:
726 return -3;
727 case STORE_SLICE+3:
728 return -4;
729
730 case DELETE_SLICE+0:
731 return -1;
732 case DELETE_SLICE+1:
733 return -2;
734 case DELETE_SLICE+2:
735 return -2;
736 case DELETE_SLICE+3:
737 return -3;
738
739 case INPLACE_ADD:
740 case INPLACE_SUBTRACT:
741 case INPLACE_MULTIPLY:
742 case INPLACE_DIVIDE:
743 case INPLACE_MODULO:
744 return -1;
745 case STORE_SUBSCR:
746 return -3;
Raymond Hettingereffde122007-12-18 18:26:18 +0000747 case STORE_MAP:
748 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 case DELETE_SUBSCR:
750 return -2;
751
752 case BINARY_LSHIFT:
753 case BINARY_RSHIFT:
754 case BINARY_AND:
755 case BINARY_XOR:
756 case BINARY_OR:
757 return -1;
758 case INPLACE_POWER:
759 return -1;
760 case GET_ITER:
761 return 0;
762
763 case PRINT_EXPR:
764 return -1;
765 case PRINT_ITEM:
766 return -1;
767 case PRINT_NEWLINE:
768 return 0;
769 case PRINT_ITEM_TO:
770 return -2;
771 case PRINT_NEWLINE_TO:
772 return -1;
773 case INPLACE_LSHIFT:
774 case INPLACE_RSHIFT:
775 case INPLACE_AND:
776 case INPLACE_XOR:
777 case INPLACE_OR:
778 return -1;
779 case BREAK_LOOP:
780 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000781 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000782 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 case LOAD_LOCALS:
784 return 1;
785 case RETURN_VALUE:
786 return -1;
787 case IMPORT_STAR:
788 return -1;
789 case EXEC_STMT:
790 return -3;
791 case YIELD_VALUE:
792 return 0;
793
794 case POP_BLOCK:
795 return 0;
796 case END_FINALLY:
797 return -1; /* or -2 or -3 if exception occurred */
798 case BUILD_CLASS:
799 return -2;
800
801 case STORE_NAME:
802 return -1;
803 case DELETE_NAME:
804 return 0;
805 case UNPACK_SEQUENCE:
806 return oparg-1;
807 case FOR_ITER:
808 return 1;
809
810 case STORE_ATTR:
811 return -2;
812 case DELETE_ATTR:
813 return -1;
814 case STORE_GLOBAL:
815 return -1;
816 case DELETE_GLOBAL:
817 return 0;
818 case DUP_TOPX:
819 return oparg;
820 case LOAD_CONST:
821 return 1;
822 case LOAD_NAME:
823 return 1;
824 case BUILD_TUPLE:
825 case BUILD_LIST:
826 return 1-oparg;
827 case BUILD_MAP:
828 return 1;
829 case LOAD_ATTR:
830 return 0;
831 case COMPARE_OP:
832 return -1;
833 case IMPORT_NAME:
834 return 0;
835 case IMPORT_FROM:
836 return 1;
837
838 case JUMP_FORWARD:
839 case JUMP_IF_FALSE:
840 case JUMP_IF_TRUE:
841 case JUMP_ABSOLUTE:
842 return 0;
843
844 case LOAD_GLOBAL:
845 return 1;
846
847 case CONTINUE_LOOP:
848 return 0;
849 case SETUP_LOOP:
850 return 0;
851 case SETUP_EXCEPT:
852 case SETUP_FINALLY:
853 return 3; /* actually pushed by an exception */
854
855 case LOAD_FAST:
856 return 1;
857 case STORE_FAST:
858 return -1;
859 case DELETE_FAST:
860 return 0;
861
862 case RAISE_VARARGS:
863 return -oparg;
864#define NARGS(o) (((o) % 256) + 2*((o) / 256))
865 case CALL_FUNCTION:
866 return -NARGS(oparg);
867 case CALL_FUNCTION_VAR:
868 case CALL_FUNCTION_KW:
869 return -NARGS(oparg)-1;
870 case CALL_FUNCTION_VAR_KW:
871 return -NARGS(oparg)-2;
872#undef NARGS
873 case MAKE_FUNCTION:
874 return -oparg;
875 case BUILD_SLICE:
876 if (oparg == 3)
877 return -2;
878 else
879 return -1;
880
881 case MAKE_CLOSURE:
882 return -oparg;
883 case LOAD_CLOSURE:
884 return 1;
885 case LOAD_DEREF:
886 return 1;
887 case STORE_DEREF:
888 return -1;
889 default:
890 fprintf(stderr, "opcode = %d\n", opcode);
891 Py_FatalError("opcode_stack_effect()");
892
893 }
894 return 0; /* not reachable */
895}
896
897/* Add an opcode with no argument.
898 Returns 0 on failure, 1 on success.
899*/
900
901static int
902compiler_addop(struct compiler *c, int opcode)
903{
904 basicblock *b;
905 struct instr *i;
906 int off;
907 off = compiler_next_instr(c, c->u->u_curblock);
908 if (off < 0)
909 return 0;
910 b = c->u->u_curblock;
911 i = &b->b_instr[off];
912 i->i_opcode = opcode;
913 i->i_hasarg = 0;
914 if (opcode == RETURN_VALUE)
915 b->b_return = 1;
916 compiler_set_lineno(c, off);
917 return 1;
918}
919
920static int
921compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
922{
923 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000924 Py_ssize_t arg;
Mark Dickinson105be772008-01-31 22:17:37 +0000925 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000927 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000928 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
929 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000930 d = PyFloat_AS_DOUBLE(o);
Mark Dickinson105be772008-01-31 22:17:37 +0000931 /* all we need is to make the tuple different in either the 0.0
932 * or -0.0 case from all others, just to avoid the "coercion".
933 */
Mark Dickinson48a522d2009-11-28 16:37:36 +0000934 if (d == 0.0 && copysign(1.0, d) < 0.0)
Mark Dickinson105be772008-01-31 22:17:37 +0000935 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
936 else
937 t = PyTuple_Pack(2, o, o->ob_type);
938 }
Mark Dickinsona47460a2009-10-27 17:26:31 +0000939#ifndef WITHOUT_COMPLEX
Mark Dickinson105be772008-01-31 22:17:37 +0000940 else if (PyComplex_Check(o)) {
Mark Dickinsona47460a2009-10-27 17:26:31 +0000941 Py_complex z;
Mark Dickinson48a522d2009-11-28 16:37:36 +0000942 int real_negzero, imag_negzero;
943 /* For the complex case we must make complex(x, 0.)
944 different from complex(x, -0.) and complex(0., y)
945 different from complex(-0., y), for any x and y.
946 All four complex zeros must be distinguished.*/
Mark Dickinson105be772008-01-31 22:17:37 +0000947 z = PyComplex_AsCComplex(o);
Mark Dickinson48a522d2009-11-28 16:37:36 +0000948 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
949 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
950 if (real_negzero && imag_negzero) {
951 t = PyTuple_Pack(5, o, o->ob_type,
952 Py_None, Py_None, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000953 }
Mark Dickinson48a522d2009-11-28 16:37:36 +0000954 else if (imag_negzero) {
955 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000956 }
Mark Dickinson48a522d2009-11-28 16:37:36 +0000957 else if (real_negzero) {
958 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000959 }
960 else {
961 t = PyTuple_Pack(2, o, o->ob_type);
962 }
963 }
Mark Dickinsona47460a2009-10-27 17:26:31 +0000964#endif /* WITHOUT_COMPLEX */
Mark Dickinson105be772008-01-31 22:17:37 +0000965 else {
966 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000967 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000968 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000969 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970
971 v = PyDict_GetItem(dict, t);
972 if (!v) {
973 arg = PyDict_Size(dict);
974 v = PyInt_FromLong(arg);
975 if (!v) {
976 Py_DECREF(t);
977 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000978 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979 if (PyDict_SetItem(dict, t, v) < 0) {
980 Py_DECREF(t);
981 Py_DECREF(v);
982 return -1;
983 }
984 Py_DECREF(v);
985 }
986 else
987 arg = PyInt_AsLong(v);
988 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000989 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990}
991
992static int
993compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
994 PyObject *o)
995{
996 int arg = compiler_add_o(c, dict, o);
997 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000998 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 return compiler_addop_i(c, opcode, arg);
1000}
1001
1002static int
1003compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001004 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005{
1006 int arg;
1007 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1008 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 arg = compiler_add_o(c, dict, mangled);
1011 Py_DECREF(mangled);
1012 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001013 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014 return compiler_addop_i(c, opcode, arg);
1015}
1016
1017/* Add an opcode with an integer argument.
1018 Returns 0 on failure, 1 on success.
1019*/
1020
1021static int
1022compiler_addop_i(struct compiler *c, int opcode, int oparg)
1023{
1024 struct instr *i;
1025 int off;
1026 off = compiler_next_instr(c, c->u->u_curblock);
1027 if (off < 0)
1028 return 0;
1029 i = &c->u->u_curblock->b_instr[off];
1030 i->i_opcode = opcode;
1031 i->i_oparg = oparg;
1032 i->i_hasarg = 1;
1033 compiler_set_lineno(c, off);
1034 return 1;
1035}
1036
1037static int
1038compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1039{
1040 struct instr *i;
1041 int off;
1042
1043 assert(b != NULL);
1044 off = compiler_next_instr(c, c->u->u_curblock);
1045 if (off < 0)
1046 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 i = &c->u->u_curblock->b_instr[off];
1048 i->i_opcode = opcode;
1049 i->i_target = b;
1050 i->i_hasarg = 1;
1051 if (absolute)
1052 i->i_jabs = 1;
1053 else
1054 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001055 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056 return 1;
1057}
1058
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001059/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1060 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061 it as the current block. NEXT_BLOCK() also creates an implicit jump
1062 from the current block to the new block.
1063*/
1064
Neal Norwitzf733a012006-10-29 18:30:10 +00001065/* The returns inside these macros make it impossible to decref objects
1066 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067*/
1068
1069
1070#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001071 if (compiler_use_new_block((C)) == NULL) \
1072 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
1074
1075#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001076 if (compiler_next_block((C)) == NULL) \
1077 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078}
1079
1080#define ADDOP(C, OP) { \
1081 if (!compiler_addop((C), (OP))) \
1082 return 0; \
1083}
1084
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001085#define ADDOP_IN_SCOPE(C, OP) { \
1086 if (!compiler_addop((C), (OP))) { \
1087 compiler_exit_scope(c); \
1088 return 0; \
1089 } \
1090}
1091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092#define ADDOP_O(C, OP, O, TYPE) { \
1093 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1094 return 0; \
1095}
1096
1097#define ADDOP_NAME(C, OP, O, TYPE) { \
1098 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1099 return 0; \
1100}
1101
1102#define ADDOP_I(C, OP, O) { \
1103 if (!compiler_addop_i((C), (OP), (O))) \
1104 return 0; \
1105}
1106
1107#define ADDOP_JABS(C, OP, O) { \
1108 if (!compiler_addop_j((C), (OP), (O), 1)) \
1109 return 0; \
1110}
1111
1112#define ADDOP_JREL(C, OP, O) { \
1113 if (!compiler_addop_j((C), (OP), (O), 0)) \
1114 return 0; \
1115}
1116
1117/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1118 the ASDL name to synthesize the name of the C type and the visit function.
1119*/
1120
1121#define VISIT(C, TYPE, V) {\
1122 if (!compiler_visit_ ## TYPE((C), (V))) \
1123 return 0; \
1124}
1125
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001126#define VISIT_IN_SCOPE(C, TYPE, V) {\
1127 if (!compiler_visit_ ## TYPE((C), (V))) { \
1128 compiler_exit_scope(c); \
1129 return 0; \
1130 } \
1131}
1132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133#define VISIT_SLICE(C, V, CTX) {\
1134 if (!compiler_visit_slice((C), (V), (CTX))) \
1135 return 0; \
1136}
1137
1138#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001139 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001141 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001142 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001143 if (!compiler_visit_ ## TYPE((C), elt)) \
1144 return 0; \
1145 } \
1146}
1147
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001148#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001149 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001150 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001151 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001152 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001153 if (!compiler_visit_ ## TYPE((C), elt)) { \
1154 compiler_exit_scope(c); \
1155 return 0; \
1156 } \
1157 } \
1158}
1159
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160static int
1161compiler_isdocstring(stmt_ty s)
1162{
1163 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001164 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165 return s->v.Expr.value->kind == Str_kind;
1166}
1167
1168/* Compile a sequence of statements, checking for a docstring. */
1169
1170static int
1171compiler_body(struct compiler *c, asdl_seq *stmts)
1172{
1173 int i = 0;
1174 stmt_ty st;
1175
1176 if (!asdl_seq_LEN(stmts))
1177 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001178 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001179 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1180 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181 i = 1;
1182 VISIT(c, expr, st->v.Expr.value);
1183 if (!compiler_nameop(c, __doc__, Store))
1184 return 0;
1185 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001186 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001187 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 return 1;
1189}
1190
1191static PyCodeObject *
1192compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001193{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001194 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001195 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 static PyObject *module;
1197 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001198 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 if (!module)
1200 return NULL;
1201 }
Neal Norwitzed657552006-07-10 00:04:44 +00001202 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1203 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001204 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 switch (mod->kind) {
1206 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001207 if (!compiler_body(c, mod->v.Module.body)) {
1208 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 break;
1212 case Interactive_kind:
1213 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001214 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001215 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 break;
1217 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001218 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001219 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 break;
1221 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001222 PyErr_SetString(PyExc_SystemError,
1223 "suite should not be possible");
1224 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001225 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001226 PyErr_Format(PyExc_SystemError,
1227 "module kind %d should not be possible",
1228 mod->kind);
1229 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001230 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 co = assemble(c, addNone);
1232 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001233 return co;
1234}
1235
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001236/* The test for LOCAL must come before the test for FREE in order to
1237 handle classes where name is both local and free. The local var is
1238 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001239*/
1240
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241static int
1242get_ref_type(struct compiler *c, PyObject *name)
1243{
1244 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001245 if (scope == 0) {
1246 char buf[350];
1247 PyOS_snprintf(buf, sizeof(buf),
1248 "unknown scope for %.100s in %.100s(%s) in %s\n"
1249 "symbols: %s\nlocals: %s\nglobals: %s\n",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001250 PyString_AS_STRING(name),
1251 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001252 PyObject_REPR(c->u->u_ste->ste_id),
1253 c->c_filename,
1254 PyObject_REPR(c->u->u_ste->ste_symbols),
1255 PyObject_REPR(c->u->u_varnames),
1256 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001258 Py_FatalError(buf);
1259 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001260
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001261 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264static int
1265compiler_lookup_arg(PyObject *dict, PyObject *name)
1266{
1267 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001268 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001270 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001272 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001274 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275 return PyInt_AS_LONG(v);
1276}
1277
1278static int
1279compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1280{
1281 int i, free = PyCode_GetNumFree(co);
1282 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001283 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1284 ADDOP_I(c, MAKE_FUNCTION, args);
1285 return 1;
1286 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001287 for (i = 0; i < free; ++i) {
1288 /* Bypass com_addop_varname because it will generate
1289 LOAD_DEREF but LOAD_CLOSURE is needed.
1290 */
1291 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1292 int arg, reftype;
1293
1294 /* Special case: If a class contains a method with a
1295 free variable that has the same name as a method,
1296 the name will be considered free *and* local in the
1297 class. It should be handled by the closure, as
1298 well as by the normal name loookup logic.
1299 */
1300 reftype = get_ref_type(c, name);
1301 if (reftype == CELL)
1302 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1303 else /* (reftype == FREE) */
1304 arg = compiler_lookup_arg(c->u->u_freevars, name);
1305 if (arg == -1) {
1306 printf("lookup %s in %s %d %d\n"
1307 "freevars of %s: %s\n",
1308 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001309 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001311 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 PyObject_REPR(co->co_freevars));
1313 Py_FatalError("compiler_make_closure()");
1314 }
1315 ADDOP_I(c, LOAD_CLOSURE, arg);
1316 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001317 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001319 ADDOP_I(c, MAKE_CLOSURE, args);
1320 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321}
1322
1323static int
1324compiler_decorators(struct compiler *c, asdl_seq* decos)
1325{
1326 int i;
1327
1328 if (!decos)
1329 return 1;
1330
1331 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001332 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 }
1334 return 1;
1335}
1336
1337static int
1338compiler_arguments(struct compiler *c, arguments_ty args)
1339{
1340 int i;
1341 int n = asdl_seq_LEN(args->args);
1342 /* Correctly handle nested argument lists */
1343 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001344 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001346 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001347 if (id == NULL) {
1348 return 0;
1349 }
1350 if (!compiler_nameop(c, id, Load)) {
1351 Py_DECREF(id);
1352 return 0;
1353 }
1354 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001355 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 }
1357 }
1358 return 1;
1359}
1360
1361static int
1362compiler_function(struct compiler *c, stmt_ty s)
1363{
1364 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001365 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001367 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001368 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369 int i, n, docstring;
1370
1371 assert(s->kind == FunctionDef_kind);
1372
1373 if (!compiler_decorators(c, decos))
1374 return 0;
1375 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001376 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1378 s->lineno))
1379 return 0;
1380
Anthony Baxter7b782b62006-04-11 12:01:56 +00001381 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001382 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001383 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001384 first_const = st->v.Expr.value->v.Str.s;
1385 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001386 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001387 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001388 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001390 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 compiler_arguments(c, args);
1392
1393 c->u->u_argcount = asdl_seq_LEN(args->args);
1394 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001395 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001397 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1398 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 }
1400 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001401 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 if (co == NULL)
1403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001405 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001406 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407
1408 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1409 ADDOP_I(c, CALL_FUNCTION, 1);
1410 }
1411
1412 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1413}
1414
1415static int
1416compiler_class(struct compiler *c, stmt_ty s)
1417{
Christian Heimes5224d282008-02-23 15:01:05 +00001418 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001420 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001421 asdl_seq* decos = s->v.ClassDef.decorator_list;
1422
1423 if (!compiler_decorators(c, decos))
1424 return 0;
1425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426 /* push class name on stack, needed by BUILD_CLASS */
1427 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1428 /* push the tuple of base classes on the stack */
1429 n = asdl_seq_LEN(s->v.ClassDef.bases);
1430 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001431 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 ADDOP_I(c, BUILD_TUPLE, n);
1433 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1434 s->lineno))
1435 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001436 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001437 c->u->u_private = s->v.ClassDef.name;
1438 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001439 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 if (!str || !compiler_nameop(c, str, Load)) {
1441 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001442 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001444 }
1445
1446 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001447 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 if (!str || !compiler_nameop(c, str, Store)) {
1449 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001450 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001452 }
1453 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001455 if (!compiler_body(c, s->v.ClassDef.body)) {
1456 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001458 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001460 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1461 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001463 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001464 if (co == NULL)
1465 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001467 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001468 Py_DECREF(co);
1469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 ADDOP_I(c, CALL_FUNCTION, 0);
1471 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001472 /* apply decorators */
1473 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1474 ADDOP_I(c, CALL_FUNCTION, 1);
1475 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1477 return 0;
1478 return 1;
1479}
1480
1481static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001482compiler_ifexp(struct compiler *c, expr_ty e)
1483{
1484 basicblock *end, *next;
1485
1486 assert(e->kind == IfExp_kind);
1487 end = compiler_new_block(c);
1488 if (end == NULL)
1489 return 0;
1490 next = compiler_new_block(c);
1491 if (next == NULL)
1492 return 0;
1493 VISIT(c, expr, e->v.IfExp.test);
1494 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1495 ADDOP(c, POP_TOP);
1496 VISIT(c, expr, e->v.IfExp.body);
1497 ADDOP_JREL(c, JUMP_FORWARD, end);
1498 compiler_use_next_block(c, next);
1499 ADDOP(c, POP_TOP);
1500 VISIT(c, expr, e->v.IfExp.orelse);
1501 compiler_use_next_block(c, end);
1502 return 1;
1503}
1504
1505static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506compiler_lambda(struct compiler *c, expr_ty e)
1507{
1508 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001509 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510 arguments_ty args = e->v.Lambda.args;
1511 assert(e->kind == Lambda_kind);
1512
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001513 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001514 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001515 if (!name)
1516 return 0;
1517 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518
1519 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001520 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1522 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001523
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001524 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525 compiler_arguments(c, args);
1526
1527 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001528 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1529 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001530 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001531 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 if (co == NULL)
1533 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001535 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001536 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537
1538 return 1;
1539}
1540
1541static int
1542compiler_print(struct compiler *c, stmt_ty s)
1543{
1544 int i, n;
1545 bool dest;
1546
1547 assert(s->kind == Print_kind);
1548 n = asdl_seq_LEN(s->v.Print.values);
1549 dest = false;
1550 if (s->v.Print.dest) {
1551 VISIT(c, expr, s->v.Print.dest);
1552 dest = true;
1553 }
1554 for (i = 0; i < n; i++) {
1555 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1556 if (dest) {
1557 ADDOP(c, DUP_TOP);
1558 VISIT(c, expr, e);
1559 ADDOP(c, ROT_TWO);
1560 ADDOP(c, PRINT_ITEM_TO);
1561 }
1562 else {
1563 VISIT(c, expr, e);
1564 ADDOP(c, PRINT_ITEM);
1565 }
1566 }
1567 if (s->v.Print.nl) {
1568 if (dest)
1569 ADDOP(c, PRINT_NEWLINE_TO)
1570 else
1571 ADDOP(c, PRINT_NEWLINE)
1572 }
1573 else if (dest)
1574 ADDOP(c, POP_TOP);
1575 return 1;
1576}
1577
1578static int
1579compiler_if(struct compiler *c, stmt_ty s)
1580{
1581 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001582 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583 assert(s->kind == If_kind);
1584 end = compiler_new_block(c);
1585 if (end == NULL)
1586 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001587 next = compiler_new_block(c);
1588 if (next == NULL)
1589 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001590
1591 constant = expr_constant(s->v.If.test);
1592 /* constant = 0: "if 0"
1593 * constant = 1: "if 1", "if 2", ...
1594 * constant = -1: rest */
1595 if (constant == 0) {
1596 if (s->v.If.orelse)
1597 VISIT_SEQ(c, stmt, s->v.If.orelse);
1598 } else if (constant == 1) {
1599 VISIT_SEQ(c, stmt, s->v.If.body);
1600 } else {
1601 VISIT(c, expr, s->v.If.test);
1602 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1603 ADDOP(c, POP_TOP);
1604 VISIT_SEQ(c, stmt, s->v.If.body);
1605 ADDOP_JREL(c, JUMP_FORWARD, end);
1606 compiler_use_next_block(c, next);
1607 ADDOP(c, POP_TOP);
1608 if (s->v.If.orelse)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001609 VISIT_SEQ(c, stmt, s->v.If.orelse);
Georg Brandlddbaa662006-06-04 21:56:52 +00001610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611 compiler_use_next_block(c, end);
1612 return 1;
1613}
1614
1615static int
1616compiler_for(struct compiler *c, stmt_ty s)
1617{
1618 basicblock *start, *cleanup, *end;
1619
1620 start = compiler_new_block(c);
1621 cleanup = compiler_new_block(c);
1622 end = compiler_new_block(c);
1623 if (start == NULL || end == NULL || cleanup == NULL)
1624 return 0;
1625 ADDOP_JREL(c, SETUP_LOOP, end);
1626 if (!compiler_push_fblock(c, LOOP, start))
1627 return 0;
1628 VISIT(c, expr, s->v.For.iter);
1629 ADDOP(c, GET_ITER);
1630 compiler_use_next_block(c, start);
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001631 /* for expressions must be traced on each iteration,
1632 so we need to set an extra line number. */
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001633 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 ADDOP_JREL(c, FOR_ITER, cleanup);
1635 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001636 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001637 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1638 compiler_use_next_block(c, cleanup);
1639 ADDOP(c, POP_BLOCK);
1640 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001641 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 compiler_use_next_block(c, end);
1643 return 1;
1644}
1645
1646static int
1647compiler_while(struct compiler *c, stmt_ty s)
1648{
1649 basicblock *loop, *orelse, *end, *anchor = NULL;
1650 int constant = expr_constant(s->v.While.test);
1651
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001652 if (constant == 0) {
1653 if (s->v.While.orelse)
1654 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 loop = compiler_new_block(c);
1658 end = compiler_new_block(c);
1659 if (constant == -1) {
1660 anchor = compiler_new_block(c);
1661 if (anchor == NULL)
1662 return 0;
1663 }
1664 if (loop == NULL || end == NULL)
1665 return 0;
1666 if (s->v.While.orelse) {
1667 orelse = compiler_new_block(c);
1668 if (orelse == NULL)
1669 return 0;
1670 }
1671 else
1672 orelse = NULL;
1673
1674 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001675 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 if (!compiler_push_fblock(c, LOOP, loop))
1677 return 0;
1678 if (constant == -1) {
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001679 /* while expressions must be traced on each iteration,
1680 so we need to set an extra line number. */
1681 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 VISIT(c, expr, s->v.While.test);
1683 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1684 ADDOP(c, POP_TOP);
1685 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001686 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1688
1689 /* XXX should the two POP instructions be in a separate block
1690 if there is no else clause ?
1691 */
1692
1693 if (constant == -1) {
1694 compiler_use_next_block(c, anchor);
1695 ADDOP(c, POP_TOP);
1696 ADDOP(c, POP_BLOCK);
1697 }
1698 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001699 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001700 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701 compiler_use_next_block(c, end);
1702
1703 return 1;
1704}
1705
1706static int
1707compiler_continue(struct compiler *c)
1708{
1709 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001710 static const char IN_FINALLY_ERROR_MSG[] =
1711 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 int i;
1713
1714 if (!c->u->u_nfblocks)
1715 return compiler_error(c, LOOP_ERROR_MSG);
1716 i = c->u->u_nfblocks - 1;
1717 switch (c->u->u_fblock[i].fb_type) {
1718 case LOOP:
1719 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1720 break;
1721 case EXCEPT:
1722 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001723 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1724 /* Prevent continue anywhere under a finally
1725 even if hidden in a sub-try or except. */
1726 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1727 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 if (i == -1)
1730 return compiler_error(c, LOOP_ERROR_MSG);
1731 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1732 break;
1733 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001734 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 }
1736
1737 return 1;
1738}
1739
1740/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1741
1742 SETUP_FINALLY L
1743 <code for body>
1744 POP_BLOCK
1745 LOAD_CONST <None>
1746 L: <code for finalbody>
1747 END_FINALLY
1748
1749 The special instructions use the block stack. Each block
1750 stack entry contains the instruction that created it (here
1751 SETUP_FINALLY), the level of the value stack at the time the
1752 block stack entry was created, and a label (here L).
1753
1754 SETUP_FINALLY:
1755 Pushes the current value stack level and the label
1756 onto the block stack.
1757 POP_BLOCK:
1758 Pops en entry from the block stack, and pops the value
1759 stack until its level is the same as indicated on the
1760 block stack. (The label is ignored.)
1761 END_FINALLY:
1762 Pops a variable number of entries from the *value* stack
1763 and re-raises the exception they specify. The number of
1764 entries popped depends on the (pseudo) exception type.
1765
1766 The block stack is unwound when an exception is raised:
1767 when a SETUP_FINALLY entry is found, the exception is pushed
1768 onto the value stack (and the exception condition is cleared),
1769 and the interpreter jumps to the label gotten from the block
1770 stack.
1771*/
1772
1773static int
1774compiler_try_finally(struct compiler *c, stmt_ty s)
1775{
1776 basicblock *body, *end;
1777 body = compiler_new_block(c);
1778 end = compiler_new_block(c);
1779 if (body == NULL || end == NULL)
1780 return 0;
1781
1782 ADDOP_JREL(c, SETUP_FINALLY, end);
1783 compiler_use_next_block(c, body);
1784 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1785 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001786 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787 ADDOP(c, POP_BLOCK);
1788 compiler_pop_fblock(c, FINALLY_TRY, body);
1789
1790 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1791 compiler_use_next_block(c, end);
1792 if (!compiler_push_fblock(c, FINALLY_END, end))
1793 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001794 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 ADDOP(c, END_FINALLY);
1796 compiler_pop_fblock(c, FINALLY_END, end);
1797
1798 return 1;
1799}
1800
1801/*
1802 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1803 (The contents of the value stack is shown in [], with the top
1804 at the right; 'tb' is trace-back info, 'val' the exception's
1805 associated value, and 'exc' the exception.)
1806
1807 Value stack Label Instruction Argument
1808 [] SETUP_EXCEPT L1
1809 [] <code for S>
1810 [] POP_BLOCK
1811 [] JUMP_FORWARD L0
1812
1813 [tb, val, exc] L1: DUP )
1814 [tb, val, exc, exc] <evaluate E1> )
1815 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1816 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1817 [tb, val, exc, 1] POP )
1818 [tb, val, exc] POP
1819 [tb, val] <assign to V1> (or POP if no V1)
1820 [tb] POP
1821 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001822 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823
1824 [tb, val, exc, 0] L2: POP
1825 [tb, val, exc] DUP
1826 .............................etc.......................
1827
1828 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001829 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830
1831 [] L0: <next statement>
1832
1833 Of course, parts are not generated if Vi or Ei is not present.
1834*/
1835static int
1836compiler_try_except(struct compiler *c, stmt_ty s)
1837{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001838 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839 int i, n;
1840
1841 body = compiler_new_block(c);
1842 except = compiler_new_block(c);
1843 orelse = compiler_new_block(c);
1844 end = compiler_new_block(c);
1845 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1846 return 0;
1847 ADDOP_JREL(c, SETUP_EXCEPT, except);
1848 compiler_use_next_block(c, body);
1849 if (!compiler_push_fblock(c, EXCEPT, body))
1850 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001851 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 ADDOP(c, POP_BLOCK);
1853 compiler_pop_fblock(c, EXCEPT, body);
1854 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1855 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1856 compiler_use_next_block(c, except);
1857 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001858 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001860 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001862 c->u->u_lineno_set = false;
1863 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 except = compiler_new_block(c);
1865 if (except == NULL)
1866 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001867 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001869 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1871 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1872 ADDOP(c, POP_TOP);
1873 }
1874 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001875 if (handler->v.ExceptHandler.name) {
1876 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 }
1878 else {
1879 ADDOP(c, POP_TOP);
1880 }
1881 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001882 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 ADDOP_JREL(c, JUMP_FORWARD, end);
1884 compiler_use_next_block(c, except);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001885 if (handler->v.ExceptHandler.type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 ADDOP(c, POP_TOP);
1887 }
1888 ADDOP(c, END_FINALLY);
1889 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001890 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 compiler_use_next_block(c, end);
1892 return 1;
1893}
1894
1895static int
1896compiler_import_as(struct compiler *c, identifier name, identifier asname)
1897{
1898 /* The IMPORT_NAME opcode was already generated. This function
1899 merely needs to bind the result to a name.
1900
1901 If there is a dot in name, we need to split it and emit a
1902 LOAD_ATTR for each name.
1903 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001904 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 const char *dot = strchr(src, '.');
1906 if (dot) {
1907 /* Consume the base module name to get the first attribute */
1908 src = dot + 1;
1909 while (dot) {
1910 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001911 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001913 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001915 if (!attr)
1916 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001917 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001918 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919 src = dot + 1;
1920 }
1921 }
1922 return compiler_nameop(c, asname, Store);
1923}
1924
1925static int
1926compiler_import(struct compiler *c, stmt_ty s)
1927{
1928 /* The Import node stores a module name like a.b.c as a single
1929 string. This is convenient for all cases except
1930 import a.b.c as d
1931 where we need to parse that string to extract the individual
1932 module names.
1933 XXX Perhaps change the representation to make this case simpler?
1934 */
1935 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001936
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001938 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001940 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941
Neal Norwitzcbce2802006-04-03 06:26:32 +00001942 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001943 level = PyInt_FromLong(0);
1944 else
1945 level = PyInt_FromLong(-1);
1946
1947 if (level == NULL)
1948 return 0;
1949
1950 ADDOP_O(c, LOAD_CONST, level, consts);
1951 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1953 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1954
1955 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001956 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001957 if (!r)
1958 return r;
1959 }
1960 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001962 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963 char *dot = strchr(base, '.');
1964 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001965 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001966 dot - base);
1967 r = compiler_nameop(c, tmp, Store);
1968 if (dot) {
1969 Py_DECREF(tmp);
1970 }
1971 if (!r)
1972 return r;
1973 }
1974 }
1975 return 1;
1976}
1977
1978static int
1979compiler_from_import(struct compiler *c, stmt_ty s)
1980{
1981 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982
1983 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001984 PyObject *level;
1985
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001986 if (!names)
1987 return 0;
1988
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001989 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001990 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001991 level = PyInt_FromLong(-1);
1992 else
1993 level = PyInt_FromLong(s->v.ImportFrom.level);
1994
1995 if (!level) {
1996 Py_DECREF(names);
1997 return 0;
1998 }
1999
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 /* build up the names */
2001 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002002 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 Py_INCREF(alias->name);
2004 PyTuple_SET_ITEM(names, i, alias->name);
2005 }
2006
2007 if (s->lineno > c->c_future->ff_lineno) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002008 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002010 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 Py_DECREF(names);
2012 return compiler_error(c,
2013 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002014 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015
2016 }
2017 }
2018
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002019 ADDOP_O(c, LOAD_CONST, level, consts);
2020 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002022 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2024 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002025 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026 identifier store_name;
2027
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002028 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 assert(n == 1);
2030 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002031 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 }
2033
2034 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2035 store_name = alias->name;
2036 if (alias->asname)
2037 store_name = alias->asname;
2038
2039 if (!compiler_nameop(c, store_name, Store)) {
2040 Py_DECREF(names);
2041 return 0;
2042 }
2043 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002044 /* remove imported module */
2045 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 return 1;
2047}
2048
2049static int
2050compiler_assert(struct compiler *c, stmt_ty s)
2051{
2052 static PyObject *assertion_error = NULL;
2053 basicblock *end;
2054
2055 if (Py_OptimizeFlag)
2056 return 1;
2057 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002058 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 if (assertion_error == NULL)
2060 return 0;
2061 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002062 if (s->v.Assert.test->kind == Tuple_kind &&
2063 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2064 const char* msg =
2065 "assertion is always true, perhaps remove parentheses?";
2066 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2067 c->u->u_lineno, NULL, NULL) == -1)
2068 return 0;
2069 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 VISIT(c, expr, s->v.Assert.test);
2071 end = compiler_new_block(c);
2072 if (end == NULL)
2073 return 0;
2074 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2075 ADDOP(c, POP_TOP);
2076 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2077 if (s->v.Assert.msg) {
2078 VISIT(c, expr, s->v.Assert.msg);
2079 ADDOP_I(c, RAISE_VARARGS, 2);
2080 }
2081 else {
2082 ADDOP_I(c, RAISE_VARARGS, 1);
2083 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002084 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 ADDOP(c, POP_TOP);
2086 return 1;
2087}
2088
2089static int
2090compiler_visit_stmt(struct compiler *c, stmt_ty s)
2091{
2092 int i, n;
2093
Neal Norwitzf733a012006-10-29 18:30:10 +00002094 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 c->u->u_lineno = s->lineno;
2096 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002099 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002101 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002103 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 if (c->u->u_ste->ste_type != FunctionBlock)
2105 return compiler_error(c, "'return' outside function");
2106 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 VISIT(c, expr, s->v.Return.value);
2108 }
2109 else
2110 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2111 ADDOP(c, RETURN_VALUE);
2112 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002113 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002114 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002116 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 n = asdl_seq_LEN(s->v.Assign.targets);
2118 VISIT(c, expr, s->v.Assign.value);
2119 for (i = 0; i < n; i++) {
2120 if (i < n - 1)
2121 ADDOP(c, DUP_TOP);
2122 VISIT(c, expr,
2123 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2124 }
2125 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002126 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002128 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002130 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002132 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002134 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002136 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 n = 0;
2138 if (s->v.Raise.type) {
2139 VISIT(c, expr, s->v.Raise.type);
2140 n++;
2141 if (s->v.Raise.inst) {
2142 VISIT(c, expr, s->v.Raise.inst);
2143 n++;
2144 if (s->v.Raise.tback) {
2145 VISIT(c, expr, s->v.Raise.tback);
2146 n++;
2147 }
2148 }
2149 }
2150 ADDOP_I(c, RAISE_VARARGS, n);
2151 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002152 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002154 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002156 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002158 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002160 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002162 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 VISIT(c, expr, s->v.Exec.body);
2164 if (s->v.Exec.globals) {
2165 VISIT(c, expr, s->v.Exec.globals);
2166 if (s->v.Exec.locals) {
2167 VISIT(c, expr, s->v.Exec.locals);
2168 } else {
2169 ADDOP(c, DUP_TOP);
2170 }
2171 } else {
2172 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2173 ADDOP(c, DUP_TOP);
2174 }
2175 ADDOP(c, EXEC_STMT);
2176 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002177 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002179 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002181 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 ADDOP(c, PRINT_EXPR);
2183 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002184 else if (s->v.Expr.value->kind != Str_kind &&
2185 s->v.Expr.value->kind != Num_kind) {
2186 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 ADDOP(c, POP_TOP);
2188 }
2189 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002190 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002192 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002193 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 return compiler_error(c, "'break' outside loop");
2195 ADDOP(c, BREAK_LOOP);
2196 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002197 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002199 case With_kind:
2200 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 }
2202 return 1;
2203}
2204
2205static int
2206unaryop(unaryop_ty op)
2207{
2208 switch (op) {
2209 case Invert:
2210 return UNARY_INVERT;
2211 case Not:
2212 return UNARY_NOT;
2213 case UAdd:
2214 return UNARY_POSITIVE;
2215 case USub:
2216 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002217 default:
2218 PyErr_Format(PyExc_SystemError,
2219 "unary op %d should not be possible", op);
2220 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222}
2223
2224static int
2225binop(struct compiler *c, operator_ty op)
2226{
2227 switch (op) {
2228 case Add:
2229 return BINARY_ADD;
2230 case Sub:
2231 return BINARY_SUBTRACT;
2232 case Mult:
2233 return BINARY_MULTIPLY;
2234 case Div:
2235 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2236 return BINARY_TRUE_DIVIDE;
2237 else
2238 return BINARY_DIVIDE;
2239 case Mod:
2240 return BINARY_MODULO;
2241 case Pow:
2242 return BINARY_POWER;
2243 case LShift:
2244 return BINARY_LSHIFT;
2245 case RShift:
2246 return BINARY_RSHIFT;
2247 case BitOr:
2248 return BINARY_OR;
2249 case BitXor:
2250 return BINARY_XOR;
2251 case BitAnd:
2252 return BINARY_AND;
2253 case FloorDiv:
2254 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002255 default:
2256 PyErr_Format(PyExc_SystemError,
2257 "binary op %d should not be possible", op);
2258 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260}
2261
2262static int
2263cmpop(cmpop_ty op)
2264{
2265 switch (op) {
2266 case Eq:
2267 return PyCmp_EQ;
2268 case NotEq:
2269 return PyCmp_NE;
2270 case Lt:
2271 return PyCmp_LT;
2272 case LtE:
2273 return PyCmp_LE;
2274 case Gt:
2275 return PyCmp_GT;
2276 case GtE:
2277 return PyCmp_GE;
2278 case Is:
2279 return PyCmp_IS;
2280 case IsNot:
2281 return PyCmp_IS_NOT;
2282 case In:
2283 return PyCmp_IN;
2284 case NotIn:
2285 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002286 default:
2287 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289}
2290
2291static int
2292inplace_binop(struct compiler *c, operator_ty op)
2293{
2294 switch (op) {
2295 case Add:
2296 return INPLACE_ADD;
2297 case Sub:
2298 return INPLACE_SUBTRACT;
2299 case Mult:
2300 return INPLACE_MULTIPLY;
2301 case Div:
2302 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2303 return INPLACE_TRUE_DIVIDE;
2304 else
2305 return INPLACE_DIVIDE;
2306 case Mod:
2307 return INPLACE_MODULO;
2308 case Pow:
2309 return INPLACE_POWER;
2310 case LShift:
2311 return INPLACE_LSHIFT;
2312 case RShift:
2313 return INPLACE_RSHIFT;
2314 case BitOr:
2315 return INPLACE_OR;
2316 case BitXor:
2317 return INPLACE_XOR;
2318 case BitAnd:
2319 return INPLACE_AND;
2320 case FloorDiv:
2321 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002322 default:
2323 PyErr_Format(PyExc_SystemError,
2324 "inplace binary op %d should not be possible", op);
2325 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327}
2328
2329static int
2330compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2331{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002332 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2334
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002335 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002336 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 /* XXX AugStore isn't used anywhere! */
2338
2339 /* First check for assignment to __debug__. Param? */
2340 if ((ctx == Store || ctx == AugStore || ctx == Del)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002341 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002342 return compiler_error(c, "can not assign to __debug__");
2343 }
2344
Neal Norwitz0031ff32008-02-25 01:45:37 +00002345 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002346 if (!mangled)
2347 return 0;
2348
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 op = 0;
2350 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002351 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 switch (scope) {
2353 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002354 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 optype = OP_DEREF;
2356 break;
2357 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002358 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 optype = OP_DEREF;
2360 break;
2361 case LOCAL:
2362 if (c->u->u_ste->ste_type == FunctionBlock)
2363 optype = OP_FAST;
2364 break;
2365 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002366 if (c->u->u_ste->ste_type == FunctionBlock &&
2367 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368 optype = OP_GLOBAL;
2369 break;
2370 case GLOBAL_EXPLICIT:
2371 optype = OP_GLOBAL;
2372 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002373 default:
2374 /* scope can be 0 */
2375 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 }
2377
2378 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002379 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
2381 switch (optype) {
2382 case OP_DEREF:
2383 switch (ctx) {
2384 case Load: op = LOAD_DEREF; break;
2385 case Store: op = STORE_DEREF; break;
2386 case AugLoad:
2387 case AugStore:
2388 break;
2389 case Del:
2390 PyErr_Format(PyExc_SyntaxError,
2391 "can not delete variable '%s' referenced "
2392 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002393 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002394 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002395 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002397 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002398 PyErr_SetString(PyExc_SystemError,
2399 "param invalid for deref variable");
2400 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 }
2402 break;
2403 case OP_FAST:
2404 switch (ctx) {
2405 case Load: op = LOAD_FAST; break;
2406 case Store: op = STORE_FAST; break;
2407 case Del: op = DELETE_FAST; break;
2408 case AugLoad:
2409 case AugStore:
2410 break;
2411 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002412 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002413 PyErr_SetString(PyExc_SystemError,
2414 "param invalid for local variable");
2415 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002417 ADDOP_O(c, op, mangled, varnames);
2418 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002419 return 1;
2420 case OP_GLOBAL:
2421 switch (ctx) {
2422 case Load: op = LOAD_GLOBAL; break;
2423 case Store: op = STORE_GLOBAL; break;
2424 case Del: op = DELETE_GLOBAL; break;
2425 case AugLoad:
2426 case AugStore:
2427 break;
2428 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002429 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002430 PyErr_SetString(PyExc_SystemError,
2431 "param invalid for global variable");
2432 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002433 }
2434 break;
2435 case OP_NAME:
2436 switch (ctx) {
2437 case Load: op = LOAD_NAME; break;
2438 case Store: op = STORE_NAME; break;
2439 case Del: op = DELETE_NAME; break;
2440 case AugLoad:
2441 case AugStore:
2442 break;
2443 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002444 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002445 PyErr_SetString(PyExc_SystemError,
2446 "param invalid for name variable");
2447 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002448 }
2449 break;
2450 }
2451
2452 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002453 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002454 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002455 if (arg < 0)
2456 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002457 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458}
2459
2460static int
2461compiler_boolop(struct compiler *c, expr_ty e)
2462{
2463 basicblock *end;
2464 int jumpi, i, n;
2465 asdl_seq *s;
2466
2467 assert(e->kind == BoolOp_kind);
2468 if (e->v.BoolOp.op == And)
2469 jumpi = JUMP_IF_FALSE;
2470 else
2471 jumpi = JUMP_IF_TRUE;
2472 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002473 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 return 0;
2475 s = e->v.BoolOp.values;
2476 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002477 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002478 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002479 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 ADDOP_JREL(c, jumpi, end);
2481 ADDOP(c, POP_TOP)
2482 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002483 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 compiler_use_next_block(c, end);
2485 return 1;
2486}
2487
2488static int
2489compiler_list(struct compiler *c, expr_ty e)
2490{
2491 int n = asdl_seq_LEN(e->v.List.elts);
2492 if (e->v.List.ctx == Store) {
2493 ADDOP_I(c, UNPACK_SEQUENCE, n);
2494 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002495 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 if (e->v.List.ctx == Load) {
2497 ADDOP_I(c, BUILD_LIST, n);
2498 }
2499 return 1;
2500}
2501
2502static int
2503compiler_tuple(struct compiler *c, expr_ty e)
2504{
2505 int n = asdl_seq_LEN(e->v.Tuple.elts);
2506 if (e->v.Tuple.ctx == Store) {
2507 ADDOP_I(c, UNPACK_SEQUENCE, n);
2508 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002509 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 if (e->v.Tuple.ctx == Load) {
2511 ADDOP_I(c, BUILD_TUPLE, n);
2512 }
2513 return 1;
2514}
2515
2516static int
2517compiler_compare(struct compiler *c, expr_ty e)
2518{
2519 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002520 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521
2522 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2523 VISIT(c, expr, e->v.Compare.left);
2524 n = asdl_seq_LEN(e->v.Compare.ops);
2525 assert(n > 0);
2526 if (n > 1) {
2527 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002528 if (cleanup == NULL)
2529 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002530 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002531 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 }
2533 for (i = 1; i < n; i++) {
2534 ADDOP(c, DUP_TOP);
2535 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002537 cmpop((cmpop_ty)(asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002538 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2540 NEXT_BLOCK(c);
2541 ADDOP(c, POP_TOP);
2542 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002543 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002544 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002545 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002546 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002548 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 if (n > 1) {
2550 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002551 if (end == NULL)
2552 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 ADDOP_JREL(c, JUMP_FORWARD, end);
2554 compiler_use_next_block(c, cleanup);
2555 ADDOP(c, ROT_TWO);
2556 ADDOP(c, POP_TOP);
2557 compiler_use_next_block(c, end);
2558 }
2559 return 1;
2560}
2561
2562static int
2563compiler_call(struct compiler *c, expr_ty e)
2564{
2565 int n, code = 0;
2566
2567 VISIT(c, expr, e->v.Call.func);
2568 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002569 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002570 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002571 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2573 }
2574 if (e->v.Call.starargs) {
2575 VISIT(c, expr, e->v.Call.starargs);
2576 code |= 1;
2577 }
2578 if (e->v.Call.kwargs) {
2579 VISIT(c, expr, e->v.Call.kwargs);
2580 code |= 2;
2581 }
2582 switch (code) {
2583 case 0:
2584 ADDOP_I(c, CALL_FUNCTION, n);
2585 break;
2586 case 1:
2587 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2588 break;
2589 case 2:
2590 ADDOP_I(c, CALL_FUNCTION_KW, n);
2591 break;
2592 case 3:
2593 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2594 break;
2595 }
2596 return 1;
2597}
2598
2599static int
2600compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002601 asdl_seq *generators, int gen_index,
2602 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603{
2604 /* generate code for the iterator, then each of the ifs,
2605 and then write to the element */
2606
2607 comprehension_ty l;
2608 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002609 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610
2611 start = compiler_new_block(c);
2612 skip = compiler_new_block(c);
2613 if_cleanup = compiler_new_block(c);
2614 anchor = compiler_new_block(c);
2615
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002616 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2617 anchor == NULL)
2618 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619
Anthony Baxter7b782b62006-04-11 12:01:56 +00002620 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621 VISIT(c, expr, l->iter);
2622 ADDOP(c, GET_ITER);
2623 compiler_use_next_block(c, start);
2624 ADDOP_JREL(c, FOR_ITER, anchor);
2625 NEXT_BLOCK(c);
2626 VISIT(c, expr, l->target);
2627
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002628 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 n = asdl_seq_LEN(l->ifs);
2630 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002631 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 VISIT(c, expr, e);
2633 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2634 NEXT_BLOCK(c);
2635 ADDOP(c, POP_TOP);
2636 }
2637
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002638 if (++gen_index < asdl_seq_LEN(generators))
2639 if (!compiler_listcomp_generator(c, tmpname,
2640 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002643 /* only append after the last for generator */
2644 if (gen_index >= asdl_seq_LEN(generators)) {
2645 if (!compiler_nameop(c, tmpname, Load))
2646 return 0;
2647 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002648 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002649
2650 compiler_use_next_block(c, skip);
2651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 for (i = 0; i < n; i++) {
2653 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002654 if (i == 0)
2655 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 ADDOP(c, POP_TOP);
2657 }
2658 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2659 compiler_use_next_block(c, anchor);
Georg Brandl2c4fb8d2006-10-29 08:47:08 +00002660 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002662 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 return 0;
2664
2665 return 1;
2666}
2667
2668static int
2669compiler_listcomp(struct compiler *c, expr_ty e)
2670{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002672 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 asdl_seq *generators = e->v.ListComp.generators;
2674
2675 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002676 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677 if (!tmp)
2678 return 0;
2679 ADDOP_I(c, BUILD_LIST, 0);
2680 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002682 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2683 e->v.ListComp.elt);
2684 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685 return rc;
2686}
2687
2688static int
2689compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002690 asdl_seq *generators, int gen_index,
2691 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692{
2693 /* generate code for the iterator, then each of the ifs,
2694 and then write to the element */
2695
2696 comprehension_ty ge;
2697 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002698 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699
2700 start = compiler_new_block(c);
2701 skip = compiler_new_block(c);
2702 if_cleanup = compiler_new_block(c);
2703 anchor = compiler_new_block(c);
2704 end = compiler_new_block(c);
2705
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002706 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 anchor == NULL || end == NULL)
2708 return 0;
2709
Anthony Baxter7b782b62006-04-11 12:01:56 +00002710 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 ADDOP_JREL(c, SETUP_LOOP, end);
2712 if (!compiler_push_fblock(c, LOOP, start))
2713 return 0;
2714
2715 if (gen_index == 0) {
2716 /* Receive outermost iter as an implicit argument */
2717 c->u->u_argcount = 1;
2718 ADDOP_I(c, LOAD_FAST, 0);
2719 }
2720 else {
2721 /* Sub-iter - calculate on the fly */
2722 VISIT(c, expr, ge->iter);
2723 ADDOP(c, GET_ITER);
2724 }
2725 compiler_use_next_block(c, start);
2726 ADDOP_JREL(c, FOR_ITER, anchor);
2727 NEXT_BLOCK(c);
2728 VISIT(c, expr, ge->target);
2729
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002730 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 n = asdl_seq_LEN(ge->ifs);
2732 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002733 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 VISIT(c, expr, e);
2735 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2736 NEXT_BLOCK(c);
2737 ADDOP(c, POP_TOP);
2738 }
2739
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002740 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2742 return 0;
2743
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002744 /* only append after the last 'for' generator */
2745 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 VISIT(c, expr, elt);
2747 ADDOP(c, YIELD_VALUE);
2748 ADDOP(c, POP_TOP);
2749
2750 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002751 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 for (i = 0; i < n; i++) {
2753 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002754 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 compiler_use_next_block(c, if_cleanup);
2756
2757 ADDOP(c, POP_TOP);
2758 }
2759 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2760 compiler_use_next_block(c, anchor);
2761 ADDOP(c, POP_BLOCK);
2762 compiler_pop_fblock(c, LOOP, start);
2763 compiler_use_next_block(c, end);
2764
2765 return 1;
2766}
2767
2768static int
2769compiler_genexp(struct compiler *c, expr_ty e)
2770{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002771 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 PyCodeObject *co;
2773 expr_ty outermost_iter = ((comprehension_ty)
2774 (asdl_seq_GET(e->v.GeneratorExp.generators,
2775 0)))->iter;
2776
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002777 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002778 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002779 if (!name)
2780 return 0;
2781 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002782
2783 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2784 return 0;
2785 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2786 e->v.GeneratorExp.elt);
2787 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002788 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789 if (co == NULL)
2790 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002792 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002793 Py_DECREF(co);
2794
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 VISIT(c, expr, outermost_iter);
2796 ADDOP(c, GET_ITER);
2797 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002798
2799 return 1;
2800}
2801
2802static int
2803compiler_visit_keyword(struct compiler *c, keyword_ty k)
2804{
2805 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2806 VISIT(c, expr, k->value);
2807 return 1;
2808}
2809
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002810/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811 whether they are true or false.
2812
2813 Return values: 1 for true, 0 for false, -1 for non-constant.
2814 */
2815
2816static int
2817expr_constant(expr_ty e)
2818{
2819 switch (e->kind) {
2820 case Num_kind:
2821 return PyObject_IsTrue(e->v.Num.n);
2822 case Str_kind:
2823 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002824 case Name_kind:
2825 /* __debug__ is not assignable, so we can optimize
2826 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002827 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002828 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002829 return ! Py_OptimizeFlag;
2830 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 default:
2832 return -1;
2833 }
2834}
2835
Guido van Rossumc2e20742006-02-27 22:32:47 +00002836/*
2837 Implements the with statement from PEP 343.
2838
2839 The semantics outlined in that PEP are as follows:
2840
2841 with EXPR as VAR:
2842 BLOCK
2843
2844 It is implemented roughly as:
2845
Guido van Rossumda5b7012006-05-02 19:47:52 +00002846 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002847 exit = context.__exit__ # not calling it
2848 value = context.__enter__()
2849 try:
2850 VAR = value # if VAR present in the syntax
2851 BLOCK
2852 finally:
2853 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002854 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002855 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002856 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002857 exit(*exc)
2858 */
2859static int
2860compiler_with(struct compiler *c, stmt_ty s)
2861{
Guido van Rossumda5b7012006-05-02 19:47:52 +00002862 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002863 basicblock *block, *finally;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002864 identifier tmpvalue = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002865
2866 assert(s->kind == With_kind);
2867
Guido van Rossumc2e20742006-02-27 22:32:47 +00002868 if (!enter_attr) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002869 enter_attr = PyString_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002870 if (!enter_attr)
2871 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002872 }
2873 if (!exit_attr) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002874 exit_attr = PyString_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002875 if (!exit_attr)
2876 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002877 }
2878
2879 block = compiler_new_block(c);
2880 finally = compiler_new_block(c);
2881 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002882 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002883
Guido van Rossumc2e20742006-02-27 22:32:47 +00002884 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002885 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002886 We need to do this rather than preserving it on the stack
2887 because SETUP_FINALLY remembers the stack level.
2888 We need to do the assignment *inside* the try/finally
2889 so that context.__exit__() is called when the assignment
2890 fails. But we need to call context.__enter__() *before*
2891 the try/finally so that if it fails we won't call
2892 context.__exit__().
2893 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002894 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002895 if (tmpvalue == NULL)
2896 return 0;
2897 PyArena_AddPyObject(c->c_arena, tmpvalue);
2898 }
2899
Guido van Rossumda5b7012006-05-02 19:47:52 +00002900 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002901 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002902
Nick Coghlan7af53be2008-03-07 14:13:28 +00002903 /* Squirrel away context.__exit__ by stuffing it under context */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002904 ADDOP(c, DUP_TOP);
2905 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
Nick Coghlan7af53be2008-03-07 14:13:28 +00002906 ADDOP(c, ROT_TWO);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002907
2908 /* Call context.__enter__() */
2909 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2910 ADDOP_I(c, CALL_FUNCTION, 0);
2911
2912 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002913 /* Store it in tmpvalue */
2914 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00002915 return 0;
2916 }
2917 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002918 /* Discard result from context.__enter__() */
2919 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002920 }
2921
2922 /* Start the try block */
2923 ADDOP_JREL(c, SETUP_FINALLY, finally);
2924
2925 compiler_use_next_block(c, block);
2926 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002927 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002928 }
2929
2930 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002931 /* Bind saved result of context.__enter__() to VAR */
2932 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00002933 !compiler_nameop(c, tmpvalue, Del))
2934 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002935 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002936 }
2937
2938 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002939 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002940
2941 /* End of try block; start the finally block */
2942 ADDOP(c, POP_BLOCK);
2943 compiler_pop_fblock(c, FINALLY_TRY, block);
2944
2945 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2946 compiler_use_next_block(c, finally);
2947 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002948 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002949
Nick Coghlan7af53be2008-03-07 14:13:28 +00002950 /* Finally block starts; context.__exit__ is on the stack under
2951 the exception or return information. Just issue our magic
2952 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002953 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002954
2955 /* Finally block ends. */
2956 ADDOP(c, END_FINALLY);
2957 compiler_pop_fblock(c, FINALLY_END, finally);
2958 return 1;
2959}
2960
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961static int
2962compiler_visit_expr(struct compiler *c, expr_ty e)
2963{
2964 int i, n;
2965
Neal Norwitzf733a012006-10-29 18:30:10 +00002966 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002967 set a new line number for the next instruction.
2968 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 if (e->lineno > c->u->u_lineno) {
2970 c->u->u_lineno = e->lineno;
2971 c->u->u_lineno_set = false;
2972 }
2973 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002974 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002976 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 VISIT(c, expr, e->v.BinOp.left);
2978 VISIT(c, expr, e->v.BinOp.right);
2979 ADDOP(c, binop(c, e->v.BinOp.op));
2980 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002981 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 VISIT(c, expr, e->v.UnaryOp.operand);
2983 ADDOP(c, unaryop(e->v.UnaryOp.op));
2984 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002985 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002987 case IfExp_kind:
2988 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002989 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00002991 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002993 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002994 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00002995 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002996 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00002997 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 }
2999 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003000 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003002 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 return compiler_genexp(c, e);
3004 case Yield_kind:
3005 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003006 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 if (e->v.Yield.value) {
3008 VISIT(c, expr, e->v.Yield.value);
3009 }
3010 else {
3011 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3012 }
3013 ADDOP(c, YIELD_VALUE);
3014 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003015 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003017 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003019 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 VISIT(c, expr, e->v.Repr.value);
3021 ADDOP(c, UNARY_CONVERT);
3022 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003023 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3025 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003026 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3028 break;
3029 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 if (e->v.Attribute.ctx != AugStore)
3032 VISIT(c, expr, e->v.Attribute.value);
3033 switch (e->v.Attribute.ctx) {
3034 case AugLoad:
3035 ADDOP(c, DUP_TOP);
3036 /* Fall through to load */
3037 case Load:
3038 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3039 break;
3040 case AugStore:
3041 ADDOP(c, ROT_TWO);
3042 /* Fall through to save */
3043 case Store:
3044 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3045 break;
3046 case Del:
3047 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3048 break;
3049 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003050 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003051 PyErr_SetString(PyExc_SystemError,
3052 "param invalid in attribute expression");
3053 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 }
3055 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003056 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 switch (e->v.Subscript.ctx) {
3058 case AugLoad:
3059 VISIT(c, expr, e->v.Subscript.value);
3060 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3061 break;
3062 case Load:
3063 VISIT(c, expr, e->v.Subscript.value);
3064 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3065 break;
3066 case AugStore:
3067 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3068 break;
3069 case Store:
3070 VISIT(c, expr, e->v.Subscript.value);
3071 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3072 break;
3073 case Del:
3074 VISIT(c, expr, e->v.Subscript.value);
3075 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3076 break;
3077 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003078 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003079 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003080 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003081 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 }
3083 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003084 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3086 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003087 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003088 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003089 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090 return compiler_tuple(c, e);
3091 }
3092 return 1;
3093}
3094
3095static int
3096compiler_augassign(struct compiler *c, stmt_ty s)
3097{
3098 expr_ty e = s->v.AugAssign.target;
3099 expr_ty auge;
3100
3101 assert(s->kind == AugAssign_kind);
3102
3103 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003104 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003106 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003107 if (auge == NULL)
3108 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003109 VISIT(c, expr, auge);
3110 VISIT(c, expr, s->v.AugAssign.value);
3111 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3112 auge->v.Attribute.ctx = AugStore;
3113 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114 break;
3115 case Subscript_kind:
3116 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003117 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003118 if (auge == NULL)
3119 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 VISIT(c, expr, auge);
3121 VISIT(c, expr, s->v.AugAssign.value);
3122 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003123 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003125 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003127 if (!compiler_nameop(c, e->v.Name.id, Load))
3128 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003129 VISIT(c, expr, s->v.AugAssign.value);
3130 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3131 return compiler_nameop(c, e->v.Name.id, Store);
3132 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003133 PyErr_Format(PyExc_SystemError,
3134 "invalid node type (%d) for augmented assignment",
3135 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003136 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 }
3138 return 1;
3139}
3140
3141static int
3142compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3143{
3144 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003145 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3146 PyErr_SetString(PyExc_SystemError,
3147 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003149 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 f = &c->u->u_fblock[c->u->u_nfblocks++];
3151 f->fb_type = t;
3152 f->fb_block = b;
3153 return 1;
3154}
3155
3156static void
3157compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3158{
3159 struct compiler_unit *u = c->u;
3160 assert(u->u_nfblocks > 0);
3161 u->u_nfblocks--;
3162 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3163 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3164}
3165
Jeremy Hylton82271f12006-10-04 02:24:52 +00003166static int
3167compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003168 int i;
3169 struct compiler_unit *u = c->u;
3170 for (i = 0; i < u->u_nfblocks; ++i) {
3171 if (u->u_fblock[i].fb_type == LOOP)
3172 return 1;
3173 }
3174 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003175}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176/* Raises a SyntaxError and returns 0.
3177 If something goes wrong, a different exception may be raised.
3178*/
3179
3180static int
3181compiler_error(struct compiler *c, const char *errstr)
3182{
3183 PyObject *loc;
3184 PyObject *u = NULL, *v = NULL;
3185
3186 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3187 if (!loc) {
3188 Py_INCREF(Py_None);
3189 loc = Py_None;
3190 }
3191 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3192 Py_None, loc);
3193 if (!u)
3194 goto exit;
3195 v = Py_BuildValue("(zO)", errstr, u);
3196 if (!v)
3197 goto exit;
3198 PyErr_SetObject(PyExc_SyntaxError, v);
3199 exit:
3200 Py_DECREF(loc);
3201 Py_XDECREF(u);
3202 Py_XDECREF(v);
3203 return 0;
3204}
3205
3206static int
3207compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003208 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003210 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003212 /* XXX this code is duplicated */
3213 switch (ctx) {
3214 case AugLoad: /* fall through to Load */
3215 case Load: op = BINARY_SUBSCR; break;
3216 case AugStore:/* fall through to Store */
3217 case Store: op = STORE_SUBSCR; break;
3218 case Del: op = DELETE_SUBSCR; break;
3219 case Param:
3220 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003221 "invalid %s kind %d in subscript\n",
3222 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003223 return 0;
3224 }
3225 if (ctx == AugLoad) {
3226 ADDOP_I(c, DUP_TOPX, 2);
3227 }
3228 else if (ctx == AugStore) {
3229 ADDOP(c, ROT_THREE);
3230 }
3231 ADDOP(c, op);
3232 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233}
3234
3235static int
3236compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3237{
3238 int n = 2;
3239 assert(s->kind == Slice_kind);
3240
3241 /* only handles the cases where BUILD_SLICE is emitted */
3242 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003243 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244 }
3245 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003246 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003248
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003250 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 }
3252 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003253 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 }
3255
3256 if (s->v.Slice.step) {
3257 n++;
3258 VISIT(c, expr, s->v.Slice.step);
3259 }
3260 ADDOP_I(c, BUILD_SLICE, n);
3261 return 1;
3262}
3263
3264static int
3265compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3266{
3267 int op = 0, slice_offset = 0, stack_count = 0;
3268
3269 assert(s->v.Slice.step == NULL);
3270 if (s->v.Slice.lower) {
3271 slice_offset++;
3272 stack_count++;
3273 if (ctx != AugStore)
3274 VISIT(c, expr, s->v.Slice.lower);
3275 }
3276 if (s->v.Slice.upper) {
3277 slice_offset += 2;
3278 stack_count++;
3279 if (ctx != AugStore)
3280 VISIT(c, expr, s->v.Slice.upper);
3281 }
3282
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003283 if (ctx == AugLoad) {
3284 switch (stack_count) {
3285 case 0: ADDOP(c, DUP_TOP); break;
3286 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3287 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3288 }
3289 }
3290 else if (ctx == AugStore) {
3291 switch (stack_count) {
3292 case 0: ADDOP(c, ROT_TWO); break;
3293 case 1: ADDOP(c, ROT_THREE); break;
3294 case 2: ADDOP(c, ROT_FOUR); break;
3295 }
3296 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297
3298 switch (ctx) {
3299 case AugLoad: /* fall through to Load */
3300 case Load: op = SLICE; break;
3301 case AugStore:/* fall through to Store */
3302 case Store: op = STORE_SLICE; break;
3303 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003304 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003305 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003306 PyErr_SetString(PyExc_SystemError,
3307 "param invalid in simple slice");
3308 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 }
3310
3311 ADDOP(c, op + slice_offset);
3312 return 1;
3313}
3314
3315static int
3316compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3317 expr_context_ty ctx)
3318{
3319 switch (s->kind) {
3320 case Ellipsis_kind:
3321 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3322 break;
3323 case Slice_kind:
3324 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325 case Index_kind:
3326 VISIT(c, expr, s->v.Index.value);
3327 break;
3328 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003329 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003330 PyErr_SetString(PyExc_SystemError,
3331 "extended slice invalid in nested slice");
3332 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333 }
3334 return 1;
3335}
3336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337static int
3338compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3339{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003340 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003342 case Index_kind:
3343 kindname = "index";
3344 if (ctx != AugStore) {
3345 VISIT(c, expr, s->v.Index.value);
3346 }
3347 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003349 kindname = "ellipsis";
3350 if (ctx != AugStore) {
3351 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 break;
3354 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003355 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 if (!s->v.Slice.step)
3357 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003358 if (ctx != AugStore) {
3359 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360 return 0;
3361 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003362 break;
3363 case ExtSlice_kind:
3364 kindname = "extended slice";
3365 if (ctx != AugStore) {
3366 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3367 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003368 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003369 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003370 if (!compiler_visit_nested_slice(c, sub, ctx))
3371 return 0;
3372 }
3373 ADDOP_I(c, BUILD_TUPLE, n);
3374 }
3375 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003376 default:
3377 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003378 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003379 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003380 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003381 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382}
3383
Neal Norwitzf733a012006-10-29 18:30:10 +00003384
3385/* End of the compiler section, beginning of the assembler section */
3386
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387/* do depth-first search of basic block graph, starting with block.
3388 post records the block indices in post-order.
3389
3390 XXX must handle implicit jumps from one block to next
3391*/
3392
Neal Norwitzf733a012006-10-29 18:30:10 +00003393struct assembler {
3394 PyObject *a_bytecode; /* string containing bytecode */
3395 int a_offset; /* offset into bytecode */
3396 int a_nblocks; /* number of reachable blocks */
3397 basicblock **a_postorder; /* list of blocks in dfs postorder */
3398 PyObject *a_lnotab; /* string containing lnotab */
3399 int a_lnotab_off; /* offset into lnotab */
3400 int a_lineno; /* last lineno of emitted instruction */
3401 int a_lineno_off; /* bytecode offset of last lineno */
3402};
3403
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404static void
3405dfs(struct compiler *c, basicblock *b, struct assembler *a)
3406{
3407 int i;
3408 struct instr *instr = NULL;
3409
3410 if (b->b_seen)
3411 return;
3412 b->b_seen = 1;
3413 if (b->b_next != NULL)
3414 dfs(c, b->b_next, a);
3415 for (i = 0; i < b->b_iused; i++) {
3416 instr = &b->b_instr[i];
3417 if (instr->i_jrel || instr->i_jabs)
3418 dfs(c, instr->i_target, a);
3419 }
3420 a->a_postorder[a->a_nblocks++] = b;
3421}
3422
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003423static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3425{
3426 int i;
3427 struct instr *instr;
3428 if (b->b_seen || b->b_startdepth >= depth)
3429 return maxdepth;
3430 b->b_seen = 1;
3431 b->b_startdepth = depth;
3432 for (i = 0; i < b->b_iused; i++) {
3433 instr = &b->b_instr[i];
3434 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3435 if (depth > maxdepth)
3436 maxdepth = depth;
3437 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3438 if (instr->i_jrel || instr->i_jabs) {
3439 maxdepth = stackdepth_walk(c, instr->i_target,
3440 depth, maxdepth);
3441 if (instr->i_opcode == JUMP_ABSOLUTE ||
3442 instr->i_opcode == JUMP_FORWARD) {
3443 goto out; /* remaining code is dead */
3444 }
3445 }
3446 }
3447 if (b->b_next)
3448 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3449out:
3450 b->b_seen = 0;
3451 return maxdepth;
3452}
3453
3454/* Find the flow path that needs the largest stack. We assume that
3455 * cycles in the flow graph have no net effect on the stack depth.
3456 */
3457static int
3458stackdepth(struct compiler *c)
3459{
3460 basicblock *b, *entryblock;
3461 entryblock = NULL;
3462 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3463 b->b_seen = 0;
3464 b->b_startdepth = INT_MIN;
3465 entryblock = b;
3466 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003467 if (!entryblock)
3468 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 return stackdepth_walk(c, entryblock, 0, 0);
3470}
3471
3472static int
3473assemble_init(struct assembler *a, int nblocks, int firstlineno)
3474{
3475 memset(a, 0, sizeof(struct assembler));
3476 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003477 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 if (!a->a_bytecode)
3479 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003480 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 if (!a->a_lnotab)
3482 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003483 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3484 PyErr_NoMemory();
3485 return 0;
3486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003488 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003489 if (!a->a_postorder) {
3490 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003491 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 return 1;
3494}
3495
3496static void
3497assemble_free(struct assembler *a)
3498{
3499 Py_XDECREF(a->a_bytecode);
3500 Py_XDECREF(a->a_lnotab);
3501 if (a->a_postorder)
3502 PyObject_Free(a->a_postorder);
3503}
3504
3505/* Return the size of a basic block in bytes. */
3506
3507static int
3508instrsize(struct instr *instr)
3509{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003510 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003511 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003512 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003513 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3514 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515}
3516
3517static int
3518blocksize(basicblock *b)
3519{
3520 int i;
3521 int size = 0;
3522
3523 for (i = 0; i < b->b_iused; i++)
3524 size += instrsize(&b->b_instr[i]);
3525 return size;
3526}
3527
3528/* All about a_lnotab.
3529
3530c_lnotab is an array of unsigned bytes disguised as a Python string.
3531It is used to map bytecode offsets to source code line #s (when needed
3532for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003533
Tim Peters2a7f3842001-06-09 09:26:21 +00003534The array is conceptually a list of
3535 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003536pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003537
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003538 byte code offset source code line number
3539 0 1
3540 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003541 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003542 350 307
3543 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003544
3545The first trick is that these numbers aren't stored, only the increments
3546from one row to the next (this doesn't really work, but it's a start):
3547
3548 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3549
3550The second trick is that an unsigned byte can't hold negative values, or
3551values larger than 255, so (a) there's a deep assumption that byte code
3552offsets and their corresponding line #s both increase monotonically, and (b)
3553if at least one column jumps by more than 255 from one row to the next, more
3554than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003555from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003556part. A user of c_lnotab desiring to find the source line number
3557corresponding to a bytecode address A should do something like this
3558
3559 lineno = addr = 0
3560 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003561 addr += addr_incr
3562 if addr > A:
3563 return lineno
3564 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003565
3566In order for this to work, when the addr field increments by more than 255,
3567the line # increment in each pair generated must be 0 until the remaining addr
Neal Norwitz84be93b2006-07-16 01:50:38 +00003568increment is < 256. So, in the example above, assemble_lnotab (it used
3569to be called com_set_lineno) should not (as was actually done until 2.2)
3570expand 300, 300 to 255, 255, 45, 45,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003571 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003572*/
3573
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003574static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003576{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 int d_bytecode, d_lineno;
3578 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003579 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580
3581 d_bytecode = a->a_offset - a->a_lineno_off;
3582 d_lineno = i->i_lineno - a->a_lineno;
3583
3584 assert(d_bytecode >= 0);
3585 assert(d_lineno >= 0);
3586
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003587 if(d_bytecode == 0 && d_lineno == 0)
3588 return 1;
3589
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003590 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003591 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003593 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003595 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003597 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003599 else {
3600 PyErr_NoMemory();
3601 return 0;
3602 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003603 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003605 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003606 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003607 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003608 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 *lnotab++ = 255;
3610 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 d_bytecode -= ncodes * 255;
3613 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003614 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 assert(d_bytecode <= 255);
3616 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003617 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003619 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003621 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003623 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003625 else {
3626 PyErr_NoMemory();
3627 return 0;
3628 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003629 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003631 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003632 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003633 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003635 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003637 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003639 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003640 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 d_lineno -= ncodes * 255;
3642 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003643 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003644
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003645 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003647 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003648 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003649 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003650 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003651 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653 a->a_lnotab_off += 2;
3654 if (d_bytecode) {
3655 *lnotab++ = d_bytecode;
3656 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003657 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003658 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659 *lnotab++ = 0;
3660 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003661 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 a->a_lineno = i->i_lineno;
3663 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003664 return 1;
3665}
3666
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667/* assemble_emit()
3668 Extend the bytecode with a new instruction.
3669 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003670*/
3671
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003672static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003674{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003675 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003676 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677 char *code;
3678
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003679 size = instrsize(i);
3680 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003682 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003685 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003687 if (len > PY_SSIZE_T_MAX / 2)
3688 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003689 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003690 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003691 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003692 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003694 if (size == 6) {
3695 assert(i->i_hasarg);
3696 *code++ = (char)EXTENDED_ARG;
3697 *code++ = ext & 0xff;
3698 *code++ = ext >> 8;
3699 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003700 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003702 if (i->i_hasarg) {
3703 assert(size == 3 || size == 6);
3704 *code++ = arg & 0xff;
3705 *code++ = arg >> 8;
3706 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003708}
3709
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003710static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003712{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003714 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003715 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003716
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 /* Compute the size of each block and fixup jump args.
3718 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003719start:
3720 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003722 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 bsize = blocksize(b);
3724 b->b_offset = totsize;
3725 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003726 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003727 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3729 bsize = b->b_offset;
3730 for (i = 0; i < b->b_iused; i++) {
3731 struct instr *instr = &b->b_instr[i];
3732 /* Relative jumps are computed relative to
3733 the instruction pointer after fetching
3734 the jump instruction.
3735 */
3736 bsize += instrsize(instr);
3737 if (instr->i_jabs)
3738 instr->i_oparg = instr->i_target->b_offset;
3739 else if (instr->i_jrel) {
3740 int delta = instr->i_target->b_offset - bsize;
3741 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003742 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003743 else
3744 continue;
3745 if (instr->i_oparg > 0xffff)
3746 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003747 }
3748 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003749
3750 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003751 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003752 with a better solution.
3753
3754 In the meantime, should the goto be dropped in favor
3755 of a loop?
3756
3757 The issue is that in the first loop blocksize() is called
3758 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003759 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003760 i_oparg is calculated in the second loop above.
3761
3762 So we loop until we stop seeing new EXTENDED_ARGs.
3763 The only EXTENDED_ARGs that could be popping up are
3764 ones in jump instructions. So this should converge
3765 fairly quickly.
3766 */
3767 if (last_extended_arg_count != extended_arg_count) {
3768 last_extended_arg_count = extended_arg_count;
3769 goto start;
3770 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003771}
3772
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003773static PyObject *
3774dict_keys_inorder(PyObject *dict, int offset)
3775{
3776 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003777 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003778
3779 tuple = PyTuple_New(size);
3780 if (tuple == NULL)
3781 return NULL;
3782 while (PyDict_Next(dict, &pos, &k, &v)) {
3783 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003784 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003785 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003786 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003787 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003788 PyTuple_SET_ITEM(tuple, i - offset, k);
3789 }
3790 return tuple;
3791}
3792
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003793static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003795{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 PySTEntryObject *ste = c->u->u_ste;
3797 int flags = 0, n;
3798 if (ste->ste_type != ModuleBlock)
3799 flags |= CO_NEWLOCALS;
3800 if (ste->ste_type == FunctionBlock) {
3801 if (!ste->ste_unoptimized)
3802 flags |= CO_OPTIMIZED;
3803 if (ste->ste_nested)
3804 flags |= CO_NESTED;
3805 if (ste->ste_generator)
3806 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 if (ste->ste_varargs)
3809 flags |= CO_VARARGS;
3810 if (ste->ste_varkeywords)
3811 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003812 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003814
3815 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003816 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 n = PyDict_Size(c->u->u_freevars);
3819 if (n < 0)
3820 return -1;
3821 if (n == 0) {
3822 n = PyDict_Size(c->u->u_cellvars);
3823 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003824 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 if (n == 0) {
3826 flags |= CO_NOFREE;
3827 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003828 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003829
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003830 return flags;
3831}
3832
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833static PyCodeObject *
3834makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003835{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836 PyObject *tmp;
3837 PyCodeObject *co = NULL;
3838 PyObject *consts = NULL;
3839 PyObject *names = NULL;
3840 PyObject *varnames = NULL;
3841 PyObject *filename = NULL;
3842 PyObject *name = NULL;
3843 PyObject *freevars = NULL;
3844 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003845 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003847
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 tmp = dict_keys_inorder(c->u->u_consts, 0);
3849 if (!tmp)
3850 goto error;
3851 consts = PySequence_List(tmp); /* optimize_code requires a list */
3852 Py_DECREF(tmp);
3853
3854 names = dict_keys_inorder(c->u->u_names, 0);
3855 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3856 if (!consts || !names || !varnames)
3857 goto error;
3858
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003859 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3860 if (!cellvars)
3861 goto error;
3862 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3863 if (!freevars)
3864 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003865 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 if (!filename)
3867 goto error;
3868
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003869 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003870 flags = compute_code_flags(c);
3871 if (flags < 0)
3872 goto error;
3873
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003874 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 if (!bytecode)
3876 goto error;
3877
3878 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3879 if (!tmp)
3880 goto error;
3881 Py_DECREF(consts);
3882 consts = tmp;
3883
3884 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3885 bytecode, consts, names, varnames,
3886 freevars, cellvars,
3887 filename, c->u->u_name,
3888 c->u->u_firstlineno,
3889 a->a_lnotab);
3890 error:
3891 Py_XDECREF(consts);
3892 Py_XDECREF(names);
3893 Py_XDECREF(varnames);
3894 Py_XDECREF(filename);
3895 Py_XDECREF(name);
3896 Py_XDECREF(freevars);
3897 Py_XDECREF(cellvars);
3898 Py_XDECREF(bytecode);
3899 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003900}
3901
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003902
3903/* For debugging purposes only */
3904#if 0
3905static void
3906dump_instr(const struct instr *i)
3907{
3908 const char *jrel = i->i_jrel ? "jrel " : "";
3909 const char *jabs = i->i_jabs ? "jabs " : "";
3910 char arg[128];
3911
3912 *arg = '\0';
3913 if (i->i_hasarg)
3914 sprintf(arg, "arg: %d ", i->i_oparg);
3915
3916 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3917 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3918}
3919
3920static void
3921dump_basicblock(const basicblock *b)
3922{
3923 const char *seen = b->b_seen ? "seen " : "";
3924 const char *b_return = b->b_return ? "return " : "";
3925 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3926 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3927 if (b->b_instr) {
3928 int i;
3929 for (i = 0; i < b->b_iused; i++) {
3930 fprintf(stderr, " [%02d] ", i);
3931 dump_instr(b->b_instr + i);
3932 }
3933 }
3934}
3935#endif
3936
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937static PyCodeObject *
3938assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003939{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003940 basicblock *b, *entryblock;
3941 struct assembler a;
3942 int i, j, nblocks;
3943 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945 /* Make sure every block that falls off the end returns None.
3946 XXX NEXT_BLOCK() isn't quite right, because if the last
3947 block ends with a jump or return b_next shouldn't set.
3948 */
3949 if (!c->u->u_curblock->b_return) {
3950 NEXT_BLOCK(c);
3951 if (addNone)
3952 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3953 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003954 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 nblocks = 0;
3957 entryblock = NULL;
3958 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3959 nblocks++;
3960 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003961 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003962
Neal Norwitzed657552006-07-10 00:04:44 +00003963 /* Set firstlineno if it wasn't explicitly set. */
3964 if (!c->u->u_firstlineno) {
3965 if (entryblock && entryblock->b_instr)
3966 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3967 else
3968 c->u->u_firstlineno = 1;
3969 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3971 goto error;
3972 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003975 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 /* Emit code in reverse postorder from dfs. */
3978 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003979 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003980 for (j = 0; j < b->b_iused; j++)
3981 if (!assemble_emit(&a, &b->b_instr[j]))
3982 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003983 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003984
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003985 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003986 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003987 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003989
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 co = makecode(c, &a);
3991 error:
3992 assemble_free(&a);
3993 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003994}