blob: 4f55b36fdd38502f93ce951b1b56ab3b6c4aee38 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Jeremy Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Neal Norwitzf733a012006-10-29 18:30:10 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Jeremy Hylton819de6c2007-02-27 16:13:23 +000011 * this file.
Neal Norwitzf733a012006-10-29 18:30:10 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Neal Norwitzf733a012006-10-29 18:30:10 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000042struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000043 unsigned i_jabs : 1;
44 unsigned i_jrel : 1;
45 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046 unsigned char i_opcode;
47 int i_oparg;
48 struct basicblock_ *i_target; /* target block (if jump instruction) */
49 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000050};
51
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000052typedef struct basicblock_ {
Jeremy Hylton12603c42006-04-01 16:18:02 +000053 /* Each basicblock in a compilation unit is linked via b_list in the
54 reverse order that the block are allocated. b_list points to the next
55 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056 struct basicblock_ *b_list;
57 /* number of instructions used */
58 int b_iused;
59 /* length of instruction array (b_instr) */
60 int b_ialloc;
61 /* pointer to an array of instructions, initially NULL */
62 struct instr *b_instr;
63 /* If b_next is non-NULL, it is a pointer to the next
64 block reached by normal control flow. */
65 struct basicblock_ *b_next;
66 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000067 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000068 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000069 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000070 /* depth of stack upon entry of block, computed by stackdepth() */
71 int b_startdepth;
72 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000073 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074} basicblock;
75
76/* fblockinfo tracks the current frame block.
77
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078A frame block is used to handle loops, try/except, and try/finally.
79It's called a frame block to distinguish it from a basic block in the
80compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081*/
82
83enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
84
85struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000086 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000087 basicblock *fb_block;
88};
89
90/* The following items change on entry and exit of code blocks.
91 They must be saved and restored when returning to a block.
92*/
93struct compiler_unit {
94 PySTEntryObject *u_ste;
95
96 PyObject *u_name;
97 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +000098 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099 the argument for opcodes that refer to those collections.
100 */
101 PyObject *u_consts; /* all constants */
102 PyObject *u_names; /* all names */
103 PyObject *u_varnames; /* local variables */
104 PyObject *u_cellvars; /* cell variables */
105 PyObject *u_freevars; /* free variables */
106
107 PyObject *u_private; /* for private name mangling */
108
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000109 int u_argcount; /* number of arguments for block */
Neal Norwitzf733a012006-10-29 18:30:10 +0000110 /* Pointer to the most recently allocated block. By following b_list
111 members, you can reach all early allocated blocks. */
Jeremy Hylton12603c42006-04-01 16:18:02 +0000112 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113 basicblock *u_curblock; /* pointer to current block */
Neal Norwitzf733a012006-10-29 18:30:10 +0000114 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115
116 int u_nfblocks;
117 struct fblockinfo u_fblock[CO_MAXBLOCKS];
118
119 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000120 int u_lineno; /* the lineno for the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121 bool u_lineno_set; /* boolean to indicate whether instr
122 has been generated with current lineno */
123};
124
125/* This struct captures the global state of a compilation.
126
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000127The u pointer points to the current compilation unit, while units
128for enclosing blocks are stored in c_stack. The u and c_stack are
129managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000130*/
131
132struct compiler {
133 const char *c_filename;
134 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000135 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136 PyCompilerFlags *c_flags;
137
Neal Norwitz4ffedad2006-08-04 04:58:47 +0000138 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000139 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141 struct compiler_unit *u; /* compiler state for current block */
142 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145};
146
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147static int compiler_enter_scope(struct compiler *, identifier, void *, int);
148static void compiler_free(struct compiler *);
149static basicblock *compiler_new_block(struct compiler *);
150static int compiler_next_instr(struct compiler *, basicblock *);
151static int compiler_addop(struct compiler *, int);
152static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
153static int compiler_addop_i(struct compiler *, int, int);
154static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000155static basicblock *compiler_use_new_block(struct compiler *);
156static int compiler_error(struct compiler *, const char *);
157static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
158
159static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
160static int compiler_visit_stmt(struct compiler *, stmt_ty);
161static int compiler_visit_keyword(struct compiler *, keyword_ty);
162static int compiler_visit_expr(struct compiler *, expr_ty);
163static int compiler_augassign(struct compiler *, stmt_ty);
164static int compiler_visit_slice(struct compiler *, slice_ty,
165 expr_context_ty);
166
167static int compiler_push_fblock(struct compiler *, enum fblocktype,
168 basicblock *);
169static void compiler_pop_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
Jeremy Hylton82271f12006-10-04 02:24:52 +0000171/* Returns true if there is a loop on the fblock stack. */
172static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173
174static int inplace_binop(struct compiler *, operator_ty);
175static int expr_constant(expr_ty e);
176
Guido van Rossumc2e20742006-02-27 22:32:47 +0000177static int compiler_with(struct compiler *, stmt_ty);
178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179static PyCodeObject *assemble(struct compiler *, int addNone);
180static PyObject *__doc__;
181
182PyObject *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000183_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000184{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185 /* Name mangling: __private becomes _classname__private.
186 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000187 const char *p, *name = PyString_AsString(ident);
188 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189 size_t nlen, plen;
Neal Norwitz84167d02006-08-12 01:45:47 +0000190 if (privateobj == NULL || !PyString_Check(privateobj) ||
191 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000192 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000194 }
Anthony Baxter7b782b62006-04-11 12:01:56 +0000195 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 nlen = strlen(name);
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000197 /* Don't mangle __id__ or names with dots.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000198
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000199 The only time a name with a dot can occur is when
200 we are compiling an import statement that has a
201 package name.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000202
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000203 TODO(jhylton): Decide whether we want to support
204 mangling of the module name, e.g. __M.X.
205 */
Jeremy Hylton37075c52007-02-27 01:01:59 +0000206 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000207 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000208 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211 /* Strip leading underscores from class name */
212 while (*p == '_')
213 p++;
214 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000215 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218 plen = strlen(p);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000219 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
220 if (!ident)
221 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000223 buffer = PyString_AS_STRING(ident);
224 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225 strncpy(buffer+1, p, plen);
226 strcpy(buffer+1+plen, name);
227 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000228}
229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230static int
231compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000232{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235 c->c_stack = PyList_New(0);
236 if (!c->c_stack)
237 return 0;
238
239 return 1;
240}
241
242PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000243PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000244 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245{
246 struct compiler c;
247 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000248 PyCompilerFlags local_flags;
249 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000251 if (!__doc__) {
252 __doc__ = PyString_InternFromString("__doc__");
253 if (!__doc__)
254 return NULL;
255 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256
257 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000258 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000259 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000260 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261 c.c_future = PyFuture_FromAST(mod, filename);
262 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000263 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000265 local_flags.cf_flags = 0;
266 flags = &local_flags;
267 }
268 merged = c.c_future->ff_features | flags->cf_flags;
269 c.c_future->ff_features = merged;
270 flags->cf_flags = merged;
271 c.c_flags = flags;
272 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
274 c.c_st = PySymtable_Build(mod, filename, c.c_future);
275 if (c.c_st == NULL) {
276 if (!PyErr_Occurred())
277 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000278 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 }
280
281 /* XXX initialize to NULL for now, need to handle */
282 c.c_encoding = NULL;
283
284 co = compiler_mod(&c, mod);
285
Thomas Wouters1175c432006-02-27 22:49:54 +0000286 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000288 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 return co;
290}
291
292PyCodeObject *
293PyNode_Compile(struct _node *n, const char *filename)
294{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000295 PyCodeObject *co = NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000296 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000297 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000298 if (!arena)
299 return NULL;
300 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000301 if (mod)
302 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000303 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000304 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000305}
306
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000307static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000309{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000310 if (c->c_st)
311 PySymtable_Free(c->c_st);
312 if (c->c_future)
Neal Norwitz14bc4e42006-04-10 06:57:06 +0000313 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000314 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000315}
316
Guido van Rossum79f25d91997-04-29 20:08:16 +0000317static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000319{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000320 Py_ssize_t i, n;
Georg Brandl5c170fd2006-03-17 19:03:25 +0000321 PyObject *v, *k;
322 PyObject *dict = PyDict_New();
323 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000324
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 n = PyList_Size(list);
326 for (i = 0; i < n; i++) {
327 v = PyInt_FromLong(i);
328 if (!v) {
329 Py_DECREF(dict);
330 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000331 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000332 k = PyList_GET_ITEM(list, i);
Georg Brandl7784f122006-05-26 20:04:44 +0000333 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000334 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
335 Py_XDECREF(k);
336 Py_DECREF(v);
337 Py_DECREF(dict);
338 return NULL;
339 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000340 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000342 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000343 return dict;
344}
345
346/* Return new dict containing names from src that match scope(s).
347
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000348src is a symbol table dictionary. If the scope of a name matches
349either scope_type or flag is set, insert it into the new dict. The
350values are integers, starting at offset and increasing by one for
351each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352*/
353
354static PyObject *
355dictbytype(PyObject *src, int scope_type, int flag, int offset)
356{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000357 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358 PyObject *k, *v, *dest = PyDict_New();
359
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000360 assert(offset >= 0);
361 if (dest == NULL)
362 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363
364 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000365 /* XXX this should probably be a macro in symtable.h */
366 assert(PyInt_Check(v));
367 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000369 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
370 PyObject *tuple, *item = PyInt_FromLong(i);
371 if (item == NULL) {
372 Py_DECREF(dest);
373 return NULL;
374 }
375 i++;
Georg Brandl7784f122006-05-26 20:04:44 +0000376 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000377 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
378 Py_DECREF(item);
379 Py_DECREF(dest);
380 Py_XDECREF(tuple);
381 return NULL;
382 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000384 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386 }
387 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000388}
389
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390static void
391compiler_unit_check(struct compiler_unit *u)
392{
393 basicblock *block;
394 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Brett Cannona6c41bc2008-02-07 07:47:31 +0000395 assert((void *)block != (void *)0xcbcbcbcb);
396 assert((void *)block != (void *)0xfbfbfbfb);
397 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 if (block->b_instr != NULL) {
399 assert(block->b_ialloc > 0);
400 assert(block->b_iused > 0);
401 assert(block->b_ialloc >= block->b_iused);
402 }
403 else {
404 assert (block->b_iused == 0);
405 assert (block->b_ialloc == 0);
406 }
407 }
408}
409
410static void
411compiler_unit_free(struct compiler_unit *u)
412{
413 basicblock *b, *next;
414
415 compiler_unit_check(u);
416 b = u->u_blocks;
417 while (b != NULL) {
418 if (b->b_instr)
419 PyObject_Free((void *)b->b_instr);
420 next = b->b_list;
421 PyObject_Free((void *)b);
422 b = next;
423 }
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000424 Py_CLEAR(u->u_ste);
425 Py_CLEAR(u->u_name);
426 Py_CLEAR(u->u_consts);
427 Py_CLEAR(u->u_names);
428 Py_CLEAR(u->u_varnames);
429 Py_CLEAR(u->u_freevars);
430 Py_CLEAR(u->u_cellvars);
431 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 PyObject_Free(u);
433}
434
435static int
436compiler_enter_scope(struct compiler *c, identifier name, void *key,
437 int lineno)
438{
439 struct compiler_unit *u;
440
Anthony Baxter7b782b62006-04-11 12:01:56 +0000441 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000442 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000443 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000444 PyErr_NoMemory();
445 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000446 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000447 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000448 u->u_argcount = 0;
449 u->u_ste = PySymtable_Lookup(c->c_st, key);
450 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000451 compiler_unit_free(u);
452 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 }
454 Py_INCREF(name);
455 u->u_name = name;
456 u->u_varnames = list2dict(u->u_ste->ste_varnames);
457 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000458 if (!u->u_varnames || !u->u_cellvars) {
459 compiler_unit_free(u);
460 return 0;
461 }
462
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000464 PyDict_Size(u->u_cellvars));
Neal Norwitzd12bd012006-07-21 07:59:47 +0000465 if (!u->u_freevars) {
466 compiler_unit_free(u);
467 return 0;
468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
470 u->u_blocks = NULL;
471 u->u_tmpname = 0;
472 u->u_nfblocks = 0;
473 u->u_firstlineno = lineno;
474 u->u_lineno = 0;
475 u->u_lineno_set = false;
476 u->u_consts = PyDict_New();
477 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000478 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479 return 0;
480 }
481 u->u_names = PyDict_New();
482 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000483 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484 return 0;
485 }
486
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000487 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488
489 /* Push the old compiler_unit on the stack. */
490 if (c->u) {
491 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000492 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
493 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000494 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495 return 0;
496 }
497 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 u->u_private = c->u->u_private;
499 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500 }
501 c->u = u;
502
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000503 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000504 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000505 return 0;
506
507 return 1;
508}
509
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000510static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511compiler_exit_scope(struct compiler *c)
512{
513 int n;
514 PyObject *wrapper;
515
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000516 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000517 compiler_unit_free(c->u);
518 /* Restore c->u to the parent unit. */
519 n = PyList_GET_SIZE(c->c_stack) - 1;
520 if (n >= 0) {
521 wrapper = PyList_GET_ITEM(c->c_stack, n);
522 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Neal Norwitz87557cd2006-08-21 18:01:30 +0000523 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000524 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000526 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527 compiler_unit_check(c->u);
528 }
529 else
530 c->u = NULL;
531
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532}
533
Guido van Rossumc2e20742006-02-27 22:32:47 +0000534/* Allocate a new "anonymous" local variable.
535 Used by list comprehensions and with statements.
536*/
537
538static PyObject *
539compiler_new_tmpname(struct compiler *c)
540{
541 char tmpname[256];
542 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
543 return PyString_FromString(tmpname);
544}
545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546/* Allocate a new block and return a pointer to it.
547 Returns NULL on error.
548*/
549
550static basicblock *
551compiler_new_block(struct compiler *c)
552{
553 basicblock *b;
554 struct compiler_unit *u;
555
556 u = c->u;
557 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000558 if (b == NULL) {
559 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000561 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562 memset((void *)b, 0, sizeof(basicblock));
Neal Norwitzf733a012006-10-29 18:30:10 +0000563 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 b->b_list = u->u_blocks;
565 u->u_blocks = b;
566 return b;
567}
568
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569static basicblock *
570compiler_use_new_block(struct compiler *c)
571{
572 basicblock *block = compiler_new_block(c);
573 if (block == NULL)
574 return NULL;
575 c->u->u_curblock = block;
576 return block;
577}
578
579static basicblock *
580compiler_next_block(struct compiler *c)
581{
582 basicblock *block = compiler_new_block(c);
583 if (block == NULL)
584 return NULL;
585 c->u->u_curblock->b_next = block;
586 c->u->u_curblock = block;
587 return block;
588}
589
590static basicblock *
591compiler_use_next_block(struct compiler *c, basicblock *block)
592{
593 assert(block != NULL);
594 c->u->u_curblock->b_next = block;
595 c->u->u_curblock = block;
596 return block;
597}
598
599/* Returns the offset of the next instruction in the current block's
600 b_instr array. Resizes the b_instr as necessary.
601 Returns -1 on failure.
Neal Norwitzf733a012006-10-29 18:30:10 +0000602*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603
604static int
605compiler_next_instr(struct compiler *c, basicblock *b)
606{
607 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000608 if (b->b_instr == NULL) {
Anthony Baxter7b782b62006-04-11 12:01:56 +0000609 b->b_instr = (struct instr *)PyObject_Malloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000610 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 if (b->b_instr == NULL) {
612 PyErr_NoMemory();
613 return -1;
614 }
615 b->b_ialloc = DEFAULT_BLOCK_SIZE;
616 memset((char *)b->b_instr, 0,
617 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619 else if (b->b_iused == b->b_ialloc) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000620 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 size_t oldsize, newsize;
622 oldsize = b->b_ialloc * sizeof(struct instr);
623 newsize = oldsize << 1;
624 if (newsize == 0) {
625 PyErr_NoMemory();
626 return -1;
627 }
628 b->b_ialloc <<= 1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000629 tmp = (struct instr *)PyObject_Realloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000630 (void *)b->b_instr, newsize);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000631 if (tmp == NULL) {
632 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 return -1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000634 }
635 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000636 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
637 }
638 return b->b_iused++;
639}
640
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000641/* Set the i_lineno member of the instruction at offset off if the
642 line number for the current expression/statement has not
Jeremy Hylton12603c42006-04-01 16:18:02 +0000643 already been set. If it has been set, the call has no effect.
644
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000645 The line number is reset in the following cases:
646 - when entering a new scope
647 - on each statement
648 - on each expression that start a new line
649 - before the "except" clause
650 - before the "for" and "while" expressions
Neal Norwitzf733a012006-10-29 18:30:10 +0000651*/
Jeremy Hylton12603c42006-04-01 16:18:02 +0000652
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000653static void
654compiler_set_lineno(struct compiler *c, int off)
655{
656 basicblock *b;
657 if (c->u->u_lineno_set)
658 return;
659 c->u->u_lineno_set = true;
660 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000661 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662}
663
664static int
665opcode_stack_effect(int opcode, int oparg)
666{
667 switch (opcode) {
668 case POP_TOP:
669 return -1;
670 case ROT_TWO:
671 case ROT_THREE:
672 return 0;
673 case DUP_TOP:
674 return 1;
675 case ROT_FOUR:
676 return 0;
677
678 case UNARY_POSITIVE:
679 case UNARY_NEGATIVE:
680 case UNARY_NOT:
681 case UNARY_CONVERT:
682 case UNARY_INVERT:
683 return 0;
684
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000685 case LIST_APPEND:
686 return -2;
687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 case BINARY_POWER:
689 case BINARY_MULTIPLY:
690 case BINARY_DIVIDE:
691 case BINARY_MODULO:
692 case BINARY_ADD:
693 case BINARY_SUBTRACT:
694 case BINARY_SUBSCR:
695 case BINARY_FLOOR_DIVIDE:
696 case BINARY_TRUE_DIVIDE:
697 return -1;
698 case INPLACE_FLOOR_DIVIDE:
699 case INPLACE_TRUE_DIVIDE:
700 return -1;
701
702 case SLICE+0:
703 return 1;
704 case SLICE+1:
705 return 0;
706 case SLICE+2:
707 return 0;
708 case SLICE+3:
709 return -1;
710
711 case STORE_SLICE+0:
712 return -2;
713 case STORE_SLICE+1:
714 return -3;
715 case STORE_SLICE+2:
716 return -3;
717 case STORE_SLICE+3:
718 return -4;
719
720 case DELETE_SLICE+0:
721 return -1;
722 case DELETE_SLICE+1:
723 return -2;
724 case DELETE_SLICE+2:
725 return -2;
726 case DELETE_SLICE+3:
727 return -3;
728
729 case INPLACE_ADD:
730 case INPLACE_SUBTRACT:
731 case INPLACE_MULTIPLY:
732 case INPLACE_DIVIDE:
733 case INPLACE_MODULO:
734 return -1;
735 case STORE_SUBSCR:
736 return -3;
Raymond Hettingereffde122007-12-18 18:26:18 +0000737 case STORE_MAP:
738 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 case DELETE_SUBSCR:
740 return -2;
741
742 case BINARY_LSHIFT:
743 case BINARY_RSHIFT:
744 case BINARY_AND:
745 case BINARY_XOR:
746 case BINARY_OR:
747 return -1;
748 case INPLACE_POWER:
749 return -1;
750 case GET_ITER:
751 return 0;
752
753 case PRINT_EXPR:
754 return -1;
755 case PRINT_ITEM:
756 return -1;
757 case PRINT_NEWLINE:
758 return 0;
759 case PRINT_ITEM_TO:
760 return -2;
761 case PRINT_NEWLINE_TO:
762 return -1;
763 case INPLACE_LSHIFT:
764 case INPLACE_RSHIFT:
765 case INPLACE_AND:
766 case INPLACE_XOR:
767 case INPLACE_OR:
768 return -1;
769 case BREAK_LOOP:
770 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000771 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000772 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 case LOAD_LOCALS:
774 return 1;
775 case RETURN_VALUE:
776 return -1;
777 case IMPORT_STAR:
778 return -1;
779 case EXEC_STMT:
780 return -3;
781 case YIELD_VALUE:
782 return 0;
783
784 case POP_BLOCK:
785 return 0;
786 case END_FINALLY:
787 return -1; /* or -2 or -3 if exception occurred */
788 case BUILD_CLASS:
789 return -2;
790
791 case STORE_NAME:
792 return -1;
793 case DELETE_NAME:
794 return 0;
795 case UNPACK_SEQUENCE:
796 return oparg-1;
797 case FOR_ITER:
798 return 1;
799
800 case STORE_ATTR:
801 return -2;
802 case DELETE_ATTR:
803 return -1;
804 case STORE_GLOBAL:
805 return -1;
806 case DELETE_GLOBAL:
807 return 0;
808 case DUP_TOPX:
809 return oparg;
810 case LOAD_CONST:
811 return 1;
812 case LOAD_NAME:
813 return 1;
814 case BUILD_TUPLE:
815 case BUILD_LIST:
816 return 1-oparg;
817 case BUILD_MAP:
818 return 1;
819 case LOAD_ATTR:
820 return 0;
821 case COMPARE_OP:
822 return -1;
823 case IMPORT_NAME:
824 return 0;
825 case IMPORT_FROM:
826 return 1;
827
828 case JUMP_FORWARD:
829 case JUMP_IF_FALSE:
830 case JUMP_IF_TRUE:
831 case JUMP_ABSOLUTE:
832 return 0;
833
834 case LOAD_GLOBAL:
835 return 1;
836
837 case CONTINUE_LOOP:
838 return 0;
839 case SETUP_LOOP:
840 return 0;
841 case SETUP_EXCEPT:
842 case SETUP_FINALLY:
843 return 3; /* actually pushed by an exception */
844
845 case LOAD_FAST:
846 return 1;
847 case STORE_FAST:
848 return -1;
849 case DELETE_FAST:
850 return 0;
851
852 case RAISE_VARARGS:
853 return -oparg;
854#define NARGS(o) (((o) % 256) + 2*((o) / 256))
855 case CALL_FUNCTION:
856 return -NARGS(oparg);
857 case CALL_FUNCTION_VAR:
858 case CALL_FUNCTION_KW:
859 return -NARGS(oparg)-1;
860 case CALL_FUNCTION_VAR_KW:
861 return -NARGS(oparg)-2;
862#undef NARGS
863 case MAKE_FUNCTION:
864 return -oparg;
865 case BUILD_SLICE:
866 if (oparg == 3)
867 return -2;
868 else
869 return -1;
870
871 case MAKE_CLOSURE:
872 return -oparg;
873 case LOAD_CLOSURE:
874 return 1;
875 case LOAD_DEREF:
876 return 1;
877 case STORE_DEREF:
878 return -1;
879 default:
880 fprintf(stderr, "opcode = %d\n", opcode);
881 Py_FatalError("opcode_stack_effect()");
882
883 }
884 return 0; /* not reachable */
885}
886
887/* Add an opcode with no argument.
888 Returns 0 on failure, 1 on success.
889*/
890
891static int
892compiler_addop(struct compiler *c, int opcode)
893{
894 basicblock *b;
895 struct instr *i;
896 int off;
897 off = compiler_next_instr(c, c->u->u_curblock);
898 if (off < 0)
899 return 0;
900 b = c->u->u_curblock;
901 i = &b->b_instr[off];
902 i->i_opcode = opcode;
903 i->i_hasarg = 0;
904 if (opcode == RETURN_VALUE)
905 b->b_return = 1;
906 compiler_set_lineno(c, off);
907 return 1;
908}
909
910static int
911compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
912{
913 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000914 Py_ssize_t arg;
Mark Dickinson105be772008-01-31 22:17:37 +0000915 unsigned char *p, *q;
916 Py_complex z;
917 double d;
918 int real_part_zero, imag_part_zero;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000920 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000921 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
922 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000923 d = PyFloat_AS_DOUBLE(o);
924 p = (unsigned char*) &d;
925 /* all we need is to make the tuple different in either the 0.0
926 * or -0.0 case from all others, just to avoid the "coercion".
927 */
928 if (*p==0 && p[sizeof(double)-1]==0)
929 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
930 else
931 t = PyTuple_Pack(2, o, o->ob_type);
932 }
933 else if (PyComplex_Check(o)) {
934 /* 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 }
963 else {
964 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000965 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000966 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000967 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000968
969 v = PyDict_GetItem(dict, t);
970 if (!v) {
971 arg = PyDict_Size(dict);
972 v = PyInt_FromLong(arg);
973 if (!v) {
974 Py_DECREF(t);
975 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000976 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977 if (PyDict_SetItem(dict, t, v) < 0) {
978 Py_DECREF(t);
979 Py_DECREF(v);
980 return -1;
981 }
982 Py_DECREF(v);
983 }
984 else
985 arg = PyInt_AsLong(v);
986 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000987 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988}
989
990static int
991compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
992 PyObject *o)
993{
994 int arg = compiler_add_o(c, dict, o);
995 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000996 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 return compiler_addop_i(c, opcode, arg);
998}
999
1000static int
1001compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001002 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003{
1004 int arg;
1005 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1006 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001007 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001008 arg = compiler_add_o(c, dict, mangled);
1009 Py_DECREF(mangled);
1010 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001011 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 return compiler_addop_i(c, opcode, arg);
1013}
1014
1015/* Add an opcode with an integer argument.
1016 Returns 0 on failure, 1 on success.
1017*/
1018
1019static int
1020compiler_addop_i(struct compiler *c, int opcode, int oparg)
1021{
1022 struct instr *i;
1023 int off;
1024 off = compiler_next_instr(c, c->u->u_curblock);
1025 if (off < 0)
1026 return 0;
1027 i = &c->u->u_curblock->b_instr[off];
1028 i->i_opcode = opcode;
1029 i->i_oparg = oparg;
1030 i->i_hasarg = 1;
1031 compiler_set_lineno(c, off);
1032 return 1;
1033}
1034
1035static int
1036compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1037{
1038 struct instr *i;
1039 int off;
1040
1041 assert(b != NULL);
1042 off = compiler_next_instr(c, c->u->u_curblock);
1043 if (off < 0)
1044 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045 i = &c->u->u_curblock->b_instr[off];
1046 i->i_opcode = opcode;
1047 i->i_target = b;
1048 i->i_hasarg = 1;
1049 if (absolute)
1050 i->i_jabs = 1;
1051 else
1052 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001053 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054 return 1;
1055}
1056
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001057/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1058 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059 it as the current block. NEXT_BLOCK() also creates an implicit jump
1060 from the current block to the new block.
1061*/
1062
Neal Norwitzf733a012006-10-29 18:30:10 +00001063/* The returns inside these macros make it impossible to decref objects
1064 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065*/
1066
1067
1068#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001069 if (compiler_use_new_block((C)) == NULL) \
1070 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071}
1072
1073#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001074 if (compiler_next_block((C)) == NULL) \
1075 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076}
1077
1078#define ADDOP(C, OP) { \
1079 if (!compiler_addop((C), (OP))) \
1080 return 0; \
1081}
1082
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001083#define ADDOP_IN_SCOPE(C, OP) { \
1084 if (!compiler_addop((C), (OP))) { \
1085 compiler_exit_scope(c); \
1086 return 0; \
1087 } \
1088}
1089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001090#define ADDOP_O(C, OP, O, TYPE) { \
1091 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1092 return 0; \
1093}
1094
1095#define ADDOP_NAME(C, OP, O, TYPE) { \
1096 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1097 return 0; \
1098}
1099
1100#define ADDOP_I(C, OP, O) { \
1101 if (!compiler_addop_i((C), (OP), (O))) \
1102 return 0; \
1103}
1104
1105#define ADDOP_JABS(C, OP, O) { \
1106 if (!compiler_addop_j((C), (OP), (O), 1)) \
1107 return 0; \
1108}
1109
1110#define ADDOP_JREL(C, OP, O) { \
1111 if (!compiler_addop_j((C), (OP), (O), 0)) \
1112 return 0; \
1113}
1114
1115/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1116 the ASDL name to synthesize the name of the C type and the visit function.
1117*/
1118
1119#define VISIT(C, TYPE, V) {\
1120 if (!compiler_visit_ ## TYPE((C), (V))) \
1121 return 0; \
1122}
1123
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001124#define VISIT_IN_SCOPE(C, TYPE, V) {\
1125 if (!compiler_visit_ ## TYPE((C), (V))) { \
1126 compiler_exit_scope(c); \
1127 return 0; \
1128 } \
1129}
1130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131#define VISIT_SLICE(C, V, CTX) {\
1132 if (!compiler_visit_slice((C), (V), (CTX))) \
1133 return 0; \
1134}
1135
1136#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001137 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001139 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001140 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001141 if (!compiler_visit_ ## TYPE((C), elt)) \
1142 return 0; \
1143 } \
1144}
1145
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001146#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001147 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001148 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001149 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001150 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001151 if (!compiler_visit_ ## TYPE((C), elt)) { \
1152 compiler_exit_scope(c); \
1153 return 0; \
1154 } \
1155 } \
1156}
1157
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158static int
1159compiler_isdocstring(stmt_ty s)
1160{
1161 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001162 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163 return s->v.Expr.value->kind == Str_kind;
1164}
1165
1166/* Compile a sequence of statements, checking for a docstring. */
1167
1168static int
1169compiler_body(struct compiler *c, asdl_seq *stmts)
1170{
1171 int i = 0;
1172 stmt_ty st;
1173
1174 if (!asdl_seq_LEN(stmts))
1175 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001176 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001177 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1178 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 i = 1;
1180 VISIT(c, expr, st->v.Expr.value);
1181 if (!compiler_nameop(c, __doc__, Store))
1182 return 0;
1183 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001184 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001185 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186 return 1;
1187}
1188
1189static PyCodeObject *
1190compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001191{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001192 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001193 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 static PyObject *module;
1195 if (!module) {
Christian Heimesd7e1b2b2008-01-28 02:07:53 +00001196 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 if (!module)
1198 return NULL;
1199 }
Neal Norwitzed657552006-07-10 00:04:44 +00001200 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1201 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001202 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 switch (mod->kind) {
1204 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001205 if (!compiler_body(c, mod->v.Module.body)) {
1206 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001208 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 break;
1210 case Interactive_kind:
1211 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001212 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001213 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 break;
1215 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001216 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001217 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218 break;
1219 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001220 PyErr_SetString(PyExc_SystemError,
1221 "suite should not be possible");
1222 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001223 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001224 PyErr_Format(PyExc_SystemError,
1225 "module kind %d should not be possible",
1226 mod->kind);
1227 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001228 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001229 co = assemble(c, addNone);
1230 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001231 return co;
1232}
1233
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001234/* The test for LOCAL must come before the test for FREE in order to
1235 handle classes where name is both local and free. The local var is
1236 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001237*/
1238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239static int
1240get_ref_type(struct compiler *c, PyObject *name)
1241{
1242 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001243 if (scope == 0) {
1244 char buf[350];
1245 PyOS_snprintf(buf, sizeof(buf),
1246 "unknown scope for %.100s in %.100s(%s) in %s\n"
1247 "symbols: %s\nlocals: %s\nglobals: %s\n",
1248 PyString_AS_STRING(name),
1249 PyString_AS_STRING(c->u->u_name),
1250 PyObject_REPR(c->u->u_ste->ste_id),
1251 c->c_filename,
1252 PyObject_REPR(c->u->u_ste->ste_symbols),
1253 PyObject_REPR(c->u->u_varnames),
1254 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001256 Py_FatalError(buf);
1257 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001258
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001259 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260}
1261
1262static int
1263compiler_lookup_arg(PyObject *dict, PyObject *name)
1264{
1265 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001266 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001268 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001270 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001272 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 return PyInt_AS_LONG(v);
1274}
1275
1276static int
1277compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1278{
1279 int i, free = PyCode_GetNumFree(co);
1280 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001281 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1282 ADDOP_I(c, MAKE_FUNCTION, args);
1283 return 1;
1284 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 for (i = 0; i < free; ++i) {
1286 /* Bypass com_addop_varname because it will generate
1287 LOAD_DEREF but LOAD_CLOSURE is needed.
1288 */
1289 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1290 int arg, reftype;
1291
1292 /* Special case: If a class contains a method with a
1293 free variable that has the same name as a method,
1294 the name will be considered free *and* local in the
1295 class. It should be handled by the closure, as
1296 well as by the normal name loookup logic.
1297 */
1298 reftype = get_ref_type(c, name);
1299 if (reftype == CELL)
1300 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1301 else /* (reftype == FREE) */
1302 arg = compiler_lookup_arg(c->u->u_freevars, name);
1303 if (arg == -1) {
1304 printf("lookup %s in %s %d %d\n"
1305 "freevars of %s: %s\n",
1306 PyObject_REPR(name),
1307 PyString_AS_STRING(c->u->u_name),
1308 reftype, arg,
1309 PyString_AS_STRING(co->co_name),
1310 PyObject_REPR(co->co_freevars));
1311 Py_FatalError("compiler_make_closure()");
1312 }
1313 ADDOP_I(c, LOAD_CLOSURE, arg);
1314 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001315 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001317 ADDOP_I(c, MAKE_CLOSURE, args);
1318 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319}
1320
1321static int
1322compiler_decorators(struct compiler *c, asdl_seq* decos)
1323{
1324 int i;
1325
1326 if (!decos)
1327 return 1;
1328
1329 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001330 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001331 }
1332 return 1;
1333}
1334
1335static int
1336compiler_arguments(struct compiler *c, arguments_ty args)
1337{
1338 int i;
1339 int n = asdl_seq_LEN(args->args);
1340 /* Correctly handle nested argument lists */
1341 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001342 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343 if (arg->kind == Tuple_kind) {
1344 PyObject *id = PyString_FromFormat(".%d", i);
1345 if (id == NULL) {
1346 return 0;
1347 }
1348 if (!compiler_nameop(c, id, Load)) {
1349 Py_DECREF(id);
1350 return 0;
1351 }
1352 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001353 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001354 }
1355 }
1356 return 1;
1357}
1358
1359static int
1360compiler_function(struct compiler *c, stmt_ty s)
1361{
1362 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001363 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001365 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001366 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367 int i, n, docstring;
1368
1369 assert(s->kind == FunctionDef_kind);
1370
1371 if (!compiler_decorators(c, decos))
1372 return 0;
1373 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001374 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001375 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1376 s->lineno))
1377 return 0;
1378
Anthony Baxter7b782b62006-04-11 12:01:56 +00001379 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001380 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001381 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001382 first_const = st->v.Expr.value->v.Str.s;
1383 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001384 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001385 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001386 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001387
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001388 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001389 compiler_arguments(c, args);
1390
1391 c->u->u_argcount = asdl_seq_LEN(args->args);
1392 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001393 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001394 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001395 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1396 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397 }
1398 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001399 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 if (co == NULL)
1401 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001402
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001403 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001404 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001405
1406 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1407 ADDOP_I(c, CALL_FUNCTION, 1);
1408 }
1409
1410 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1411}
1412
1413static int
1414compiler_class(struct compiler *c, stmt_ty s)
1415{
Christian Heimes5224d282008-02-23 15:01:05 +00001416 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001417 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001418 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001419 asdl_seq* decos = s->v.ClassDef.decorator_list;
1420
1421 if (!compiler_decorators(c, decos))
1422 return 0;
1423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424 /* push class name on stack, needed by BUILD_CLASS */
1425 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1426 /* push the tuple of base classes on the stack */
1427 n = asdl_seq_LEN(s->v.ClassDef.bases);
1428 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001429 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430 ADDOP_I(c, BUILD_TUPLE, n);
1431 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1432 s->lineno))
1433 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001434 c->u->u_private = s->v.ClassDef.name;
1435 Py_INCREF(c->u->u_private);
1436 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437 if (!str || !compiler_nameop(c, str, Load)) {
1438 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001439 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001441 }
1442
1443 Py_DECREF(str);
1444 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 if (!str || !compiler_nameop(c, str, Store)) {
1446 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001447 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001449 }
1450 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001452 if (!compiler_body(c, s->v.ClassDef.body)) {
1453 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001455 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001457 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1458 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001459 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001460 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 if (co == NULL)
1462 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001464 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001465 Py_DECREF(co);
1466
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467 ADDOP_I(c, CALL_FUNCTION, 0);
1468 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001469 /* apply decorators */
1470 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1471 ADDOP_I(c, CALL_FUNCTION, 1);
1472 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1474 return 0;
1475 return 1;
1476}
1477
1478static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001479compiler_ifexp(struct compiler *c, expr_ty e)
1480{
1481 basicblock *end, *next;
1482
1483 assert(e->kind == IfExp_kind);
1484 end = compiler_new_block(c);
1485 if (end == NULL)
1486 return 0;
1487 next = compiler_new_block(c);
1488 if (next == NULL)
1489 return 0;
1490 VISIT(c, expr, e->v.IfExp.test);
1491 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1492 ADDOP(c, POP_TOP);
1493 VISIT(c, expr, e->v.IfExp.body);
1494 ADDOP_JREL(c, JUMP_FORWARD, end);
1495 compiler_use_next_block(c, next);
1496 ADDOP(c, POP_TOP);
1497 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) {
1511 name = PyString_InternFromString("<lambda>");
1512 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);
1526 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001528 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 if (co == NULL)
1530 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001532 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001533 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534
1535 return 1;
1536}
1537
1538static int
1539compiler_print(struct compiler *c, stmt_ty s)
1540{
1541 int i, n;
1542 bool dest;
1543
1544 assert(s->kind == Print_kind);
1545 n = asdl_seq_LEN(s->v.Print.values);
1546 dest = false;
1547 if (s->v.Print.dest) {
1548 VISIT(c, expr, s->v.Print.dest);
1549 dest = true;
1550 }
1551 for (i = 0; i < n; i++) {
1552 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1553 if (dest) {
1554 ADDOP(c, DUP_TOP);
1555 VISIT(c, expr, e);
1556 ADDOP(c, ROT_TWO);
1557 ADDOP(c, PRINT_ITEM_TO);
1558 }
1559 else {
1560 VISIT(c, expr, e);
1561 ADDOP(c, PRINT_ITEM);
1562 }
1563 }
1564 if (s->v.Print.nl) {
1565 if (dest)
1566 ADDOP(c, PRINT_NEWLINE_TO)
1567 else
1568 ADDOP(c, PRINT_NEWLINE)
1569 }
1570 else if (dest)
1571 ADDOP(c, POP_TOP);
1572 return 1;
1573}
1574
1575static int
1576compiler_if(struct compiler *c, stmt_ty s)
1577{
1578 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001579 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001580 assert(s->kind == If_kind);
1581 end = compiler_new_block(c);
1582 if (end == NULL)
1583 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001584 next = compiler_new_block(c);
1585 if (next == NULL)
1586 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001587
1588 constant = expr_constant(s->v.If.test);
1589 /* constant = 0: "if 0"
1590 * constant = 1: "if 1", "if 2", ...
1591 * constant = -1: rest */
1592 if (constant == 0) {
1593 if (s->v.If.orelse)
1594 VISIT_SEQ(c, stmt, s->v.If.orelse);
1595 } else if (constant == 1) {
1596 VISIT_SEQ(c, stmt, s->v.If.body);
1597 } else {
1598 VISIT(c, expr, s->v.If.test);
1599 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1600 ADDOP(c, POP_TOP);
1601 VISIT_SEQ(c, stmt, s->v.If.body);
1602 ADDOP_JREL(c, JUMP_FORWARD, end);
1603 compiler_use_next_block(c, next);
1604 ADDOP(c, POP_TOP);
1605 if (s->v.If.orelse)
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001606 VISIT_SEQ(c, stmt, s->v.If.orelse);
Georg Brandlddbaa662006-06-04 21:56:52 +00001607 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001608 compiler_use_next_block(c, end);
1609 return 1;
1610}
1611
1612static int
1613compiler_for(struct compiler *c, stmt_ty s)
1614{
1615 basicblock *start, *cleanup, *end;
1616
1617 start = compiler_new_block(c);
1618 cleanup = compiler_new_block(c);
1619 end = compiler_new_block(c);
1620 if (start == NULL || end == NULL || cleanup == NULL)
1621 return 0;
1622 ADDOP_JREL(c, SETUP_LOOP, end);
1623 if (!compiler_push_fblock(c, LOOP, start))
1624 return 0;
1625 VISIT(c, expr, s->v.For.iter);
1626 ADDOP(c, GET_ITER);
1627 compiler_use_next_block(c, start);
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001628 /* for expressions must be traced on each iteration,
1629 so we need to set an extra line number. */
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001630 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001631 ADDOP_JREL(c, FOR_ITER, cleanup);
1632 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001633 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001634 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1635 compiler_use_next_block(c, cleanup);
1636 ADDOP(c, POP_BLOCK);
1637 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001638 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001639 compiler_use_next_block(c, end);
1640 return 1;
1641}
1642
1643static int
1644compiler_while(struct compiler *c, stmt_ty s)
1645{
1646 basicblock *loop, *orelse, *end, *anchor = NULL;
1647 int constant = expr_constant(s->v.While.test);
1648
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001649 if (constant == 0) {
1650 if (s->v.While.orelse)
1651 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001653 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 loop = compiler_new_block(c);
1655 end = compiler_new_block(c);
1656 if (constant == -1) {
1657 anchor = compiler_new_block(c);
1658 if (anchor == NULL)
1659 return 0;
1660 }
1661 if (loop == NULL || end == NULL)
1662 return 0;
1663 if (s->v.While.orelse) {
1664 orelse = compiler_new_block(c);
1665 if (orelse == NULL)
1666 return 0;
1667 }
1668 else
1669 orelse = NULL;
1670
1671 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001672 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 if (!compiler_push_fblock(c, LOOP, loop))
1674 return 0;
1675 if (constant == -1) {
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001676 /* while expressions must be traced on each iteration,
1677 so we need to set an extra line number. */
1678 c->u->u_lineno_set = false;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679 VISIT(c, expr, s->v.While.test);
1680 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1681 ADDOP(c, POP_TOP);
1682 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001683 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1685
1686 /* XXX should the two POP instructions be in a separate block
1687 if there is no else clause ?
1688 */
1689
1690 if (constant == -1) {
1691 compiler_use_next_block(c, anchor);
1692 ADDOP(c, POP_TOP);
1693 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
1813 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1814 [tb, val, exc, 1] POP )
1815 [tb, val, exc] POP
1816 [tb, val] <assign to V1> (or POP if no V1)
1817 [tb] POP
1818 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001819 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820
1821 [tb, val, exc, 0] L2: POP
1822 [tb, val, exc] DUP
1823 .............................etc.......................
1824
1825 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001826 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827
1828 [] L0: <next statement>
1829
1830 Of course, parts are not generated if Vi or Ei is not present.
1831*/
1832static int
1833compiler_try_except(struct compiler *c, stmt_ty s)
1834{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001835 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836 int i, n;
1837
1838 body = compiler_new_block(c);
1839 except = compiler_new_block(c);
1840 orelse = compiler_new_block(c);
1841 end = compiler_new_block(c);
1842 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1843 return 0;
1844 ADDOP_JREL(c, SETUP_EXCEPT, except);
1845 compiler_use_next_block(c, body);
1846 if (!compiler_push_fblock(c, EXCEPT, body))
1847 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001848 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849 ADDOP(c, POP_BLOCK);
1850 compiler_pop_fblock(c, EXCEPT, body);
1851 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1852 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1853 compiler_use_next_block(c, except);
1854 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001855 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001856 s->v.TryExcept.handlers, i);
1857 if (!handler->type && i < n-1)
1858 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001859 c->u->u_lineno_set = false;
1860 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861 except = compiler_new_block(c);
1862 if (except == NULL)
1863 return 0;
1864 if (handler->type) {
1865 ADDOP(c, DUP_TOP);
1866 VISIT(c, expr, handler->type);
1867 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1868 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1869 ADDOP(c, POP_TOP);
1870 }
1871 ADDOP(c, POP_TOP);
1872 if (handler->name) {
1873 VISIT(c, expr, handler->name);
1874 }
1875 else {
1876 ADDOP(c, POP_TOP);
1877 }
1878 ADDOP(c, POP_TOP);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001879 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880 ADDOP_JREL(c, JUMP_FORWARD, end);
1881 compiler_use_next_block(c, except);
1882 if (handler->type)
1883 ADDOP(c, POP_TOP);
1884 }
1885 ADDOP(c, END_FINALLY);
1886 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001887 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888 compiler_use_next_block(c, end);
1889 return 1;
1890}
1891
1892static int
1893compiler_import_as(struct compiler *c, identifier name, identifier asname)
1894{
1895 /* The IMPORT_NAME opcode was already generated. This function
1896 merely needs to bind the result to a name.
1897
1898 If there is a dot in name, we need to split it and emit a
1899 LOAD_ATTR for each name.
1900 */
1901 const char *src = PyString_AS_STRING(name);
1902 const char *dot = strchr(src, '.');
1903 if (dot) {
1904 /* Consume the base module name to get the first attribute */
1905 src = dot + 1;
1906 while (dot) {
1907 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001908 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001909 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00001910 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001912 if (!attr)
1913 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001915 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001916 src = dot + 1;
1917 }
1918 }
1919 return compiler_nameop(c, asname, Store);
1920}
1921
1922static int
1923compiler_import(struct compiler *c, stmt_ty s)
1924{
1925 /* The Import node stores a module name like a.b.c as a single
1926 string. This is convenient for all cases except
1927 import a.b.c as d
1928 where we need to parse that string to extract the individual
1929 module names.
1930 XXX Perhaps change the representation to make this case simpler?
1931 */
1932 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001933
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001935 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001936 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001937 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001938
Neal Norwitzcbce2802006-04-03 06:26:32 +00001939 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001940 level = PyInt_FromLong(0);
1941 else
1942 level = PyInt_FromLong(-1);
1943
1944 if (level == NULL)
1945 return 0;
1946
1947 ADDOP_O(c, LOAD_CONST, level, consts);
1948 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001949 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1950 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1951
1952 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001953 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001954 if (!r)
1955 return r;
1956 }
1957 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 identifier tmp = alias->name;
1959 const char *base = PyString_AS_STRING(alias->name);
1960 char *dot = strchr(base, '.');
1961 if (dot)
1962 tmp = PyString_FromStringAndSize(base,
1963 dot - base);
1964 r = compiler_nameop(c, tmp, Store);
1965 if (dot) {
1966 Py_DECREF(tmp);
1967 }
1968 if (!r)
1969 return r;
1970 }
1971 }
1972 return 1;
1973}
1974
1975static int
1976compiler_from_import(struct compiler *c, stmt_ty s)
1977{
1978 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979
1980 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001981 PyObject *level;
1982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 if (!names)
1984 return 0;
1985
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001986 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001987 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001988 level = PyInt_FromLong(-1);
1989 else
1990 level = PyInt_FromLong(s->v.ImportFrom.level);
1991
1992 if (!level) {
1993 Py_DECREF(names);
1994 return 0;
1995 }
1996
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001997 /* build up the names */
1998 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001999 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002000 Py_INCREF(alias->name);
2001 PyTuple_SET_ITEM(names, i, alias->name);
2002 }
2003
2004 if (s->lineno > c->c_future->ff_lineno) {
2005 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2006 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002007 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002008 Py_DECREF(names);
2009 return compiler_error(c,
2010 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002011 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012
2013 }
2014 }
2015
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002016 ADDOP_O(c, LOAD_CONST, level, consts);
2017 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002019 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2021 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002022 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 identifier store_name;
2024
2025 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2026 assert(n == 1);
2027 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002028 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002029 }
2030
2031 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2032 store_name = alias->name;
2033 if (alias->asname)
2034 store_name = alias->asname;
2035
2036 if (!compiler_nameop(c, store_name, Store)) {
2037 Py_DECREF(names);
2038 return 0;
2039 }
2040 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002041 /* remove imported module */
2042 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002043 return 1;
2044}
2045
2046static int
2047compiler_assert(struct compiler *c, stmt_ty s)
2048{
2049 static PyObject *assertion_error = NULL;
2050 basicblock *end;
2051
2052 if (Py_OptimizeFlag)
2053 return 1;
2054 if (assertion_error == NULL) {
Christian Heimesd7e1b2b2008-01-28 02:07:53 +00002055 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002056 if (assertion_error == NULL)
2057 return 0;
2058 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002059 if (s->v.Assert.test->kind == Tuple_kind &&
2060 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2061 const char* msg =
2062 "assertion is always true, perhaps remove parentheses?";
2063 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2064 c->u->u_lineno, NULL, NULL) == -1)
2065 return 0;
2066 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 VISIT(c, expr, s->v.Assert.test);
2068 end = compiler_new_block(c);
2069 if (end == NULL)
2070 return 0;
2071 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2072 ADDOP(c, POP_TOP);
2073 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2074 if (s->v.Assert.msg) {
2075 VISIT(c, expr, s->v.Assert.msg);
2076 ADDOP_I(c, RAISE_VARARGS, 2);
2077 }
2078 else {
2079 ADDOP_I(c, RAISE_VARARGS, 1);
2080 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002081 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082 ADDOP(c, POP_TOP);
2083 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
2336 /* First check for assignment to __debug__. Param? */
2337 if ((ctx == Store || ctx == AugStore || ctx == Del)
2338 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2339 return compiler_error(c, "can not assign to __debug__");
2340 }
2341
Neal Norwitz0031ff32008-02-25 01:45:37 +00002342 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002343 if (!mangled)
2344 return 0;
2345
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 op = 0;
2347 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002348 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002349 switch (scope) {
2350 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002351 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002352 optype = OP_DEREF;
2353 break;
2354 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002355 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002356 optype = OP_DEREF;
2357 break;
2358 case LOCAL:
2359 if (c->u->u_ste->ste_type == FunctionBlock)
2360 optype = OP_FAST;
2361 break;
2362 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002363 if (c->u->u_ste->ste_type == FunctionBlock &&
2364 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002365 optype = OP_GLOBAL;
2366 break;
2367 case GLOBAL_EXPLICIT:
2368 optype = OP_GLOBAL;
2369 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002370 default:
2371 /* scope can be 0 */
2372 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373 }
2374
2375 /* XXX Leave assert here, but handle __doc__ and the like better */
2376 assert(scope || PyString_AS_STRING(name)[0] == '_');
2377
2378 switch (optype) {
2379 case OP_DEREF:
2380 switch (ctx) {
2381 case Load: op = LOAD_DEREF; break;
2382 case Store: op = STORE_DEREF; break;
2383 case AugLoad:
2384 case AugStore:
2385 break;
2386 case Del:
2387 PyErr_Format(PyExc_SyntaxError,
2388 "can not delete variable '%s' referenced "
2389 "in nested scope",
2390 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002391 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002393 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002394 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002395 PyErr_SetString(PyExc_SystemError,
2396 "param invalid for deref variable");
2397 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 }
2399 break;
2400 case OP_FAST:
2401 switch (ctx) {
2402 case Load: op = LOAD_FAST; break;
2403 case Store: op = STORE_FAST; break;
2404 case Del: op = DELETE_FAST; break;
2405 case AugLoad:
2406 case AugStore:
2407 break;
2408 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002409 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002410 PyErr_SetString(PyExc_SystemError,
2411 "param invalid for local variable");
2412 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002413 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002414 ADDOP_O(c, op, mangled, varnames);
2415 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002416 return 1;
2417 case OP_GLOBAL:
2418 switch (ctx) {
2419 case Load: op = LOAD_GLOBAL; break;
2420 case Store: op = STORE_GLOBAL; break;
2421 case Del: op = DELETE_GLOBAL; break;
2422 case AugLoad:
2423 case AugStore:
2424 break;
2425 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002426 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002427 PyErr_SetString(PyExc_SystemError,
2428 "param invalid for global variable");
2429 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 }
2431 break;
2432 case OP_NAME:
2433 switch (ctx) {
2434 case Load: op = LOAD_NAME; break;
2435 case Store: op = STORE_NAME; break;
2436 case Del: op = DELETE_NAME; break;
2437 case AugLoad:
2438 case AugStore:
2439 break;
2440 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002441 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002442 PyErr_SetString(PyExc_SystemError,
2443 "param invalid for name variable");
2444 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 }
2446 break;
2447 }
2448
2449 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002450 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002451 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002452 if (arg < 0)
2453 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002454 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455}
2456
2457static int
2458compiler_boolop(struct compiler *c, expr_ty e)
2459{
2460 basicblock *end;
2461 int jumpi, i, n;
2462 asdl_seq *s;
2463
2464 assert(e->kind == BoolOp_kind);
2465 if (e->v.BoolOp.op == And)
2466 jumpi = JUMP_IF_FALSE;
2467 else
2468 jumpi = JUMP_IF_TRUE;
2469 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002470 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 return 0;
2472 s = e->v.BoolOp.values;
2473 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002474 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002475 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002476 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 ADDOP_JREL(c, jumpi, end);
2478 ADDOP(c, POP_TOP)
2479 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002480 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002481 compiler_use_next_block(c, end);
2482 return 1;
2483}
2484
2485static int
2486compiler_list(struct compiler *c, expr_ty e)
2487{
2488 int n = asdl_seq_LEN(e->v.List.elts);
2489 if (e->v.List.ctx == Store) {
2490 ADDOP_I(c, UNPACK_SEQUENCE, n);
2491 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002492 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 if (e->v.List.ctx == Load) {
2494 ADDOP_I(c, BUILD_LIST, n);
2495 }
2496 return 1;
2497}
2498
2499static int
2500compiler_tuple(struct compiler *c, expr_ty e)
2501{
2502 int n = asdl_seq_LEN(e->v.Tuple.elts);
2503 if (e->v.Tuple.ctx == Store) {
2504 ADDOP_I(c, UNPACK_SEQUENCE, n);
2505 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002506 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507 if (e->v.Tuple.ctx == Load) {
2508 ADDOP_I(c, BUILD_TUPLE, n);
2509 }
2510 return 1;
2511}
2512
2513static int
2514compiler_compare(struct compiler *c, expr_ty e)
2515{
2516 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002517 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518
2519 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2520 VISIT(c, expr, e->v.Compare.left);
2521 n = asdl_seq_LEN(e->v.Compare.ops);
2522 assert(n > 0);
2523 if (n > 1) {
2524 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002525 if (cleanup == NULL)
2526 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002527 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002528 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 }
2530 for (i = 1; i < n; i++) {
2531 ADDOP(c, DUP_TOP);
2532 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002534 cmpop((cmpop_ty)(asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002535 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2537 NEXT_BLOCK(c);
2538 ADDOP(c, POP_TOP);
2539 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002540 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002541 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002543 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002544 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002545 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 if (n > 1) {
2547 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002548 if (end == NULL)
2549 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 ADDOP_JREL(c, JUMP_FORWARD, end);
2551 compiler_use_next_block(c, cleanup);
2552 ADDOP(c, ROT_TWO);
2553 ADDOP(c, POP_TOP);
2554 compiler_use_next_block(c, end);
2555 }
2556 return 1;
2557}
2558
2559static int
2560compiler_call(struct compiler *c, expr_ty e)
2561{
2562 int n, code = 0;
2563
2564 VISIT(c, expr, e->v.Call.func);
2565 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002566 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002567 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002568 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002569 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2570 }
2571 if (e->v.Call.starargs) {
2572 VISIT(c, expr, e->v.Call.starargs);
2573 code |= 1;
2574 }
2575 if (e->v.Call.kwargs) {
2576 VISIT(c, expr, e->v.Call.kwargs);
2577 code |= 2;
2578 }
2579 switch (code) {
2580 case 0:
2581 ADDOP_I(c, CALL_FUNCTION, n);
2582 break;
2583 case 1:
2584 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2585 break;
2586 case 2:
2587 ADDOP_I(c, CALL_FUNCTION_KW, n);
2588 break;
2589 case 3:
2590 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2591 break;
2592 }
2593 return 1;
2594}
2595
2596static int
2597compiler_listcomp_generator(struct compiler *c, PyObject *tmpname,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002598 asdl_seq *generators, int gen_index,
2599 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600{
2601 /* generate code for the iterator, then each of the ifs,
2602 and then write to the element */
2603
2604 comprehension_ty l;
2605 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002606 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607
2608 start = compiler_new_block(c);
2609 skip = compiler_new_block(c);
2610 if_cleanup = compiler_new_block(c);
2611 anchor = compiler_new_block(c);
2612
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002613 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2614 anchor == NULL)
2615 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616
Anthony Baxter7b782b62006-04-11 12:01:56 +00002617 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618 VISIT(c, expr, l->iter);
2619 ADDOP(c, GET_ITER);
2620 compiler_use_next_block(c, start);
2621 ADDOP_JREL(c, FOR_ITER, anchor);
2622 NEXT_BLOCK(c);
2623 VISIT(c, expr, l->target);
2624
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002625 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002626 n = asdl_seq_LEN(l->ifs);
2627 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002628 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629 VISIT(c, expr, e);
2630 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2631 NEXT_BLOCK(c);
2632 ADDOP(c, POP_TOP);
2633 }
2634
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002635 if (++gen_index < asdl_seq_LEN(generators))
2636 if (!compiler_listcomp_generator(c, tmpname,
2637 generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002640 /* only append after the last for generator */
2641 if (gen_index >= asdl_seq_LEN(generators)) {
2642 if (!compiler_nameop(c, tmpname, Load))
2643 return 0;
2644 VISIT(c, expr, elt);
Neal Norwitz10be2ea2006-03-03 20:29:11 +00002645 ADDOP(c, LIST_APPEND);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002646
2647 compiler_use_next_block(c, skip);
2648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 for (i = 0; i < n; i++) {
2650 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002651 if (i == 0)
2652 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 ADDOP(c, POP_TOP);
2654 }
2655 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2656 compiler_use_next_block(c, anchor);
Georg Brandl2c4fb8d2006-10-29 08:47:08 +00002657 /* delete the temporary list name added to locals */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 if (gen_index == 1)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002659 if (!compiler_nameop(c, tmpname, Del))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002660 return 0;
2661
2662 return 1;
2663}
2664
2665static int
2666compiler_listcomp(struct compiler *c, expr_ty e)
2667{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 identifier tmp;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002669 int rc = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 asdl_seq *generators = e->v.ListComp.generators;
2671
2672 assert(e->kind == ListComp_kind);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002673 tmp = compiler_new_tmpname(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674 if (!tmp)
2675 return 0;
2676 ADDOP_I(c, BUILD_LIST, 0);
2677 ADDOP(c, DUP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 if (compiler_nameop(c, tmp, Store))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002679 rc = compiler_listcomp_generator(c, tmp, generators, 0,
2680 e->v.ListComp.elt);
2681 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 return rc;
2683}
2684
2685static int
2686compiler_genexp_generator(struct compiler *c,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002687 asdl_seq *generators, int gen_index,
2688 expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689{
2690 /* generate code for the iterator, then each of the ifs,
2691 and then write to the element */
2692
2693 comprehension_ty ge;
2694 basicblock *start, *anchor, *skip, *if_cleanup, *end;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002695 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002696
2697 start = compiler_new_block(c);
2698 skip = compiler_new_block(c);
2699 if_cleanup = compiler_new_block(c);
2700 anchor = compiler_new_block(c);
2701 end = compiler_new_block(c);
2702
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002703 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002704 anchor == NULL || end == NULL)
2705 return 0;
2706
Anthony Baxter7b782b62006-04-11 12:01:56 +00002707 ge = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 ADDOP_JREL(c, SETUP_LOOP, end);
2709 if (!compiler_push_fblock(c, LOOP, start))
2710 return 0;
2711
2712 if (gen_index == 0) {
2713 /* Receive outermost iter as an implicit argument */
2714 c->u->u_argcount = 1;
2715 ADDOP_I(c, LOAD_FAST, 0);
2716 }
2717 else {
2718 /* Sub-iter - calculate on the fly */
2719 VISIT(c, expr, ge->iter);
2720 ADDOP(c, GET_ITER);
2721 }
2722 compiler_use_next_block(c, start);
2723 ADDOP_JREL(c, FOR_ITER, anchor);
2724 NEXT_BLOCK(c);
2725 VISIT(c, expr, ge->target);
2726
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002727 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002728 n = asdl_seq_LEN(ge->ifs);
2729 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002730 expr_ty e = (expr_ty)asdl_seq_GET(ge->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 VISIT(c, expr, e);
2732 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2733 NEXT_BLOCK(c);
2734 ADDOP(c, POP_TOP);
2735 }
2736
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002737 if (++gen_index < asdl_seq_LEN(generators))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738 if (!compiler_genexp_generator(c, generators, gen_index, elt))
2739 return 0;
2740
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002741 /* only append after the last 'for' generator */
2742 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 VISIT(c, expr, elt);
2744 ADDOP(c, YIELD_VALUE);
2745 ADDOP(c, POP_TOP);
2746
2747 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002748 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749 for (i = 0; i < n; i++) {
2750 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002751 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 compiler_use_next_block(c, if_cleanup);
2753
2754 ADDOP(c, POP_TOP);
2755 }
2756 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2757 compiler_use_next_block(c, anchor);
2758 ADDOP(c, POP_BLOCK);
2759 compiler_pop_fblock(c, LOOP, start);
2760 compiler_use_next_block(c, end);
2761
2762 return 1;
2763}
2764
2765static int
2766compiler_genexp(struct compiler *c, expr_ty e)
2767{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002768 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 PyCodeObject *co;
2770 expr_ty outermost_iter = ((comprehension_ty)
2771 (asdl_seq_GET(e->v.GeneratorExp.generators,
2772 0)))->iter;
2773
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002774 if (!name) {
2775 name = PyString_FromString("<genexpr>");
2776 if (!name)
2777 return 0;
2778 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779
2780 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2781 return 0;
2782 compiler_genexp_generator(c, e->v.GeneratorExp.generators, 0,
2783 e->v.GeneratorExp.elt);
2784 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00002785 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786 if (co == NULL)
2787 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002789 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00002790 Py_DECREF(co);
2791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002792 VISIT(c, expr, outermost_iter);
2793 ADDOP(c, GET_ITER);
2794 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795
2796 return 1;
2797}
2798
2799static int
2800compiler_visit_keyword(struct compiler *c, keyword_ty k)
2801{
2802 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2803 VISIT(c, expr, k->value);
2804 return 1;
2805}
2806
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002807/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808 whether they are true or false.
2809
2810 Return values: 1 for true, 0 for false, -1 for non-constant.
2811 */
2812
2813static int
2814expr_constant(expr_ty e)
2815{
2816 switch (e->kind) {
2817 case Num_kind:
2818 return PyObject_IsTrue(e->v.Num.n);
2819 case Str_kind:
2820 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002821 case Name_kind:
2822 /* __debug__ is not assignable, so we can optimize
2823 * it away in if and while statements */
2824 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002825 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002826 return ! Py_OptimizeFlag;
2827 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 default:
2829 return -1;
2830 }
2831}
2832
Guido van Rossumc2e20742006-02-27 22:32:47 +00002833/*
2834 Implements the with statement from PEP 343.
2835
2836 The semantics outlined in that PEP are as follows:
2837
2838 with EXPR as VAR:
2839 BLOCK
2840
2841 It is implemented roughly as:
2842
Guido van Rossumda5b7012006-05-02 19:47:52 +00002843 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002844 exit = context.__exit__ # not calling it
2845 value = context.__enter__()
2846 try:
2847 VAR = value # if VAR present in the syntax
2848 BLOCK
2849 finally:
2850 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002851 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002852 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002853 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002854 exit(*exc)
2855 */
2856static int
2857compiler_with(struct compiler *c, stmt_ty s)
2858{
Guido van Rossumda5b7012006-05-02 19:47:52 +00002859 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002860 basicblock *block, *finally;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002861 identifier tmpvalue = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002862
2863 assert(s->kind == With_kind);
2864
Guido van Rossumc2e20742006-02-27 22:32:47 +00002865 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002866 enter_attr = PyString_InternFromString("__enter__");
2867 if (!enter_attr)
2868 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002869 }
2870 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002871 exit_attr = PyString_InternFromString("__exit__");
2872 if (!exit_attr)
2873 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002874 }
2875
2876 block = compiler_new_block(c);
2877 finally = compiler_new_block(c);
2878 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002879 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002880
Guido van Rossumc2e20742006-02-27 22:32:47 +00002881 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002882 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00002883 We need to do this rather than preserving it on the stack
2884 because SETUP_FINALLY remembers the stack level.
2885 We need to do the assignment *inside* the try/finally
2886 so that context.__exit__() is called when the assignment
2887 fails. But we need to call context.__enter__() *before*
2888 the try/finally so that if it fails we won't call
2889 context.__exit__().
2890 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002891 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002892 if (tmpvalue == NULL)
2893 return 0;
2894 PyArena_AddPyObject(c->c_arena, tmpvalue);
2895 }
2896
Guido van Rossumda5b7012006-05-02 19:47:52 +00002897 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002898 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002899
Nick Coghlan7af53be2008-03-07 14:13:28 +00002900 /* Squirrel away context.__exit__ by stuffing it under context */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002901 ADDOP(c, DUP_TOP);
2902 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
Nick Coghlan7af53be2008-03-07 14:13:28 +00002903 ADDOP(c, ROT_TWO);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002904
2905 /* Call context.__enter__() */
2906 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
2907 ADDOP_I(c, CALL_FUNCTION, 0);
2908
2909 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002910 /* Store it in tmpvalue */
2911 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00002912 return 0;
2913 }
2914 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002915 /* Discard result from context.__enter__() */
2916 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002917 }
2918
2919 /* Start the try block */
2920 ADDOP_JREL(c, SETUP_FINALLY, finally);
2921
2922 compiler_use_next_block(c, block);
2923 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002924 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002925 }
2926
2927 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002928 /* Bind saved result of context.__enter__() to VAR */
2929 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00002930 !compiler_nameop(c, tmpvalue, Del))
2931 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002932 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002933 }
2934
2935 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002936 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002937
2938 /* End of try block; start the finally block */
2939 ADDOP(c, POP_BLOCK);
2940 compiler_pop_fblock(c, FINALLY_TRY, block);
2941
2942 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2943 compiler_use_next_block(c, finally);
2944 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002945 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002946
Nick Coghlan7af53be2008-03-07 14:13:28 +00002947 /* Finally block starts; context.__exit__ is on the stack under
2948 the exception or return information. Just issue our magic
2949 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002950 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002951
2952 /* Finally block ends. */
2953 ADDOP(c, END_FINALLY);
2954 compiler_pop_fblock(c, FINALLY_END, finally);
2955 return 1;
2956}
2957
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958static int
2959compiler_visit_expr(struct compiler *c, expr_ty e)
2960{
2961 int i, n;
2962
Neal Norwitzf733a012006-10-29 18:30:10 +00002963 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002964 set a new line number for the next instruction.
2965 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 if (e->lineno > c->u->u_lineno) {
2967 c->u->u_lineno = e->lineno;
2968 c->u->u_lineno_set = false;
2969 }
2970 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002971 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002973 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 VISIT(c, expr, e->v.BinOp.left);
2975 VISIT(c, expr, e->v.BinOp.right);
2976 ADDOP(c, binop(c, e->v.BinOp.op));
2977 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002978 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979 VISIT(c, expr, e->v.UnaryOp.operand);
2980 ADDOP(c, unaryop(e->v.UnaryOp.op));
2981 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002982 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002984 case IfExp_kind:
2985 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002986 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00002988 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002990 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002991 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00002992 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002993 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00002994 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002995 }
2996 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002997 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002998 return compiler_listcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002999 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 return compiler_genexp(c, e);
3001 case Yield_kind:
3002 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003003 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 if (e->v.Yield.value) {
3005 VISIT(c, expr, e->v.Yield.value);
3006 }
3007 else {
3008 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3009 }
3010 ADDOP(c, YIELD_VALUE);
3011 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003012 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003014 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003016 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017 VISIT(c, expr, e->v.Repr.value);
3018 ADDOP(c, UNARY_CONVERT);
3019 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003020 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3022 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003023 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3025 break;
3026 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003027 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003028 if (e->v.Attribute.ctx != AugStore)
3029 VISIT(c, expr, e->v.Attribute.value);
3030 switch (e->v.Attribute.ctx) {
3031 case AugLoad:
3032 ADDOP(c, DUP_TOP);
3033 /* Fall through to load */
3034 case Load:
3035 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3036 break;
3037 case AugStore:
3038 ADDOP(c, ROT_TWO);
3039 /* Fall through to save */
3040 case Store:
3041 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3042 break;
3043 case Del:
3044 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3045 break;
3046 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003047 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003048 PyErr_SetString(PyExc_SystemError,
3049 "param invalid in attribute expression");
3050 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003051 }
3052 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003053 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 switch (e->v.Subscript.ctx) {
3055 case AugLoad:
3056 VISIT(c, expr, e->v.Subscript.value);
3057 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3058 break;
3059 case Load:
3060 VISIT(c, expr, e->v.Subscript.value);
3061 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3062 break;
3063 case AugStore:
3064 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3065 break;
3066 case Store:
3067 VISIT(c, expr, e->v.Subscript.value);
3068 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3069 break;
3070 case Del:
3071 VISIT(c, expr, e->v.Subscript.value);
3072 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3073 break;
3074 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003075 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003076 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003077 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003078 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003079 }
3080 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003081 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003082 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3083 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003084 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003085 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003086 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003087 return compiler_tuple(c, e);
3088 }
3089 return 1;
3090}
3091
3092static int
3093compiler_augassign(struct compiler *c, stmt_ty s)
3094{
3095 expr_ty e = s->v.AugAssign.target;
3096 expr_ty auge;
3097
3098 assert(s->kind == AugAssign_kind);
3099
3100 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003101 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003102 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003103 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003104 if (auge == NULL)
3105 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003106 VISIT(c, expr, auge);
3107 VISIT(c, expr, s->v.AugAssign.value);
3108 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3109 auge->v.Attribute.ctx = AugStore;
3110 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 break;
3112 case Subscript_kind:
3113 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003114 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003115 if (auge == NULL)
3116 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003117 VISIT(c, expr, auge);
3118 VISIT(c, expr, s->v.AugAssign.value);
3119 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003122 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003124 if (!compiler_nameop(c, e->v.Name.id, Load))
3125 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 VISIT(c, expr, s->v.AugAssign.value);
3127 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3128 return compiler_nameop(c, e->v.Name.id, Store);
3129 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003130 PyErr_Format(PyExc_SystemError,
3131 "invalid node type (%d) for augmented assignment",
3132 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003133 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 }
3135 return 1;
3136}
3137
3138static int
3139compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3140{
3141 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003142 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3143 PyErr_SetString(PyExc_SystemError,
3144 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 f = &c->u->u_fblock[c->u->u_nfblocks++];
3148 f->fb_type = t;
3149 f->fb_block = b;
3150 return 1;
3151}
3152
3153static void
3154compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3155{
3156 struct compiler_unit *u = c->u;
3157 assert(u->u_nfblocks > 0);
3158 u->u_nfblocks--;
3159 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3160 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3161}
3162
Jeremy Hylton82271f12006-10-04 02:24:52 +00003163static int
3164compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003165 int i;
3166 struct compiler_unit *u = c->u;
3167 for (i = 0; i < u->u_nfblocks; ++i) {
3168 if (u->u_fblock[i].fb_type == LOOP)
3169 return 1;
3170 }
3171 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003172}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173/* Raises a SyntaxError and returns 0.
3174 If something goes wrong, a different exception may be raised.
3175*/
3176
3177static int
3178compiler_error(struct compiler *c, const char *errstr)
3179{
3180 PyObject *loc;
3181 PyObject *u = NULL, *v = NULL;
3182
3183 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3184 if (!loc) {
3185 Py_INCREF(Py_None);
3186 loc = Py_None;
3187 }
3188 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3189 Py_None, loc);
3190 if (!u)
3191 goto exit;
3192 v = Py_BuildValue("(zO)", errstr, u);
3193 if (!v)
3194 goto exit;
3195 PyErr_SetObject(PyExc_SyntaxError, v);
3196 exit:
3197 Py_DECREF(loc);
3198 Py_XDECREF(u);
3199 Py_XDECREF(v);
3200 return 0;
3201}
3202
3203static int
3204compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003205 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003207 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003209 /* XXX this code is duplicated */
3210 switch (ctx) {
3211 case AugLoad: /* fall through to Load */
3212 case Load: op = BINARY_SUBSCR; break;
3213 case AugStore:/* fall through to Store */
3214 case Store: op = STORE_SUBSCR; break;
3215 case Del: op = DELETE_SUBSCR; break;
3216 case Param:
3217 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003218 "invalid %s kind %d in subscript\n",
3219 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003220 return 0;
3221 }
3222 if (ctx == AugLoad) {
3223 ADDOP_I(c, DUP_TOPX, 2);
3224 }
3225 else if (ctx == AugStore) {
3226 ADDOP(c, ROT_THREE);
3227 }
3228 ADDOP(c, op);
3229 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230}
3231
3232static int
3233compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3234{
3235 int n = 2;
3236 assert(s->kind == Slice_kind);
3237
3238 /* only handles the cases where BUILD_SLICE is emitted */
3239 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003240 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 }
3242 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003243 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003244 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003246 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003247 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 }
3249 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003250 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 }
3252
3253 if (s->v.Slice.step) {
3254 n++;
3255 VISIT(c, expr, s->v.Slice.step);
3256 }
3257 ADDOP_I(c, BUILD_SLICE, n);
3258 return 1;
3259}
3260
3261static int
3262compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3263{
3264 int op = 0, slice_offset = 0, stack_count = 0;
3265
3266 assert(s->v.Slice.step == NULL);
3267 if (s->v.Slice.lower) {
3268 slice_offset++;
3269 stack_count++;
3270 if (ctx != AugStore)
3271 VISIT(c, expr, s->v.Slice.lower);
3272 }
3273 if (s->v.Slice.upper) {
3274 slice_offset += 2;
3275 stack_count++;
3276 if (ctx != AugStore)
3277 VISIT(c, expr, s->v.Slice.upper);
3278 }
3279
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003280 if (ctx == AugLoad) {
3281 switch (stack_count) {
3282 case 0: ADDOP(c, DUP_TOP); break;
3283 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3284 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3285 }
3286 }
3287 else if (ctx == AugStore) {
3288 switch (stack_count) {
3289 case 0: ADDOP(c, ROT_TWO); break;
3290 case 1: ADDOP(c, ROT_THREE); break;
3291 case 2: ADDOP(c, ROT_FOUR); break;
3292 }
3293 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294
3295 switch (ctx) {
3296 case AugLoad: /* fall through to Load */
3297 case Load: op = SLICE; break;
3298 case AugStore:/* fall through to Store */
3299 case Store: op = STORE_SLICE; break;
3300 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003301 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003302 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003303 PyErr_SetString(PyExc_SystemError,
3304 "param invalid in simple slice");
3305 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 }
3307
3308 ADDOP(c, op + slice_offset);
3309 return 1;
3310}
3311
3312static int
3313compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3314 expr_context_ty ctx)
3315{
3316 switch (s->kind) {
3317 case Ellipsis_kind:
3318 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3319 break;
3320 case Slice_kind:
3321 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003322 case Index_kind:
3323 VISIT(c, expr, s->v.Index.value);
3324 break;
3325 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003326 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003327 PyErr_SetString(PyExc_SystemError,
3328 "extended slice invalid in nested slice");
3329 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330 }
3331 return 1;
3332}
3333
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334static int
3335compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3336{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003337 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003338 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003339 case Index_kind:
3340 kindname = "index";
3341 if (ctx != AugStore) {
3342 VISIT(c, expr, s->v.Index.value);
3343 }
3344 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003346 kindname = "ellipsis";
3347 if (ctx != AugStore) {
3348 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3349 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350 break;
3351 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003352 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003353 if (!s->v.Slice.step)
3354 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003355 if (ctx != AugStore) {
3356 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 return 0;
3358 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003359 break;
3360 case ExtSlice_kind:
3361 kindname = "extended slice";
3362 if (ctx != AugStore) {
3363 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3364 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003365 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003366 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003367 if (!compiler_visit_nested_slice(c, sub, ctx))
3368 return 0;
3369 }
3370 ADDOP_I(c, BUILD_TUPLE, n);
3371 }
3372 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003373 default:
3374 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003375 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003376 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003378 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379}
3380
Neal Norwitzf733a012006-10-29 18:30:10 +00003381
3382/* End of the compiler section, beginning of the assembler section */
3383
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003384/* do depth-first search of basic block graph, starting with block.
3385 post records the block indices in post-order.
3386
3387 XXX must handle implicit jumps from one block to next
3388*/
3389
Neal Norwitzf733a012006-10-29 18:30:10 +00003390struct assembler {
3391 PyObject *a_bytecode; /* string containing bytecode */
3392 int a_offset; /* offset into bytecode */
3393 int a_nblocks; /* number of reachable blocks */
3394 basicblock **a_postorder; /* list of blocks in dfs postorder */
3395 PyObject *a_lnotab; /* string containing lnotab */
3396 int a_lnotab_off; /* offset into lnotab */
3397 int a_lineno; /* last lineno of emitted instruction */
3398 int a_lineno_off; /* bytecode offset of last lineno */
3399};
3400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401static void
3402dfs(struct compiler *c, basicblock *b, struct assembler *a)
3403{
3404 int i;
3405 struct instr *instr = NULL;
3406
3407 if (b->b_seen)
3408 return;
3409 b->b_seen = 1;
3410 if (b->b_next != NULL)
3411 dfs(c, b->b_next, a);
3412 for (i = 0; i < b->b_iused; i++) {
3413 instr = &b->b_instr[i];
3414 if (instr->i_jrel || instr->i_jabs)
3415 dfs(c, instr->i_target, a);
3416 }
3417 a->a_postorder[a->a_nblocks++] = b;
3418}
3419
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003420static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003421stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3422{
3423 int i;
3424 struct instr *instr;
3425 if (b->b_seen || b->b_startdepth >= depth)
3426 return maxdepth;
3427 b->b_seen = 1;
3428 b->b_startdepth = depth;
3429 for (i = 0; i < b->b_iused; i++) {
3430 instr = &b->b_instr[i];
3431 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3432 if (depth > maxdepth)
3433 maxdepth = depth;
3434 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3435 if (instr->i_jrel || instr->i_jabs) {
3436 maxdepth = stackdepth_walk(c, instr->i_target,
3437 depth, maxdepth);
3438 if (instr->i_opcode == JUMP_ABSOLUTE ||
3439 instr->i_opcode == JUMP_FORWARD) {
3440 goto out; /* remaining code is dead */
3441 }
3442 }
3443 }
3444 if (b->b_next)
3445 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3446out:
3447 b->b_seen = 0;
3448 return maxdepth;
3449}
3450
3451/* Find the flow path that needs the largest stack. We assume that
3452 * cycles in the flow graph have no net effect on the stack depth.
3453 */
3454static int
3455stackdepth(struct compiler *c)
3456{
3457 basicblock *b, *entryblock;
3458 entryblock = NULL;
3459 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3460 b->b_seen = 0;
3461 b->b_startdepth = INT_MIN;
3462 entryblock = b;
3463 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003464 if (!entryblock)
3465 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466 return stackdepth_walk(c, entryblock, 0, 0);
3467}
3468
3469static int
3470assemble_init(struct assembler *a, int nblocks, int firstlineno)
3471{
3472 memset(a, 0, sizeof(struct assembler));
3473 a->a_lineno = firstlineno;
3474 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3475 if (!a->a_bytecode)
3476 return 0;
3477 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3478 if (!a->a_lnotab)
3479 return 0;
3480 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003481 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003482 if (!a->a_postorder) {
3483 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003485 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 return 1;
3487}
3488
3489static void
3490assemble_free(struct assembler *a)
3491{
3492 Py_XDECREF(a->a_bytecode);
3493 Py_XDECREF(a->a_lnotab);
3494 if (a->a_postorder)
3495 PyObject_Free(a->a_postorder);
3496}
3497
3498/* Return the size of a basic block in bytes. */
3499
3500static int
3501instrsize(struct instr *instr)
3502{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003503 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003504 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003505 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003506 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3507 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508}
3509
3510static int
3511blocksize(basicblock *b)
3512{
3513 int i;
3514 int size = 0;
3515
3516 for (i = 0; i < b->b_iused; i++)
3517 size += instrsize(&b->b_instr[i]);
3518 return size;
3519}
3520
3521/* All about a_lnotab.
3522
3523c_lnotab is an array of unsigned bytes disguised as a Python string.
3524It is used to map bytecode offsets to source code line #s (when needed
3525for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003526
Tim Peters2a7f3842001-06-09 09:26:21 +00003527The array is conceptually a list of
3528 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003529pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003530
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003531 byte code offset source code line number
3532 0 1
3533 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003534 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003535 350 307
3536 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003537
3538The first trick is that these numbers aren't stored, only the increments
3539from one row to the next (this doesn't really work, but it's a start):
3540
3541 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3542
3543The second trick is that an unsigned byte can't hold negative values, or
3544values larger than 255, so (a) there's a deep assumption that byte code
3545offsets and their corresponding line #s both increase monotonically, and (b)
3546if at least one column jumps by more than 255 from one row to the next, more
3547than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003548from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003549part. A user of c_lnotab desiring to find the source line number
3550corresponding to a bytecode address A should do something like this
3551
3552 lineno = addr = 0
3553 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003554 addr += addr_incr
3555 if addr > A:
3556 return lineno
3557 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003558
3559In order for this to work, when the addr field increments by more than 255,
3560the line # increment in each pair generated must be 0 until the remaining addr
Neal Norwitz84be93b2006-07-16 01:50:38 +00003561increment is < 256. So, in the example above, assemble_lnotab (it used
3562to be called com_set_lineno) should not (as was actually done until 2.2)
3563expand 300, 300 to 255, 255, 45, 45,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003564 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003565*/
3566
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003567static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003569{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570 int d_bytecode, d_lineno;
3571 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003572 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573
3574 d_bytecode = a->a_offset - a->a_lineno_off;
3575 d_lineno = i->i_lineno - a->a_lineno;
3576
3577 assert(d_bytecode >= 0);
3578 assert(d_lineno >= 0);
3579
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003580 if(d_bytecode == 0 && d_lineno == 0)
3581 return 1;
3582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003584 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585 nbytes = a->a_lnotab_off + 2 * ncodes;
3586 len = PyString_GET_SIZE(a->a_lnotab);
3587 if (nbytes >= len) {
3588 if (len * 2 < nbytes)
3589 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003590 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003591 len *= 2;
3592 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3593 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003594 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003595 lnotab = (unsigned char *)
3596 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003597 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 *lnotab++ = 255;
3599 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003600 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601 d_bytecode -= ncodes * 255;
3602 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003603 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 assert(d_bytecode <= 255);
3605 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003606 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 nbytes = a->a_lnotab_off + 2 * ncodes;
3608 len = PyString_GET_SIZE(a->a_lnotab);
3609 if (nbytes >= len) {
3610 if (len * 2 < nbytes)
3611 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003612 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 len *= 2;
3614 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3615 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003616 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003617 lnotab = (unsigned char *)
3618 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003619 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003620 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003621 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003622 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003624 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003625 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 d_lineno -= ncodes * 255;
3627 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003628 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003629
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630 len = PyString_GET_SIZE(a->a_lnotab);
3631 if (a->a_lnotab_off + 2 >= len) {
3632 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003633 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003634 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003635 lnotab = (unsigned char *)
3636 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003637
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003638 a->a_lnotab_off += 2;
3639 if (d_bytecode) {
3640 *lnotab++ = d_bytecode;
3641 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003642 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003643 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644 *lnotab++ = 0;
3645 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003646 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003647 a->a_lineno = i->i_lineno;
3648 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003649 return 1;
3650}
3651
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652/* assemble_emit()
3653 Extend the bytecode with a new instruction.
3654 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003655*/
3656
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003657static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003658assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003659{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003660 int size, arg = 0, ext = 0;
Neal Norwitz2585ad52006-06-12 02:09:34 +00003661 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003662 char *code;
3663
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003664 size = instrsize(i);
3665 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003667 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003668 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003669 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003670 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 if (a->a_offset + size >= len) {
3672 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003673 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3676 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003677 if (size == 6) {
3678 assert(i->i_hasarg);
3679 *code++ = (char)EXTENDED_ARG;
3680 *code++ = ext & 0xff;
3681 *code++ = ext >> 8;
3682 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003684 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003685 if (i->i_hasarg) {
3686 assert(size == 3 || size == 6);
3687 *code++ = arg & 0xff;
3688 *code++ = arg >> 8;
3689 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003691}
3692
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003693static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003695{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003697 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003698 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003699
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003700 /* Compute the size of each block and fixup jump args.
3701 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003702start:
3703 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003705 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 bsize = blocksize(b);
3707 b->b_offset = totsize;
3708 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003709 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003710 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3712 bsize = b->b_offset;
3713 for (i = 0; i < b->b_iused; i++) {
3714 struct instr *instr = &b->b_instr[i];
3715 /* Relative jumps are computed relative to
3716 the instruction pointer after fetching
3717 the jump instruction.
3718 */
3719 bsize += instrsize(instr);
3720 if (instr->i_jabs)
3721 instr->i_oparg = instr->i_target->b_offset;
3722 else if (instr->i_jrel) {
3723 int delta = instr->i_target->b_offset - bsize;
3724 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003725 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003726 else
3727 continue;
3728 if (instr->i_oparg > 0xffff)
3729 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003730 }
3731 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003732
3733 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003734 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003735 with a better solution.
3736
3737 In the meantime, should the goto be dropped in favor
3738 of a loop?
3739
3740 The issue is that in the first loop blocksize() is called
3741 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003742 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003743 i_oparg is calculated in the second loop above.
3744
3745 So we loop until we stop seeing new EXTENDED_ARGs.
3746 The only EXTENDED_ARGs that could be popping up are
3747 ones in jump instructions. So this should converge
3748 fairly quickly.
3749 */
3750 if (last_extended_arg_count != extended_arg_count) {
3751 last_extended_arg_count = extended_arg_count;
3752 goto start;
3753 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003754}
3755
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003756static PyObject *
3757dict_keys_inorder(PyObject *dict, int offset)
3758{
3759 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003760 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003761
3762 tuple = PyTuple_New(size);
3763 if (tuple == NULL)
3764 return NULL;
3765 while (PyDict_Next(dict, &pos, &k, &v)) {
3766 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003767 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003768 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003769 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003770 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003771 PyTuple_SET_ITEM(tuple, i - offset, k);
3772 }
3773 return tuple;
3774}
3775
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003776static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003778{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 PySTEntryObject *ste = c->u->u_ste;
3780 int flags = 0, n;
3781 if (ste->ste_type != ModuleBlock)
3782 flags |= CO_NEWLOCALS;
3783 if (ste->ste_type == FunctionBlock) {
3784 if (!ste->ste_unoptimized)
3785 flags |= CO_OPTIMIZED;
3786 if (ste->ste_nested)
3787 flags |= CO_NESTED;
3788 if (ste->ste_generator)
3789 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 if (ste->ste_varargs)
3792 flags |= CO_VARARGS;
3793 if (ste->ste_varkeywords)
3794 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003795 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003797
3798 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003799 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003800
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 n = PyDict_Size(c->u->u_freevars);
3802 if (n < 0)
3803 return -1;
3804 if (n == 0) {
3805 n = PyDict_Size(c->u->u_cellvars);
3806 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003807 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 if (n == 0) {
3809 flags |= CO_NOFREE;
3810 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003811 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003812
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003813 return flags;
3814}
3815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816static PyCodeObject *
3817makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003818{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 PyObject *tmp;
3820 PyCodeObject *co = NULL;
3821 PyObject *consts = NULL;
3822 PyObject *names = NULL;
3823 PyObject *varnames = NULL;
3824 PyObject *filename = NULL;
3825 PyObject *name = NULL;
3826 PyObject *freevars = NULL;
3827 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003828 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 tmp = dict_keys_inorder(c->u->u_consts, 0);
3832 if (!tmp)
3833 goto error;
3834 consts = PySequence_List(tmp); /* optimize_code requires a list */
3835 Py_DECREF(tmp);
3836
3837 names = dict_keys_inorder(c->u->u_names, 0);
3838 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3839 if (!consts || !names || !varnames)
3840 goto error;
3841
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003842 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3843 if (!cellvars)
3844 goto error;
3845 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3846 if (!freevars)
3847 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 filename = PyString_FromString(c->c_filename);
3849 if (!filename)
3850 goto error;
3851
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003852 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 flags = compute_code_flags(c);
3854 if (flags < 0)
3855 goto error;
3856
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003857 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 if (!bytecode)
3859 goto error;
3860
3861 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3862 if (!tmp)
3863 goto error;
3864 Py_DECREF(consts);
3865 consts = tmp;
3866
3867 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3868 bytecode, consts, names, varnames,
3869 freevars, cellvars,
3870 filename, c->u->u_name,
3871 c->u->u_firstlineno,
3872 a->a_lnotab);
3873 error:
3874 Py_XDECREF(consts);
3875 Py_XDECREF(names);
3876 Py_XDECREF(varnames);
3877 Py_XDECREF(filename);
3878 Py_XDECREF(name);
3879 Py_XDECREF(freevars);
3880 Py_XDECREF(cellvars);
3881 Py_XDECREF(bytecode);
3882 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003883}
3884
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003885
3886/* For debugging purposes only */
3887#if 0
3888static void
3889dump_instr(const struct instr *i)
3890{
3891 const char *jrel = i->i_jrel ? "jrel " : "";
3892 const char *jabs = i->i_jabs ? "jabs " : "";
3893 char arg[128];
3894
3895 *arg = '\0';
3896 if (i->i_hasarg)
3897 sprintf(arg, "arg: %d ", i->i_oparg);
3898
3899 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3900 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3901}
3902
3903static void
3904dump_basicblock(const basicblock *b)
3905{
3906 const char *seen = b->b_seen ? "seen " : "";
3907 const char *b_return = b->b_return ? "return " : "";
3908 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3909 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3910 if (b->b_instr) {
3911 int i;
3912 for (i = 0; i < b->b_iused; i++) {
3913 fprintf(stderr, " [%02d] ", i);
3914 dump_instr(b->b_instr + i);
3915 }
3916 }
3917}
3918#endif
3919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920static PyCodeObject *
3921assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003922{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923 basicblock *b, *entryblock;
3924 struct assembler a;
3925 int i, j, nblocks;
3926 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003927
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003928 /* Make sure every block that falls off the end returns None.
3929 XXX NEXT_BLOCK() isn't quite right, because if the last
3930 block ends with a jump or return b_next shouldn't set.
3931 */
3932 if (!c->u->u_curblock->b_return) {
3933 NEXT_BLOCK(c);
3934 if (addNone)
3935 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3936 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003937 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003938
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 nblocks = 0;
3940 entryblock = NULL;
3941 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3942 nblocks++;
3943 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003944 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003945
Neal Norwitzed657552006-07-10 00:04:44 +00003946 /* Set firstlineno if it wasn't explicitly set. */
3947 if (!c->u->u_firstlineno) {
3948 if (entryblock && entryblock->b_instr)
3949 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3950 else
3951 c->u->u_firstlineno = 1;
3952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3954 goto error;
3955 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003956
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003958 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003959
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003960 /* Emit code in reverse postorder from dfs. */
3961 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003962 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 for (j = 0; j < b->b_iused; j++)
3964 if (!assemble_emit(&a, &b->b_instr[j]))
3965 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003966 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003967
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
3969 goto error;
3970 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
3971 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003972
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 co = makecode(c, &a);
3974 error:
3975 assemble_free(&a);
3976 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003977}