blob: a3c79e891f18eb346685d6507aad52b05aa40c4a [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 Dickinsona47460a2009-10-27 17:26:31 +0000925 unsigned char *p;
Mark Dickinson105be772008-01-31 22:17:37 +0000926 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000928 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000929 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
930 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000931 d = PyFloat_AS_DOUBLE(o);
932 p = (unsigned char*) &d;
933 /* all we need is to make the tuple different in either the 0.0
934 * or -0.0 case from all others, just to avoid the "coercion".
935 */
936 if (*p==0 && p[sizeof(double)-1]==0)
937 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
938 else
939 t = PyTuple_Pack(2, o, o->ob_type);
940 }
Mark Dickinsona47460a2009-10-27 17:26:31 +0000941#ifndef WITHOUT_COMPLEX
Mark Dickinson105be772008-01-31 22:17:37 +0000942 else if (PyComplex_Check(o)) {
Mark Dickinsona47460a2009-10-27 17:26:31 +0000943 Py_complex z;
944 int real_part_zero, imag_part_zero;
945 unsigned char *q;
Mark Dickinson105be772008-01-31 22:17:37 +0000946 /* complex case is even messier: we need to make complex(x,
947 0.) different from complex(x, -0.) and complex(0., y)
948 different from complex(-0., y), for any x and y. In
949 particular, all four complex zeros should be
950 distinguished.*/
951 z = PyComplex_AsCComplex(o);
952 p = (unsigned char*) &(z.real);
953 q = (unsigned char*) &(z.imag);
954 /* all that matters here is that on IEEE platforms
955 real_part_zero will be true if z.real == 0., and false if
956 z.real == -0. In fact, real_part_zero will also be true
957 for some other rarely occurring nonzero floats, but this
958 doesn't matter. Similar comments apply to
959 imag_part_zero. */
960 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
961 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
962 if (real_part_zero && imag_part_zero) {
963 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
964 }
965 else if (real_part_zero && !imag_part_zero) {
966 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
967 }
968 else if (!real_part_zero && imag_part_zero) {
969 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
970 }
971 else {
972 t = PyTuple_Pack(2, o, o->ob_type);
973 }
974 }
Mark Dickinsona47460a2009-10-27 17:26:31 +0000975#endif /* WITHOUT_COMPLEX */
Mark Dickinson105be772008-01-31 22:17:37 +0000976 else {
977 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000978 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000979 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000980 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
982 v = PyDict_GetItem(dict, t);
983 if (!v) {
984 arg = PyDict_Size(dict);
985 v = PyInt_FromLong(arg);
986 if (!v) {
987 Py_DECREF(t);
988 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000989 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990 if (PyDict_SetItem(dict, t, v) < 0) {
991 Py_DECREF(t);
992 Py_DECREF(v);
993 return -1;
994 }
995 Py_DECREF(v);
996 }
997 else
998 arg = PyInt_AsLong(v);
999 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001000 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001}
1002
1003static int
1004compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1005 PyObject *o)
1006{
1007 int arg = compiler_add_o(c, dict, o);
1008 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 return compiler_addop_i(c, opcode, arg);
1011}
1012
1013static int
1014compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001015 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016{
1017 int arg;
1018 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1019 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001020 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 arg = compiler_add_o(c, dict, mangled);
1022 Py_DECREF(mangled);
1023 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001024 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001025 return compiler_addop_i(c, opcode, arg);
1026}
1027
1028/* Add an opcode with an integer argument.
1029 Returns 0 on failure, 1 on success.
1030*/
1031
1032static int
1033compiler_addop_i(struct compiler *c, int opcode, int oparg)
1034{
1035 struct instr *i;
1036 int off;
1037 off = compiler_next_instr(c, c->u->u_curblock);
1038 if (off < 0)
1039 return 0;
1040 i = &c->u->u_curblock->b_instr[off];
1041 i->i_opcode = opcode;
1042 i->i_oparg = oparg;
1043 i->i_hasarg = 1;
1044 compiler_set_lineno(c, off);
1045 return 1;
1046}
1047
1048static int
1049compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1050{
1051 struct instr *i;
1052 int off;
1053
1054 assert(b != NULL);
1055 off = compiler_next_instr(c, c->u->u_curblock);
1056 if (off < 0)
1057 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001058 i = &c->u->u_curblock->b_instr[off];
1059 i->i_opcode = opcode;
1060 i->i_target = b;
1061 i->i_hasarg = 1;
1062 if (absolute)
1063 i->i_jabs = 1;
1064 else
1065 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001066 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067 return 1;
1068}
1069
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001070/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1071 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 it as the current block. NEXT_BLOCK() also creates an implicit jump
1073 from the current block to the new block.
1074*/
1075
Neal Norwitzf733a012006-10-29 18:30:10 +00001076/* The returns inside these macros make it impossible to decref objects
1077 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078*/
1079
1080
1081#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001082 if (compiler_use_new_block((C)) == NULL) \
1083 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084}
1085
1086#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001087 if (compiler_next_block((C)) == NULL) \
1088 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001089}
1090
1091#define ADDOP(C, OP) { \
1092 if (!compiler_addop((C), (OP))) \
1093 return 0; \
1094}
1095
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001096#define ADDOP_IN_SCOPE(C, OP) { \
1097 if (!compiler_addop((C), (OP))) { \
1098 compiler_exit_scope(c); \
1099 return 0; \
1100 } \
1101}
1102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103#define ADDOP_O(C, OP, O, TYPE) { \
1104 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1105 return 0; \
1106}
1107
1108#define ADDOP_NAME(C, OP, O, TYPE) { \
1109 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1110 return 0; \
1111}
1112
1113#define ADDOP_I(C, OP, O) { \
1114 if (!compiler_addop_i((C), (OP), (O))) \
1115 return 0; \
1116}
1117
1118#define ADDOP_JABS(C, OP, O) { \
1119 if (!compiler_addop_j((C), (OP), (O), 1)) \
1120 return 0; \
1121}
1122
1123#define ADDOP_JREL(C, OP, O) { \
1124 if (!compiler_addop_j((C), (OP), (O), 0)) \
1125 return 0; \
1126}
1127
1128/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1129 the ASDL name to synthesize the name of the C type and the visit function.
1130*/
1131
1132#define VISIT(C, TYPE, V) {\
1133 if (!compiler_visit_ ## TYPE((C), (V))) \
1134 return 0; \
1135}
1136
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137#define VISIT_IN_SCOPE(C, TYPE, V) {\
1138 if (!compiler_visit_ ## TYPE((C), (V))) { \
1139 compiler_exit_scope(c); \
1140 return 0; \
1141 } \
1142}
1143
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001144#define VISIT_SLICE(C, V, CTX) {\
1145 if (!compiler_visit_slice((C), (V), (CTX))) \
1146 return 0; \
1147}
1148
1149#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001150 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001152 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001153 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001154 if (!compiler_visit_ ## TYPE((C), elt)) \
1155 return 0; \
1156 } \
1157}
1158
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001159#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001160 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001161 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001162 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001163 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001164 if (!compiler_visit_ ## TYPE((C), elt)) { \
1165 compiler_exit_scope(c); \
1166 return 0; \
1167 } \
1168 } \
1169}
1170
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171static int
1172compiler_isdocstring(stmt_ty s)
1173{
1174 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001175 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176 return s->v.Expr.value->kind == Str_kind;
1177}
1178
1179/* Compile a sequence of statements, checking for a docstring. */
1180
1181static int
1182compiler_body(struct compiler *c, asdl_seq *stmts)
1183{
1184 int i = 0;
1185 stmt_ty st;
1186
1187 if (!asdl_seq_LEN(stmts))
1188 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001189 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001190 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1191 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 i = 1;
1193 VISIT(c, expr, st->v.Expr.value);
1194 if (!compiler_nameop(c, __doc__, Store))
1195 return 0;
1196 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001197 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001198 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 return 1;
1200}
1201
1202static PyCodeObject *
1203compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001204{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001205 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001206 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 static PyObject *module;
1208 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001209 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 if (!module)
1211 return NULL;
1212 }
Neal Norwitzed657552006-07-10 00:04:44 +00001213 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1214 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001215 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 switch (mod->kind) {
1217 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001218 if (!compiler_body(c, mod->v.Module.body)) {
1219 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 break;
1223 case Interactive_kind:
1224 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001225 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001226 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 break;
1228 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001229 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001230 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001231 break;
1232 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001233 PyErr_SetString(PyExc_SystemError,
1234 "suite should not be possible");
1235 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001236 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001237 PyErr_Format(PyExc_SystemError,
1238 "module kind %d should not be possible",
1239 mod->kind);
1240 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001241 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242 co = assemble(c, addNone);
1243 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001244 return co;
1245}
1246
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247/* The test for LOCAL must come before the test for FREE in order to
1248 handle classes where name is both local and free. The local var is
1249 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001250*/
1251
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252static int
1253get_ref_type(struct compiler *c, PyObject *name)
1254{
1255 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001256 if (scope == 0) {
1257 char buf[350];
1258 PyOS_snprintf(buf, sizeof(buf),
1259 "unknown scope for %.100s in %.100s(%s) in %s\n"
1260 "symbols: %s\nlocals: %s\nglobals: %s\n",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001261 PyString_AS_STRING(name),
1262 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001263 PyObject_REPR(c->u->u_ste->ste_id),
1264 c->c_filename,
1265 PyObject_REPR(c->u->u_ste->ste_symbols),
1266 PyObject_REPR(c->u->u_varnames),
1267 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 Py_FatalError(buf);
1270 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001271
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001272 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273}
1274
1275static int
1276compiler_lookup_arg(PyObject *dict, PyObject *name)
1277{
1278 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001279 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001281 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001282 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001283 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001285 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 return PyInt_AS_LONG(v);
1287}
1288
1289static int
1290compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1291{
1292 int i, free = PyCode_GetNumFree(co);
1293 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001294 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1295 ADDOP_I(c, MAKE_FUNCTION, args);
1296 return 1;
1297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001298 for (i = 0; i < free; ++i) {
1299 /* Bypass com_addop_varname because it will generate
1300 LOAD_DEREF but LOAD_CLOSURE is needed.
1301 */
1302 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1303 int arg, reftype;
1304
1305 /* Special case: If a class contains a method with a
1306 free variable that has the same name as a method,
1307 the name will be considered free *and* local in the
1308 class. It should be handled by the closure, as
1309 well as by the normal name loookup logic.
1310 */
1311 reftype = get_ref_type(c, name);
1312 if (reftype == CELL)
1313 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1314 else /* (reftype == FREE) */
1315 arg = compiler_lookup_arg(c->u->u_freevars, name);
1316 if (arg == -1) {
1317 printf("lookup %s in %s %d %d\n"
1318 "freevars of %s: %s\n",
1319 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001320 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001322 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001323 PyObject_REPR(co->co_freevars));
1324 Py_FatalError("compiler_make_closure()");
1325 }
1326 ADDOP_I(c, LOAD_CLOSURE, arg);
1327 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001328 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001330 ADDOP_I(c, MAKE_CLOSURE, args);
1331 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332}
1333
1334static int
1335compiler_decorators(struct compiler *c, asdl_seq* decos)
1336{
1337 int i;
1338
1339 if (!decos)
1340 return 1;
1341
1342 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001343 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 }
1345 return 1;
1346}
1347
1348static int
1349compiler_arguments(struct compiler *c, arguments_ty args)
1350{
1351 int i;
1352 int n = asdl_seq_LEN(args->args);
1353 /* Correctly handle nested argument lists */
1354 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001355 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001356 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001357 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 if (id == NULL) {
1359 return 0;
1360 }
1361 if (!compiler_nameop(c, id, Load)) {
1362 Py_DECREF(id);
1363 return 0;
1364 }
1365 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001366 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 }
1368 }
1369 return 1;
1370}
1371
1372static int
1373compiler_function(struct compiler *c, stmt_ty s)
1374{
1375 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001376 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001378 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001379 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 int i, n, docstring;
1381
1382 assert(s->kind == FunctionDef_kind);
1383
1384 if (!compiler_decorators(c, decos))
1385 return 0;
1386 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001387 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1389 s->lineno))
1390 return 0;
1391
Anthony Baxter7b782b62006-04-11 12:01:56 +00001392 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001393 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001394 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001395 first_const = st->v.Expr.value->v.Str.s;
1396 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001397 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001398 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001399 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001401 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402 compiler_arguments(c, args);
1403
1404 c->u->u_argcount = asdl_seq_LEN(args->args);
1405 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001406 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001408 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1409 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 }
1411 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001412 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 if (co == NULL)
1414 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001416 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001417 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418
1419 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1420 ADDOP_I(c, CALL_FUNCTION, 1);
1421 }
1422
1423 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1424}
1425
1426static int
1427compiler_class(struct compiler *c, stmt_ty s)
1428{
Christian Heimes5224d282008-02-23 15:01:05 +00001429 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001431 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001432 asdl_seq* decos = s->v.ClassDef.decorator_list;
1433
1434 if (!compiler_decorators(c, decos))
1435 return 0;
1436
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 /* push class name on stack, needed by BUILD_CLASS */
1438 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1439 /* push the tuple of base classes on the stack */
1440 n = asdl_seq_LEN(s->v.ClassDef.bases);
1441 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001442 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443 ADDOP_I(c, BUILD_TUPLE, n);
1444 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1445 s->lineno))
1446 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001447 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001448 c->u->u_private = s->v.ClassDef.name;
1449 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001450 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 if (!str || !compiler_nameop(c, str, Load)) {
1452 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001453 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001455 }
1456
1457 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001458 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 if (!str || !compiler_nameop(c, str, Store)) {
1460 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001461 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001463 }
1464 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001466 if (!compiler_body(c, s->v.ClassDef.body)) {
1467 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001468 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001471 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1472 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001474 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 if (co == NULL)
1476 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001478 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001479 Py_DECREF(co);
1480
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481 ADDOP_I(c, CALL_FUNCTION, 0);
1482 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001483 /* apply decorators */
1484 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1485 ADDOP_I(c, CALL_FUNCTION, 1);
1486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1488 return 0;
1489 return 1;
1490}
1491
1492static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001493compiler_ifexp(struct compiler *c, expr_ty e)
1494{
1495 basicblock *end, *next;
1496
1497 assert(e->kind == IfExp_kind);
1498 end = compiler_new_block(c);
1499 if (end == NULL)
1500 return 0;
1501 next = compiler_new_block(c);
1502 if (next == NULL)
1503 return 0;
1504 VISIT(c, expr, e->v.IfExp.test);
1505 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1506 ADDOP(c, POP_TOP);
1507 VISIT(c, expr, e->v.IfExp.body);
1508 ADDOP_JREL(c, JUMP_FORWARD, end);
1509 compiler_use_next_block(c, next);
1510 ADDOP(c, POP_TOP);
1511 VISIT(c, expr, e->v.IfExp.orelse);
1512 compiler_use_next_block(c, end);
1513 return 1;
1514}
1515
1516static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517compiler_lambda(struct compiler *c, expr_ty e)
1518{
1519 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001520 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521 arguments_ty args = e->v.Lambda.args;
1522 assert(e->kind == Lambda_kind);
1523
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001524 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001525 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001526 if (!name)
1527 return 0;
1528 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529
1530 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001531 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1533 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001534
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001535 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536 compiler_arguments(c, args);
1537
1538 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001539 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1540 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001542 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001543 if (co == NULL)
1544 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001546 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001547 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001548
1549 return 1;
1550}
1551
1552static int
1553compiler_print(struct compiler *c, stmt_ty s)
1554{
1555 int i, n;
1556 bool dest;
1557
1558 assert(s->kind == Print_kind);
1559 n = asdl_seq_LEN(s->v.Print.values);
1560 dest = false;
1561 if (s->v.Print.dest) {
1562 VISIT(c, expr, s->v.Print.dest);
1563 dest = true;
1564 }
1565 for (i = 0; i < n; i++) {
1566 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1567 if (dest) {
1568 ADDOP(c, DUP_TOP);
1569 VISIT(c, expr, e);
1570 ADDOP(c, ROT_TWO);
1571 ADDOP(c, PRINT_ITEM_TO);
1572 }
1573 else {
1574 VISIT(c, expr, e);
1575 ADDOP(c, PRINT_ITEM);
1576 }
1577 }
1578 if (s->v.Print.nl) {
1579 if (dest)
1580 ADDOP(c, PRINT_NEWLINE_TO)
1581 else
1582 ADDOP(c, PRINT_NEWLINE)
1583 }
1584 else if (dest)
1585 ADDOP(c, POP_TOP);
1586 return 1;
1587}
1588
1589static int
1590compiler_if(struct compiler *c, stmt_ty s)
1591{
1592 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001593 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594 assert(s->kind == If_kind);
1595 end = compiler_new_block(c);
1596 if (end == NULL)
1597 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001598 next = compiler_new_block(c);
1599 if (next == NULL)
1600 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001601
1602 constant = expr_constant(s->v.If.test);
1603 /* constant = 0: "if 0"
1604 * constant = 1: "if 1", "if 2", ...
1605 * constant = -1: rest */
1606 if (constant == 0) {
1607 if (s->v.If.orelse)
1608 VISIT_SEQ(c, stmt, s->v.If.orelse);
1609 } else if (constant == 1) {
1610 VISIT_SEQ(c, stmt, s->v.If.body);
1611 } else {
1612 VISIT(c, expr, s->v.If.test);
1613 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1614 ADDOP(c, POP_TOP);
1615 VISIT_SEQ(c, stmt, s->v.If.body);
1616 ADDOP_JREL(c, JUMP_FORWARD, end);
1617 compiler_use_next_block(c, next);
1618 ADDOP(c, POP_TOP);
1619 if (s->v.If.orelse)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001620 VISIT_SEQ(c, stmt, s->v.If.orelse);
Georg Brandlddbaa662006-06-04 21:56:52 +00001621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622 compiler_use_next_block(c, end);
1623 return 1;
1624}
1625
1626static int
1627compiler_for(struct compiler *c, stmt_ty s)
1628{
1629 basicblock *start, *cleanup, *end;
1630
1631 start = compiler_new_block(c);
1632 cleanup = compiler_new_block(c);
1633 end = compiler_new_block(c);
1634 if (start == NULL || end == NULL || cleanup == NULL)
1635 return 0;
1636 ADDOP_JREL(c, SETUP_LOOP, end);
1637 if (!compiler_push_fblock(c, LOOP, start))
1638 return 0;
1639 VISIT(c, expr, s->v.For.iter);
1640 ADDOP(c, GET_ITER);
1641 compiler_use_next_block(c, start);
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001642 /* for expressions must be traced on each iteration,
1643 so we need to set an extra line number. */
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001644 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 ADDOP_JREL(c, FOR_ITER, cleanup);
1646 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001647 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1649 compiler_use_next_block(c, cleanup);
1650 ADDOP(c, POP_BLOCK);
1651 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001652 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 compiler_use_next_block(c, end);
1654 return 1;
1655}
1656
1657static int
1658compiler_while(struct compiler *c, stmt_ty s)
1659{
1660 basicblock *loop, *orelse, *end, *anchor = NULL;
1661 int constant = expr_constant(s->v.While.test);
1662
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001663 if (constant == 0) {
1664 if (s->v.While.orelse)
1665 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 loop = compiler_new_block(c);
1669 end = compiler_new_block(c);
1670 if (constant == -1) {
1671 anchor = compiler_new_block(c);
1672 if (anchor == NULL)
1673 return 0;
1674 }
1675 if (loop == NULL || end == NULL)
1676 return 0;
1677 if (s->v.While.orelse) {
1678 orelse = compiler_new_block(c);
1679 if (orelse == NULL)
1680 return 0;
1681 }
1682 else
1683 orelse = NULL;
1684
1685 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001686 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 if (!compiler_push_fblock(c, LOOP, loop))
1688 return 0;
1689 if (constant == -1) {
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001690 /* while expressions must be traced on each iteration,
1691 so we need to set an extra line number. */
1692 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 VISIT(c, expr, s->v.While.test);
1694 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1695 ADDOP(c, POP_TOP);
1696 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001697 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1699
1700 /* XXX should the two POP instructions be in a separate block
1701 if there is no else clause ?
1702 */
1703
1704 if (constant == -1) {
1705 compiler_use_next_block(c, anchor);
1706 ADDOP(c, POP_TOP);
1707 ADDOP(c, POP_BLOCK);
1708 }
1709 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001710 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001711 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712 compiler_use_next_block(c, end);
1713
1714 return 1;
1715}
1716
1717static int
1718compiler_continue(struct compiler *c)
1719{
1720 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001721 static const char IN_FINALLY_ERROR_MSG[] =
1722 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 int i;
1724
1725 if (!c->u->u_nfblocks)
1726 return compiler_error(c, LOOP_ERROR_MSG);
1727 i = c->u->u_nfblocks - 1;
1728 switch (c->u->u_fblock[i].fb_type) {
1729 case LOOP:
1730 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1731 break;
1732 case EXCEPT:
1733 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001734 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1735 /* Prevent continue anywhere under a finally
1736 even if hidden in a sub-try or except. */
1737 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1738 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1739 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740 if (i == -1)
1741 return compiler_error(c, LOOP_ERROR_MSG);
1742 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1743 break;
1744 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001745 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746 }
1747
1748 return 1;
1749}
1750
1751/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1752
1753 SETUP_FINALLY L
1754 <code for body>
1755 POP_BLOCK
1756 LOAD_CONST <None>
1757 L: <code for finalbody>
1758 END_FINALLY
1759
1760 The special instructions use the block stack. Each block
1761 stack entry contains the instruction that created it (here
1762 SETUP_FINALLY), the level of the value stack at the time the
1763 block stack entry was created, and a label (here L).
1764
1765 SETUP_FINALLY:
1766 Pushes the current value stack level and the label
1767 onto the block stack.
1768 POP_BLOCK:
1769 Pops en entry from the block stack, and pops the value
1770 stack until its level is the same as indicated on the
1771 block stack. (The label is ignored.)
1772 END_FINALLY:
1773 Pops a variable number of entries from the *value* stack
1774 and re-raises the exception they specify. The number of
1775 entries popped depends on the (pseudo) exception type.
1776
1777 The block stack is unwound when an exception is raised:
1778 when a SETUP_FINALLY entry is found, the exception is pushed
1779 onto the value stack (and the exception condition is cleared),
1780 and the interpreter jumps to the label gotten from the block
1781 stack.
1782*/
1783
1784static int
1785compiler_try_finally(struct compiler *c, stmt_ty s)
1786{
1787 basicblock *body, *end;
1788 body = compiler_new_block(c);
1789 end = compiler_new_block(c);
1790 if (body == NULL || end == NULL)
1791 return 0;
1792
1793 ADDOP_JREL(c, SETUP_FINALLY, end);
1794 compiler_use_next_block(c, body);
1795 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1796 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001797 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001798 ADDOP(c, POP_BLOCK);
1799 compiler_pop_fblock(c, FINALLY_TRY, body);
1800
1801 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1802 compiler_use_next_block(c, end);
1803 if (!compiler_push_fblock(c, FINALLY_END, end))
1804 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001805 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001806 ADDOP(c, END_FINALLY);
1807 compiler_pop_fblock(c, FINALLY_END, end);
1808
1809 return 1;
1810}
1811
1812/*
1813 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1814 (The contents of the value stack is shown in [], with the top
1815 at the right; 'tb' is trace-back info, 'val' the exception's
1816 associated value, and 'exc' the exception.)
1817
1818 Value stack Label Instruction Argument
1819 [] SETUP_EXCEPT L1
1820 [] <code for S>
1821 [] POP_BLOCK
1822 [] JUMP_FORWARD L0
1823
1824 [tb, val, exc] L1: DUP )
1825 [tb, val, exc, exc] <evaluate E1> )
1826 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1827 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1828 [tb, val, exc, 1] POP )
1829 [tb, val, exc] POP
1830 [tb, val] <assign to V1> (or POP if no V1)
1831 [tb] POP
1832 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001833 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834
1835 [tb, val, exc, 0] L2: POP
1836 [tb, val, exc] DUP
1837 .............................etc.......................
1838
1839 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001840 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841
1842 [] L0: <next statement>
1843
1844 Of course, parts are not generated if Vi or Ei is not present.
1845*/
1846static int
1847compiler_try_except(struct compiler *c, stmt_ty s)
1848{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001849 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 int i, n;
1851
1852 body = compiler_new_block(c);
1853 except = compiler_new_block(c);
1854 orelse = compiler_new_block(c);
1855 end = compiler_new_block(c);
1856 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1857 return 0;
1858 ADDOP_JREL(c, SETUP_EXCEPT, except);
1859 compiler_use_next_block(c, body);
1860 if (!compiler_push_fblock(c, EXCEPT, body))
1861 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001862 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 ADDOP(c, POP_BLOCK);
1864 compiler_pop_fblock(c, EXCEPT, body);
1865 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1866 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1867 compiler_use_next_block(c, except);
1868 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001869 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001871 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001873 c->u->u_lineno_set = false;
1874 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 except = compiler_new_block(c);
1876 if (except == NULL)
1877 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001878 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001880 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1882 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1883 ADDOP(c, POP_TOP);
1884 }
1885 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001886 if (handler->v.ExceptHandler.name) {
1887 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 }
1889 else {
1890 ADDOP(c, POP_TOP);
1891 }
1892 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001893 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 ADDOP_JREL(c, JUMP_FORWARD, end);
1895 compiler_use_next_block(c, except);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001896 if (handler->v.ExceptHandler.type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897 ADDOP(c, POP_TOP);
1898 }
1899 ADDOP(c, END_FINALLY);
1900 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001901 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 compiler_use_next_block(c, end);
1903 return 1;
1904}
1905
1906static int
1907compiler_import_as(struct compiler *c, identifier name, identifier asname)
1908{
1909 /* The IMPORT_NAME opcode was already generated. This function
1910 merely needs to bind the result to a name.
1911
1912 If there is a dot in name, we need to split it and emit a
1913 LOAD_ATTR for each name.
1914 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001915 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 const char *dot = strchr(src, '.');
1917 if (dot) {
1918 /* Consume the base module name to get the first attribute */
1919 src = dot + 1;
1920 while (dot) {
1921 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001922 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001924 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001926 if (!attr)
1927 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001929 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 src = dot + 1;
1931 }
1932 }
1933 return compiler_nameop(c, asname, Store);
1934}
1935
1936static int
1937compiler_import(struct compiler *c, stmt_ty s)
1938{
1939 /* The Import node stores a module name like a.b.c as a single
1940 string. This is convenient for all cases except
1941 import a.b.c as d
1942 where we need to parse that string to extract the individual
1943 module names.
1944 XXX Perhaps change the representation to make this case simpler?
1945 */
1946 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001949 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001951 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952
Neal Norwitzcbce2802006-04-03 06:26:32 +00001953 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001954 level = PyInt_FromLong(0);
1955 else
1956 level = PyInt_FromLong(-1);
1957
1958 if (level == NULL)
1959 return 0;
1960
1961 ADDOP_O(c, LOAD_CONST, level, consts);
1962 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1964 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1965
1966 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001967 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001968 if (!r)
1969 return r;
1970 }
1971 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001973 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 char *dot = strchr(base, '.');
1975 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001976 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 dot - base);
1978 r = compiler_nameop(c, tmp, Store);
1979 if (dot) {
1980 Py_DECREF(tmp);
1981 }
1982 if (!r)
1983 return r;
1984 }
1985 }
1986 return 1;
1987}
1988
1989static int
1990compiler_from_import(struct compiler *c, stmt_ty s)
1991{
1992 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993
1994 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001995 PyObject *level;
1996
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 if (!names)
1998 return 0;
1999
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002000 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00002001 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002002 level = PyInt_FromLong(-1);
2003 else
2004 level = PyInt_FromLong(s->v.ImportFrom.level);
2005
2006 if (!level) {
2007 Py_DECREF(names);
2008 return 0;
2009 }
2010
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 /* build up the names */
2012 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002013 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002014 Py_INCREF(alias->name);
2015 PyTuple_SET_ITEM(names, i, alias->name);
2016 }
2017
2018 if (s->lineno > c->c_future->ff_lineno) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002019 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002021 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 Py_DECREF(names);
2023 return compiler_error(c,
2024 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002025 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002026
2027 }
2028 }
2029
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002030 ADDOP_O(c, LOAD_CONST, level, consts);
2031 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002032 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002033 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2035 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002036 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 identifier store_name;
2038
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002039 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 assert(n == 1);
2041 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002042 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 }
2044
2045 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2046 store_name = alias->name;
2047 if (alias->asname)
2048 store_name = alias->asname;
2049
2050 if (!compiler_nameop(c, store_name, Store)) {
2051 Py_DECREF(names);
2052 return 0;
2053 }
2054 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002055 /* remove imported module */
2056 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 return 1;
2058}
2059
2060static int
2061compiler_assert(struct compiler *c, stmt_ty s)
2062{
2063 static PyObject *assertion_error = NULL;
2064 basicblock *end;
2065
2066 if (Py_OptimizeFlag)
2067 return 1;
2068 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002069 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 if (assertion_error == NULL)
2071 return 0;
2072 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002073 if (s->v.Assert.test->kind == Tuple_kind &&
2074 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2075 const char* msg =
2076 "assertion is always true, perhaps remove parentheses?";
2077 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2078 c->u->u_lineno, NULL, NULL) == -1)
2079 return 0;
2080 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 VISIT(c, expr, s->v.Assert.test);
2082 end = compiler_new_block(c);
2083 if (end == NULL)
2084 return 0;
2085 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2086 ADDOP(c, POP_TOP);
2087 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2088 if (s->v.Assert.msg) {
2089 VISIT(c, expr, s->v.Assert.msg);
2090 ADDOP_I(c, RAISE_VARARGS, 2);
2091 }
2092 else {
2093 ADDOP_I(c, RAISE_VARARGS, 1);
2094 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002095 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 ADDOP(c, POP_TOP);
2097 return 1;
2098}
2099
2100static int
2101compiler_visit_stmt(struct compiler *c, stmt_ty s)
2102{
2103 int i, n;
2104
Neal Norwitzf733a012006-10-29 18:30:10 +00002105 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 c->u->u_lineno = s->lineno;
2107 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002110 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002112 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002113 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002114 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 if (c->u->u_ste->ste_type != FunctionBlock)
2116 return compiler_error(c, "'return' outside function");
2117 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 VISIT(c, expr, s->v.Return.value);
2119 }
2120 else
2121 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2122 ADDOP(c, RETURN_VALUE);
2123 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002124 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002125 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002127 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 n = asdl_seq_LEN(s->v.Assign.targets);
2129 VISIT(c, expr, s->v.Assign.value);
2130 for (i = 0; i < n; i++) {
2131 if (i < n - 1)
2132 ADDOP(c, DUP_TOP);
2133 VISIT(c, expr,
2134 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2135 }
2136 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002137 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002138 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002139 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002141 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002143 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002145 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002147 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 n = 0;
2149 if (s->v.Raise.type) {
2150 VISIT(c, expr, s->v.Raise.type);
2151 n++;
2152 if (s->v.Raise.inst) {
2153 VISIT(c, expr, s->v.Raise.inst);
2154 n++;
2155 if (s->v.Raise.tback) {
2156 VISIT(c, expr, s->v.Raise.tback);
2157 n++;
2158 }
2159 }
2160 }
2161 ADDOP_I(c, RAISE_VARARGS, n);
2162 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002163 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002165 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002167 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002169 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002171 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002173 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 VISIT(c, expr, s->v.Exec.body);
2175 if (s->v.Exec.globals) {
2176 VISIT(c, expr, s->v.Exec.globals);
2177 if (s->v.Exec.locals) {
2178 VISIT(c, expr, s->v.Exec.locals);
2179 } else {
2180 ADDOP(c, DUP_TOP);
2181 }
2182 } else {
2183 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2184 ADDOP(c, DUP_TOP);
2185 }
2186 ADDOP(c, EXEC_STMT);
2187 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002188 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002190 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002192 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 ADDOP(c, PRINT_EXPR);
2194 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002195 else if (s->v.Expr.value->kind != Str_kind &&
2196 s->v.Expr.value->kind != Num_kind) {
2197 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 ADDOP(c, POP_TOP);
2199 }
2200 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002201 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002203 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002204 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205 return compiler_error(c, "'break' outside loop");
2206 ADDOP(c, BREAK_LOOP);
2207 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002208 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002210 case With_kind:
2211 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 }
2213 return 1;
2214}
2215
2216static int
2217unaryop(unaryop_ty op)
2218{
2219 switch (op) {
2220 case Invert:
2221 return UNARY_INVERT;
2222 case Not:
2223 return UNARY_NOT;
2224 case UAdd:
2225 return UNARY_POSITIVE;
2226 case USub:
2227 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002228 default:
2229 PyErr_Format(PyExc_SystemError,
2230 "unary op %d should not be possible", op);
2231 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002233}
2234
2235static int
2236binop(struct compiler *c, operator_ty op)
2237{
2238 switch (op) {
2239 case Add:
2240 return BINARY_ADD;
2241 case Sub:
2242 return BINARY_SUBTRACT;
2243 case Mult:
2244 return BINARY_MULTIPLY;
2245 case Div:
2246 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2247 return BINARY_TRUE_DIVIDE;
2248 else
2249 return BINARY_DIVIDE;
2250 case Mod:
2251 return BINARY_MODULO;
2252 case Pow:
2253 return BINARY_POWER;
2254 case LShift:
2255 return BINARY_LSHIFT;
2256 case RShift:
2257 return BINARY_RSHIFT;
2258 case BitOr:
2259 return BINARY_OR;
2260 case BitXor:
2261 return BINARY_XOR;
2262 case BitAnd:
2263 return BINARY_AND;
2264 case FloorDiv:
2265 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002266 default:
2267 PyErr_Format(PyExc_SystemError,
2268 "binary op %d should not be possible", op);
2269 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271}
2272
2273static int
2274cmpop(cmpop_ty op)
2275{
2276 switch (op) {
2277 case Eq:
2278 return PyCmp_EQ;
2279 case NotEq:
2280 return PyCmp_NE;
2281 case Lt:
2282 return PyCmp_LT;
2283 case LtE:
2284 return PyCmp_LE;
2285 case Gt:
2286 return PyCmp_GT;
2287 case GtE:
2288 return PyCmp_GE;
2289 case Is:
2290 return PyCmp_IS;
2291 case IsNot:
2292 return PyCmp_IS_NOT;
2293 case In:
2294 return PyCmp_IN;
2295 case NotIn:
2296 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002297 default:
2298 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300}
2301
2302static int
2303inplace_binop(struct compiler *c, operator_ty op)
2304{
2305 switch (op) {
2306 case Add:
2307 return INPLACE_ADD;
2308 case Sub:
2309 return INPLACE_SUBTRACT;
2310 case Mult:
2311 return INPLACE_MULTIPLY;
2312 case Div:
2313 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2314 return INPLACE_TRUE_DIVIDE;
2315 else
2316 return INPLACE_DIVIDE;
2317 case Mod:
2318 return INPLACE_MODULO;
2319 case Pow:
2320 return INPLACE_POWER;
2321 case LShift:
2322 return INPLACE_LSHIFT;
2323 case RShift:
2324 return INPLACE_RSHIFT;
2325 case BitOr:
2326 return INPLACE_OR;
2327 case BitXor:
2328 return INPLACE_XOR;
2329 case BitAnd:
2330 return INPLACE_AND;
2331 case FloorDiv:
2332 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002333 default:
2334 PyErr_Format(PyExc_SystemError,
2335 "inplace binary op %d should not be possible", op);
2336 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338}
2339
2340static int
2341compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2342{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002343 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2345
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002346 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002347 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 /* XXX AugStore isn't used anywhere! */
2349
2350 /* First check for assignment to __debug__. Param? */
2351 if ((ctx == Store || ctx == AugStore || ctx == Del)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002352 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002353 return compiler_error(c, "can not assign to __debug__");
2354 }
2355
Neal Norwitz0031ff32008-02-25 01:45:37 +00002356 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002357 if (!mangled)
2358 return 0;
2359
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002360 op = 0;
2361 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002362 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 switch (scope) {
2364 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002365 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 optype = OP_DEREF;
2367 break;
2368 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002369 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 optype = OP_DEREF;
2371 break;
2372 case LOCAL:
2373 if (c->u->u_ste->ste_type == FunctionBlock)
2374 optype = OP_FAST;
2375 break;
2376 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002377 if (c->u->u_ste->ste_type == FunctionBlock &&
2378 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002379 optype = OP_GLOBAL;
2380 break;
2381 case GLOBAL_EXPLICIT:
2382 optype = OP_GLOBAL;
2383 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002384 default:
2385 /* scope can be 0 */
2386 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 }
2388
2389 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002390 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391
2392 switch (optype) {
2393 case OP_DEREF:
2394 switch (ctx) {
2395 case Load: op = LOAD_DEREF; break;
2396 case Store: op = STORE_DEREF; break;
2397 case AugLoad:
2398 case AugStore:
2399 break;
2400 case Del:
2401 PyErr_Format(PyExc_SyntaxError,
2402 "can not delete variable '%s' referenced "
2403 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002404 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002405 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002406 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002408 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002409 PyErr_SetString(PyExc_SystemError,
2410 "param invalid for deref variable");
2411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 }
2413 break;
2414 case OP_FAST:
2415 switch (ctx) {
2416 case Load: op = LOAD_FAST; break;
2417 case Store: op = STORE_FAST; break;
2418 case Del: op = DELETE_FAST; break;
2419 case AugLoad:
2420 case AugStore:
2421 break;
2422 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002423 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002424 PyErr_SetString(PyExc_SystemError,
2425 "param invalid for local variable");
2426 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002428 ADDOP_O(c, op, mangled, varnames);
2429 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 return 1;
2431 case OP_GLOBAL:
2432 switch (ctx) {
2433 case Load: op = LOAD_GLOBAL; break;
2434 case Store: op = STORE_GLOBAL; break;
2435 case Del: op = DELETE_GLOBAL; break;
2436 case AugLoad:
2437 case AugStore:
2438 break;
2439 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002440 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002441 PyErr_SetString(PyExc_SystemError,
2442 "param invalid for global variable");
2443 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 }
2445 break;
2446 case OP_NAME:
2447 switch (ctx) {
2448 case Load: op = LOAD_NAME; break;
2449 case Store: op = STORE_NAME; break;
2450 case Del: op = DELETE_NAME; break;
2451 case AugLoad:
2452 case AugStore:
2453 break;
2454 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002455 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002456 PyErr_SetString(PyExc_SystemError,
2457 "param invalid for name variable");
2458 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 }
2460 break;
2461 }
2462
2463 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002464 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002465 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002466 if (arg < 0)
2467 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002468 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469}
2470
2471static int
2472compiler_boolop(struct compiler *c, expr_ty e)
2473{
2474 basicblock *end;
2475 int jumpi, i, n;
2476 asdl_seq *s;
2477
2478 assert(e->kind == BoolOp_kind);
2479 if (e->v.BoolOp.op == And)
2480 jumpi = JUMP_IF_FALSE;
2481 else
2482 jumpi = JUMP_IF_TRUE;
2483 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002484 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 return 0;
2486 s = e->v.BoolOp.values;
2487 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002488 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002490 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 ADDOP_JREL(c, jumpi, end);
2492 ADDOP(c, POP_TOP)
2493 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002494 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 compiler_use_next_block(c, end);
2496 return 1;
2497}
2498
2499static int
2500compiler_list(struct compiler *c, expr_ty e)
2501{
2502 int n = asdl_seq_LEN(e->v.List.elts);
2503 if (e->v.List.ctx == Store) {
2504 ADDOP_I(c, UNPACK_SEQUENCE, n);
2505 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002506 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (e->v.List.ctx == Load) {
2508 ADDOP_I(c, BUILD_LIST, n);
2509 }
2510 return 1;
2511}
2512
2513static int
2514compiler_tuple(struct compiler *c, expr_ty e)
2515{
2516 int n = asdl_seq_LEN(e->v.Tuple.elts);
2517 if (e->v.Tuple.ctx == Store) {
2518 ADDOP_I(c, UNPACK_SEQUENCE, n);
2519 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002520 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 if (e->v.Tuple.ctx == Load) {
2522 ADDOP_I(c, BUILD_TUPLE, n);
2523 }
2524 return 1;
2525}
2526
2527static int
2528compiler_compare(struct compiler *c, expr_ty e)
2529{
2530 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002531 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532
2533 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2534 VISIT(c, expr, e->v.Compare.left);
2535 n = asdl_seq_LEN(e->v.Compare.ops);
2536 assert(n > 0);
2537 if (n > 1) {
2538 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002539 if (cleanup == NULL)
2540 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002541 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002542 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 }
2544 for (i = 1; i < n; i++) {
2545 ADDOP(c, DUP_TOP);
2546 ADDOP(c, ROT_THREE);
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(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002549 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2551 NEXT_BLOCK(c);
2552 ADDOP(c, POP_TOP);
2553 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002554 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002555 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002557 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002559 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 if (n > 1) {
2561 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002562 if (end == NULL)
2563 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 ADDOP_JREL(c, JUMP_FORWARD, end);
2565 compiler_use_next_block(c, cleanup);
2566 ADDOP(c, ROT_TWO);
2567 ADDOP(c, POP_TOP);
2568 compiler_use_next_block(c, end);
2569 }
2570 return 1;
2571}
2572
2573static int
2574compiler_call(struct compiler *c, expr_ty e)
2575{
2576 int n, code = 0;
2577
2578 VISIT(c, expr, e->v.Call.func);
2579 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002580 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002582 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2584 }
2585 if (e->v.Call.starargs) {
2586 VISIT(c, expr, e->v.Call.starargs);
2587 code |= 1;
2588 }
2589 if (e->v.Call.kwargs) {
2590 VISIT(c, expr, e->v.Call.kwargs);
2591 code |= 2;
2592 }
2593 switch (code) {
2594 case 0:
2595 ADDOP_I(c, CALL_FUNCTION, n);
2596 break;
2597 case 1:
2598 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2599 break;
2600 case 2:
2601 ADDOP_I(c, CALL_FUNCTION_KW, n);
2602 break;
2603 case 3:
2604 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2605 break;
2606 }
2607 return 1;
2608}
2609
2610static int
2611compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002612 asdl_seq *generators, int gen_index,
2613 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614{
2615 /* generate code for the iterator, then each of the ifs,
2616 and then write to the element */
2617
2618 comprehension_ty l;
2619 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002620 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621
2622 start = compiler_new_block(c);
2623 skip = compiler_new_block(c);
2624 if_cleanup = compiler_new_block(c);
2625 anchor = compiler_new_block(c);
2626
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002627 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2628 anchor == NULL)
2629 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630
Anthony Baxter7b782b62006-04-11 12:01:56 +00002631 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 VISIT(c, expr, l->iter);
2633 ADDOP(c, GET_ITER);
2634 compiler_use_next_block(c, start);
2635 ADDOP_JREL(c, FOR_ITER, anchor);
2636 NEXT_BLOCK(c);
2637 VISIT(c, expr, l->target);
2638
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002639 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640 n = asdl_seq_LEN(l->ifs);
2641 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002642 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 VISIT(c, expr, e);
2644 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2645 NEXT_BLOCK(c);
2646 ADDOP(c, POP_TOP);
2647 }
2648
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002649 if (++gen_index < asdl_seq_LEN(generators))
2650 if (!compiler_listcomp_generator(c, tmpname,
2651 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002654 /* only append after the last for generator */
2655 if (gen_index >= asdl_seq_LEN(generators)) {
2656 if (!compiler_nameop(c, tmpname, Load))
2657 return 0;
2658 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002659 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002660
2661 compiler_use_next_block(c, skip);
2662 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 for (i = 0; i < n; i++) {
2664 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002665 if (i == 0)
2666 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 ADDOP(c, POP_TOP);
2668 }
2669 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2670 compiler_use_next_block(c, anchor);
Georg Brandl2c4fb8d2006-10-29 08:47:08 +00002671 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002673 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 return 0;
2675
2676 return 1;
2677}
2678
2679static int
2680compiler_listcomp(struct compiler *c, expr_ty e)
2681{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002683 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684 asdl_seq *generators = e->v.ListComp.generators;
2685
2686 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002687 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 if (!tmp)
2689 return 0;
2690 ADDOP_I(c, BUILD_LIST, 0);
2691 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002693 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2694 e->v.ListComp.elt);
2695 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696 return rc;
2697}
2698
2699static int
2700compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002701 asdl_seq *generators, int gen_index,
2702 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703{
2704 /* generate code for the iterator, then each of the ifs,
2705 and then write to the element */
2706
2707 comprehension_ty ge;
2708 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002709 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710
2711 start = compiler_new_block(c);
2712 skip = compiler_new_block(c);
2713 if_cleanup = compiler_new_block(c);
2714 anchor = compiler_new_block(c);
2715 end = compiler_new_block(c);
2716
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002717 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 anchor == NULL || end == NULL)
2719 return 0;
2720
Anthony Baxter7b782b62006-04-11 12:01:56 +00002721 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 ADDOP_JREL(c, SETUP_LOOP, end);
2723 if (!compiler_push_fblock(c, LOOP, start))
2724 return 0;
2725
2726 if (gen_index == 0) {
2727 /* Receive outermost iter as an implicit argument */
2728 c->u->u_argcount = 1;
2729 ADDOP_I(c, LOAD_FAST, 0);
2730 }
2731 else {
2732 /* Sub-iter - calculate on the fly */
2733 VISIT(c, expr, ge->iter);
2734 ADDOP(c, GET_ITER);
2735 }
2736 compiler_use_next_block(c, start);
2737 ADDOP_JREL(c, FOR_ITER, anchor);
2738 NEXT_BLOCK(c);
2739 VISIT(c, expr, ge->target);
2740
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002741 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 n = asdl_seq_LEN(ge->ifs);
2743 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002744 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745 VISIT(c, expr, e);
2746 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2747 NEXT_BLOCK(c);
2748 ADDOP(c, POP_TOP);
2749 }
2750
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002751 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2753 return 0;
2754
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002755 /* only append after the last 'for' generator */
2756 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 VISIT(c, expr, elt);
2758 ADDOP(c, YIELD_VALUE);
2759 ADDOP(c, POP_TOP);
2760
2761 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002763 for (i = 0; i < n; i++) {
2764 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002765 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 compiler_use_next_block(c, if_cleanup);
2767
2768 ADDOP(c, POP_TOP);
2769 }
2770 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2771 compiler_use_next_block(c, anchor);
2772 ADDOP(c, POP_BLOCK);
2773 compiler_pop_fblock(c, LOOP, start);
2774 compiler_use_next_block(c, end);
2775
2776 return 1;
2777}
2778
2779static int
2780compiler_genexp(struct compiler *c, expr_ty e)
2781{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002782 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002783 PyCodeObject *co;
2784 expr_ty outermost_iter = ((comprehension_ty)
2785 (asdl_seq_GET(e->v.GeneratorExp.generators,
2786 0)))->iter;
2787
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002788 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002789 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002790 if (!name)
2791 return 0;
2792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793
2794 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2795 return 0;
2796 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2797 e->v.GeneratorExp.elt);
2798 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002799 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800 if (co == NULL)
2801 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002802
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002803 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002804 Py_DECREF(co);
2805
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 VISIT(c, expr, outermost_iter);
2807 ADDOP(c, GET_ITER);
2808 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809
2810 return 1;
2811}
2812
2813static int
2814compiler_visit_keyword(struct compiler *c, keyword_ty k)
2815{
2816 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2817 VISIT(c, expr, k->value);
2818 return 1;
2819}
2820
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002821/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822 whether they are true or false.
2823
2824 Return values: 1 for true, 0 for false, -1 for non-constant.
2825 */
2826
2827static int
2828expr_constant(expr_ty e)
2829{
2830 switch (e->kind) {
2831 case Num_kind:
2832 return PyObject_IsTrue(e->v.Num.n);
2833 case Str_kind:
2834 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002835 case Name_kind:
2836 /* __debug__ is not assignable, so we can optimize
2837 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002838 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002839 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002840 return ! Py_OptimizeFlag;
2841 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 default:
2843 return -1;
2844 }
2845}
2846
Guido van Rossumc2e20742006-02-27 22:32:47 +00002847/*
2848 Implements the with statement from PEP 343.
2849
2850 The semantics outlined in that PEP are as follows:
2851
2852 with EXPR as VAR:
2853 BLOCK
2854
2855 It is implemented roughly as:
2856
Guido van Rossumda5b7012006-05-02 19:47:52 +00002857 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002858 exit = context.__exit__ # not calling it
2859 value = context.__enter__()
2860 try:
2861 VAR = value # if VAR present in the syntax
2862 BLOCK
2863 finally:
2864 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002865 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002866 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002867 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002868 exit(*exc)
2869 */
2870static int
2871compiler_with(struct compiler *c, stmt_ty s)
2872{
Guido van Rossumda5b7012006-05-02 19:47:52 +00002873 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002874 basicblock *block, *finally;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002875 identifier tmpvalue = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002876
2877 assert(s->kind == With_kind);
2878
Guido van Rossumc2e20742006-02-27 22:32:47 +00002879 if (!enter_attr) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002880 enter_attr = PyString_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002881 if (!enter_attr)
2882 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002883 }
2884 if (!exit_attr) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002885 exit_attr = PyString_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002886 if (!exit_attr)
2887 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002888 }
2889
2890 block = compiler_new_block(c);
2891 finally = compiler_new_block(c);
2892 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002893 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002894
Guido van Rossumc2e20742006-02-27 22:32:47 +00002895 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002896 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002897 We need to do this rather than preserving it on the stack
2898 because SETUP_FINALLY remembers the stack level.
2899 We need to do the assignment *inside* the try/finally
2900 so that context.__exit__() is called when the assignment
2901 fails. But we need to call context.__enter__() *before*
2902 the try/finally so that if it fails we won't call
2903 context.__exit__().
2904 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002905 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002906 if (tmpvalue == NULL)
2907 return 0;
2908 PyArena_AddPyObject(c->c_arena, tmpvalue);
2909 }
2910
Guido van Rossumda5b7012006-05-02 19:47:52 +00002911 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002912 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002913
Nick Coghlan7af53be2008-03-07 14:13:28 +00002914 /* Squirrel away context.__exit__ by stuffing it under context */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002915 ADDOP(c, DUP_TOP);
2916 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
Nick Coghlan7af53be2008-03-07 14:13:28 +00002917 ADDOP(c, ROT_TWO);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002918
2919 /* Call context.__enter__() */
2920 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2921 ADDOP_I(c, CALL_FUNCTION, 0);
2922
2923 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002924 /* Store it in tmpvalue */
2925 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00002926 return 0;
2927 }
2928 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002929 /* Discard result from context.__enter__() */
2930 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002931 }
2932
2933 /* Start the try block */
2934 ADDOP_JREL(c, SETUP_FINALLY, finally);
2935
2936 compiler_use_next_block(c, block);
2937 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002938 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002939 }
2940
2941 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002942 /* Bind saved result of context.__enter__() to VAR */
2943 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00002944 !compiler_nameop(c, tmpvalue, Del))
2945 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002946 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002947 }
2948
2949 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002950 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002951
2952 /* End of try block; start the finally block */
2953 ADDOP(c, POP_BLOCK);
2954 compiler_pop_fblock(c, FINALLY_TRY, block);
2955
2956 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2957 compiler_use_next_block(c, finally);
2958 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002959 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002960
Nick Coghlan7af53be2008-03-07 14:13:28 +00002961 /* Finally block starts; context.__exit__ is on the stack under
2962 the exception or return information. Just issue our magic
2963 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002964 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002965
2966 /* Finally block ends. */
2967 ADDOP(c, END_FINALLY);
2968 compiler_pop_fblock(c, FINALLY_END, finally);
2969 return 1;
2970}
2971
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972static int
2973compiler_visit_expr(struct compiler *c, expr_ty e)
2974{
2975 int i, n;
2976
Neal Norwitzf733a012006-10-29 18:30:10 +00002977 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002978 set a new line number for the next instruction.
2979 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002980 if (e->lineno > c->u->u_lineno) {
2981 c->u->u_lineno = e->lineno;
2982 c->u->u_lineno_set = false;
2983 }
2984 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002985 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002986 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002987 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 VISIT(c, expr, e->v.BinOp.left);
2989 VISIT(c, expr, e->v.BinOp.right);
2990 ADDOP(c, binop(c, e->v.BinOp.op));
2991 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002992 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 VISIT(c, expr, e->v.UnaryOp.operand);
2994 ADDOP(c, unaryop(e->v.UnaryOp.op));
2995 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002996 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002998 case IfExp_kind:
2999 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003000 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003001 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00003002 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003004 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003005 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00003006 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003007 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00003008 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 }
3010 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003011 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003013 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 return compiler_genexp(c, e);
3015 case Yield_kind:
3016 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003017 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 if (e->v.Yield.value) {
3019 VISIT(c, expr, e->v.Yield.value);
3020 }
3021 else {
3022 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3023 }
3024 ADDOP(c, YIELD_VALUE);
3025 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003026 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003028 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 VISIT(c, expr, e->v.Repr.value);
3032 ADDOP(c, UNARY_CONVERT);
3033 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003034 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3036 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003037 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3039 break;
3040 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003041 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 if (e->v.Attribute.ctx != AugStore)
3043 VISIT(c, expr, e->v.Attribute.value);
3044 switch (e->v.Attribute.ctx) {
3045 case AugLoad:
3046 ADDOP(c, DUP_TOP);
3047 /* Fall through to load */
3048 case Load:
3049 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3050 break;
3051 case AugStore:
3052 ADDOP(c, ROT_TWO);
3053 /* Fall through to save */
3054 case Store:
3055 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3056 break;
3057 case Del:
3058 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3059 break;
3060 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003061 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003062 PyErr_SetString(PyExc_SystemError,
3063 "param invalid in attribute expression");
3064 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003065 }
3066 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003067 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003068 switch (e->v.Subscript.ctx) {
3069 case AugLoad:
3070 VISIT(c, expr, e->v.Subscript.value);
3071 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3072 break;
3073 case Load:
3074 VISIT(c, expr, e->v.Subscript.value);
3075 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3076 break;
3077 case AugStore:
3078 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3079 break;
3080 case Store:
3081 VISIT(c, expr, e->v.Subscript.value);
3082 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3083 break;
3084 case Del:
3085 VISIT(c, expr, e->v.Subscript.value);
3086 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3087 break;
3088 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003089 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003090 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003091 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003092 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003093 }
3094 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003095 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3097 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003098 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003099 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003100 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003101 return compiler_tuple(c, e);
3102 }
3103 return 1;
3104}
3105
3106static int
3107compiler_augassign(struct compiler *c, stmt_ty s)
3108{
3109 expr_ty e = s->v.AugAssign.target;
3110 expr_ty auge;
3111
3112 assert(s->kind == AugAssign_kind);
3113
3114 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003115 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
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));
3123 auge->v.Attribute.ctx = AugStore;
3124 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003125 break;
3126 case Subscript_kind:
3127 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003128 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003129 if (auge == NULL)
3130 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 VISIT(c, expr, auge);
3132 VISIT(c, expr, s->v.AugAssign.value);
3133 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003134 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003136 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003138 if (!compiler_nameop(c, e->v.Name.id, Load))
3139 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003140 VISIT(c, expr, s->v.AugAssign.value);
3141 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3142 return compiler_nameop(c, e->v.Name.id, Store);
3143 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003144 PyErr_Format(PyExc_SystemError,
3145 "invalid node type (%d) for augmented assignment",
3146 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003147 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 }
3149 return 1;
3150}
3151
3152static int
3153compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3154{
3155 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003156 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3157 PyErr_SetString(PyExc_SystemError,
3158 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003160 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 f = &c->u->u_fblock[c->u->u_nfblocks++];
3162 f->fb_type = t;
3163 f->fb_block = b;
3164 return 1;
3165}
3166
3167static void
3168compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3169{
3170 struct compiler_unit *u = c->u;
3171 assert(u->u_nfblocks > 0);
3172 u->u_nfblocks--;
3173 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3174 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3175}
3176
Jeremy Hylton82271f12006-10-04 02:24:52 +00003177static int
3178compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003179 int i;
3180 struct compiler_unit *u = c->u;
3181 for (i = 0; i < u->u_nfblocks; ++i) {
3182 if (u->u_fblock[i].fb_type == LOOP)
3183 return 1;
3184 }
3185 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003186}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187/* Raises a SyntaxError and returns 0.
3188 If something goes wrong, a different exception may be raised.
3189*/
3190
3191static int
3192compiler_error(struct compiler *c, const char *errstr)
3193{
3194 PyObject *loc;
3195 PyObject *u = NULL, *v = NULL;
3196
3197 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3198 if (!loc) {
3199 Py_INCREF(Py_None);
3200 loc = Py_None;
3201 }
3202 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3203 Py_None, loc);
3204 if (!u)
3205 goto exit;
3206 v = Py_BuildValue("(zO)", errstr, u);
3207 if (!v)
3208 goto exit;
3209 PyErr_SetObject(PyExc_SyntaxError, v);
3210 exit:
3211 Py_DECREF(loc);
3212 Py_XDECREF(u);
3213 Py_XDECREF(v);
3214 return 0;
3215}
3216
3217static int
3218compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003219 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003221 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003222
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003223 /* XXX this code is duplicated */
3224 switch (ctx) {
3225 case AugLoad: /* fall through to Load */
3226 case Load: op = BINARY_SUBSCR; break;
3227 case AugStore:/* fall through to Store */
3228 case Store: op = STORE_SUBSCR; break;
3229 case Del: op = DELETE_SUBSCR; break;
3230 case Param:
3231 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003232 "invalid %s kind %d in subscript\n",
3233 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003234 return 0;
3235 }
3236 if (ctx == AugLoad) {
3237 ADDOP_I(c, DUP_TOPX, 2);
3238 }
3239 else if (ctx == AugStore) {
3240 ADDOP(c, ROT_THREE);
3241 }
3242 ADDOP(c, op);
3243 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244}
3245
3246static int
3247compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3248{
3249 int n = 2;
3250 assert(s->kind == Slice_kind);
3251
3252 /* only handles the cases where BUILD_SLICE is emitted */
3253 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003254 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003255 }
3256 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003257 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003259
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003260 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003261 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 }
3263 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003264 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265 }
3266
3267 if (s->v.Slice.step) {
3268 n++;
3269 VISIT(c, expr, s->v.Slice.step);
3270 }
3271 ADDOP_I(c, BUILD_SLICE, n);
3272 return 1;
3273}
3274
3275static int
3276compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3277{
3278 int op = 0, slice_offset = 0, stack_count = 0;
3279
3280 assert(s->v.Slice.step == NULL);
3281 if (s->v.Slice.lower) {
3282 slice_offset++;
3283 stack_count++;
3284 if (ctx != AugStore)
3285 VISIT(c, expr, s->v.Slice.lower);
3286 }
3287 if (s->v.Slice.upper) {
3288 slice_offset += 2;
3289 stack_count++;
3290 if (ctx != AugStore)
3291 VISIT(c, expr, s->v.Slice.upper);
3292 }
3293
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003294 if (ctx == AugLoad) {
3295 switch (stack_count) {
3296 case 0: ADDOP(c, DUP_TOP); break;
3297 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3298 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3299 }
3300 }
3301 else if (ctx == AugStore) {
3302 switch (stack_count) {
3303 case 0: ADDOP(c, ROT_TWO); break;
3304 case 1: ADDOP(c, ROT_THREE); break;
3305 case 2: ADDOP(c, ROT_FOUR); break;
3306 }
3307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308
3309 switch (ctx) {
3310 case AugLoad: /* fall through to Load */
3311 case Load: op = SLICE; break;
3312 case AugStore:/* fall through to Store */
3313 case Store: op = STORE_SLICE; break;
3314 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003315 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003316 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003317 PyErr_SetString(PyExc_SystemError,
3318 "param invalid in simple slice");
3319 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320 }
3321
3322 ADDOP(c, op + slice_offset);
3323 return 1;
3324}
3325
3326static int
3327compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3328 expr_context_ty ctx)
3329{
3330 switch (s->kind) {
3331 case Ellipsis_kind:
3332 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3333 break;
3334 case Slice_kind:
3335 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 case Index_kind:
3337 VISIT(c, expr, s->v.Index.value);
3338 break;
3339 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003340 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003341 PyErr_SetString(PyExc_SystemError,
3342 "extended slice invalid in nested slice");
3343 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344 }
3345 return 1;
3346}
3347
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348static int
3349compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3350{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003351 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003352 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003353 case Index_kind:
3354 kindname = "index";
3355 if (ctx != AugStore) {
3356 VISIT(c, expr, s->v.Index.value);
3357 }
3358 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003359 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003360 kindname = "ellipsis";
3361 if (ctx != AugStore) {
3362 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 break;
3365 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003366 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003367 if (!s->v.Slice.step)
3368 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003369 if (ctx != AugStore) {
3370 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 return 0;
3372 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003373 break;
3374 case ExtSlice_kind:
3375 kindname = "extended slice";
3376 if (ctx != AugStore) {
3377 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3378 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003379 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003380 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003381 if (!compiler_visit_nested_slice(c, sub, ctx))
3382 return 0;
3383 }
3384 ADDOP_I(c, BUILD_TUPLE, n);
3385 }
3386 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003387 default:
3388 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003389 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003390 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003392 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393}
3394
Neal Norwitzf733a012006-10-29 18:30:10 +00003395
3396/* End of the compiler section, beginning of the assembler section */
3397
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398/* do depth-first search of basic block graph, starting with block.
3399 post records the block indices in post-order.
3400
3401 XXX must handle implicit jumps from one block to next
3402*/
3403
Neal Norwitzf733a012006-10-29 18:30:10 +00003404struct assembler {
3405 PyObject *a_bytecode; /* string containing bytecode */
3406 int a_offset; /* offset into bytecode */
3407 int a_nblocks; /* number of reachable blocks */
3408 basicblock **a_postorder; /* list of blocks in dfs postorder */
3409 PyObject *a_lnotab; /* string containing lnotab */
3410 int a_lnotab_off; /* offset into lnotab */
3411 int a_lineno; /* last lineno of emitted instruction */
3412 int a_lineno_off; /* bytecode offset of last lineno */
3413};
3414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415static void
3416dfs(struct compiler *c, basicblock *b, struct assembler *a)
3417{
3418 int i;
3419 struct instr *instr = NULL;
3420
3421 if (b->b_seen)
3422 return;
3423 b->b_seen = 1;
3424 if (b->b_next != NULL)
3425 dfs(c, b->b_next, a);
3426 for (i = 0; i < b->b_iused; i++) {
3427 instr = &b->b_instr[i];
3428 if (instr->i_jrel || instr->i_jabs)
3429 dfs(c, instr->i_target, a);
3430 }
3431 a->a_postorder[a->a_nblocks++] = b;
3432}
3433
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003434static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3436{
3437 int i;
3438 struct instr *instr;
3439 if (b->b_seen || b->b_startdepth >= depth)
3440 return maxdepth;
3441 b->b_seen = 1;
3442 b->b_startdepth = depth;
3443 for (i = 0; i < b->b_iused; i++) {
3444 instr = &b->b_instr[i];
3445 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3446 if (depth > maxdepth)
3447 maxdepth = depth;
3448 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3449 if (instr->i_jrel || instr->i_jabs) {
3450 maxdepth = stackdepth_walk(c, instr->i_target,
3451 depth, maxdepth);
3452 if (instr->i_opcode == JUMP_ABSOLUTE ||
3453 instr->i_opcode == JUMP_FORWARD) {
3454 goto out; /* remaining code is dead */
3455 }
3456 }
3457 }
3458 if (b->b_next)
3459 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3460out:
3461 b->b_seen = 0;
3462 return maxdepth;
3463}
3464
3465/* Find the flow path that needs the largest stack. We assume that
3466 * cycles in the flow graph have no net effect on the stack depth.
3467 */
3468static int
3469stackdepth(struct compiler *c)
3470{
3471 basicblock *b, *entryblock;
3472 entryblock = NULL;
3473 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3474 b->b_seen = 0;
3475 b->b_startdepth = INT_MIN;
3476 entryblock = b;
3477 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003478 if (!entryblock)
3479 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 return stackdepth_walk(c, entryblock, 0, 0);
3481}
3482
3483static int
3484assemble_init(struct assembler *a, int nblocks, int firstlineno)
3485{
3486 memset(a, 0, sizeof(struct assembler));
3487 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003488 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 if (!a->a_bytecode)
3490 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003491 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 if (!a->a_lnotab)
3493 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003494 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3495 PyErr_NoMemory();
3496 return 0;
3497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003498 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003499 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003500 if (!a->a_postorder) {
3501 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003502 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 return 1;
3505}
3506
3507static void
3508assemble_free(struct assembler *a)
3509{
3510 Py_XDECREF(a->a_bytecode);
3511 Py_XDECREF(a->a_lnotab);
3512 if (a->a_postorder)
3513 PyObject_Free(a->a_postorder);
3514}
3515
3516/* Return the size of a basic block in bytes. */
3517
3518static int
3519instrsize(struct instr *instr)
3520{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003521 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003522 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003523 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003524 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3525 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
3528static int
3529blocksize(basicblock *b)
3530{
3531 int i;
3532 int size = 0;
3533
3534 for (i = 0; i < b->b_iused; i++)
3535 size += instrsize(&b->b_instr[i]);
3536 return size;
3537}
3538
3539/* All about a_lnotab.
3540
3541c_lnotab is an array of unsigned bytes disguised as a Python string.
3542It is used to map bytecode offsets to source code line #s (when needed
3543for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003544
Tim Peters2a7f3842001-06-09 09:26:21 +00003545The array is conceptually a list of
3546 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003547pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003548
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003549 byte code offset source code line number
3550 0 1
3551 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003552 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003553 350 307
3554 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003555
3556The first trick is that these numbers aren't stored, only the increments
3557from one row to the next (this doesn't really work, but it's a start):
3558
3559 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3560
3561The second trick is that an unsigned byte can't hold negative values, or
3562values larger than 255, so (a) there's a deep assumption that byte code
3563offsets and their corresponding line #s both increase monotonically, and (b)
3564if at least one column jumps by more than 255 from one row to the next, more
3565than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003566from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003567part. A user of c_lnotab desiring to find the source line number
3568corresponding to a bytecode address A should do something like this
3569
3570 lineno = addr = 0
3571 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003572 addr += addr_incr
3573 if addr > A:
3574 return lineno
3575 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003576
3577In order for this to work, when the addr field increments by more than 255,
3578the line # increment in each pair generated must be 0 until the remaining addr
Neal Norwitz84be93b2006-07-16 01:50:38 +00003579increment is < 256. So, in the example above, assemble_lnotab (it used
3580to be called com_set_lineno) should not (as was actually done until 2.2)
3581expand 300, 300 to 255, 255, 45, 45,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003582 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003583*/
3584
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003585static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003587{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 int d_bytecode, d_lineno;
3589 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003590 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591
3592 d_bytecode = a->a_offset - a->a_lineno_off;
3593 d_lineno = i->i_lineno - a->a_lineno;
3594
3595 assert(d_bytecode >= 0);
3596 assert(d_lineno >= 0);
3597
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003598 if(d_bytecode == 0 && d_lineno == 0)
3599 return 1;
3600
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003602 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003604 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003606 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003608 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003610 else {
3611 PyErr_NoMemory();
3612 return 0;
3613 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003614 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003615 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003616 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003617 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003618 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003619 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 *lnotab++ = 255;
3621 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 d_bytecode -= ncodes * 255;
3624 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003625 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 assert(d_bytecode <= 255);
3627 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003628 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003629 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003630 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003632 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003634 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003635 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003636 else {
3637 PyErr_NoMemory();
3638 return 0;
3639 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003640 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003642 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003643 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003644 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003646 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003648 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003650 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 d_lineno -= ncodes * 255;
3653 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003654 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003655
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003656 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003658 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003659 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003660 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003661 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003662 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003663
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 a->a_lnotab_off += 2;
3665 if (d_bytecode) {
3666 *lnotab++ = d_bytecode;
3667 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003668 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003669 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 *lnotab++ = 0;
3671 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003672 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 a->a_lineno = i->i_lineno;
3674 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003675 return 1;
3676}
3677
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678/* assemble_emit()
3679 Extend the bytecode with a new instruction.
3680 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003681*/
3682
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003683static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003685{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003686 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003687 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 char *code;
3689
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003690 size = instrsize(i);
3691 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003692 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003693 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003694 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003696 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003698 if (len > PY_SSIZE_T_MAX / 2)
3699 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003700 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003701 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003702 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003703 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003705 if (size == 6) {
3706 assert(i->i_hasarg);
3707 *code++ = (char)EXTENDED_ARG;
3708 *code++ = ext & 0xff;
3709 *code++ = ext >> 8;
3710 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003711 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003712 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003713 if (i->i_hasarg) {
3714 assert(size == 3 || size == 6);
3715 *code++ = arg & 0xff;
3716 *code++ = arg >> 8;
3717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003718 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003719}
3720
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003721static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003723{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003724 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003725 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003726 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003727
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003728 /* Compute the size of each block and fixup jump args.
3729 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003730start:
3731 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003733 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734 bsize = blocksize(b);
3735 b->b_offset = totsize;
3736 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003737 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003738 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3740 bsize = b->b_offset;
3741 for (i = 0; i < b->b_iused; i++) {
3742 struct instr *instr = &b->b_instr[i];
3743 /* Relative jumps are computed relative to
3744 the instruction pointer after fetching
3745 the jump instruction.
3746 */
3747 bsize += instrsize(instr);
3748 if (instr->i_jabs)
3749 instr->i_oparg = instr->i_target->b_offset;
3750 else if (instr->i_jrel) {
3751 int delta = instr->i_target->b_offset - bsize;
3752 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003753 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003754 else
3755 continue;
3756 if (instr->i_oparg > 0xffff)
3757 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003758 }
3759 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003760
3761 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003762 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003763 with a better solution.
3764
3765 In the meantime, should the goto be dropped in favor
3766 of a loop?
3767
3768 The issue is that in the first loop blocksize() is called
3769 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003770 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003771 i_oparg is calculated in the second loop above.
3772
3773 So we loop until we stop seeing new EXTENDED_ARGs.
3774 The only EXTENDED_ARGs that could be popping up are
3775 ones in jump instructions. So this should converge
3776 fairly quickly.
3777 */
3778 if (last_extended_arg_count != extended_arg_count) {
3779 last_extended_arg_count = extended_arg_count;
3780 goto start;
3781 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003782}
3783
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003784static PyObject *
3785dict_keys_inorder(PyObject *dict, int offset)
3786{
3787 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003788 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003789
3790 tuple = PyTuple_New(size);
3791 if (tuple == NULL)
3792 return NULL;
3793 while (PyDict_Next(dict, &pos, &k, &v)) {
3794 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003795 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003796 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003797 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003798 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003799 PyTuple_SET_ITEM(tuple, i - offset, k);
3800 }
3801 return tuple;
3802}
3803
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003804static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003806{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003807 PySTEntryObject *ste = c->u->u_ste;
3808 int flags = 0, n;
3809 if (ste->ste_type != ModuleBlock)
3810 flags |= CO_NEWLOCALS;
3811 if (ste->ste_type == FunctionBlock) {
3812 if (!ste->ste_unoptimized)
3813 flags |= CO_OPTIMIZED;
3814 if (ste->ste_nested)
3815 flags |= CO_NESTED;
3816 if (ste->ste_generator)
3817 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 if (ste->ste_varargs)
3820 flags |= CO_VARARGS;
3821 if (ste->ste_varkeywords)
3822 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003823 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003825
3826 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003827 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003828
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 n = PyDict_Size(c->u->u_freevars);
3830 if (n < 0)
3831 return -1;
3832 if (n == 0) {
3833 n = PyDict_Size(c->u->u_cellvars);
3834 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003835 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836 if (n == 0) {
3837 flags |= CO_NOFREE;
3838 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003839 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003840
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003841 return flags;
3842}
3843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844static PyCodeObject *
3845makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003846{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847 PyObject *tmp;
3848 PyCodeObject *co = NULL;
3849 PyObject *consts = NULL;
3850 PyObject *names = NULL;
3851 PyObject *varnames = NULL;
3852 PyObject *filename = NULL;
3853 PyObject *name = NULL;
3854 PyObject *freevars = NULL;
3855 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003856 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003858
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003859 tmp = dict_keys_inorder(c->u->u_consts, 0);
3860 if (!tmp)
3861 goto error;
3862 consts = PySequence_List(tmp); /* optimize_code requires a list */
3863 Py_DECREF(tmp);
3864
3865 names = dict_keys_inorder(c->u->u_names, 0);
3866 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3867 if (!consts || !names || !varnames)
3868 goto error;
3869
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003870 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3871 if (!cellvars)
3872 goto error;
3873 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3874 if (!freevars)
3875 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003876 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 if (!filename)
3878 goto error;
3879
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003880 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003881 flags = compute_code_flags(c);
3882 if (flags < 0)
3883 goto error;
3884
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003885 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 if (!bytecode)
3887 goto error;
3888
3889 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3890 if (!tmp)
3891 goto error;
3892 Py_DECREF(consts);
3893 consts = tmp;
3894
3895 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3896 bytecode, consts, names, varnames,
3897 freevars, cellvars,
3898 filename, c->u->u_name,
3899 c->u->u_firstlineno,
3900 a->a_lnotab);
3901 error:
3902 Py_XDECREF(consts);
3903 Py_XDECREF(names);
3904 Py_XDECREF(varnames);
3905 Py_XDECREF(filename);
3906 Py_XDECREF(name);
3907 Py_XDECREF(freevars);
3908 Py_XDECREF(cellvars);
3909 Py_XDECREF(bytecode);
3910 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003911}
3912
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003913
3914/* For debugging purposes only */
3915#if 0
3916static void
3917dump_instr(const struct instr *i)
3918{
3919 const char *jrel = i->i_jrel ? "jrel " : "";
3920 const char *jabs = i->i_jabs ? "jabs " : "";
3921 char arg[128];
3922
3923 *arg = '\0';
3924 if (i->i_hasarg)
3925 sprintf(arg, "arg: %d ", i->i_oparg);
3926
3927 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3928 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3929}
3930
3931static void
3932dump_basicblock(const basicblock *b)
3933{
3934 const char *seen = b->b_seen ? "seen " : "";
3935 const char *b_return = b->b_return ? "return " : "";
3936 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3937 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3938 if (b->b_instr) {
3939 int i;
3940 for (i = 0; i < b->b_iused; i++) {
3941 fprintf(stderr, " [%02d] ", i);
3942 dump_instr(b->b_instr + i);
3943 }
3944 }
3945}
3946#endif
3947
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003948static PyCodeObject *
3949assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003950{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951 basicblock *b, *entryblock;
3952 struct assembler a;
3953 int i, j, nblocks;
3954 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003955
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956 /* Make sure every block that falls off the end returns None.
3957 XXX NEXT_BLOCK() isn't quite right, because if the last
3958 block ends with a jump or return b_next shouldn't set.
3959 */
3960 if (!c->u->u_curblock->b_return) {
3961 NEXT_BLOCK(c);
3962 if (addNone)
3963 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3964 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003965 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003966
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967 nblocks = 0;
3968 entryblock = NULL;
3969 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3970 nblocks++;
3971 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003972 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003973
Neal Norwitzed657552006-07-10 00:04:44 +00003974 /* Set firstlineno if it wasn't explicitly set. */
3975 if (!c->u->u_firstlineno) {
3976 if (entryblock && entryblock->b_instr)
3977 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3978 else
3979 c->u->u_firstlineno = 1;
3980 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3982 goto error;
3983 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003984
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003985 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003986 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 /* Emit code in reverse postorder from dfs. */
3989 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003990 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 for (j = 0; j < b->b_iused; j++)
3992 if (!assemble_emit(&a, &b->b_instr[j]))
3993 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003994 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003995
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003996 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003997 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003998 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004000
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 co = makecode(c, &a);
4002 error:
4003 assemble_free(&a);
4004 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004005}