blob: 88d54ab9445a51e9f52c1c7741621ccf9d852b6d [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Jeremy Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Neal Norwitzf733a012006-10-29 18:30:10 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Jeremy Hylton819de6c2007-02-27 16:13:23 +000011 * this file.
Neal Norwitzf733a012006-10-29 18:30:10 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Neal Norwitzf733a012006-10-29 18:30:10 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000043 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000050};
51
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052typedef struct basicblock_ {
Jeremy Hylton12603c42006-04-01 16:18:02 +000053 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000067 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000069 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000073 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074} basicblock;
75
76/* fblockinfo tracks the current frame block.
77
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078A frame block is used to handle loops, try/except, and try/finally.
79It's called a frame block to distinguish it from a basic block in the
80compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081*/
82
83enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
84
85struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000086 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 basicblock *fb_block;
88};
89
90/* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
92*/
93struct compiler_unit {
94 PySTEntryObject *u_ste;
95
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +000098 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 the argument for opcodes that refer to those collections.
100 */
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
106
107 PyObject *u_private; /* for private name mangling */
108
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000109 int u_argcount; /* number of arguments for block */
Neal Norwitzf733a012006-10-29 18:30:10 +0000110 /* Pointer to the most recently allocated block. By following b_list
111 members, you can reach all early allocated blocks. */
Jeremy Hylton12603c42006-04-01 16:18:02 +0000112 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 basicblock *u_curblock; /* pointer to current block */
Neal Norwitzf733a012006-10-29 18:30:10 +0000114 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115
116 int u_nfblocks;
117 struct fblockinfo u_fblock[CO_MAXBLOCKS];
118
119 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000120 int u_lineno; /* the lineno for the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121 bool u_lineno_set; /* boolean to indicate whether instr
122 has been generated with current lineno */
123};
124
125/* This struct captures the global state of a compilation.
126
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000127The u pointer points to the current compilation unit, while units
128for enclosing blocks are stored in c_stack. The u and c_stack are
129managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000130*/
131
132struct compiler {
133 const char *c_filename;
134 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000135 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136 PyCompilerFlags *c_flags;
137
Neal Norwitz4ffedad2006-08-04 04:58:47 +0000138 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000139 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141 struct compiler_unit *u; /* compiler state for current block */
142 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145};
146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147static int compiler_enter_scope(struct compiler *, identifier, void *, int);
148static void compiler_free(struct compiler *);
149static basicblock *compiler_new_block(struct compiler *);
150static int compiler_next_instr(struct compiler *, basicblock *);
151static int compiler_addop(struct compiler *, int);
152static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
153static int compiler_addop_i(struct compiler *, int, int);
154static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000155static basicblock *compiler_use_new_block(struct compiler *);
156static int compiler_error(struct compiler *, const char *);
157static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
158
159static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
160static int compiler_visit_stmt(struct compiler *, stmt_ty);
161static int compiler_visit_keyword(struct compiler *, keyword_ty);
162static int compiler_visit_expr(struct compiler *, expr_ty);
163static int compiler_augassign(struct compiler *, stmt_ty);
164static int compiler_visit_slice(struct compiler *, slice_ty,
165 expr_context_ty);
166
167static int compiler_push_fblock(struct compiler *, enum fblocktype,
168 basicblock *);
169static void compiler_pop_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
Jeremy Hylton82271f12006-10-04 02:24:52 +0000171/* Returns true if there is a loop on the fblock stack. */
172static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173
174static int inplace_binop(struct compiler *, operator_ty);
175static int expr_constant(expr_ty e);
176
Guido van Rossumc2e20742006-02-27 22:32:47 +0000177static int compiler_with(struct compiler *, stmt_ty);
178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static PyCodeObject *assemble(struct compiler *, int addNone);
180static PyObject *__doc__;
181
182PyObject *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000183_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000184{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185 /* Name mangling: __private becomes _classname__private.
186 This is independent from how the name is used. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000187 const char *p, *name = PyString_AsString(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000188 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189 size_t nlen, plen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000190 if (privateobj == NULL || !PyString_Check(privateobj) ||
Neal Norwitz84167d02006-08-12 01:45:47 +0000191 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000192 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000194 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000195 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 nlen = strlen(name);
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000197 /* Don't mangle __id__ or names with dots.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000198
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000199 The only time a name with a dot can occur is when
200 we are compiling an import statement that has a
201 package name.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000202
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000203 TODO(jhylton): Decide whether we want to support
204 mangling of the module name, e.g. __M.X.
205 */
Jeremy Hylton37075c52007-02-27 01:01:59 +0000206 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000207 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 /* Strip leading underscores from class name */
212 while (*p == '_')
213 p++;
214 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000215 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 plen = strlen(p);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000219
220 assert(1 <= PY_SSIZE_T_MAX - nlen);
221 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
222
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000223 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000224 if (!ident)
225 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000227 buffer = PyString_AS_STRING(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 strncpy(buffer+1, p, plen);
230 strcpy(buffer+1+plen, name);
231 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000232}
233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234static int
235compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000236{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 c->c_stack = PyList_New(0);
240 if (!c->c_stack)
241 return 0;
242
243 return 1;
244}
245
246PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000247PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000248 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249{
250 struct compiler c;
251 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000252 PyCompilerFlags local_flags;
253 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000255 if (!__doc__) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000256 __doc__ = PyString_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000257 if (!__doc__)
258 return NULL;
259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260
261 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000262 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000264 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265 c.c_future = PyFuture_FromAST(mod, filename);
266 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000267 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000269 local_flags.cf_flags = 0;
270 flags = &local_flags;
271 }
272 merged = c.c_future->ff_features | flags->cf_flags;
273 c.c_future->ff_features = merged;
274 flags->cf_flags = merged;
275 c.c_flags = flags;
276 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277
278 c.c_st = PySymtable_Build(mod, filename, c.c_future);
279 if (c.c_st == NULL) {
280 if (!PyErr_Occurred())
281 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000282 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 }
284
285 /* XXX initialize to NULL for now, need to handle */
286 c.c_encoding = NULL;
287
288 co = compiler_mod(&c, mod);
289
Thomas Wouters1175c432006-02-27 22:49:54 +0000290 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000291 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000292 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293 return co;
294}
295
296PyCodeObject *
297PyNode_Compile(struct _node *n, const char *filename)
298{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000299 PyCodeObject *co = NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000300 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000301 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000302 if (!arena)
303 return NULL;
304 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000305 if (mod)
306 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000307 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000308 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000309}
310
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000311static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000312compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000313{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 if (c->c_st)
315 PySymtable_Free(c->c_st);
316 if (c->c_future)
Neal Norwitz14bc4e42006-04-10 06:57:06 +0000317 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319}
320
Guido van Rossum79f25d91997-04-29 20:08:16 +0000321static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000323{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000324 Py_ssize_t i, n;
Georg Brandl5c170fd2006-03-17 19:03:25 +0000325 PyObject *v, *k;
326 PyObject *dict = PyDict_New();
327 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000328
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329 n = PyList_Size(list);
330 for (i = 0; i < n; i++) {
331 v = PyInt_FromLong(i);
332 if (!v) {
333 Py_DECREF(dict);
334 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000335 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000336 k = PyList_GET_ITEM(list, i);
Georg Brandl7784f122006-05-26 20:04:44 +0000337 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
339 Py_XDECREF(k);
340 Py_DECREF(v);
341 Py_DECREF(dict);
342 return NULL;
343 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000344 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000346 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000347 return dict;
348}
349
350/* Return new dict containing names from src that match scope(s).
351
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000352src is a symbol table dictionary. If the scope of a name matches
353either scope_type or flag is set, insert it into the new dict. The
354values are integers, starting at offset and increasing by one for
355each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356*/
357
358static PyObject *
359dictbytype(PyObject *src, int scope_type, int flag, int offset)
360{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000361 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362 PyObject *k, *v, *dest = PyDict_New();
363
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000364 assert(offset >= 0);
365 if (dest == NULL)
366 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367
368 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000369 /* XXX this should probably be a macro in symtable.h */
370 assert(PyInt_Check(v));
371 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000373 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
374 PyObject *tuple, *item = PyInt_FromLong(i);
375 if (item == NULL) {
376 Py_DECREF(dest);
377 return NULL;
378 }
379 i++;
Georg Brandl7784f122006-05-26 20:04:44 +0000380 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000381 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
382 Py_DECREF(item);
383 Py_DECREF(dest);
384 Py_XDECREF(tuple);
385 return NULL;
386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000388 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390 }
391 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000392}
393
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394static void
395compiler_unit_check(struct compiler_unit *u)
396{
397 basicblock *block;
398 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Brett Cannona6c41bc2008-02-07 07:47:31 +0000399 assert((void *)block != (void *)0xcbcbcbcb);
400 assert((void *)block != (void *)0xfbfbfbfb);
401 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402 if (block->b_instr != NULL) {
403 assert(block->b_ialloc > 0);
404 assert(block->b_iused > 0);
405 assert(block->b_ialloc >= block->b_iused);
406 }
407 else {
408 assert (block->b_iused == 0);
409 assert (block->b_ialloc == 0);
410 }
411 }
412}
413
414static void
415compiler_unit_free(struct compiler_unit *u)
416{
417 basicblock *b, *next;
418
419 compiler_unit_check(u);
420 b = u->u_blocks;
421 while (b != NULL) {
422 if (b->b_instr)
423 PyObject_Free((void *)b->b_instr);
424 next = b->b_list;
425 PyObject_Free((void *)b);
426 b = next;
427 }
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000428 Py_CLEAR(u->u_ste);
429 Py_CLEAR(u->u_name);
430 Py_CLEAR(u->u_consts);
431 Py_CLEAR(u->u_names);
432 Py_CLEAR(u->u_varnames);
433 Py_CLEAR(u->u_freevars);
434 Py_CLEAR(u->u_cellvars);
435 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 PyObject_Free(u);
437}
438
439static int
440compiler_enter_scope(struct compiler *c, identifier name, void *key,
441 int lineno)
442{
443 struct compiler_unit *u;
444
Anthony Baxter7b782b62006-04-11 12:01:56 +0000445 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000446 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000447 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000448 PyErr_NoMemory();
449 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000450 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000451 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 u->u_argcount = 0;
453 u->u_ste = PySymtable_Lookup(c->c_st, key);
454 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000455 compiler_unit_free(u);
456 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457 }
458 Py_INCREF(name);
459 u->u_name = name;
460 u->u_varnames = list2dict(u->u_ste->ste_varnames);
461 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000462 if (!u->u_varnames || !u->u_cellvars) {
463 compiler_unit_free(u);
464 return 0;
465 }
466
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000468 PyDict_Size(u->u_cellvars));
Neal Norwitzd12bd012006-07-21 07:59:47 +0000469 if (!u->u_freevars) {
470 compiler_unit_free(u);
471 return 0;
472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
474 u->u_blocks = NULL;
475 u->u_tmpname = 0;
476 u->u_nfblocks = 0;
477 u->u_firstlineno = lineno;
478 u->u_lineno = 0;
479 u->u_lineno_set = false;
480 u->u_consts = PyDict_New();
481 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000482 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 return 0;
484 }
485 u->u_names = PyDict_New();
486 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000487 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 return 0;
489 }
490
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000491 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
493 /* Push the old compiler_unit on the stack. */
494 if (c->u) {
495 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000496 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
497 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 return 0;
500 }
501 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000502 u->u_private = c->u->u_private;
503 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 }
505 c->u = u;
506
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000507 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000508 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 return 0;
510
511 return 1;
512}
513
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000514static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515compiler_exit_scope(struct compiler *c)
516{
517 int n;
518 PyObject *wrapper;
519
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000520 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 compiler_unit_free(c->u);
522 /* Restore c->u to the parent unit. */
523 n = PyList_GET_SIZE(c->c_stack) - 1;
524 if (n >= 0) {
525 wrapper = PyList_GET_ITEM(c->c_stack, n);
526 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neal Norwitz87557cd2006-08-21 18:01:30 +0000527 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000528 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000530 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 compiler_unit_check(c->u);
532 }
533 else
534 c->u = NULL;
535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536}
537
Guido van Rossumc2e20742006-02-27 22:32:47 +0000538/* Allocate a new "anonymous" local variable.
539 Used by list comprehensions and with statements.
540*/
541
542static PyObject *
543compiler_new_tmpname(struct compiler *c)
544{
545 char tmpname[256];
546 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000547 return PyString_FromString(tmpname);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000548}
549
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550/* Allocate a new block and return a pointer to it.
551 Returns NULL on error.
552*/
553
554static basicblock *
555compiler_new_block(struct compiler *c)
556{
557 basicblock *b;
558 struct compiler_unit *u;
559
560 u = c->u;
561 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000562 if (b == NULL) {
563 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000565 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 memset((void *)b, 0, sizeof(basicblock));
Neal Norwitzf733a012006-10-29 18:30:10 +0000567 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000568 b->b_list = u->u_blocks;
569 u->u_blocks = b;
570 return b;
571}
572
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573static basicblock *
574compiler_use_new_block(struct compiler *c)
575{
576 basicblock *block = compiler_new_block(c);
577 if (block == NULL)
578 return NULL;
579 c->u->u_curblock = block;
580 return block;
581}
582
583static basicblock *
584compiler_next_block(struct compiler *c)
585{
586 basicblock *block = compiler_new_block(c);
587 if (block == NULL)
588 return NULL;
589 c->u->u_curblock->b_next = block;
590 c->u->u_curblock = block;
591 return block;
592}
593
594static basicblock *
595compiler_use_next_block(struct compiler *c, basicblock *block)
596{
597 assert(block != NULL);
598 c->u->u_curblock->b_next = block;
599 c->u->u_curblock = block;
600 return block;
601}
602
603/* Returns the offset of the next instruction in the current block's
604 b_instr array. Resizes the b_instr as necessary.
605 Returns -1 on failure.
Neal Norwitzf733a012006-10-29 18:30:10 +0000606*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
608static int
609compiler_next_instr(struct compiler *c, basicblock *b)
610{
611 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000612 if (b->b_instr == NULL) {
Anthony Baxter7b782b62006-04-11 12:01:56 +0000613 b->b_instr = (struct instr *)PyObject_Malloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000614 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 if (b->b_instr == NULL) {
616 PyErr_NoMemory();
617 return -1;
618 }
619 b->b_ialloc = DEFAULT_BLOCK_SIZE;
620 memset((char *)b->b_instr, 0,
621 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000622 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 else if (b->b_iused == b->b_ialloc) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000624 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 size_t oldsize, newsize;
626 oldsize = b->b_ialloc * sizeof(struct instr);
627 newsize = oldsize << 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000628
629 if (oldsize > (PY_SIZE_MAX >> 1)) {
630 PyErr_NoMemory();
631 return -1;
632 }
633
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 if (newsize == 0) {
635 PyErr_NoMemory();
636 return -1;
637 }
638 b->b_ialloc <<= 1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000639 tmp = (struct instr *)PyObject_Realloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000640 (void *)b->b_instr, newsize);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000641 if (tmp == NULL) {
642 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000643 return -1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000644 }
645 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
647 }
648 return b->b_iused++;
649}
650
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000651/* Set the i_lineno member of the instruction at offset off if the
652 line number for the current expression/statement has not
Jeremy Hylton12603c42006-04-01 16:18:02 +0000653 already been set. If it has been set, the call has no effect.
654
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000655 The line number is reset in the following cases:
656 - when entering a new scope
657 - on each statement
658 - on each expression that start a new line
659 - before the "except" clause
660 - before the "for" and "while" expressions
Neal Norwitzf733a012006-10-29 18:30:10 +0000661*/
Jeremy Hylton12603c42006-04-01 16:18:02 +0000662
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663static void
664compiler_set_lineno(struct compiler *c, int off)
665{
666 basicblock *b;
667 if (c->u->u_lineno_set)
668 return;
669 c->u->u_lineno_set = true;
670 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000671 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672}
673
674static int
675opcode_stack_effect(int opcode, int oparg)
676{
677 switch (opcode) {
678 case POP_TOP:
679 return -1;
680 case ROT_TWO:
681 case ROT_THREE:
682 return 0;
683 case DUP_TOP:
684 return 1;
685 case ROT_FOUR:
686 return 0;
687
688 case UNARY_POSITIVE:
689 case UNARY_NEGATIVE:
690 case UNARY_NOT:
691 case UNARY_CONVERT:
692 case UNARY_INVERT:
693 return 0;
694
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000695 case LIST_APPEND:
696 return -2;
697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 case BINARY_POWER:
699 case BINARY_MULTIPLY:
700 case BINARY_DIVIDE:
701 case BINARY_MODULO:
702 case BINARY_ADD:
703 case BINARY_SUBTRACT:
704 case BINARY_SUBSCR:
705 case BINARY_FLOOR_DIVIDE:
706 case BINARY_TRUE_DIVIDE:
707 return -1;
708 case INPLACE_FLOOR_DIVIDE:
709 case INPLACE_TRUE_DIVIDE:
710 return -1;
711
712 case SLICE+0:
713 return 1;
714 case SLICE+1:
715 return 0;
716 case SLICE+2:
717 return 0;
718 case SLICE+3:
719 return -1;
720
721 case STORE_SLICE+0:
722 return -2;
723 case STORE_SLICE+1:
724 return -3;
725 case STORE_SLICE+2:
726 return -3;
727 case STORE_SLICE+3:
728 return -4;
729
730 case DELETE_SLICE+0:
731 return -1;
732 case DELETE_SLICE+1:
733 return -2;
734 case DELETE_SLICE+2:
735 return -2;
736 case DELETE_SLICE+3:
737 return -3;
738
739 case INPLACE_ADD:
740 case INPLACE_SUBTRACT:
741 case INPLACE_MULTIPLY:
742 case INPLACE_DIVIDE:
743 case INPLACE_MODULO:
744 return -1;
745 case STORE_SUBSCR:
746 return -3;
Raymond Hettingereffde122007-12-18 18:26:18 +0000747 case STORE_MAP:
748 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 case DELETE_SUBSCR:
750 return -2;
751
752 case BINARY_LSHIFT:
753 case BINARY_RSHIFT:
754 case BINARY_AND:
755 case BINARY_XOR:
756 case BINARY_OR:
757 return -1;
758 case INPLACE_POWER:
759 return -1;
760 case GET_ITER:
761 return 0;
762
763 case PRINT_EXPR:
764 return -1;
765 case PRINT_ITEM:
766 return -1;
767 case PRINT_NEWLINE:
768 return 0;
769 case PRINT_ITEM_TO:
770 return -2;
771 case PRINT_NEWLINE_TO:
772 return -1;
773 case INPLACE_LSHIFT:
774 case INPLACE_RSHIFT:
775 case INPLACE_AND:
776 case INPLACE_XOR:
777 case INPLACE_OR:
778 return -1;
779 case BREAK_LOOP:
780 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000781 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000782 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 case LOAD_LOCALS:
784 return 1;
785 case RETURN_VALUE:
786 return -1;
787 case IMPORT_STAR:
788 return -1;
789 case EXEC_STMT:
790 return -3;
791 case YIELD_VALUE:
792 return 0;
793
794 case POP_BLOCK:
795 return 0;
796 case END_FINALLY:
797 return -1; /* or -2 or -3 if exception occurred */
798 case BUILD_CLASS:
799 return -2;
800
801 case STORE_NAME:
802 return -1;
803 case DELETE_NAME:
804 return 0;
805 case UNPACK_SEQUENCE:
806 return oparg-1;
807 case FOR_ITER:
808 return 1;
809
810 case STORE_ATTR:
811 return -2;
812 case DELETE_ATTR:
813 return -1;
814 case STORE_GLOBAL:
815 return -1;
816 case DELETE_GLOBAL:
817 return 0;
818 case DUP_TOPX:
819 return oparg;
820 case LOAD_CONST:
821 return 1;
822 case LOAD_NAME:
823 return 1;
824 case BUILD_TUPLE:
825 case BUILD_LIST:
826 return 1-oparg;
827 case BUILD_MAP:
828 return 1;
829 case LOAD_ATTR:
830 return 0;
831 case COMPARE_OP:
832 return -1;
833 case IMPORT_NAME:
834 return 0;
835 case IMPORT_FROM:
836 return 1;
837
838 case JUMP_FORWARD:
839 case JUMP_IF_FALSE:
840 case JUMP_IF_TRUE:
841 case JUMP_ABSOLUTE:
842 return 0;
843
844 case LOAD_GLOBAL:
845 return 1;
846
847 case CONTINUE_LOOP:
848 return 0;
849 case SETUP_LOOP:
850 return 0;
851 case SETUP_EXCEPT:
852 case SETUP_FINALLY:
853 return 3; /* actually pushed by an exception */
854
855 case LOAD_FAST:
856 return 1;
857 case STORE_FAST:
858 return -1;
859 case DELETE_FAST:
860 return 0;
861
862 case RAISE_VARARGS:
863 return -oparg;
864#define NARGS(o) (((o) % 256) + 2*((o) / 256))
865 case CALL_FUNCTION:
866 return -NARGS(oparg);
867 case CALL_FUNCTION_VAR:
868 case CALL_FUNCTION_KW:
869 return -NARGS(oparg)-1;
870 case CALL_FUNCTION_VAR_KW:
871 return -NARGS(oparg)-2;
872#undef NARGS
873 case MAKE_FUNCTION:
874 return -oparg;
875 case BUILD_SLICE:
876 if (oparg == 3)
877 return -2;
878 else
879 return -1;
880
881 case MAKE_CLOSURE:
882 return -oparg;
883 case LOAD_CLOSURE:
884 return 1;
885 case LOAD_DEREF:
886 return 1;
887 case STORE_DEREF:
888 return -1;
889 default:
890 fprintf(stderr, "opcode = %d\n", opcode);
891 Py_FatalError("opcode_stack_effect()");
892
893 }
894 return 0; /* not reachable */
895}
896
897/* Add an opcode with no argument.
898 Returns 0 on failure, 1 on success.
899*/
900
901static int
902compiler_addop(struct compiler *c, int opcode)
903{
904 basicblock *b;
905 struct instr *i;
906 int off;
907 off = compiler_next_instr(c, c->u->u_curblock);
908 if (off < 0)
909 return 0;
910 b = c->u->u_curblock;
911 i = &b->b_instr[off];
912 i->i_opcode = opcode;
913 i->i_hasarg = 0;
914 if (opcode == RETURN_VALUE)
915 b->b_return = 1;
916 compiler_set_lineno(c, off);
917 return 1;
918}
919
920static int
921compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
922{
923 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000924 Py_ssize_t arg;
Mark Dickinson105be772008-01-31 22:17:37 +0000925 unsigned char *p, *q;
926 Py_complex z;
927 double d;
928 int real_part_zero, imag_part_zero;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000930 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000931 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
932 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000933 d = PyFloat_AS_DOUBLE(o);
934 p = (unsigned char*) &d;
935 /* all we need is to make the tuple different in either the 0.0
936 * or -0.0 case from all others, just to avoid the "coercion".
937 */
938 if (*p==0 && p[sizeof(double)-1]==0)
939 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
940 else
941 t = PyTuple_Pack(2, o, o->ob_type);
942 }
943 else if (PyComplex_Check(o)) {
944 /* complex case is even messier: we need to make complex(x,
945 0.) different from complex(x, -0.) and complex(0., y)
946 different from complex(-0., y), for any x and y. In
947 particular, all four complex zeros should be
948 distinguished.*/
949 z = PyComplex_AsCComplex(o);
950 p = (unsigned char*) &(z.real);
951 q = (unsigned char*) &(z.imag);
952 /* all that matters here is that on IEEE platforms
953 real_part_zero will be true if z.real == 0., and false if
954 z.real == -0. In fact, real_part_zero will also be true
955 for some other rarely occurring nonzero floats, but this
956 doesn't matter. Similar comments apply to
957 imag_part_zero. */
958 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
959 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
960 if (real_part_zero && imag_part_zero) {
961 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
962 }
963 else if (real_part_zero && !imag_part_zero) {
964 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
965 }
966 else if (!real_part_zero && imag_part_zero) {
967 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
968 }
969 else {
970 t = PyTuple_Pack(2, o, o->ob_type);
971 }
972 }
973 else {
974 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000975 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000976 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000977 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978
979 v = PyDict_GetItem(dict, t);
980 if (!v) {
981 arg = PyDict_Size(dict);
982 v = PyInt_FromLong(arg);
983 if (!v) {
984 Py_DECREF(t);
985 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000986 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 if (PyDict_SetItem(dict, t, v) < 0) {
988 Py_DECREF(t);
989 Py_DECREF(v);
990 return -1;
991 }
992 Py_DECREF(v);
993 }
994 else
995 arg = PyInt_AsLong(v);
996 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000997 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998}
999
1000static int
1001compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1002 PyObject *o)
1003{
1004 int arg = compiler_add_o(c, dict, o);
1005 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001006 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 return compiler_addop_i(c, opcode, arg);
1008}
1009
1010static int
1011compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001012 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013{
1014 int arg;
1015 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1016 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001017 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018 arg = compiler_add_o(c, dict, mangled);
1019 Py_DECREF(mangled);
1020 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001021 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022 return compiler_addop_i(c, opcode, arg);
1023}
1024
1025/* Add an opcode with an integer argument.
1026 Returns 0 on failure, 1 on success.
1027*/
1028
1029static int
1030compiler_addop_i(struct compiler *c, int opcode, int oparg)
1031{
1032 struct instr *i;
1033 int off;
1034 off = compiler_next_instr(c, c->u->u_curblock);
1035 if (off < 0)
1036 return 0;
1037 i = &c->u->u_curblock->b_instr[off];
1038 i->i_opcode = opcode;
1039 i->i_oparg = oparg;
1040 i->i_hasarg = 1;
1041 compiler_set_lineno(c, off);
1042 return 1;
1043}
1044
1045static int
1046compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1047{
1048 struct instr *i;
1049 int off;
1050
1051 assert(b != NULL);
1052 off = compiler_next_instr(c, c->u->u_curblock);
1053 if (off < 0)
1054 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 i = &c->u->u_curblock->b_instr[off];
1056 i->i_opcode = opcode;
1057 i->i_target = b;
1058 i->i_hasarg = 1;
1059 if (absolute)
1060 i->i_jabs = 1;
1061 else
1062 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001063 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064 return 1;
1065}
1066
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001067/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1068 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069 it as the current block. NEXT_BLOCK() also creates an implicit jump
1070 from the current block to the new block.
1071*/
1072
Neal Norwitzf733a012006-10-29 18:30:10 +00001073/* The returns inside these macros make it impossible to decref objects
1074 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075*/
1076
1077
1078#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001079 if (compiler_use_new_block((C)) == NULL) \
1080 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081}
1082
1083#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001084 if (compiler_next_block((C)) == NULL) \
1085 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086}
1087
1088#define ADDOP(C, OP) { \
1089 if (!compiler_addop((C), (OP))) \
1090 return 0; \
1091}
1092
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001093#define ADDOP_IN_SCOPE(C, OP) { \
1094 if (!compiler_addop((C), (OP))) { \
1095 compiler_exit_scope(c); \
1096 return 0; \
1097 } \
1098}
1099
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100#define ADDOP_O(C, OP, O, TYPE) { \
1101 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1102 return 0; \
1103}
1104
1105#define ADDOP_NAME(C, OP, O, TYPE) { \
1106 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1107 return 0; \
1108}
1109
1110#define ADDOP_I(C, OP, O) { \
1111 if (!compiler_addop_i((C), (OP), (O))) \
1112 return 0; \
1113}
1114
1115#define ADDOP_JABS(C, OP, O) { \
1116 if (!compiler_addop_j((C), (OP), (O), 1)) \
1117 return 0; \
1118}
1119
1120#define ADDOP_JREL(C, OP, O) { \
1121 if (!compiler_addop_j((C), (OP), (O), 0)) \
1122 return 0; \
1123}
1124
1125/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1126 the ASDL name to synthesize the name of the C type and the visit function.
1127*/
1128
1129#define VISIT(C, TYPE, V) {\
1130 if (!compiler_visit_ ## TYPE((C), (V))) \
1131 return 0; \
1132}
1133
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001134#define VISIT_IN_SCOPE(C, TYPE, V) {\
1135 if (!compiler_visit_ ## TYPE((C), (V))) { \
1136 compiler_exit_scope(c); \
1137 return 0; \
1138 } \
1139}
1140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141#define VISIT_SLICE(C, V, CTX) {\
1142 if (!compiler_visit_slice((C), (V), (CTX))) \
1143 return 0; \
1144}
1145
1146#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001147 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001149 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001150 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001151 if (!compiler_visit_ ## TYPE((C), elt)) \
1152 return 0; \
1153 } \
1154}
1155
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001156#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001157 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001158 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001159 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001160 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001161 if (!compiler_visit_ ## TYPE((C), elt)) { \
1162 compiler_exit_scope(c); \
1163 return 0; \
1164 } \
1165 } \
1166}
1167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168static int
1169compiler_isdocstring(stmt_ty s)
1170{
1171 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001172 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173 return s->v.Expr.value->kind == Str_kind;
1174}
1175
1176/* Compile a sequence of statements, checking for a docstring. */
1177
1178static int
1179compiler_body(struct compiler *c, asdl_seq *stmts)
1180{
1181 int i = 0;
1182 stmt_ty st;
1183
1184 if (!asdl_seq_LEN(stmts))
1185 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001186 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001187 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1188 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189 i = 1;
1190 VISIT(c, expr, st->v.Expr.value);
1191 if (!compiler_nameop(c, __doc__, Store))
1192 return 0;
1193 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001194 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001195 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196 return 1;
1197}
1198
1199static PyCodeObject *
1200compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001201{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001202 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001203 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 static PyObject *module;
1205 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001206 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 if (!module)
1208 return NULL;
1209 }
Neal Norwitzed657552006-07-10 00:04:44 +00001210 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1211 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001212 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213 switch (mod->kind) {
1214 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001215 if (!compiler_body(c, mod->v.Module.body)) {
1216 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 break;
1220 case Interactive_kind:
1221 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001222 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001223 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 break;
1225 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001226 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001227 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001228 break;
1229 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001230 PyErr_SetString(PyExc_SystemError,
1231 "suite should not be possible");
1232 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001233 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001234 PyErr_Format(PyExc_SystemError,
1235 "module kind %d should not be possible",
1236 mod->kind);
1237 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239 co = assemble(c, addNone);
1240 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001241 return co;
1242}
1243
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244/* The test for LOCAL must come before the test for FREE in order to
1245 handle classes where name is both local and free. The local var is
1246 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001247*/
1248
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001249static int
1250get_ref_type(struct compiler *c, PyObject *name)
1251{
1252 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001253 if (scope == 0) {
1254 char buf[350];
1255 PyOS_snprintf(buf, sizeof(buf),
1256 "unknown scope for %.100s in %.100s(%s) in %s\n"
1257 "symbols: %s\nlocals: %s\nglobals: %s\n",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001258 PyString_AS_STRING(name),
1259 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001260 PyObject_REPR(c->u->u_ste->ste_id),
1261 c->c_filename,
1262 PyObject_REPR(c->u->u_ste->ste_symbols),
1263 PyObject_REPR(c->u->u_varnames),
1264 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001266 Py_FatalError(buf);
1267 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001268
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270}
1271
1272static int
1273compiler_lookup_arg(PyObject *dict, PyObject *name)
1274{
1275 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001276 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001277 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001278 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001280 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001281 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001282 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 return PyInt_AS_LONG(v);
1284}
1285
1286static int
1287compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1288{
1289 int i, free = PyCode_GetNumFree(co);
1290 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001291 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1292 ADDOP_I(c, MAKE_FUNCTION, args);
1293 return 1;
1294 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001295 for (i = 0; i < free; ++i) {
1296 /* Bypass com_addop_varname because it will generate
1297 LOAD_DEREF but LOAD_CLOSURE is needed.
1298 */
1299 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1300 int arg, reftype;
1301
1302 /* Special case: If a class contains a method with a
1303 free variable that has the same name as a method,
1304 the name will be considered free *and* local in the
1305 class. It should be handled by the closure, as
1306 well as by the normal name loookup logic.
1307 */
1308 reftype = get_ref_type(c, name);
1309 if (reftype == CELL)
1310 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1311 else /* (reftype == FREE) */
1312 arg = compiler_lookup_arg(c->u->u_freevars, name);
1313 if (arg == -1) {
1314 printf("lookup %s in %s %d %d\n"
1315 "freevars of %s: %s\n",
1316 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001317 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001319 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 PyObject_REPR(co->co_freevars));
1321 Py_FatalError("compiler_make_closure()");
1322 }
1323 ADDOP_I(c, LOAD_CLOSURE, arg);
1324 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001325 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001327 ADDOP_I(c, MAKE_CLOSURE, args);
1328 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329}
1330
1331static int
1332compiler_decorators(struct compiler *c, asdl_seq* decos)
1333{
1334 int i;
1335
1336 if (!decos)
1337 return 1;
1338
1339 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001340 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 }
1342 return 1;
1343}
1344
1345static int
1346compiler_arguments(struct compiler *c, arguments_ty args)
1347{
1348 int i;
1349 int n = asdl_seq_LEN(args->args);
1350 /* Correctly handle nested argument lists */
1351 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001352 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001353 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001354 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 if (id == NULL) {
1356 return 0;
1357 }
1358 if (!compiler_nameop(c, id, Load)) {
1359 Py_DECREF(id);
1360 return 0;
1361 }
1362 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001363 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 }
1365 }
1366 return 1;
1367}
1368
1369static int
1370compiler_function(struct compiler *c, stmt_ty s)
1371{
1372 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001373 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001374 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001375 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001376 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001377 int i, n, docstring;
1378
1379 assert(s->kind == FunctionDef_kind);
1380
1381 if (!compiler_decorators(c, decos))
1382 return 0;
1383 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001384 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1386 s->lineno))
1387 return 0;
1388
Anthony Baxter7b782b62006-04-11 12:01:56 +00001389 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001390 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001391 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001392 first_const = st->v.Expr.value->v.Str.s;
1393 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001394 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001395 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001398 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001399 compiler_arguments(c, args);
1400
1401 c->u->u_argcount = asdl_seq_LEN(args->args);
1402 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001403 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001405 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1406 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001407 }
1408 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001409 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410 if (co == NULL)
1411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001413 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001414 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415
1416 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1417 ADDOP_I(c, CALL_FUNCTION, 1);
1418 }
1419
1420 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1421}
1422
1423static int
1424compiler_class(struct compiler *c, stmt_ty s)
1425{
Christian Heimes5224d282008-02-23 15:01:05 +00001426 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001428 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001429 asdl_seq* decos = s->v.ClassDef.decorator_list;
1430
1431 if (!compiler_decorators(c, decos))
1432 return 0;
1433
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434 /* push class name on stack, needed by BUILD_CLASS */
1435 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1436 /* push the tuple of base classes on the stack */
1437 n = asdl_seq_LEN(s->v.ClassDef.bases);
1438 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001439 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 ADDOP_I(c, BUILD_TUPLE, n);
1441 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1442 s->lineno))
1443 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001444 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001445 c->u->u_private = s->v.ClassDef.name;
1446 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001447 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 if (!str || !compiler_nameop(c, str, Load)) {
1449 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001450 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001452 }
1453
1454 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001455 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 if (!str || !compiler_nameop(c, str, Store)) {
1457 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001458 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001460 }
1461 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001463 if (!compiler_body(c, s->v.ClassDef.body)) {
1464 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001468 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1469 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001471 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 if (co == NULL)
1473 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001475 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001476 Py_DECREF(co);
1477
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478 ADDOP_I(c, CALL_FUNCTION, 0);
1479 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001480 /* apply decorators */
1481 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1482 ADDOP_I(c, CALL_FUNCTION, 1);
1483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1485 return 0;
1486 return 1;
1487}
1488
1489static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001490compiler_ifexp(struct compiler *c, expr_ty e)
1491{
1492 basicblock *end, *next;
1493
1494 assert(e->kind == IfExp_kind);
1495 end = compiler_new_block(c);
1496 if (end == NULL)
1497 return 0;
1498 next = compiler_new_block(c);
1499 if (next == NULL)
1500 return 0;
1501 VISIT(c, expr, e->v.IfExp.test);
1502 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1503 ADDOP(c, POP_TOP);
1504 VISIT(c, expr, e->v.IfExp.body);
1505 ADDOP_JREL(c, JUMP_FORWARD, end);
1506 compiler_use_next_block(c, next);
1507 ADDOP(c, POP_TOP);
1508 VISIT(c, expr, e->v.IfExp.orelse);
1509 compiler_use_next_block(c, end);
1510 return 1;
1511}
1512
1513static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514compiler_lambda(struct compiler *c, expr_ty e)
1515{
1516 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001517 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 arguments_ty args = e->v.Lambda.args;
1519 assert(e->kind == Lambda_kind);
1520
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001521 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001522 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001523 if (!name)
1524 return 0;
1525 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526
1527 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001528 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1530 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001531
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001532 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533 compiler_arguments(c, args);
1534
1535 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001536 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1537 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001538 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001539 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540 if (co == NULL)
1541 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001542
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001543 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001544 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001545
1546 return 1;
1547}
1548
1549static int
1550compiler_print(struct compiler *c, stmt_ty s)
1551{
1552 int i, n;
1553 bool dest;
1554
1555 assert(s->kind == Print_kind);
1556 n = asdl_seq_LEN(s->v.Print.values);
1557 dest = false;
1558 if (s->v.Print.dest) {
1559 VISIT(c, expr, s->v.Print.dest);
1560 dest = true;
1561 }
1562 for (i = 0; i < n; i++) {
1563 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1564 if (dest) {
1565 ADDOP(c, DUP_TOP);
1566 VISIT(c, expr, e);
1567 ADDOP(c, ROT_TWO);
1568 ADDOP(c, PRINT_ITEM_TO);
1569 }
1570 else {
1571 VISIT(c, expr, e);
1572 ADDOP(c, PRINT_ITEM);
1573 }
1574 }
1575 if (s->v.Print.nl) {
1576 if (dest)
1577 ADDOP(c, PRINT_NEWLINE_TO)
1578 else
1579 ADDOP(c, PRINT_NEWLINE)
1580 }
1581 else if (dest)
1582 ADDOP(c, POP_TOP);
1583 return 1;
1584}
1585
1586static int
1587compiler_if(struct compiler *c, stmt_ty s)
1588{
1589 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001590 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001591 assert(s->kind == If_kind);
1592 end = compiler_new_block(c);
1593 if (end == NULL)
1594 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001595 next = compiler_new_block(c);
1596 if (next == NULL)
1597 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001598
1599 constant = expr_constant(s->v.If.test);
1600 /* constant = 0: "if 0"
1601 * constant = 1: "if 1", "if 2", ...
1602 * constant = -1: rest */
1603 if (constant == 0) {
1604 if (s->v.If.orelse)
1605 VISIT_SEQ(c, stmt, s->v.If.orelse);
1606 } else if (constant == 1) {
1607 VISIT_SEQ(c, stmt, s->v.If.body);
1608 } else {
1609 VISIT(c, expr, s->v.If.test);
1610 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1611 ADDOP(c, POP_TOP);
1612 VISIT_SEQ(c, stmt, s->v.If.body);
1613 ADDOP_JREL(c, JUMP_FORWARD, end);
1614 compiler_use_next_block(c, next);
1615 ADDOP(c, POP_TOP);
1616 if (s->v.If.orelse)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001617 VISIT_SEQ(c, stmt, s->v.If.orelse);
Georg Brandlddbaa662006-06-04 21:56:52 +00001618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619 compiler_use_next_block(c, end);
1620 return 1;
1621}
1622
1623static int
1624compiler_for(struct compiler *c, stmt_ty s)
1625{
1626 basicblock *start, *cleanup, *end;
1627
1628 start = compiler_new_block(c);
1629 cleanup = compiler_new_block(c);
1630 end = compiler_new_block(c);
1631 if (start == NULL || end == NULL || cleanup == NULL)
1632 return 0;
1633 ADDOP_JREL(c, SETUP_LOOP, end);
1634 if (!compiler_push_fblock(c, LOOP, start))
1635 return 0;
1636 VISIT(c, expr, s->v.For.iter);
1637 ADDOP(c, GET_ITER);
1638 compiler_use_next_block(c, start);
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001639 /* for expressions must be traced on each iteration,
1640 so we need to set an extra line number. */
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001641 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642 ADDOP_JREL(c, FOR_ITER, cleanup);
1643 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001644 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001645 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1646 compiler_use_next_block(c, cleanup);
1647 ADDOP(c, POP_BLOCK);
1648 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001649 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650 compiler_use_next_block(c, end);
1651 return 1;
1652}
1653
1654static int
1655compiler_while(struct compiler *c, stmt_ty s)
1656{
1657 basicblock *loop, *orelse, *end, *anchor = NULL;
1658 int constant = expr_constant(s->v.While.test);
1659
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001660 if (constant == 0) {
1661 if (s->v.While.orelse)
1662 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001663 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001664 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 loop = compiler_new_block(c);
1666 end = compiler_new_block(c);
1667 if (constant == -1) {
1668 anchor = compiler_new_block(c);
1669 if (anchor == NULL)
1670 return 0;
1671 }
1672 if (loop == NULL || end == NULL)
1673 return 0;
1674 if (s->v.While.orelse) {
1675 orelse = compiler_new_block(c);
1676 if (orelse == NULL)
1677 return 0;
1678 }
1679 else
1680 orelse = NULL;
1681
1682 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001683 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 if (!compiler_push_fblock(c, LOOP, loop))
1685 return 0;
1686 if (constant == -1) {
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001687 /* while expressions must be traced on each iteration,
1688 so we need to set an extra line number. */
1689 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 VISIT(c, expr, s->v.While.test);
1691 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1692 ADDOP(c, POP_TOP);
1693 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001694 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1696
1697 /* XXX should the two POP instructions be in a separate block
1698 if there is no else clause ?
1699 */
1700
1701 if (constant == -1) {
1702 compiler_use_next_block(c, anchor);
1703 ADDOP(c, POP_TOP);
1704 ADDOP(c, POP_BLOCK);
1705 }
1706 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001707 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001708 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 compiler_use_next_block(c, end);
1710
1711 return 1;
1712}
1713
1714static int
1715compiler_continue(struct compiler *c)
1716{
1717 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001718 static const char IN_FINALLY_ERROR_MSG[] =
1719 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720 int i;
1721
1722 if (!c->u->u_nfblocks)
1723 return compiler_error(c, LOOP_ERROR_MSG);
1724 i = c->u->u_nfblocks - 1;
1725 switch (c->u->u_fblock[i].fb_type) {
1726 case LOOP:
1727 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1728 break;
1729 case EXCEPT:
1730 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001731 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1732 /* Prevent continue anywhere under a finally
1733 even if hidden in a sub-try or except. */
1734 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1735 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 if (i == -1)
1738 return compiler_error(c, LOOP_ERROR_MSG);
1739 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1740 break;
1741 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001742 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743 }
1744
1745 return 1;
1746}
1747
1748/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1749
1750 SETUP_FINALLY L
1751 <code for body>
1752 POP_BLOCK
1753 LOAD_CONST <None>
1754 L: <code for finalbody>
1755 END_FINALLY
1756
1757 The special instructions use the block stack. Each block
1758 stack entry contains the instruction that created it (here
1759 SETUP_FINALLY), the level of the value stack at the time the
1760 block stack entry was created, and a label (here L).
1761
1762 SETUP_FINALLY:
1763 Pushes the current value stack level and the label
1764 onto the block stack.
1765 POP_BLOCK:
1766 Pops en entry from the block stack, and pops the value
1767 stack until its level is the same as indicated on the
1768 block stack. (The label is ignored.)
1769 END_FINALLY:
1770 Pops a variable number of entries from the *value* stack
1771 and re-raises the exception they specify. The number of
1772 entries popped depends on the (pseudo) exception type.
1773
1774 The block stack is unwound when an exception is raised:
1775 when a SETUP_FINALLY entry is found, the exception is pushed
1776 onto the value stack (and the exception condition is cleared),
1777 and the interpreter jumps to the label gotten from the block
1778 stack.
1779*/
1780
1781static int
1782compiler_try_finally(struct compiler *c, stmt_ty s)
1783{
1784 basicblock *body, *end;
1785 body = compiler_new_block(c);
1786 end = compiler_new_block(c);
1787 if (body == NULL || end == NULL)
1788 return 0;
1789
1790 ADDOP_JREL(c, SETUP_FINALLY, end);
1791 compiler_use_next_block(c, body);
1792 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1793 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001794 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 ADDOP(c, POP_BLOCK);
1796 compiler_pop_fblock(c, FINALLY_TRY, body);
1797
1798 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1799 compiler_use_next_block(c, end);
1800 if (!compiler_push_fblock(c, FINALLY_END, end))
1801 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001802 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803 ADDOP(c, END_FINALLY);
1804 compiler_pop_fblock(c, FINALLY_END, end);
1805
1806 return 1;
1807}
1808
1809/*
1810 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1811 (The contents of the value stack is shown in [], with the top
1812 at the right; 'tb' is trace-back info, 'val' the exception's
1813 associated value, and 'exc' the exception.)
1814
1815 Value stack Label Instruction Argument
1816 [] SETUP_EXCEPT L1
1817 [] <code for S>
1818 [] POP_BLOCK
1819 [] JUMP_FORWARD L0
1820
1821 [tb, val, exc] L1: DUP )
1822 [tb, val, exc, exc] <evaluate E1> )
1823 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1824 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1825 [tb, val, exc, 1] POP )
1826 [tb, val, exc] POP
1827 [tb, val] <assign to V1> (or POP if no V1)
1828 [tb] POP
1829 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001830 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001831
1832 [tb, val, exc, 0] L2: POP
1833 [tb, val, exc] DUP
1834 .............................etc.......................
1835
1836 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001837 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838
1839 [] L0: <next statement>
1840
1841 Of course, parts are not generated if Vi or Ei is not present.
1842*/
1843static int
1844compiler_try_except(struct compiler *c, stmt_ty s)
1845{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001846 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 int i, n;
1848
1849 body = compiler_new_block(c);
1850 except = compiler_new_block(c);
1851 orelse = compiler_new_block(c);
1852 end = compiler_new_block(c);
1853 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1854 return 0;
1855 ADDOP_JREL(c, SETUP_EXCEPT, except);
1856 compiler_use_next_block(c, body);
1857 if (!compiler_push_fblock(c, EXCEPT, body))
1858 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001859 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 ADDOP(c, POP_BLOCK);
1861 compiler_pop_fblock(c, EXCEPT, body);
1862 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1863 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1864 compiler_use_next_block(c, except);
1865 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001866 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001868 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001870 c->u->u_lineno_set = false;
1871 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 except = compiler_new_block(c);
1873 if (except == NULL)
1874 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001875 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001877 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1879 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1880 ADDOP(c, POP_TOP);
1881 }
1882 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001883 if (handler->v.ExceptHandler.name) {
1884 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885 }
1886 else {
1887 ADDOP(c, POP_TOP);
1888 }
1889 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001890 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001891 ADDOP_JREL(c, JUMP_FORWARD, end);
1892 compiler_use_next_block(c, except);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001893 if (handler->v.ExceptHandler.type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 ADDOP(c, POP_TOP);
1895 }
1896 ADDOP(c, END_FINALLY);
1897 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001898 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001899 compiler_use_next_block(c, end);
1900 return 1;
1901}
1902
1903static int
1904compiler_import_as(struct compiler *c, identifier name, identifier asname)
1905{
1906 /* The IMPORT_NAME opcode was already generated. This function
1907 merely needs to bind the result to a name.
1908
1909 If there is a dot in name, we need to split it and emit a
1910 LOAD_ATTR for each name.
1911 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001912 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 const char *dot = strchr(src, '.');
1914 if (dot) {
1915 /* Consume the base module name to get the first attribute */
1916 src = dot + 1;
1917 while (dot) {
1918 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001919 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001921 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001923 if (!attr)
1924 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001926 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 src = dot + 1;
1928 }
1929 }
1930 return compiler_nameop(c, asname, Store);
1931}
1932
1933static int
1934compiler_import(struct compiler *c, stmt_ty s)
1935{
1936 /* The Import node stores a module name like a.b.c as a single
1937 string. This is convenient for all cases except
1938 import a.b.c as d
1939 where we need to parse that string to extract the individual
1940 module names.
1941 XXX Perhaps change the representation to make this case simpler?
1942 */
1943 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001946 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001948 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949
Neal Norwitzcbce2802006-04-03 06:26:32 +00001950 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001951 level = PyInt_FromLong(0);
1952 else
1953 level = PyInt_FromLong(-1);
1954
1955 if (level == NULL)
1956 return 0;
1957
1958 ADDOP_O(c, LOAD_CONST, level, consts);
1959 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001960 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1961 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1962
1963 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001964 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001965 if (!r)
1966 return r;
1967 }
1968 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001969 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001970 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 char *dot = strchr(base, '.');
1972 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001973 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 dot - base);
1975 r = compiler_nameop(c, tmp, Store);
1976 if (dot) {
1977 Py_DECREF(tmp);
1978 }
1979 if (!r)
1980 return r;
1981 }
1982 }
1983 return 1;
1984}
1985
1986static int
1987compiler_from_import(struct compiler *c, stmt_ty s)
1988{
1989 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990
1991 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001992 PyObject *level;
1993
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001994 if (!names)
1995 return 0;
1996
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001997 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001998 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001999 level = PyInt_FromLong(-1);
2000 else
2001 level = PyInt_FromLong(s->v.ImportFrom.level);
2002
2003 if (!level) {
2004 Py_DECREF(names);
2005 return 0;
2006 }
2007
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 /* build up the names */
2009 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002010 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 Py_INCREF(alias->name);
2012 PyTuple_SET_ITEM(names, i, alias->name);
2013 }
2014
2015 if (s->lineno > c->c_future->ff_lineno) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002016 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002018 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002019 Py_DECREF(names);
2020 return compiler_error(c,
2021 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002022 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023
2024 }
2025 }
2026
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002027 ADDOP_O(c, LOAD_CONST, level, consts);
2028 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002030 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2032 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002033 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 identifier store_name;
2035
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002036 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002037 assert(n == 1);
2038 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002039 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002040 }
2041
2042 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2043 store_name = alias->name;
2044 if (alias->asname)
2045 store_name = alias->asname;
2046
2047 if (!compiler_nameop(c, store_name, Store)) {
2048 Py_DECREF(names);
2049 return 0;
2050 }
2051 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002052 /* remove imported module */
2053 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 return 1;
2055}
2056
2057static int
2058compiler_assert(struct compiler *c, stmt_ty s)
2059{
2060 static PyObject *assertion_error = NULL;
2061 basicblock *end;
2062
2063 if (Py_OptimizeFlag)
2064 return 1;
2065 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002066 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 if (assertion_error == NULL)
2068 return 0;
2069 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002070 if (s->v.Assert.test->kind == Tuple_kind &&
2071 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2072 const char* msg =
2073 "assertion is always true, perhaps remove parentheses?";
2074 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2075 c->u->u_lineno, NULL, NULL) == -1)
2076 return 0;
2077 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 VISIT(c, expr, s->v.Assert.test);
2079 end = compiler_new_block(c);
2080 if (end == NULL)
2081 return 0;
2082 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2083 ADDOP(c, POP_TOP);
2084 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2085 if (s->v.Assert.msg) {
2086 VISIT(c, expr, s->v.Assert.msg);
2087 ADDOP_I(c, RAISE_VARARGS, 2);
2088 }
2089 else {
2090 ADDOP_I(c, RAISE_VARARGS, 1);
2091 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002092 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093 ADDOP(c, POP_TOP);
2094 return 1;
2095}
2096
2097static int
2098compiler_visit_stmt(struct compiler *c, stmt_ty s)
2099{
2100 int i, n;
2101
Neal Norwitzf733a012006-10-29 18:30:10 +00002102 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 c->u->u_lineno = s->lineno;
2104 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002107 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002109 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002111 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 if (c->u->u_ste->ste_type != FunctionBlock)
2113 return compiler_error(c, "'return' outside function");
2114 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 VISIT(c, expr, s->v.Return.value);
2116 }
2117 else
2118 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2119 ADDOP(c, RETURN_VALUE);
2120 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002121 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002122 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002124 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 n = asdl_seq_LEN(s->v.Assign.targets);
2126 VISIT(c, expr, s->v.Assign.value);
2127 for (i = 0; i < n; i++) {
2128 if (i < n - 1)
2129 ADDOP(c, DUP_TOP);
2130 VISIT(c, expr,
2131 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2132 }
2133 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002134 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002136 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002138 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002140 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002141 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002142 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002144 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 n = 0;
2146 if (s->v.Raise.type) {
2147 VISIT(c, expr, s->v.Raise.type);
2148 n++;
2149 if (s->v.Raise.inst) {
2150 VISIT(c, expr, s->v.Raise.inst);
2151 n++;
2152 if (s->v.Raise.tback) {
2153 VISIT(c, expr, s->v.Raise.tback);
2154 n++;
2155 }
2156 }
2157 }
2158 ADDOP_I(c, RAISE_VARARGS, n);
2159 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002160 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002162 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002164 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002166 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002168 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002170 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 VISIT(c, expr, s->v.Exec.body);
2172 if (s->v.Exec.globals) {
2173 VISIT(c, expr, s->v.Exec.globals);
2174 if (s->v.Exec.locals) {
2175 VISIT(c, expr, s->v.Exec.locals);
2176 } else {
2177 ADDOP(c, DUP_TOP);
2178 }
2179 } else {
2180 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2181 ADDOP(c, DUP_TOP);
2182 }
2183 ADDOP(c, EXEC_STMT);
2184 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002185 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002187 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002189 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 ADDOP(c, PRINT_EXPR);
2191 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002192 else if (s->v.Expr.value->kind != Str_kind &&
2193 s->v.Expr.value->kind != Num_kind) {
2194 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 ADDOP(c, POP_TOP);
2196 }
2197 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002198 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002199 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002200 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002201 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002202 return compiler_error(c, "'break' outside loop");
2203 ADDOP(c, BREAK_LOOP);
2204 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002205 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002207 case With_kind:
2208 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 }
2210 return 1;
2211}
2212
2213static int
2214unaryop(unaryop_ty op)
2215{
2216 switch (op) {
2217 case Invert:
2218 return UNARY_INVERT;
2219 case Not:
2220 return UNARY_NOT;
2221 case UAdd:
2222 return UNARY_POSITIVE;
2223 case USub:
2224 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002225 default:
2226 PyErr_Format(PyExc_SystemError,
2227 "unary op %d should not be possible", op);
2228 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230}
2231
2232static int
2233binop(struct compiler *c, operator_ty op)
2234{
2235 switch (op) {
2236 case Add:
2237 return BINARY_ADD;
2238 case Sub:
2239 return BINARY_SUBTRACT;
2240 case Mult:
2241 return BINARY_MULTIPLY;
2242 case Div:
2243 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2244 return BINARY_TRUE_DIVIDE;
2245 else
2246 return BINARY_DIVIDE;
2247 case Mod:
2248 return BINARY_MODULO;
2249 case Pow:
2250 return BINARY_POWER;
2251 case LShift:
2252 return BINARY_LSHIFT;
2253 case RShift:
2254 return BINARY_RSHIFT;
2255 case BitOr:
2256 return BINARY_OR;
2257 case BitXor:
2258 return BINARY_XOR;
2259 case BitAnd:
2260 return BINARY_AND;
2261 case FloorDiv:
2262 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002263 default:
2264 PyErr_Format(PyExc_SystemError,
2265 "binary op %d should not be possible", op);
2266 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268}
2269
2270static int
2271cmpop(cmpop_ty op)
2272{
2273 switch (op) {
2274 case Eq:
2275 return PyCmp_EQ;
2276 case NotEq:
2277 return PyCmp_NE;
2278 case Lt:
2279 return PyCmp_LT;
2280 case LtE:
2281 return PyCmp_LE;
2282 case Gt:
2283 return PyCmp_GT;
2284 case GtE:
2285 return PyCmp_GE;
2286 case Is:
2287 return PyCmp_IS;
2288 case IsNot:
2289 return PyCmp_IS_NOT;
2290 case In:
2291 return PyCmp_IN;
2292 case NotIn:
2293 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002294 default:
2295 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297}
2298
2299static int
2300inplace_binop(struct compiler *c, operator_ty op)
2301{
2302 switch (op) {
2303 case Add:
2304 return INPLACE_ADD;
2305 case Sub:
2306 return INPLACE_SUBTRACT;
2307 case Mult:
2308 return INPLACE_MULTIPLY;
2309 case Div:
2310 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2311 return INPLACE_TRUE_DIVIDE;
2312 else
2313 return INPLACE_DIVIDE;
2314 case Mod:
2315 return INPLACE_MODULO;
2316 case Pow:
2317 return INPLACE_POWER;
2318 case LShift:
2319 return INPLACE_LSHIFT;
2320 case RShift:
2321 return INPLACE_RSHIFT;
2322 case BitOr:
2323 return INPLACE_OR;
2324 case BitXor:
2325 return INPLACE_XOR;
2326 case BitAnd:
2327 return INPLACE_AND;
2328 case FloorDiv:
2329 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002330 default:
2331 PyErr_Format(PyExc_SystemError,
2332 "inplace binary op %d should not be possible", op);
2333 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335}
2336
2337static int
2338compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2339{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002340 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2342
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002343 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002344 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 /* XXX AugStore isn't used anywhere! */
2346
Neal Norwitz0031ff32008-02-25 01:45:37 +00002347 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002348 if (!mangled)
2349 return 0;
2350
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 op = 0;
2352 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002353 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 switch (scope) {
2355 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002356 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 optype = OP_DEREF;
2358 break;
2359 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002360 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361 optype = OP_DEREF;
2362 break;
2363 case LOCAL:
2364 if (c->u->u_ste->ste_type == FunctionBlock)
2365 optype = OP_FAST;
2366 break;
2367 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002368 if (c->u->u_ste->ste_type == FunctionBlock &&
2369 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002370 optype = OP_GLOBAL;
2371 break;
2372 case GLOBAL_EXPLICIT:
2373 optype = OP_GLOBAL;
2374 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002375 default:
2376 /* scope can be 0 */
2377 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 }
2379
2380 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002381 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382
2383 switch (optype) {
2384 case OP_DEREF:
2385 switch (ctx) {
2386 case Load: op = LOAD_DEREF; break;
2387 case Store: op = STORE_DEREF; break;
2388 case AugLoad:
2389 case AugStore:
2390 break;
2391 case Del:
2392 PyErr_Format(PyExc_SyntaxError,
2393 "can not delete variable '%s' referenced "
2394 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002395 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002396 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002399 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002400 PyErr_SetString(PyExc_SystemError,
2401 "param invalid for deref variable");
2402 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403 }
2404 break;
2405 case OP_FAST:
2406 switch (ctx) {
2407 case Load: op = LOAD_FAST; break;
2408 case Store: op = STORE_FAST; break;
2409 case Del: op = DELETE_FAST; break;
2410 case AugLoad:
2411 case AugStore:
2412 break;
2413 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002414 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002415 PyErr_SetString(PyExc_SystemError,
2416 "param invalid for local variable");
2417 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002418 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002419 ADDOP_O(c, op, mangled, varnames);
2420 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 return 1;
2422 case OP_GLOBAL:
2423 switch (ctx) {
2424 case Load: op = LOAD_GLOBAL; break;
2425 case Store: op = STORE_GLOBAL; break;
2426 case Del: op = DELETE_GLOBAL; break;
2427 case AugLoad:
2428 case AugStore:
2429 break;
2430 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002431 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002432 PyErr_SetString(PyExc_SystemError,
2433 "param invalid for global variable");
2434 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435 }
2436 break;
2437 case OP_NAME:
2438 switch (ctx) {
2439 case Load: op = LOAD_NAME; break;
2440 case Store: op = STORE_NAME; break;
2441 case Del: op = DELETE_NAME; break;
2442 case AugLoad:
2443 case AugStore:
2444 break;
2445 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002446 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002447 PyErr_SetString(PyExc_SystemError,
2448 "param invalid for name variable");
2449 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 }
2451 break;
2452 }
2453
2454 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002455 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002456 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002457 if (arg < 0)
2458 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002459 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460}
2461
2462static int
2463compiler_boolop(struct compiler *c, expr_ty e)
2464{
2465 basicblock *end;
2466 int jumpi, i, n;
2467 asdl_seq *s;
2468
2469 assert(e->kind == BoolOp_kind);
2470 if (e->v.BoolOp.op == And)
2471 jumpi = JUMP_IF_FALSE;
2472 else
2473 jumpi = JUMP_IF_TRUE;
2474 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002475 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 return 0;
2477 s = e->v.BoolOp.values;
2478 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002479 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002481 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 ADDOP_JREL(c, jumpi, end);
2483 ADDOP(c, POP_TOP)
2484 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002485 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 compiler_use_next_block(c, end);
2487 return 1;
2488}
2489
2490static int
2491compiler_list(struct compiler *c, expr_ty e)
2492{
2493 int n = asdl_seq_LEN(e->v.List.elts);
2494 if (e->v.List.ctx == Store) {
2495 ADDOP_I(c, UNPACK_SEQUENCE, n);
2496 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002497 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 if (e->v.List.ctx == Load) {
2499 ADDOP_I(c, BUILD_LIST, n);
2500 }
2501 return 1;
2502}
2503
2504static int
2505compiler_tuple(struct compiler *c, expr_ty e)
2506{
2507 int n = asdl_seq_LEN(e->v.Tuple.elts);
2508 if (e->v.Tuple.ctx == Store) {
2509 ADDOP_I(c, UNPACK_SEQUENCE, n);
2510 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002511 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 if (e->v.Tuple.ctx == Load) {
2513 ADDOP_I(c, BUILD_TUPLE, n);
2514 }
2515 return 1;
2516}
2517
2518static int
2519compiler_compare(struct compiler *c, expr_ty e)
2520{
2521 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002522 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523
2524 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2525 VISIT(c, expr, e->v.Compare.left);
2526 n = asdl_seq_LEN(e->v.Compare.ops);
2527 assert(n > 0);
2528 if (n > 1) {
2529 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002530 if (cleanup == NULL)
2531 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002532 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002533 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 }
2535 for (i = 1; i < n; i++) {
2536 ADDOP(c, DUP_TOP);
2537 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002539 cmpop((cmpop_ty)(asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002540 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2542 NEXT_BLOCK(c);
2543 ADDOP(c, POP_TOP);
2544 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002545 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002546 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002548 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002550 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 if (n > 1) {
2552 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002553 if (end == NULL)
2554 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 ADDOP_JREL(c, JUMP_FORWARD, end);
2556 compiler_use_next_block(c, cleanup);
2557 ADDOP(c, ROT_TWO);
2558 ADDOP(c, POP_TOP);
2559 compiler_use_next_block(c, end);
2560 }
2561 return 1;
2562}
2563
2564static int
2565compiler_call(struct compiler *c, expr_ty e)
2566{
2567 int n, code = 0;
2568
2569 VISIT(c, expr, e->v.Call.func);
2570 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002571 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002573 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2575 }
2576 if (e->v.Call.starargs) {
2577 VISIT(c, expr, e->v.Call.starargs);
2578 code |= 1;
2579 }
2580 if (e->v.Call.kwargs) {
2581 VISIT(c, expr, e->v.Call.kwargs);
2582 code |= 2;
2583 }
2584 switch (code) {
2585 case 0:
2586 ADDOP_I(c, CALL_FUNCTION, n);
2587 break;
2588 case 1:
2589 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2590 break;
2591 case 2:
2592 ADDOP_I(c, CALL_FUNCTION_KW, n);
2593 break;
2594 case 3:
2595 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2596 break;
2597 }
2598 return 1;
2599}
2600
2601static int
2602compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002603 asdl_seq *generators, int gen_index,
2604 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605{
2606 /* generate code for the iterator, then each of the ifs,
2607 and then write to the element */
2608
2609 comprehension_ty l;
2610 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002611 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
2613 start = compiler_new_block(c);
2614 skip = compiler_new_block(c);
2615 if_cleanup = compiler_new_block(c);
2616 anchor = compiler_new_block(c);
2617
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002618 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2619 anchor == NULL)
2620 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002621
Anthony Baxter7b782b62006-04-11 12:01:56 +00002622 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 VISIT(c, expr, l->iter);
2624 ADDOP(c, GET_ITER);
2625 compiler_use_next_block(c, start);
2626 ADDOP_JREL(c, FOR_ITER, anchor);
2627 NEXT_BLOCK(c);
2628 VISIT(c, expr, l->target);
2629
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002630 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631 n = asdl_seq_LEN(l->ifs);
2632 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002633 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 VISIT(c, expr, e);
2635 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2636 NEXT_BLOCK(c);
2637 ADDOP(c, POP_TOP);
2638 }
2639
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002640 if (++gen_index < asdl_seq_LEN(generators))
2641 if (!compiler_listcomp_generator(c, tmpname,
2642 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002645 /* only append after the last for generator */
2646 if (gen_index >= asdl_seq_LEN(generators)) {
2647 if (!compiler_nameop(c, tmpname, Load))
2648 return 0;
2649 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002650 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002651
2652 compiler_use_next_block(c, skip);
2653 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 for (i = 0; i < n; i++) {
2655 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002656 if (i == 0)
2657 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 ADDOP(c, POP_TOP);
2659 }
2660 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2661 compiler_use_next_block(c, anchor);
Georg Brandl2c4fb8d2006-10-29 08:47:08 +00002662 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002664 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 return 0;
2666
2667 return 1;
2668}
2669
2670static int
2671compiler_listcomp(struct compiler *c, expr_ty e)
2672{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002674 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 asdl_seq *generators = e->v.ListComp.generators;
2676
2677 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002678 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002679 if (!tmp)
2680 return 0;
2681 ADDOP_I(c, BUILD_LIST, 0);
2682 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002684 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2685 e->v.ListComp.elt);
2686 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 return rc;
2688}
2689
2690static int
2691compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002692 asdl_seq *generators, int gen_index,
2693 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694{
2695 /* generate code for the iterator, then each of the ifs,
2696 and then write to the element */
2697
2698 comprehension_ty ge;
2699 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002700 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701
2702 start = compiler_new_block(c);
2703 skip = compiler_new_block(c);
2704 if_cleanup = compiler_new_block(c);
2705 anchor = compiler_new_block(c);
2706 end = compiler_new_block(c);
2707
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002708 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 anchor == NULL || end == NULL)
2710 return 0;
2711
Anthony Baxter7b782b62006-04-11 12:01:56 +00002712 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 ADDOP_JREL(c, SETUP_LOOP, end);
2714 if (!compiler_push_fblock(c, LOOP, start))
2715 return 0;
2716
2717 if (gen_index == 0) {
2718 /* Receive outermost iter as an implicit argument */
2719 c->u->u_argcount = 1;
2720 ADDOP_I(c, LOAD_FAST, 0);
2721 }
2722 else {
2723 /* Sub-iter - calculate on the fly */
2724 VISIT(c, expr, ge->iter);
2725 ADDOP(c, GET_ITER);
2726 }
2727 compiler_use_next_block(c, start);
2728 ADDOP_JREL(c, FOR_ITER, anchor);
2729 NEXT_BLOCK(c);
2730 VISIT(c, expr, ge->target);
2731
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002732 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002733 n = asdl_seq_LEN(ge->ifs);
2734 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002735 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 VISIT(c, expr, e);
2737 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2738 NEXT_BLOCK(c);
2739 ADDOP(c, POP_TOP);
2740 }
2741
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002742 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2744 return 0;
2745
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002746 /* only append after the last 'for' generator */
2747 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748 VISIT(c, expr, elt);
2749 ADDOP(c, YIELD_VALUE);
2750 ADDOP(c, POP_TOP);
2751
2752 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754 for (i = 0; i < n; i++) {
2755 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002756 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 compiler_use_next_block(c, if_cleanup);
2758
2759 ADDOP(c, POP_TOP);
2760 }
2761 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2762 compiler_use_next_block(c, anchor);
2763 ADDOP(c, POP_BLOCK);
2764 compiler_pop_fblock(c, LOOP, start);
2765 compiler_use_next_block(c, end);
2766
2767 return 1;
2768}
2769
2770static int
2771compiler_genexp(struct compiler *c, expr_ty e)
2772{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002773 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 PyCodeObject *co;
2775 expr_ty outermost_iter = ((comprehension_ty)
2776 (asdl_seq_GET(e->v.GeneratorExp.generators,
2777 0)))->iter;
2778
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002779 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002780 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002781 if (!name)
2782 return 0;
2783 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784
2785 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2786 return 0;
2787 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2788 e->v.GeneratorExp.elt);
2789 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002790 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002791 if (co == NULL)
2792 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002794 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002795 Py_DECREF(co);
2796
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797 VISIT(c, expr, outermost_iter);
2798 ADDOP(c, GET_ITER);
2799 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800
2801 return 1;
2802}
2803
2804static int
2805compiler_visit_keyword(struct compiler *c, keyword_ty k)
2806{
2807 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2808 VISIT(c, expr, k->value);
2809 return 1;
2810}
2811
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002812/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813 whether they are true or false.
2814
2815 Return values: 1 for true, 0 for false, -1 for non-constant.
2816 */
2817
2818static int
2819expr_constant(expr_ty e)
2820{
2821 switch (e->kind) {
2822 case Num_kind:
2823 return PyObject_IsTrue(e->v.Num.n);
2824 case Str_kind:
2825 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002826 case Name_kind:
2827 /* __debug__ is not assignable, so we can optimize
2828 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002829 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002830 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002831 return ! Py_OptimizeFlag;
2832 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002833 default:
2834 return -1;
2835 }
2836}
2837
Guido van Rossumc2e20742006-02-27 22:32:47 +00002838/*
2839 Implements the with statement from PEP 343.
2840
2841 The semantics outlined in that PEP are as follows:
2842
2843 with EXPR as VAR:
2844 BLOCK
2845
2846 It is implemented roughly as:
2847
Guido van Rossumda5b7012006-05-02 19:47:52 +00002848 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002849 exit = context.__exit__ # not calling it
2850 value = context.__enter__()
2851 try:
2852 VAR = value # if VAR present in the syntax
2853 BLOCK
2854 finally:
2855 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002856 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002857 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002858 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002859 exit(*exc)
2860 */
2861static int
2862compiler_with(struct compiler *c, stmt_ty s)
2863{
Guido van Rossumda5b7012006-05-02 19:47:52 +00002864 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002865 basicblock *block, *finally;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002866 identifier tmpvalue = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002867
2868 assert(s->kind == With_kind);
2869
Guido van Rossumc2e20742006-02-27 22:32:47 +00002870 if (!enter_attr) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002871 enter_attr = PyString_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002872 if (!enter_attr)
2873 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002874 }
2875 if (!exit_attr) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002876 exit_attr = PyString_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002877 if (!exit_attr)
2878 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002879 }
2880
2881 block = compiler_new_block(c);
2882 finally = compiler_new_block(c);
2883 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002884 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002885
Guido van Rossumc2e20742006-02-27 22:32:47 +00002886 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002887 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002888 We need to do this rather than preserving it on the stack
2889 because SETUP_FINALLY remembers the stack level.
2890 We need to do the assignment *inside* the try/finally
2891 so that context.__exit__() is called when the assignment
2892 fails. But we need to call context.__enter__() *before*
2893 the try/finally so that if it fails we won't call
2894 context.__exit__().
2895 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002896 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002897 if (tmpvalue == NULL)
2898 return 0;
2899 PyArena_AddPyObject(c->c_arena, tmpvalue);
2900 }
2901
Guido van Rossumda5b7012006-05-02 19:47:52 +00002902 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002903 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002904
Nick Coghlan7af53be2008-03-07 14:13:28 +00002905 /* Squirrel away context.__exit__ by stuffing it under context */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002906 ADDOP(c, DUP_TOP);
2907 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
Nick Coghlan7af53be2008-03-07 14:13:28 +00002908 ADDOP(c, ROT_TWO);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002909
2910 /* Call context.__enter__() */
2911 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2912 ADDOP_I(c, CALL_FUNCTION, 0);
2913
2914 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002915 /* Store it in tmpvalue */
2916 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00002917 return 0;
2918 }
2919 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002920 /* Discard result from context.__enter__() */
2921 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002922 }
2923
2924 /* Start the try block */
2925 ADDOP_JREL(c, SETUP_FINALLY, finally);
2926
2927 compiler_use_next_block(c, block);
2928 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002929 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002930 }
2931
2932 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002933 /* Bind saved result of context.__enter__() to VAR */
2934 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00002935 !compiler_nameop(c, tmpvalue, Del))
2936 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002937 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002938 }
2939
2940 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002941 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002942
2943 /* End of try block; start the finally block */
2944 ADDOP(c, POP_BLOCK);
2945 compiler_pop_fblock(c, FINALLY_TRY, block);
2946
2947 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2948 compiler_use_next_block(c, finally);
2949 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002950 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002951
Nick Coghlan7af53be2008-03-07 14:13:28 +00002952 /* Finally block starts; context.__exit__ is on the stack under
2953 the exception or return information. Just issue our magic
2954 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002955 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002956
2957 /* Finally block ends. */
2958 ADDOP(c, END_FINALLY);
2959 compiler_pop_fblock(c, FINALLY_END, finally);
2960 return 1;
2961}
2962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963static int
2964compiler_visit_expr(struct compiler *c, expr_ty e)
2965{
2966 int i, n;
2967
Neal Norwitzf733a012006-10-29 18:30:10 +00002968 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002969 set a new line number for the next instruction.
2970 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 if (e->lineno > c->u->u_lineno) {
2972 c->u->u_lineno = e->lineno;
2973 c->u->u_lineno_set = false;
2974 }
2975 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002976 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002978 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 VISIT(c, expr, e->v.BinOp.left);
2980 VISIT(c, expr, e->v.BinOp.right);
2981 ADDOP(c, binop(c, e->v.BinOp.op));
2982 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002983 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002984 VISIT(c, expr, e->v.UnaryOp.operand);
2985 ADDOP(c, unaryop(e->v.UnaryOp.op));
2986 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002987 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002989 case IfExp_kind:
2990 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002991 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002992 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00002993 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002995 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002996 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00002997 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002998 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00002999 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 }
3001 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003002 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003003 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003004 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003005 return compiler_genexp(c, e);
3006 case Yield_kind:
3007 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003008 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003009 if (e->v.Yield.value) {
3010 VISIT(c, expr, e->v.Yield.value);
3011 }
3012 else {
3013 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3014 }
3015 ADDOP(c, YIELD_VALUE);
3016 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003017 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003018 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003019 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003021 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 VISIT(c, expr, e->v.Repr.value);
3023 ADDOP(c, UNARY_CONVERT);
3024 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003025 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3027 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003028 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3030 break;
3031 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003032 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003033 if (e->v.Attribute.ctx != AugStore)
3034 VISIT(c, expr, e->v.Attribute.value);
3035 switch (e->v.Attribute.ctx) {
3036 case AugLoad:
3037 ADDOP(c, DUP_TOP);
3038 /* Fall through to load */
3039 case Load:
3040 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3041 break;
3042 case AugStore:
3043 ADDOP(c, ROT_TWO);
3044 /* Fall through to save */
3045 case Store:
3046 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3047 break;
3048 case Del:
3049 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3050 break;
3051 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003052 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003053 PyErr_SetString(PyExc_SystemError,
3054 "param invalid in attribute expression");
3055 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 }
3057 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003058 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059 switch (e->v.Subscript.ctx) {
3060 case AugLoad:
3061 VISIT(c, expr, e->v.Subscript.value);
3062 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3063 break;
3064 case Load:
3065 VISIT(c, expr, e->v.Subscript.value);
3066 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3067 break;
3068 case AugStore:
3069 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3070 break;
3071 case Store:
3072 VISIT(c, expr, e->v.Subscript.value);
3073 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3074 break;
3075 case Del:
3076 VISIT(c, expr, e->v.Subscript.value);
3077 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3078 break;
3079 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003080 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003081 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003082 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003083 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084 }
3085 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003086 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3088 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003089 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003090 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003091 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003092 return compiler_tuple(c, e);
3093 }
3094 return 1;
3095}
3096
3097static int
3098compiler_augassign(struct compiler *c, stmt_ty s)
3099{
3100 expr_ty e = s->v.AugAssign.target;
3101 expr_ty auge;
3102
3103 assert(s->kind == AugAssign_kind);
3104
3105 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003106 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003107 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003108 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003109 if (auge == NULL)
3110 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 VISIT(c, expr, auge);
3112 VISIT(c, expr, s->v.AugAssign.value);
3113 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3114 auge->v.Attribute.ctx = AugStore;
3115 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116 break;
3117 case Subscript_kind:
3118 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003119 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 if (auge == NULL)
3121 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003122 VISIT(c, expr, auge);
3123 VISIT(c, expr, s->v.AugAssign.value);
3124 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003125 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003127 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003129 if (!compiler_nameop(c, e->v.Name.id, Load))
3130 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 VISIT(c, expr, s->v.AugAssign.value);
3132 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3133 return compiler_nameop(c, e->v.Name.id, Store);
3134 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003135 PyErr_Format(PyExc_SystemError,
3136 "invalid node type (%d) for augmented assignment",
3137 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003138 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003139 }
3140 return 1;
3141}
3142
3143static int
3144compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3145{
3146 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003147 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3148 PyErr_SetString(PyExc_SystemError,
3149 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003151 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 f = &c->u->u_fblock[c->u->u_nfblocks++];
3153 f->fb_type = t;
3154 f->fb_block = b;
3155 return 1;
3156}
3157
3158static void
3159compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3160{
3161 struct compiler_unit *u = c->u;
3162 assert(u->u_nfblocks > 0);
3163 u->u_nfblocks--;
3164 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3165 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3166}
3167
Jeremy Hylton82271f12006-10-04 02:24:52 +00003168static int
3169compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003170 int i;
3171 struct compiler_unit *u = c->u;
3172 for (i = 0; i < u->u_nfblocks; ++i) {
3173 if (u->u_fblock[i].fb_type == LOOP)
3174 return 1;
3175 }
3176 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003177}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178/* Raises a SyntaxError and returns 0.
3179 If something goes wrong, a different exception may be raised.
3180*/
3181
3182static int
3183compiler_error(struct compiler *c, const char *errstr)
3184{
3185 PyObject *loc;
3186 PyObject *u = NULL, *v = NULL;
3187
3188 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3189 if (!loc) {
3190 Py_INCREF(Py_None);
3191 loc = Py_None;
3192 }
3193 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3194 Py_None, loc);
3195 if (!u)
3196 goto exit;
3197 v = Py_BuildValue("(zO)", errstr, u);
3198 if (!v)
3199 goto exit;
3200 PyErr_SetObject(PyExc_SyntaxError, v);
3201 exit:
3202 Py_DECREF(loc);
3203 Py_XDECREF(u);
3204 Py_XDECREF(v);
3205 return 0;
3206}
3207
3208static int
3209compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003210 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003212 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003214 /* XXX this code is duplicated */
3215 switch (ctx) {
3216 case AugLoad: /* fall through to Load */
3217 case Load: op = BINARY_SUBSCR; break;
3218 case AugStore:/* fall through to Store */
3219 case Store: op = STORE_SUBSCR; break;
3220 case Del: op = DELETE_SUBSCR; break;
3221 case Param:
3222 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003223 "invalid %s kind %d in subscript\n",
3224 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003225 return 0;
3226 }
3227 if (ctx == AugLoad) {
3228 ADDOP_I(c, DUP_TOPX, 2);
3229 }
3230 else if (ctx == AugStore) {
3231 ADDOP(c, ROT_THREE);
3232 }
3233 ADDOP(c, op);
3234 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003235}
3236
3237static int
3238compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3239{
3240 int n = 2;
3241 assert(s->kind == Slice_kind);
3242
3243 /* only handles the cases where BUILD_SLICE is emitted */
3244 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003245 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 }
3247 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003248 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003250
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003252 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 }
3254 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003255 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 }
3257
3258 if (s->v.Slice.step) {
3259 n++;
3260 VISIT(c, expr, s->v.Slice.step);
3261 }
3262 ADDOP_I(c, BUILD_SLICE, n);
3263 return 1;
3264}
3265
3266static int
3267compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3268{
3269 int op = 0, slice_offset = 0, stack_count = 0;
3270
3271 assert(s->v.Slice.step == NULL);
3272 if (s->v.Slice.lower) {
3273 slice_offset++;
3274 stack_count++;
3275 if (ctx != AugStore)
3276 VISIT(c, expr, s->v.Slice.lower);
3277 }
3278 if (s->v.Slice.upper) {
3279 slice_offset += 2;
3280 stack_count++;
3281 if (ctx != AugStore)
3282 VISIT(c, expr, s->v.Slice.upper);
3283 }
3284
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003285 if (ctx == AugLoad) {
3286 switch (stack_count) {
3287 case 0: ADDOP(c, DUP_TOP); break;
3288 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3289 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3290 }
3291 }
3292 else if (ctx == AugStore) {
3293 switch (stack_count) {
3294 case 0: ADDOP(c, ROT_TWO); break;
3295 case 1: ADDOP(c, ROT_THREE); break;
3296 case 2: ADDOP(c, ROT_FOUR); break;
3297 }
3298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299
3300 switch (ctx) {
3301 case AugLoad: /* fall through to Load */
3302 case Load: op = SLICE; break;
3303 case AugStore:/* fall through to Store */
3304 case Store: op = STORE_SLICE; break;
3305 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003306 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003307 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003308 PyErr_SetString(PyExc_SystemError,
3309 "param invalid in simple slice");
3310 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003311 }
3312
3313 ADDOP(c, op + slice_offset);
3314 return 1;
3315}
3316
3317static int
3318compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3319 expr_context_ty ctx)
3320{
3321 switch (s->kind) {
3322 case Ellipsis_kind:
3323 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3324 break;
3325 case Slice_kind:
3326 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 case Index_kind:
3328 VISIT(c, expr, s->v.Index.value);
3329 break;
3330 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003331 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003332 PyErr_SetString(PyExc_SystemError,
3333 "extended slice invalid in nested slice");
3334 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335 }
3336 return 1;
3337}
3338
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339static int
3340compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3341{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003342 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003343 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003344 case Index_kind:
3345 kindname = "index";
3346 if (ctx != AugStore) {
3347 VISIT(c, expr, s->v.Index.value);
3348 }
3349 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003351 kindname = "ellipsis";
3352 if (ctx != AugStore) {
3353 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 break;
3356 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003357 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 if (!s->v.Slice.step)
3359 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003360 if (ctx != AugStore) {
3361 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362 return 0;
3363 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003364 break;
3365 case ExtSlice_kind:
3366 kindname = "extended slice";
3367 if (ctx != AugStore) {
3368 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3369 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003370 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003371 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003372 if (!compiler_visit_nested_slice(c, sub, ctx))
3373 return 0;
3374 }
3375 ADDOP_I(c, BUILD_TUPLE, n);
3376 }
3377 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003378 default:
3379 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003380 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003381 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003383 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384}
3385
Neal Norwitzf733a012006-10-29 18:30:10 +00003386
3387/* End of the compiler section, beginning of the assembler section */
3388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003389/* do depth-first search of basic block graph, starting with block.
3390 post records the block indices in post-order.
3391
3392 XXX must handle implicit jumps from one block to next
3393*/
3394
Neal Norwitzf733a012006-10-29 18:30:10 +00003395struct assembler {
3396 PyObject *a_bytecode; /* string containing bytecode */
3397 int a_offset; /* offset into bytecode */
3398 int a_nblocks; /* number of reachable blocks */
3399 basicblock **a_postorder; /* list of blocks in dfs postorder */
3400 PyObject *a_lnotab; /* string containing lnotab */
3401 int a_lnotab_off; /* offset into lnotab */
3402 int a_lineno; /* last lineno of emitted instruction */
3403 int a_lineno_off; /* bytecode offset of last lineno */
3404};
3405
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003406static void
3407dfs(struct compiler *c, basicblock *b, struct assembler *a)
3408{
3409 int i;
3410 struct instr *instr = NULL;
3411
3412 if (b->b_seen)
3413 return;
3414 b->b_seen = 1;
3415 if (b->b_next != NULL)
3416 dfs(c, b->b_next, a);
3417 for (i = 0; i < b->b_iused; i++) {
3418 instr = &b->b_instr[i];
3419 if (instr->i_jrel || instr->i_jabs)
3420 dfs(c, instr->i_target, a);
3421 }
3422 a->a_postorder[a->a_nblocks++] = b;
3423}
3424
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003425static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3427{
3428 int i;
3429 struct instr *instr;
3430 if (b->b_seen || b->b_startdepth >= depth)
3431 return maxdepth;
3432 b->b_seen = 1;
3433 b->b_startdepth = depth;
3434 for (i = 0; i < b->b_iused; i++) {
3435 instr = &b->b_instr[i];
3436 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3437 if (depth > maxdepth)
3438 maxdepth = depth;
3439 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3440 if (instr->i_jrel || instr->i_jabs) {
3441 maxdepth = stackdepth_walk(c, instr->i_target,
3442 depth, maxdepth);
3443 if (instr->i_opcode == JUMP_ABSOLUTE ||
3444 instr->i_opcode == JUMP_FORWARD) {
3445 goto out; /* remaining code is dead */
3446 }
3447 }
3448 }
3449 if (b->b_next)
3450 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3451out:
3452 b->b_seen = 0;
3453 return maxdepth;
3454}
3455
3456/* Find the flow path that needs the largest stack. We assume that
3457 * cycles in the flow graph have no net effect on the stack depth.
3458 */
3459static int
3460stackdepth(struct compiler *c)
3461{
3462 basicblock *b, *entryblock;
3463 entryblock = NULL;
3464 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3465 b->b_seen = 0;
3466 b->b_startdepth = INT_MIN;
3467 entryblock = b;
3468 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003469 if (!entryblock)
3470 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 return stackdepth_walk(c, entryblock, 0, 0);
3472}
3473
3474static int
3475assemble_init(struct assembler *a, int nblocks, int firstlineno)
3476{
3477 memset(a, 0, sizeof(struct assembler));
3478 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003479 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480 if (!a->a_bytecode)
3481 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003482 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 if (!a->a_lnotab)
3484 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003485 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3486 PyErr_NoMemory();
3487 return 0;
3488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003490 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003491 if (!a->a_postorder) {
3492 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 return 1;
3496}
3497
3498static void
3499assemble_free(struct assembler *a)
3500{
3501 Py_XDECREF(a->a_bytecode);
3502 Py_XDECREF(a->a_lnotab);
3503 if (a->a_postorder)
3504 PyObject_Free(a->a_postorder);
3505}
3506
3507/* Return the size of a basic block in bytes. */
3508
3509static int
3510instrsize(struct instr *instr)
3511{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003512 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003513 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003514 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003515 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3516 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517}
3518
3519static int
3520blocksize(basicblock *b)
3521{
3522 int i;
3523 int size = 0;
3524
3525 for (i = 0; i < b->b_iused; i++)
3526 size += instrsize(&b->b_instr[i]);
3527 return size;
3528}
3529
3530/* All about a_lnotab.
3531
3532c_lnotab is an array of unsigned bytes disguised as a Python string.
3533It is used to map bytecode offsets to source code line #s (when needed
3534for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003535
Tim Peters2a7f3842001-06-09 09:26:21 +00003536The array is conceptually a list of
3537 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003538pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003539
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003540 byte code offset source code line number
3541 0 1
3542 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003543 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003544 350 307
3545 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003546
3547The first trick is that these numbers aren't stored, only the increments
3548from one row to the next (this doesn't really work, but it's a start):
3549
3550 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3551
3552The second trick is that an unsigned byte can't hold negative values, or
3553values larger than 255, so (a) there's a deep assumption that byte code
3554offsets and their corresponding line #s both increase monotonically, and (b)
3555if at least one column jumps by more than 255 from one row to the next, more
3556than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003557from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003558part. A user of c_lnotab desiring to find the source line number
3559corresponding to a bytecode address A should do something like this
3560
3561 lineno = addr = 0
3562 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003563 addr += addr_incr
3564 if addr > A:
3565 return lineno
3566 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003567
3568In order for this to work, when the addr field increments by more than 255,
3569the line # increment in each pair generated must be 0 until the remaining addr
Neal Norwitz84be93b2006-07-16 01:50:38 +00003570increment is < 256. So, in the example above, assemble_lnotab (it used
3571to be called com_set_lineno) should not (as was actually done until 2.2)
3572expand 300, 300 to 255, 255, 45, 45,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003573 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003574*/
3575
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003576static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003578{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003579 int d_bytecode, d_lineno;
3580 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003581 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582
3583 d_bytecode = a->a_offset - a->a_lineno_off;
3584 d_lineno = i->i_lineno - a->a_lineno;
3585
3586 assert(d_bytecode >= 0);
3587 assert(d_lineno >= 0);
3588
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003589 if(d_bytecode == 0 && d_lineno == 0)
3590 return 1;
3591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003593 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003595 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003597 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003599 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003601 else {
3602 PyErr_NoMemory();
3603 return 0;
3604 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003605 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003606 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003607 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003608 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003609 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003610 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003611 *lnotab++ = 255;
3612 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 d_bytecode -= ncodes * 255;
3615 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003616 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003617 assert(d_bytecode <= 255);
3618 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003619 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003621 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003623 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003625 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003627 else {
3628 PyErr_NoMemory();
3629 return 0;
3630 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003631 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003632 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003633 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003634 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003635 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003637 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003639 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003640 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003641 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 d_lineno -= ncodes * 255;
3644 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003645 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003646
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003647 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003649 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003650 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003651 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003652 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003653 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003654
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655 a->a_lnotab_off += 2;
3656 if (d_bytecode) {
3657 *lnotab++ = d_bytecode;
3658 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003659 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003660 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 *lnotab++ = 0;
3662 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003664 a->a_lineno = i->i_lineno;
3665 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003666 return 1;
3667}
3668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669/* assemble_emit()
3670 Extend the bytecode with a new instruction.
3671 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003672*/
3673
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003674static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003676{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003677 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003678 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003679 char *code;
3680
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003681 size = instrsize(i);
3682 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003684 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003686 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003687 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003689 if (len > PY_SSIZE_T_MAX / 2)
3690 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003691 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003692 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003693 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003694 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003695 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003696 if (size == 6) {
3697 assert(i->i_hasarg);
3698 *code++ = (char)EXTENDED_ARG;
3699 *code++ = ext & 0xff;
3700 *code++ = ext >> 8;
3701 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003702 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003704 if (i->i_hasarg) {
3705 assert(size == 3 || size == 6);
3706 *code++ = arg & 0xff;
3707 *code++ = arg >> 8;
3708 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003710}
3711
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003712static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003714{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003716 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003717 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003718
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 /* Compute the size of each block and fixup jump args.
3720 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003721start:
3722 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003724 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 bsize = blocksize(b);
3726 b->b_offset = totsize;
3727 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003728 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003729 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003730 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3731 bsize = b->b_offset;
3732 for (i = 0; i < b->b_iused; i++) {
3733 struct instr *instr = &b->b_instr[i];
3734 /* Relative jumps are computed relative to
3735 the instruction pointer after fetching
3736 the jump instruction.
3737 */
3738 bsize += instrsize(instr);
3739 if (instr->i_jabs)
3740 instr->i_oparg = instr->i_target->b_offset;
3741 else if (instr->i_jrel) {
3742 int delta = instr->i_target->b_offset - bsize;
3743 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003744 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003745 else
3746 continue;
3747 if (instr->i_oparg > 0xffff)
3748 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003749 }
3750 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003751
3752 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003753 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003754 with a better solution.
3755
3756 In the meantime, should the goto be dropped in favor
3757 of a loop?
3758
3759 The issue is that in the first loop blocksize() is called
3760 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003761 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003762 i_oparg is calculated in the second loop above.
3763
3764 So we loop until we stop seeing new EXTENDED_ARGs.
3765 The only EXTENDED_ARGs that could be popping up are
3766 ones in jump instructions. So this should converge
3767 fairly quickly.
3768 */
3769 if (last_extended_arg_count != extended_arg_count) {
3770 last_extended_arg_count = extended_arg_count;
3771 goto start;
3772 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003773}
3774
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003775static PyObject *
3776dict_keys_inorder(PyObject *dict, int offset)
3777{
3778 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003779 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003780
3781 tuple = PyTuple_New(size);
3782 if (tuple == NULL)
3783 return NULL;
3784 while (PyDict_Next(dict, &pos, &k, &v)) {
3785 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003786 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003787 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003788 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003789 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003790 PyTuple_SET_ITEM(tuple, i - offset, k);
3791 }
3792 return tuple;
3793}
3794
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003795static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003797{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 PySTEntryObject *ste = c->u->u_ste;
3799 int flags = 0, n;
3800 if (ste->ste_type != ModuleBlock)
3801 flags |= CO_NEWLOCALS;
3802 if (ste->ste_type == FunctionBlock) {
3803 if (!ste->ste_unoptimized)
3804 flags |= CO_OPTIMIZED;
3805 if (ste->ste_nested)
3806 flags |= CO_NESTED;
3807 if (ste->ste_generator)
3808 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003809 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 if (ste->ste_varargs)
3811 flags |= CO_VARARGS;
3812 if (ste->ste_varkeywords)
3813 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003814 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003816
3817 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003818 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 n = PyDict_Size(c->u->u_freevars);
3821 if (n < 0)
3822 return -1;
3823 if (n == 0) {
3824 n = PyDict_Size(c->u->u_cellvars);
3825 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003826 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003827 if (n == 0) {
3828 flags |= CO_NOFREE;
3829 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003830 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003831
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003832 return flags;
3833}
3834
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835static PyCodeObject *
3836makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003837{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003838 PyObject *tmp;
3839 PyCodeObject *co = NULL;
3840 PyObject *consts = NULL;
3841 PyObject *names = NULL;
3842 PyObject *varnames = NULL;
3843 PyObject *filename = NULL;
3844 PyObject *name = NULL;
3845 PyObject *freevars = NULL;
3846 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003847 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 tmp = dict_keys_inorder(c->u->u_consts, 0);
3851 if (!tmp)
3852 goto error;
3853 consts = PySequence_List(tmp); /* optimize_code requires a list */
3854 Py_DECREF(tmp);
3855
3856 names = dict_keys_inorder(c->u->u_names, 0);
3857 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3858 if (!consts || !names || !varnames)
3859 goto error;
3860
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003861 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3862 if (!cellvars)
3863 goto error;
3864 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3865 if (!freevars)
3866 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003867 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 if (!filename)
3869 goto error;
3870
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003871 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 flags = compute_code_flags(c);
3873 if (flags < 0)
3874 goto error;
3875
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003876 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003877 if (!bytecode)
3878 goto error;
3879
3880 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3881 if (!tmp)
3882 goto error;
3883 Py_DECREF(consts);
3884 consts = tmp;
3885
3886 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3887 bytecode, consts, names, varnames,
3888 freevars, cellvars,
3889 filename, c->u->u_name,
3890 c->u->u_firstlineno,
3891 a->a_lnotab);
3892 error:
3893 Py_XDECREF(consts);
3894 Py_XDECREF(names);
3895 Py_XDECREF(varnames);
3896 Py_XDECREF(filename);
3897 Py_XDECREF(name);
3898 Py_XDECREF(freevars);
3899 Py_XDECREF(cellvars);
3900 Py_XDECREF(bytecode);
3901 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003902}
3903
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003904
3905/* For debugging purposes only */
3906#if 0
3907static void
3908dump_instr(const struct instr *i)
3909{
3910 const char *jrel = i->i_jrel ? "jrel " : "";
3911 const char *jabs = i->i_jabs ? "jabs " : "";
3912 char arg[128];
3913
3914 *arg = '\0';
3915 if (i->i_hasarg)
3916 sprintf(arg, "arg: %d ", i->i_oparg);
3917
3918 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3919 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3920}
3921
3922static void
3923dump_basicblock(const basicblock *b)
3924{
3925 const char *seen = b->b_seen ? "seen " : "";
3926 const char *b_return = b->b_return ? "return " : "";
3927 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3928 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3929 if (b->b_instr) {
3930 int i;
3931 for (i = 0; i < b->b_iused; i++) {
3932 fprintf(stderr, " [%02d] ", i);
3933 dump_instr(b->b_instr + i);
3934 }
3935 }
3936}
3937#endif
3938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939static PyCodeObject *
3940assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003941{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003942 basicblock *b, *entryblock;
3943 struct assembler a;
3944 int i, j, nblocks;
3945 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947 /* Make sure every block that falls off the end returns None.
3948 XXX NEXT_BLOCK() isn't quite right, because if the last
3949 block ends with a jump or return b_next shouldn't set.
3950 */
3951 if (!c->u->u_curblock->b_return) {
3952 NEXT_BLOCK(c);
3953 if (addNone)
3954 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3955 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003956 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 nblocks = 0;
3959 entryblock = NULL;
3960 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3961 nblocks++;
3962 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003963 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003964
Neal Norwitzed657552006-07-10 00:04:44 +00003965 /* Set firstlineno if it wasn't explicitly set. */
3966 if (!c->u->u_firstlineno) {
3967 if (entryblock && entryblock->b_instr)
3968 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3969 else
3970 c->u->u_firstlineno = 1;
3971 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3973 goto error;
3974 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003975
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003977 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003978
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 /* Emit code in reverse postorder from dfs. */
3980 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003981 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003982 for (j = 0; j < b->b_iused; j++)
3983 if (!assemble_emit(&a, &b->b_instr[j]))
3984 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003985 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003986
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003987 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003988 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003989 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 co = makecode(c, &a);
3993 error:
3994 assemble_free(&a);
3995 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003996}