blob: bee48dab1d6d76f5d35897069478407e72087be6 [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.
Thomas Wouters89f507f2006-12-13 04:49:30 +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
Thomas Wouters89f507f2006-12-13 04:49:30 +000011 * this file.
12 * 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
Thomas Wouters89f507f2006-12-13 04:49:30 +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
Nick Coghlan650f0d02007-04-15 12:05:43 +000042#define COMP_GENEXP 0
43#define COMP_LISTCOMP 1
44#define COMP_SETCOMP 2
45
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000047 unsigned i_jabs : 1;
48 unsigned i_jrel : 1;
49 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000071 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000073 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000077 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078} basicblock;
79
80/* fblockinfo tracks the current frame block.
81
Jeremy Hyltone9357b22006-03-01 15:47:05 +000082A frame block is used to handle loops, try/except, and try/finally.
83It's called a frame block to distinguish it from a basic block in the
84compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085*/
86
87enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
88
89struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000090 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 basicblock *fb_block;
92};
93
94/* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
96*/
97struct compiler_unit {
98 PySTEntryObject *u_ste;
99
100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000102 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103 the argument for opcodes that refer to those collections.
104 */
105 PyObject *u_consts; /* all constants */
106 PyObject *u_names; /* all names */
107 PyObject *u_varnames; /* local variables */
108 PyObject *u_cellvars; /* cell variables */
109 PyObject *u_freevars; /* free variables */
110
111 PyObject *u_private; /* for private name mangling */
112
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000113 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000114 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 /* Pointer to the most recently allocated block. By following b_list
116 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
121 int u_nfblocks;
122 struct fblockinfo u_fblock[CO_MAXBLOCKS];
123
124 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000125 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000126 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 has been generated with current lineno */
128};
129
130/* This struct captures the global state of a compilation.
131
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132The u pointer points to the current compilation unit, while units
133for enclosing blocks are stored in c_stack. The u and c_stack are
134managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135*/
136
137struct compiler {
138 const char *c_filename;
139 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141 PyCompilerFlags *c_flags;
142
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000149 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150};
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152static int compiler_enter_scope(struct compiler *, identifier, void *, int);
153static void compiler_free(struct compiler *);
154static basicblock *compiler_new_block(struct compiler *);
155static int compiler_next_instr(struct compiler *, basicblock *);
156static int compiler_addop(struct compiler *, int);
157static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158static int compiler_addop_i(struct compiler *, int, int);
159static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static basicblock *compiler_use_new_block(struct compiler *);
161static int compiler_error(struct compiler *, const char *);
162static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
163
164static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
165static int compiler_visit_stmt(struct compiler *, stmt_ty);
166static int compiler_visit_keyword(struct compiler *, keyword_ty);
167static int compiler_visit_expr(struct compiler *, expr_ty);
168static int compiler_augassign(struct compiler *, stmt_ty);
169static int compiler_visit_slice(struct compiler *, slice_ty,
170 expr_context_ty);
171
172static int compiler_push_fblock(struct compiler *, enum fblocktype,
173 basicblock *);
174static void compiler_pop_fblock(struct compiler *, enum fblocktype,
175 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176/* Returns true if there is a loop on the fblock stack. */
177static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int inplace_binop(struct compiler *, operator_ty);
180static int expr_constant(expr_ty e);
181
Guido van Rossumc2e20742006-02-27 22:32:47 +0000182static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000183static int compiler_call_helper(struct compiler *c, int n,
184 asdl_seq *args,
185 asdl_seq *keywords,
186 expr_ty starargs,
187 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static PyCodeObject *assemble(struct compiler *, int addNone);
190static PyObject *__doc__;
191
192PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000194{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 /* Name mangling: __private becomes _classname__private.
196 This is independent from how the name is used. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000197 const char *p, *name = PyString_AsString(ident);
198 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 size_t nlen, plen;
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000200 if (privateobj == NULL || !PyString_Check(privateobj) ||
201 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000202 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000205 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 nlen = strlen(name);
207 if (name[nlen-1] == '_' && name[nlen-2] == '_') {
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;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000297 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +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)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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);
Thomas Wouters477c8d52006-05-27 19:21:47 +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));
Nick Coghlan650f0d02007-04-15 12:05:43 +0000367 scope = (PyInt_AS_LONG(v) >> SCOPE_OFFSET) & 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++;
Thomas Wouters477c8d52006-05-27 19:21:47 +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) {
395 assert(block != (void *)0xcbcbcbcb);
396 assert(block != (void *)0xfbfbfbfb);
397 assert(block != (void *)0xdbdbdbdb);
398 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 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
442 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;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000449 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450 u->u_ste = PySymtable_Lookup(c->c_st, key);
451 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000452 compiler_unit_free(u);
453 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454 }
455 Py_INCREF(name);
456 u->u_name = name;
457 u->u_varnames = list2dict(u->u_ste->ste_varnames);
458 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000459 if (!u->u_varnames || !u->u_cellvars) {
460 compiler_unit_free(u);
461 return 0;
462 }
463
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000465 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000466 if (!u->u_freevars) {
467 compiler_unit_free(u);
468 return 0;
469 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000470
471 u->u_blocks = NULL;
472 u->u_tmpname = 0;
473 u->u_nfblocks = 0;
474 u->u_firstlineno = lineno;
475 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000476 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 u->u_consts = PyDict_New();
478 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000479 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000480 return 0;
481 }
482 u->u_names = PyDict_New();
483 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000484 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485 return 0;
486 }
487
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000488 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
490 /* Push the old compiler_unit on the stack. */
491 if (c->u) {
492 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000493 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
494 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000495 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 return 0;
497 }
498 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000499 u->u_private = c->u->u_private;
500 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501 }
502 c->u = u;
503
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000504 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000505 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506 return 0;
507
508 return 1;
509}
510
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000511static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512compiler_exit_scope(struct compiler *c)
513{
514 int n;
515 PyObject *wrapper;
516
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000517 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 compiler_unit_free(c->u);
519 /* Restore c->u to the parent unit. */
520 n = PyList_GET_SIZE(c->c_stack) - 1;
521 if (n >= 0) {
522 wrapper = PyList_GET_ITEM(c->c_stack, n);
523 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000524 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000525 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000527 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528 compiler_unit_check(c->u);
529 }
530 else
531 c->u = NULL;
532
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533}
534
Guido van Rossumc2e20742006-02-27 22:32:47 +0000535/* Allocate a new "anonymous" local variable.
536 Used by list comprehensions and with statements.
537*/
538
539static PyObject *
540compiler_new_tmpname(struct compiler *c)
541{
542 char tmpname[256];
543 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
544 return PyString_FromString(tmpname);
545}
546
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547/* Allocate a new block and return a pointer to it.
548 Returns NULL on error.
549*/
550
551static basicblock *
552compiler_new_block(struct compiler *c)
553{
554 basicblock *b;
555 struct compiler_unit *u;
556
557 u = c->u;
558 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000559 if (b == NULL) {
560 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000562 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000564 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565 b->b_list = u->u_blocks;
566 u->u_blocks = b;
567 return b;
568}
569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570static basicblock *
571compiler_use_new_block(struct compiler *c)
572{
573 basicblock *block = compiler_new_block(c);
574 if (block == NULL)
575 return NULL;
576 c->u->u_curblock = block;
577 return block;
578}
579
580static basicblock *
581compiler_next_block(struct compiler *c)
582{
583 basicblock *block = compiler_new_block(c);
584 if (block == NULL)
585 return NULL;
586 c->u->u_curblock->b_next = block;
587 c->u->u_curblock = block;
588 return block;
589}
590
591static basicblock *
592compiler_use_next_block(struct compiler *c, basicblock *block)
593{
594 assert(block != NULL);
595 c->u->u_curblock->b_next = block;
596 c->u->u_curblock = block;
597 return block;
598}
599
600/* Returns the offset of the next instruction in the current block's
601 b_instr array. Resizes the b_instr as necessary.
602 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000603*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604
605static int
606compiler_next_instr(struct compiler *c, basicblock *b)
607{
608 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000609 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610 b->b_instr = (struct instr *)PyObject_Malloc(
611 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 if (b->b_instr == NULL) {
613 PyErr_NoMemory();
614 return -1;
615 }
616 b->b_ialloc = DEFAULT_BLOCK_SIZE;
617 memset((char *)b->b_instr, 0,
618 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000619 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000621 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 size_t oldsize, newsize;
623 oldsize = b->b_ialloc * sizeof(struct instr);
624 newsize = oldsize << 1;
625 if (newsize == 0) {
626 PyErr_NoMemory();
627 return -1;
628 }
629 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000630 tmp = (struct instr *)PyObject_Realloc(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000632 if (tmp == NULL) {
633 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000635 }
636 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
638 }
639 return b->b_iused++;
640}
641
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000642/* Set the i_lineno member of the instruction at offse off if the
643 line number for the current expression/statement (?) has not
644 already been set. If it has been set, the call has no effect.
645
646 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000647*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649static void
650compiler_set_lineno(struct compiler *c, int off)
651{
652 basicblock *b;
653 if (c->u->u_lineno_set)
654 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000655 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000657 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658}
659
660static int
661opcode_stack_effect(int opcode, int oparg)
662{
663 switch (opcode) {
664 case POP_TOP:
665 return -1;
666 case ROT_TWO:
667 case ROT_THREE:
668 return 0;
669 case DUP_TOP:
670 return 1;
671 case ROT_FOUR:
672 return 0;
673
674 case UNARY_POSITIVE:
675 case UNARY_NEGATIVE:
676 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677 case UNARY_INVERT:
678 return 0;
679
Nick Coghlan650f0d02007-04-15 12:05:43 +0000680 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000681 case LIST_APPEND:
682 return -2;
683
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 case BINARY_POWER:
685 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 case BINARY_MODULO:
687 case BINARY_ADD:
688 case BINARY_SUBTRACT:
689 case BINARY_SUBSCR:
690 case BINARY_FLOOR_DIVIDE:
691 case BINARY_TRUE_DIVIDE:
692 return -1;
693 case INPLACE_FLOOR_DIVIDE:
694 case INPLACE_TRUE_DIVIDE:
695 return -1;
696
697 case SLICE+0:
698 return 1;
699 case SLICE+1:
700 return 0;
701 case SLICE+2:
702 return 0;
703 case SLICE+3:
704 return -1;
705
706 case STORE_SLICE+0:
707 return -2;
708 case STORE_SLICE+1:
709 return -3;
710 case STORE_SLICE+2:
711 return -3;
712 case STORE_SLICE+3:
713 return -4;
714
715 case DELETE_SLICE+0:
716 return -1;
717 case DELETE_SLICE+1:
718 return -2;
719 case DELETE_SLICE+2:
720 return -2;
721 case DELETE_SLICE+3:
722 return -3;
723
724 case INPLACE_ADD:
725 case INPLACE_SUBTRACT:
726 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000727 case INPLACE_MODULO:
728 return -1;
729 case STORE_SUBSCR:
730 return -3;
731 case DELETE_SUBSCR:
732 return -2;
733
734 case BINARY_LSHIFT:
735 case BINARY_RSHIFT:
736 case BINARY_AND:
737 case BINARY_XOR:
738 case BINARY_OR:
739 return -1;
740 case INPLACE_POWER:
741 return -1;
742 case GET_ITER:
743 return 0;
744
745 case PRINT_EXPR:
746 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000747 case LOAD_BUILD_CLASS:
748 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 case INPLACE_LSHIFT:
750 case INPLACE_RSHIFT:
751 case INPLACE_AND:
752 case INPLACE_XOR:
753 case INPLACE_OR:
754 return -1;
755 case BREAK_LOOP:
756 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000757 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000758 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000759 case STORE_LOCALS:
760 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 case RETURN_VALUE:
762 return -1;
763 case IMPORT_STAR:
764 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765 case YIELD_VALUE:
766 return 0;
767
768 case POP_BLOCK:
769 return 0;
770 case END_FINALLY:
771 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772
773 case STORE_NAME:
774 return -1;
775 case DELETE_NAME:
776 return 0;
777 case UNPACK_SEQUENCE:
778 return oparg-1;
779 case FOR_ITER:
780 return 1;
781
782 case STORE_ATTR:
783 return -2;
784 case DELETE_ATTR:
785 return -1;
786 case STORE_GLOBAL:
787 return -1;
788 case DELETE_GLOBAL:
789 return 0;
790 case DUP_TOPX:
791 return oparg;
792 case LOAD_CONST:
793 return 1;
794 case LOAD_NAME:
795 return 1;
796 case BUILD_TUPLE:
797 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000798 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799 return 1-oparg;
800 case BUILD_MAP:
801 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000802 case MAKE_BYTES:
803 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804 case LOAD_ATTR:
805 return 0;
806 case COMPARE_OP:
807 return -1;
808 case IMPORT_NAME:
809 return 0;
810 case IMPORT_FROM:
811 return 1;
812
813 case JUMP_FORWARD:
814 case JUMP_IF_FALSE:
815 case JUMP_IF_TRUE:
816 case JUMP_ABSOLUTE:
817 return 0;
818
819 case LOAD_GLOBAL:
820 return 1;
821
822 case CONTINUE_LOOP:
823 return 0;
824 case SETUP_LOOP:
825 return 0;
826 case SETUP_EXCEPT:
827 case SETUP_FINALLY:
828 return 3; /* actually pushed by an exception */
829
830 case LOAD_FAST:
831 return 1;
832 case STORE_FAST:
833 return -1;
834 case DELETE_FAST:
835 return 0;
836
837 case RAISE_VARARGS:
838 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000839#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 case CALL_FUNCTION:
841 return -NARGS(oparg);
842 case CALL_FUNCTION_VAR:
843 case CALL_FUNCTION_KW:
844 return -NARGS(oparg)-1;
845 case CALL_FUNCTION_VAR_KW:
846 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000848 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000849 case MAKE_CLOSURE:
850 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000851#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852 case BUILD_SLICE:
853 if (oparg == 3)
854 return -2;
855 else
856 return -1;
857
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858 case LOAD_CLOSURE:
859 return 1;
860 case LOAD_DEREF:
861 return 1;
862 case STORE_DEREF:
863 return -1;
864 default:
865 fprintf(stderr, "opcode = %d\n", opcode);
866 Py_FatalError("opcode_stack_effect()");
867
868 }
869 return 0; /* not reachable */
870}
871
872/* Add an opcode with no argument.
873 Returns 0 on failure, 1 on success.
874*/
875
876static int
877compiler_addop(struct compiler *c, int opcode)
878{
879 basicblock *b;
880 struct instr *i;
881 int off;
882 off = compiler_next_instr(c, c->u->u_curblock);
883 if (off < 0)
884 return 0;
885 b = c->u->u_curblock;
886 i = &b->b_instr[off];
887 i->i_opcode = opcode;
888 i->i_hasarg = 0;
889 if (opcode == RETURN_VALUE)
890 b->b_return = 1;
891 compiler_set_lineno(c, off);
892 return 1;
893}
894
895static int
896compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
897{
898 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000899 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000901 /* necessary to make sure types aren't coerced (e.g., int and long) */
902 t = PyTuple_Pack(2, o, o->ob_type);
903 if (t == NULL)
904 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905
906 v = PyDict_GetItem(dict, t);
907 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000908 if (PyErr_Occurred())
909 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910 arg = PyDict_Size(dict);
911 v = PyInt_FromLong(arg);
912 if (!v) {
913 Py_DECREF(t);
914 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000915 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916 if (PyDict_SetItem(dict, t, v) < 0) {
917 Py_DECREF(t);
918 Py_DECREF(v);
919 return -1;
920 }
921 Py_DECREF(v);
922 }
923 else
924 arg = PyInt_AsLong(v);
925 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000926 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927}
928
929static int
930compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
931 PyObject *o)
932{
933 int arg = compiler_add_o(c, dict, o);
934 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000935 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936 return compiler_addop_i(c, opcode, arg);
937}
938
939static int
940compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000941 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942{
943 int arg;
944 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
945 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000946 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 arg = compiler_add_o(c, dict, mangled);
948 Py_DECREF(mangled);
949 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000950 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951 return compiler_addop_i(c, opcode, arg);
952}
953
954/* Add an opcode with an integer argument.
955 Returns 0 on failure, 1 on success.
956*/
957
958static int
959compiler_addop_i(struct compiler *c, int opcode, int oparg)
960{
961 struct instr *i;
962 int off;
963 off = compiler_next_instr(c, c->u->u_curblock);
964 if (off < 0)
965 return 0;
966 i = &c->u->u_curblock->b_instr[off];
967 i->i_opcode = opcode;
968 i->i_oparg = oparg;
969 i->i_hasarg = 1;
970 compiler_set_lineno(c, off);
971 return 1;
972}
973
974static int
975compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
976{
977 struct instr *i;
978 int off;
979
980 assert(b != NULL);
981 off = compiler_next_instr(c, c->u->u_curblock);
982 if (off < 0)
983 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984 i = &c->u->u_curblock->b_instr[off];
985 i->i_opcode = opcode;
986 i->i_target = b;
987 i->i_hasarg = 1;
988 if (absolute)
989 i->i_jabs = 1;
990 else
991 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000992 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 return 1;
994}
995
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000996/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
997 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 it as the current block. NEXT_BLOCK() also creates an implicit jump
999 from the current block to the new block.
1000*/
1001
Thomas Wouters89f507f2006-12-13 04:49:30 +00001002/* The returns inside these macros make it impossible to decref objects
1003 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004*/
1005
1006
1007#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008 if (compiler_use_new_block((C)) == NULL) \
1009 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010}
1011
1012#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001013 if (compiler_next_block((C)) == NULL) \
1014 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001015}
1016
1017#define ADDOP(C, OP) { \
1018 if (!compiler_addop((C), (OP))) \
1019 return 0; \
1020}
1021
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001022#define ADDOP_IN_SCOPE(C, OP) { \
1023 if (!compiler_addop((C), (OP))) { \
1024 compiler_exit_scope(c); \
1025 return 0; \
1026 } \
1027}
1028
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029#define ADDOP_O(C, OP, O, TYPE) { \
1030 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1031 return 0; \
1032}
1033
1034#define ADDOP_NAME(C, OP, O, TYPE) { \
1035 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1036 return 0; \
1037}
1038
1039#define ADDOP_I(C, OP, O) { \
1040 if (!compiler_addop_i((C), (OP), (O))) \
1041 return 0; \
1042}
1043
1044#define ADDOP_JABS(C, OP, O) { \
1045 if (!compiler_addop_j((C), (OP), (O), 1)) \
1046 return 0; \
1047}
1048
1049#define ADDOP_JREL(C, OP, O) { \
1050 if (!compiler_addop_j((C), (OP), (O), 0)) \
1051 return 0; \
1052}
1053
1054/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1055 the ASDL name to synthesize the name of the C type and the visit function.
1056*/
1057
1058#define VISIT(C, TYPE, V) {\
1059 if (!compiler_visit_ ## TYPE((C), (V))) \
1060 return 0; \
1061}
1062
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001063#define VISIT_IN_SCOPE(C, TYPE, V) {\
1064 if (!compiler_visit_ ## TYPE((C), (V))) { \
1065 compiler_exit_scope(c); \
1066 return 0; \
1067 } \
1068}
1069
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001070#define VISIT_SLICE(C, V, CTX) {\
1071 if (!compiler_visit_slice((C), (V), (CTX))) \
1072 return 0; \
1073}
1074
1075#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001076 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001078 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001079 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001080 if (!compiler_visit_ ## TYPE((C), elt)) \
1081 return 0; \
1082 } \
1083}
1084
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001085#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001086 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001087 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001088 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001089 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001090 if (!compiler_visit_ ## TYPE((C), elt)) { \
1091 compiler_exit_scope(c); \
1092 return 0; \
1093 } \
1094 } \
1095}
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097static int
1098compiler_isdocstring(stmt_ty s)
1099{
1100 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001101 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102 return s->v.Expr.value->kind == Str_kind;
1103}
1104
1105/* Compile a sequence of statements, checking for a docstring. */
1106
1107static int
1108compiler_body(struct compiler *c, asdl_seq *stmts)
1109{
1110 int i = 0;
1111 stmt_ty st;
1112
1113 if (!asdl_seq_LEN(stmts))
1114 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 if (compiler_isdocstring(st)) {
1117 i = 1;
1118 VISIT(c, expr, st->v.Expr.value);
1119 if (!compiler_nameop(c, __doc__, Store))
1120 return 0;
1121 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001122 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001123 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001124 return 1;
1125}
1126
1127static PyCodeObject *
1128compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001129{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001130 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001131 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 static PyObject *module;
1133 if (!module) {
1134 module = PyString_FromString("<module>");
1135 if (!module)
1136 return NULL;
1137 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001138 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1139 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001140 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141 switch (mod->kind) {
1142 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001143 if (!compiler_body(c, mod->v.Module.body)) {
1144 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001146 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 break;
1148 case Interactive_kind:
1149 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001150 VISIT_SEQ_IN_SCOPE(c, stmt,
1151 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001152 break;
1153 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001154 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001155 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 break;
1157 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001158 PyErr_SetString(PyExc_SystemError,
1159 "suite should not be possible");
1160 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001161 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001162 PyErr_Format(PyExc_SystemError,
1163 "module kind %d should not be possible",
1164 mod->kind);
1165 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001166 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 co = assemble(c, addNone);
1168 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001169 return co;
1170}
1171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001172/* The test for LOCAL must come before the test for FREE in order to
1173 handle classes where name is both local and free. The local var is
1174 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001175*/
1176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177static int
1178get_ref_type(struct compiler *c, PyObject *name)
1179{
1180 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001181 if (scope == 0) {
1182 char buf[350];
1183 PyOS_snprintf(buf, sizeof(buf),
1184 "unknown scope for %.100s in %.100s(%s) in %s\n"
1185 "symbols: %s\nlocals: %s\nglobals: %s\n",
1186 PyString_AS_STRING(name),
1187 PyString_AS_STRING(c->u->u_name),
1188 PyObject_REPR(c->u->u_ste->ste_id),
1189 c->c_filename,
1190 PyObject_REPR(c->u->u_ste->ste_symbols),
1191 PyObject_REPR(c->u->u_varnames),
1192 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001193 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001194 Py_FatalError(buf);
1195 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001196
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001197 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198}
1199
1200static int
1201compiler_lookup_arg(PyObject *dict, PyObject *name)
1202{
1203 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001204 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001205 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001206 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001208 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001210 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 return PyInt_AS_LONG(v);
1212}
1213
1214static int
1215compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1216{
1217 int i, free = PyCode_GetNumFree(co);
1218 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001219 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1220 ADDOP_I(c, MAKE_FUNCTION, args);
1221 return 1;
1222 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223 for (i = 0; i < free; ++i) {
1224 /* Bypass com_addop_varname because it will generate
1225 LOAD_DEREF but LOAD_CLOSURE is needed.
1226 */
1227 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1228 int arg, reftype;
1229
1230 /* Special case: If a class contains a method with a
1231 free variable that has the same name as a method,
1232 the name will be considered free *and* local in the
1233 class. It should be handled by the closure, as
1234 well as by the normal name loookup logic.
1235 */
1236 reftype = get_ref_type(c, name);
1237 if (reftype == CELL)
1238 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1239 else /* (reftype == FREE) */
1240 arg = compiler_lookup_arg(c->u->u_freevars, name);
1241 if (arg == -1) {
1242 printf("lookup %s in %s %d %d\n"
1243 "freevars of %s: %s\n",
1244 PyObject_REPR(name),
1245 PyString_AS_STRING(c->u->u_name),
1246 reftype, arg,
1247 PyString_AS_STRING(co->co_name),
1248 PyObject_REPR(co->co_freevars));
1249 Py_FatalError("compiler_make_closure()");
1250 }
1251 ADDOP_I(c, LOAD_CLOSURE, arg);
1252 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001253 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001255 ADDOP_I(c, MAKE_CLOSURE, args);
1256 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
1259static int
1260compiler_decorators(struct compiler *c, asdl_seq* decos)
1261{
1262 int i;
1263
1264 if (!decos)
1265 return 1;
1266
1267 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001269 }
1270 return 1;
1271}
1272
1273static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001274compiler_unpack_nested(struct compiler *c, asdl_seq *args) {
1275 int i, len;
1276 len = asdl_seq_LEN(args);
1277 ADDOP_I(c, UNPACK_SEQUENCE, len);
1278 for (i = 0; i < len; i++) {
1279 arg_ty elt = (arg_ty)asdl_seq_GET(args, i);
1280 switch (elt->kind) {
1281 case SimpleArg_kind:
1282 if (!compiler_nameop(c, elt->v.SimpleArg.arg, Store))
1283 return 0;
1284 break;
1285 case NestedArgs_kind:
1286 if (!compiler_unpack_nested(c, elt->v.NestedArgs.args))
1287 return 0;
1288 break;
1289 default:
1290 return 0;
1291 }
1292 }
1293 return 1;
1294}
1295
1296static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297compiler_arguments(struct compiler *c, arguments_ty args)
1298{
1299 int i;
1300 int n = asdl_seq_LEN(args->args);
Neal Norwitzc1505362006-12-28 06:47:50 +00001301
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 for (i = 0; i < n; i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001303 arg_ty arg = (arg_ty)asdl_seq_GET(args->args, i);
1304 if (arg->kind == NestedArgs_kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 PyObject *id = PyString_FromFormat(".%d", i);
1306 if (id == NULL) {
1307 return 0;
1308 }
1309 if (!compiler_nameop(c, id, Load)) {
1310 Py_DECREF(id);
1311 return 0;
1312 }
1313 Py_DECREF(id);
Neal Norwitzc1505362006-12-28 06:47:50 +00001314 if (!compiler_unpack_nested(c, arg->v.NestedArgs.args))
1315 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316 }
1317 }
1318 return 1;
1319}
1320
1321static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1323 asdl_seq *kw_defaults)
1324{
1325 int i, default_count = 0;
1326 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001327 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1329 if (default_) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001330 ADDOP_O(c, LOAD_CONST, arg->v.SimpleArg.arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 if (!compiler_visit_expr(c, default_)) {
1332 return -1;
1333 }
1334 default_count++;
1335 }
1336 }
1337 return default_count;
1338}
1339
1340static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001341compiler_visit_argannotation(struct compiler *c, identifier id,
1342 expr_ty annotation, PyObject *names)
1343{
1344 if (annotation) {
1345 VISIT(c, expr, annotation);
1346 if (PyList_Append(names, id))
1347 return -1;
1348 }
1349 return 0;
1350}
1351
1352static int
1353compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1354 PyObject *names)
1355{
1356 int i, error;
1357 for (i = 0; i < asdl_seq_LEN(args); i++) {
1358 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1359 if (arg->kind == NestedArgs_kind)
1360 error = compiler_visit_argannotations(
1361 c,
1362 arg->v.NestedArgs.args,
1363 names);
1364 else
1365 error = compiler_visit_argannotation(
1366 c,
1367 arg->v.SimpleArg.arg,
1368 arg->v.SimpleArg.annotation,
1369 names);
1370 if (error)
1371 return error;
1372 }
1373 return 0;
1374}
1375
1376static int
1377compiler_visit_annotations(struct compiler *c, arguments_ty args,
1378 expr_ty returns)
1379{
Guido van Rossum0240b922007-02-26 21:23:50 +00001380 /* Push arg annotations and a list of the argument names. Return the #
1381 of items pushed. The expressions are evaluated out-of-order wrt the
1382 source code.
1383
1384 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1385 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001386 static identifier return_str;
1387 PyObject *names;
1388 int len;
1389 names = PyList_New(0);
1390 if (!names)
1391 return -1;
1392
1393 if (compiler_visit_argannotations(c, args->args, names))
1394 goto error;
1395 if (args->varargannotation &&
1396 compiler_visit_argannotation(c, args->vararg,
1397 args->varargannotation, names))
1398 goto error;
1399 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1400 goto error;
1401 if (args->kwargannotation &&
1402 compiler_visit_argannotation(c, args->kwarg,
1403 args->kwargannotation, names))
1404 goto error;
1405
1406 if (!return_str) {
1407 return_str = PyString_InternFromString("return");
1408 if (!return_str)
1409 goto error;
1410 }
1411 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1412 goto error;
1413 }
1414
1415 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001416 if (len > 65534) {
1417 /* len must fit in 16 bits, and len is incremented below */
1418 PyErr_SetString(PyExc_SyntaxError,
1419 "too many annotations");
1420 goto error;
1421 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001422 if (len) {
1423 /* convert names to a tuple and place on stack */
1424 PyObject *elt;
1425 int i;
1426 PyObject *s = PyTuple_New(len);
1427 if (!s)
1428 goto error;
1429 for (i = 0; i < len; i++) {
1430 elt = PyList_GET_ITEM(names, i);
1431 Py_INCREF(elt);
1432 PyTuple_SET_ITEM(s, i, elt);
1433 }
1434 ADDOP_O(c, LOAD_CONST, s, consts);
1435 Py_DECREF(s);
1436 len++; /* include the just-pushed tuple */
1437 }
1438 Py_DECREF(names);
1439 return len;
1440
1441error:
1442 Py_DECREF(names);
1443 return -1;
1444}
1445
1446static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447compiler_function(struct compiler *c, stmt_ty s)
1448{
1449 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001450 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001452 expr_ty returns = s->v.FunctionDef.returns;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 asdl_seq* decos = s->v.FunctionDef.decorators;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001454 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001455 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001456 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457
1458 assert(s->kind == FunctionDef_kind);
1459
1460 if (!compiler_decorators(c, decos))
1461 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001462 if (args->kwonlyargs) {
1463 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1464 args->kw_defaults);
1465 if (res < 0)
1466 return 0;
1467 kw_default_count = res;
1468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 if (args->defaults)
1470 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001471 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001472 if (num_annotations < 0)
1473 return 0;
1474 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001476 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1477 s->lineno))
1478 return 0;
1479
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001481 docstring = compiler_isdocstring(st);
1482 if (docstring)
1483 first_const = st->v.Expr.value->v.Str.s;
1484 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001485 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001486 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001487 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001489 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 compiler_arguments(c, args);
1491
1492 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001495 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001497 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1498 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 }
1500 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001501 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502 if (co == NULL)
1503 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
Guido van Rossum4f72a782006-10-27 23:31:49 +00001505 arglength = asdl_seq_LEN(args->defaults);
1506 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001507 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001508 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001509 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001510
Neal Norwitzc1505362006-12-28 06:47:50 +00001511 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1513 ADDOP_I(c, CALL_FUNCTION, 1);
1514 }
1515
1516 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1517}
1518
1519static int
1520compiler_class(struct compiler *c, stmt_ty s)
1521{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001522 static PyObject *build_class = NULL;
1523 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001525 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001526 PySTEntryObject *ste;
Guido van Rossum3a383622007-03-21 21:26:58 +00001527 int err;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001529 /* initialize statics */
1530 if (build_class == NULL) {
1531 build_class = PyString_FromString("__build_class__");
1532 if (build_class == NULL)
1533 return 0;
1534 }
1535 if (locals == NULL) {
1536 locals = PyString_FromString("__locals__");
1537 if (locals == NULL)
1538 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001539 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001540
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001541 /* ultimately generate code for:
1542 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1543 where:
1544 <func> is a function/closure created from the class body
1545 <name> is the class name
1546 <bases> is the positional arguments and *varargs argument
1547 <keywords> is the keyword arguments and **kwds argument
1548 This borrows from compiler_call.
1549 */
1550
1551 /* 0. Create a fake variable named __locals__ */
1552 ste = PySymtable_Lookup(c->c_st, s);
1553 if (ste == NULL)
1554 return 0;
1555 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001556 err = PyList_Append(ste->ste_varnames, locals);
1557 Py_DECREF(ste);
1558 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001559 return 0;
1560
1561 /* 1. compile the class body into a code object */
1562 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1563 return 0;
1564 /* this block represents what we do in the new scope */
1565 {
1566 /* use the class name for name mangling */
1567 Py_INCREF(s->v.ClassDef.name);
1568 c->u->u_private = s->v.ClassDef.name;
1569 /* force it to have one mandatory argument */
1570 c->u->u_argcount = 1;
1571 /* load the first argument ... */
1572 ADDOP_I(c, LOAD_FAST, 0);
1573 /* ... and store it into f_locals */
1574 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1575 /* load __name__ ... */
1576 str = PyString_InternFromString("__name__");
1577 if (!str || !compiler_nameop(c, str, Load)) {
1578 Py_XDECREF(str);
1579 compiler_exit_scope(c);
1580 return 0;
1581 }
1582 Py_DECREF(str);
1583 /* ... and store it as __module__ */
1584 str = PyString_InternFromString("__module__");
1585 if (!str || !compiler_nameop(c, str, Store)) {
1586 Py_XDECREF(str);
1587 compiler_exit_scope(c);
1588 return 0;
1589 }
1590 Py_DECREF(str);
1591 /* compile the body proper */
1592 if (!compiler_body(c, s->v.ClassDef.body)) {
1593 compiler_exit_scope(c);
1594 return 0;
1595 }
1596 /* return None */
1597 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1598 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1599 /* create the code object */
1600 co = assemble(c, 1);
1601 }
1602 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001603 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001604 if (co == NULL)
1605 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001607 /* 2. load the 'build_class' function */
1608 ADDOP(c, LOAD_BUILD_CLASS);
1609
1610 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001611 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001612 Py_DECREF(co);
1613
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001614 /* 4. load class name */
1615 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1616
1617 /* 5. generate the rest of the code for the call */
1618 if (!compiler_call_helper(c, 2,
1619 s->v.ClassDef.bases,
1620 s->v.ClassDef.keywords,
1621 s->v.ClassDef.starargs,
1622 s->v.ClassDef.kwargs))
1623 return 0;
1624
1625 /* 6. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1627 return 0;
1628 return 1;
1629}
1630
1631static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001632compiler_ifexp(struct compiler *c, expr_ty e)
1633{
1634 basicblock *end, *next;
1635
1636 assert(e->kind == IfExp_kind);
1637 end = compiler_new_block(c);
1638 if (end == NULL)
1639 return 0;
1640 next = compiler_new_block(c);
1641 if (next == NULL)
1642 return 0;
1643 VISIT(c, expr, e->v.IfExp.test);
1644 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1645 ADDOP(c, POP_TOP);
1646 VISIT(c, expr, e->v.IfExp.body);
1647 ADDOP_JREL(c, JUMP_FORWARD, end);
1648 compiler_use_next_block(c, next);
1649 ADDOP(c, POP_TOP);
1650 VISIT(c, expr, e->v.IfExp.orelse);
1651 compiler_use_next_block(c, end);
1652 return 1;
1653}
1654
1655static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656compiler_lambda(struct compiler *c, expr_ty e)
1657{
1658 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001659 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001660 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 arguments_ty args = e->v.Lambda.args;
1662 assert(e->kind == Lambda_kind);
1663
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001664 if (!name) {
1665 name = PyString_InternFromString("<lambda>");
1666 if (!name)
1667 return 0;
1668 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669
Guido van Rossum4f72a782006-10-27 23:31:49 +00001670 if (args->kwonlyargs) {
1671 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1672 args->kw_defaults);
1673 if (res < 0) return 0;
1674 kw_default_count = res;
1675 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 if (args->defaults)
1677 VISIT_SEQ(c, expr, args->defaults);
1678 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1679 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001680
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001681 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 compiler_arguments(c, args);
1683
1684 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001685 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001686 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1687 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001689 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 if (co == NULL)
1691 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692
Guido van Rossum4f72a782006-10-27 23:31:49 +00001693 arglength = asdl_seq_LEN(args->defaults);
1694 arglength |= kw_default_count << 8;
1695 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001696 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697
1698 return 1;
1699}
1700
1701static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702compiler_if(struct compiler *c, stmt_ty s)
1703{
1704 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001705 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706 assert(s->kind == If_kind);
1707 end = compiler_new_block(c);
1708 if (end == NULL)
1709 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001710 next = compiler_new_block(c);
1711 if (next == NULL)
1712 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001713
1714 constant = expr_constant(s->v.If.test);
1715 /* constant = 0: "if 0"
1716 * constant = 1: "if 1", "if 2", ...
1717 * constant = -1: rest */
1718 if (constant == 0) {
1719 if (s->v.If.orelse)
1720 VISIT_SEQ(c, stmt, s->v.If.orelse);
1721 } else if (constant == 1) {
1722 VISIT_SEQ(c, stmt, s->v.If.body);
1723 } else {
1724 VISIT(c, expr, s->v.If.test);
1725 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1726 ADDOP(c, POP_TOP);
1727 VISIT_SEQ(c, stmt, s->v.If.body);
1728 ADDOP_JREL(c, JUMP_FORWARD, end);
1729 compiler_use_next_block(c, next);
1730 ADDOP(c, POP_TOP);
1731 if (s->v.If.orelse)
1732 VISIT_SEQ(c, stmt, s->v.If.orelse);
1733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 compiler_use_next_block(c, end);
1735 return 1;
1736}
1737
1738static int
1739compiler_for(struct compiler *c, stmt_ty s)
1740{
1741 basicblock *start, *cleanup, *end;
1742
1743 start = compiler_new_block(c);
1744 cleanup = compiler_new_block(c);
1745 end = compiler_new_block(c);
1746 if (start == NULL || end == NULL || cleanup == NULL)
1747 return 0;
1748 ADDOP_JREL(c, SETUP_LOOP, end);
1749 if (!compiler_push_fblock(c, LOOP, start))
1750 return 0;
1751 VISIT(c, expr, s->v.For.iter);
1752 ADDOP(c, GET_ITER);
1753 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001754 /* XXX(nnorwitz): is there a better way to handle this?
1755 for loops are special, we want to be able to trace them
1756 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001757 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758 ADDOP_JREL(c, FOR_ITER, cleanup);
1759 VISIT(c, expr, s->v.For.target);
1760 VISIT_SEQ(c, stmt, s->v.For.body);
1761 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1762 compiler_use_next_block(c, cleanup);
1763 ADDOP(c, POP_BLOCK);
1764 compiler_pop_fblock(c, LOOP, start);
1765 VISIT_SEQ(c, stmt, s->v.For.orelse);
1766 compiler_use_next_block(c, end);
1767 return 1;
1768}
1769
1770static int
1771compiler_while(struct compiler *c, stmt_ty s)
1772{
1773 basicblock *loop, *orelse, *end, *anchor = NULL;
1774 int constant = expr_constant(s->v.While.test);
1775
1776 if (constant == 0)
1777 return 1;
1778 loop = compiler_new_block(c);
1779 end = compiler_new_block(c);
1780 if (constant == -1) {
1781 anchor = compiler_new_block(c);
1782 if (anchor == NULL)
1783 return 0;
1784 }
1785 if (loop == NULL || end == NULL)
1786 return 0;
1787 if (s->v.While.orelse) {
1788 orelse = compiler_new_block(c);
1789 if (orelse == NULL)
1790 return 0;
1791 }
1792 else
1793 orelse = NULL;
1794
1795 ADDOP_JREL(c, SETUP_LOOP, end);
1796 compiler_use_next_block(c, loop);
1797 if (!compiler_push_fblock(c, LOOP, loop))
1798 return 0;
1799 if (constant == -1) {
1800 VISIT(c, expr, s->v.While.test);
1801 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1802 ADDOP(c, POP_TOP);
1803 }
1804 VISIT_SEQ(c, stmt, s->v.While.body);
1805 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1806
1807 /* XXX should the two POP instructions be in a separate block
1808 if there is no else clause ?
1809 */
1810
1811 if (constant == -1) {
1812 compiler_use_next_block(c, anchor);
1813 ADDOP(c, POP_TOP);
1814 ADDOP(c, POP_BLOCK);
1815 }
1816 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001817 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 VISIT_SEQ(c, stmt, s->v.While.orelse);
1819 compiler_use_next_block(c, end);
1820
1821 return 1;
1822}
1823
1824static int
1825compiler_continue(struct compiler *c)
1826{
1827 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001828 static const char IN_FINALLY_ERROR_MSG[] =
1829 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 int i;
1831
1832 if (!c->u->u_nfblocks)
1833 return compiler_error(c, LOOP_ERROR_MSG);
1834 i = c->u->u_nfblocks - 1;
1835 switch (c->u->u_fblock[i].fb_type) {
1836 case LOOP:
1837 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1838 break;
1839 case EXCEPT:
1840 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001841 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1842 /* Prevent continue anywhere under a finally
1843 even if hidden in a sub-try or except. */
1844 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1845 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 if (i == -1)
1848 return compiler_error(c, LOOP_ERROR_MSG);
1849 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1850 break;
1851 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001852 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 }
1854
1855 return 1;
1856}
1857
1858/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1859
1860 SETUP_FINALLY L
1861 <code for body>
1862 POP_BLOCK
1863 LOAD_CONST <None>
1864 L: <code for finalbody>
1865 END_FINALLY
1866
1867 The special instructions use the block stack. Each block
1868 stack entry contains the instruction that created it (here
1869 SETUP_FINALLY), the level of the value stack at the time the
1870 block stack entry was created, and a label (here L).
1871
1872 SETUP_FINALLY:
1873 Pushes the current value stack level and the label
1874 onto the block stack.
1875 POP_BLOCK:
1876 Pops en entry from the block stack, and pops the value
1877 stack until its level is the same as indicated on the
1878 block stack. (The label is ignored.)
1879 END_FINALLY:
1880 Pops a variable number of entries from the *value* stack
1881 and re-raises the exception they specify. The number of
1882 entries popped depends on the (pseudo) exception type.
1883
1884 The block stack is unwound when an exception is raised:
1885 when a SETUP_FINALLY entry is found, the exception is pushed
1886 onto the value stack (and the exception condition is cleared),
1887 and the interpreter jumps to the label gotten from the block
1888 stack.
1889*/
1890
1891static int
1892compiler_try_finally(struct compiler *c, stmt_ty s)
1893{
1894 basicblock *body, *end;
1895 body = compiler_new_block(c);
1896 end = compiler_new_block(c);
1897 if (body == NULL || end == NULL)
1898 return 0;
1899
1900 ADDOP_JREL(c, SETUP_FINALLY, end);
1901 compiler_use_next_block(c, body);
1902 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1903 return 0;
1904 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1905 ADDOP(c, POP_BLOCK);
1906 compiler_pop_fblock(c, FINALLY_TRY, body);
1907
1908 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1909 compiler_use_next_block(c, end);
1910 if (!compiler_push_fblock(c, FINALLY_END, end))
1911 return 0;
1912 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1913 ADDOP(c, END_FINALLY);
1914 compiler_pop_fblock(c, FINALLY_END, end);
1915
1916 return 1;
1917}
1918
1919/*
1920 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1921 (The contents of the value stack is shown in [], with the top
1922 at the right; 'tb' is trace-back info, 'val' the exception's
1923 associated value, and 'exc' the exception.)
1924
1925 Value stack Label Instruction Argument
1926 [] SETUP_EXCEPT L1
1927 [] <code for S>
1928 [] POP_BLOCK
1929 [] JUMP_FORWARD L0
1930
1931 [tb, val, exc] L1: DUP )
1932 [tb, val, exc, exc] <evaluate E1> )
1933 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1934 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1935 [tb, val, exc, 1] POP )
1936 [tb, val, exc] POP
1937 [tb, val] <assign to V1> (or POP if no V1)
1938 [tb] POP
1939 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001940 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941
1942 [tb, val, exc, 0] L2: POP
1943 [tb, val, exc] DUP
1944 .............................etc.......................
1945
1946 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001947 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948
1949 [] L0: <next statement>
1950
1951 Of course, parts are not generated if Vi or Ei is not present.
1952*/
1953static int
1954compiler_try_except(struct compiler *c, stmt_ty s)
1955{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001956 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 int i, n;
1958
1959 body = compiler_new_block(c);
1960 except = compiler_new_block(c);
1961 orelse = compiler_new_block(c);
1962 end = compiler_new_block(c);
1963 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1964 return 0;
1965 ADDOP_JREL(c, SETUP_EXCEPT, except);
1966 compiler_use_next_block(c, body);
1967 if (!compiler_push_fblock(c, EXCEPT, body))
1968 return 0;
1969 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1970 ADDOP(c, POP_BLOCK);
1971 compiler_pop_fblock(c, EXCEPT, body);
1972 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1973 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1974 compiler_use_next_block(c, except);
1975 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001976 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977 s->v.TryExcept.handlers, i);
1978 if (!handler->type && i < n-1)
1979 return compiler_error(c, "default 'except:' must be last");
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001980 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001981 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 except = compiler_new_block(c);
1983 if (except == NULL)
1984 return 0;
1985 if (handler->type) {
1986 ADDOP(c, DUP_TOP);
1987 VISIT(c, expr, handler->type);
1988 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1989 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1990 ADDOP(c, POP_TOP);
1991 }
1992 ADDOP(c, POP_TOP);
1993 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001994 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001995
1996 cleanup_end = compiler_new_block(c);
1997 cleanup_body = compiler_new_block(c);
1998 if(!(cleanup_end || cleanup_body))
1999 return 0;
2000
Guido van Rossum16be03e2007-01-10 18:51:35 +00002001 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002002 ADDOP(c, POP_TOP);
2003
2004 /*
2005 try:
2006 # body
2007 except type as name:
2008 try:
2009 # body
2010 finally:
2011 name = None
2012 del name
2013 */
2014
2015 /* second try: */
2016 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2017 compiler_use_next_block(c, cleanup_body);
2018 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2019 return 0;
2020
2021 /* second # body */
2022 VISIT_SEQ(c, stmt, handler->body);
2023 ADDOP(c, POP_BLOCK);
2024 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2025
2026 /* finally: */
2027 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2028 compiler_use_next_block(c, cleanup_end);
2029 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2030 return 0;
2031
2032 /* name = None */
2033 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00002034 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002035
Guido van Rossum16be03e2007-01-10 18:51:35 +00002036 /* del name */
2037 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002038
2039 ADDOP(c, END_FINALLY);
2040 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002041 }
2042 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002043 ADDOP(c, POP_TOP);
2044 ADDOP(c, POP_TOP);
2045 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002046 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 ADDOP_JREL(c, JUMP_FORWARD, end);
2048 compiler_use_next_block(c, except);
2049 if (handler->type)
2050 ADDOP(c, POP_TOP);
2051 }
2052 ADDOP(c, END_FINALLY);
2053 compiler_use_next_block(c, orelse);
2054 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2055 compiler_use_next_block(c, end);
2056 return 1;
2057}
2058
2059static int
2060compiler_import_as(struct compiler *c, identifier name, identifier asname)
2061{
2062 /* The IMPORT_NAME opcode was already generated. This function
2063 merely needs to bind the result to a name.
2064
2065 If there is a dot in name, we need to split it and emit a
2066 LOAD_ATTR for each name.
2067 */
2068 const char *src = PyString_AS_STRING(name);
2069 const char *dot = strchr(src, '.');
2070 if (dot) {
2071 /* Consume the base module name to get the first attribute */
2072 src = dot + 1;
2073 while (dot) {
2074 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002075 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 dot = strchr(src, '.');
Armin Rigo31441302005-10-21 12:57:31 +00002077 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002079 if (!attr)
2080 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002081 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002082 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002083 src = dot + 1;
2084 }
2085 }
2086 return compiler_nameop(c, asname, Store);
2087}
2088
2089static int
2090compiler_import(struct compiler *c, stmt_ty s)
2091{
2092 /* The Import node stores a module name like a.b.c as a single
2093 string. This is convenient for all cases except
2094 import a.b.c as d
2095 where we need to parse that string to extract the individual
2096 module names.
2097 XXX Perhaps change the representation to make this case simpler?
2098 */
2099 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002100
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002102 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002104 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105
Guido van Rossum45aecf42006-03-15 04:58:47 +00002106 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002107 if (level == NULL)
2108 return 0;
2109
2110 ADDOP_O(c, LOAD_CONST, level, consts);
2111 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002112 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2113 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2114
2115 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002116 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002117 if (!r)
2118 return r;
2119 }
2120 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002121 identifier tmp = alias->name;
2122 const char *base = PyString_AS_STRING(alias->name);
2123 char *dot = strchr(base, '.');
2124 if (dot)
2125 tmp = PyString_FromStringAndSize(base,
2126 dot - base);
2127 r = compiler_nameop(c, tmp, Store);
2128 if (dot) {
2129 Py_DECREF(tmp);
2130 }
2131 if (!r)
2132 return r;
2133 }
2134 }
2135 return 1;
2136}
2137
2138static int
2139compiler_from_import(struct compiler *c, stmt_ty s)
2140{
2141 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142
2143 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002144 PyObject *level;
2145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 if (!names)
2147 return 0;
2148
Guido van Rossum45aecf42006-03-15 04:58:47 +00002149 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002150 if (!level) {
2151 Py_DECREF(names);
2152 return 0;
2153 }
2154
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 /* build up the names */
2156 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002157 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 Py_INCREF(alias->name);
2159 PyTuple_SET_ITEM(names, i, alias->name);
2160 }
2161
2162 if (s->lineno > c->c_future->ff_lineno) {
2163 if (!strcmp(PyString_AS_STRING(s->v.ImportFrom.module),
2164 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002165 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002166 Py_DECREF(names);
2167 return compiler_error(c,
2168 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002169 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170
2171 }
2172 }
2173
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002174 ADDOP_O(c, LOAD_CONST, level, consts);
2175 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002176 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002177 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2179 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002180 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 identifier store_name;
2182
2183 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2184 assert(n == 1);
2185 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002186 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002187 }
2188
2189 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2190 store_name = alias->name;
2191 if (alias->asname)
2192 store_name = alias->asname;
2193
2194 if (!compiler_nameop(c, store_name, Store)) {
2195 Py_DECREF(names);
2196 return 0;
2197 }
2198 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002199 /* remove imported module */
2200 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 return 1;
2202}
2203
2204static int
2205compiler_assert(struct compiler *c, stmt_ty s)
2206{
2207 static PyObject *assertion_error = NULL;
2208 basicblock *end;
2209
2210 if (Py_OptimizeFlag)
2211 return 1;
2212 if (assertion_error == NULL) {
2213 assertion_error = PyString_FromString("AssertionError");
2214 if (assertion_error == NULL)
2215 return 0;
2216 }
2217 VISIT(c, expr, s->v.Assert.test);
2218 end = compiler_new_block(c);
2219 if (end == NULL)
2220 return 0;
2221 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2222 ADDOP(c, POP_TOP);
2223 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2224 if (s->v.Assert.msg) {
2225 VISIT(c, expr, s->v.Assert.msg);
2226 ADDOP_I(c, RAISE_VARARGS, 2);
2227 }
2228 else {
2229 ADDOP_I(c, RAISE_VARARGS, 1);
2230 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002231 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 ADDOP(c, POP_TOP);
2233 return 1;
2234}
2235
2236static int
2237compiler_visit_stmt(struct compiler *c, stmt_ty s)
2238{
2239 int i, n;
2240
Thomas Wouters89f507f2006-12-13 04:49:30 +00002241 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002243 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002246 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002248 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002249 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002250 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 if (c->u->u_ste->ste_type != FunctionBlock)
2252 return compiler_error(c, "'return' outside function");
2253 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002254 VISIT(c, expr, s->v.Return.value);
2255 }
2256 else
2257 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2258 ADDOP(c, RETURN_VALUE);
2259 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002260 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 VISIT_SEQ(c, expr, s->v.Delete.targets)
2262 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002263 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 n = asdl_seq_LEN(s->v.Assign.targets);
2265 VISIT(c, expr, s->v.Assign.value);
2266 for (i = 0; i < n; i++) {
2267 if (i < n - 1)
2268 ADDOP(c, DUP_TOP);
2269 VISIT(c, expr,
2270 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2271 }
2272 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002273 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002274 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002275 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002277 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002279 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002281 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 n = 0;
2283 if (s->v.Raise.type) {
2284 VISIT(c, expr, s->v.Raise.type);
2285 n++;
2286 if (s->v.Raise.inst) {
2287 VISIT(c, expr, s->v.Raise.inst);
2288 n++;
2289 if (s->v.Raise.tback) {
2290 VISIT(c, expr, s->v.Raise.tback);
2291 n++;
2292 }
2293 }
2294 }
2295 ADDOP_I(c, RAISE_VARARGS, n);
2296 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002297 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002299 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002301 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002303 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002305 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002307 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002308 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002310 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002312 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 ADDOP(c, PRINT_EXPR);
2314 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002315 else if (s->v.Expr.value->kind != Str_kind &&
2316 s->v.Expr.value->kind != Num_kind) {
2317 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 ADDOP(c, POP_TOP);
2319 }
2320 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002321 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002323 case Break_kind:
Thomas Wouters89f507f2006-12-13 04:49:30 +00002324 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 return compiler_error(c, "'break' outside loop");
2326 ADDOP(c, BREAK_LOOP);
2327 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002328 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002330 case With_kind:
2331 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 }
2333 return 1;
2334}
2335
2336static int
2337unaryop(unaryop_ty op)
2338{
2339 switch (op) {
2340 case Invert:
2341 return UNARY_INVERT;
2342 case Not:
2343 return UNARY_NOT;
2344 case UAdd:
2345 return UNARY_POSITIVE;
2346 case USub:
2347 return UNARY_NEGATIVE;
2348 }
2349 return 0;
2350}
2351
2352static int
2353binop(struct compiler *c, operator_ty op)
2354{
2355 switch (op) {
2356 case Add:
2357 return BINARY_ADD;
2358 case Sub:
2359 return BINARY_SUBTRACT;
2360 case Mult:
2361 return BINARY_MULTIPLY;
2362 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002363 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 case Mod:
2365 return BINARY_MODULO;
2366 case Pow:
2367 return BINARY_POWER;
2368 case LShift:
2369 return BINARY_LSHIFT;
2370 case RShift:
2371 return BINARY_RSHIFT;
2372 case BitOr:
2373 return BINARY_OR;
2374 case BitXor:
2375 return BINARY_XOR;
2376 case BitAnd:
2377 return BINARY_AND;
2378 case FloorDiv:
2379 return BINARY_FLOOR_DIVIDE;
2380 }
2381 return 0;
2382}
2383
2384static int
2385cmpop(cmpop_ty op)
2386{
2387 switch (op) {
2388 case Eq:
2389 return PyCmp_EQ;
2390 case NotEq:
2391 return PyCmp_NE;
2392 case Lt:
2393 return PyCmp_LT;
2394 case LtE:
2395 return PyCmp_LE;
2396 case Gt:
2397 return PyCmp_GT;
2398 case GtE:
2399 return PyCmp_GE;
2400 case Is:
2401 return PyCmp_IS;
2402 case IsNot:
2403 return PyCmp_IS_NOT;
2404 case In:
2405 return PyCmp_IN;
2406 case NotIn:
2407 return PyCmp_NOT_IN;
2408 }
2409 return PyCmp_BAD;
2410}
2411
2412static int
2413inplace_binop(struct compiler *c, operator_ty op)
2414{
2415 switch (op) {
2416 case Add:
2417 return INPLACE_ADD;
2418 case Sub:
2419 return INPLACE_SUBTRACT;
2420 case Mult:
2421 return INPLACE_MULTIPLY;
2422 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002423 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 case Mod:
2425 return INPLACE_MODULO;
2426 case Pow:
2427 return INPLACE_POWER;
2428 case LShift:
2429 return INPLACE_LSHIFT;
2430 case RShift:
2431 return INPLACE_RSHIFT;
2432 case BitOr:
2433 return INPLACE_OR;
2434 case BitXor:
2435 return INPLACE_XOR;
2436 case BitAnd:
2437 return INPLACE_AND;
2438 case FloorDiv:
2439 return INPLACE_FLOOR_DIVIDE;
2440 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002441 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002442 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443 return 0;
2444}
2445
2446static int
2447compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2448{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002449 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002450 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2451
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002452 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002453 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454 /* XXX AugStore isn't used anywhere! */
2455
2456 /* First check for assignment to __debug__. Param? */
2457 if ((ctx == Store || ctx == AugStore || ctx == Del)
2458 && !strcmp(PyString_AS_STRING(name), "__debug__")) {
2459 return compiler_error(c, "can not assign to __debug__");
2460 }
2461
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002462 mangled = _Py_Mangle(c->u->u_private, name);
2463 if (!mangled)
2464 return 0;
2465
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 op = 0;
2467 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002468 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 switch (scope) {
2470 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002471 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 optype = OP_DEREF;
2473 break;
2474 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002475 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 optype = OP_DEREF;
2477 break;
2478 case LOCAL:
2479 if (c->u->u_ste->ste_type == FunctionBlock)
2480 optype = OP_FAST;
2481 break;
2482 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002483 if (c->u->u_ste->ste_type == FunctionBlock &&
2484 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 optype = OP_GLOBAL;
2486 break;
2487 case GLOBAL_EXPLICIT:
2488 optype = OP_GLOBAL;
2489 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002490 default:
2491 /* scope can be 0 */
2492 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 }
2494
2495 /* XXX Leave assert here, but handle __doc__ and the like better */
2496 assert(scope || PyString_AS_STRING(name)[0] == '_');
2497
2498 switch (optype) {
2499 case OP_DEREF:
2500 switch (ctx) {
2501 case Load: op = LOAD_DEREF; break;
2502 case Store: op = STORE_DEREF; break;
2503 case AugLoad:
2504 case AugStore:
2505 break;
2506 case Del:
2507 PyErr_Format(PyExc_SyntaxError,
2508 "can not delete variable '%s' referenced "
2509 "in nested scope",
2510 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002511 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002514 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002515 PyErr_SetString(PyExc_SystemError,
2516 "param invalid for deref variable");
2517 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002518 }
2519 break;
2520 case OP_FAST:
2521 switch (ctx) {
2522 case Load: op = LOAD_FAST; break;
2523 case Store: op = STORE_FAST; break;
2524 case Del: op = DELETE_FAST; break;
2525 case AugLoad:
2526 case AugStore:
2527 break;
2528 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002529 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002530 PyErr_SetString(PyExc_SystemError,
2531 "param invalid for local variable");
2532 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002534 ADDOP_O(c, op, mangled, varnames);
2535 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 return 1;
2537 case OP_GLOBAL:
2538 switch (ctx) {
2539 case Load: op = LOAD_GLOBAL; break;
2540 case Store: op = STORE_GLOBAL; break;
2541 case Del: op = DELETE_GLOBAL; break;
2542 case AugLoad:
2543 case AugStore:
2544 break;
2545 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002546 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002547 PyErr_SetString(PyExc_SystemError,
2548 "param invalid for global variable");
2549 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002550 }
2551 break;
2552 case OP_NAME:
2553 switch (ctx) {
2554 case Load: op = LOAD_NAME; break;
2555 case Store: op = STORE_NAME; break;
2556 case Del: op = DELETE_NAME; break;
2557 case AugLoad:
2558 case AugStore:
2559 break;
2560 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002561 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002562 PyErr_SetString(PyExc_SystemError,
2563 "param invalid for name variable");
2564 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002565 }
2566 break;
2567 }
2568
2569 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002570 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002571 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002572 if (arg < 0)
2573 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002574 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002575}
2576
2577static int
2578compiler_boolop(struct compiler *c, expr_ty e)
2579{
2580 basicblock *end;
2581 int jumpi, i, n;
2582 asdl_seq *s;
2583
2584 assert(e->kind == BoolOp_kind);
2585 if (e->v.BoolOp.op == And)
2586 jumpi = JUMP_IF_FALSE;
2587 else
2588 jumpi = JUMP_IF_TRUE;
2589 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002590 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591 return 0;
2592 s = e->v.BoolOp.values;
2593 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002594 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002595 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002596 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597 ADDOP_JREL(c, jumpi, end);
2598 ADDOP(c, POP_TOP)
2599 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002600 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601 compiler_use_next_block(c, end);
2602 return 1;
2603}
2604
2605static int
2606compiler_list(struct compiler *c, expr_ty e)
2607{
2608 int n = asdl_seq_LEN(e->v.List.elts);
2609 if (e->v.List.ctx == Store) {
2610 ADDOP_I(c, UNPACK_SEQUENCE, n);
2611 }
2612 VISIT_SEQ(c, expr, e->v.List.elts);
2613 if (e->v.List.ctx == Load) {
2614 ADDOP_I(c, BUILD_LIST, n);
2615 }
2616 return 1;
2617}
2618
2619static int
2620compiler_tuple(struct compiler *c, expr_ty e)
2621{
2622 int n = asdl_seq_LEN(e->v.Tuple.elts);
2623 if (e->v.Tuple.ctx == Store) {
2624 ADDOP_I(c, UNPACK_SEQUENCE, n);
2625 }
2626 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2627 if (e->v.Tuple.ctx == Load) {
2628 ADDOP_I(c, BUILD_TUPLE, n);
2629 }
2630 return 1;
2631}
2632
2633static int
2634compiler_compare(struct compiler *c, expr_ty e)
2635{
2636 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002637 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638
2639 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2640 VISIT(c, expr, e->v.Compare.left);
2641 n = asdl_seq_LEN(e->v.Compare.ops);
2642 assert(n > 0);
2643 if (n > 1) {
2644 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002645 if (cleanup == NULL)
2646 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002647 VISIT(c, expr,
2648 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649 }
2650 for (i = 1; i < n; i++) {
2651 ADDOP(c, DUP_TOP);
2652 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002654 cmpop((cmpop_ty)(asdl_seq_GET(
2655 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2657 NEXT_BLOCK(c);
2658 ADDOP(c, POP_TOP);
2659 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002660 VISIT(c, expr,
2661 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002662 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002663 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002664 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002665 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666 if (n > 1) {
2667 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002668 if (end == NULL)
2669 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670 ADDOP_JREL(c, JUMP_FORWARD, end);
2671 compiler_use_next_block(c, cleanup);
2672 ADDOP(c, ROT_TWO);
2673 ADDOP(c, POP_TOP);
2674 compiler_use_next_block(c, end);
2675 }
2676 return 1;
2677}
2678
2679static int
2680compiler_call(struct compiler *c, expr_ty e)
2681{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002682 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002683 return compiler_call_helper(c, 0,
2684 e->v.Call.args,
2685 e->v.Call.keywords,
2686 e->v.Call.starargs,
2687 e->v.Call.kwargs);
2688}
2689
2690/* shared code between compiler_call and compiler_class */
2691static int
2692compiler_call_helper(struct compiler *c,
2693 int n, /* Args already pushed */
2694 asdl_seq *args,
2695 asdl_seq *keywords,
2696 expr_ty starargs,
2697 expr_ty kwargs)
2698{
2699 int code = 0;
2700
2701 n += asdl_seq_LEN(args);
2702 VISIT_SEQ(c, expr, args);
2703 if (keywords) {
2704 VISIT_SEQ(c, keyword, keywords);
2705 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002707 if (starargs) {
2708 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709 code |= 1;
2710 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002711 if (kwargs) {
2712 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 code |= 2;
2714 }
2715 switch (code) {
2716 case 0:
2717 ADDOP_I(c, CALL_FUNCTION, n);
2718 break;
2719 case 1:
2720 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2721 break;
2722 case 2:
2723 ADDOP_I(c, CALL_FUNCTION_KW, n);
2724 break;
2725 case 3:
2726 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2727 break;
2728 }
2729 return 1;
2730}
2731
Nick Coghlan650f0d02007-04-15 12:05:43 +00002732
2733/* List and set comprehensions and generator expressions work by creating a
2734 nested function to perform the actual iteration. This means that the
2735 iteration variables don't leak into the current scope.
2736 The defined function is called immediately following its definition, with the
2737 result of that call being the result of the expression.
2738 The LC/SC version returns the populated container, while the GE version is
2739 flagged in symtable.c as a generator, so it returns the generator object
2740 when the function is called.
2741 This code *knows* that the loop cannot contain break, continue, or return,
2742 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2743
2744 Possible cleanups:
2745 - iterate over the generator sequence instead of using recursion
2746*/
2747
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002749compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2750 asdl_seq *generators, int gen_index,
2751 expr_ty elt, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752{
2753 /* generate code for the iterator, then each of the ifs,
2754 and then write to the element */
2755
Nick Coghlan650f0d02007-04-15 12:05:43 +00002756 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002758 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002759
2760 start = compiler_new_block(c);
2761 skip = compiler_new_block(c);
2762 if_cleanup = compiler_new_block(c);
2763 anchor = compiler_new_block(c);
2764
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002765 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002766 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002767 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768
Nick Coghlan650f0d02007-04-15 12:05:43 +00002769 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002770
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771 if (gen_index == 0) {
2772 /* Receive outermost iter as an implicit argument */
2773 c->u->u_argcount = 1;
2774 ADDOP_I(c, LOAD_FAST, 0);
2775 }
2776 else {
2777 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002778 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002779 ADDOP(c, GET_ITER);
2780 }
2781 compiler_use_next_block(c, start);
2782 ADDOP_JREL(c, FOR_ITER, anchor);
2783 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002784 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002785
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002786 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002787 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002788 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790 VISIT(c, expr, e);
2791 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2792 NEXT_BLOCK(c);
2793 ADDOP(c, POP_TOP);
2794 }
2795
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002796 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002797 if (!compiler_comprehension_generator(c, tmpname,
2798 generators, gen_index,
2799 elt, type))
2800 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801
Nick Coghlan650f0d02007-04-15 12:05:43 +00002802 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002803 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002804 /* comprehension specific code */
2805 switch (type) {
2806 case COMP_GENEXP:
2807 VISIT(c, expr, elt);
2808 ADDOP(c, YIELD_VALUE);
2809 ADDOP(c, POP_TOP);
2810 break;
2811 case COMP_LISTCOMP:
2812 if (!compiler_nameop(c, tmpname, Load))
2813 return 0;
2814 VISIT(c, expr, elt);
2815 ADDOP(c, LIST_APPEND);
2816 break;
2817 case COMP_SETCOMP:
2818 if (!compiler_nameop(c, tmpname, Load))
2819 return 0;
2820 VISIT(c, expr, elt);
2821 ADDOP(c, SET_ADD);
2822 break;
2823 default:
2824 return 0;
2825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826
2827 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 for (i = 0; i < n; i++) {
2830 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002831 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002832 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 ADDOP(c, POP_TOP);
2835 }
2836 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2837 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838
2839 return 1;
2840}
2841
2842static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002843compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2844 asdl_seq *generators, expr_ty elt)
2845{
2846 PyCodeObject *co = NULL;
2847 identifier tmp = NULL;
2848 expr_ty outermost_iter;
2849
2850 outermost_iter = ((comprehension_ty)
2851 asdl_seq_GET(generators, 0))->iter;
2852
2853 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2854 goto error;
2855
2856 if (type != COMP_GENEXP) {
2857 tmp = compiler_new_tmpname(c);
2858 if (!tmp)
2859 goto error_in_scope;
2860
2861 ADDOP_I(c, (type == COMP_LISTCOMP ?
2862 BUILD_LIST : BUILD_SET), 0);
2863 ADDOP(c, DUP_TOP);
2864 if (!compiler_nameop(c, tmp, Store))
2865 goto error_in_scope;
2866 }
2867
2868 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt, type))
2869 goto error_in_scope;
2870
2871 if (type != COMP_GENEXP) {
2872 ADDOP(c, RETURN_VALUE);
2873 }
2874
2875 co = assemble(c, 1);
2876 compiler_exit_scope(c);
2877 if (co == NULL)
2878 goto error;
2879
2880 if (!compiler_make_closure(c, co, 0))
2881 goto error;
2882 Py_DECREF(co);
2883 Py_XDECREF(tmp);
2884
2885 VISIT(c, expr, outermost_iter);
2886 ADDOP(c, GET_ITER);
2887 ADDOP_I(c, CALL_FUNCTION, 1);
2888 return 1;
2889error_in_scope:
2890 compiler_exit_scope(c);
2891error:
2892 Py_XDECREF(co);
2893 Py_XDECREF(tmp);
2894 return 0;
2895}
2896
2897static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002898compiler_genexp(struct compiler *c, expr_ty e)
2899{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002900 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002901 if (!name) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002902 name = PyString_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002903 if (!name)
2904 return 0;
2905 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002906 assert(e->kind == GeneratorExp_kind);
2907 return compiler_comprehension(c, e, COMP_GENEXP, name,
2908 e->v.GeneratorExp.generators,
2909 e->v.GeneratorExp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002910}
2911
2912static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002913compiler_listcomp(struct compiler *c, expr_ty e)
2914{
2915 static identifier name;
2916 if (!name) {
2917 name = PyString_FromString("<listcomp>");
2918 if (!name)
2919 return 0;
2920 }
2921 assert(e->kind == ListComp_kind);
2922 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2923 e->v.ListComp.generators,
2924 e->v.ListComp.elt);
2925}
2926
2927static int
2928compiler_setcomp(struct compiler *c, expr_ty e)
2929{
2930 static identifier name;
2931 if (!name) {
2932 name = PyString_FromString("<setcomp>");
2933 if (!name)
2934 return 0;
2935 }
2936 assert(e->kind == SetComp_kind);
2937 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2938 e->v.SetComp.generators,
2939 e->v.SetComp.elt);
2940}
2941
2942
2943static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002944compiler_visit_keyword(struct compiler *c, keyword_ty k)
2945{
2946 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2947 VISIT(c, expr, k->value);
2948 return 1;
2949}
2950
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002951/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952 whether they are true or false.
2953
2954 Return values: 1 for true, 0 for false, -1 for non-constant.
2955 */
2956
2957static int
2958expr_constant(expr_ty e)
2959{
2960 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002961 case Ellipsis_kind:
2962 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 case Num_kind:
2964 return PyObject_IsTrue(e->v.Num.n);
2965 case Str_kind:
2966 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002967 case Name_kind:
2968 /* __debug__ is not assignable, so we can optimize
2969 * it away in if and while statements */
2970 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2971 "__debug__") == 0)
2972 return ! Py_OptimizeFlag;
2973 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 default:
2975 return -1;
2976 }
2977}
2978
Guido van Rossumc2e20742006-02-27 22:32:47 +00002979/*
2980 Implements the with statement from PEP 343.
2981
2982 The semantics outlined in that PEP are as follows:
2983
2984 with EXPR as VAR:
2985 BLOCK
2986
2987 It is implemented roughly as:
2988
Thomas Wouters477c8d52006-05-27 19:21:47 +00002989 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002990 exit = context.__exit__ # not calling it
2991 value = context.__enter__()
2992 try:
2993 VAR = value # if VAR present in the syntax
2994 BLOCK
2995 finally:
2996 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002997 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002999 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003000 exit(*exc)
3001 */
3002static int
3003compiler_with(struct compiler *c, stmt_ty s)
3004{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003005 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003006 basicblock *block, *finally;
3007 identifier tmpexit, tmpvalue = NULL;
3008
3009 assert(s->kind == With_kind);
3010
Guido van Rossumc2e20742006-02-27 22:32:47 +00003011 if (!enter_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003012 enter_attr = PyString_InternFromString("__enter__");
3013 if (!enter_attr)
3014 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003015 }
3016 if (!exit_attr) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003017 exit_attr = PyString_InternFromString("__exit__");
3018 if (!exit_attr)
3019 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003020 }
3021
3022 block = compiler_new_block(c);
3023 finally = compiler_new_block(c);
3024 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003025 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003026
3027 /* Create a temporary variable to hold context.__exit__ */
3028 tmpexit = compiler_new_tmpname(c);
3029 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003031 PyArena_AddPyObject(c->c_arena, tmpexit);
3032
3033 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003034 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003035 We need to do this rather than preserving it on the stack
3036 because SETUP_FINALLY remembers the stack level.
3037 We need to do the assignment *inside* the try/finally
3038 so that context.__exit__() is called when the assignment
3039 fails. But we need to call context.__enter__() *before*
3040 the try/finally so that if it fails we won't call
3041 context.__exit__().
3042 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003043 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003044 if (tmpvalue == NULL)
3045 return 0;
3046 PyArena_AddPyObject(c->c_arena, tmpvalue);
3047 }
3048
Thomas Wouters477c8d52006-05-27 19:21:47 +00003049 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003050 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003051
3052 /* Squirrel away context.__exit__ */
3053 ADDOP(c, DUP_TOP);
3054 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3055 if (!compiler_nameop(c, tmpexit, Store))
3056 return 0;
3057
3058 /* Call context.__enter__() */
3059 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3060 ADDOP_I(c, CALL_FUNCTION, 0);
3061
3062 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003063 /* Store it in tmpvalue */
3064 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065 return 0;
3066 }
3067 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003068 /* Discard result from context.__enter__() */
3069 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003070 }
3071
3072 /* Start the try block */
3073 ADDOP_JREL(c, SETUP_FINALLY, finally);
3074
3075 compiler_use_next_block(c, block);
3076 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003077 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003078 }
3079
3080 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003081 /* Bind saved result of context.__enter__() to VAR */
3082 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083 !compiler_nameop(c, tmpvalue, Del))
3084 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003085 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003086 }
3087
3088 /* BLOCK code */
3089 VISIT_SEQ(c, stmt, s->v.With.body);
3090
3091 /* End of try block; start the finally block */
3092 ADDOP(c, POP_BLOCK);
3093 compiler_pop_fblock(c, FINALLY_TRY, block);
3094
3095 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3096 compiler_use_next_block(c, finally);
3097 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003098 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003099
3100 /* Finally block starts; push tmpexit and issue our magic opcode. */
3101 if (!compiler_nameop(c, tmpexit, Load) ||
3102 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003103 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003104 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003105
3106 /* Finally block ends. */
3107 ADDOP(c, END_FINALLY);
3108 compiler_pop_fblock(c, FINALLY_END, finally);
3109 return 1;
3110}
3111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112static int
3113compiler_visit_expr(struct compiler *c, expr_ty e)
3114{
3115 int i, n;
3116
Thomas Wouters89f507f2006-12-13 04:49:30 +00003117 /* If expr e has a different line number than the last expr/stmt,
3118 set a new line number for the next instruction.
3119 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120 if (e->lineno > c->u->u_lineno) {
3121 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003122 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123 }
3124 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003125 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003126 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003127 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 VISIT(c, expr, e->v.BinOp.left);
3129 VISIT(c, expr, e->v.BinOp.right);
3130 ADDOP(c, binop(c, e->v.BinOp.op));
3131 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003132 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003133 VISIT(c, expr, e->v.UnaryOp.operand);
3134 ADDOP(c, unaryop(e->v.UnaryOp.op));
3135 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003136 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003138 case IfExp_kind:
3139 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003140 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 /* XXX get rid of arg? */
3142 ADDOP_I(c, BUILD_MAP, 0);
3143 n = asdl_seq_LEN(e->v.Dict.values);
3144 /* We must arrange things just right for STORE_SUBSCR.
3145 It wants the stack to look like (value) (dict) (key) */
3146 for (i = 0; i < n; i++) {
3147 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003148 VISIT(c, expr,
3149 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003150 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003151 VISIT(c, expr,
3152 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 ADDOP(c, STORE_SUBSCR);
3154 }
3155 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003156 case Set_kind:
3157 n = asdl_seq_LEN(e->v.Set.elts);
3158 VISIT_SEQ(c, expr, e->v.Set.elts);
3159 ADDOP_I(c, BUILD_SET, n);
3160 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003161 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003162 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003163 case ListComp_kind:
3164 return compiler_listcomp(c, e);
3165 case SetComp_kind:
3166 return compiler_setcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 case Yield_kind:
3168 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003169 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 if (e->v.Yield.value) {
3171 VISIT(c, expr, e->v.Yield.value);
3172 }
3173 else {
3174 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3175 }
3176 ADDOP(c, YIELD_VALUE);
3177 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003178 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003180 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003181 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003182 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3184 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003185 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003186 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3187 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003188 case Bytes_kind:
3189 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3190 ADDOP(c, MAKE_BYTES);
3191 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003192 case Ellipsis_kind:
3193 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3194 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003196 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 if (e->v.Attribute.ctx != AugStore)
3198 VISIT(c, expr, e->v.Attribute.value);
3199 switch (e->v.Attribute.ctx) {
3200 case AugLoad:
3201 ADDOP(c, DUP_TOP);
3202 /* Fall through to load */
3203 case Load:
3204 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3205 break;
3206 case AugStore:
3207 ADDOP(c, ROT_TWO);
3208 /* Fall through to save */
3209 case Store:
3210 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3211 break;
3212 case Del:
3213 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3214 break;
3215 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003216 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003217 PyErr_SetString(PyExc_SystemError,
3218 "param invalid in attribute expression");
3219 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 }
3221 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003222 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003223 switch (e->v.Subscript.ctx) {
3224 case AugLoad:
3225 VISIT(c, expr, e->v.Subscript.value);
3226 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3227 break;
3228 case Load:
3229 VISIT(c, expr, e->v.Subscript.value);
3230 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3231 break;
3232 case AugStore:
3233 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3234 break;
3235 case Store:
3236 VISIT(c, expr, e->v.Subscript.value);
3237 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3238 break;
3239 case Del:
3240 VISIT(c, expr, e->v.Subscript.value);
3241 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3242 break;
3243 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003244 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003245 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003246 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003247 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003248 }
3249 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003250 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003251 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3252 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003253 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003255 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 return compiler_tuple(c, e);
3257 }
3258 return 1;
3259}
3260
3261static int
3262compiler_augassign(struct compiler *c, stmt_ty s)
3263{
3264 expr_ty e = s->v.AugAssign.target;
3265 expr_ty auge;
3266
3267 assert(s->kind == AugAssign_kind);
3268
3269 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003270 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003272 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003273 if (auge == NULL)
3274 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275 VISIT(c, expr, auge);
3276 VISIT(c, expr, s->v.AugAssign.value);
3277 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3278 auge->v.Attribute.ctx = AugStore;
3279 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 break;
3281 case Subscript_kind:
3282 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003283 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003284 if (auge == NULL)
3285 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003286 VISIT(c, expr, auge);
3287 VISIT(c, expr, s->v.AugAssign.value);
3288 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003289 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003290 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003291 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003293 if (!compiler_nameop(c, e->v.Name.id, Load))
3294 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 VISIT(c, expr, s->v.AugAssign.value);
3296 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3297 return compiler_nameop(c, e->v.Name.id, Store);
3298 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003299 PyErr_Format(PyExc_SystemError,
3300 "invalid node type (%d) for augmented assignment",
3301 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003302 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 }
3304 return 1;
3305}
3306
3307static int
3308compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3309{
3310 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003311 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3312 PyErr_SetString(PyExc_SystemError,
3313 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003315 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316 f = &c->u->u_fblock[c->u->u_nfblocks++];
3317 f->fb_type = t;
3318 f->fb_block = b;
3319 return 1;
3320}
3321
3322static void
3323compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3324{
3325 struct compiler_unit *u = c->u;
3326 assert(u->u_nfblocks > 0);
3327 u->u_nfblocks--;
3328 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3329 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3330}
3331
Thomas Wouters89f507f2006-12-13 04:49:30 +00003332static int
3333compiler_in_loop(struct compiler *c) {
3334 int i;
3335 struct compiler_unit *u = c->u;
3336 for (i = 0; i < u->u_nfblocks; ++i) {
3337 if (u->u_fblock[i].fb_type == LOOP)
3338 return 1;
3339 }
3340 return 0;
3341}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342/* Raises a SyntaxError and returns 0.
3343 If something goes wrong, a different exception may be raised.
3344*/
3345
3346static int
3347compiler_error(struct compiler *c, const char *errstr)
3348{
3349 PyObject *loc;
3350 PyObject *u = NULL, *v = NULL;
3351
3352 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3353 if (!loc) {
3354 Py_INCREF(Py_None);
3355 loc = Py_None;
3356 }
3357 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3358 Py_None, loc);
3359 if (!u)
3360 goto exit;
3361 v = Py_BuildValue("(zO)", errstr, u);
3362 if (!v)
3363 goto exit;
3364 PyErr_SetObject(PyExc_SyntaxError, v);
3365 exit:
3366 Py_DECREF(loc);
3367 Py_XDECREF(u);
3368 Py_XDECREF(v);
3369 return 0;
3370}
3371
3372static int
3373compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003374 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003375{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003376 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003378 /* XXX this code is duplicated */
3379 switch (ctx) {
3380 case AugLoad: /* fall through to Load */
3381 case Load: op = BINARY_SUBSCR; break;
3382 case AugStore:/* fall through to Store */
3383 case Store: op = STORE_SUBSCR; break;
3384 case Del: op = DELETE_SUBSCR; break;
3385 case Param:
3386 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003387 "invalid %s kind %d in subscript\n",
3388 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003389 return 0;
3390 }
3391 if (ctx == AugLoad) {
3392 ADDOP_I(c, DUP_TOPX, 2);
3393 }
3394 else if (ctx == AugStore) {
3395 ADDOP(c, ROT_THREE);
3396 }
3397 ADDOP(c, op);
3398 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003399}
3400
3401static int
3402compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3403{
3404 int n = 2;
3405 assert(s->kind == Slice_kind);
3406
3407 /* only handles the cases where BUILD_SLICE is emitted */
3408 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003409 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410 }
3411 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003412 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003413 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003414
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003416 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417 }
3418 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003419 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420 }
3421
3422 if (s->v.Slice.step) {
3423 n++;
3424 VISIT(c, expr, s->v.Slice.step);
3425 }
3426 ADDOP_I(c, BUILD_SLICE, n);
3427 return 1;
3428}
3429
3430static int
3431compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3432{
3433 int op = 0, slice_offset = 0, stack_count = 0;
3434
3435 assert(s->v.Slice.step == NULL);
3436 if (s->v.Slice.lower) {
3437 slice_offset++;
3438 stack_count++;
3439 if (ctx != AugStore)
3440 VISIT(c, expr, s->v.Slice.lower);
3441 }
3442 if (s->v.Slice.upper) {
3443 slice_offset += 2;
3444 stack_count++;
3445 if (ctx != AugStore)
3446 VISIT(c, expr, s->v.Slice.upper);
3447 }
3448
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003449 if (ctx == AugLoad) {
3450 switch (stack_count) {
3451 case 0: ADDOP(c, DUP_TOP); break;
3452 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3453 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3454 }
3455 }
3456 else if (ctx == AugStore) {
3457 switch (stack_count) {
3458 case 0: ADDOP(c, ROT_TWO); break;
3459 case 1: ADDOP(c, ROT_THREE); break;
3460 case 2: ADDOP(c, ROT_FOUR); break;
3461 }
3462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463
3464 switch (ctx) {
3465 case AugLoad: /* fall through to Load */
3466 case Load: op = SLICE; break;
3467 case AugStore:/* fall through to Store */
3468 case Store: op = STORE_SLICE; break;
3469 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003470 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003471 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003472 PyErr_SetString(PyExc_SystemError,
3473 "param invalid in simple slice");
3474 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475 }
3476
3477 ADDOP(c, op + slice_offset);
3478 return 1;
3479}
3480
3481static int
3482compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3483 expr_context_ty ctx)
3484{
3485 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 case Slice_kind:
3487 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003488 case Index_kind:
3489 VISIT(c, expr, s->v.Index.value);
3490 break;
3491 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003492 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003493 PyErr_SetString(PyExc_SystemError,
3494 "extended slice invalid in nested slice");
3495 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003496 }
3497 return 1;
3498}
3499
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500static int
3501compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3502{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003503 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003505 case Index_kind:
3506 kindname = "index";
3507 if (ctx != AugStore) {
3508 VISIT(c, expr, s->v.Index.value);
3509 }
3510 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003512 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 if (!s->v.Slice.step)
3514 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003515 if (ctx != AugStore) {
3516 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517 return 0;
3518 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003519 break;
3520 case ExtSlice_kind:
3521 kindname = "extended slice";
3522 if (ctx != AugStore) {
3523 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3524 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003525 slice_ty sub = (slice_ty)asdl_seq_GET(
3526 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003527 if (!compiler_visit_nested_slice(c, sub, ctx))
3528 return 0;
3529 }
3530 ADDOP_I(c, BUILD_TUPLE, n);
3531 }
3532 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003533 default:
3534 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003535 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003536 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003538 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003539}
3540
Thomas Wouters89f507f2006-12-13 04:49:30 +00003541/* End of the compiler section, beginning of the assembler section */
3542
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543/* do depth-first search of basic block graph, starting with block.
3544 post records the block indices in post-order.
3545
3546 XXX must handle implicit jumps from one block to next
3547*/
3548
Thomas Wouters89f507f2006-12-13 04:49:30 +00003549struct assembler {
3550 PyObject *a_bytecode; /* string containing bytecode */
3551 int a_offset; /* offset into bytecode */
3552 int a_nblocks; /* number of reachable blocks */
3553 basicblock **a_postorder; /* list of blocks in dfs postorder */
3554 PyObject *a_lnotab; /* string containing lnotab */
3555 int a_lnotab_off; /* offset into lnotab */
3556 int a_lineno; /* last lineno of emitted instruction */
3557 int a_lineno_off; /* bytecode offset of last lineno */
3558};
3559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560static void
3561dfs(struct compiler *c, basicblock *b, struct assembler *a)
3562{
3563 int i;
3564 struct instr *instr = NULL;
3565
3566 if (b->b_seen)
3567 return;
3568 b->b_seen = 1;
3569 if (b->b_next != NULL)
3570 dfs(c, b->b_next, a);
3571 for (i = 0; i < b->b_iused; i++) {
3572 instr = &b->b_instr[i];
3573 if (instr->i_jrel || instr->i_jabs)
3574 dfs(c, instr->i_target, a);
3575 }
3576 a->a_postorder[a->a_nblocks++] = b;
3577}
3578
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003579static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3581{
3582 int i;
3583 struct instr *instr;
3584 if (b->b_seen || b->b_startdepth >= depth)
3585 return maxdepth;
3586 b->b_seen = 1;
3587 b->b_startdepth = depth;
3588 for (i = 0; i < b->b_iused; i++) {
3589 instr = &b->b_instr[i];
3590 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3591 if (depth > maxdepth)
3592 maxdepth = depth;
3593 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3594 if (instr->i_jrel || instr->i_jabs) {
3595 maxdepth = stackdepth_walk(c, instr->i_target,
3596 depth, maxdepth);
3597 if (instr->i_opcode == JUMP_ABSOLUTE ||
3598 instr->i_opcode == JUMP_FORWARD) {
3599 goto out; /* remaining code is dead */
3600 }
3601 }
3602 }
3603 if (b->b_next)
3604 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3605out:
3606 b->b_seen = 0;
3607 return maxdepth;
3608}
3609
3610/* Find the flow path that needs the largest stack. We assume that
3611 * cycles in the flow graph have no net effect on the stack depth.
3612 */
3613static int
3614stackdepth(struct compiler *c)
3615{
3616 basicblock *b, *entryblock;
3617 entryblock = NULL;
3618 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3619 b->b_seen = 0;
3620 b->b_startdepth = INT_MIN;
3621 entryblock = b;
3622 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003623 if (!entryblock)
3624 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003625 return stackdepth_walk(c, entryblock, 0, 0);
3626}
3627
3628static int
3629assemble_init(struct assembler *a, int nblocks, int firstlineno)
3630{
3631 memset(a, 0, sizeof(struct assembler));
3632 a->a_lineno = firstlineno;
3633 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3634 if (!a->a_bytecode)
3635 return 0;
3636 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3637 if (!a->a_lnotab)
3638 return 0;
3639 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003640 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003641 if (!a->a_postorder) {
3642 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 return 1;
3646}
3647
3648static void
3649assemble_free(struct assembler *a)
3650{
3651 Py_XDECREF(a->a_bytecode);
3652 Py_XDECREF(a->a_lnotab);
3653 if (a->a_postorder)
3654 PyObject_Free(a->a_postorder);
3655}
3656
3657/* Return the size of a basic block in bytes. */
3658
3659static int
3660instrsize(struct instr *instr)
3661{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003662 if (!instr->i_hasarg)
3663 return 1;
3664 if (instr->i_oparg > 0xffff)
3665 return 6;
3666 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667}
3668
3669static int
3670blocksize(basicblock *b)
3671{
3672 int i;
3673 int size = 0;
3674
3675 for (i = 0; i < b->b_iused; i++)
3676 size += instrsize(&b->b_instr[i]);
3677 return size;
3678}
3679
3680/* All about a_lnotab.
3681
3682c_lnotab is an array of unsigned bytes disguised as a Python string.
3683It is used to map bytecode offsets to source code line #s (when needed
3684for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003685
Tim Peters2a7f3842001-06-09 09:26:21 +00003686The array is conceptually a list of
3687 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003688pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003689
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003690 byte code offset source code line number
3691 0 1
3692 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003693 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003694 350 307
3695 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003696
3697The first trick is that these numbers aren't stored, only the increments
3698from one row to the next (this doesn't really work, but it's a start):
3699
3700 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3701
3702The second trick is that an unsigned byte can't hold negative values, or
3703values larger than 255, so (a) there's a deep assumption that byte code
3704offsets and their corresponding line #s both increase monotonically, and (b)
3705if at least one column jumps by more than 255 from one row to the next, more
3706than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003707from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003708part. A user of c_lnotab desiring to find the source line number
3709corresponding to a bytecode address A should do something like this
3710
3711 lineno = addr = 0
3712 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003713 addr += addr_incr
3714 if addr > A:
3715 return lineno
3716 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003717
3718In order for this to work, when the addr field increments by more than 255,
3719the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003720increment is < 256. So, in the example above, assemble_lnotab (it used
3721to be called com_set_lineno) should not (as was actually done until 2.2)
3722expand 300, 300 to 255, 255, 45, 45,
3723 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003724*/
3725
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003726static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003728{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 int d_bytecode, d_lineno;
3730 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003731 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732
3733 d_bytecode = a->a_offset - a->a_lineno_off;
3734 d_lineno = i->i_lineno - a->a_lineno;
3735
3736 assert(d_bytecode >= 0);
3737 assert(d_lineno >= 0);
3738
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003739 /* XXX(nnorwitz): is there a better way to handle this?
3740 for loops are special, we want to be able to trace them
3741 each time around, so we need to set an extra line number. */
3742 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003743 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003744
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003745 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003746 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 nbytes = a->a_lnotab_off + 2 * ncodes;
3748 len = PyString_GET_SIZE(a->a_lnotab);
3749 if (nbytes >= len) {
3750 if (len * 2 < nbytes)
3751 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003752 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 len *= 2;
3754 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3755 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003756 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003757 lnotab = (unsigned char *)
3758 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003759 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760 *lnotab++ = 255;
3761 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003762 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 d_bytecode -= ncodes * 255;
3764 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766 assert(d_bytecode <= 255);
3767 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003768 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 nbytes = a->a_lnotab_off + 2 * ncodes;
3770 len = PyString_GET_SIZE(a->a_lnotab);
3771 if (nbytes >= len) {
3772 if (len * 2 < nbytes)
3773 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003774 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 len *= 2;
3776 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3777 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003778 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003779 lnotab = (unsigned char *)
3780 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003782 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003784 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003785 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003786 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 d_lineno -= ncodes * 255;
3789 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003790 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003791
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 len = PyString_GET_SIZE(a->a_lnotab);
3793 if (a->a_lnotab_off + 2 >= len) {
3794 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003795 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003796 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003797 lnotab = (unsigned char *)
3798 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 a->a_lnotab_off += 2;
3801 if (d_bytecode) {
3802 *lnotab++ = d_bytecode;
3803 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003804 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003805 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 *lnotab++ = 0;
3807 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 a->a_lineno = i->i_lineno;
3810 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003811 return 1;
3812}
3813
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814/* assemble_emit()
3815 Extend the bytecode with a new instruction.
3816 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003817*/
3818
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003819static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003821{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003822 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003823 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 char *code;
3825
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003826 size = instrsize(i);
3827 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003829 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003832 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833 if (a->a_offset + size >= len) {
3834 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003835 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003836 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003837 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3838 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003839 if (size == 6) {
3840 assert(i->i_hasarg);
3841 *code++ = (char)EXTENDED_ARG;
3842 *code++ = ext & 0xff;
3843 *code++ = ext >> 8;
3844 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003847 if (i->i_hasarg) {
3848 assert(size == 3 || size == 6);
3849 *code++ = arg & 0xff;
3850 *code++ = arg >> 8;
3851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003853}
3854
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003855static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003857{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003859 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003860 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003861
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003862 /* Compute the size of each block and fixup jump args.
3863 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003864start:
3865 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003867 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003868 bsize = blocksize(b);
3869 b->b_offset = totsize;
3870 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003871 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003872 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3874 bsize = b->b_offset;
3875 for (i = 0; i < b->b_iused; i++) {
3876 struct instr *instr = &b->b_instr[i];
3877 /* Relative jumps are computed relative to
3878 the instruction pointer after fetching
3879 the jump instruction.
3880 */
3881 bsize += instrsize(instr);
3882 if (instr->i_jabs)
3883 instr->i_oparg = instr->i_target->b_offset;
3884 else if (instr->i_jrel) {
3885 int delta = instr->i_target->b_offset - bsize;
3886 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003887 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003888 else
3889 continue;
3890 if (instr->i_oparg > 0xffff)
3891 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003892 }
3893 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003894
3895 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003896 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003897 with a better solution.
3898
3899 In the meantime, should the goto be dropped in favor
3900 of a loop?
3901
3902 The issue is that in the first loop blocksize() is called
3903 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003904 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003905 i_oparg is calculated in the second loop above.
3906
3907 So we loop until we stop seeing new EXTENDED_ARGs.
3908 The only EXTENDED_ARGs that could be popping up are
3909 ones in jump instructions. So this should converge
3910 fairly quickly.
3911 */
3912 if (last_extended_arg_count != extended_arg_count) {
3913 last_extended_arg_count = extended_arg_count;
3914 goto start;
3915 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003916}
3917
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003918static PyObject *
3919dict_keys_inorder(PyObject *dict, int offset)
3920{
3921 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003922 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003923
3924 tuple = PyTuple_New(size);
3925 if (tuple == NULL)
3926 return NULL;
3927 while (PyDict_Next(dict, &pos, &k, &v)) {
3928 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003929 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003930 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003931 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003932 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003933 PyTuple_SET_ITEM(tuple, i - offset, k);
3934 }
3935 return tuple;
3936}
3937
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003938static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003940{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 PySTEntryObject *ste = c->u->u_ste;
3942 int flags = 0, n;
3943 if (ste->ste_type != ModuleBlock)
3944 flags |= CO_NEWLOCALS;
3945 if (ste->ste_type == FunctionBlock) {
3946 if (!ste->ste_unoptimized)
3947 flags |= CO_OPTIMIZED;
3948 if (ste->ste_nested)
3949 flags |= CO_NESTED;
3950 if (ste->ste_generator)
3951 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003952 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003953 if (ste->ste_varargs)
3954 flags |= CO_VARARGS;
3955 if (ste->ste_varkeywords)
3956 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003957 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003959
3960 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003961 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 n = PyDict_Size(c->u->u_freevars);
3964 if (n < 0)
3965 return -1;
3966 if (n == 0) {
3967 n = PyDict_Size(c->u->u_cellvars);
3968 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003969 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003970 if (n == 0) {
3971 flags |= CO_NOFREE;
3972 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003973 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003974
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003975 return flags;
3976}
3977
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978static PyCodeObject *
3979makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003980{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 PyObject *tmp;
3982 PyCodeObject *co = NULL;
3983 PyObject *consts = NULL;
3984 PyObject *names = NULL;
3985 PyObject *varnames = NULL;
3986 PyObject *filename = NULL;
3987 PyObject *name = NULL;
3988 PyObject *freevars = NULL;
3989 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003990 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003992
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003993 tmp = dict_keys_inorder(c->u->u_consts, 0);
3994 if (!tmp)
3995 goto error;
3996 consts = PySequence_List(tmp); /* optimize_code requires a list */
3997 Py_DECREF(tmp);
3998
3999 names = dict_keys_inorder(c->u->u_names, 0);
4000 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4001 if (!consts || !names || !varnames)
4002 goto error;
4003
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004004 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4005 if (!cellvars)
4006 goto error;
4007 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4008 if (!freevars)
4009 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004010 filename = PyString_FromString(c->c_filename);
4011 if (!filename)
4012 goto error;
4013
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004014 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 flags = compute_code_flags(c);
4016 if (flags < 0)
4017 goto error;
4018
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004019 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004020 if (!bytecode)
4021 goto error;
4022
4023 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4024 if (!tmp)
4025 goto error;
4026 Py_DECREF(consts);
4027 consts = tmp;
4028
Guido van Rossum4f72a782006-10-27 23:31:49 +00004029 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4030 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031 bytecode, consts, names, varnames,
4032 freevars, cellvars,
4033 filename, c->u->u_name,
4034 c->u->u_firstlineno,
4035 a->a_lnotab);
4036 error:
4037 Py_XDECREF(consts);
4038 Py_XDECREF(names);
4039 Py_XDECREF(varnames);
4040 Py_XDECREF(filename);
4041 Py_XDECREF(name);
4042 Py_XDECREF(freevars);
4043 Py_XDECREF(cellvars);
4044 Py_XDECREF(bytecode);
4045 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004046}
4047
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004048
4049/* For debugging purposes only */
4050#if 0
4051static void
4052dump_instr(const struct instr *i)
4053{
4054 const char *jrel = i->i_jrel ? "jrel " : "";
4055 const char *jabs = i->i_jabs ? "jabs " : "";
4056 char arg[128];
4057
4058 *arg = '\0';
4059 if (i->i_hasarg)
4060 sprintf(arg, "arg: %d ", i->i_oparg);
4061
4062 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4063 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4064}
4065
4066static void
4067dump_basicblock(const basicblock *b)
4068{
4069 const char *seen = b->b_seen ? "seen " : "";
4070 const char *b_return = b->b_return ? "return " : "";
4071 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4072 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4073 if (b->b_instr) {
4074 int i;
4075 for (i = 0; i < b->b_iused; i++) {
4076 fprintf(stderr, " [%02d] ", i);
4077 dump_instr(b->b_instr + i);
4078 }
4079 }
4080}
4081#endif
4082
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083static PyCodeObject *
4084assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004085{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086 basicblock *b, *entryblock;
4087 struct assembler a;
4088 int i, j, nblocks;
4089 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 /* Make sure every block that falls off the end returns None.
4092 XXX NEXT_BLOCK() isn't quite right, because if the last
4093 block ends with a jump or return b_next shouldn't set.
4094 */
4095 if (!c->u->u_curblock->b_return) {
4096 NEXT_BLOCK(c);
4097 if (addNone)
4098 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4099 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004100 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004102 nblocks = 0;
4103 entryblock = NULL;
4104 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4105 nblocks++;
4106 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004107 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004108
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004109 /* Set firstlineno if it wasn't explicitly set. */
4110 if (!c->u->u_firstlineno) {
4111 if (entryblock && entryblock->b_instr)
4112 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4113 else
4114 c->u->u_firstlineno = 1;
4115 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4117 goto error;
4118 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004121 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004123 /* Emit code in reverse postorder from dfs. */
4124 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004125 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004126 for (j = 0; j < b->b_iused; j++)
4127 if (!assemble_emit(&a, &b->b_instr[j]))
4128 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004129 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004130
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004131 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4132 goto error;
4133 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4134 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004135
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 co = makecode(c, &a);
4137 error:
4138 assemble_free(&a);
4139 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004140}