blob: 3b5f5ef0a77ff15d62c63d105680c9784390c21f [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 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114
115 int u_nfblocks;
116 struct fblockinfo u_fblock[CO_MAXBLOCKS];
117
118 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000119 int u_lineno; /* the lineno for the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120 bool u_lineno_set; /* boolean to indicate whether instr
121 has been generated with current lineno */
122};
123
124/* This struct captures the global state of a compilation.
125
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000126The u pointer points to the current compilation unit, while units
127for enclosing blocks are stored in c_stack. The u and c_stack are
128managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129*/
130
131struct compiler {
132 const char *c_filename;
133 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000134 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135 PyCompilerFlags *c_flags;
136
Neal Norwitz4ffedad2006-08-04 04:58:47 +0000137 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000138 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000139
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 struct compiler_unit *u; /* compiler state for current block */
141 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000142 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143};
144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145static int compiler_enter_scope(struct compiler *, identifier, void *, int);
146static void compiler_free(struct compiler *);
147static basicblock *compiler_new_block(struct compiler *);
148static int compiler_next_instr(struct compiler *, basicblock *);
149static int compiler_addop(struct compiler *, int);
150static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
151static int compiler_addop_i(struct compiler *, int, int);
152static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153static basicblock *compiler_use_new_block(struct compiler *);
154static int compiler_error(struct compiler *, const char *);
155static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
156
157static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
158static int compiler_visit_stmt(struct compiler *, stmt_ty);
159static int compiler_visit_keyword(struct compiler *, keyword_ty);
160static int compiler_visit_expr(struct compiler *, expr_ty);
161static int compiler_augassign(struct compiler *, stmt_ty);
162static int compiler_visit_slice(struct compiler *, slice_ty,
163 expr_context_ty);
164
165static int compiler_push_fblock(struct compiler *, enum fblocktype,
166 basicblock *);
167static void compiler_pop_fblock(struct compiler *, enum fblocktype,
168 basicblock *);
Jeremy Hylton82271f12006-10-04 02:24:52 +0000169/* Returns true if there is a loop on the fblock stack. */
170static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
172static int inplace_binop(struct compiler *, operator_ty);
173static int expr_constant(expr_ty e);
174
Guido van Rossumc2e20742006-02-27 22:32:47 +0000175static int compiler_with(struct compiler *, stmt_ty);
176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177static PyCodeObject *assemble(struct compiler *, int addNone);
178static PyObject *__doc__;
179
180PyObject *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000181_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000182{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000183 /* Name mangling: __private becomes _classname__private.
184 This is independent from how the name is used. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000185 const char *p, *name = PyString_AsString(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000186 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187 size_t nlen, plen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000188 if (privateobj == NULL || !PyString_Check(privateobj) ||
Neal Norwitz84167d02006-08-12 01:45:47 +0000189 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000190 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000192 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000193 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194 nlen = strlen(name);
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000195 /* Don't mangle __id__ or names with dots.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000196
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000197 The only time a name with a dot can occur is when
198 we are compiling an import statement that has a
199 package name.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000200
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000201 TODO(jhylton): Decide whether we want to support
202 mangling of the module name, e.g. __M.X.
203 */
Jeremy Hylton37075c52007-02-27 01:01:59 +0000204 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000205 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000206 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 /* Strip leading underscores from class name */
210 while (*p == '_')
211 p++;
212 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000213 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000214 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000215 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 plen = strlen(p);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000217
218 assert(1 <= PY_SSIZE_T_MAX - nlen);
219 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
220
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000221 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000222 if (!ident)
223 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000225 buffer = PyString_AS_STRING(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000226 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227 strncpy(buffer+1, p, plen);
228 strcpy(buffer+1+plen, name);
229 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000230}
231
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232static int
233compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000234{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 c->c_stack = PyList_New(0);
238 if (!c->c_stack)
239 return 0;
240
241 return 1;
242}
243
244PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000245PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000246 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247{
248 struct compiler c;
249 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000250 PyCompilerFlags local_flags;
251 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000253 if (!__doc__) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000254 __doc__ = PyString_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000255 if (!__doc__)
256 return NULL;
257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258
259 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000260 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000262 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263 c.c_future = PyFuture_FromAST(mod, filename);
264 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000265 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000267 local_flags.cf_flags = 0;
268 flags = &local_flags;
269 }
270 merged = c.c_future->ff_features | flags->cf_flags;
271 c.c_future->ff_features = merged;
272 flags->cf_flags = merged;
273 c.c_flags = flags;
274 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275
276 c.c_st = PySymtable_Build(mod, filename, c.c_future);
277 if (c.c_st == NULL) {
278 if (!PyErr_Occurred())
279 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000280 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 }
282
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283 co = compiler_mod(&c, mod);
284
Thomas Wouters1175c432006-02-27 22:49:54 +0000285 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000287 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288 return co;
289}
290
291PyCodeObject *
292PyNode_Compile(struct _node *n, const char *filename)
293{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000294 PyCodeObject *co = NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000295 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000296 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000297 if (!arena)
298 return NULL;
299 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000300 if (mod)
301 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000302 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000303 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000304}
305
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000306static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000308{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000309 if (c->c_st)
310 PySymtable_Free(c->c_st);
311 if (c->c_future)
Neal Norwitz14bc4e42006-04-10 06:57:06 +0000312 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000314}
315
Guido van Rossum79f25d91997-04-29 20:08:16 +0000316static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000317list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000318{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000319 Py_ssize_t i, n;
Georg Brandl5c170fd2006-03-17 19:03:25 +0000320 PyObject *v, *k;
321 PyObject *dict = PyDict_New();
322 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000323
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 n = PyList_Size(list);
325 for (i = 0; i < n; i++) {
326 v = PyInt_FromLong(i);
327 if (!v) {
328 Py_DECREF(dict);
329 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000330 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000331 k = PyList_GET_ITEM(list, i);
Georg Brandl7784f122006-05-26 20:04:44 +0000332 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000333 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
334 Py_XDECREF(k);
335 Py_DECREF(v);
336 Py_DECREF(dict);
337 return NULL;
338 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000339 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000341 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342 return dict;
343}
344
345/* Return new dict containing names from src that match scope(s).
346
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000347src is a symbol table dictionary. If the scope of a name matches
348either scope_type or flag is set, insert it into the new dict. The
349values are integers, starting at offset and increasing by one for
350each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351*/
352
353static PyObject *
354dictbytype(PyObject *src, int scope_type, int flag, int offset)
355{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000356 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357 PyObject *k, *v, *dest = PyDict_New();
358
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000359 assert(offset >= 0);
360 if (dest == NULL)
361 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362
363 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000364 /* XXX this should probably be a macro in symtable.h */
365 assert(PyInt_Check(v));
366 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000367
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000368 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
369 PyObject *tuple, *item = PyInt_FromLong(i);
370 if (item == NULL) {
371 Py_DECREF(dest);
372 return NULL;
373 }
374 i++;
Georg Brandl7784f122006-05-26 20:04:44 +0000375 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000376 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
377 Py_DECREF(item);
378 Py_DECREF(dest);
379 Py_XDECREF(tuple);
380 return NULL;
381 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000383 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385 }
386 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000387}
388
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389static void
390compiler_unit_check(struct compiler_unit *u)
391{
392 basicblock *block;
393 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Brett Cannona6c41bc2008-02-07 07:47:31 +0000394 assert((void *)block != (void *)0xcbcbcbcb);
395 assert((void *)block != (void *)0xfbfbfbfb);
396 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 if (block->b_instr != NULL) {
398 assert(block->b_ialloc > 0);
399 assert(block->b_iused > 0);
400 assert(block->b_ialloc >= block->b_iused);
401 }
402 else {
403 assert (block->b_iused == 0);
404 assert (block->b_ialloc == 0);
405 }
406 }
407}
408
409static void
410compiler_unit_free(struct compiler_unit *u)
411{
412 basicblock *b, *next;
413
414 compiler_unit_check(u);
415 b = u->u_blocks;
416 while (b != NULL) {
417 if (b->b_instr)
418 PyObject_Free((void *)b->b_instr);
419 next = b->b_list;
420 PyObject_Free((void *)b);
421 b = next;
422 }
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000423 Py_CLEAR(u->u_ste);
424 Py_CLEAR(u->u_name);
425 Py_CLEAR(u->u_consts);
426 Py_CLEAR(u->u_names);
427 Py_CLEAR(u->u_varnames);
428 Py_CLEAR(u->u_freevars);
429 Py_CLEAR(u->u_cellvars);
430 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 PyObject_Free(u);
432}
433
434static int
435compiler_enter_scope(struct compiler *c, identifier name, void *key,
436 int lineno)
437{
438 struct compiler_unit *u;
439
Anthony Baxter7b782b62006-04-11 12:01:56 +0000440 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000441 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000442 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000443 PyErr_NoMemory();
444 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000445 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000446 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447 u->u_argcount = 0;
448 u->u_ste = PySymtable_Lookup(c->c_st, key);
449 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000450 compiler_unit_free(u);
451 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452 }
453 Py_INCREF(name);
454 u->u_name = name;
455 u->u_varnames = list2dict(u->u_ste->ste_varnames);
456 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000457 if (!u->u_varnames || !u->u_cellvars) {
458 compiler_unit_free(u);
459 return 0;
460 }
461
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000463 PyDict_Size(u->u_cellvars));
Neal Norwitzd12bd012006-07-21 07:59:47 +0000464 if (!u->u_freevars) {
465 compiler_unit_free(u);
466 return 0;
467 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468
469 u->u_blocks = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470 u->u_nfblocks = 0;
471 u->u_firstlineno = lineno;
472 u->u_lineno = 0;
473 u->u_lineno_set = false;
474 u->u_consts = PyDict_New();
475 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000476 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 return 0;
478 }
479 u->u_names = PyDict_New();
480 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000481 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482 return 0;
483 }
484
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000485 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486
487 /* Push the old compiler_unit on the stack. */
488 if (c->u) {
489 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000490 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
491 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000492 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return 0;
494 }
495 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000496 u->u_private = c->u->u_private;
497 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 }
499 c->u = u;
500
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000501 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000502 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000503 return 0;
504
505 return 1;
506}
507
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000508static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509compiler_exit_scope(struct compiler *c)
510{
511 int n;
512 PyObject *wrapper;
513
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000514 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 compiler_unit_free(c->u);
516 /* Restore c->u to the parent unit. */
517 n = PyList_GET_SIZE(c->c_stack) - 1;
518 if (n >= 0) {
519 wrapper = PyList_GET_ITEM(c->c_stack, n);
520 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neal Norwitz87557cd2006-08-21 18:01:30 +0000521 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000522 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000524 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 compiler_unit_check(c->u);
526 }
527 else
528 c->u = NULL;
529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
532/* Allocate a new block and return a pointer to it.
533 Returns NULL on error.
534*/
535
536static basicblock *
537compiler_new_block(struct compiler *c)
538{
539 basicblock *b;
540 struct compiler_unit *u;
541
542 u = c->u;
543 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000544 if (b == NULL) {
545 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000547 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548 memset((void *)b, 0, sizeof(basicblock));
Neal Norwitzf733a012006-10-29 18:30:10 +0000549 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550 b->b_list = u->u_blocks;
551 u->u_blocks = b;
552 return b;
553}
554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555static basicblock *
556compiler_use_new_block(struct compiler *c)
557{
558 basicblock *block = compiler_new_block(c);
559 if (block == NULL)
560 return NULL;
561 c->u->u_curblock = block;
562 return block;
563}
564
565static basicblock *
566compiler_next_block(struct compiler *c)
567{
568 basicblock *block = compiler_new_block(c);
569 if (block == NULL)
570 return NULL;
571 c->u->u_curblock->b_next = block;
572 c->u->u_curblock = block;
573 return block;
574}
575
576static basicblock *
577compiler_use_next_block(struct compiler *c, basicblock *block)
578{
579 assert(block != NULL);
580 c->u->u_curblock->b_next = block;
581 c->u->u_curblock = block;
582 return block;
583}
584
585/* Returns the offset of the next instruction in the current block's
586 b_instr array. Resizes the b_instr as necessary.
587 Returns -1 on failure.
Neal Norwitzf733a012006-10-29 18:30:10 +0000588*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589
590static int
591compiler_next_instr(struct compiler *c, basicblock *b)
592{
593 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000594 if (b->b_instr == NULL) {
Anthony Baxter7b782b62006-04-11 12:01:56 +0000595 b->b_instr = (struct instr *)PyObject_Malloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000596 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000597 if (b->b_instr == NULL) {
598 PyErr_NoMemory();
599 return -1;
600 }
601 b->b_ialloc = DEFAULT_BLOCK_SIZE;
602 memset((char *)b->b_instr, 0,
603 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000604 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605 else if (b->b_iused == b->b_ialloc) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000606 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607 size_t oldsize, newsize;
608 oldsize = b->b_ialloc * sizeof(struct instr);
609 newsize = oldsize << 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000610
611 if (oldsize > (PY_SIZE_MAX >> 1)) {
612 PyErr_NoMemory();
613 return -1;
614 }
615
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 if (newsize == 0) {
617 PyErr_NoMemory();
618 return -1;
619 }
620 b->b_ialloc <<= 1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000621 tmp = (struct instr *)PyObject_Realloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000622 (void *)b->b_instr, newsize);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000623 if (tmp == NULL) {
624 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 return -1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000626 }
627 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
629 }
630 return b->b_iused++;
631}
632
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000633/* Set the i_lineno member of the instruction at offset off if the
634 line number for the current expression/statement has not
Jeremy Hylton12603c42006-04-01 16:18:02 +0000635 already been set. If it has been set, the call has no effect.
636
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000637 The line number is reset in the following cases:
638 - when entering a new scope
639 - on each statement
640 - on each expression that start a new line
641 - before the "except" clause
642 - before the "for" and "while" expressions
Neal Norwitzf733a012006-10-29 18:30:10 +0000643*/
Jeremy Hylton12603c42006-04-01 16:18:02 +0000644
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645static void
646compiler_set_lineno(struct compiler *c, int off)
647{
648 basicblock *b;
649 if (c->u->u_lineno_set)
650 return;
651 c->u->u_lineno_set = true;
652 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000653 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000654}
655
656static int
657opcode_stack_effect(int opcode, int oparg)
658{
659 switch (opcode) {
660 case POP_TOP:
661 return -1;
662 case ROT_TWO:
663 case ROT_THREE:
664 return 0;
665 case DUP_TOP:
666 return 1;
667 case ROT_FOUR:
668 return 0;
669
670 case UNARY_POSITIVE:
671 case UNARY_NEGATIVE:
672 case UNARY_NOT:
673 case UNARY_CONVERT:
674 case UNARY_INVERT:
675 return 0;
676
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000677 case LIST_APPEND:
Antoine Pitroud0c35152008-12-17 00:38:28 +0000678 return -1;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 case BINARY_POWER:
681 case BINARY_MULTIPLY:
682 case BINARY_DIVIDE:
683 case BINARY_MODULO:
684 case BINARY_ADD:
685 case BINARY_SUBTRACT:
686 case BINARY_SUBSCR:
687 case BINARY_FLOOR_DIVIDE:
688 case BINARY_TRUE_DIVIDE:
689 return -1;
690 case INPLACE_FLOOR_DIVIDE:
691 case INPLACE_TRUE_DIVIDE:
692 return -1;
693
694 case SLICE+0:
695 return 1;
696 case SLICE+1:
697 return 0;
698 case SLICE+2:
699 return 0;
700 case SLICE+3:
701 return -1;
702
703 case STORE_SLICE+0:
704 return -2;
705 case STORE_SLICE+1:
706 return -3;
707 case STORE_SLICE+2:
708 return -3;
709 case STORE_SLICE+3:
710 return -4;
711
712 case DELETE_SLICE+0:
713 return -1;
714 case DELETE_SLICE+1:
715 return -2;
716 case DELETE_SLICE+2:
717 return -2;
718 case DELETE_SLICE+3:
719 return -3;
720
721 case INPLACE_ADD:
722 case INPLACE_SUBTRACT:
723 case INPLACE_MULTIPLY:
724 case INPLACE_DIVIDE:
725 case INPLACE_MODULO:
726 return -1;
727 case STORE_SUBSCR:
728 return -3;
Raymond Hettingereffde122007-12-18 18:26:18 +0000729 case STORE_MAP:
730 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731 case DELETE_SUBSCR:
732 return -2;
733
734 case BINARY_LSHIFT:
735 case BINARY_RSHIFT:
736 case BINARY_AND:
737 case BINARY_XOR:
738 case BINARY_OR:
739 return -1;
740 case INPLACE_POWER:
741 return -1;
742 case GET_ITER:
743 return 0;
744
745 case PRINT_EXPR:
746 return -1;
747 case PRINT_ITEM:
748 return -1;
749 case PRINT_NEWLINE:
750 return 0;
751 case PRINT_ITEM_TO:
752 return -2;
753 case PRINT_NEWLINE_TO:
754 return -1;
755 case INPLACE_LSHIFT:
756 case INPLACE_RSHIFT:
757 case INPLACE_AND:
758 case INPLACE_XOR:
759 case INPLACE_OR:
760 return -1;
761 case BREAK_LOOP:
762 return 0;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000763 case SETUP_WITH:
Benjamin Peterson49a6b0e2009-05-25 20:12:57 +0000764 return 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000765 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000766 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767 case LOAD_LOCALS:
768 return 1;
769 case RETURN_VALUE:
770 return -1;
771 case IMPORT_STAR:
772 return -1;
773 case EXEC_STMT:
774 return -3;
775 case YIELD_VALUE:
776 return 0;
777
778 case POP_BLOCK:
779 return 0;
780 case END_FINALLY:
781 return -1; /* or -2 or -3 if exception occurred */
782 case BUILD_CLASS:
783 return -2;
784
785 case STORE_NAME:
786 return -1;
787 case DELETE_NAME:
788 return 0;
789 case UNPACK_SEQUENCE:
790 return oparg-1;
791 case FOR_ITER:
792 return 1;
793
794 case STORE_ATTR:
795 return -2;
796 case DELETE_ATTR:
797 return -1;
798 case STORE_GLOBAL:
799 return -1;
800 case DELETE_GLOBAL:
801 return 0;
802 case DUP_TOPX:
803 return oparg;
804 case LOAD_CONST:
805 return 1;
806 case LOAD_NAME:
807 return 1;
808 case BUILD_TUPLE:
809 case BUILD_LIST:
810 return 1-oparg;
811 case BUILD_MAP:
812 return 1;
813 case LOAD_ATTR:
814 return 0;
815 case COMPARE_OP:
816 return -1;
817 case IMPORT_NAME:
818 return 0;
819 case IMPORT_FROM:
820 return 1;
821
822 case JUMP_FORWARD:
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000823 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
824 case JUMP_IF_FALSE_OR_POP: /* "" */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 case JUMP_ABSOLUTE:
826 return 0;
827
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000828 case POP_JUMP_IF_FALSE:
829 case POP_JUMP_IF_TRUE:
830 return -1;
831
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 case LOAD_GLOBAL:
833 return 1;
834
835 case CONTINUE_LOOP:
836 return 0;
837 case SETUP_LOOP:
838 return 0;
839 case SETUP_EXCEPT:
840 case SETUP_FINALLY:
841 return 3; /* actually pushed by an exception */
842
843 case LOAD_FAST:
844 return 1;
845 case STORE_FAST:
846 return -1;
847 case DELETE_FAST:
848 return 0;
849
850 case RAISE_VARARGS:
851 return -oparg;
852#define NARGS(o) (((o) % 256) + 2*((o) / 256))
853 case CALL_FUNCTION:
854 return -NARGS(oparg);
855 case CALL_FUNCTION_VAR:
856 case CALL_FUNCTION_KW:
857 return -NARGS(oparg)-1;
858 case CALL_FUNCTION_VAR_KW:
859 return -NARGS(oparg)-2;
860#undef NARGS
861 case MAKE_FUNCTION:
862 return -oparg;
863 case BUILD_SLICE:
864 if (oparg == 3)
865 return -2;
866 else
867 return -1;
868
869 case MAKE_CLOSURE:
870 return -oparg;
871 case LOAD_CLOSURE:
872 return 1;
873 case LOAD_DEREF:
874 return 1;
875 case STORE_DEREF:
876 return -1;
877 default:
878 fprintf(stderr, "opcode = %d\n", opcode);
879 Py_FatalError("opcode_stack_effect()");
880
881 }
882 return 0; /* not reachable */
883}
884
885/* Add an opcode with no argument.
886 Returns 0 on failure, 1 on success.
887*/
888
889static int
890compiler_addop(struct compiler *c, int opcode)
891{
892 basicblock *b;
893 struct instr *i;
894 int off;
895 off = compiler_next_instr(c, c->u->u_curblock);
896 if (off < 0)
897 return 0;
898 b = c->u->u_curblock;
899 i = &b->b_instr[off];
900 i->i_opcode = opcode;
901 i->i_hasarg = 0;
902 if (opcode == RETURN_VALUE)
903 b->b_return = 1;
904 compiler_set_lineno(c, off);
905 return 1;
906}
907
908static int
909compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
910{
911 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000912 Py_ssize_t arg;
Mark Dickinson105be772008-01-31 22:17:37 +0000913 unsigned char *p, *q;
914 Py_complex z;
915 double d;
916 int real_part_zero, imag_part_zero;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000918 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000919 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
920 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000921 d = PyFloat_AS_DOUBLE(o);
922 p = (unsigned char*) &d;
923 /* all we need is to make the tuple different in either the 0.0
924 * or -0.0 case from all others, just to avoid the "coercion".
925 */
926 if (*p==0 && p[sizeof(double)-1]==0)
927 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
928 else
929 t = PyTuple_Pack(2, o, o->ob_type);
930 }
931 else if (PyComplex_Check(o)) {
932 /* complex case is even messier: we need to make complex(x,
933 0.) different from complex(x, -0.) and complex(0., y)
934 different from complex(-0., y), for any x and y. In
935 particular, all four complex zeros should be
936 distinguished.*/
937 z = PyComplex_AsCComplex(o);
938 p = (unsigned char*) &(z.real);
939 q = (unsigned char*) &(z.imag);
940 /* all that matters here is that on IEEE platforms
941 real_part_zero will be true if z.real == 0., and false if
942 z.real == -0. In fact, real_part_zero will also be true
943 for some other rarely occurring nonzero floats, but this
944 doesn't matter. Similar comments apply to
945 imag_part_zero. */
946 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
947 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
948 if (real_part_zero && imag_part_zero) {
949 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
950 }
951 else if (real_part_zero && !imag_part_zero) {
952 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
953 }
954 else if (!real_part_zero && imag_part_zero) {
955 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
956 }
957 else {
958 t = PyTuple_Pack(2, o, o->ob_type);
959 }
960 }
961 else {
962 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000963 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000964 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000965 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
967 v = PyDict_GetItem(dict, t);
968 if (!v) {
969 arg = PyDict_Size(dict);
970 v = PyInt_FromLong(arg);
971 if (!v) {
972 Py_DECREF(t);
973 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000974 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000975 if (PyDict_SetItem(dict, t, v) < 0) {
976 Py_DECREF(t);
977 Py_DECREF(v);
978 return -1;
979 }
980 Py_DECREF(v);
981 }
982 else
983 arg = PyInt_AsLong(v);
984 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000985 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986}
987
988static int
989compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
990 PyObject *o)
991{
992 int arg = compiler_add_o(c, dict, o);
993 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000994 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return compiler_addop_i(c, opcode, arg);
996}
997
998static int
999compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001000 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001{
1002 int arg;
1003 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1004 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006 arg = compiler_add_o(c, dict, mangled);
1007 Py_DECREF(mangled);
1008 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010 return compiler_addop_i(c, opcode, arg);
1011}
1012
1013/* Add an opcode with an integer argument.
1014 Returns 0 on failure, 1 on success.
1015*/
1016
1017static int
1018compiler_addop_i(struct compiler *c, int opcode, int oparg)
1019{
1020 struct instr *i;
1021 int off;
1022 off = compiler_next_instr(c, c->u->u_curblock);
1023 if (off < 0)
1024 return 0;
1025 i = &c->u->u_curblock->b_instr[off];
1026 i->i_opcode = opcode;
1027 i->i_oparg = oparg;
1028 i->i_hasarg = 1;
1029 compiler_set_lineno(c, off);
1030 return 1;
1031}
1032
1033static int
1034compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1035{
1036 struct instr *i;
1037 int off;
1038
1039 assert(b != NULL);
1040 off = compiler_next_instr(c, c->u->u_curblock);
1041 if (off < 0)
1042 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 i = &c->u->u_curblock->b_instr[off];
1044 i->i_opcode = opcode;
1045 i->i_target = b;
1046 i->i_hasarg = 1;
1047 if (absolute)
1048 i->i_jabs = 1;
1049 else
1050 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001051 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 return 1;
1053}
1054
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001055/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1056 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057 it as the current block. NEXT_BLOCK() also creates an implicit jump
1058 from the current block to the new block.
1059*/
1060
Neal Norwitzf733a012006-10-29 18:30:10 +00001061/* The returns inside these macros make it impossible to decref objects
1062 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063*/
1064
1065
1066#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001067 if (compiler_use_new_block((C)) == NULL) \
1068 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001069}
1070
1071#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001072 if (compiler_next_block((C)) == NULL) \
1073 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074}
1075
1076#define ADDOP(C, OP) { \
1077 if (!compiler_addop((C), (OP))) \
1078 return 0; \
1079}
1080
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001081#define ADDOP_IN_SCOPE(C, OP) { \
1082 if (!compiler_addop((C), (OP))) { \
1083 compiler_exit_scope(c); \
1084 return 0; \
1085 } \
1086}
1087
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001088#define ADDOP_O(C, OP, O, TYPE) { \
1089 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1090 return 0; \
1091}
1092
1093#define ADDOP_NAME(C, OP, O, TYPE) { \
1094 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1095 return 0; \
1096}
1097
1098#define ADDOP_I(C, OP, O) { \
1099 if (!compiler_addop_i((C), (OP), (O))) \
1100 return 0; \
1101}
1102
1103#define ADDOP_JABS(C, OP, O) { \
1104 if (!compiler_addop_j((C), (OP), (O), 1)) \
1105 return 0; \
1106}
1107
1108#define ADDOP_JREL(C, OP, O) { \
1109 if (!compiler_addop_j((C), (OP), (O), 0)) \
1110 return 0; \
1111}
1112
1113/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1114 the ASDL name to synthesize the name of the C type and the visit function.
1115*/
1116
1117#define VISIT(C, TYPE, V) {\
1118 if (!compiler_visit_ ## TYPE((C), (V))) \
1119 return 0; \
1120}
1121
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001122#define VISIT_IN_SCOPE(C, TYPE, V) {\
1123 if (!compiler_visit_ ## TYPE((C), (V))) { \
1124 compiler_exit_scope(c); \
1125 return 0; \
1126 } \
1127}
1128
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129#define VISIT_SLICE(C, V, CTX) {\
1130 if (!compiler_visit_slice((C), (V), (CTX))) \
1131 return 0; \
1132}
1133
1134#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001135 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001137 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001138 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001139 if (!compiler_visit_ ## TYPE((C), elt)) \
1140 return 0; \
1141 } \
1142}
1143
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001144#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001145 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001146 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001147 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001148 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001149 if (!compiler_visit_ ## TYPE((C), elt)) { \
1150 compiler_exit_scope(c); \
1151 return 0; \
1152 } \
1153 } \
1154}
1155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156static int
1157compiler_isdocstring(stmt_ty s)
1158{
1159 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001160 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001161 return s->v.Expr.value->kind == Str_kind;
1162}
1163
1164/* Compile a sequence of statements, checking for a docstring. */
1165
1166static int
1167compiler_body(struct compiler *c, asdl_seq *stmts)
1168{
1169 int i = 0;
1170 stmt_ty st;
1171
1172 if (!asdl_seq_LEN(stmts))
1173 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001174 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001175 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1176 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 i = 1;
1178 VISIT(c, expr, st->v.Expr.value);
1179 if (!compiler_nameop(c, __doc__, Store))
1180 return 0;
1181 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001182 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001183 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001184 return 1;
1185}
1186
1187static PyCodeObject *
1188compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001189{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001190 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001191 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 static PyObject *module;
1193 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001194 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 if (!module)
1196 return NULL;
1197 }
Neal Norwitzed657552006-07-10 00:04:44 +00001198 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1199 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001200 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201 switch (mod->kind) {
1202 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001203 if (!compiler_body(c, mod->v.Module.body)) {
1204 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001206 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 break;
1208 case Interactive_kind:
1209 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001210 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001211 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001212 break;
1213 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001214 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001215 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001216 break;
1217 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001218 PyErr_SetString(PyExc_SystemError,
1219 "suite should not be possible");
1220 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001221 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001222 PyErr_Format(PyExc_SystemError,
1223 "module kind %d should not be possible",
1224 mod->kind);
1225 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001226 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227 co = assemble(c, addNone);
1228 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001229 return co;
1230}
1231
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232/* The test for LOCAL must come before the test for FREE in order to
1233 handle classes where name is both local and free. The local var is
1234 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001235*/
1236
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001237static int
1238get_ref_type(struct compiler *c, PyObject *name)
1239{
1240 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001241 if (scope == 0) {
1242 char buf[350];
1243 PyOS_snprintf(buf, sizeof(buf),
1244 "unknown scope for %.100s in %.100s(%s) in %s\n"
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00001245 "symbols: %s\nlocals: %s\nglobals: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001246 PyString_AS_STRING(name),
1247 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001248 PyObject_REPR(c->u->u_ste->ste_id),
1249 c->c_filename,
1250 PyObject_REPR(c->u->u_ste->ste_symbols),
1251 PyObject_REPR(c->u->u_varnames),
1252 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001254 Py_FatalError(buf);
1255 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001256
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001257 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258}
1259
1260static int
1261compiler_lookup_arg(PyObject *dict, PyObject *name)
1262{
1263 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001264 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001265 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001266 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001268 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001270 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 return PyInt_AS_LONG(v);
1272}
1273
1274static int
1275compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1276{
1277 int i, free = PyCode_GetNumFree(co);
1278 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001279 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1280 ADDOP_I(c, MAKE_FUNCTION, args);
1281 return 1;
1282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283 for (i = 0; i < free; ++i) {
1284 /* Bypass com_addop_varname because it will generate
1285 LOAD_DEREF but LOAD_CLOSURE is needed.
1286 */
1287 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1288 int arg, reftype;
1289
1290 /* Special case: If a class contains a method with a
1291 free variable that has the same name as a method,
1292 the name will be considered free *and* local in the
1293 class. It should be handled by the closure, as
1294 well as by the normal name loookup logic.
1295 */
1296 reftype = get_ref_type(c, name);
1297 if (reftype == CELL)
1298 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1299 else /* (reftype == FREE) */
1300 arg = compiler_lookup_arg(c->u->u_freevars, name);
1301 if (arg == -1) {
1302 printf("lookup %s in %s %d %d\n"
1303 "freevars of %s: %s\n",
1304 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001305 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001306 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001307 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308 PyObject_REPR(co->co_freevars));
1309 Py_FatalError("compiler_make_closure()");
1310 }
1311 ADDOP_I(c, LOAD_CLOSURE, arg);
1312 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001313 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001314 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001315 ADDOP_I(c, MAKE_CLOSURE, args);
1316 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
1319static int
1320compiler_decorators(struct compiler *c, asdl_seq* decos)
1321{
1322 int i;
1323
1324 if (!decos)
1325 return 1;
1326
1327 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001328 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329 }
1330 return 1;
1331}
1332
1333static int
1334compiler_arguments(struct compiler *c, arguments_ty args)
1335{
1336 int i;
1337 int n = asdl_seq_LEN(args->args);
1338 /* Correctly handle nested argument lists */
1339 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001340 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001342 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 if (id == NULL) {
1344 return 0;
1345 }
1346 if (!compiler_nameop(c, id, Load)) {
1347 Py_DECREF(id);
1348 return 0;
1349 }
1350 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001351 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001352 }
1353 }
1354 return 1;
1355}
1356
1357static int
1358compiler_function(struct compiler *c, stmt_ty s)
1359{
1360 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001361 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001363 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001364 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 int i, n, docstring;
1366
1367 assert(s->kind == FunctionDef_kind);
1368
1369 if (!compiler_decorators(c, decos))
1370 return 0;
1371 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001372 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1374 s->lineno))
1375 return 0;
1376
Anthony Baxter7b782b62006-04-11 12:01:56 +00001377 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001378 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001379 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001380 first_const = st->v.Expr.value->v.Str.s;
1381 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001382 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001383 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001384 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001386 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387 compiler_arguments(c, args);
1388
1389 c->u->u_argcount = asdl_seq_LEN(args->args);
1390 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001391 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001392 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001393 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1394 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 }
1396 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001397 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 if (co == NULL)
1399 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001401 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001402 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403
1404 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1405 ADDOP_I(c, CALL_FUNCTION, 1);
1406 }
1407
1408 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1409}
1410
1411static int
1412compiler_class(struct compiler *c, stmt_ty s)
1413{
Christian Heimes5224d282008-02-23 15:01:05 +00001414 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001416 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001417 asdl_seq* decos = s->v.ClassDef.decorator_list;
1418
1419 if (!compiler_decorators(c, decos))
1420 return 0;
1421
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422 /* push class name on stack, needed by BUILD_CLASS */
1423 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1424 /* push the tuple of base classes on the stack */
1425 n = asdl_seq_LEN(s->v.ClassDef.bases);
1426 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001427 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428 ADDOP_I(c, BUILD_TUPLE, n);
1429 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1430 s->lineno))
1431 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001432 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001433 c->u->u_private = s->v.ClassDef.name;
1434 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001435 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436 if (!str || !compiler_nameop(c, str, Load)) {
1437 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001438 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001440 }
1441
1442 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001443 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444 if (!str || !compiler_nameop(c, str, Store)) {
1445 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001446 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001448 }
1449 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001451 if (!compiler_body(c, s->v.ClassDef.body)) {
1452 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001454 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001456 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1457 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001459 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 if (co == NULL)
1461 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001462
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001463 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001464 Py_DECREF(co);
1465
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 ADDOP_I(c, CALL_FUNCTION, 0);
1467 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001468 /* apply decorators */
1469 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1470 ADDOP_I(c, CALL_FUNCTION, 1);
1471 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1473 return 0;
1474 return 1;
1475}
1476
1477static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001478compiler_ifexp(struct compiler *c, expr_ty e)
1479{
1480 basicblock *end, *next;
1481
1482 assert(e->kind == IfExp_kind);
1483 end = compiler_new_block(c);
1484 if (end == NULL)
1485 return 0;
1486 next = compiler_new_block(c);
1487 if (next == NULL)
1488 return 0;
1489 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001490 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001491 VISIT(c, expr, e->v.IfExp.body);
1492 ADDOP_JREL(c, JUMP_FORWARD, end);
1493 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001494 VISIT(c, expr, e->v.IfExp.orelse);
1495 compiler_use_next_block(c, end);
1496 return 1;
1497}
1498
1499static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500compiler_lambda(struct compiler *c, expr_ty e)
1501{
1502 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001503 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504 arguments_ty args = e->v.Lambda.args;
1505 assert(e->kind == Lambda_kind);
1506
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001507 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001508 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001509 if (!name)
1510 return 0;
1511 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512
1513 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001514 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1516 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001517
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001518 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 compiler_arguments(c, args);
1520
1521 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001522 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson8d5934b2008-12-27 18:24:11 +00001523 if (c->u->u_ste->ste_generator) {
1524 ADDOP_IN_SCOPE(c, POP_TOP);
1525 }
1526 else {
1527 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1528 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001530 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531 if (co == NULL)
1532 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001534 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001535 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536
1537 return 1;
1538}
1539
1540static int
1541compiler_print(struct compiler *c, stmt_ty s)
1542{
1543 int i, n;
1544 bool dest;
1545
1546 assert(s->kind == Print_kind);
1547 n = asdl_seq_LEN(s->v.Print.values);
1548 dest = false;
1549 if (s->v.Print.dest) {
1550 VISIT(c, expr, s->v.Print.dest);
1551 dest = true;
1552 }
1553 for (i = 0; i < n; i++) {
1554 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1555 if (dest) {
1556 ADDOP(c, DUP_TOP);
1557 VISIT(c, expr, e);
1558 ADDOP(c, ROT_TWO);
1559 ADDOP(c, PRINT_ITEM_TO);
1560 }
1561 else {
1562 VISIT(c, expr, e);
1563 ADDOP(c, PRINT_ITEM);
1564 }
1565 }
1566 if (s->v.Print.nl) {
1567 if (dest)
1568 ADDOP(c, PRINT_NEWLINE_TO)
1569 else
1570 ADDOP(c, PRINT_NEWLINE)
1571 }
1572 else if (dest)
1573 ADDOP(c, POP_TOP);
1574 return 1;
1575}
1576
1577static int
1578compiler_if(struct compiler *c, stmt_ty s)
1579{
1580 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001581 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001582 assert(s->kind == If_kind);
1583 end = compiler_new_block(c);
1584 if (end == NULL)
1585 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001586
1587 constant = expr_constant(s->v.If.test);
1588 /* constant = 0: "if 0"
1589 * constant = 1: "if 1", "if 2", ...
1590 * constant = -1: rest */
1591 if (constant == 0) {
1592 if (s->v.If.orelse)
1593 VISIT_SEQ(c, stmt, s->v.If.orelse);
1594 } else if (constant == 1) {
1595 VISIT_SEQ(c, stmt, s->v.If.body);
1596 } else {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001597 if (s->v.If.orelse) {
1598 next = compiler_new_block(c);
1599 if (next == NULL)
1600 return 0;
1601 }
1602 else
1603 next = end;
Georg Brandlddbaa662006-06-04 21:56:52 +00001604 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001605 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Georg Brandlddbaa662006-06-04 21:56:52 +00001606 VISIT_SEQ(c, stmt, s->v.If.body);
1607 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001608 if (s->v.If.orelse) {
1609 compiler_use_next_block(c, next);
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001610 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001611 }
Georg Brandlddbaa662006-06-04 21:56:52 +00001612 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613 compiler_use_next_block(c, end);
1614 return 1;
1615}
1616
1617static int
1618compiler_for(struct compiler *c, stmt_ty s)
1619{
1620 basicblock *start, *cleanup, *end;
1621
1622 start = compiler_new_block(c);
1623 cleanup = compiler_new_block(c);
1624 end = compiler_new_block(c);
1625 if (start == NULL || end == NULL || cleanup == NULL)
1626 return 0;
1627 ADDOP_JREL(c, SETUP_LOOP, end);
1628 if (!compiler_push_fblock(c, LOOP, start))
1629 return 0;
1630 VISIT(c, expr, s->v.For.iter);
1631 ADDOP(c, GET_ITER);
1632 compiler_use_next_block(c, start);
1633 ADDOP_JREL(c, FOR_ITER, cleanup);
1634 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001635 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1637 compiler_use_next_block(c, cleanup);
1638 ADDOP(c, POP_BLOCK);
1639 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001640 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 compiler_use_next_block(c, end);
1642 return 1;
1643}
1644
1645static int
1646compiler_while(struct compiler *c, stmt_ty s)
1647{
1648 basicblock *loop, *orelse, *end, *anchor = NULL;
1649 int constant = expr_constant(s->v.While.test);
1650
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001651 if (constant == 0) {
1652 if (s->v.While.orelse)
1653 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 loop = compiler_new_block(c);
1657 end = compiler_new_block(c);
1658 if (constant == -1) {
1659 anchor = compiler_new_block(c);
1660 if (anchor == NULL)
1661 return 0;
1662 }
1663 if (loop == NULL || end == NULL)
1664 return 0;
1665 if (s->v.While.orelse) {
1666 orelse = compiler_new_block(c);
1667 if (orelse == NULL)
1668 return 0;
1669 }
1670 else
1671 orelse = NULL;
1672
1673 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001674 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 if (!compiler_push_fblock(c, LOOP, loop))
1676 return 0;
1677 if (constant == -1) {
1678 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001679 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001681 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1683
1684 /* XXX should the two POP instructions be in a separate block
1685 if there is no else clause ?
1686 */
1687
1688 if (constant == -1) {
1689 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 ADDOP(c, POP_BLOCK);
1691 }
1692 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001693 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001694 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695 compiler_use_next_block(c, end);
1696
1697 return 1;
1698}
1699
1700static int
1701compiler_continue(struct compiler *c)
1702{
1703 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001704 static const char IN_FINALLY_ERROR_MSG[] =
1705 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 int i;
1707
1708 if (!c->u->u_nfblocks)
1709 return compiler_error(c, LOOP_ERROR_MSG);
1710 i = c->u->u_nfblocks - 1;
1711 switch (c->u->u_fblock[i].fb_type) {
1712 case LOOP:
1713 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1714 break;
1715 case EXCEPT:
1716 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001717 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1718 /* Prevent continue anywhere under a finally
1719 even if hidden in a sub-try or except. */
1720 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1721 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1722 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723 if (i == -1)
1724 return compiler_error(c, LOOP_ERROR_MSG);
1725 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1726 break;
1727 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001728 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001729 }
1730
1731 return 1;
1732}
1733
1734/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1735
1736 SETUP_FINALLY L
1737 <code for body>
1738 POP_BLOCK
1739 LOAD_CONST <None>
1740 L: <code for finalbody>
1741 END_FINALLY
1742
1743 The special instructions use the block stack. Each block
1744 stack entry contains the instruction that created it (here
1745 SETUP_FINALLY), the level of the value stack at the time the
1746 block stack entry was created, and a label (here L).
1747
1748 SETUP_FINALLY:
1749 Pushes the current value stack level and the label
1750 onto the block stack.
1751 POP_BLOCK:
1752 Pops en entry from the block stack, and pops the value
1753 stack until its level is the same as indicated on the
1754 block stack. (The label is ignored.)
1755 END_FINALLY:
1756 Pops a variable number of entries from the *value* stack
1757 and re-raises the exception they specify. The number of
1758 entries popped depends on the (pseudo) exception type.
1759
1760 The block stack is unwound when an exception is raised:
1761 when a SETUP_FINALLY entry is found, the exception is pushed
1762 onto the value stack (and the exception condition is cleared),
1763 and the interpreter jumps to the label gotten from the block
1764 stack.
1765*/
1766
1767static int
1768compiler_try_finally(struct compiler *c, stmt_ty s)
1769{
1770 basicblock *body, *end;
1771 body = compiler_new_block(c);
1772 end = compiler_new_block(c);
1773 if (body == NULL || end == NULL)
1774 return 0;
1775
1776 ADDOP_JREL(c, SETUP_FINALLY, end);
1777 compiler_use_next_block(c, body);
1778 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1779 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001780 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781 ADDOP(c, POP_BLOCK);
1782 compiler_pop_fblock(c, FINALLY_TRY, body);
1783
1784 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1785 compiler_use_next_block(c, end);
1786 if (!compiler_push_fblock(c, FINALLY_END, end))
1787 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001788 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 ADDOP(c, END_FINALLY);
1790 compiler_pop_fblock(c, FINALLY_END, end);
1791
1792 return 1;
1793}
1794
1795/*
1796 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1797 (The contents of the value stack is shown in [], with the top
1798 at the right; 'tb' is trace-back info, 'val' the exception's
1799 associated value, and 'exc' the exception.)
1800
1801 Value stack Label Instruction Argument
1802 [] SETUP_EXCEPT L1
1803 [] <code for S>
1804 [] POP_BLOCK
1805 [] JUMP_FORWARD L0
1806
1807 [tb, val, exc] L1: DUP )
1808 [tb, val, exc, exc] <evaluate E1> )
1809 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001810 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 [tb, val, exc] POP
1812 [tb, val] <assign to V1> (or POP if no V1)
1813 [tb] POP
1814 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001815 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001817 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 .............................etc.......................
1819
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001820 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821
1822 [] L0: <next statement>
1823
1824 Of course, parts are not generated if Vi or Ei is not present.
1825*/
1826static int
1827compiler_try_except(struct compiler *c, stmt_ty s)
1828{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001829 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 int i, n;
1831
1832 body = compiler_new_block(c);
1833 except = compiler_new_block(c);
1834 orelse = compiler_new_block(c);
1835 end = compiler_new_block(c);
1836 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1837 return 0;
1838 ADDOP_JREL(c, SETUP_EXCEPT, except);
1839 compiler_use_next_block(c, body);
1840 if (!compiler_push_fblock(c, EXCEPT, body))
1841 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001842 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 ADDOP(c, POP_BLOCK);
1844 compiler_pop_fblock(c, EXCEPT, body);
1845 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1846 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1847 compiler_use_next_block(c, except);
1848 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001849 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001851 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001853 c->u->u_lineno_set = false;
1854 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 except = compiler_new_block(c);
1856 if (except == NULL)
1857 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001858 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001859 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001860 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001862 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 }
1864 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001865 if (handler->v.ExceptHandler.name) {
1866 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 }
1868 else {
1869 ADDOP(c, POP_TOP);
1870 }
1871 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001872 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 ADDOP_JREL(c, JUMP_FORWARD, end);
1874 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 }
1876 ADDOP(c, END_FINALLY);
1877 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001878 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001879 compiler_use_next_block(c, end);
1880 return 1;
1881}
1882
1883static int
1884compiler_import_as(struct compiler *c, identifier name, identifier asname)
1885{
1886 /* The IMPORT_NAME opcode was already generated. This function
1887 merely needs to bind the result to a name.
1888
1889 If there is a dot in name, we need to split it and emit a
1890 LOAD_ATTR for each name.
1891 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001892 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 const char *dot = strchr(src, '.');
1894 if (dot) {
1895 /* Consume the base module name to get the first attribute */
1896 src = dot + 1;
1897 while (dot) {
1898 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001899 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001901 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001903 if (!attr)
1904 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001906 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907 src = dot + 1;
1908 }
1909 }
1910 return compiler_nameop(c, asname, Store);
1911}
1912
1913static int
1914compiler_import(struct compiler *c, stmt_ty s)
1915{
1916 /* The Import node stores a module name like a.b.c as a single
1917 string. This is convenient for all cases except
1918 import a.b.c as d
1919 where we need to parse that string to extract the individual
1920 module names.
1921 XXX Perhaps change the representation to make this case simpler?
1922 */
1923 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001924
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001926 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001927 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001928 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929
Neal Norwitzcbce2802006-04-03 06:26:32 +00001930 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001931 level = PyInt_FromLong(0);
1932 else
1933 level = PyInt_FromLong(-1);
1934
1935 if (level == NULL)
1936 return 0;
1937
1938 ADDOP_O(c, LOAD_CONST, level, consts);
1939 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1941 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1942
1943 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001944 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001945 if (!r)
1946 return r;
1947 }
1948 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001950 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 char *dot = strchr(base, '.');
1952 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001953 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 dot - base);
1955 r = compiler_nameop(c, tmp, Store);
1956 if (dot) {
1957 Py_DECREF(tmp);
1958 }
1959 if (!r)
1960 return r;
1961 }
1962 }
1963 return 1;
1964}
1965
1966static int
1967compiler_from_import(struct compiler *c, stmt_ty s)
1968{
1969 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970
1971 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001972 PyObject *level;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00001973 static PyObject *empty_string;
1974
1975 if (!empty_string) {
1976 empty_string = PyString_FromString("");
1977 if (!empty_string)
1978 return 0;
1979 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001981 if (!names)
1982 return 0;
1983
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001984 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001985 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001986 level = PyInt_FromLong(-1);
1987 else
1988 level = PyInt_FromLong(s->v.ImportFrom.level);
1989
1990 if (!level) {
1991 Py_DECREF(names);
1992 return 0;
1993 }
1994
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 /* build up the names */
1996 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001997 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 Py_INCREF(alias->name);
1999 PyTuple_SET_ITEM(names, i, alias->name);
2000 }
2001
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002002 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2003 !strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {
2004 Py_DECREF(level);
2005 Py_DECREF(names);
2006 return compiler_error(c, "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002007 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 }
2009
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002010 ADDOP_O(c, LOAD_CONST, level, consts);
2011 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002013 Py_DECREF(names);
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002014 if (s->v.ImportFrom.module) {
2015 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2016 }
2017 else {
2018 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2019 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002021 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002022 identifier store_name;
2023
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002024 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 assert(n == 1);
2026 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002027 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 }
2029
2030 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2031 store_name = alias->name;
2032 if (alias->asname)
2033 store_name = alias->asname;
2034
2035 if (!compiler_nameop(c, store_name, Store)) {
2036 Py_DECREF(names);
2037 return 0;
2038 }
2039 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002040 /* remove imported module */
2041 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002042 return 1;
2043}
2044
2045static int
2046compiler_assert(struct compiler *c, stmt_ty s)
2047{
2048 static PyObject *assertion_error = NULL;
2049 basicblock *end;
2050
2051 if (Py_OptimizeFlag)
2052 return 1;
2053 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002054 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 if (assertion_error == NULL)
2056 return 0;
2057 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002058 if (s->v.Assert.test->kind == Tuple_kind &&
2059 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2060 const char* msg =
2061 "assertion is always true, perhaps remove parentheses?";
2062 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2063 c->u->u_lineno, NULL, NULL) == -1)
2064 return 0;
2065 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002066 VISIT(c, expr, s->v.Assert.test);
2067 end = compiler_new_block(c);
2068 if (end == NULL)
2069 return 0;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002070 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2072 if (s->v.Assert.msg) {
2073 VISIT(c, expr, s->v.Assert.msg);
2074 ADDOP_I(c, RAISE_VARARGS, 2);
2075 }
2076 else {
2077 ADDOP_I(c, RAISE_VARARGS, 1);
2078 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002079 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 return 1;
2081}
2082
2083static int
2084compiler_visit_stmt(struct compiler *c, stmt_ty s)
2085{
2086 int i, n;
2087
Neal Norwitzf733a012006-10-29 18:30:10 +00002088 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 c->u->u_lineno = s->lineno;
2090 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002093 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002095 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002096 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002097 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 if (c->u->u_ste->ste_type != FunctionBlock)
2099 return compiler_error(c, "'return' outside function");
2100 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 VISIT(c, expr, s->v.Return.value);
2102 }
2103 else
2104 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2105 ADDOP(c, RETURN_VALUE);
2106 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002107 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002108 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002110 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111 n = asdl_seq_LEN(s->v.Assign.targets);
2112 VISIT(c, expr, s->v.Assign.value);
2113 for (i = 0; i < n; i++) {
2114 if (i < n - 1)
2115 ADDOP(c, DUP_TOP);
2116 VISIT(c, expr,
2117 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2118 }
2119 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002120 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002122 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002124 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002126 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002128 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002130 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 n = 0;
2132 if (s->v.Raise.type) {
2133 VISIT(c, expr, s->v.Raise.type);
2134 n++;
2135 if (s->v.Raise.inst) {
2136 VISIT(c, expr, s->v.Raise.inst);
2137 n++;
2138 if (s->v.Raise.tback) {
2139 VISIT(c, expr, s->v.Raise.tback);
2140 n++;
2141 }
2142 }
2143 }
2144 ADDOP_I(c, RAISE_VARARGS, n);
2145 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002146 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002148 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002150 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002152 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002154 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002156 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 VISIT(c, expr, s->v.Exec.body);
2158 if (s->v.Exec.globals) {
2159 VISIT(c, expr, s->v.Exec.globals);
2160 if (s->v.Exec.locals) {
2161 VISIT(c, expr, s->v.Exec.locals);
2162 } else {
2163 ADDOP(c, DUP_TOP);
2164 }
2165 } else {
2166 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2167 ADDOP(c, DUP_TOP);
2168 }
2169 ADDOP(c, EXEC_STMT);
2170 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002171 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002173 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002175 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 ADDOP(c, PRINT_EXPR);
2177 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002178 else if (s->v.Expr.value->kind != Str_kind &&
2179 s->v.Expr.value->kind != Num_kind) {
2180 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 ADDOP(c, POP_TOP);
2182 }
2183 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002184 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002186 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002187 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 return compiler_error(c, "'break' outside loop");
2189 ADDOP(c, BREAK_LOOP);
2190 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002191 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002193 case With_kind:
2194 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 }
2196 return 1;
2197}
2198
2199static int
2200unaryop(unaryop_ty op)
2201{
2202 switch (op) {
2203 case Invert:
2204 return UNARY_INVERT;
2205 case Not:
2206 return UNARY_NOT;
2207 case UAdd:
2208 return UNARY_POSITIVE;
2209 case USub:
2210 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002211 default:
2212 PyErr_Format(PyExc_SystemError,
2213 "unary op %d should not be possible", op);
2214 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216}
2217
2218static int
2219binop(struct compiler *c, operator_ty op)
2220{
2221 switch (op) {
2222 case Add:
2223 return BINARY_ADD;
2224 case Sub:
2225 return BINARY_SUBTRACT;
2226 case Mult:
2227 return BINARY_MULTIPLY;
2228 case Div:
2229 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2230 return BINARY_TRUE_DIVIDE;
2231 else
2232 return BINARY_DIVIDE;
2233 case Mod:
2234 return BINARY_MODULO;
2235 case Pow:
2236 return BINARY_POWER;
2237 case LShift:
2238 return BINARY_LSHIFT;
2239 case RShift:
2240 return BINARY_RSHIFT;
2241 case BitOr:
2242 return BINARY_OR;
2243 case BitXor:
2244 return BINARY_XOR;
2245 case BitAnd:
2246 return BINARY_AND;
2247 case FloorDiv:
2248 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002249 default:
2250 PyErr_Format(PyExc_SystemError,
2251 "binary op %d should not be possible", op);
2252 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254}
2255
2256static int
2257cmpop(cmpop_ty op)
2258{
2259 switch (op) {
2260 case Eq:
2261 return PyCmp_EQ;
2262 case NotEq:
2263 return PyCmp_NE;
2264 case Lt:
2265 return PyCmp_LT;
2266 case LtE:
2267 return PyCmp_LE;
2268 case Gt:
2269 return PyCmp_GT;
2270 case GtE:
2271 return PyCmp_GE;
2272 case Is:
2273 return PyCmp_IS;
2274 case IsNot:
2275 return PyCmp_IS_NOT;
2276 case In:
2277 return PyCmp_IN;
2278 case NotIn:
2279 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002280 default:
2281 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283}
2284
2285static int
2286inplace_binop(struct compiler *c, operator_ty op)
2287{
2288 switch (op) {
2289 case Add:
2290 return INPLACE_ADD;
2291 case Sub:
2292 return INPLACE_SUBTRACT;
2293 case Mult:
2294 return INPLACE_MULTIPLY;
2295 case Div:
2296 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2297 return INPLACE_TRUE_DIVIDE;
2298 else
2299 return INPLACE_DIVIDE;
2300 case Mod:
2301 return INPLACE_MODULO;
2302 case Pow:
2303 return INPLACE_POWER;
2304 case LShift:
2305 return INPLACE_LSHIFT;
2306 case RShift:
2307 return INPLACE_RSHIFT;
2308 case BitOr:
2309 return INPLACE_OR;
2310 case BitXor:
2311 return INPLACE_XOR;
2312 case BitAnd:
2313 return INPLACE_AND;
2314 case FloorDiv:
2315 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002316 default:
2317 PyErr_Format(PyExc_SystemError,
2318 "inplace binary op %d should not be possible", op);
2319 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002321}
2322
2323static int
2324compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2325{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002326 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2328
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002329 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002330 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 /* XXX AugStore isn't used anywhere! */
2332
Neal Norwitz0031ff32008-02-25 01:45:37 +00002333 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002334 if (!mangled)
2335 return 0;
2336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002337 op = 0;
2338 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002339 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 switch (scope) {
2341 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002342 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 optype = OP_DEREF;
2344 break;
2345 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002346 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 optype = OP_DEREF;
2348 break;
2349 case LOCAL:
2350 if (c->u->u_ste->ste_type == FunctionBlock)
2351 optype = OP_FAST;
2352 break;
2353 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002354 if (c->u->u_ste->ste_type == FunctionBlock &&
2355 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 optype = OP_GLOBAL;
2357 break;
2358 case GLOBAL_EXPLICIT:
2359 optype = OP_GLOBAL;
2360 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002361 default:
2362 /* scope can be 0 */
2363 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 }
2365
2366 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002367 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002368
2369 switch (optype) {
2370 case OP_DEREF:
2371 switch (ctx) {
2372 case Load: op = LOAD_DEREF; break;
2373 case Store: op = STORE_DEREF; break;
2374 case AugLoad:
2375 case AugStore:
2376 break;
2377 case Del:
2378 PyErr_Format(PyExc_SyntaxError,
2379 "can not delete variable '%s' referenced "
2380 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002381 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002382 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002383 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002385 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002386 PyErr_SetString(PyExc_SystemError,
2387 "param invalid for deref variable");
2388 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 }
2390 break;
2391 case OP_FAST:
2392 switch (ctx) {
2393 case Load: op = LOAD_FAST; break;
2394 case Store: op = STORE_FAST; break;
2395 case Del: op = DELETE_FAST; break;
2396 case AugLoad:
2397 case AugStore:
2398 break;
2399 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002400 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002401 PyErr_SetString(PyExc_SystemError,
2402 "param invalid for local variable");
2403 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002404 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002405 ADDOP_O(c, op, mangled, varnames);
2406 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 return 1;
2408 case OP_GLOBAL:
2409 switch (ctx) {
2410 case Load: op = LOAD_GLOBAL; break;
2411 case Store: op = STORE_GLOBAL; break;
2412 case Del: op = DELETE_GLOBAL; break;
2413 case AugLoad:
2414 case AugStore:
2415 break;
2416 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002417 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002418 PyErr_SetString(PyExc_SystemError,
2419 "param invalid for global variable");
2420 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002421 }
2422 break;
2423 case OP_NAME:
2424 switch (ctx) {
2425 case Load: op = LOAD_NAME; break;
2426 case Store: op = STORE_NAME; break;
2427 case Del: op = DELETE_NAME; break;
2428 case AugLoad:
2429 case AugStore:
2430 break;
2431 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002432 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002433 PyErr_SetString(PyExc_SystemError,
2434 "param invalid for name variable");
2435 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 }
2437 break;
2438 }
2439
2440 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002441 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002442 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002443 if (arg < 0)
2444 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002445 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446}
2447
2448static int
2449compiler_boolop(struct compiler *c, expr_ty e)
2450{
2451 basicblock *end;
2452 int jumpi, i, n;
2453 asdl_seq *s;
2454
2455 assert(e->kind == BoolOp_kind);
2456 if (e->v.BoolOp.op == And)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002457 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458 else
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002459 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002461 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 return 0;
2463 s = e->v.BoolOp.values;
2464 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002465 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002467 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002468 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002470 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 compiler_use_next_block(c, end);
2472 return 1;
2473}
2474
2475static int
2476compiler_list(struct compiler *c, expr_ty e)
2477{
2478 int n = asdl_seq_LEN(e->v.List.elts);
2479 if (e->v.List.ctx == Store) {
2480 ADDOP_I(c, UNPACK_SEQUENCE, n);
2481 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002482 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002483 if (e->v.List.ctx == Load) {
2484 ADDOP_I(c, BUILD_LIST, n);
2485 }
2486 return 1;
2487}
2488
2489static int
2490compiler_tuple(struct compiler *c, expr_ty e)
2491{
2492 int n = asdl_seq_LEN(e->v.Tuple.elts);
2493 if (e->v.Tuple.ctx == Store) {
2494 ADDOP_I(c, UNPACK_SEQUENCE, n);
2495 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002496 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002497 if (e->v.Tuple.ctx == Load) {
2498 ADDOP_I(c, BUILD_TUPLE, n);
2499 }
2500 return 1;
2501}
2502
2503static int
2504compiler_compare(struct compiler *c, expr_ty e)
2505{
2506 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002507 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508
2509 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2510 VISIT(c, expr, e->v.Compare.left);
2511 n = asdl_seq_LEN(e->v.Compare.ops);
2512 assert(n > 0);
2513 if (n > 1) {
2514 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002515 if (cleanup == NULL)
2516 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002517 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002518 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519 }
2520 for (i = 1; i < n; i++) {
2521 ADDOP(c, DUP_TOP);
2522 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002524 cmpop((cmpop_ty)(asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002525 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002526 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002529 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002530 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002532 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002534 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 if (n > 1) {
2536 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002537 if (end == NULL)
2538 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 ADDOP_JREL(c, JUMP_FORWARD, end);
2540 compiler_use_next_block(c, cleanup);
2541 ADDOP(c, ROT_TWO);
2542 ADDOP(c, POP_TOP);
2543 compiler_use_next_block(c, end);
2544 }
2545 return 1;
2546}
2547
2548static int
2549compiler_call(struct compiler *c, expr_ty e)
2550{
2551 int n, code = 0;
2552
2553 VISIT(c, expr, e->v.Call.func);
2554 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002555 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002557 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2559 }
2560 if (e->v.Call.starargs) {
2561 VISIT(c, expr, e->v.Call.starargs);
2562 code |= 1;
2563 }
2564 if (e->v.Call.kwargs) {
2565 VISIT(c, expr, e->v.Call.kwargs);
2566 code |= 2;
2567 }
2568 switch (code) {
2569 case 0:
2570 ADDOP_I(c, CALL_FUNCTION, n);
2571 break;
2572 case 1:
2573 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2574 break;
2575 case 2:
2576 ADDOP_I(c, CALL_FUNCTION_KW, n);
2577 break;
2578 case 3:
2579 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2580 break;
2581 }
2582 return 1;
2583}
2584
2585static int
Antoine Pitroud0c35152008-12-17 00:38:28 +00002586compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
2587 int gen_index, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588{
2589 /* generate code for the iterator, then each of the ifs,
2590 and then write to the element */
2591
2592 comprehension_ty l;
2593 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002594 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595
2596 start = compiler_new_block(c);
2597 skip = compiler_new_block(c);
2598 if_cleanup = compiler_new_block(c);
2599 anchor = compiler_new_block(c);
2600
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002601 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2602 anchor == NULL)
2603 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604
Anthony Baxter7b782b62006-04-11 12:01:56 +00002605 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606 VISIT(c, expr, l->iter);
2607 ADDOP(c, GET_ITER);
2608 compiler_use_next_block(c, start);
2609 ADDOP_JREL(c, FOR_ITER, anchor);
2610 NEXT_BLOCK(c);
2611 VISIT(c, expr, l->target);
2612
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002613 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 n = asdl_seq_LEN(l->ifs);
2615 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002616 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002618 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 }
2621
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002622 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitroud0c35152008-12-17 00:38:28 +00002623 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002626 /* only append after the last for generator */
2627 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002628 VISIT(c, expr, elt);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002629 ADDOP_I(c, LIST_APPEND, gen_index+1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002630
2631 compiler_use_next_block(c, skip);
2632 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002633 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002634 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2635 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636
2637 return 1;
2638}
2639
2640static int
2641compiler_listcomp(struct compiler *c, expr_ty e)
2642{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643 assert(e->kind == ListComp_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644 ADDOP_I(c, BUILD_LIST, 0);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002645 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2646 e->v.ListComp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647}
2648
2649static int
2650compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002651 asdl_seq *generators, int gen_index,
2652 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653{
2654 /* generate code for the iterator, then each of the ifs,
2655 and then write to the element */
2656
2657 comprehension_ty ge;
2658 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002659 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660
2661 start = compiler_new_block(c);
2662 skip = compiler_new_block(c);
2663 if_cleanup = compiler_new_block(c);
2664 anchor = compiler_new_block(c);
2665 end = compiler_new_block(c);
2666
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002667 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 anchor == NULL || end == NULL)
2669 return 0;
2670
Anthony Baxter7b782b62006-04-11 12:01:56 +00002671 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672 ADDOP_JREL(c, SETUP_LOOP, end);
2673 if (!compiler_push_fblock(c, LOOP, start))
2674 return 0;
2675
2676 if (gen_index == 0) {
2677 /* Receive outermost iter as an implicit argument */
2678 c->u->u_argcount = 1;
2679 ADDOP_I(c, LOAD_FAST, 0);
2680 }
2681 else {
2682 /* Sub-iter - calculate on the fly */
2683 VISIT(c, expr, ge->iter);
2684 ADDOP(c, GET_ITER);
2685 }
2686 compiler_use_next_block(c, start);
2687 ADDOP_JREL(c, FOR_ITER, anchor);
2688 NEXT_BLOCK(c);
2689 VISIT(c, expr, ge->target);
2690
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002691 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 n = asdl_seq_LEN(ge->ifs);
2693 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002694 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002696 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002697 NEXT_BLOCK(c);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002698 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002700 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2702 return 0;
2703
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002704 /* only append after the last 'for' generator */
2705 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 VISIT(c, expr, elt);
2707 ADDOP(c, YIELD_VALUE);
2708 ADDOP(c, POP_TOP);
2709
2710 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002711 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002712 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2714 compiler_use_next_block(c, anchor);
2715 ADDOP(c, POP_BLOCK);
2716 compiler_pop_fblock(c, LOOP, start);
2717 compiler_use_next_block(c, end);
2718
2719 return 1;
2720}
2721
2722static int
2723compiler_genexp(struct compiler *c, expr_ty e)
2724{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002725 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 PyCodeObject *co;
2727 expr_ty outermost_iter = ((comprehension_ty)
2728 (asdl_seq_GET(e->v.GeneratorExp.generators,
2729 0)))->iter;
2730
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002731 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002732 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002733 if (!name)
2734 return 0;
2735 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736
2737 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2738 return 0;
2739 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2740 e->v.GeneratorExp.elt);
2741 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002742 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 if (co == NULL)
2744 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002746 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002747 Py_DECREF(co);
2748
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 VISIT(c, expr, outermost_iter);
2750 ADDOP(c, GET_ITER);
2751 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752
2753 return 1;
2754}
2755
2756static int
2757compiler_visit_keyword(struct compiler *c, keyword_ty k)
2758{
2759 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2760 VISIT(c, expr, k->value);
2761 return 1;
2762}
2763
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002764/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 whether they are true or false.
2766
2767 Return values: 1 for true, 0 for false, -1 for non-constant.
2768 */
2769
2770static int
2771expr_constant(expr_ty e)
2772{
2773 switch (e->kind) {
2774 case Num_kind:
2775 return PyObject_IsTrue(e->v.Num.n);
2776 case Str_kind:
2777 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002778 case Name_kind:
2779 /* __debug__ is not assignable, so we can optimize
2780 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002781 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002782 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002783 return ! Py_OptimizeFlag;
2784 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785 default:
2786 return -1;
2787 }
2788}
2789
Guido van Rossumc2e20742006-02-27 22:32:47 +00002790/*
2791 Implements the with statement from PEP 343.
2792
2793 The semantics outlined in that PEP are as follows:
2794
2795 with EXPR as VAR:
2796 BLOCK
2797
2798 It is implemented roughly as:
2799
Guido van Rossumda5b7012006-05-02 19:47:52 +00002800 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002801 exit = context.__exit__ # not calling it
2802 value = context.__enter__()
2803 try:
2804 VAR = value # if VAR present in the syntax
2805 BLOCK
2806 finally:
2807 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002808 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002809 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002810 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002811 exit(*exc)
2812 */
2813static int
2814compiler_with(struct compiler *c, stmt_ty s)
2815{
Guido van Rossumc2e20742006-02-27 22:32:47 +00002816 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002817
2818 assert(s->kind == With_kind);
2819
Guido van Rossumc2e20742006-02-27 22:32:47 +00002820 block = compiler_new_block(c);
2821 finally = compiler_new_block(c);
2822 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002823 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002824
Guido van Rossumda5b7012006-05-02 19:47:52 +00002825 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002826 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002827 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002828
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002829 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002830 compiler_use_next_block(c, block);
2831 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002832 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002833 }
2834
2835 if (s->v.With.optional_vars) {
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002836 VISIT(c, expr, s->v.With.optional_vars);
2837 }
2838 else {
2839 /* Discard result from context.__enter__() */
2840 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002841 }
2842
2843 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002844 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002845
2846 /* End of try block; start the finally block */
2847 ADDOP(c, POP_BLOCK);
2848 compiler_pop_fblock(c, FINALLY_TRY, block);
2849
2850 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2851 compiler_use_next_block(c, finally);
2852 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002853 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002854
Nick Coghlan7af53be2008-03-07 14:13:28 +00002855 /* Finally block starts; context.__exit__ is on the stack under
2856 the exception or return information. Just issue our magic
2857 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002858 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002859
2860 /* Finally block ends. */
2861 ADDOP(c, END_FINALLY);
2862 compiler_pop_fblock(c, FINALLY_END, finally);
2863 return 1;
2864}
2865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866static int
2867compiler_visit_expr(struct compiler *c, expr_ty e)
2868{
2869 int i, n;
2870
Neal Norwitzf733a012006-10-29 18:30:10 +00002871 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002872 set a new line number for the next instruction.
2873 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874 if (e->lineno > c->u->u_lineno) {
2875 c->u->u_lineno = e->lineno;
2876 c->u->u_lineno_set = false;
2877 }
2878 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002879 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002881 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882 VISIT(c, expr, e->v.BinOp.left);
2883 VISIT(c, expr, e->v.BinOp.right);
2884 ADDOP(c, binop(c, e->v.BinOp.op));
2885 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002886 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887 VISIT(c, expr, e->v.UnaryOp.operand);
2888 ADDOP(c, unaryop(e->v.UnaryOp.op));
2889 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002890 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002892 case IfExp_kind:
2893 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002894 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00002896 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002897 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002898 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002899 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00002900 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002901 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00002902 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903 }
2904 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002905 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002907 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002908 return compiler_genexp(c, e);
2909 case Yield_kind:
2910 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002911 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002912 if (e->v.Yield.value) {
2913 VISIT(c, expr, e->v.Yield.value);
2914 }
2915 else {
2916 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2917 }
2918 ADDOP(c, YIELD_VALUE);
2919 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002920 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002922 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002924 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002925 VISIT(c, expr, e->v.Repr.value);
2926 ADDOP(c, UNARY_CONVERT);
2927 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002928 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002929 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
2930 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002931 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
2933 break;
2934 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002935 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936 if (e->v.Attribute.ctx != AugStore)
2937 VISIT(c, expr, e->v.Attribute.value);
2938 switch (e->v.Attribute.ctx) {
2939 case AugLoad:
2940 ADDOP(c, DUP_TOP);
2941 /* Fall through to load */
2942 case Load:
2943 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
2944 break;
2945 case AugStore:
2946 ADDOP(c, ROT_TWO);
2947 /* Fall through to save */
2948 case Store:
2949 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
2950 break;
2951 case Del:
2952 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
2953 break;
2954 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002955 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002956 PyErr_SetString(PyExc_SystemError,
2957 "param invalid in attribute expression");
2958 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002959 }
2960 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002961 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 switch (e->v.Subscript.ctx) {
2963 case AugLoad:
2964 VISIT(c, expr, e->v.Subscript.value);
2965 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
2966 break;
2967 case Load:
2968 VISIT(c, expr, e->v.Subscript.value);
2969 VISIT_SLICE(c, e->v.Subscript.slice, Load);
2970 break;
2971 case AugStore:
2972 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
2973 break;
2974 case Store:
2975 VISIT(c, expr, e->v.Subscript.value);
2976 VISIT_SLICE(c, e->v.Subscript.slice, Store);
2977 break;
2978 case Del:
2979 VISIT(c, expr, e->v.Subscript.value);
2980 VISIT_SLICE(c, e->v.Subscript.slice, Del);
2981 break;
2982 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002983 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002984 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002985 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00002986 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 }
2988 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002989 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
2991 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002992 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002994 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 return compiler_tuple(c, e);
2996 }
2997 return 1;
2998}
2999
3000static int
3001compiler_augassign(struct compiler *c, stmt_ty s)
3002{
3003 expr_ty e = s->v.AugAssign.target;
3004 expr_ty auge;
3005
3006 assert(s->kind == AugAssign_kind);
3007
3008 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003009 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003010 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003011 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003012 if (auge == NULL)
3013 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003014 VISIT(c, expr, auge);
3015 VISIT(c, expr, s->v.AugAssign.value);
3016 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3017 auge->v.Attribute.ctx = AugStore;
3018 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003019 break;
3020 case Subscript_kind:
3021 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003022 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003023 if (auge == NULL)
3024 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003025 VISIT(c, expr, auge);
3026 VISIT(c, expr, s->v.AugAssign.value);
3027 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003028 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003032 if (!compiler_nameop(c, e->v.Name.id, Load))
3033 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 VISIT(c, expr, s->v.AugAssign.value);
3035 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3036 return compiler_nameop(c, e->v.Name.id, Store);
3037 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003038 PyErr_Format(PyExc_SystemError,
3039 "invalid node type (%d) for augmented assignment",
3040 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003041 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042 }
3043 return 1;
3044}
3045
3046static int
3047compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3048{
3049 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003050 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3051 PyErr_SetString(PyExc_SystemError,
3052 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003054 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003055 f = &c->u->u_fblock[c->u->u_nfblocks++];
3056 f->fb_type = t;
3057 f->fb_block = b;
3058 return 1;
3059}
3060
3061static void
3062compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3063{
3064 struct compiler_unit *u = c->u;
3065 assert(u->u_nfblocks > 0);
3066 u->u_nfblocks--;
3067 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3068 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3069}
3070
Jeremy Hylton82271f12006-10-04 02:24:52 +00003071static int
3072compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003073 int i;
3074 struct compiler_unit *u = c->u;
3075 for (i = 0; i < u->u_nfblocks; ++i) {
3076 if (u->u_fblock[i].fb_type == LOOP)
3077 return 1;
3078 }
3079 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003080}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003081/* Raises a SyntaxError and returns 0.
3082 If something goes wrong, a different exception may be raised.
3083*/
3084
3085static int
3086compiler_error(struct compiler *c, const char *errstr)
3087{
3088 PyObject *loc;
3089 PyObject *u = NULL, *v = NULL;
3090
3091 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3092 if (!loc) {
3093 Py_INCREF(Py_None);
3094 loc = Py_None;
3095 }
3096 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3097 Py_None, loc);
3098 if (!u)
3099 goto exit;
3100 v = Py_BuildValue("(zO)", errstr, u);
3101 if (!v)
3102 goto exit;
3103 PyErr_SetObject(PyExc_SyntaxError, v);
3104 exit:
3105 Py_DECREF(loc);
3106 Py_XDECREF(u);
3107 Py_XDECREF(v);
3108 return 0;
3109}
3110
3111static int
3112compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003113 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003115 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003116
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003117 /* XXX this code is duplicated */
3118 switch (ctx) {
3119 case AugLoad: /* fall through to Load */
3120 case Load: op = BINARY_SUBSCR; break;
3121 case AugStore:/* fall through to Store */
3122 case Store: op = STORE_SUBSCR; break;
3123 case Del: op = DELETE_SUBSCR; break;
3124 case Param:
3125 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003126 "invalid %s kind %d in subscript\n",
3127 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003128 return 0;
3129 }
3130 if (ctx == AugLoad) {
3131 ADDOP_I(c, DUP_TOPX, 2);
3132 }
3133 else if (ctx == AugStore) {
3134 ADDOP(c, ROT_THREE);
3135 }
3136 ADDOP(c, op);
3137 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138}
3139
3140static int
3141compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3142{
3143 int n = 2;
3144 assert(s->kind == Slice_kind);
3145
3146 /* only handles the cases where BUILD_SLICE is emitted */
3147 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003148 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 }
3150 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003151 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003155 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
3157 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003158 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 }
3160
3161 if (s->v.Slice.step) {
3162 n++;
3163 VISIT(c, expr, s->v.Slice.step);
3164 }
3165 ADDOP_I(c, BUILD_SLICE, n);
3166 return 1;
3167}
3168
3169static int
3170compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3171{
3172 int op = 0, slice_offset = 0, stack_count = 0;
3173
3174 assert(s->v.Slice.step == NULL);
3175 if (s->v.Slice.lower) {
3176 slice_offset++;
3177 stack_count++;
3178 if (ctx != AugStore)
3179 VISIT(c, expr, s->v.Slice.lower);
3180 }
3181 if (s->v.Slice.upper) {
3182 slice_offset += 2;
3183 stack_count++;
3184 if (ctx != AugStore)
3185 VISIT(c, expr, s->v.Slice.upper);
3186 }
3187
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003188 if (ctx == AugLoad) {
3189 switch (stack_count) {
3190 case 0: ADDOP(c, DUP_TOP); break;
3191 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3192 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3193 }
3194 }
3195 else if (ctx == AugStore) {
3196 switch (stack_count) {
3197 case 0: ADDOP(c, ROT_TWO); break;
3198 case 1: ADDOP(c, ROT_THREE); break;
3199 case 2: ADDOP(c, ROT_FOUR); break;
3200 }
3201 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
3203 switch (ctx) {
3204 case AugLoad: /* fall through to Load */
3205 case Load: op = SLICE; break;
3206 case AugStore:/* fall through to Store */
3207 case Store: op = STORE_SLICE; break;
3208 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003209 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003210 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003211 PyErr_SetString(PyExc_SystemError,
3212 "param invalid in simple slice");
3213 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003214 }
3215
3216 ADDOP(c, op + slice_offset);
3217 return 1;
3218}
3219
3220static int
3221compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3222 expr_context_ty ctx)
3223{
3224 switch (s->kind) {
3225 case Ellipsis_kind:
3226 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3227 break;
3228 case Slice_kind:
3229 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230 case Index_kind:
3231 VISIT(c, expr, s->v.Index.value);
3232 break;
3233 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003234 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003235 PyErr_SetString(PyExc_SystemError,
3236 "extended slice invalid in nested slice");
3237 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003238 }
3239 return 1;
3240}
3241
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003242static int
3243compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3244{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003245 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003247 case Index_kind:
3248 kindname = "index";
3249 if (ctx != AugStore) {
3250 VISIT(c, expr, s->v.Index.value);
3251 }
3252 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003253 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003254 kindname = "ellipsis";
3255 if (ctx != AugStore) {
3256 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3257 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258 break;
3259 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003260 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 if (!s->v.Slice.step)
3262 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003263 if (ctx != AugStore) {
3264 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265 return 0;
3266 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003267 break;
3268 case ExtSlice_kind:
3269 kindname = "extended slice";
3270 if (ctx != AugStore) {
3271 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3272 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003273 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003274 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003275 if (!compiler_visit_nested_slice(c, sub, ctx))
3276 return 0;
3277 }
3278 ADDOP_I(c, BUILD_TUPLE, n);
3279 }
3280 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003281 default:
3282 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003283 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003284 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003286 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287}
3288
Neal Norwitzf733a012006-10-29 18:30:10 +00003289
3290/* End of the compiler section, beginning of the assembler section */
3291
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292/* do depth-first search of basic block graph, starting with block.
3293 post records the block indices in post-order.
3294
3295 XXX must handle implicit jumps from one block to next
3296*/
3297
Neal Norwitzf733a012006-10-29 18:30:10 +00003298struct assembler {
3299 PyObject *a_bytecode; /* string containing bytecode */
3300 int a_offset; /* offset into bytecode */
3301 int a_nblocks; /* number of reachable blocks */
3302 basicblock **a_postorder; /* list of blocks in dfs postorder */
3303 PyObject *a_lnotab; /* string containing lnotab */
3304 int a_lnotab_off; /* offset into lnotab */
3305 int a_lineno; /* last lineno of emitted instruction */
3306 int a_lineno_off; /* bytecode offset of last lineno */
3307};
3308
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309static void
3310dfs(struct compiler *c, basicblock *b, struct assembler *a)
3311{
3312 int i;
3313 struct instr *instr = NULL;
3314
3315 if (b->b_seen)
3316 return;
3317 b->b_seen = 1;
3318 if (b->b_next != NULL)
3319 dfs(c, b->b_next, a);
3320 for (i = 0; i < b->b_iused; i++) {
3321 instr = &b->b_instr[i];
3322 if (instr->i_jrel || instr->i_jabs)
3323 dfs(c, instr->i_target, a);
3324 }
3325 a->a_postorder[a->a_nblocks++] = b;
3326}
3327
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003328static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3330{
3331 int i;
3332 struct instr *instr;
3333 if (b->b_seen || b->b_startdepth >= depth)
3334 return maxdepth;
3335 b->b_seen = 1;
3336 b->b_startdepth = depth;
3337 for (i = 0; i < b->b_iused; i++) {
3338 instr = &b->b_instr[i];
3339 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3340 if (depth > maxdepth)
3341 maxdepth = depth;
3342 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3343 if (instr->i_jrel || instr->i_jabs) {
3344 maxdepth = stackdepth_walk(c, instr->i_target,
3345 depth, maxdepth);
3346 if (instr->i_opcode == JUMP_ABSOLUTE ||
3347 instr->i_opcode == JUMP_FORWARD) {
3348 goto out; /* remaining code is dead */
3349 }
3350 }
3351 }
3352 if (b->b_next)
3353 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3354out:
3355 b->b_seen = 0;
3356 return maxdepth;
3357}
3358
3359/* Find the flow path that needs the largest stack. We assume that
3360 * cycles in the flow graph have no net effect on the stack depth.
3361 */
3362static int
3363stackdepth(struct compiler *c)
3364{
3365 basicblock *b, *entryblock;
3366 entryblock = NULL;
3367 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3368 b->b_seen = 0;
3369 b->b_startdepth = INT_MIN;
3370 entryblock = b;
3371 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003372 if (!entryblock)
3373 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374 return stackdepth_walk(c, entryblock, 0, 0);
3375}
3376
3377static int
3378assemble_init(struct assembler *a, int nblocks, int firstlineno)
3379{
3380 memset(a, 0, sizeof(struct assembler));
3381 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003382 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 if (!a->a_bytecode)
3384 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003385 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 if (!a->a_lnotab)
3387 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003388 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3389 PyErr_NoMemory();
3390 return 0;
3391 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003392 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003393 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003394 if (!a->a_postorder) {
3395 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 return 1;
3399}
3400
3401static void
3402assemble_free(struct assembler *a)
3403{
3404 Py_XDECREF(a->a_bytecode);
3405 Py_XDECREF(a->a_lnotab);
3406 if (a->a_postorder)
3407 PyObject_Free(a->a_postorder);
3408}
3409
3410/* Return the size of a basic block in bytes. */
3411
3412static int
3413instrsize(struct instr *instr)
3414{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003415 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003416 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003417 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003418 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3419 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420}
3421
3422static int
3423blocksize(basicblock *b)
3424{
3425 int i;
3426 int size = 0;
3427
3428 for (i = 0; i < b->b_iused; i++)
3429 size += instrsize(&b->b_instr[i]);
3430 return size;
3431}
3432
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003433/* Appends a pair to the end of the line number table, a_lnotab, representing
3434 the instruction's bytecode offset and line number. See
3435 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003436
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003437static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003439{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 int d_bytecode, d_lineno;
3441 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003442 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443
3444 d_bytecode = a->a_offset - a->a_lineno_off;
3445 d_lineno = i->i_lineno - a->a_lineno;
3446
3447 assert(d_bytecode >= 0);
3448 assert(d_lineno >= 0);
3449
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003450 if(d_bytecode == 0 && d_lineno == 0)
3451 return 1;
3452
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003453 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003454 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003456 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003458 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003459 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003460 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003462 else {
3463 PyErr_NoMemory();
3464 return 0;
3465 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003466 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003468 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003469 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003470 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003471 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472 *lnotab++ = 255;
3473 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003474 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 d_bytecode -= ncodes * 255;
3476 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 assert(d_bytecode <= 255);
3479 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003480 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003481 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003482 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003484 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003486 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003488 else {
3489 PyErr_NoMemory();
3490 return 0;
3491 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003492 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003494 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003495 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003496 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003498 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003500 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003502 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 d_lineno -= ncodes * 255;
3505 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003506 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003507
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003508 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003510 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003511 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003512 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003513 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003514 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 a->a_lnotab_off += 2;
3517 if (d_bytecode) {
3518 *lnotab++ = d_bytecode;
3519 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003520 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003521 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522 *lnotab++ = 0;
3523 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 a->a_lineno = i->i_lineno;
3526 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003527 return 1;
3528}
3529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530/* assemble_emit()
3531 Extend the bytecode with a new instruction.
3532 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003533*/
3534
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003535static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003537{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003538 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003539 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 char *code;
3541
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003542 size = instrsize(i);
3543 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003544 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003545 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003548 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003549 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003550 if (len > PY_SSIZE_T_MAX / 2)
3551 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003552 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003553 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003554 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003555 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003557 if (size == 6) {
3558 assert(i->i_hasarg);
3559 *code++ = (char)EXTENDED_ARG;
3560 *code++ = ext & 0xff;
3561 *code++ = ext >> 8;
3562 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003563 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003565 if (i->i_hasarg) {
3566 assert(size == 3 || size == 6);
3567 *code++ = arg & 0xff;
3568 *code++ = arg >> 8;
3569 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003571}
3572
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003573static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003575{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003577 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003578 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 /* Compute the size of each block and fixup jump args.
3581 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003582start:
3583 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003585 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 bsize = blocksize(b);
3587 b->b_offset = totsize;
3588 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003589 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003590 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3592 bsize = b->b_offset;
3593 for (i = 0; i < b->b_iused; i++) {
3594 struct instr *instr = &b->b_instr[i];
3595 /* Relative jumps are computed relative to
3596 the instruction pointer after fetching
3597 the jump instruction.
3598 */
3599 bsize += instrsize(instr);
3600 if (instr->i_jabs)
3601 instr->i_oparg = instr->i_target->b_offset;
3602 else if (instr->i_jrel) {
3603 int delta = instr->i_target->b_offset - bsize;
3604 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003605 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003606 else
3607 continue;
3608 if (instr->i_oparg > 0xffff)
3609 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003610 }
3611 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003612
3613 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003614 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003615 with a better solution.
3616
3617 In the meantime, should the goto be dropped in favor
3618 of a loop?
3619
3620 The issue is that in the first loop blocksize() is called
3621 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003622 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003623 i_oparg is calculated in the second loop above.
3624
3625 So we loop until we stop seeing new EXTENDED_ARGs.
3626 The only EXTENDED_ARGs that could be popping up are
3627 ones in jump instructions. So this should converge
3628 fairly quickly.
3629 */
3630 if (last_extended_arg_count != extended_arg_count) {
3631 last_extended_arg_count = extended_arg_count;
3632 goto start;
3633 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003634}
3635
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003636static PyObject *
3637dict_keys_inorder(PyObject *dict, int offset)
3638{
3639 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003640 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003641
3642 tuple = PyTuple_New(size);
3643 if (tuple == NULL)
3644 return NULL;
3645 while (PyDict_Next(dict, &pos, &k, &v)) {
3646 i = PyInt_AS_LONG(v);
Benjamin Petersonda9327f2009-01-31 23:43:25 +00003647 /* The keys of the dictionary are tuples. (see compiler_add_o)
3648 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003649 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003650 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003651 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003652 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003653 PyTuple_SET_ITEM(tuple, i - offset, k);
3654 }
3655 return tuple;
3656}
3657
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003658static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003660{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661 PySTEntryObject *ste = c->u->u_ste;
3662 int flags = 0, n;
3663 if (ste->ste_type != ModuleBlock)
3664 flags |= CO_NEWLOCALS;
3665 if (ste->ste_type == FunctionBlock) {
3666 if (!ste->ste_unoptimized)
3667 flags |= CO_OPTIMIZED;
3668 if (ste->ste_nested)
3669 flags |= CO_NESTED;
3670 if (ste->ste_generator)
3671 flags |= CO_GENERATOR;
Benjamin Peterson12554cb2009-01-31 23:54:38 +00003672 if (ste->ste_varargs)
3673 flags |= CO_VARARGS;
3674 if (ste->ste_varkeywords)
3675 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003676 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003677
3678 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003679 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003680
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 n = PyDict_Size(c->u->u_freevars);
3682 if (n < 0)
3683 return -1;
3684 if (n == 0) {
3685 n = PyDict_Size(c->u->u_cellvars);
3686 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003687 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003688 if (n == 0) {
3689 flags |= CO_NOFREE;
3690 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003691 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003692
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003693 return flags;
3694}
3695
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696static PyCodeObject *
3697makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003698{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699 PyObject *tmp;
3700 PyCodeObject *co = NULL;
3701 PyObject *consts = NULL;
3702 PyObject *names = NULL;
3703 PyObject *varnames = NULL;
3704 PyObject *filename = NULL;
3705 PyObject *name = NULL;
3706 PyObject *freevars = NULL;
3707 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003708 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003710
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 tmp = dict_keys_inorder(c->u->u_consts, 0);
3712 if (!tmp)
3713 goto error;
3714 consts = PySequence_List(tmp); /* optimize_code requires a list */
3715 Py_DECREF(tmp);
3716
3717 names = dict_keys_inorder(c->u->u_names, 0);
3718 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3719 if (!consts || !names || !varnames)
3720 goto error;
3721
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003722 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3723 if (!cellvars)
3724 goto error;
3725 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3726 if (!freevars)
3727 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003728 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 if (!filename)
3730 goto error;
3731
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003732 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 flags = compute_code_flags(c);
3734 if (flags < 0)
3735 goto error;
3736
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003737 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 if (!bytecode)
3739 goto error;
3740
3741 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3742 if (!tmp)
3743 goto error;
3744 Py_DECREF(consts);
3745 consts = tmp;
3746
3747 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3748 bytecode, consts, names, varnames,
3749 freevars, cellvars,
3750 filename, c->u->u_name,
3751 c->u->u_firstlineno,
3752 a->a_lnotab);
3753 error:
3754 Py_XDECREF(consts);
3755 Py_XDECREF(names);
3756 Py_XDECREF(varnames);
3757 Py_XDECREF(filename);
3758 Py_XDECREF(name);
3759 Py_XDECREF(freevars);
3760 Py_XDECREF(cellvars);
3761 Py_XDECREF(bytecode);
3762 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003763}
3764
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003765
3766/* For debugging purposes only */
3767#if 0
3768static void
3769dump_instr(const struct instr *i)
3770{
3771 const char *jrel = i->i_jrel ? "jrel " : "";
3772 const char *jabs = i->i_jabs ? "jabs " : "";
3773 char arg[128];
3774
3775 *arg = '\0';
3776 if (i->i_hasarg)
3777 sprintf(arg, "arg: %d ", i->i_oparg);
3778
3779 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3780 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3781}
3782
3783static void
3784dump_basicblock(const basicblock *b)
3785{
3786 const char *seen = b->b_seen ? "seen " : "";
3787 const char *b_return = b->b_return ? "return " : "";
3788 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3789 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3790 if (b->b_instr) {
3791 int i;
3792 for (i = 0; i < b->b_iused; i++) {
3793 fprintf(stderr, " [%02d] ", i);
3794 dump_instr(b->b_instr + i);
3795 }
3796 }
3797}
3798#endif
3799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800static PyCodeObject *
3801assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003802{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 basicblock *b, *entryblock;
3804 struct assembler a;
3805 int i, j, nblocks;
3806 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003807
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 /* Make sure every block that falls off the end returns None.
3809 XXX NEXT_BLOCK() isn't quite right, because if the last
3810 block ends with a jump or return b_next shouldn't set.
3811 */
3812 if (!c->u->u_curblock->b_return) {
3813 NEXT_BLOCK(c);
3814 if (addNone)
3815 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3816 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003817 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003818
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 nblocks = 0;
3820 entryblock = NULL;
3821 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3822 nblocks++;
3823 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003824 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003825
Neal Norwitzed657552006-07-10 00:04:44 +00003826 /* Set firstlineno if it wasn't explicitly set. */
3827 if (!c->u->u_firstlineno) {
3828 if (entryblock && entryblock->b_instr)
3829 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3830 else
3831 c->u->u_firstlineno = 1;
3832 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3834 goto error;
3835 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003836
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003838 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003839
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 /* Emit code in reverse postorder from dfs. */
3841 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003842 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003843 for (j = 0; j < b->b_iused; j++)
3844 if (!assemble_emit(&a, &b->b_instr[j]))
3845 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003846 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003847
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003848 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003850 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003852
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 co = makecode(c, &a);
3854 error:
3855 assemble_free(&a);
3856 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003857}