blob: c8cab1ce4df29d459f6150605d1455c9a0fe4f6b [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:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000695 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 case SLICE+1:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 return -1;
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000698 case SLICE+2:
699 return -1;
700 case SLICE+3:
701 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702
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:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000781 return -3; /* or -1 or -2 if no exception occurred or
782 return/break/continue */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783 case BUILD_CLASS:
784 return -2;
785
786 case STORE_NAME:
787 return -1;
788 case DELETE_NAME:
789 return 0;
790 case UNPACK_SEQUENCE:
791 return oparg-1;
792 case FOR_ITER:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000793 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794
795 case STORE_ATTR:
796 return -2;
797 case DELETE_ATTR:
798 return -1;
799 case STORE_GLOBAL:
800 return -1;
801 case DELETE_GLOBAL:
802 return 0;
803 case DUP_TOPX:
804 return oparg;
805 case LOAD_CONST:
806 return 1;
807 case LOAD_NAME:
808 return 1;
809 case BUILD_TUPLE:
810 case BUILD_LIST:
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000811 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812 return 1-oparg;
813 case BUILD_MAP:
814 return 1;
815 case LOAD_ATTR:
816 return 0;
817 case COMPARE_OP:
818 return -1;
819 case IMPORT_NAME:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000820 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821 case IMPORT_FROM:
822 return 1;
823
824 case JUMP_FORWARD:
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000825 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
826 case JUMP_IF_FALSE_OR_POP: /* "" */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827 case JUMP_ABSOLUTE:
828 return 0;
829
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000830 case POP_JUMP_IF_FALSE:
831 case POP_JUMP_IF_TRUE:
832 return -1;
833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834 case LOAD_GLOBAL:
835 return 1;
836
837 case CONTINUE_LOOP:
838 return 0;
839 case SETUP_LOOP:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 case SETUP_EXCEPT:
841 case SETUP_FINALLY:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000842 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843
844 case LOAD_FAST:
845 return 1;
846 case STORE_FAST:
847 return -1;
848 case DELETE_FAST:
849 return 0;
850
851 case RAISE_VARARGS:
852 return -oparg;
853#define NARGS(o) (((o) % 256) + 2*((o) / 256))
854 case CALL_FUNCTION:
855 return -NARGS(oparg);
856 case CALL_FUNCTION_VAR:
857 case CALL_FUNCTION_KW:
858 return -NARGS(oparg)-1;
859 case CALL_FUNCTION_VAR_KW:
860 return -NARGS(oparg)-2;
861#undef NARGS
862 case MAKE_FUNCTION:
863 return -oparg;
864 case BUILD_SLICE:
865 if (oparg == 3)
866 return -2;
867 else
868 return -1;
869
870 case MAKE_CLOSURE:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000871 return -oparg-1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 case LOAD_CLOSURE:
873 return 1;
874 case LOAD_DEREF:
875 return 1;
876 case STORE_DEREF:
877 return -1;
878 default:
879 fprintf(stderr, "opcode = %d\n", opcode);
880 Py_FatalError("opcode_stack_effect()");
881
882 }
883 return 0; /* not reachable */
884}
885
886/* Add an opcode with no argument.
887 Returns 0 on failure, 1 on success.
888*/
889
890static int
891compiler_addop(struct compiler *c, int opcode)
892{
893 basicblock *b;
894 struct instr *i;
895 int off;
896 off = compiler_next_instr(c, c->u->u_curblock);
897 if (off < 0)
898 return 0;
899 b = c->u->u_curblock;
900 i = &b->b_instr[off];
901 i->i_opcode = opcode;
902 i->i_hasarg = 0;
903 if (opcode == RETURN_VALUE)
904 b->b_return = 1;
905 compiler_set_lineno(c, off);
906 return 1;
907}
908
909static int
910compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
911{
912 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913 Py_ssize_t arg;
Mark Dickinson105be772008-01-31 22:17:37 +0000914 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000916 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000917 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
918 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000919 d = PyFloat_AS_DOUBLE(o);
Mark Dickinson105be772008-01-31 22:17:37 +0000920 /* all we need is to make the tuple different in either the 0.0
921 * or -0.0 case from all others, just to avoid the "coercion".
922 */
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000923 if (d == 0.0 && copysign(1.0, d) < 0.0)
Mark Dickinson105be772008-01-31 22:17:37 +0000924 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
925 else
926 t = PyTuple_Pack(2, o, o->ob_type);
927 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000928#ifndef WITHOUT_COMPLEX
Mark Dickinson105be772008-01-31 22:17:37 +0000929 else if (PyComplex_Check(o)) {
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000930 Py_complex z;
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000931 int real_negzero, imag_negzero;
932 /* For the complex case we must make complex(x, 0.)
933 different from complex(x, -0.) and complex(0., y)
934 different from complex(-0., y), for any x and y.
935 All four complex zeros must be distinguished.*/
Mark Dickinson105be772008-01-31 22:17:37 +0000936 z = PyComplex_AsCComplex(o);
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000937 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
938 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
939 if (real_negzero && imag_negzero) {
940 t = PyTuple_Pack(5, o, o->ob_type,
941 Py_None, Py_None, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000942 }
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000943 else if (imag_negzero) {
944 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000945 }
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000946 else if (real_negzero) {
947 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000948 }
949 else {
950 t = PyTuple_Pack(2, o, o->ob_type);
951 }
952 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000953#endif /* WITHOUT_COMPLEX */
Mark Dickinson105be772008-01-31 22:17:37 +0000954 else {
955 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000956 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000957 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000958 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959
960 v = PyDict_GetItem(dict, t);
961 if (!v) {
962 arg = PyDict_Size(dict);
963 v = PyInt_FromLong(arg);
964 if (!v) {
965 Py_DECREF(t);
966 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000967 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968 if (PyDict_SetItem(dict, t, v) < 0) {
969 Py_DECREF(t);
970 Py_DECREF(v);
971 return -1;
972 }
973 Py_DECREF(v);
974 }
975 else
976 arg = PyInt_AsLong(v);
977 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000978 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000979}
980
981static int
982compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
983 PyObject *o)
984{
985 int arg = compiler_add_o(c, dict, o);
986 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000987 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988 return compiler_addop_i(c, opcode, arg);
989}
990
991static int
992compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000993 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994{
995 int arg;
996 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
997 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000998 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 arg = compiler_add_o(c, dict, mangled);
1000 Py_DECREF(mangled);
1001 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001002 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003 return compiler_addop_i(c, opcode, arg);
1004}
1005
1006/* Add an opcode with an integer argument.
1007 Returns 0 on failure, 1 on success.
1008*/
1009
1010static int
1011compiler_addop_i(struct compiler *c, int opcode, int oparg)
1012{
1013 struct instr *i;
1014 int off;
1015 off = compiler_next_instr(c, c->u->u_curblock);
1016 if (off < 0)
1017 return 0;
1018 i = &c->u->u_curblock->b_instr[off];
1019 i->i_opcode = opcode;
1020 i->i_oparg = oparg;
1021 i->i_hasarg = 1;
1022 compiler_set_lineno(c, off);
1023 return 1;
1024}
1025
1026static int
1027compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1028{
1029 struct instr *i;
1030 int off;
1031
1032 assert(b != NULL);
1033 off = compiler_next_instr(c, c->u->u_curblock);
1034 if (off < 0)
1035 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001036 i = &c->u->u_curblock->b_instr[off];
1037 i->i_opcode = opcode;
1038 i->i_target = b;
1039 i->i_hasarg = 1;
1040 if (absolute)
1041 i->i_jabs = 1;
1042 else
1043 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001044 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 return 1;
1046}
1047
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001048/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1049 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050 it as the current block. NEXT_BLOCK() also creates an implicit jump
1051 from the current block to the new block.
1052*/
1053
Neal Norwitzf733a012006-10-29 18:30:10 +00001054/* The returns inside these macros make it impossible to decref objects
1055 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056*/
1057
1058
1059#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001060 if (compiler_use_new_block((C)) == NULL) \
1061 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062}
1063
1064#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001065 if (compiler_next_block((C)) == NULL) \
1066 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001067}
1068
1069#define ADDOP(C, OP) { \
1070 if (!compiler_addop((C), (OP))) \
1071 return 0; \
1072}
1073
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001074#define ADDOP_IN_SCOPE(C, OP) { \
1075 if (!compiler_addop((C), (OP))) { \
1076 compiler_exit_scope(c); \
1077 return 0; \
1078 } \
1079}
1080
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081#define ADDOP_O(C, OP, O, TYPE) { \
1082 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1083 return 0; \
1084}
1085
1086#define ADDOP_NAME(C, OP, O, TYPE) { \
1087 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1088 return 0; \
1089}
1090
1091#define ADDOP_I(C, OP, O) { \
1092 if (!compiler_addop_i((C), (OP), (O))) \
1093 return 0; \
1094}
1095
1096#define ADDOP_JABS(C, OP, O) { \
1097 if (!compiler_addop_j((C), (OP), (O), 1)) \
1098 return 0; \
1099}
1100
1101#define ADDOP_JREL(C, OP, O) { \
1102 if (!compiler_addop_j((C), (OP), (O), 0)) \
1103 return 0; \
1104}
1105
1106/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1107 the ASDL name to synthesize the name of the C type and the visit function.
1108*/
1109
1110#define VISIT(C, TYPE, V) {\
1111 if (!compiler_visit_ ## TYPE((C), (V))) \
1112 return 0; \
1113}
1114
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001115#define VISIT_IN_SCOPE(C, TYPE, V) {\
1116 if (!compiler_visit_ ## TYPE((C), (V))) { \
1117 compiler_exit_scope(c); \
1118 return 0; \
1119 } \
1120}
1121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122#define VISIT_SLICE(C, V, CTX) {\
1123 if (!compiler_visit_slice((C), (V), (CTX))) \
1124 return 0; \
1125}
1126
1127#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001128 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001130 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001131 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001132 if (!compiler_visit_ ## TYPE((C), elt)) \
1133 return 0; \
1134 } \
1135}
1136
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001137#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001138 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001142 if (!compiler_visit_ ## TYPE((C), elt)) { \
1143 compiler_exit_scope(c); \
1144 return 0; \
1145 } \
1146 } \
1147}
1148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149static int
1150compiler_isdocstring(stmt_ty s)
1151{
1152 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001153 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return s->v.Expr.value->kind == Str_kind;
1155}
1156
1157/* Compile a sequence of statements, checking for a docstring. */
1158
1159static int
1160compiler_body(struct compiler *c, asdl_seq *stmts)
1161{
1162 int i = 0;
1163 stmt_ty st;
1164
1165 if (!asdl_seq_LEN(stmts))
1166 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001167 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001168 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1169 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170 i = 1;
1171 VISIT(c, expr, st->v.Expr.value);
1172 if (!compiler_nameop(c, __doc__, Store))
1173 return 0;
1174 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001175 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001176 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 return 1;
1178}
1179
1180static PyCodeObject *
1181compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001182{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001183 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001184 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 static PyObject *module;
1186 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001187 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 if (!module)
1189 return NULL;
1190 }
Neal Norwitzed657552006-07-10 00:04:44 +00001191 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1192 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001193 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 switch (mod->kind) {
1195 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001196 if (!compiler_body(c, mod->v.Module.body)) {
1197 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 break;
1201 case Interactive_kind:
1202 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001203 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001204 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 break;
1206 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001207 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001208 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 break;
1210 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001211 PyErr_SetString(PyExc_SystemError,
1212 "suite should not be possible");
1213 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001214 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001215 PyErr_Format(PyExc_SystemError,
1216 "module kind %d should not be possible",
1217 mod->kind);
1218 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001219 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 co = assemble(c, addNone);
1221 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001222 return co;
1223}
1224
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225/* The test for LOCAL must come before the test for FREE in order to
1226 handle classes where name is both local and free. The local var is
1227 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001228*/
1229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230static int
1231get_ref_type(struct compiler *c, PyObject *name)
1232{
1233 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001234 if (scope == 0) {
1235 char buf[350];
1236 PyOS_snprintf(buf, sizeof(buf),
1237 "unknown scope for %.100s in %.100s(%s) in %s\n"
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00001238 "symbols: %s\nlocals: %s\nglobals: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001239 PyString_AS_STRING(name),
1240 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001241 PyObject_REPR(c->u->u_ste->ste_id),
1242 c->c_filename,
1243 PyObject_REPR(c->u->u_ste->ste_symbols),
1244 PyObject_REPR(c->u->u_varnames),
1245 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001247 Py_FatalError(buf);
1248 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001249
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001250 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251}
1252
1253static int
1254compiler_lookup_arg(PyObject *dict, PyObject *name)
1255{
1256 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001257 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001259 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001261 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001263 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264 return PyInt_AS_LONG(v);
1265}
1266
1267static int
1268compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1269{
1270 int i, free = PyCode_GetNumFree(co);
1271 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001272 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1273 ADDOP_I(c, MAKE_FUNCTION, args);
1274 return 1;
1275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276 for (i = 0; i < free; ++i) {
1277 /* Bypass com_addop_varname because it will generate
1278 LOAD_DEREF but LOAD_CLOSURE is needed.
1279 */
1280 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1281 int arg, reftype;
1282
1283 /* Special case: If a class contains a method with a
1284 free variable that has the same name as a method,
1285 the name will be considered free *and* local in the
1286 class. It should be handled by the closure, as
1287 well as by the normal name loookup logic.
1288 */
1289 reftype = get_ref_type(c, name);
1290 if (reftype == CELL)
1291 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1292 else /* (reftype == FREE) */
1293 arg = compiler_lookup_arg(c->u->u_freevars, name);
1294 if (arg == -1) {
1295 printf("lookup %s in %s %d %d\n"
1296 "freevars of %s: %s\n",
1297 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001298 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001300 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001301 PyObject_REPR(co->co_freevars));
1302 Py_FatalError("compiler_make_closure()");
1303 }
1304 ADDOP_I(c, LOAD_CLOSURE, arg);
1305 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001306 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001308 ADDOP_I(c, MAKE_CLOSURE, args);
1309 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310}
1311
1312static int
1313compiler_decorators(struct compiler *c, asdl_seq* decos)
1314{
1315 int i;
1316
1317 if (!decos)
1318 return 1;
1319
1320 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001321 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322 }
1323 return 1;
1324}
1325
1326static int
1327compiler_arguments(struct compiler *c, arguments_ty args)
1328{
1329 int i;
1330 int n = asdl_seq_LEN(args->args);
1331 /* Correctly handle nested argument lists */
1332 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001333 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001335 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336 if (id == NULL) {
1337 return 0;
1338 }
1339 if (!compiler_nameop(c, id, Load)) {
1340 Py_DECREF(id);
1341 return 0;
1342 }
1343 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001344 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001345 }
1346 }
1347 return 1;
1348}
1349
1350static int
1351compiler_function(struct compiler *c, stmt_ty s)
1352{
1353 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001354 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001356 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001357 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358 int i, n, docstring;
1359
1360 assert(s->kind == FunctionDef_kind);
1361
1362 if (!compiler_decorators(c, decos))
1363 return 0;
1364 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001365 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1367 s->lineno))
1368 return 0;
1369
Anthony Baxter7b782b62006-04-11 12:01:56 +00001370 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001371 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001372 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001373 first_const = st->v.Expr.value->v.Str.s;
1374 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001375 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001376 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001377 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001378
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001379 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380 compiler_arguments(c, args);
1381
1382 c->u->u_argcount = asdl_seq_LEN(args->args);
1383 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001384 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001386 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1387 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388 }
1389 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001390 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391 if (co == NULL)
1392 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001393
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001394 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001395 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396
1397 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1398 ADDOP_I(c, CALL_FUNCTION, 1);
1399 }
1400
1401 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1402}
1403
1404static int
1405compiler_class(struct compiler *c, stmt_ty s)
1406{
Christian Heimes5224d282008-02-23 15:01:05 +00001407 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001408 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001409 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001410 asdl_seq* decos = s->v.ClassDef.decorator_list;
1411
1412 if (!compiler_decorators(c, decos))
1413 return 0;
1414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001415 /* push class name on stack, needed by BUILD_CLASS */
1416 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1417 /* push the tuple of base classes on the stack */
1418 n = asdl_seq_LEN(s->v.ClassDef.bases);
1419 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001420 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 ADDOP_I(c, BUILD_TUPLE, n);
1422 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1423 s->lineno))
1424 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001425 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001426 c->u->u_private = s->v.ClassDef.name;
1427 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001428 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429 if (!str || !compiler_nameop(c, str, Load)) {
1430 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001431 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001432 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001433 }
1434
1435 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001436 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 if (!str || !compiler_nameop(c, str, Store)) {
1438 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001439 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001441 }
1442 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001443
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001444 if (!compiler_body(c, s->v.ClassDef.body)) {
1445 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001446 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001447 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001449 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1450 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001452 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 if (co == NULL)
1454 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001456 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001457 Py_DECREF(co);
1458
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 ADDOP_I(c, CALL_FUNCTION, 0);
1460 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001461 /* apply decorators */
1462 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1463 ADDOP_I(c, CALL_FUNCTION, 1);
1464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1466 return 0;
1467 return 1;
1468}
1469
1470static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001471compiler_ifexp(struct compiler *c, expr_ty e)
1472{
1473 basicblock *end, *next;
1474
1475 assert(e->kind == IfExp_kind);
1476 end = compiler_new_block(c);
1477 if (end == NULL)
1478 return 0;
1479 next = compiler_new_block(c);
1480 if (next == NULL)
1481 return 0;
1482 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001483 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001484 VISIT(c, expr, e->v.IfExp.body);
1485 ADDOP_JREL(c, JUMP_FORWARD, end);
1486 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001487 VISIT(c, expr, e->v.IfExp.orelse);
1488 compiler_use_next_block(c, end);
1489 return 1;
1490}
1491
1492static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493compiler_lambda(struct compiler *c, expr_ty e)
1494{
1495 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001496 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497 arguments_ty args = e->v.Lambda.args;
1498 assert(e->kind == Lambda_kind);
1499
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001500 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001501 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001502 if (!name)
1503 return 0;
1504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
1506 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001507 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001508 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1509 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001510
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001511 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 compiler_arguments(c, args);
1513
1514 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001515 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson8d5934b2008-12-27 18:24:11 +00001516 if (c->u->u_ste->ste_generator) {
1517 ADDOP_IN_SCOPE(c, POP_TOP);
1518 }
1519 else {
1520 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1521 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001523 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 if (co == NULL)
1525 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001527 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001528 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529
1530 return 1;
1531}
1532
1533static int
1534compiler_print(struct compiler *c, stmt_ty s)
1535{
1536 int i, n;
1537 bool dest;
1538
1539 assert(s->kind == Print_kind);
1540 n = asdl_seq_LEN(s->v.Print.values);
1541 dest = false;
1542 if (s->v.Print.dest) {
1543 VISIT(c, expr, s->v.Print.dest);
1544 dest = true;
1545 }
1546 for (i = 0; i < n; i++) {
1547 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1548 if (dest) {
1549 ADDOP(c, DUP_TOP);
1550 VISIT(c, expr, e);
1551 ADDOP(c, ROT_TWO);
1552 ADDOP(c, PRINT_ITEM_TO);
1553 }
1554 else {
1555 VISIT(c, expr, e);
1556 ADDOP(c, PRINT_ITEM);
1557 }
1558 }
1559 if (s->v.Print.nl) {
1560 if (dest)
1561 ADDOP(c, PRINT_NEWLINE_TO)
1562 else
1563 ADDOP(c, PRINT_NEWLINE)
1564 }
1565 else if (dest)
1566 ADDOP(c, POP_TOP);
1567 return 1;
1568}
1569
1570static int
1571compiler_if(struct compiler *c, stmt_ty s)
1572{
1573 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001574 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001575 assert(s->kind == If_kind);
1576 end = compiler_new_block(c);
1577 if (end == NULL)
1578 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001579
1580 constant = expr_constant(s->v.If.test);
1581 /* constant = 0: "if 0"
1582 * constant = 1: "if 1", "if 2", ...
1583 * constant = -1: rest */
1584 if (constant == 0) {
1585 if (s->v.If.orelse)
1586 VISIT_SEQ(c, stmt, s->v.If.orelse);
1587 } else if (constant == 1) {
1588 VISIT_SEQ(c, stmt, s->v.If.body);
1589 } else {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001590 if (s->v.If.orelse) {
1591 next = compiler_new_block(c);
1592 if (next == NULL)
1593 return 0;
1594 }
1595 else
1596 next = end;
Georg Brandlddbaa662006-06-04 21:56:52 +00001597 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001598 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Georg Brandlddbaa662006-06-04 21:56:52 +00001599 VISIT_SEQ(c, stmt, s->v.If.body);
1600 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001601 if (s->v.If.orelse) {
1602 compiler_use_next_block(c, next);
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001603 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001604 }
Georg Brandlddbaa662006-06-04 21:56:52 +00001605 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 compiler_use_next_block(c, end);
1607 return 1;
1608}
1609
1610static int
1611compiler_for(struct compiler *c, stmt_ty s)
1612{
1613 basicblock *start, *cleanup, *end;
1614
1615 start = compiler_new_block(c);
1616 cleanup = compiler_new_block(c);
1617 end = compiler_new_block(c);
1618 if (start == NULL || end == NULL || cleanup == NULL)
1619 return 0;
1620 ADDOP_JREL(c, SETUP_LOOP, end);
1621 if (!compiler_push_fblock(c, LOOP, start))
1622 return 0;
1623 VISIT(c, expr, s->v.For.iter);
1624 ADDOP(c, GET_ITER);
1625 compiler_use_next_block(c, start);
1626 ADDOP_JREL(c, FOR_ITER, cleanup);
1627 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001628 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1630 compiler_use_next_block(c, cleanup);
1631 ADDOP(c, POP_BLOCK);
1632 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001633 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 compiler_use_next_block(c, end);
1635 return 1;
1636}
1637
1638static int
1639compiler_while(struct compiler *c, stmt_ty s)
1640{
1641 basicblock *loop, *orelse, *end, *anchor = NULL;
1642 int constant = expr_constant(s->v.While.test);
1643
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001644 if (constant == 0) {
1645 if (s->v.While.orelse)
1646 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649 loop = compiler_new_block(c);
1650 end = compiler_new_block(c);
1651 if (constant == -1) {
1652 anchor = compiler_new_block(c);
1653 if (anchor == NULL)
1654 return 0;
1655 }
1656 if (loop == NULL || end == NULL)
1657 return 0;
1658 if (s->v.While.orelse) {
1659 orelse = compiler_new_block(c);
1660 if (orelse == NULL)
1661 return 0;
1662 }
1663 else
1664 orelse = NULL;
1665
1666 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001667 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 if (!compiler_push_fblock(c, LOOP, loop))
1669 return 0;
1670 if (constant == -1) {
1671 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001672 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001674 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1676
1677 /* XXX should the two POP instructions be in a separate block
1678 if there is no else clause ?
1679 */
1680
1681 if (constant == -1) {
1682 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 ADDOP(c, POP_BLOCK);
1684 }
1685 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001686 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001687 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 compiler_use_next_block(c, end);
1689
1690 return 1;
1691}
1692
1693static int
1694compiler_continue(struct compiler *c)
1695{
1696 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001697 static const char IN_FINALLY_ERROR_MSG[] =
1698 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699 int i;
1700
1701 if (!c->u->u_nfblocks)
1702 return compiler_error(c, LOOP_ERROR_MSG);
1703 i = c->u->u_nfblocks - 1;
1704 switch (c->u->u_fblock[i].fb_type) {
1705 case LOOP:
1706 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1707 break;
1708 case EXCEPT:
1709 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001710 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1711 /* Prevent continue anywhere under a finally
1712 even if hidden in a sub-try or except. */
1713 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1714 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1715 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 if (i == -1)
1717 return compiler_error(c, LOOP_ERROR_MSG);
1718 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1719 break;
1720 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001721 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 }
1723
1724 return 1;
1725}
1726
1727/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1728
1729 SETUP_FINALLY L
1730 <code for body>
1731 POP_BLOCK
1732 LOAD_CONST <None>
1733 L: <code for finalbody>
1734 END_FINALLY
1735
1736 The special instructions use the block stack. Each block
1737 stack entry contains the instruction that created it (here
1738 SETUP_FINALLY), the level of the value stack at the time the
1739 block stack entry was created, and a label (here L).
1740
1741 SETUP_FINALLY:
1742 Pushes the current value stack level and the label
1743 onto the block stack.
1744 POP_BLOCK:
1745 Pops en entry from the block stack, and pops the value
1746 stack until its level is the same as indicated on the
1747 block stack. (The label is ignored.)
1748 END_FINALLY:
1749 Pops a variable number of entries from the *value* stack
1750 and re-raises the exception they specify. The number of
1751 entries popped depends on the (pseudo) exception type.
1752
1753 The block stack is unwound when an exception is raised:
1754 when a SETUP_FINALLY entry is found, the exception is pushed
1755 onto the value stack (and the exception condition is cleared),
1756 and the interpreter jumps to the label gotten from the block
1757 stack.
1758*/
1759
1760static int
1761compiler_try_finally(struct compiler *c, stmt_ty s)
1762{
1763 basicblock *body, *end;
1764 body = compiler_new_block(c);
1765 end = compiler_new_block(c);
1766 if (body == NULL || end == NULL)
1767 return 0;
1768
1769 ADDOP_JREL(c, SETUP_FINALLY, end);
1770 compiler_use_next_block(c, body);
1771 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1772 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001773 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001774 ADDOP(c, POP_BLOCK);
1775 compiler_pop_fblock(c, FINALLY_TRY, body);
1776
1777 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1778 compiler_use_next_block(c, end);
1779 if (!compiler_push_fblock(c, FINALLY_END, end))
1780 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001781 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 ADDOP(c, END_FINALLY);
1783 compiler_pop_fblock(c, FINALLY_END, end);
1784
1785 return 1;
1786}
1787
1788/*
1789 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1790 (The contents of the value stack is shown in [], with the top
1791 at the right; 'tb' is trace-back info, 'val' the exception's
1792 associated value, and 'exc' the exception.)
1793
1794 Value stack Label Instruction Argument
1795 [] SETUP_EXCEPT L1
1796 [] <code for S>
1797 [] POP_BLOCK
1798 [] JUMP_FORWARD L0
1799
1800 [tb, val, exc] L1: DUP )
1801 [tb, val, exc, exc] <evaluate E1> )
1802 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001803 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 [tb, val, exc] POP
1805 [tb, val] <assign to V1> (or POP if no V1)
1806 [tb] POP
1807 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001808 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001810 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 .............................etc.......................
1812
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001813 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814
1815 [] L0: <next statement>
1816
1817 Of course, parts are not generated if Vi or Ei is not present.
1818*/
1819static int
1820compiler_try_except(struct compiler *c, stmt_ty s)
1821{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001822 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001823 int i, n;
1824
1825 body = compiler_new_block(c);
1826 except = compiler_new_block(c);
1827 orelse = compiler_new_block(c);
1828 end = compiler_new_block(c);
1829 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1830 return 0;
1831 ADDOP_JREL(c, SETUP_EXCEPT, except);
1832 compiler_use_next_block(c, body);
1833 if (!compiler_push_fblock(c, EXCEPT, body))
1834 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001835 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 ADDOP(c, POP_BLOCK);
1837 compiler_pop_fblock(c, EXCEPT, body);
1838 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1839 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1840 compiler_use_next_block(c, except);
1841 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001842 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001844 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001846 c->u->u_lineno_set = false;
1847 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848 except = compiler_new_block(c);
1849 if (except == NULL)
1850 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001851 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001853 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001855 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 }
1857 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001858 if (handler->v.ExceptHandler.name) {
1859 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 }
1861 else {
1862 ADDOP(c, POP_TOP);
1863 }
1864 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001865 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 ADDOP_JREL(c, JUMP_FORWARD, end);
1867 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001868 }
1869 ADDOP(c, END_FINALLY);
1870 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001871 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 compiler_use_next_block(c, end);
1873 return 1;
1874}
1875
1876static int
1877compiler_import_as(struct compiler *c, identifier name, identifier asname)
1878{
1879 /* The IMPORT_NAME opcode was already generated. This function
1880 merely needs to bind the result to a name.
1881
1882 If there is a dot in name, we need to split it and emit a
1883 LOAD_ATTR for each name.
1884 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001885 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001886 const char *dot = strchr(src, '.');
1887 if (dot) {
1888 /* Consume the base module name to get the first attribute */
1889 src = dot + 1;
1890 while (dot) {
1891 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001892 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001893 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001894 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001896 if (!attr)
1897 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001899 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001900 src = dot + 1;
1901 }
1902 }
1903 return compiler_nameop(c, asname, Store);
1904}
1905
1906static int
1907compiler_import(struct compiler *c, stmt_ty s)
1908{
1909 /* The Import node stores a module name like a.b.c as a single
1910 string. This is convenient for all cases except
1911 import a.b.c as d
1912 where we need to parse that string to extract the individual
1913 module names.
1914 XXX Perhaps change the representation to make this case simpler?
1915 */
1916 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001917
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001919 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001921 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922
Neal Norwitzcbce2802006-04-03 06:26:32 +00001923 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001924 level = PyInt_FromLong(0);
1925 else
1926 level = PyInt_FromLong(-1);
1927
1928 if (level == NULL)
1929 return 0;
1930
1931 ADDOP_O(c, LOAD_CONST, level, consts);
1932 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1934 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1935
1936 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001937 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001938 if (!r)
1939 return r;
1940 }
1941 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001943 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944 char *dot = strchr(base, '.');
1945 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001946 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947 dot - base);
1948 r = compiler_nameop(c, tmp, Store);
1949 if (dot) {
1950 Py_DECREF(tmp);
1951 }
1952 if (!r)
1953 return r;
1954 }
1955 }
1956 return 1;
1957}
1958
1959static int
1960compiler_from_import(struct compiler *c, stmt_ty s)
1961{
1962 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963
1964 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001965 PyObject *level;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00001966 static PyObject *empty_string;
1967
1968 if (!empty_string) {
1969 empty_string = PyString_FromString("");
1970 if (!empty_string)
1971 return 0;
1972 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 if (!names)
1975 return 0;
1976
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001977 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001978 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001979 level = PyInt_FromLong(-1);
1980 else
1981 level = PyInt_FromLong(s->v.ImportFrom.level);
1982
1983 if (!level) {
1984 Py_DECREF(names);
1985 return 0;
1986 }
1987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988 /* build up the names */
1989 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001990 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991 Py_INCREF(alias->name);
1992 PyTuple_SET_ITEM(names, i, alias->name);
1993 }
1994
Benjamin Petersona72be3b2009-06-13 20:23:33 +00001995 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
1996 !strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {
1997 Py_DECREF(level);
1998 Py_DECREF(names);
1999 return compiler_error(c, "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002000 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 }
2002
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002003 ADDOP_O(c, LOAD_CONST, level, consts);
2004 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002006 Py_DECREF(names);
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002007 if (s->v.ImportFrom.module) {
2008 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2009 }
2010 else {
2011 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2012 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002013 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002014 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 identifier store_name;
2016
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002017 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 assert(n == 1);
2019 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002020 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 }
2022
2023 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2024 store_name = alias->name;
2025 if (alias->asname)
2026 store_name = alias->asname;
2027
2028 if (!compiler_nameop(c, store_name, Store)) {
2029 Py_DECREF(names);
2030 return 0;
2031 }
2032 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002033 /* remove imported module */
2034 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 return 1;
2036}
2037
2038static int
2039compiler_assert(struct compiler *c, stmt_ty s)
2040{
2041 static PyObject *assertion_error = NULL;
2042 basicblock *end;
2043
2044 if (Py_OptimizeFlag)
2045 return 1;
2046 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002047 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002048 if (assertion_error == NULL)
2049 return 0;
2050 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002051 if (s->v.Assert.test->kind == Tuple_kind &&
2052 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2053 const char* msg =
2054 "assertion is always true, perhaps remove parentheses?";
2055 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2056 c->u->u_lineno, NULL, NULL) == -1)
2057 return 0;
2058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002059 VISIT(c, expr, s->v.Assert.test);
2060 end = compiler_new_block(c);
2061 if (end == NULL)
2062 return 0;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002063 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2065 if (s->v.Assert.msg) {
2066 VISIT(c, expr, s->v.Assert.msg);
2067 ADDOP_I(c, RAISE_VARARGS, 2);
2068 }
2069 else {
2070 ADDOP_I(c, RAISE_VARARGS, 1);
2071 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002072 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 return 1;
2074}
2075
2076static int
2077compiler_visit_stmt(struct compiler *c, stmt_ty s)
2078{
2079 int i, n;
2080
Neal Norwitzf733a012006-10-29 18:30:10 +00002081 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 c->u->u_lineno = s->lineno;
2083 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002084
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002086 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002088 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002090 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 if (c->u->u_ste->ste_type != FunctionBlock)
2092 return compiler_error(c, "'return' outside function");
2093 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002094 VISIT(c, expr, s->v.Return.value);
2095 }
2096 else
2097 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2098 ADDOP(c, RETURN_VALUE);
2099 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002100 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002101 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002103 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 n = asdl_seq_LEN(s->v.Assign.targets);
2105 VISIT(c, expr, s->v.Assign.value);
2106 for (i = 0; i < n; i++) {
2107 if (i < n - 1)
2108 ADDOP(c, DUP_TOP);
2109 VISIT(c, expr,
2110 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2111 }
2112 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002113 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002115 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002116 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002117 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002119 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002120 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002121 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002123 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124 n = 0;
2125 if (s->v.Raise.type) {
2126 VISIT(c, expr, s->v.Raise.type);
2127 n++;
2128 if (s->v.Raise.inst) {
2129 VISIT(c, expr, s->v.Raise.inst);
2130 n++;
2131 if (s->v.Raise.tback) {
2132 VISIT(c, expr, s->v.Raise.tback);
2133 n++;
2134 }
2135 }
2136 }
2137 ADDOP_I(c, RAISE_VARARGS, n);
2138 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002139 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002141 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002143 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002144 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002145 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002147 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002149 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 VISIT(c, expr, s->v.Exec.body);
2151 if (s->v.Exec.globals) {
2152 VISIT(c, expr, s->v.Exec.globals);
2153 if (s->v.Exec.locals) {
2154 VISIT(c, expr, s->v.Exec.locals);
2155 } else {
2156 ADDOP(c, DUP_TOP);
2157 }
2158 } else {
2159 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2160 ADDOP(c, DUP_TOP);
2161 }
2162 ADDOP(c, EXEC_STMT);
2163 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002164 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002166 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002167 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002168 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169 ADDOP(c, PRINT_EXPR);
2170 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002171 else if (s->v.Expr.value->kind != Str_kind &&
2172 s->v.Expr.value->kind != Num_kind) {
2173 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002174 ADDOP(c, POP_TOP);
2175 }
2176 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002177 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002179 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002180 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 return compiler_error(c, "'break' outside loop");
2182 ADDOP(c, BREAK_LOOP);
2183 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002184 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002186 case With_kind:
2187 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 }
2189 return 1;
2190}
2191
2192static int
2193unaryop(unaryop_ty op)
2194{
2195 switch (op) {
2196 case Invert:
2197 return UNARY_INVERT;
2198 case Not:
2199 return UNARY_NOT;
2200 case UAdd:
2201 return UNARY_POSITIVE;
2202 case USub:
2203 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002204 default:
2205 PyErr_Format(PyExc_SystemError,
2206 "unary op %d should not be possible", op);
2207 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209}
2210
2211static int
2212binop(struct compiler *c, operator_ty op)
2213{
2214 switch (op) {
2215 case Add:
2216 return BINARY_ADD;
2217 case Sub:
2218 return BINARY_SUBTRACT;
2219 case Mult:
2220 return BINARY_MULTIPLY;
2221 case Div:
2222 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2223 return BINARY_TRUE_DIVIDE;
2224 else
2225 return BINARY_DIVIDE;
2226 case Mod:
2227 return BINARY_MODULO;
2228 case Pow:
2229 return BINARY_POWER;
2230 case LShift:
2231 return BINARY_LSHIFT;
2232 case RShift:
2233 return BINARY_RSHIFT;
2234 case BitOr:
2235 return BINARY_OR;
2236 case BitXor:
2237 return BINARY_XOR;
2238 case BitAnd:
2239 return BINARY_AND;
2240 case FloorDiv:
2241 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002242 default:
2243 PyErr_Format(PyExc_SystemError,
2244 "binary op %d should not be possible", op);
2245 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247}
2248
2249static int
2250cmpop(cmpop_ty op)
2251{
2252 switch (op) {
2253 case Eq:
2254 return PyCmp_EQ;
2255 case NotEq:
2256 return PyCmp_NE;
2257 case Lt:
2258 return PyCmp_LT;
2259 case LtE:
2260 return PyCmp_LE;
2261 case Gt:
2262 return PyCmp_GT;
2263 case GtE:
2264 return PyCmp_GE;
2265 case Is:
2266 return PyCmp_IS;
2267 case IsNot:
2268 return PyCmp_IS_NOT;
2269 case In:
2270 return PyCmp_IN;
2271 case NotIn:
2272 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002273 default:
2274 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276}
2277
2278static int
2279inplace_binop(struct compiler *c, operator_ty op)
2280{
2281 switch (op) {
2282 case Add:
2283 return INPLACE_ADD;
2284 case Sub:
2285 return INPLACE_SUBTRACT;
2286 case Mult:
2287 return INPLACE_MULTIPLY;
2288 case Div:
2289 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2290 return INPLACE_TRUE_DIVIDE;
2291 else
2292 return INPLACE_DIVIDE;
2293 case Mod:
2294 return INPLACE_MODULO;
2295 case Pow:
2296 return INPLACE_POWER;
2297 case LShift:
2298 return INPLACE_LSHIFT;
2299 case RShift:
2300 return INPLACE_RSHIFT;
2301 case BitOr:
2302 return INPLACE_OR;
2303 case BitXor:
2304 return INPLACE_XOR;
2305 case BitAnd:
2306 return INPLACE_AND;
2307 case FloorDiv:
2308 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002309 default:
2310 PyErr_Format(PyExc_SystemError,
2311 "inplace binary op %d should not be possible", op);
2312 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314}
2315
2316static int
2317compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2318{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002319 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2321
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002322 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002323 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 /* XXX AugStore isn't used anywhere! */
2325
Neal Norwitz0031ff32008-02-25 01:45:37 +00002326 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002327 if (!mangled)
2328 return 0;
2329
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 op = 0;
2331 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002332 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 switch (scope) {
2334 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002335 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 optype = OP_DEREF;
2337 break;
2338 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002339 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 optype = OP_DEREF;
2341 break;
2342 case LOCAL:
2343 if (c->u->u_ste->ste_type == FunctionBlock)
2344 optype = OP_FAST;
2345 break;
2346 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002347 if (c->u->u_ste->ste_type == FunctionBlock &&
2348 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 optype = OP_GLOBAL;
2350 break;
2351 case GLOBAL_EXPLICIT:
2352 optype = OP_GLOBAL;
2353 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002354 default:
2355 /* scope can be 0 */
2356 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002357 }
2358
2359 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002360 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361
2362 switch (optype) {
2363 case OP_DEREF:
2364 switch (ctx) {
2365 case Load: op = LOAD_DEREF; break;
2366 case Store: op = STORE_DEREF; break;
2367 case AugLoad:
2368 case AugStore:
2369 break;
2370 case Del:
2371 PyErr_Format(PyExc_SyntaxError,
2372 "can not delete variable '%s' referenced "
2373 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002374 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002375 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002377 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002378 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002379 PyErr_SetString(PyExc_SystemError,
2380 "param invalid for deref variable");
2381 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 }
2383 break;
2384 case OP_FAST:
2385 switch (ctx) {
2386 case Load: op = LOAD_FAST; break;
2387 case Store: op = STORE_FAST; break;
2388 case Del: op = DELETE_FAST; break;
2389 case AugLoad:
2390 case AugStore:
2391 break;
2392 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002393 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002394 PyErr_SetString(PyExc_SystemError,
2395 "param invalid for local variable");
2396 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002398 ADDOP_O(c, op, mangled, varnames);
2399 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400 return 1;
2401 case OP_GLOBAL:
2402 switch (ctx) {
2403 case Load: op = LOAD_GLOBAL; break;
2404 case Store: op = STORE_GLOBAL; break;
2405 case Del: op = DELETE_GLOBAL; break;
2406 case AugLoad:
2407 case AugStore:
2408 break;
2409 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002410 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002411 PyErr_SetString(PyExc_SystemError,
2412 "param invalid for global variable");
2413 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002414 }
2415 break;
2416 case OP_NAME:
2417 switch (ctx) {
2418 case Load: op = LOAD_NAME; break;
2419 case Store: op = STORE_NAME; break;
2420 case Del: op = DELETE_NAME; break;
2421 case AugLoad:
2422 case AugStore:
2423 break;
2424 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002425 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002426 PyErr_SetString(PyExc_SystemError,
2427 "param invalid for name variable");
2428 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 }
2430 break;
2431 }
2432
2433 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002434 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002435 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002436 if (arg < 0)
2437 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002438 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439}
2440
2441static int
2442compiler_boolop(struct compiler *c, expr_ty e)
2443{
2444 basicblock *end;
2445 int jumpi, i, n;
2446 asdl_seq *s;
2447
2448 assert(e->kind == BoolOp_kind);
2449 if (e->v.BoolOp.op == And)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002450 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 else
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002452 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002454 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455 return 0;
2456 s = e->v.BoolOp.values;
2457 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002458 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002459 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002460 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002461 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002463 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002464 compiler_use_next_block(c, end);
2465 return 1;
2466}
2467
2468static int
2469compiler_list(struct compiler *c, expr_ty e)
2470{
2471 int n = asdl_seq_LEN(e->v.List.elts);
2472 if (e->v.List.ctx == Store) {
2473 ADDOP_I(c, UNPACK_SEQUENCE, n);
2474 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002475 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 if (e->v.List.ctx == Load) {
2477 ADDOP_I(c, BUILD_LIST, n);
2478 }
2479 return 1;
2480}
2481
2482static int
2483compiler_tuple(struct compiler *c, expr_ty e)
2484{
2485 int n = asdl_seq_LEN(e->v.Tuple.elts);
2486 if (e->v.Tuple.ctx == Store) {
2487 ADDOP_I(c, UNPACK_SEQUENCE, n);
2488 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002489 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 if (e->v.Tuple.ctx == Load) {
2491 ADDOP_I(c, BUILD_TUPLE, n);
2492 }
2493 return 1;
2494}
2495
2496static int
2497compiler_compare(struct compiler *c, expr_ty e)
2498{
2499 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002500 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501
2502 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2503 VISIT(c, expr, e->v.Compare.left);
2504 n = asdl_seq_LEN(e->v.Compare.ops);
2505 assert(n > 0);
2506 if (n > 1) {
2507 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002508 if (cleanup == NULL)
2509 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002510 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002511 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 }
2513 for (i = 1; i < n; i++) {
2514 ADDOP(c, DUP_TOP);
2515 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002517 cmpop((cmpop_ty)(asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002518 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002519 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002520 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002522 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002523 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002524 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002525 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002527 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 if (n > 1) {
2529 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002530 if (end == NULL)
2531 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002532 ADDOP_JREL(c, JUMP_FORWARD, end);
2533 compiler_use_next_block(c, cleanup);
2534 ADDOP(c, ROT_TWO);
2535 ADDOP(c, POP_TOP);
2536 compiler_use_next_block(c, end);
2537 }
2538 return 1;
2539}
2540
2541static int
2542compiler_call(struct compiler *c, expr_ty e)
2543{
2544 int n, code = 0;
2545
2546 VISIT(c, expr, e->v.Call.func);
2547 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002548 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002550 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002551 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2552 }
2553 if (e->v.Call.starargs) {
2554 VISIT(c, expr, e->v.Call.starargs);
2555 code |= 1;
2556 }
2557 if (e->v.Call.kwargs) {
2558 VISIT(c, expr, e->v.Call.kwargs);
2559 code |= 2;
2560 }
2561 switch (code) {
2562 case 0:
2563 ADDOP_I(c, CALL_FUNCTION, n);
2564 break;
2565 case 1:
2566 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2567 break;
2568 case 2:
2569 ADDOP_I(c, CALL_FUNCTION_KW, n);
2570 break;
2571 case 3:
2572 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2573 break;
2574 }
2575 return 1;
2576}
2577
2578static int
Antoine Pitroud0c35152008-12-17 00:38:28 +00002579compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
2580 int gen_index, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581{
2582 /* generate code for the iterator, then each of the ifs,
2583 and then write to the element */
2584
2585 comprehension_ty l;
2586 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002587 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588
2589 start = compiler_new_block(c);
2590 skip = compiler_new_block(c);
2591 if_cleanup = compiler_new_block(c);
2592 anchor = compiler_new_block(c);
2593
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002594 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2595 anchor == NULL)
2596 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597
Anthony Baxter7b782b62006-04-11 12:01:56 +00002598 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599 VISIT(c, expr, l->iter);
2600 ADDOP(c, GET_ITER);
2601 compiler_use_next_block(c, start);
2602 ADDOP_JREL(c, FOR_ITER, anchor);
2603 NEXT_BLOCK(c);
2604 VISIT(c, expr, l->target);
2605
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002606 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 n = asdl_seq_LEN(l->ifs);
2608 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002609 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002611 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 }
2614
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002615 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitroud0c35152008-12-17 00:38:28 +00002616 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002619 /* only append after the last for generator */
2620 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002621 VISIT(c, expr, elt);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002622 ADDOP_I(c, LIST_APPEND, gen_index+1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002623
2624 compiler_use_next_block(c, skip);
2625 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002626 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2628 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629
2630 return 1;
2631}
2632
2633static int
2634compiler_listcomp(struct compiler *c, expr_ty e)
2635{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002636 assert(e->kind == ListComp_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 ADDOP_I(c, BUILD_LIST, 0);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002638 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2639 e->v.ListComp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002640}
2641
2642static int
2643compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002644 asdl_seq *generators, int gen_index,
2645 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646{
2647 /* generate code for the iterator, then each of the ifs,
2648 and then write to the element */
2649
2650 comprehension_ty ge;
2651 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002652 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653
2654 start = compiler_new_block(c);
2655 skip = compiler_new_block(c);
2656 if_cleanup = compiler_new_block(c);
2657 anchor = compiler_new_block(c);
2658 end = compiler_new_block(c);
2659
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002660 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 anchor == NULL || end == NULL)
2662 return 0;
2663
Anthony Baxter7b782b62006-04-11 12:01:56 +00002664 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002665 ADDOP_JREL(c, SETUP_LOOP, end);
2666 if (!compiler_push_fblock(c, LOOP, start))
2667 return 0;
2668
2669 if (gen_index == 0) {
2670 /* Receive outermost iter as an implicit argument */
2671 c->u->u_argcount = 1;
2672 ADDOP_I(c, LOAD_FAST, 0);
2673 }
2674 else {
2675 /* Sub-iter - calculate on the fly */
2676 VISIT(c, expr, ge->iter);
2677 ADDOP(c, GET_ITER);
2678 }
2679 compiler_use_next_block(c, start);
2680 ADDOP_JREL(c, FOR_ITER, anchor);
2681 NEXT_BLOCK(c);
2682 VISIT(c, expr, ge->target);
2683
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002684 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002685 n = asdl_seq_LEN(ge->ifs);
2686 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002687 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002689 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690 NEXT_BLOCK(c);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002691 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002693 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002694 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2695 return 0;
2696
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002697 /* only append after the last 'for' generator */
2698 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 VISIT(c, expr, elt);
2700 ADDOP(c, YIELD_VALUE);
2701 ADDOP(c, POP_TOP);
2702
2703 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002704 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002705 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2707 compiler_use_next_block(c, anchor);
2708 ADDOP(c, POP_BLOCK);
2709 compiler_pop_fblock(c, LOOP, start);
2710 compiler_use_next_block(c, end);
2711
2712 return 1;
2713}
2714
2715static int
2716compiler_genexp(struct compiler *c, expr_ty e)
2717{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002718 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 PyCodeObject *co;
2720 expr_ty outermost_iter = ((comprehension_ty)
2721 (asdl_seq_GET(e->v.GeneratorExp.generators,
2722 0)))->iter;
2723
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002724 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002725 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002726 if (!name)
2727 return 0;
2728 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729
2730 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2731 return 0;
2732 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2733 e->v.GeneratorExp.elt);
2734 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002735 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736 if (co == NULL)
2737 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002739 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002740 Py_DECREF(co);
2741
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 VISIT(c, expr, outermost_iter);
2743 ADDOP(c, GET_ITER);
2744 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745
2746 return 1;
2747}
2748
2749static int
2750compiler_visit_keyword(struct compiler *c, keyword_ty k)
2751{
2752 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2753 VISIT(c, expr, k->value);
2754 return 1;
2755}
2756
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002757/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 whether they are true or false.
2759
2760 Return values: 1 for true, 0 for false, -1 for non-constant.
2761 */
2762
2763static int
2764expr_constant(expr_ty e)
2765{
2766 switch (e->kind) {
2767 case Num_kind:
2768 return PyObject_IsTrue(e->v.Num.n);
2769 case Str_kind:
2770 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002771 case Name_kind:
2772 /* __debug__ is not assignable, so we can optimize
2773 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002774 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002775 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002776 return ! Py_OptimizeFlag;
2777 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002778 default:
2779 return -1;
2780 }
2781}
2782
Guido van Rossumc2e20742006-02-27 22:32:47 +00002783/*
2784 Implements the with statement from PEP 343.
2785
2786 The semantics outlined in that PEP are as follows:
2787
2788 with EXPR as VAR:
2789 BLOCK
2790
2791 It is implemented roughly as:
2792
Guido van Rossumda5b7012006-05-02 19:47:52 +00002793 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002794 exit = context.__exit__ # not calling it
2795 value = context.__enter__()
2796 try:
2797 VAR = value # if VAR present in the syntax
2798 BLOCK
2799 finally:
2800 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002801 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002802 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002803 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002804 exit(*exc)
2805 */
2806static int
2807compiler_with(struct compiler *c, stmt_ty s)
2808{
Guido van Rossumc2e20742006-02-27 22:32:47 +00002809 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002810
2811 assert(s->kind == With_kind);
2812
Guido van Rossumc2e20742006-02-27 22:32:47 +00002813 block = compiler_new_block(c);
2814 finally = compiler_new_block(c);
2815 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002816 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002817
Guido van Rossumda5b7012006-05-02 19:47:52 +00002818 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002819 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002820 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002821
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002822 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002823 compiler_use_next_block(c, block);
2824 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002825 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002826 }
2827
2828 if (s->v.With.optional_vars) {
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002829 VISIT(c, expr, s->v.With.optional_vars);
2830 }
2831 else {
2832 /* Discard result from context.__enter__() */
2833 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002834 }
2835
2836 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002837 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002838
2839 /* End of try block; start the finally block */
2840 ADDOP(c, POP_BLOCK);
2841 compiler_pop_fblock(c, FINALLY_TRY, block);
2842
2843 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2844 compiler_use_next_block(c, finally);
2845 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002846 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002847
Nick Coghlan7af53be2008-03-07 14:13:28 +00002848 /* Finally block starts; context.__exit__ is on the stack under
2849 the exception or return information. Just issue our magic
2850 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002851 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002852
2853 /* Finally block ends. */
2854 ADDOP(c, END_FINALLY);
2855 compiler_pop_fblock(c, FINALLY_END, finally);
2856 return 1;
2857}
2858
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859static int
2860compiler_visit_expr(struct compiler *c, expr_ty e)
2861{
2862 int i, n;
2863
Neal Norwitzf733a012006-10-29 18:30:10 +00002864 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002865 set a new line number for the next instruction.
2866 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002867 if (e->lineno > c->u->u_lineno) {
2868 c->u->u_lineno = e->lineno;
2869 c->u->u_lineno_set = false;
2870 }
2871 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002872 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002874 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875 VISIT(c, expr, e->v.BinOp.left);
2876 VISIT(c, expr, e->v.BinOp.right);
2877 ADDOP(c, binop(c, e->v.BinOp.op));
2878 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002879 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880 VISIT(c, expr, e->v.UnaryOp.operand);
2881 ADDOP(c, unaryop(e->v.UnaryOp.op));
2882 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002883 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002885 case IfExp_kind:
2886 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002887 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00002889 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002891 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002892 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00002893 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002894 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00002895 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896 }
2897 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002898 case Set_kind:
2899 n = asdl_seq_LEN(e->v.Set.elts);
2900 VISIT_SEQ(c, expr, e->v.Set.elts);
2901 ADDOP_I(c, BUILD_SET, n);
2902 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002903 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002904 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002905 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 return compiler_genexp(c, e);
2907 case Yield_kind:
2908 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002909 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910 if (e->v.Yield.value) {
2911 VISIT(c, expr, e->v.Yield.value);
2912 }
2913 else {
2914 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2915 }
2916 ADDOP(c, YIELD_VALUE);
2917 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002918 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002920 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002922 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923 VISIT(c, expr, e->v.Repr.value);
2924 ADDOP(c, UNARY_CONVERT);
2925 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002926 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002927 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
2928 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002929 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002930 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
2931 break;
2932 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002933 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002934 if (e->v.Attribute.ctx != AugStore)
2935 VISIT(c, expr, e->v.Attribute.value);
2936 switch (e->v.Attribute.ctx) {
2937 case AugLoad:
2938 ADDOP(c, DUP_TOP);
2939 /* Fall through to load */
2940 case Load:
2941 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
2942 break;
2943 case AugStore:
2944 ADDOP(c, ROT_TWO);
2945 /* Fall through to save */
2946 case Store:
2947 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
2948 break;
2949 case Del:
2950 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
2951 break;
2952 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002953 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002954 PyErr_SetString(PyExc_SystemError,
2955 "param invalid in attribute expression");
2956 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 }
2958 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002959 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960 switch (e->v.Subscript.ctx) {
2961 case AugLoad:
2962 VISIT(c, expr, e->v.Subscript.value);
2963 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
2964 break;
2965 case Load:
2966 VISIT(c, expr, e->v.Subscript.value);
2967 VISIT_SLICE(c, e->v.Subscript.slice, Load);
2968 break;
2969 case AugStore:
2970 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
2971 break;
2972 case Store:
2973 VISIT(c, expr, e->v.Subscript.value);
2974 VISIT_SLICE(c, e->v.Subscript.slice, Store);
2975 break;
2976 case Del:
2977 VISIT(c, expr, e->v.Subscript.value);
2978 VISIT_SLICE(c, e->v.Subscript.slice, Del);
2979 break;
2980 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002981 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002982 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002983 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00002984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002985 }
2986 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002987 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
2989 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002990 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002992 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 return compiler_tuple(c, e);
2994 }
2995 return 1;
2996}
2997
2998static int
2999compiler_augassign(struct compiler *c, stmt_ty s)
3000{
3001 expr_ty e = s->v.AugAssign.target;
3002 expr_ty auge;
3003
3004 assert(s->kind == AugAssign_kind);
3005
3006 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003007 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003009 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003010 if (auge == NULL)
3011 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 VISIT(c, expr, auge);
3013 VISIT(c, expr, s->v.AugAssign.value);
3014 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3015 auge->v.Attribute.ctx = AugStore;
3016 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 break;
3018 case Subscript_kind:
3019 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003020 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003021 if (auge == NULL)
3022 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 VISIT(c, expr, auge);
3024 VISIT(c, expr, s->v.AugAssign.value);
3025 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003026 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003028 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003030 if (!compiler_nameop(c, e->v.Name.id, Load))
3031 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 VISIT(c, expr, s->v.AugAssign.value);
3033 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3034 return compiler_nameop(c, e->v.Name.id, Store);
3035 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003036 PyErr_Format(PyExc_SystemError,
3037 "invalid node type (%d) for augmented assignment",
3038 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003039 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 }
3041 return 1;
3042}
3043
3044static int
3045compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3046{
3047 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003048 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3049 PyErr_SetString(PyExc_SystemError,
3050 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003052 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003053 f = &c->u->u_fblock[c->u->u_nfblocks++];
3054 f->fb_type = t;
3055 f->fb_block = b;
3056 return 1;
3057}
3058
3059static void
3060compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3061{
3062 struct compiler_unit *u = c->u;
3063 assert(u->u_nfblocks > 0);
3064 u->u_nfblocks--;
3065 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3066 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3067}
3068
Jeremy Hylton82271f12006-10-04 02:24:52 +00003069static int
3070compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003071 int i;
3072 struct compiler_unit *u = c->u;
3073 for (i = 0; i < u->u_nfblocks; ++i) {
3074 if (u->u_fblock[i].fb_type == LOOP)
3075 return 1;
3076 }
3077 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003078}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079/* Raises a SyntaxError and returns 0.
3080 If something goes wrong, a different exception may be raised.
3081*/
3082
3083static int
3084compiler_error(struct compiler *c, const char *errstr)
3085{
3086 PyObject *loc;
3087 PyObject *u = NULL, *v = NULL;
3088
3089 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3090 if (!loc) {
3091 Py_INCREF(Py_None);
3092 loc = Py_None;
3093 }
3094 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3095 Py_None, loc);
3096 if (!u)
3097 goto exit;
3098 v = Py_BuildValue("(zO)", errstr, u);
3099 if (!v)
3100 goto exit;
3101 PyErr_SetObject(PyExc_SyntaxError, v);
3102 exit:
3103 Py_DECREF(loc);
3104 Py_XDECREF(u);
3105 Py_XDECREF(v);
3106 return 0;
3107}
3108
3109static int
3110compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003111 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003113 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003115 /* XXX this code is duplicated */
3116 switch (ctx) {
3117 case AugLoad: /* fall through to Load */
3118 case Load: op = BINARY_SUBSCR; break;
3119 case AugStore:/* fall through to Store */
3120 case Store: op = STORE_SUBSCR; break;
3121 case Del: op = DELETE_SUBSCR; break;
3122 case Param:
3123 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003124 "invalid %s kind %d in subscript\n",
3125 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003126 return 0;
3127 }
3128 if (ctx == AugLoad) {
3129 ADDOP_I(c, DUP_TOPX, 2);
3130 }
3131 else if (ctx == AugStore) {
3132 ADDOP(c, ROT_THREE);
3133 }
3134 ADDOP(c, op);
3135 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136}
3137
3138static int
3139compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3140{
3141 int n = 2;
3142 assert(s->kind == Slice_kind);
3143
3144 /* only handles the cases where BUILD_SLICE is emitted */
3145 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003146 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 }
3148 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003149 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003153 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003154 }
3155 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003156 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 }
3158
3159 if (s->v.Slice.step) {
3160 n++;
3161 VISIT(c, expr, s->v.Slice.step);
3162 }
3163 ADDOP_I(c, BUILD_SLICE, n);
3164 return 1;
3165}
3166
3167static int
3168compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3169{
3170 int op = 0, slice_offset = 0, stack_count = 0;
3171
3172 assert(s->v.Slice.step == NULL);
3173 if (s->v.Slice.lower) {
3174 slice_offset++;
3175 stack_count++;
3176 if (ctx != AugStore)
3177 VISIT(c, expr, s->v.Slice.lower);
3178 }
3179 if (s->v.Slice.upper) {
3180 slice_offset += 2;
3181 stack_count++;
3182 if (ctx != AugStore)
3183 VISIT(c, expr, s->v.Slice.upper);
3184 }
3185
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003186 if (ctx == AugLoad) {
3187 switch (stack_count) {
3188 case 0: ADDOP(c, DUP_TOP); break;
3189 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3190 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3191 }
3192 }
3193 else if (ctx == AugStore) {
3194 switch (stack_count) {
3195 case 0: ADDOP(c, ROT_TWO); break;
3196 case 1: ADDOP(c, ROT_THREE); break;
3197 case 2: ADDOP(c, ROT_FOUR); break;
3198 }
3199 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200
3201 switch (ctx) {
3202 case AugLoad: /* fall through to Load */
3203 case Load: op = SLICE; break;
3204 case AugStore:/* fall through to Store */
3205 case Store: op = STORE_SLICE; break;
3206 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003207 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003208 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003209 PyErr_SetString(PyExc_SystemError,
3210 "param invalid in simple slice");
3211 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003212 }
3213
3214 ADDOP(c, op + slice_offset);
3215 return 1;
3216}
3217
3218static int
3219compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3220 expr_context_ty ctx)
3221{
3222 switch (s->kind) {
3223 case Ellipsis_kind:
3224 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3225 break;
3226 case Slice_kind:
3227 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 case Index_kind:
3229 VISIT(c, expr, s->v.Index.value);
3230 break;
3231 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003232 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003233 PyErr_SetString(PyExc_SystemError,
3234 "extended slice invalid in nested slice");
3235 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003236 }
3237 return 1;
3238}
3239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240static int
3241compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3242{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003243 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003245 case Index_kind:
3246 kindname = "index";
3247 if (ctx != AugStore) {
3248 VISIT(c, expr, s->v.Index.value);
3249 }
3250 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003252 kindname = "ellipsis";
3253 if (ctx != AugStore) {
3254 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 break;
3257 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003258 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 if (!s->v.Slice.step)
3260 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003261 if (ctx != AugStore) {
3262 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 return 0;
3264 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003265 break;
3266 case ExtSlice_kind:
3267 kindname = "extended slice";
3268 if (ctx != AugStore) {
3269 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3270 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003271 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003272 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003273 if (!compiler_visit_nested_slice(c, sub, ctx))
3274 return 0;
3275 }
3276 ADDOP_I(c, BUILD_TUPLE, n);
3277 }
3278 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003279 default:
3280 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003281 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003282 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003284 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003285}
3286
Neal Norwitzf733a012006-10-29 18:30:10 +00003287
3288/* End of the compiler section, beginning of the assembler section */
3289
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290/* do depth-first search of basic block graph, starting with block.
3291 post records the block indices in post-order.
3292
3293 XXX must handle implicit jumps from one block to next
3294*/
3295
Neal Norwitzf733a012006-10-29 18:30:10 +00003296struct assembler {
3297 PyObject *a_bytecode; /* string containing bytecode */
3298 int a_offset; /* offset into bytecode */
3299 int a_nblocks; /* number of reachable blocks */
3300 basicblock **a_postorder; /* list of blocks in dfs postorder */
3301 PyObject *a_lnotab; /* string containing lnotab */
3302 int a_lnotab_off; /* offset into lnotab */
3303 int a_lineno; /* last lineno of emitted instruction */
3304 int a_lineno_off; /* bytecode offset of last lineno */
3305};
3306
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003307static void
3308dfs(struct compiler *c, basicblock *b, struct assembler *a)
3309{
3310 int i;
3311 struct instr *instr = NULL;
3312
3313 if (b->b_seen)
3314 return;
3315 b->b_seen = 1;
3316 if (b->b_next != NULL)
3317 dfs(c, b->b_next, a);
3318 for (i = 0; i < b->b_iused; i++) {
3319 instr = &b->b_instr[i];
3320 if (instr->i_jrel || instr->i_jabs)
3321 dfs(c, instr->i_target, a);
3322 }
3323 a->a_postorder[a->a_nblocks++] = b;
3324}
3325
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003326static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3328{
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003329 int i, target_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 struct instr *instr;
3331 if (b->b_seen || b->b_startdepth >= depth)
3332 return maxdepth;
3333 b->b_seen = 1;
3334 b->b_startdepth = depth;
3335 for (i = 0; i < b->b_iused; i++) {
3336 instr = &b->b_instr[i];
3337 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3338 if (depth > maxdepth)
3339 maxdepth = depth;
3340 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3341 if (instr->i_jrel || instr->i_jabs) {
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003342 target_depth = depth;
3343 if (instr->i_opcode == FOR_ITER) {
3344 target_depth = depth-2;
3345 } else if (instr->i_opcode == SETUP_FINALLY ||
3346 instr->i_opcode == SETUP_EXCEPT) {
3347 target_depth = depth+3;
3348 if (target_depth > maxdepth)
3349 maxdepth = target_depth;
3350 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351 maxdepth = stackdepth_walk(c, instr->i_target,
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003352 target_depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 if (instr->i_opcode == JUMP_ABSOLUTE ||
3354 instr->i_opcode == JUMP_FORWARD) {
3355 goto out; /* remaining code is dead */
3356 }
3357 }
3358 }
3359 if (b->b_next)
3360 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3361out:
3362 b->b_seen = 0;
3363 return maxdepth;
3364}
3365
3366/* Find the flow path that needs the largest stack. We assume that
3367 * cycles in the flow graph have no net effect on the stack depth.
3368 */
3369static int
3370stackdepth(struct compiler *c)
3371{
3372 basicblock *b, *entryblock;
3373 entryblock = NULL;
3374 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3375 b->b_seen = 0;
3376 b->b_startdepth = INT_MIN;
3377 entryblock = b;
3378 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003379 if (!entryblock)
3380 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 return stackdepth_walk(c, entryblock, 0, 0);
3382}
3383
3384static int
3385assemble_init(struct assembler *a, int nblocks, int firstlineno)
3386{
3387 memset(a, 0, sizeof(struct assembler));
3388 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003389 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390 if (!a->a_bytecode)
3391 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003392 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003393 if (!a->a_lnotab)
3394 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003395 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3396 PyErr_NoMemory();
3397 return 0;
3398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003400 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003401 if (!a->a_postorder) {
3402 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 return 1;
3406}
3407
3408static void
3409assemble_free(struct assembler *a)
3410{
3411 Py_XDECREF(a->a_bytecode);
3412 Py_XDECREF(a->a_lnotab);
3413 if (a->a_postorder)
3414 PyObject_Free(a->a_postorder);
3415}
3416
3417/* Return the size of a basic block in bytes. */
3418
3419static int
3420instrsize(struct instr *instr)
3421{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003422 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003423 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003424 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003425 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3426 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427}
3428
3429static int
3430blocksize(basicblock *b)
3431{
3432 int i;
3433 int size = 0;
3434
3435 for (i = 0; i < b->b_iused; i++)
3436 size += instrsize(&b->b_instr[i]);
3437 return size;
3438}
3439
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003440/* Appends a pair to the end of the line number table, a_lnotab, representing
3441 the instruction's bytecode offset and line number. See
3442 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003443
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003444static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003446{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447 int d_bytecode, d_lineno;
3448 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003449 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450
3451 d_bytecode = a->a_offset - a->a_lineno_off;
3452 d_lineno = i->i_lineno - a->a_lineno;
3453
3454 assert(d_bytecode >= 0);
3455 assert(d_lineno >= 0);
3456
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003457 if(d_bytecode == 0 && d_lineno == 0)
3458 return 1;
3459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003461 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003462 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003463 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003464 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003465 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003467 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003469 else {
3470 PyErr_NoMemory();
3471 return 0;
3472 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003473 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003475 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003476 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003477 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003478 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 *lnotab++ = 255;
3480 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482 d_bytecode -= ncodes * 255;
3483 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003485 assert(d_bytecode <= 255);
3486 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003487 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003489 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003491 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003493 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003495 else {
3496 PyErr_NoMemory();
3497 return 0;
3498 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003499 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003501 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003502 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003503 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003505 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003507 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003509 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 d_lineno -= ncodes * 255;
3512 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003513 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003514
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003515 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003517 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003518 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003519 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003520 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003521 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003522
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 a->a_lnotab_off += 2;
3524 if (d_bytecode) {
3525 *lnotab++ = d_bytecode;
3526 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003527 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003528 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003529 *lnotab++ = 0;
3530 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532 a->a_lineno = i->i_lineno;
3533 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003534 return 1;
3535}
3536
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537/* assemble_emit()
3538 Extend the bytecode with a new instruction.
3539 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003540*/
3541
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003542static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003544{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003545 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003546 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547 char *code;
3548
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003549 size = instrsize(i);
3550 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003552 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003555 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003557 if (len > PY_SSIZE_T_MAX / 2)
3558 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003559 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003560 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003561 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003562 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003564 if (size == 6) {
3565 assert(i->i_hasarg);
3566 *code++ = (char)EXTENDED_ARG;
3567 *code++ = ext & 0xff;
3568 *code++ = ext >> 8;
3569 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003570 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003572 if (i->i_hasarg) {
3573 assert(size == 3 || size == 6);
3574 *code++ = arg & 0xff;
3575 *code++ = arg >> 8;
3576 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003578}
3579
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003580static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003582{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 basicblock *b;
Benjamin Petersoncef97822009-11-20 02:15:50 +00003584 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003585 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003586
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587 /* Compute the size of each block and fixup jump args.
3588 Replace block pointer with position in bytecode. */
Benjamin Petersoncef97822009-11-20 02:15:50 +00003589 do {
3590 totsize = 0;
3591 for (i = a->a_nblocks - 1; i >= 0; i--) {
3592 b = a->a_postorder[i];
3593 bsize = blocksize(b);
3594 b->b_offset = totsize;
3595 totsize += bsize;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003596 }
Benjamin Petersoncef97822009-11-20 02:15:50 +00003597 last_extended_arg_count = extended_arg_count;
3598 extended_arg_count = 0;
3599 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3600 bsize = b->b_offset;
3601 for (i = 0; i < b->b_iused; i++) {
3602 struct instr *instr = &b->b_instr[i];
3603 /* Relative jumps are computed relative to
3604 the instruction pointer after fetching
3605 the jump instruction.
3606 */
3607 bsize += instrsize(instr);
3608 if (instr->i_jabs)
3609 instr->i_oparg = instr->i_target->b_offset;
3610 else if (instr->i_jrel) {
3611 int delta = instr->i_target->b_offset - bsize;
3612 instr->i_oparg = delta;
3613 }
3614 else
3615 continue;
3616 if (instr->i_oparg > 0xffff)
3617 extended_arg_count++;
3618 }
3619 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003620
3621 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003622 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003623 with a better solution.
3624
Neal Norwitzf1d50682005-10-23 23:00:41 +00003625 The issue is that in the first loop blocksize() is called
3626 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003627 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003628 i_oparg is calculated in the second loop above.
3629
3630 So we loop until we stop seeing new EXTENDED_ARGs.
3631 The only EXTENDED_ARGs that could be popping up are
3632 ones in jump instructions. So this should converge
3633 fairly quickly.
3634 */
Benjamin Petersoncef97822009-11-20 02:15:50 +00003635 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003636}
3637
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003638static PyObject *
3639dict_keys_inorder(PyObject *dict, int offset)
3640{
3641 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003642 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003643
3644 tuple = PyTuple_New(size);
3645 if (tuple == NULL)
3646 return NULL;
3647 while (PyDict_Next(dict, &pos, &k, &v)) {
3648 i = PyInt_AS_LONG(v);
Benjamin Petersonda9327f2009-01-31 23:43:25 +00003649 /* The keys of the dictionary are tuples. (see compiler_add_o)
3650 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003651 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003652 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003653 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003654 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003655 PyTuple_SET_ITEM(tuple, i - offset, k);
3656 }
3657 return tuple;
3658}
3659
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003660static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003662{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 PySTEntryObject *ste = c->u->u_ste;
3664 int flags = 0, n;
3665 if (ste->ste_type != ModuleBlock)
3666 flags |= CO_NEWLOCALS;
3667 if (ste->ste_type == FunctionBlock) {
3668 if (!ste->ste_unoptimized)
3669 flags |= CO_OPTIMIZED;
3670 if (ste->ste_nested)
3671 flags |= CO_NESTED;
3672 if (ste->ste_generator)
3673 flags |= CO_GENERATOR;
Benjamin Peterson12554cb2009-01-31 23:54:38 +00003674 if (ste->ste_varargs)
3675 flags |= CO_VARARGS;
3676 if (ste->ste_varkeywords)
3677 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003678 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003679
3680 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003681 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 n = PyDict_Size(c->u->u_freevars);
3684 if (n < 0)
3685 return -1;
3686 if (n == 0) {
3687 n = PyDict_Size(c->u->u_cellvars);
3688 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003689 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 if (n == 0) {
3691 flags |= CO_NOFREE;
3692 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003693 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003694
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003695 return flags;
3696}
3697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003698static PyCodeObject *
3699makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003700{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701 PyObject *tmp;
3702 PyCodeObject *co = NULL;
3703 PyObject *consts = NULL;
3704 PyObject *names = NULL;
3705 PyObject *varnames = NULL;
3706 PyObject *filename = NULL;
3707 PyObject *name = NULL;
3708 PyObject *freevars = NULL;
3709 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003710 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003712
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713 tmp = dict_keys_inorder(c->u->u_consts, 0);
3714 if (!tmp)
3715 goto error;
3716 consts = PySequence_List(tmp); /* optimize_code requires a list */
3717 Py_DECREF(tmp);
3718
3719 names = dict_keys_inorder(c->u->u_names, 0);
3720 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3721 if (!consts || !names || !varnames)
3722 goto error;
3723
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003724 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3725 if (!cellvars)
3726 goto error;
3727 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3728 if (!freevars)
3729 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003730 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 if (!filename)
3732 goto error;
3733
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003734 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 flags = compute_code_flags(c);
3736 if (flags < 0)
3737 goto error;
3738
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003739 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003740 if (!bytecode)
3741 goto error;
3742
3743 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3744 if (!tmp)
3745 goto error;
3746 Py_DECREF(consts);
3747 consts = tmp;
3748
3749 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3750 bytecode, consts, names, varnames,
3751 freevars, cellvars,
3752 filename, c->u->u_name,
3753 c->u->u_firstlineno,
3754 a->a_lnotab);
3755 error:
3756 Py_XDECREF(consts);
3757 Py_XDECREF(names);
3758 Py_XDECREF(varnames);
3759 Py_XDECREF(filename);
3760 Py_XDECREF(name);
3761 Py_XDECREF(freevars);
3762 Py_XDECREF(cellvars);
3763 Py_XDECREF(bytecode);
3764 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003765}
3766
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003767
3768/* For debugging purposes only */
3769#if 0
3770static void
3771dump_instr(const struct instr *i)
3772{
3773 const char *jrel = i->i_jrel ? "jrel " : "";
3774 const char *jabs = i->i_jabs ? "jabs " : "";
3775 char arg[128];
3776
3777 *arg = '\0';
3778 if (i->i_hasarg)
3779 sprintf(arg, "arg: %d ", i->i_oparg);
3780
3781 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3782 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3783}
3784
3785static void
3786dump_basicblock(const basicblock *b)
3787{
3788 const char *seen = b->b_seen ? "seen " : "";
3789 const char *b_return = b->b_return ? "return " : "";
3790 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3791 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3792 if (b->b_instr) {
3793 int i;
3794 for (i = 0; i < b->b_iused; i++) {
3795 fprintf(stderr, " [%02d] ", i);
3796 dump_instr(b->b_instr + i);
3797 }
3798 }
3799}
3800#endif
3801
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802static PyCodeObject *
3803assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003804{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 basicblock *b, *entryblock;
3806 struct assembler a;
3807 int i, j, nblocks;
3808 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 /* Make sure every block that falls off the end returns None.
3811 XXX NEXT_BLOCK() isn't quite right, because if the last
3812 block ends with a jump or return b_next shouldn't set.
3813 */
3814 if (!c->u->u_curblock->b_return) {
3815 NEXT_BLOCK(c);
3816 if (addNone)
3817 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3818 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003819 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003820
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 nblocks = 0;
3822 entryblock = NULL;
3823 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3824 nblocks++;
3825 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003826 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003827
Neal Norwitzed657552006-07-10 00:04:44 +00003828 /* Set firstlineno if it wasn't explicitly set. */
3829 if (!c->u->u_firstlineno) {
3830 if (entryblock && entryblock->b_instr)
3831 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3832 else
3833 c->u->u_firstlineno = 1;
3834 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3836 goto error;
3837 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003838
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003839 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003840 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003841
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842 /* Emit code in reverse postorder from dfs. */
3843 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003844 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845 for (j = 0; j < b->b_iused; j++)
3846 if (!assemble_emit(&a, &b->b_instr[j]))
3847 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003848 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003849
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003850 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003852 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003854
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 co = makecode(c, &a);
3856 error:
3857 assemble_free(&a);
3858 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003859}