blob: a35cda1fe88dc1525eb2629e86b511485a5876df [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:
811 return 1-oparg;
812 case BUILD_MAP:
813 return 1;
814 case LOAD_ATTR:
815 return 0;
816 case COMPARE_OP:
817 return -1;
818 case IMPORT_NAME:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000819 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820 case IMPORT_FROM:
821 return 1;
822
823 case JUMP_FORWARD:
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000824 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
825 case JUMP_IF_FALSE_OR_POP: /* "" */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826 case JUMP_ABSOLUTE:
827 return 0;
828
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000829 case POP_JUMP_IF_FALSE:
830 case POP_JUMP_IF_TRUE:
831 return -1;
832
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833 case LOAD_GLOBAL:
834 return 1;
835
836 case CONTINUE_LOOP:
837 return 0;
838 case SETUP_LOOP:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 case SETUP_EXCEPT:
840 case SETUP_FINALLY:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000841 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842
843 case LOAD_FAST:
844 return 1;
845 case STORE_FAST:
846 return -1;
847 case DELETE_FAST:
848 return 0;
849
850 case RAISE_VARARGS:
851 return -oparg;
852#define NARGS(o) (((o) % 256) + 2*((o) / 256))
853 case CALL_FUNCTION:
854 return -NARGS(oparg);
855 case CALL_FUNCTION_VAR:
856 case CALL_FUNCTION_KW:
857 return -NARGS(oparg)-1;
858 case CALL_FUNCTION_VAR_KW:
859 return -NARGS(oparg)-2;
860#undef NARGS
861 case MAKE_FUNCTION:
862 return -oparg;
863 case BUILD_SLICE:
864 if (oparg == 3)
865 return -2;
866 else
867 return -1;
868
869 case MAKE_CLOSURE:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000870 return -oparg-1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 case LOAD_CLOSURE:
872 return 1;
873 case LOAD_DEREF:
874 return 1;
875 case STORE_DEREF:
876 return -1;
877 default:
878 fprintf(stderr, "opcode = %d\n", opcode);
879 Py_FatalError("opcode_stack_effect()");
880
881 }
882 return 0; /* not reachable */
883}
884
885/* Add an opcode with no argument.
886 Returns 0 on failure, 1 on success.
887*/
888
889static int
890compiler_addop(struct compiler *c, int opcode)
891{
892 basicblock *b;
893 struct instr *i;
894 int off;
895 off = compiler_next_instr(c, c->u->u_curblock);
896 if (off < 0)
897 return 0;
898 b = c->u->u_curblock;
899 i = &b->b_instr[off];
900 i->i_opcode = opcode;
901 i->i_hasarg = 0;
902 if (opcode == RETURN_VALUE)
903 b->b_return = 1;
904 compiler_set_lineno(c, off);
905 return 1;
906}
907
908static int
909compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
910{
911 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000912 Py_ssize_t arg;
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000913 unsigned char *p;
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);
920 p = (unsigned char*) &d;
921 /* all we need is to make the tuple different in either the 0.0
922 * or -0.0 case from all others, just to avoid the "coercion".
923 */
924 if (*p==0 && p[sizeof(double)-1]==0)
925 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
926 else
927 t = PyTuple_Pack(2, o, o->ob_type);
928 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000929#ifndef WITHOUT_COMPLEX
Mark Dickinson105be772008-01-31 22:17:37 +0000930 else if (PyComplex_Check(o)) {
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000931 Py_complex z;
932 int real_part_zero, imag_part_zero;
933 unsigned char *q;
Mark Dickinson105be772008-01-31 22:17:37 +0000934 /* complex case is even messier: we need to make complex(x,
935 0.) different from complex(x, -0.) and complex(0., y)
936 different from complex(-0., y), for any x and y. In
937 particular, all four complex zeros should be
938 distinguished.*/
939 z = PyComplex_AsCComplex(o);
940 p = (unsigned char*) &(z.real);
941 q = (unsigned char*) &(z.imag);
942 /* all that matters here is that on IEEE platforms
943 real_part_zero will be true if z.real == 0., and false if
944 z.real == -0. In fact, real_part_zero will also be true
945 for some other rarely occurring nonzero floats, but this
946 doesn't matter. Similar comments apply to
947 imag_part_zero. */
948 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
949 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
950 if (real_part_zero && imag_part_zero) {
951 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
952 }
953 else if (real_part_zero && !imag_part_zero) {
954 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
955 }
956 else if (!real_part_zero && imag_part_zero) {
957 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
958 }
959 else {
960 t = PyTuple_Pack(2, o, o->ob_type);
961 }
962 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000963#endif /* WITHOUT_COMPLEX */
Mark Dickinson105be772008-01-31 22:17:37 +0000964 else {
965 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000966 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000967 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000968 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969
970 v = PyDict_GetItem(dict, t);
971 if (!v) {
972 arg = PyDict_Size(dict);
973 v = PyInt_FromLong(arg);
974 if (!v) {
975 Py_DECREF(t);
976 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 if (PyDict_SetItem(dict, t, v) < 0) {
979 Py_DECREF(t);
980 Py_DECREF(v);
981 return -1;
982 }
983 Py_DECREF(v);
984 }
985 else
986 arg = PyInt_AsLong(v);
987 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000988 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989}
990
991static int
992compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
993 PyObject *o)
994{
995 int arg = compiler_add_o(c, dict, o);
996 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000997 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return compiler_addop_i(c, opcode, arg);
999}
1000
1001static int
1002compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001003 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004{
1005 int arg;
1006 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1007 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 arg = compiler_add_o(c, dict, mangled);
1010 Py_DECREF(mangled);
1011 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001012 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return compiler_addop_i(c, opcode, arg);
1014}
1015
1016/* Add an opcode with an integer argument.
1017 Returns 0 on failure, 1 on success.
1018*/
1019
1020static int
1021compiler_addop_i(struct compiler *c, int opcode, int oparg)
1022{
1023 struct instr *i;
1024 int off;
1025 off = compiler_next_instr(c, c->u->u_curblock);
1026 if (off < 0)
1027 return 0;
1028 i = &c->u->u_curblock->b_instr[off];
1029 i->i_opcode = opcode;
1030 i->i_oparg = oparg;
1031 i->i_hasarg = 1;
1032 compiler_set_lineno(c, off);
1033 return 1;
1034}
1035
1036static int
1037compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1038{
1039 struct instr *i;
1040 int off;
1041
1042 assert(b != NULL);
1043 off = compiler_next_instr(c, c->u->u_curblock);
1044 if (off < 0)
1045 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 i = &c->u->u_curblock->b_instr[off];
1047 i->i_opcode = opcode;
1048 i->i_target = b;
1049 i->i_hasarg = 1;
1050 if (absolute)
1051 i->i_jabs = 1;
1052 else
1053 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001054 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 return 1;
1056}
1057
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001058/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1059 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 it as the current block. NEXT_BLOCK() also creates an implicit jump
1061 from the current block to the new block.
1062*/
1063
Neal Norwitzf733a012006-10-29 18:30:10 +00001064/* The returns inside these macros make it impossible to decref objects
1065 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066*/
1067
1068
1069#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001070 if (compiler_use_new_block((C)) == NULL) \
1071 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
1073
1074#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001075 if (compiler_next_block((C)) == NULL) \
1076 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
1079#define ADDOP(C, OP) { \
1080 if (!compiler_addop((C), (OP))) \
1081 return 0; \
1082}
1083
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001084#define ADDOP_IN_SCOPE(C, OP) { \
1085 if (!compiler_addop((C), (OP))) { \
1086 compiler_exit_scope(c); \
1087 return 0; \
1088 } \
1089}
1090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091#define ADDOP_O(C, OP, O, TYPE) { \
1092 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1093 return 0; \
1094}
1095
1096#define ADDOP_NAME(C, OP, O, TYPE) { \
1097 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1098 return 0; \
1099}
1100
1101#define ADDOP_I(C, OP, O) { \
1102 if (!compiler_addop_i((C), (OP), (O))) \
1103 return 0; \
1104}
1105
1106#define ADDOP_JABS(C, OP, O) { \
1107 if (!compiler_addop_j((C), (OP), (O), 1)) \
1108 return 0; \
1109}
1110
1111#define ADDOP_JREL(C, OP, O) { \
1112 if (!compiler_addop_j((C), (OP), (O), 0)) \
1113 return 0; \
1114}
1115
1116/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1117 the ASDL name to synthesize the name of the C type and the visit function.
1118*/
1119
1120#define VISIT(C, TYPE, V) {\
1121 if (!compiler_visit_ ## TYPE((C), (V))) \
1122 return 0; \
1123}
1124
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001125#define VISIT_IN_SCOPE(C, TYPE, V) {\
1126 if (!compiler_visit_ ## TYPE((C), (V))) { \
1127 compiler_exit_scope(c); \
1128 return 0; \
1129 } \
1130}
1131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132#define VISIT_SLICE(C, V, CTX) {\
1133 if (!compiler_visit_slice((C), (V), (CTX))) \
1134 return 0; \
1135}
1136
1137#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001138 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +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 return 0; \
1144 } \
1145}
1146
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001147#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001148 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001149 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001150 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001151 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001152 if (!compiler_visit_ ## TYPE((C), elt)) { \
1153 compiler_exit_scope(c); \
1154 return 0; \
1155 } \
1156 } \
1157}
1158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159static int
1160compiler_isdocstring(stmt_ty s)
1161{
1162 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001163 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return s->v.Expr.value->kind == Str_kind;
1165}
1166
1167/* Compile a sequence of statements, checking for a docstring. */
1168
1169static int
1170compiler_body(struct compiler *c, asdl_seq *stmts)
1171{
1172 int i = 0;
1173 stmt_ty st;
1174
1175 if (!asdl_seq_LEN(stmts))
1176 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001177 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001178 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1179 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 i = 1;
1181 VISIT(c, expr, st->v.Expr.value);
1182 if (!compiler_nameop(c, __doc__, Store))
1183 return 0;
1184 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001185 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001186 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return 1;
1188}
1189
1190static PyCodeObject *
1191compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001192{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001194 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 static PyObject *module;
1196 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001197 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 if (!module)
1199 return NULL;
1200 }
Neal Norwitzed657552006-07-10 00:04:44 +00001201 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1202 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 switch (mod->kind) {
1205 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001206 if (!compiler_body(c, mod->v.Module.body)) {
1207 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 break;
1211 case Interactive_kind:
1212 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001213 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001214 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 break;
1216 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001217 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001218 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 break;
1220 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001221 PyErr_SetString(PyExc_SystemError,
1222 "suite should not be possible");
1223 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001224 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001225 PyErr_Format(PyExc_SystemError,
1226 "module kind %d should not be possible",
1227 mod->kind);
1228 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001229 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 co = assemble(c, addNone);
1231 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001232 return co;
1233}
1234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235/* The test for LOCAL must come before the test for FREE in order to
1236 handle classes where name is both local and free. The local var is
1237 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001238*/
1239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240static int
1241get_ref_type(struct compiler *c, PyObject *name)
1242{
1243 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001244 if (scope == 0) {
1245 char buf[350];
1246 PyOS_snprintf(buf, sizeof(buf),
1247 "unknown scope for %.100s in %.100s(%s) in %s\n"
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00001248 "symbols: %s\nlocals: %s\nglobals: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001249 PyString_AS_STRING(name),
1250 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001251 PyObject_REPR(c->u->u_ste->ste_id),
1252 c->c_filename,
1253 PyObject_REPR(c->u->u_ste->ste_symbols),
1254 PyObject_REPR(c->u->u_varnames),
1255 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001257 Py_FatalError(buf);
1258 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001259
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001260 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261}
1262
1263static int
1264compiler_lookup_arg(PyObject *dict, PyObject *name)
1265{
1266 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001267 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001271 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001273 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return PyInt_AS_LONG(v);
1275}
1276
1277static int
1278compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1279{
1280 int i, free = PyCode_GetNumFree(co);
1281 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001282 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1283 ADDOP_I(c, MAKE_FUNCTION, args);
1284 return 1;
1285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 for (i = 0; i < free; ++i) {
1287 /* Bypass com_addop_varname because it will generate
1288 LOAD_DEREF but LOAD_CLOSURE is needed.
1289 */
1290 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1291 int arg, reftype;
1292
1293 /* Special case: If a class contains a method with a
1294 free variable that has the same name as a method,
1295 the name will be considered free *and* local in the
1296 class. It should be handled by the closure, as
1297 well as by the normal name loookup logic.
1298 */
1299 reftype = get_ref_type(c, name);
1300 if (reftype == CELL)
1301 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1302 else /* (reftype == FREE) */
1303 arg = compiler_lookup_arg(c->u->u_freevars, name);
1304 if (arg == -1) {
1305 printf("lookup %s in %s %d %d\n"
1306 "freevars of %s: %s\n",
1307 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001308 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001310 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 PyObject_REPR(co->co_freevars));
1312 Py_FatalError("compiler_make_closure()");
1313 }
1314 ADDOP_I(c, LOAD_CLOSURE, arg);
1315 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001316 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001318 ADDOP_I(c, MAKE_CLOSURE, args);
1319 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
1322static int
1323compiler_decorators(struct compiler *c, asdl_seq* decos)
1324{
1325 int i;
1326
1327 if (!decos)
1328 return 1;
1329
1330 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001331 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 }
1333 return 1;
1334}
1335
1336static int
1337compiler_arguments(struct compiler *c, arguments_ty args)
1338{
1339 int i;
1340 int n = asdl_seq_LEN(args->args);
1341 /* Correctly handle nested argument lists */
1342 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001343 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001345 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 if (id == NULL) {
1347 return 0;
1348 }
1349 if (!compiler_nameop(c, id, Load)) {
1350 Py_DECREF(id);
1351 return 0;
1352 }
1353 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001354 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 }
1356 }
1357 return 1;
1358}
1359
1360static int
1361compiler_function(struct compiler *c, stmt_ty s)
1362{
1363 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001364 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001366 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001367 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 int i, n, docstring;
1369
1370 assert(s->kind == FunctionDef_kind);
1371
1372 if (!compiler_decorators(c, decos))
1373 return 0;
1374 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001375 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1377 s->lineno))
1378 return 0;
1379
Anthony Baxter7b782b62006-04-11 12:01:56 +00001380 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001381 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001382 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001383 first_const = st->v.Expr.value->v.Str.s;
1384 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001385 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001386 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001389 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 compiler_arguments(c, args);
1391
1392 c->u->u_argcount = asdl_seq_LEN(args->args);
1393 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001394 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001396 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1397 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 }
1399 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001400 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 if (co == NULL)
1402 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001404 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001405 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406
1407 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1408 ADDOP_I(c, CALL_FUNCTION, 1);
1409 }
1410
1411 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1412}
1413
1414static int
1415compiler_class(struct compiler *c, stmt_ty s)
1416{
Christian Heimes5224d282008-02-23 15:01:05 +00001417 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001419 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001420 asdl_seq* decos = s->v.ClassDef.decorator_list;
1421
1422 if (!compiler_decorators(c, decos))
1423 return 0;
1424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 /* push class name on stack, needed by BUILD_CLASS */
1426 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1427 /* push the tuple of base classes on the stack */
1428 n = asdl_seq_LEN(s->v.ClassDef.bases);
1429 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001430 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 ADDOP_I(c, BUILD_TUPLE, n);
1432 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1433 s->lineno))
1434 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001435 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001436 c->u->u_private = s->v.ClassDef.name;
1437 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001438 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 if (!str || !compiler_nameop(c, str, Load)) {
1440 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001441 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001443 }
1444
1445 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001446 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 if (!str || !compiler_nameop(c, str, Store)) {
1448 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001449 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001451 }
1452 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001454 if (!compiler_body(c, s->v.ClassDef.body)) {
1455 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001457 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001459 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1460 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001462 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 if (co == NULL)
1464 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001466 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001467 Py_DECREF(co);
1468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 ADDOP_I(c, CALL_FUNCTION, 0);
1470 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001471 /* apply decorators */
1472 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1473 ADDOP_I(c, CALL_FUNCTION, 1);
1474 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1476 return 0;
1477 return 1;
1478}
1479
1480static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001481compiler_ifexp(struct compiler *c, expr_ty e)
1482{
1483 basicblock *end, *next;
1484
1485 assert(e->kind == IfExp_kind);
1486 end = compiler_new_block(c);
1487 if (end == NULL)
1488 return 0;
1489 next = compiler_new_block(c);
1490 if (next == NULL)
1491 return 0;
1492 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001493 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001494 VISIT(c, expr, e->v.IfExp.body);
1495 ADDOP_JREL(c, JUMP_FORWARD, end);
1496 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001497 VISIT(c, expr, e->v.IfExp.orelse);
1498 compiler_use_next_block(c, end);
1499 return 1;
1500}
1501
1502static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503compiler_lambda(struct compiler *c, expr_ty e)
1504{
1505 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001506 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 arguments_ty args = e->v.Lambda.args;
1508 assert(e->kind == Lambda_kind);
1509
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001510 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001511 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001512 if (!name)
1513 return 0;
1514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515
1516 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001517 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1519 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001520
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001521 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 compiler_arguments(c, args);
1523
1524 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001525 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson8d5934b2008-12-27 18:24:11 +00001526 if (c->u->u_ste->ste_generator) {
1527 ADDOP_IN_SCOPE(c, POP_TOP);
1528 }
1529 else {
1530 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1531 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001532 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001533 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534 if (co == NULL)
1535 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001536
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001537 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001538 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539
1540 return 1;
1541}
1542
1543static int
1544compiler_print(struct compiler *c, stmt_ty s)
1545{
1546 int i, n;
1547 bool dest;
1548
1549 assert(s->kind == Print_kind);
1550 n = asdl_seq_LEN(s->v.Print.values);
1551 dest = false;
1552 if (s->v.Print.dest) {
1553 VISIT(c, expr, s->v.Print.dest);
1554 dest = true;
1555 }
1556 for (i = 0; i < n; i++) {
1557 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1558 if (dest) {
1559 ADDOP(c, DUP_TOP);
1560 VISIT(c, expr, e);
1561 ADDOP(c, ROT_TWO);
1562 ADDOP(c, PRINT_ITEM_TO);
1563 }
1564 else {
1565 VISIT(c, expr, e);
1566 ADDOP(c, PRINT_ITEM);
1567 }
1568 }
1569 if (s->v.Print.nl) {
1570 if (dest)
1571 ADDOP(c, PRINT_NEWLINE_TO)
1572 else
1573 ADDOP(c, PRINT_NEWLINE)
1574 }
1575 else if (dest)
1576 ADDOP(c, POP_TOP);
1577 return 1;
1578}
1579
1580static int
1581compiler_if(struct compiler *c, stmt_ty s)
1582{
1583 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001584 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001585 assert(s->kind == If_kind);
1586 end = compiler_new_block(c);
1587 if (end == NULL)
1588 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001589
1590 constant = expr_constant(s->v.If.test);
1591 /* constant = 0: "if 0"
1592 * constant = 1: "if 1", "if 2", ...
1593 * constant = -1: rest */
1594 if (constant == 0) {
1595 if (s->v.If.orelse)
1596 VISIT_SEQ(c, stmt, s->v.If.orelse);
1597 } else if (constant == 1) {
1598 VISIT_SEQ(c, stmt, s->v.If.body);
1599 } else {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001600 if (s->v.If.orelse) {
1601 next = compiler_new_block(c);
1602 if (next == NULL)
1603 return 0;
1604 }
1605 else
1606 next = end;
Georg Brandlddbaa662006-06-04 21:56:52 +00001607 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001608 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Georg Brandlddbaa662006-06-04 21:56:52 +00001609 VISIT_SEQ(c, stmt, s->v.If.body);
1610 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001611 if (s->v.If.orelse) {
1612 compiler_use_next_block(c, next);
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001613 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001614 }
Georg Brandlddbaa662006-06-04 21:56:52 +00001615 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001616 compiler_use_next_block(c, end);
1617 return 1;
1618}
1619
1620static int
1621compiler_for(struct compiler *c, stmt_ty s)
1622{
1623 basicblock *start, *cleanup, *end;
1624
1625 start = compiler_new_block(c);
1626 cleanup = compiler_new_block(c);
1627 end = compiler_new_block(c);
1628 if (start == NULL || end == NULL || cleanup == NULL)
1629 return 0;
1630 ADDOP_JREL(c, SETUP_LOOP, end);
1631 if (!compiler_push_fblock(c, LOOP, start))
1632 return 0;
1633 VISIT(c, expr, s->v.For.iter);
1634 ADDOP(c, GET_ITER);
1635 compiler_use_next_block(c, start);
1636 ADDOP_JREL(c, FOR_ITER, cleanup);
1637 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001638 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1640 compiler_use_next_block(c, cleanup);
1641 ADDOP(c, POP_BLOCK);
1642 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001643 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644 compiler_use_next_block(c, end);
1645 return 1;
1646}
1647
1648static int
1649compiler_while(struct compiler *c, stmt_ty s)
1650{
1651 basicblock *loop, *orelse, *end, *anchor = NULL;
1652 int constant = expr_constant(s->v.While.test);
1653
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001654 if (constant == 0) {
1655 if (s->v.While.orelse)
1656 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001658 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659 loop = compiler_new_block(c);
1660 end = compiler_new_block(c);
1661 if (constant == -1) {
1662 anchor = compiler_new_block(c);
1663 if (anchor == NULL)
1664 return 0;
1665 }
1666 if (loop == NULL || end == NULL)
1667 return 0;
1668 if (s->v.While.orelse) {
1669 orelse = compiler_new_block(c);
1670 if (orelse == NULL)
1671 return 0;
1672 }
1673 else
1674 orelse = NULL;
1675
1676 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001677 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678 if (!compiler_push_fblock(c, LOOP, loop))
1679 return 0;
1680 if (constant == -1) {
1681 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001682 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001684 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1686
1687 /* XXX should the two POP instructions be in a separate block
1688 if there is no else clause ?
1689 */
1690
1691 if (constant == -1) {
1692 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693 ADDOP(c, POP_BLOCK);
1694 }
1695 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001696 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001697 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 compiler_use_next_block(c, end);
1699
1700 return 1;
1701}
1702
1703static int
1704compiler_continue(struct compiler *c)
1705{
1706 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001707 static const char IN_FINALLY_ERROR_MSG[] =
1708 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001709 int i;
1710
1711 if (!c->u->u_nfblocks)
1712 return compiler_error(c, LOOP_ERROR_MSG);
1713 i = c->u->u_nfblocks - 1;
1714 switch (c->u->u_fblock[i].fb_type) {
1715 case LOOP:
1716 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1717 break;
1718 case EXCEPT:
1719 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001720 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1721 /* Prevent continue anywhere under a finally
1722 even if hidden in a sub-try or except. */
1723 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1724 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1725 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001726 if (i == -1)
1727 return compiler_error(c, LOOP_ERROR_MSG);
1728 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1729 break;
1730 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001731 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001732 }
1733
1734 return 1;
1735}
1736
1737/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1738
1739 SETUP_FINALLY L
1740 <code for body>
1741 POP_BLOCK
1742 LOAD_CONST <None>
1743 L: <code for finalbody>
1744 END_FINALLY
1745
1746 The special instructions use the block stack. Each block
1747 stack entry contains the instruction that created it (here
1748 SETUP_FINALLY), the level of the value stack at the time the
1749 block stack entry was created, and a label (here L).
1750
1751 SETUP_FINALLY:
1752 Pushes the current value stack level and the label
1753 onto the block stack.
1754 POP_BLOCK:
1755 Pops en entry from the block stack, and pops the value
1756 stack until its level is the same as indicated on the
1757 block stack. (The label is ignored.)
1758 END_FINALLY:
1759 Pops a variable number of entries from the *value* stack
1760 and re-raises the exception they specify. The number of
1761 entries popped depends on the (pseudo) exception type.
1762
1763 The block stack is unwound when an exception is raised:
1764 when a SETUP_FINALLY entry is found, the exception is pushed
1765 onto the value stack (and the exception condition is cleared),
1766 and the interpreter jumps to the label gotten from the block
1767 stack.
1768*/
1769
1770static int
1771compiler_try_finally(struct compiler *c, stmt_ty s)
1772{
1773 basicblock *body, *end;
1774 body = compiler_new_block(c);
1775 end = compiler_new_block(c);
1776 if (body == NULL || end == NULL)
1777 return 0;
1778
1779 ADDOP_JREL(c, SETUP_FINALLY, end);
1780 compiler_use_next_block(c, body);
1781 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1782 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001783 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784 ADDOP(c, POP_BLOCK);
1785 compiler_pop_fblock(c, FINALLY_TRY, body);
1786
1787 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1788 compiler_use_next_block(c, end);
1789 if (!compiler_push_fblock(c, FINALLY_END, end))
1790 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001791 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001792 ADDOP(c, END_FINALLY);
1793 compiler_pop_fblock(c, FINALLY_END, end);
1794
1795 return 1;
1796}
1797
1798/*
1799 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1800 (The contents of the value stack is shown in [], with the top
1801 at the right; 'tb' is trace-back info, 'val' the exception's
1802 associated value, and 'exc' the exception.)
1803
1804 Value stack Label Instruction Argument
1805 [] SETUP_EXCEPT L1
1806 [] <code for S>
1807 [] POP_BLOCK
1808 [] JUMP_FORWARD L0
1809
1810 [tb, val, exc] L1: DUP )
1811 [tb, val, exc, exc] <evaluate E1> )
1812 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001813 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 [tb, val, exc] POP
1815 [tb, val] <assign to V1> (or POP if no V1)
1816 [tb] POP
1817 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001818 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001820 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001821 .............................etc.......................
1822
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001823 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824
1825 [] L0: <next statement>
1826
1827 Of course, parts are not generated if Vi or Ei is not present.
1828*/
1829static int
1830compiler_try_except(struct compiler *c, stmt_ty s)
1831{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001832 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001833 int i, n;
1834
1835 body = compiler_new_block(c);
1836 except = compiler_new_block(c);
1837 orelse = compiler_new_block(c);
1838 end = compiler_new_block(c);
1839 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1840 return 0;
1841 ADDOP_JREL(c, SETUP_EXCEPT, except);
1842 compiler_use_next_block(c, body);
1843 if (!compiler_push_fblock(c, EXCEPT, body))
1844 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001845 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 ADDOP(c, POP_BLOCK);
1847 compiler_pop_fblock(c, EXCEPT, body);
1848 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1849 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1850 compiler_use_next_block(c, except);
1851 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001852 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001854 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001855 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001856 c->u->u_lineno_set = false;
1857 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 except = compiler_new_block(c);
1859 if (except == NULL)
1860 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001861 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001863 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001865 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866 }
1867 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001868 if (handler->v.ExceptHandler.name) {
1869 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 }
1871 else {
1872 ADDOP(c, POP_TOP);
1873 }
1874 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001875 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 ADDOP_JREL(c, JUMP_FORWARD, end);
1877 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878 }
1879 ADDOP(c, END_FINALLY);
1880 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001881 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 compiler_use_next_block(c, end);
1883 return 1;
1884}
1885
1886static int
1887compiler_import_as(struct compiler *c, identifier name, identifier asname)
1888{
1889 /* The IMPORT_NAME opcode was already generated. This function
1890 merely needs to bind the result to a name.
1891
1892 If there is a dot in name, we need to split it and emit a
1893 LOAD_ATTR for each name.
1894 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001895 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896 const char *dot = strchr(src, '.');
1897 if (dot) {
1898 /* Consume the base module name to get the first attribute */
1899 src = dot + 1;
1900 while (dot) {
1901 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001902 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001903 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001904 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001906 if (!attr)
1907 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001909 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 src = dot + 1;
1911 }
1912 }
1913 return compiler_nameop(c, asname, Store);
1914}
1915
1916static int
1917compiler_import(struct compiler *c, stmt_ty s)
1918{
1919 /* The Import node stores a module name like a.b.c as a single
1920 string. This is convenient for all cases except
1921 import a.b.c as d
1922 where we need to parse that string to extract the individual
1923 module names.
1924 XXX Perhaps change the representation to make this case simpler?
1925 */
1926 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001927
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001929 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001931 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932
Neal Norwitzcbce2802006-04-03 06:26:32 +00001933 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001934 level = PyInt_FromLong(0);
1935 else
1936 level = PyInt_FromLong(-1);
1937
1938 if (level == NULL)
1939 return 0;
1940
1941 ADDOP_O(c, LOAD_CONST, level, consts);
1942 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1944 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1945
1946 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001947 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001948 if (!r)
1949 return r;
1950 }
1951 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001953 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 char *dot = strchr(base, '.');
1955 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001956 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 dot - base);
1958 r = compiler_nameop(c, tmp, Store);
1959 if (dot) {
1960 Py_DECREF(tmp);
1961 }
1962 if (!r)
1963 return r;
1964 }
1965 }
1966 return 1;
1967}
1968
1969static int
1970compiler_from_import(struct compiler *c, stmt_ty s)
1971{
1972 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973
1974 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001975 PyObject *level;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00001976 static PyObject *empty_string;
1977
1978 if (!empty_string) {
1979 empty_string = PyString_FromString("");
1980 if (!empty_string)
1981 return 0;
1982 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001983
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 if (!names)
1985 return 0;
1986
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001987 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001988 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001989 level = PyInt_FromLong(-1);
1990 else
1991 level = PyInt_FromLong(s->v.ImportFrom.level);
1992
1993 if (!level) {
1994 Py_DECREF(names);
1995 return 0;
1996 }
1997
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001998 /* build up the names */
1999 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002000 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 Py_INCREF(alias->name);
2002 PyTuple_SET_ITEM(names, i, alias->name);
2003 }
2004
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002005 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2006 !strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {
2007 Py_DECREF(level);
2008 Py_DECREF(names);
2009 return compiler_error(c, "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002010 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 }
2012
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002013 ADDOP_O(c, LOAD_CONST, level, consts);
2014 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002015 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002016 Py_DECREF(names);
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002017 if (s->v.ImportFrom.module) {
2018 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2019 }
2020 else {
2021 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2022 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002024 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002025 identifier store_name;
2026
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002027 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 assert(n == 1);
2029 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002030 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031 }
2032
2033 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2034 store_name = alias->name;
2035 if (alias->asname)
2036 store_name = alias->asname;
2037
2038 if (!compiler_nameop(c, store_name, Store)) {
2039 Py_DECREF(names);
2040 return 0;
2041 }
2042 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002043 /* remove imported module */
2044 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 return 1;
2046}
2047
2048static int
2049compiler_assert(struct compiler *c, stmt_ty s)
2050{
2051 static PyObject *assertion_error = NULL;
2052 basicblock *end;
2053
2054 if (Py_OptimizeFlag)
2055 return 1;
2056 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002057 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 if (assertion_error == NULL)
2059 return 0;
2060 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002061 if (s->v.Assert.test->kind == Tuple_kind &&
2062 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2063 const char* msg =
2064 "assertion is always true, perhaps remove parentheses?";
2065 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2066 c->u->u_lineno, NULL, NULL) == -1)
2067 return 0;
2068 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069 VISIT(c, expr, s->v.Assert.test);
2070 end = compiler_new_block(c);
2071 if (end == NULL)
2072 return 0;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002073 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2075 if (s->v.Assert.msg) {
2076 VISIT(c, expr, s->v.Assert.msg);
2077 ADDOP_I(c, RAISE_VARARGS, 2);
2078 }
2079 else {
2080 ADDOP_I(c, RAISE_VARARGS, 1);
2081 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002082 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 return 1;
2084}
2085
2086static int
2087compiler_visit_stmt(struct compiler *c, stmt_ty s)
2088{
2089 int i, n;
2090
Neal Norwitzf733a012006-10-29 18:30:10 +00002091 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 c->u->u_lineno = s->lineno;
2093 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002094
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002095 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002096 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002098 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002099 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002100 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 if (c->u->u_ste->ste_type != FunctionBlock)
2102 return compiler_error(c, "'return' outside function");
2103 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 VISIT(c, expr, s->v.Return.value);
2105 }
2106 else
2107 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2108 ADDOP(c, RETURN_VALUE);
2109 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002110 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002111 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002113 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114 n = asdl_seq_LEN(s->v.Assign.targets);
2115 VISIT(c, expr, s->v.Assign.value);
2116 for (i = 0; i < n; i++) {
2117 if (i < n - 1)
2118 ADDOP(c, DUP_TOP);
2119 VISIT(c, expr,
2120 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2121 }
2122 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002123 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002125 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002127 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002128 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002129 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002131 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002133 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 n = 0;
2135 if (s->v.Raise.type) {
2136 VISIT(c, expr, s->v.Raise.type);
2137 n++;
2138 if (s->v.Raise.inst) {
2139 VISIT(c, expr, s->v.Raise.inst);
2140 n++;
2141 if (s->v.Raise.tback) {
2142 VISIT(c, expr, s->v.Raise.tback);
2143 n++;
2144 }
2145 }
2146 }
2147 ADDOP_I(c, RAISE_VARARGS, n);
2148 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002149 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002150 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002151 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002152 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002153 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002155 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002157 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002159 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002160 VISIT(c, expr, s->v.Exec.body);
2161 if (s->v.Exec.globals) {
2162 VISIT(c, expr, s->v.Exec.globals);
2163 if (s->v.Exec.locals) {
2164 VISIT(c, expr, s->v.Exec.locals);
2165 } else {
2166 ADDOP(c, DUP_TOP);
2167 }
2168 } else {
2169 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2170 ADDOP(c, DUP_TOP);
2171 }
2172 ADDOP(c, EXEC_STMT);
2173 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002174 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002175 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002176 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002178 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179 ADDOP(c, PRINT_EXPR);
2180 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002181 else if (s->v.Expr.value->kind != Str_kind &&
2182 s->v.Expr.value->kind != Num_kind) {
2183 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 ADDOP(c, POP_TOP);
2185 }
2186 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002187 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002189 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002190 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 return compiler_error(c, "'break' outside loop");
2192 ADDOP(c, BREAK_LOOP);
2193 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002194 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002196 case With_kind:
2197 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 }
2199 return 1;
2200}
2201
2202static int
2203unaryop(unaryop_ty op)
2204{
2205 switch (op) {
2206 case Invert:
2207 return UNARY_INVERT;
2208 case Not:
2209 return UNARY_NOT;
2210 case UAdd:
2211 return UNARY_POSITIVE;
2212 case USub:
2213 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002214 default:
2215 PyErr_Format(PyExc_SystemError,
2216 "unary op %d should not be possible", op);
2217 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002218 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219}
2220
2221static int
2222binop(struct compiler *c, operator_ty op)
2223{
2224 switch (op) {
2225 case Add:
2226 return BINARY_ADD;
2227 case Sub:
2228 return BINARY_SUBTRACT;
2229 case Mult:
2230 return BINARY_MULTIPLY;
2231 case Div:
2232 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2233 return BINARY_TRUE_DIVIDE;
2234 else
2235 return BINARY_DIVIDE;
2236 case Mod:
2237 return BINARY_MODULO;
2238 case Pow:
2239 return BINARY_POWER;
2240 case LShift:
2241 return BINARY_LSHIFT;
2242 case RShift:
2243 return BINARY_RSHIFT;
2244 case BitOr:
2245 return BINARY_OR;
2246 case BitXor:
2247 return BINARY_XOR;
2248 case BitAnd:
2249 return BINARY_AND;
2250 case FloorDiv:
2251 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002252 default:
2253 PyErr_Format(PyExc_SystemError,
2254 "binary op %d should not be possible", op);
2255 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257}
2258
2259static int
2260cmpop(cmpop_ty op)
2261{
2262 switch (op) {
2263 case Eq:
2264 return PyCmp_EQ;
2265 case NotEq:
2266 return PyCmp_NE;
2267 case Lt:
2268 return PyCmp_LT;
2269 case LtE:
2270 return PyCmp_LE;
2271 case Gt:
2272 return PyCmp_GT;
2273 case GtE:
2274 return PyCmp_GE;
2275 case Is:
2276 return PyCmp_IS;
2277 case IsNot:
2278 return PyCmp_IS_NOT;
2279 case In:
2280 return PyCmp_IN;
2281 case NotIn:
2282 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002283 default:
2284 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286}
2287
2288static int
2289inplace_binop(struct compiler *c, operator_ty op)
2290{
2291 switch (op) {
2292 case Add:
2293 return INPLACE_ADD;
2294 case Sub:
2295 return INPLACE_SUBTRACT;
2296 case Mult:
2297 return INPLACE_MULTIPLY;
2298 case Div:
2299 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2300 return INPLACE_TRUE_DIVIDE;
2301 else
2302 return INPLACE_DIVIDE;
2303 case Mod:
2304 return INPLACE_MODULO;
2305 case Pow:
2306 return INPLACE_POWER;
2307 case LShift:
2308 return INPLACE_LSHIFT;
2309 case RShift:
2310 return INPLACE_RSHIFT;
2311 case BitOr:
2312 return INPLACE_OR;
2313 case BitXor:
2314 return INPLACE_XOR;
2315 case BitAnd:
2316 return INPLACE_AND;
2317 case FloorDiv:
2318 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002319 default:
2320 PyErr_Format(PyExc_SystemError,
2321 "inplace binary op %d should not be possible", op);
2322 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324}
2325
2326static int
2327compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2328{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002329 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002330 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2331
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002332 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002333 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334 /* XXX AugStore isn't used anywhere! */
2335
Neal Norwitz0031ff32008-02-25 01:45:37 +00002336 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002337 if (!mangled)
2338 return 0;
2339
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 op = 0;
2341 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002342 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 switch (scope) {
2344 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002345 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 optype = OP_DEREF;
2347 break;
2348 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002349 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002350 optype = OP_DEREF;
2351 break;
2352 case LOCAL:
2353 if (c->u->u_ste->ste_type == FunctionBlock)
2354 optype = OP_FAST;
2355 break;
2356 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002357 if (c->u->u_ste->ste_type == FunctionBlock &&
2358 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002359 optype = OP_GLOBAL;
2360 break;
2361 case GLOBAL_EXPLICIT:
2362 optype = OP_GLOBAL;
2363 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002364 default:
2365 /* scope can be 0 */
2366 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367 }
2368
2369 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002370 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371
2372 switch (optype) {
2373 case OP_DEREF:
2374 switch (ctx) {
2375 case Load: op = LOAD_DEREF; break;
2376 case Store: op = STORE_DEREF; break;
2377 case AugLoad:
2378 case AugStore:
2379 break;
2380 case Del:
2381 PyErr_Format(PyExc_SyntaxError,
2382 "can not delete variable '%s' referenced "
2383 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002384 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002385 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002386 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002388 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002389 PyErr_SetString(PyExc_SystemError,
2390 "param invalid for deref variable");
2391 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 }
2393 break;
2394 case OP_FAST:
2395 switch (ctx) {
2396 case Load: op = LOAD_FAST; break;
2397 case Store: op = STORE_FAST; break;
2398 case Del: op = DELETE_FAST; break;
2399 case AugLoad:
2400 case AugStore:
2401 break;
2402 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002403 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002404 PyErr_SetString(PyExc_SystemError,
2405 "param invalid for local variable");
2406 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002407 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002408 ADDOP_O(c, op, mangled, varnames);
2409 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410 return 1;
2411 case OP_GLOBAL:
2412 switch (ctx) {
2413 case Load: op = LOAD_GLOBAL; break;
2414 case Store: op = STORE_GLOBAL; break;
2415 case Del: op = DELETE_GLOBAL; break;
2416 case AugLoad:
2417 case AugStore:
2418 break;
2419 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002420 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002421 PyErr_SetString(PyExc_SystemError,
2422 "param invalid for global variable");
2423 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 }
2425 break;
2426 case OP_NAME:
2427 switch (ctx) {
2428 case Load: op = LOAD_NAME; break;
2429 case Store: op = STORE_NAME; break;
2430 case Del: op = DELETE_NAME; break;
2431 case AugLoad:
2432 case AugStore:
2433 break;
2434 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002435 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002436 PyErr_SetString(PyExc_SystemError,
2437 "param invalid for name variable");
2438 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439 }
2440 break;
2441 }
2442
2443 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002444 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002445 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002446 if (arg < 0)
2447 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002448 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449}
2450
2451static int
2452compiler_boolop(struct compiler *c, expr_ty e)
2453{
2454 basicblock *end;
2455 int jumpi, i, n;
2456 asdl_seq *s;
2457
2458 assert(e->kind == BoolOp_kind);
2459 if (e->v.BoolOp.op == And)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002460 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002461 else
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002462 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002464 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 return 0;
2466 s = e->v.BoolOp.values;
2467 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002468 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002470 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002471 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002473 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 compiler_use_next_block(c, end);
2475 return 1;
2476}
2477
2478static int
2479compiler_list(struct compiler *c, expr_ty e)
2480{
2481 int n = asdl_seq_LEN(e->v.List.elts);
2482 if (e->v.List.ctx == Store) {
2483 ADDOP_I(c, UNPACK_SEQUENCE, n);
2484 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002485 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 if (e->v.List.ctx == Load) {
2487 ADDOP_I(c, BUILD_LIST, n);
2488 }
2489 return 1;
2490}
2491
2492static int
2493compiler_tuple(struct compiler *c, expr_ty e)
2494{
2495 int n = asdl_seq_LEN(e->v.Tuple.elts);
2496 if (e->v.Tuple.ctx == Store) {
2497 ADDOP_I(c, UNPACK_SEQUENCE, n);
2498 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002499 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 if (e->v.Tuple.ctx == Load) {
2501 ADDOP_I(c, BUILD_TUPLE, n);
2502 }
2503 return 1;
2504}
2505
2506static int
2507compiler_compare(struct compiler *c, expr_ty e)
2508{
2509 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002510 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511
2512 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2513 VISIT(c, expr, e->v.Compare.left);
2514 n = asdl_seq_LEN(e->v.Compare.ops);
2515 assert(n > 0);
2516 if (n > 1) {
2517 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002518 if (cleanup == NULL)
2519 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002520 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002521 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 }
2523 for (i = 1; i < n; i++) {
2524 ADDOP(c, DUP_TOP);
2525 ADDOP(c, ROT_THREE);
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(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002528 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002529 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002530 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002532 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002533 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002535 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002537 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538 if (n > 1) {
2539 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002540 if (end == NULL)
2541 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 ADDOP_JREL(c, JUMP_FORWARD, end);
2543 compiler_use_next_block(c, cleanup);
2544 ADDOP(c, ROT_TWO);
2545 ADDOP(c, POP_TOP);
2546 compiler_use_next_block(c, end);
2547 }
2548 return 1;
2549}
2550
2551static int
2552compiler_call(struct compiler *c, expr_ty e)
2553{
2554 int n, code = 0;
2555
2556 VISIT(c, expr, e->v.Call.func);
2557 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002558 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002560 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2562 }
2563 if (e->v.Call.starargs) {
2564 VISIT(c, expr, e->v.Call.starargs);
2565 code |= 1;
2566 }
2567 if (e->v.Call.kwargs) {
2568 VISIT(c, expr, e->v.Call.kwargs);
2569 code |= 2;
2570 }
2571 switch (code) {
2572 case 0:
2573 ADDOP_I(c, CALL_FUNCTION, n);
2574 break;
2575 case 1:
2576 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2577 break;
2578 case 2:
2579 ADDOP_I(c, CALL_FUNCTION_KW, n);
2580 break;
2581 case 3:
2582 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2583 break;
2584 }
2585 return 1;
2586}
2587
2588static int
Antoine Pitroud0c35152008-12-17 00:38:28 +00002589compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
2590 int gen_index, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591{
2592 /* generate code for the iterator, then each of the ifs,
2593 and then write to the element */
2594
2595 comprehension_ty l;
2596 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002597 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598
2599 start = compiler_new_block(c);
2600 skip = compiler_new_block(c);
2601 if_cleanup = compiler_new_block(c);
2602 anchor = compiler_new_block(c);
2603
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002604 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2605 anchor == NULL)
2606 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607
Anthony Baxter7b782b62006-04-11 12:01:56 +00002608 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002609 VISIT(c, expr, l->iter);
2610 ADDOP(c, GET_ITER);
2611 compiler_use_next_block(c, start);
2612 ADDOP_JREL(c, FOR_ITER, anchor);
2613 NEXT_BLOCK(c);
2614 VISIT(c, expr, l->target);
2615
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002616 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002617 n = asdl_seq_LEN(l->ifs);
2618 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002619 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002620 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002621 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002623 }
2624
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002625 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitroud0c35152008-12-17 00:38:28 +00002626 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002629 /* only append after the last for generator */
2630 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002631 VISIT(c, expr, elt);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002632 ADDOP_I(c, LIST_APPEND, gen_index+1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002633
2634 compiler_use_next_block(c, skip);
2635 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002636 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002637 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2638 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639
2640 return 1;
2641}
2642
2643static int
2644compiler_listcomp(struct compiler *c, expr_ty e)
2645{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646 assert(e->kind == ListComp_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002647 ADDOP_I(c, BUILD_LIST, 0);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002648 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2649 e->v.ListComp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650}
2651
2652static int
2653compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002654 asdl_seq *generators, int gen_index,
2655 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656{
2657 /* generate code for the iterator, then each of the ifs,
2658 and then write to the element */
2659
2660 comprehension_ty ge;
2661 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002662 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002663
2664 start = compiler_new_block(c);
2665 skip = compiler_new_block(c);
2666 if_cleanup = compiler_new_block(c);
2667 anchor = compiler_new_block(c);
2668 end = compiler_new_block(c);
2669
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002670 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 anchor == NULL || end == NULL)
2672 return 0;
2673
Anthony Baxter7b782b62006-04-11 12:01:56 +00002674 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 ADDOP_JREL(c, SETUP_LOOP, end);
2676 if (!compiler_push_fblock(c, LOOP, start))
2677 return 0;
2678
2679 if (gen_index == 0) {
2680 /* Receive outermost iter as an implicit argument */
2681 c->u->u_argcount = 1;
2682 ADDOP_I(c, LOAD_FAST, 0);
2683 }
2684 else {
2685 /* Sub-iter - calculate on the fly */
2686 VISIT(c, expr, ge->iter);
2687 ADDOP(c, GET_ITER);
2688 }
2689 compiler_use_next_block(c, start);
2690 ADDOP_JREL(c, FOR_ITER, anchor);
2691 NEXT_BLOCK(c);
2692 VISIT(c, expr, ge->target);
2693
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002694 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 n = asdl_seq_LEN(ge->ifs);
2696 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002697 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002699 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002700 NEXT_BLOCK(c);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002701 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002703 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2705 return 0;
2706
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002707 /* only append after the last 'for' generator */
2708 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 VISIT(c, expr, elt);
2710 ADDOP(c, YIELD_VALUE);
2711 ADDOP(c, POP_TOP);
2712
2713 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002714 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002715 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2717 compiler_use_next_block(c, anchor);
2718 ADDOP(c, POP_BLOCK);
2719 compiler_pop_fblock(c, LOOP, start);
2720 compiler_use_next_block(c, end);
2721
2722 return 1;
2723}
2724
2725static int
2726compiler_genexp(struct compiler *c, expr_ty e)
2727{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002728 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002729 PyCodeObject *co;
2730 expr_ty outermost_iter = ((comprehension_ty)
2731 (asdl_seq_GET(e->v.GeneratorExp.generators,
2732 0)))->iter;
2733
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002734 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002735 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002736 if (!name)
2737 return 0;
2738 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002739
2740 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2741 return 0;
2742 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2743 e->v.GeneratorExp.elt);
2744 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002745 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746 if (co == NULL)
2747 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002749 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002750 Py_DECREF(co);
2751
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 VISIT(c, expr, outermost_iter);
2753 ADDOP(c, GET_ITER);
2754 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755
2756 return 1;
2757}
2758
2759static int
2760compiler_visit_keyword(struct compiler *c, keyword_ty k)
2761{
2762 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2763 VISIT(c, expr, k->value);
2764 return 1;
2765}
2766
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002767/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768 whether they are true or false.
2769
2770 Return values: 1 for true, 0 for false, -1 for non-constant.
2771 */
2772
2773static int
2774expr_constant(expr_ty e)
2775{
2776 switch (e->kind) {
2777 case Num_kind:
2778 return PyObject_IsTrue(e->v.Num.n);
2779 case Str_kind:
2780 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002781 case Name_kind:
2782 /* __debug__ is not assignable, so we can optimize
2783 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002784 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002785 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002786 return ! Py_OptimizeFlag;
2787 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 default:
2789 return -1;
2790 }
2791}
2792
Guido van Rossumc2e20742006-02-27 22:32:47 +00002793/*
2794 Implements the with statement from PEP 343.
2795
2796 The semantics outlined in that PEP are as follows:
2797
2798 with EXPR as VAR:
2799 BLOCK
2800
2801 It is implemented roughly as:
2802
Guido van Rossumda5b7012006-05-02 19:47:52 +00002803 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002804 exit = context.__exit__ # not calling it
2805 value = context.__enter__()
2806 try:
2807 VAR = value # if VAR present in the syntax
2808 BLOCK
2809 finally:
2810 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002811 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002812 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002813 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002814 exit(*exc)
2815 */
2816static int
2817compiler_with(struct compiler *c, stmt_ty s)
2818{
Guido van Rossumc2e20742006-02-27 22:32:47 +00002819 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002820
2821 assert(s->kind == With_kind);
2822
Guido van Rossumc2e20742006-02-27 22:32:47 +00002823 block = compiler_new_block(c);
2824 finally = compiler_new_block(c);
2825 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002826 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002827
Guido van Rossumda5b7012006-05-02 19:47:52 +00002828 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002829 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002830 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002831
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002832 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002833 compiler_use_next_block(c, block);
2834 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002835 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002836 }
2837
2838 if (s->v.With.optional_vars) {
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002839 VISIT(c, expr, s->v.With.optional_vars);
2840 }
2841 else {
2842 /* Discard result from context.__enter__() */
2843 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002844 }
2845
2846 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002847 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002848
2849 /* End of try block; start the finally block */
2850 ADDOP(c, POP_BLOCK);
2851 compiler_pop_fblock(c, FINALLY_TRY, block);
2852
2853 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2854 compiler_use_next_block(c, finally);
2855 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002856 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002857
Nick Coghlan7af53be2008-03-07 14:13:28 +00002858 /* Finally block starts; context.__exit__ is on the stack under
2859 the exception or return information. Just issue our magic
2860 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002861 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002862
2863 /* Finally block ends. */
2864 ADDOP(c, END_FINALLY);
2865 compiler_pop_fblock(c, FINALLY_END, finally);
2866 return 1;
2867}
2868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869static int
2870compiler_visit_expr(struct compiler *c, expr_ty e)
2871{
2872 int i, n;
2873
Neal Norwitzf733a012006-10-29 18:30:10 +00002874 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002875 set a new line number for the next instruction.
2876 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877 if (e->lineno > c->u->u_lineno) {
2878 c->u->u_lineno = e->lineno;
2879 c->u->u_lineno_set = false;
2880 }
2881 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002882 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002884 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002885 VISIT(c, expr, e->v.BinOp.left);
2886 VISIT(c, expr, e->v.BinOp.right);
2887 ADDOP(c, binop(c, e->v.BinOp.op));
2888 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002889 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890 VISIT(c, expr, e->v.UnaryOp.operand);
2891 ADDOP(c, unaryop(e->v.UnaryOp.op));
2892 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002893 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002895 case IfExp_kind:
2896 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002897 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00002899 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002900 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002901 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002902 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00002903 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002904 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00002905 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002906 }
2907 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002908 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002909 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002910 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911 return compiler_genexp(c, e);
2912 case Yield_kind:
2913 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002914 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915 if (e->v.Yield.value) {
2916 VISIT(c, expr, e->v.Yield.value);
2917 }
2918 else {
2919 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2920 }
2921 ADDOP(c, YIELD_VALUE);
2922 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002923 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002924 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002925 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002926 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002927 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002928 VISIT(c, expr, e->v.Repr.value);
2929 ADDOP(c, UNARY_CONVERT);
2930 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002931 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
2933 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002934 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002935 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
2936 break;
2937 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002938 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002939 if (e->v.Attribute.ctx != AugStore)
2940 VISIT(c, expr, e->v.Attribute.value);
2941 switch (e->v.Attribute.ctx) {
2942 case AugLoad:
2943 ADDOP(c, DUP_TOP);
2944 /* Fall through to load */
2945 case Load:
2946 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
2947 break;
2948 case AugStore:
2949 ADDOP(c, ROT_TWO);
2950 /* Fall through to save */
2951 case Store:
2952 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
2953 break;
2954 case Del:
2955 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
2956 break;
2957 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002958 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002959 PyErr_SetString(PyExc_SystemError,
2960 "param invalid in attribute expression");
2961 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962 }
2963 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002964 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965 switch (e->v.Subscript.ctx) {
2966 case AugLoad:
2967 VISIT(c, expr, e->v.Subscript.value);
2968 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
2969 break;
2970 case Load:
2971 VISIT(c, expr, e->v.Subscript.value);
2972 VISIT_SLICE(c, e->v.Subscript.slice, Load);
2973 break;
2974 case AugStore:
2975 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
2976 break;
2977 case Store:
2978 VISIT(c, expr, e->v.Subscript.value);
2979 VISIT_SLICE(c, e->v.Subscript.slice, Store);
2980 break;
2981 case Del:
2982 VISIT(c, expr, e->v.Subscript.value);
2983 VISIT_SLICE(c, e->v.Subscript.slice, Del);
2984 break;
2985 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002986 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002987 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002988 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00002989 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 }
2991 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002992 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
2994 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002995 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002997 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 return compiler_tuple(c, e);
2999 }
3000 return 1;
3001}
3002
3003static int
3004compiler_augassign(struct compiler *c, stmt_ty s)
3005{
3006 expr_ty e = s->v.AugAssign.target;
3007 expr_ty auge;
3008
3009 assert(s->kind == AugAssign_kind);
3010
3011 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003012 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003014 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003015 if (auge == NULL)
3016 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 VISIT(c, expr, auge);
3018 VISIT(c, expr, s->v.AugAssign.value);
3019 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3020 auge->v.Attribute.ctx = AugStore;
3021 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022 break;
3023 case Subscript_kind:
3024 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003025 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003026 if (auge == NULL)
3027 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 VISIT(c, expr, auge);
3029 VISIT(c, expr, s->v.AugAssign.value);
3030 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003031 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003033 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003035 if (!compiler_nameop(c, e->v.Name.id, Load))
3036 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037 VISIT(c, expr, s->v.AugAssign.value);
3038 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3039 return compiler_nameop(c, e->v.Name.id, Store);
3040 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003041 PyErr_Format(PyExc_SystemError,
3042 "invalid node type (%d) for augmented assignment",
3043 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003044 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045 }
3046 return 1;
3047}
3048
3049static int
3050compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3051{
3052 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003053 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3054 PyErr_SetString(PyExc_SystemError,
3055 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003057 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003058 f = &c->u->u_fblock[c->u->u_nfblocks++];
3059 f->fb_type = t;
3060 f->fb_block = b;
3061 return 1;
3062}
3063
3064static void
3065compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3066{
3067 struct compiler_unit *u = c->u;
3068 assert(u->u_nfblocks > 0);
3069 u->u_nfblocks--;
3070 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3071 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3072}
3073
Jeremy Hylton82271f12006-10-04 02:24:52 +00003074static int
3075compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003076 int i;
3077 struct compiler_unit *u = c->u;
3078 for (i = 0; i < u->u_nfblocks; ++i) {
3079 if (u->u_fblock[i].fb_type == LOOP)
3080 return 1;
3081 }
3082 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003083}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003084/* Raises a SyntaxError and returns 0.
3085 If something goes wrong, a different exception may be raised.
3086*/
3087
3088static int
3089compiler_error(struct compiler *c, const char *errstr)
3090{
3091 PyObject *loc;
3092 PyObject *u = NULL, *v = NULL;
3093
3094 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3095 if (!loc) {
3096 Py_INCREF(Py_None);
3097 loc = Py_None;
3098 }
3099 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3100 Py_None, loc);
3101 if (!u)
3102 goto exit;
3103 v = Py_BuildValue("(zO)", errstr, u);
3104 if (!v)
3105 goto exit;
3106 PyErr_SetObject(PyExc_SyntaxError, v);
3107 exit:
3108 Py_DECREF(loc);
3109 Py_XDECREF(u);
3110 Py_XDECREF(v);
3111 return 0;
3112}
3113
3114static int
3115compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003116 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003118 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003119
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 /* XXX this code is duplicated */
3121 switch (ctx) {
3122 case AugLoad: /* fall through to Load */
3123 case Load: op = BINARY_SUBSCR; break;
3124 case AugStore:/* fall through to Store */
3125 case Store: op = STORE_SUBSCR; break;
3126 case Del: op = DELETE_SUBSCR; break;
3127 case Param:
3128 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003129 "invalid %s kind %d in subscript\n",
3130 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003131 return 0;
3132 }
3133 if (ctx == AugLoad) {
3134 ADDOP_I(c, DUP_TOPX, 2);
3135 }
3136 else if (ctx == AugStore) {
3137 ADDOP(c, ROT_THREE);
3138 }
3139 ADDOP(c, op);
3140 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141}
3142
3143static int
3144compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3145{
3146 int n = 2;
3147 assert(s->kind == Slice_kind);
3148
3149 /* only handles the cases where BUILD_SLICE is emitted */
3150 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003151 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 }
3153 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003154 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003157 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003158 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 }
3160 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003161 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 }
3163
3164 if (s->v.Slice.step) {
3165 n++;
3166 VISIT(c, expr, s->v.Slice.step);
3167 }
3168 ADDOP_I(c, BUILD_SLICE, n);
3169 return 1;
3170}
3171
3172static int
3173compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3174{
3175 int op = 0, slice_offset = 0, stack_count = 0;
3176
3177 assert(s->v.Slice.step == NULL);
3178 if (s->v.Slice.lower) {
3179 slice_offset++;
3180 stack_count++;
3181 if (ctx != AugStore)
3182 VISIT(c, expr, s->v.Slice.lower);
3183 }
3184 if (s->v.Slice.upper) {
3185 slice_offset += 2;
3186 stack_count++;
3187 if (ctx != AugStore)
3188 VISIT(c, expr, s->v.Slice.upper);
3189 }
3190
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003191 if (ctx == AugLoad) {
3192 switch (stack_count) {
3193 case 0: ADDOP(c, DUP_TOP); break;
3194 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3195 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3196 }
3197 }
3198 else if (ctx == AugStore) {
3199 switch (stack_count) {
3200 case 0: ADDOP(c, ROT_TWO); break;
3201 case 1: ADDOP(c, ROT_THREE); break;
3202 case 2: ADDOP(c, ROT_FOUR); break;
3203 }
3204 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205
3206 switch (ctx) {
3207 case AugLoad: /* fall through to Load */
3208 case Load: op = SLICE; break;
3209 case AugStore:/* fall through to Store */
3210 case Store: op = STORE_SLICE; break;
3211 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003212 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003213 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003214 PyErr_SetString(PyExc_SystemError,
3215 "param invalid in simple slice");
3216 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217 }
3218
3219 ADDOP(c, op + slice_offset);
3220 return 1;
3221}
3222
3223static int
3224compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3225 expr_context_ty ctx)
3226{
3227 switch (s->kind) {
3228 case Ellipsis_kind:
3229 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3230 break;
3231 case Slice_kind:
3232 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233 case Index_kind:
3234 VISIT(c, expr, s->v.Index.value);
3235 break;
3236 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003237 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003238 PyErr_SetString(PyExc_SystemError,
3239 "extended slice invalid in nested slice");
3240 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 }
3242 return 1;
3243}
3244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003245static int
3246compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3247{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003248 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003250 case Index_kind:
3251 kindname = "index";
3252 if (ctx != AugStore) {
3253 VISIT(c, expr, s->v.Index.value);
3254 }
3255 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003257 kindname = "ellipsis";
3258 if (ctx != AugStore) {
3259 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3260 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 break;
3262 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003263 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003264 if (!s->v.Slice.step)
3265 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003266 if (ctx != AugStore) {
3267 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268 return 0;
3269 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003270 break;
3271 case ExtSlice_kind:
3272 kindname = "extended slice";
3273 if (ctx != AugStore) {
3274 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3275 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003276 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003277 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003278 if (!compiler_visit_nested_slice(c, sub, ctx))
3279 return 0;
3280 }
3281 ADDOP_I(c, BUILD_TUPLE, n);
3282 }
3283 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003284 default:
3285 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003286 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003287 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003289 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290}
3291
Neal Norwitzf733a012006-10-29 18:30:10 +00003292
3293/* End of the compiler section, beginning of the assembler section */
3294
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295/* do depth-first search of basic block graph, starting with block.
3296 post records the block indices in post-order.
3297
3298 XXX must handle implicit jumps from one block to next
3299*/
3300
Neal Norwitzf733a012006-10-29 18:30:10 +00003301struct assembler {
3302 PyObject *a_bytecode; /* string containing bytecode */
3303 int a_offset; /* offset into bytecode */
3304 int a_nblocks; /* number of reachable blocks */
3305 basicblock **a_postorder; /* list of blocks in dfs postorder */
3306 PyObject *a_lnotab; /* string containing lnotab */
3307 int a_lnotab_off; /* offset into lnotab */
3308 int a_lineno; /* last lineno of emitted instruction */
3309 int a_lineno_off; /* bytecode offset of last lineno */
3310};
3311
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312static void
3313dfs(struct compiler *c, basicblock *b, struct assembler *a)
3314{
3315 int i;
3316 struct instr *instr = NULL;
3317
3318 if (b->b_seen)
3319 return;
3320 b->b_seen = 1;
3321 if (b->b_next != NULL)
3322 dfs(c, b->b_next, a);
3323 for (i = 0; i < b->b_iused; i++) {
3324 instr = &b->b_instr[i];
3325 if (instr->i_jrel || instr->i_jabs)
3326 dfs(c, instr->i_target, a);
3327 }
3328 a->a_postorder[a->a_nblocks++] = b;
3329}
3330
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003331static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3333{
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003334 int i, target_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003335 struct instr *instr;
3336 if (b->b_seen || b->b_startdepth >= depth)
3337 return maxdepth;
3338 b->b_seen = 1;
3339 b->b_startdepth = depth;
3340 for (i = 0; i < b->b_iused; i++) {
3341 instr = &b->b_instr[i];
3342 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3343 if (depth > maxdepth)
3344 maxdepth = depth;
3345 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3346 if (instr->i_jrel || instr->i_jabs) {
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003347 target_depth = depth;
3348 if (instr->i_opcode == FOR_ITER) {
3349 target_depth = depth-2;
3350 } else if (instr->i_opcode == SETUP_FINALLY ||
3351 instr->i_opcode == SETUP_EXCEPT) {
3352 target_depth = depth+3;
3353 if (target_depth > maxdepth)
3354 maxdepth = target_depth;
3355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 maxdepth = stackdepth_walk(c, instr->i_target,
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003357 target_depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358 if (instr->i_opcode == JUMP_ABSOLUTE ||
3359 instr->i_opcode == JUMP_FORWARD) {
3360 goto out; /* remaining code is dead */
3361 }
3362 }
3363 }
3364 if (b->b_next)
3365 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3366out:
3367 b->b_seen = 0;
3368 return maxdepth;
3369}
3370
3371/* Find the flow path that needs the largest stack. We assume that
3372 * cycles in the flow graph have no net effect on the stack depth.
3373 */
3374static int
3375stackdepth(struct compiler *c)
3376{
3377 basicblock *b, *entryblock;
3378 entryblock = NULL;
3379 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3380 b->b_seen = 0;
3381 b->b_startdepth = INT_MIN;
3382 entryblock = b;
3383 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003384 if (!entryblock)
3385 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 return stackdepth_walk(c, entryblock, 0, 0);
3387}
3388
3389static int
3390assemble_init(struct assembler *a, int nblocks, int firstlineno)
3391{
3392 memset(a, 0, sizeof(struct assembler));
3393 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003394 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395 if (!a->a_bytecode)
3396 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003397 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398 if (!a->a_lnotab)
3399 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003400 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3401 PyErr_NoMemory();
3402 return 0;
3403 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003405 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003406 if (!a->a_postorder) {
3407 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003409 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 return 1;
3411}
3412
3413static void
3414assemble_free(struct assembler *a)
3415{
3416 Py_XDECREF(a->a_bytecode);
3417 Py_XDECREF(a->a_lnotab);
3418 if (a->a_postorder)
3419 PyObject_Free(a->a_postorder);
3420}
3421
3422/* Return the size of a basic block in bytes. */
3423
3424static int
3425instrsize(struct instr *instr)
3426{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003427 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003428 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003429 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003430 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3431 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003432}
3433
3434static int
3435blocksize(basicblock *b)
3436{
3437 int i;
3438 int size = 0;
3439
3440 for (i = 0; i < b->b_iused; i++)
3441 size += instrsize(&b->b_instr[i]);
3442 return size;
3443}
3444
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003445/* Appends a pair to the end of the line number table, a_lnotab, representing
3446 the instruction's bytecode offset and line number. See
3447 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003448
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003449static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003451{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003452 int d_bytecode, d_lineno;
3453 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003454 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003455
3456 d_bytecode = a->a_offset - a->a_lineno_off;
3457 d_lineno = i->i_lineno - a->a_lineno;
3458
3459 assert(d_bytecode >= 0);
3460 assert(d_lineno >= 0);
3461
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003462 if(d_bytecode == 0 && d_lineno == 0)
3463 return 1;
3464
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003465 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003466 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003468 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003470 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003472 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003474 else {
3475 PyErr_NoMemory();
3476 return 0;
3477 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003478 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003480 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003481 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003482 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003483 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484 *lnotab++ = 255;
3485 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003486 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 d_bytecode -= ncodes * 255;
3488 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 assert(d_bytecode <= 255);
3491 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003492 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003493 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003494 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003496 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003497 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003498 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003499 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003500 else {
3501 PyErr_NoMemory();
3502 return 0;
3503 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003504 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003506 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003507 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003508 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003510 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003512 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003514 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003515 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 d_lineno -= ncodes * 255;
3517 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003518 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003519
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003520 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003521 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003522 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003523 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003524 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003525 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003526 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528 a->a_lnotab_off += 2;
3529 if (d_bytecode) {
3530 *lnotab++ = d_bytecode;
3531 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003532 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003533 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003534 *lnotab++ = 0;
3535 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 a->a_lineno = i->i_lineno;
3538 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003539 return 1;
3540}
3541
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003542/* assemble_emit()
3543 Extend the bytecode with a new instruction.
3544 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003545*/
3546
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003547static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003549{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003550 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003551 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552 char *code;
3553
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003554 size = instrsize(i);
3555 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003557 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003558 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003560 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003562 if (len > PY_SSIZE_T_MAX / 2)
3563 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003564 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003565 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003566 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003567 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003569 if (size == 6) {
3570 assert(i->i_hasarg);
3571 *code++ = (char)EXTENDED_ARG;
3572 *code++ = ext & 0xff;
3573 *code++ = ext >> 8;
3574 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003575 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003577 if (i->i_hasarg) {
3578 assert(size == 3 || size == 6);
3579 *code++ = arg & 0xff;
3580 *code++ = arg >> 8;
3581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003583}
3584
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003585static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003587{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003589 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003590 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003591
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003592 /* Compute the size of each block and fixup jump args.
3593 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003594start:
3595 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003597 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 bsize = blocksize(b);
3599 b->b_offset = totsize;
3600 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003601 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003602 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3604 bsize = b->b_offset;
3605 for (i = 0; i < b->b_iused; i++) {
3606 struct instr *instr = &b->b_instr[i];
3607 /* Relative jumps are computed relative to
3608 the instruction pointer after fetching
3609 the jump instruction.
3610 */
3611 bsize += instrsize(instr);
3612 if (instr->i_jabs)
3613 instr->i_oparg = instr->i_target->b_offset;
3614 else if (instr->i_jrel) {
3615 int delta = instr->i_target->b_offset - bsize;
3616 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003617 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003618 else
3619 continue;
3620 if (instr->i_oparg > 0xffff)
3621 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003622 }
3623 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003624
3625 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003626 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003627 with a better solution.
3628
3629 In the meantime, should the goto be dropped in favor
3630 of a loop?
3631
3632 The issue is that in the first loop blocksize() is called
3633 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003634 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003635 i_oparg is calculated in the second loop above.
3636
3637 So we loop until we stop seeing new EXTENDED_ARGs.
3638 The only EXTENDED_ARGs that could be popping up are
3639 ones in jump instructions. So this should converge
3640 fairly quickly.
3641 */
3642 if (last_extended_arg_count != extended_arg_count) {
3643 last_extended_arg_count = extended_arg_count;
3644 goto start;
3645 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003646}
3647
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003648static PyObject *
3649dict_keys_inorder(PyObject *dict, int offset)
3650{
3651 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003652 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003653
3654 tuple = PyTuple_New(size);
3655 if (tuple == NULL)
3656 return NULL;
3657 while (PyDict_Next(dict, &pos, &k, &v)) {
3658 i = PyInt_AS_LONG(v);
Benjamin Petersonda9327f2009-01-31 23:43:25 +00003659 /* The keys of the dictionary are tuples. (see compiler_add_o)
3660 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003661 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003662 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003663 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003664 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003665 PyTuple_SET_ITEM(tuple, i - offset, k);
3666 }
3667 return tuple;
3668}
3669
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003670static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003672{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673 PySTEntryObject *ste = c->u->u_ste;
3674 int flags = 0, n;
3675 if (ste->ste_type != ModuleBlock)
3676 flags |= CO_NEWLOCALS;
3677 if (ste->ste_type == FunctionBlock) {
3678 if (!ste->ste_unoptimized)
3679 flags |= CO_OPTIMIZED;
3680 if (ste->ste_nested)
3681 flags |= CO_NESTED;
3682 if (ste->ste_generator)
3683 flags |= CO_GENERATOR;
Benjamin Peterson12554cb2009-01-31 23:54:38 +00003684 if (ste->ste_varargs)
3685 flags |= CO_VARARGS;
3686 if (ste->ste_varkeywords)
3687 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003688 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003689
3690 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003691 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003692
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693 n = PyDict_Size(c->u->u_freevars);
3694 if (n < 0)
3695 return -1;
3696 if (n == 0) {
3697 n = PyDict_Size(c->u->u_cellvars);
3698 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003699 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 if (n == 0) {
3701 flags |= CO_NOFREE;
3702 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003703 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003704
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003705 return flags;
3706}
3707
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708static PyCodeObject *
3709makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003710{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 PyObject *tmp;
3712 PyCodeObject *co = NULL;
3713 PyObject *consts = NULL;
3714 PyObject *names = NULL;
3715 PyObject *varnames = NULL;
3716 PyObject *filename = NULL;
3717 PyObject *name = NULL;
3718 PyObject *freevars = NULL;
3719 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003720 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003722
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 tmp = dict_keys_inorder(c->u->u_consts, 0);
3724 if (!tmp)
3725 goto error;
3726 consts = PySequence_List(tmp); /* optimize_code requires a list */
3727 Py_DECREF(tmp);
3728
3729 names = dict_keys_inorder(c->u->u_names, 0);
3730 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3731 if (!consts || !names || !varnames)
3732 goto error;
3733
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003734 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3735 if (!cellvars)
3736 goto error;
3737 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3738 if (!freevars)
3739 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003740 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 if (!filename)
3742 goto error;
3743
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003744 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 flags = compute_code_flags(c);
3746 if (flags < 0)
3747 goto error;
3748
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003749 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003750 if (!bytecode)
3751 goto error;
3752
3753 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3754 if (!tmp)
3755 goto error;
3756 Py_DECREF(consts);
3757 consts = tmp;
3758
3759 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3760 bytecode, consts, names, varnames,
3761 freevars, cellvars,
3762 filename, c->u->u_name,
3763 c->u->u_firstlineno,
3764 a->a_lnotab);
3765 error:
3766 Py_XDECREF(consts);
3767 Py_XDECREF(names);
3768 Py_XDECREF(varnames);
3769 Py_XDECREF(filename);
3770 Py_XDECREF(name);
3771 Py_XDECREF(freevars);
3772 Py_XDECREF(cellvars);
3773 Py_XDECREF(bytecode);
3774 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003775}
3776
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003777
3778/* For debugging purposes only */
3779#if 0
3780static void
3781dump_instr(const struct instr *i)
3782{
3783 const char *jrel = i->i_jrel ? "jrel " : "";
3784 const char *jabs = i->i_jabs ? "jabs " : "";
3785 char arg[128];
3786
3787 *arg = '\0';
3788 if (i->i_hasarg)
3789 sprintf(arg, "arg: %d ", i->i_oparg);
3790
3791 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3792 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3793}
3794
3795static void
3796dump_basicblock(const basicblock *b)
3797{
3798 const char *seen = b->b_seen ? "seen " : "";
3799 const char *b_return = b->b_return ? "return " : "";
3800 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3801 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3802 if (b->b_instr) {
3803 int i;
3804 for (i = 0; i < b->b_iused; i++) {
3805 fprintf(stderr, " [%02d] ", i);
3806 dump_instr(b->b_instr + i);
3807 }
3808 }
3809}
3810#endif
3811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812static PyCodeObject *
3813assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003814{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 basicblock *b, *entryblock;
3816 struct assembler a;
3817 int i, j, nblocks;
3818 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 /* Make sure every block that falls off the end returns None.
3821 XXX NEXT_BLOCK() isn't quite right, because if the last
3822 block ends with a jump or return b_next shouldn't set.
3823 */
3824 if (!c->u->u_curblock->b_return) {
3825 NEXT_BLOCK(c);
3826 if (addNone)
3827 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3828 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003829 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 nblocks = 0;
3832 entryblock = NULL;
3833 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3834 nblocks++;
3835 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003836 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003837
Neal Norwitzed657552006-07-10 00:04:44 +00003838 /* Set firstlineno if it wasn't explicitly set. */
3839 if (!c->u->u_firstlineno) {
3840 if (entryblock && entryblock->b_instr)
3841 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3842 else
3843 c->u->u_firstlineno = 1;
3844 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3846 goto error;
3847 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003848
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003849 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003850 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003851
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 /* Emit code in reverse postorder from dfs. */
3853 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003854 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 for (j = 0; j < b->b_iused; j++)
3856 if (!assemble_emit(&a, &b->b_instr[j]))
3857 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003858 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003859
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003860 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003862 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003863 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003865 co = makecode(c, &a);
3866 error:
3867 assemble_free(&a);
3868 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003869}