blob: e1f2a5581c117240c7502c15bb5d1dc771ddb8df [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
Guido van Rossumd8faa362007-04-27 19:54:29 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
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
Guido van Rossum992d4a32007-07-11 13:09:30 +000045#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000048 unsigned i_jabs : 1;
49 unsigned i_jrel : 1;
50 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055};
56
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 /* Each basicblock in a compilation unit is linked via b_list in the
59 reverse order that the block are allocated. b_list points to the next
60 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061 struct basicblock_ *b_list;
62 /* number of instructions used */
63 int b_iused;
64 /* length of instruction array (b_instr) */
65 int b_ialloc;
66 /* pointer to an array of instructions, initially NULL */
67 struct instr *b_instr;
68 /* If b_next is non-NULL, it is a pointer to the next
69 block reached by normal control flow. */
70 struct basicblock_ *b_next;
71 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000072 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000073 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000074 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075 /* depth of stack upon entry of block, computed by stackdepth() */
76 int b_startdepth;
77 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079} basicblock;
80
81/* fblockinfo tracks the current frame block.
82
Jeremy Hyltone9357b22006-03-01 15:47:05 +000083A frame block is used to handle loops, try/except, and try/finally.
84It's called a frame block to distinguish it from a basic block in the
85compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086*/
87
88enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
89
90struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000091 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 basicblock *fb_block;
93};
94
95/* The following items change on entry and exit of code blocks.
96 They must be saved and restored when returning to a block.
97*/
98struct compiler_unit {
99 PySTEntryObject *u_ste;
100
101 PyObject *u_name;
102 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000103 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 the argument for opcodes that refer to those collections.
105 */
106 PyObject *u_consts; /* all constants */
107 PyObject *u_names; /* all names */
108 PyObject *u_varnames; /* local variables */
109 PyObject *u_cellvars; /* cell variables */
110 PyObject *u_freevars; /* free variables */
111
112 PyObject *u_private; /* for private name mangling */
113
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000114 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000115 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 /* Pointer to the most recently allocated block. By following b_list
117 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
122 int u_nfblocks;
123 struct fblockinfo u_fblock[CO_MAXBLOCKS];
124
125 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000126 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000127 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128 has been generated with current lineno */
129};
130
131/* This struct captures the global state of a compilation.
132
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000133The u pointer points to the current compilation unit, while units
134for enclosing blocks are stored in c_stack. The u and c_stack are
135managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136*/
137
138struct compiler {
139 const char *c_filename;
140 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142 PyCompilerFlags *c_flags;
143
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000145 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000150 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151};
152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153static int compiler_enter_scope(struct compiler *, identifier, void *, int);
154static void compiler_free(struct compiler *);
155static basicblock *compiler_new_block(struct compiler *);
156static int compiler_next_instr(struct compiler *, basicblock *);
157static int compiler_addop(struct compiler *, int);
158static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
159static int compiler_addop_i(struct compiler *, int, int);
160static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161static basicblock *compiler_use_new_block(struct compiler *);
162static int compiler_error(struct compiler *, const char *);
163static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
164
165static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
166static int compiler_visit_stmt(struct compiler *, stmt_ty);
167static int compiler_visit_keyword(struct compiler *, keyword_ty);
168static int compiler_visit_expr(struct compiler *, expr_ty);
169static int compiler_augassign(struct compiler *, stmt_ty);
170static int compiler_visit_slice(struct compiler *, slice_ty,
171 expr_context_ty);
172
173static int compiler_push_fblock(struct compiler *, enum fblocktype,
174 basicblock *);
175static void compiler_pop_fblock(struct compiler *, enum fblocktype,
176 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177/* Returns true if there is a loop on the fblock stack. */
178static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179
180static int inplace_binop(struct compiler *, operator_ty);
181static int expr_constant(expr_ty e);
182
Guido van Rossumc2e20742006-02-27 22:32:47 +0000183static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000184static int compiler_call_helper(struct compiler *c, int n,
185 asdl_seq *args,
186 asdl_seq *keywords,
187 expr_ty starargs,
188 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190static PyCodeObject *assemble(struct compiler *, int addNone);
191static PyObject *__doc__;
192
193PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000195{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 /* Name mangling: __private becomes _classname__private.
197 This is independent from how the name is used. */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000198 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
199 Py_UNICODE *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 size_t nlen, plen;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000201 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000202 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000203 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000205 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000206 p = PyUnicode_AS_UNICODE(privateobj);
207 nlen = Py_UNICODE_strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000208 /* Don't mangle __id__ or names with dots.
209
210 The only time a name with a dot can occur is when
211 we are compiling an import statement that has a
212 package name.
213
214 TODO(jhylton): Decide whether we want to support
215 mangling of the module name, e.g. __M.X.
216 */
217 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Martin v. Löwis5b222132007-06-10 09:51:05 +0000218 || Py_UNICODE_strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000219 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 /* Strip leading underscores from class name */
223 while (*p == '_')
224 p++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000225 if (*p == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000226 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000229 plen = Py_UNICODE_strlen(p);
230 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000231 if (!ident)
232 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000234 buffer = PyUnicode_AS_UNICODE(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000235 buffer[0] = '_';
Martin v. Löwis5b222132007-06-10 09:51:05 +0000236 Py_UNICODE_strncpy(buffer+1, p, plen);
237 Py_UNICODE_strcpy(buffer+1+plen, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000239}
240
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241static int
242compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000243{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246 c->c_stack = PyList_New(0);
247 if (!c->c_stack)
248 return 0;
249
250 return 1;
251}
252
253PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000254PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000255 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256{
257 struct compiler c;
258 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000259 PyCompilerFlags local_flags;
260 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000262 if (!__doc__) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000263 __doc__ = PyUnicode_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000264 if (!__doc__)
265 return NULL;
266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
268 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000269 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000271 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 c.c_future = PyFuture_FromAST(mod, filename);
273 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000274 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000276 local_flags.cf_flags = 0;
277 flags = &local_flags;
278 }
279 merged = c.c_future->ff_features | flags->cf_flags;
280 c.c_future->ff_features = merged;
281 flags->cf_flags = merged;
282 c.c_flags = flags;
283 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000284
285 c.c_st = PySymtable_Build(mod, filename, c.c_future);
286 if (c.c_st == NULL) {
287 if (!PyErr_Occurred())
288 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000289 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290 }
291
292 /* XXX initialize to NULL for now, need to handle */
293 c.c_encoding = NULL;
294
295 co = compiler_mod(&c, mod);
296
Thomas Wouters1175c432006-02-27 22:49:54 +0000297 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000299 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300 return co;
301}
302
303PyCodeObject *
304PyNode_Compile(struct _node *n, const char *filename)
305{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000306 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000308 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309 if (!arena)
310 return NULL;
311 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000312 if (mod)
313 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000314 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000315 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000316}
317
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000318static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000320{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000321 if (c->c_st)
322 PySymtable_Free(c->c_st);
323 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000324 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000326}
327
Guido van Rossum79f25d91997-04-29 20:08:16 +0000328static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000330{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000331 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332 PyObject *v, *k;
333 PyObject *dict = PyDict_New();
334 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000335
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336 n = PyList_Size(list);
337 for (i = 0; i < n; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000338 v = PyLong_FromLong(i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 if (!v) {
340 Py_DECREF(dict);
341 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000342 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000343 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000344 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
346 Py_XDECREF(k);
347 Py_DECREF(v);
348 Py_DECREF(dict);
349 return NULL;
350 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000351 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000353 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354 return dict;
355}
356
357/* Return new dict containing names from src that match scope(s).
358
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000359src is a symbol table dictionary. If the scope of a name matches
360either scope_type or flag is set, insert it into the new dict. The
361values are integers, starting at offset and increasing by one for
362each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363*/
364
365static PyObject *
366dictbytype(PyObject *src, int scope_type, int flag, int offset)
367{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000368 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369 PyObject *k, *v, *dest = PyDict_New();
370
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000371 assert(offset >= 0);
372 if (dest == NULL)
373 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
375 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000376 /* XXX this should probably be a macro in symtable.h */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000377 long vi;
Christian Heimes217cfd12007-12-02 14:31:20 +0000378 assert(PyLong_Check(v));
379 vi = PyLong_AS_LONG(v);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000380 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000382 if (scope == scope_type || vi & flag) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000383 PyObject *tuple, *item = PyLong_FromLong(i);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000384 if (item == NULL) {
385 Py_DECREF(dest);
386 return NULL;
387 }
388 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000389 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000390 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
391 Py_DECREF(item);
392 Py_DECREF(dest);
393 Py_XDECREF(tuple);
394 return NULL;
395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000397 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399 }
400 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000401}
402
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403static void
404compiler_unit_check(struct compiler_unit *u)
405{
406 basicblock *block;
407 for (block = u->u_blocks; block != NULL; block = block->b_list) {
408 assert(block != (void *)0xcbcbcbcb);
409 assert(block != (void *)0xfbfbfbfb);
410 assert(block != (void *)0xdbdbdbdb);
411 if (block->b_instr != NULL) {
412 assert(block->b_ialloc > 0);
413 assert(block->b_iused > 0);
414 assert(block->b_ialloc >= block->b_iused);
415 }
416 else {
417 assert (block->b_iused == 0);
418 assert (block->b_ialloc == 0);
419 }
420 }
421}
422
423static void
424compiler_unit_free(struct compiler_unit *u)
425{
426 basicblock *b, *next;
427
428 compiler_unit_check(u);
429 b = u->u_blocks;
430 while (b != NULL) {
431 if (b->b_instr)
432 PyObject_Free((void *)b->b_instr);
433 next = b->b_list;
434 PyObject_Free((void *)b);
435 b = next;
436 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000437 Py_CLEAR(u->u_ste);
438 Py_CLEAR(u->u_name);
439 Py_CLEAR(u->u_consts);
440 Py_CLEAR(u->u_names);
441 Py_CLEAR(u->u_varnames);
442 Py_CLEAR(u->u_freevars);
443 Py_CLEAR(u->u_cellvars);
444 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445 PyObject_Free(u);
446}
447
448static int
449compiler_enter_scope(struct compiler *c, identifier name, void *key,
450 int lineno)
451{
452 struct compiler_unit *u;
453
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000455 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000456 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457 PyErr_NoMemory();
458 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000459 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000460 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000462 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463 u->u_ste = PySymtable_Lookup(c->c_st, key);
464 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000465 compiler_unit_free(u);
466 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 }
468 Py_INCREF(name);
469 u->u_name = name;
470 u->u_varnames = list2dict(u->u_ste->ste_varnames);
471 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000472 if (!u->u_varnames || !u->u_cellvars) {
473 compiler_unit_free(u);
474 return 0;
475 }
476
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000478 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000479 if (!u->u_freevars) {
480 compiler_unit_free(u);
481 return 0;
482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483
484 u->u_blocks = NULL;
485 u->u_tmpname = 0;
486 u->u_nfblocks = 0;
487 u->u_firstlineno = lineno;
488 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000489 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 u->u_consts = PyDict_New();
491 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000492 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return 0;
494 }
495 u->u_names = PyDict_New();
496 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000497 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 return 0;
499 }
500
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000501 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502
503 /* Push the old compiler_unit on the stack. */
504 if (c->u) {
505 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000506 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
507 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000508 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 return 0;
510 }
511 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000512 u->u_private = c->u->u_private;
513 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 }
515 c->u = u;
516
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000517 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000518 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 return 0;
520
521 return 1;
522}
523
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000524static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525compiler_exit_scope(struct compiler *c)
526{
527 int n;
528 PyObject *wrapper;
529
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000530 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 compiler_unit_free(c->u);
532 /* Restore c->u to the parent unit. */
533 n = PyList_GET_SIZE(c->c_stack) - 1;
534 if (n >= 0) {
535 wrapper = PyList_GET_ITEM(c->c_stack, n);
536 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000537 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000538 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000540 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 compiler_unit_check(c->u);
542 }
543 else
544 c->u = NULL;
545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546}
547
Guido van Rossumc2e20742006-02-27 22:32:47 +0000548/* Allocate a new "anonymous" local variable.
549 Used by list comprehensions and with statements.
550*/
551
552static PyObject *
553compiler_new_tmpname(struct compiler *c)
554{
555 char tmpname[256];
556 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000557 return PyUnicode_FromString(tmpname);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000558}
559
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000560/* Allocate a new block and return a pointer to it.
561 Returns NULL on error.
562*/
563
564static basicblock *
565compiler_new_block(struct compiler *c)
566{
567 basicblock *b;
568 struct compiler_unit *u;
569
570 u = c->u;
571 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000572 if (b == NULL) {
573 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000575 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000576 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000577 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 b->b_list = u->u_blocks;
579 u->u_blocks = b;
580 return b;
581}
582
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000583static basicblock *
584compiler_use_new_block(struct compiler *c)
585{
586 basicblock *block = compiler_new_block(c);
587 if (block == NULL)
588 return NULL;
589 c->u->u_curblock = block;
590 return block;
591}
592
593static basicblock *
594compiler_next_block(struct compiler *c)
595{
596 basicblock *block = compiler_new_block(c);
597 if (block == NULL)
598 return NULL;
599 c->u->u_curblock->b_next = block;
600 c->u->u_curblock = block;
601 return block;
602}
603
604static basicblock *
605compiler_use_next_block(struct compiler *c, basicblock *block)
606{
607 assert(block != NULL);
608 c->u->u_curblock->b_next = block;
609 c->u->u_curblock = block;
610 return block;
611}
612
613/* Returns the offset of the next instruction in the current block's
614 b_instr array. Resizes the b_instr as necessary.
615 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000616*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617
618static int
619compiler_next_instr(struct compiler *c, basicblock *b)
620{
621 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000622 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000624 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625 if (b->b_instr == NULL) {
626 PyErr_NoMemory();
627 return -1;
628 }
629 b->b_ialloc = DEFAULT_BLOCK_SIZE;
630 memset((char *)b->b_instr, 0,
631 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635 size_t oldsize, newsize;
636 oldsize = b->b_ialloc * sizeof(struct instr);
637 newsize = oldsize << 1;
638 if (newsize == 0) {
639 PyErr_NoMemory();
640 return -1;
641 }
642 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000643 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000644 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645 if (tmp == NULL) {
646 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648 }
649 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
651 }
652 return b->b_iused++;
653}
654
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655/* Set the i_lineno member of the instruction at offse off if the
656 line number for the current expression/statement (?) has not
657 already been set. If it has been set, the call has no effect.
658
659 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000660*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000661
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662static void
663compiler_set_lineno(struct compiler *c, int off)
664{
665 basicblock *b;
666 if (c->u->u_lineno_set)
667 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000668 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000670 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000671}
672
673static int
674opcode_stack_effect(int opcode, int oparg)
675{
676 switch (opcode) {
677 case POP_TOP:
678 return -1;
679 case ROT_TWO:
680 case ROT_THREE:
681 return 0;
682 case DUP_TOP:
683 return 1;
684 case ROT_FOUR:
685 return 0;
686
687 case UNARY_POSITIVE:
688 case UNARY_NEGATIVE:
689 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 case UNARY_INVERT:
691 return 0;
692
Nick Coghlan650f0d02007-04-15 12:05:43 +0000693 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000694 case LIST_APPEND:
695 return -2;
696
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000697 case BINARY_POWER:
698 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699 case BINARY_MODULO:
700 case BINARY_ADD:
701 case BINARY_SUBTRACT:
702 case BINARY_SUBSCR:
703 case BINARY_FLOOR_DIVIDE:
704 case BINARY_TRUE_DIVIDE:
705 return -1;
706 case INPLACE_FLOOR_DIVIDE:
707 case INPLACE_TRUE_DIVIDE:
708 return -1;
709
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710 case INPLACE_ADD:
711 case INPLACE_SUBTRACT:
712 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 case INPLACE_MODULO:
714 return -1;
715 case STORE_SUBSCR:
716 return -3;
Christian Heimes99170a52007-12-19 02:07:34 +0000717 case STORE_MAP:
718 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719 case DELETE_SUBSCR:
720 return -2;
721
722 case BINARY_LSHIFT:
723 case BINARY_RSHIFT:
724 case BINARY_AND:
725 case BINARY_XOR:
726 case BINARY_OR:
727 return -1;
728 case INPLACE_POWER:
729 return -1;
730 case GET_ITER:
731 return 0;
732
733 case PRINT_EXPR:
734 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000735 case LOAD_BUILD_CLASS:
736 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737 case INPLACE_LSHIFT:
738 case INPLACE_RSHIFT:
739 case INPLACE_AND:
740 case INPLACE_XOR:
741 case INPLACE_OR:
742 return -1;
743 case BREAK_LOOP:
744 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000745 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000746 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000747 case STORE_LOCALS:
748 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000749 case RETURN_VALUE:
750 return -1;
751 case IMPORT_STAR:
752 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000753 case YIELD_VALUE:
754 return 0;
755
756 case POP_BLOCK:
757 return 0;
758 case END_FINALLY:
759 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000760
761 case STORE_NAME:
762 return -1;
763 case DELETE_NAME:
764 return 0;
765 case UNPACK_SEQUENCE:
766 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000767 case UNPACK_EX:
768 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000769 case FOR_ITER:
770 return 1;
771
772 case STORE_ATTR:
773 return -2;
774 case DELETE_ATTR:
775 return -1;
776 case STORE_GLOBAL:
777 return -1;
778 case DELETE_GLOBAL:
779 return 0;
780 case DUP_TOPX:
781 return oparg;
782 case LOAD_CONST:
783 return 1;
784 case LOAD_NAME:
785 return 1;
786 case BUILD_TUPLE:
787 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000788 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000789 return 1-oparg;
790 case BUILD_MAP:
791 return 1;
792 case LOAD_ATTR:
793 return 0;
794 case COMPARE_OP:
795 return -1;
796 case IMPORT_NAME:
797 return 0;
798 case IMPORT_FROM:
799 return 1;
800
801 case JUMP_FORWARD:
802 case JUMP_IF_FALSE:
803 case JUMP_IF_TRUE:
804 case JUMP_ABSOLUTE:
805 return 0;
806
807 case LOAD_GLOBAL:
808 return 1;
809
810 case CONTINUE_LOOP:
811 return 0;
812 case SETUP_LOOP:
813 return 0;
814 case SETUP_EXCEPT:
815 case SETUP_FINALLY:
816 return 3; /* actually pushed by an exception */
817
818 case LOAD_FAST:
819 return 1;
820 case STORE_FAST:
821 return -1;
822 case DELETE_FAST:
823 return 0;
824
825 case RAISE_VARARGS:
826 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000827#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828 case CALL_FUNCTION:
829 return -NARGS(oparg);
830 case CALL_FUNCTION_VAR:
831 case CALL_FUNCTION_KW:
832 return -NARGS(oparg)-1;
833 case CALL_FUNCTION_VAR_KW:
834 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000836 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000837 case MAKE_CLOSURE:
838 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000839#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840 case BUILD_SLICE:
841 if (oparg == 3)
842 return -2;
843 else
844 return -1;
845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846 case LOAD_CLOSURE:
847 return 1;
848 case LOAD_DEREF:
849 return 1;
850 case STORE_DEREF:
851 return -1;
852 default:
853 fprintf(stderr, "opcode = %d\n", opcode);
854 Py_FatalError("opcode_stack_effect()");
855
856 }
857 return 0; /* not reachable */
858}
859
860/* Add an opcode with no argument.
861 Returns 0 on failure, 1 on success.
862*/
863
864static int
865compiler_addop(struct compiler *c, int opcode)
866{
867 basicblock *b;
868 struct instr *i;
869 int off;
870 off = compiler_next_instr(c, c->u->u_curblock);
871 if (off < 0)
872 return 0;
873 b = c->u->u_curblock;
874 i = &b->b_instr[off];
875 i->i_opcode = opcode;
876 i->i_hasarg = 0;
877 if (opcode == RETURN_VALUE)
878 b->b_return = 1;
879 compiler_set_lineno(c, off);
880 return 1;
881}
882
883static int
884compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
885{
886 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000887 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000888
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000889 /* necessary to make sure types aren't coerced (e.g., int and long) */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000890 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
891 if (PyFloat_Check(o)) {
892 double d = PyFloat_AS_DOUBLE(o);
893 unsigned char* p = (unsigned char*) &d;
894 /* all we need is to make the tuple different in either the 0.0
895 * or -0.0 case from all others, just to avoid the "coercion".
896 */
897 if (*p==0 && p[sizeof(double)-1]==0)
898 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
899 else
900 t = PyTuple_Pack(2, o, o->ob_type);
901 } else {
902 t = PyTuple_Pack(2, o, o->ob_type);
903 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000904 if (t == NULL)
905 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906
907 v = PyDict_GetItem(dict, t);
908 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000909 if (PyErr_Occurred())
910 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000911 arg = PyDict_Size(dict);
Christian Heimes217cfd12007-12-02 14:31:20 +0000912 v = PyLong_FromLong(arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913 if (!v) {
914 Py_DECREF(t);
915 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000916 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917 if (PyDict_SetItem(dict, t, v) < 0) {
918 Py_DECREF(t);
919 Py_DECREF(v);
920 return -1;
921 }
922 Py_DECREF(v);
923 }
924 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000925 arg = PyLong_AsLong(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000927 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000928}
929
930static int
931compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
932 PyObject *o)
933{
934 int arg = compiler_add_o(c, dict, o);
935 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000936 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 return compiler_addop_i(c, opcode, arg);
938}
939
940static int
941compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000942 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943{
944 int arg;
945 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
946 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000947 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000948 arg = compiler_add_o(c, dict, mangled);
949 Py_DECREF(mangled);
950 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000951 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952 return compiler_addop_i(c, opcode, arg);
953}
954
955/* Add an opcode with an integer argument.
956 Returns 0 on failure, 1 on success.
957*/
958
959static int
960compiler_addop_i(struct compiler *c, int opcode, int oparg)
961{
962 struct instr *i;
963 int off;
964 off = compiler_next_instr(c, c->u->u_curblock);
965 if (off < 0)
966 return 0;
967 i = &c->u->u_curblock->b_instr[off];
968 i->i_opcode = opcode;
969 i->i_oparg = oparg;
970 i->i_hasarg = 1;
971 compiler_set_lineno(c, off);
972 return 1;
973}
974
975static int
976compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
977{
978 struct instr *i;
979 int off;
980
981 assert(b != NULL);
982 off = compiler_next_instr(c, c->u->u_curblock);
983 if (off < 0)
984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 i = &c->u->u_curblock->b_instr[off];
986 i->i_opcode = opcode;
987 i->i_target = b;
988 i->i_hasarg = 1;
989 if (absolute)
990 i->i_jabs = 1;
991 else
992 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000993 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994 return 1;
995}
996
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000997/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
998 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999 it as the current block. NEXT_BLOCK() also creates an implicit jump
1000 from the current block to the new block.
1001*/
1002
Thomas Wouters89f507f2006-12-13 04:49:30 +00001003/* The returns inside these macros make it impossible to decref objects
1004 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005*/
1006
1007
1008#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001009 if (compiler_use_new_block((C)) == NULL) \
1010 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011}
1012
1013#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001014 if (compiler_next_block((C)) == NULL) \
1015 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016}
1017
1018#define ADDOP(C, OP) { \
1019 if (!compiler_addop((C), (OP))) \
1020 return 0; \
1021}
1022
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001023#define ADDOP_IN_SCOPE(C, OP) { \
1024 if (!compiler_addop((C), (OP))) { \
1025 compiler_exit_scope(c); \
1026 return 0; \
1027 } \
1028}
1029
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030#define ADDOP_O(C, OP, O, TYPE) { \
1031 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1032 return 0; \
1033}
1034
1035#define ADDOP_NAME(C, OP, O, TYPE) { \
1036 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1037 return 0; \
1038}
1039
1040#define ADDOP_I(C, OP, O) { \
1041 if (!compiler_addop_i((C), (OP), (O))) \
1042 return 0; \
1043}
1044
1045#define ADDOP_JABS(C, OP, O) { \
1046 if (!compiler_addop_j((C), (OP), (O), 1)) \
1047 return 0; \
1048}
1049
1050#define ADDOP_JREL(C, OP, O) { \
1051 if (!compiler_addop_j((C), (OP), (O), 0)) \
1052 return 0; \
1053}
1054
1055/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1056 the ASDL name to synthesize the name of the C type and the visit function.
1057*/
1058
1059#define VISIT(C, TYPE, V) {\
1060 if (!compiler_visit_ ## TYPE((C), (V))) \
1061 return 0; \
1062}
1063
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001064#define VISIT_IN_SCOPE(C, TYPE, V) {\
1065 if (!compiler_visit_ ## TYPE((C), (V))) { \
1066 compiler_exit_scope(c); \
1067 return 0; \
1068 } \
1069}
1070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071#define VISIT_SLICE(C, V, CTX) {\
1072 if (!compiler_visit_slice((C), (V), (CTX))) \
1073 return 0; \
1074}
1075
1076#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001077 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001079 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001080 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 if (!compiler_visit_ ## TYPE((C), elt)) \
1082 return 0; \
1083 } \
1084}
1085
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001086#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001087 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001088 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001089 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001090 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001091 if (!compiler_visit_ ## TYPE((C), elt)) { \
1092 compiler_exit_scope(c); \
1093 return 0; \
1094 } \
1095 } \
1096}
1097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098static int
1099compiler_isdocstring(stmt_ty s)
1100{
1101 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001102 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103 return s->v.Expr.value->kind == Str_kind;
1104}
1105
1106/* Compile a sequence of statements, checking for a docstring. */
1107
1108static int
1109compiler_body(struct compiler *c, asdl_seq *stmts)
1110{
1111 int i = 0;
1112 stmt_ty st;
1113
1114 if (!asdl_seq_LEN(stmts))
1115 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001116 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001117 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1118 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119 i = 1;
1120 VISIT(c, expr, st->v.Expr.value);
1121 if (!compiler_nameop(c, __doc__, Store))
1122 return 0;
1123 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001124 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 return 1;
1127}
1128
1129static PyCodeObject *
1130compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001131{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001132 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001133 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001134 static PyObject *module;
1135 if (!module) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001136 module = PyUnicode_FromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 if (!module)
1138 return NULL;
1139 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001140 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1141 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001142 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143 switch (mod->kind) {
1144 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001145 if (!compiler_body(c, mod->v.Module.body)) {
1146 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001148 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001149 break;
1150 case Interactive_kind:
1151 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001152 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001153 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 break;
1155 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001156 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001157 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001158 break;
1159 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001160 PyErr_SetString(PyExc_SystemError,
1161 "suite should not be possible");
1162 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001163 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001164 PyErr_Format(PyExc_SystemError,
1165 "module kind %d should not be possible",
1166 mod->kind);
1167 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001168 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169 co = assemble(c, addNone);
1170 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001171 return co;
1172}
1173
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174/* The test for LOCAL must come before the test for FREE in order to
1175 handle classes where name is both local and free. The local var is
1176 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001177*/
1178
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179static int
1180get_ref_type(struct compiler *c, PyObject *name)
1181{
1182 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001183 if (scope == 0) {
1184 char buf[350];
1185 PyOS_snprintf(buf, sizeof(buf),
1186 "unknown scope for %.100s in %.100s(%s) in %s\n"
1187 "symbols: %s\nlocals: %s\nglobals: %s\n",
1188 PyString_AS_STRING(name),
1189 PyString_AS_STRING(c->u->u_name),
1190 PyObject_REPR(c->u->u_ste->ste_id),
1191 c->c_filename,
1192 PyObject_REPR(c->u->u_ste->ste_symbols),
1193 PyObject_REPR(c->u->u_varnames),
1194 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001196 Py_FatalError(buf);
1197 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001198
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001199 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200}
1201
1202static int
1203compiler_lookup_arg(PyObject *dict, PyObject *name)
1204{
1205 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001206 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001208 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001209 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001210 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001211 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001212 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001213 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214}
1215
1216static int
1217compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1218{
1219 int i, free = PyCode_GetNumFree(co);
1220 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001221 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1222 ADDOP_I(c, MAKE_FUNCTION, args);
1223 return 1;
1224 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225 for (i = 0; i < free; ++i) {
1226 /* Bypass com_addop_varname because it will generate
1227 LOAD_DEREF but LOAD_CLOSURE is needed.
1228 */
1229 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1230 int arg, reftype;
1231
1232 /* Special case: If a class contains a method with a
1233 free variable that has the same name as a method,
1234 the name will be considered free *and* local in the
1235 class. It should be handled by the closure, as
1236 well as by the normal name loookup logic.
1237 */
1238 reftype = get_ref_type(c, name);
1239 if (reftype == CELL)
1240 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1241 else /* (reftype == FREE) */
1242 arg = compiler_lookup_arg(c->u->u_freevars, name);
1243 if (arg == -1) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001244 fprintf(stderr,
1245 "lookup %s in %s %d %d\n"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246 "freevars of %s: %s\n",
1247 PyObject_REPR(name),
1248 PyString_AS_STRING(c->u->u_name),
1249 reftype, arg,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00001250 PyUnicode_AsString(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 PyObject_REPR(co->co_freevars));
1252 Py_FatalError("compiler_make_closure()");
1253 }
1254 ADDOP_I(c, LOAD_CLOSURE, arg);
1255 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001256 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001258 ADDOP_I(c, MAKE_CLOSURE, args);
1259 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001260}
1261
1262static int
1263compiler_decorators(struct compiler *c, asdl_seq* decos)
1264{
1265 int i;
1266
1267 if (!decos)
1268 return 1;
1269
1270 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 }
1273 return 1;
1274}
1275
1276static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001277compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1278 asdl_seq *kw_defaults)
1279{
1280 int i, default_count = 0;
1281 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001282 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001283 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1284 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001285 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001286 if (!compiler_visit_expr(c, default_)) {
1287 return -1;
1288 }
1289 default_count++;
1290 }
1291 }
1292 return default_count;
1293}
1294
1295static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001296compiler_visit_argannotation(struct compiler *c, identifier id,
1297 expr_ty annotation, PyObject *names)
1298{
1299 if (annotation) {
1300 VISIT(c, expr, annotation);
1301 if (PyList_Append(names, id))
1302 return -1;
1303 }
1304 return 0;
1305}
1306
1307static int
1308compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1309 PyObject *names)
1310{
1311 int i, error;
1312 for (i = 0; i < asdl_seq_LEN(args); i++) {
1313 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001314 error = compiler_visit_argannotation(
1315 c,
1316 arg->arg,
1317 arg->annotation,
1318 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001319 if (error)
1320 return error;
1321 }
1322 return 0;
1323}
1324
1325static int
1326compiler_visit_annotations(struct compiler *c, arguments_ty args,
1327 expr_ty returns)
1328{
Guido van Rossum0240b922007-02-26 21:23:50 +00001329 /* Push arg annotations and a list of the argument names. Return the #
1330 of items pushed. The expressions are evaluated out-of-order wrt the
1331 source code.
1332
1333 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1334 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001335 static identifier return_str;
1336 PyObject *names;
1337 int len;
1338 names = PyList_New(0);
1339 if (!names)
1340 return -1;
1341
1342 if (compiler_visit_argannotations(c, args->args, names))
1343 goto error;
1344 if (args->varargannotation &&
1345 compiler_visit_argannotation(c, args->vararg,
1346 args->varargannotation, names))
1347 goto error;
1348 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1349 goto error;
1350 if (args->kwargannotation &&
1351 compiler_visit_argannotation(c, args->kwarg,
1352 args->kwargannotation, names))
1353 goto error;
1354
1355 if (!return_str) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001356 return_str = PyUnicode_InternFromString("return");
Neal Norwitzc1505362006-12-28 06:47:50 +00001357 if (!return_str)
1358 goto error;
1359 }
1360 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1361 goto error;
1362 }
1363
1364 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001365 if (len > 65534) {
1366 /* len must fit in 16 bits, and len is incremented below */
1367 PyErr_SetString(PyExc_SyntaxError,
1368 "too many annotations");
1369 goto error;
1370 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001371 if (len) {
1372 /* convert names to a tuple and place on stack */
1373 PyObject *elt;
1374 int i;
1375 PyObject *s = PyTuple_New(len);
1376 if (!s)
1377 goto error;
1378 for (i = 0; i < len; i++) {
1379 elt = PyList_GET_ITEM(names, i);
1380 Py_INCREF(elt);
1381 PyTuple_SET_ITEM(s, i, elt);
1382 }
1383 ADDOP_O(c, LOAD_CONST, s, consts);
1384 Py_DECREF(s);
1385 len++; /* include the just-pushed tuple */
1386 }
1387 Py_DECREF(names);
1388 return len;
1389
1390error:
1391 Py_DECREF(names);
1392 return -1;
1393}
1394
1395static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001396compiler_function(struct compiler *c, stmt_ty s)
1397{
1398 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001399 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001400 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001401 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001402 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001403 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001404 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001405 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406
1407 assert(s->kind == FunctionDef_kind);
1408
1409 if (!compiler_decorators(c, decos))
1410 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001411 if (args->kwonlyargs) {
1412 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1413 args->kw_defaults);
1414 if (res < 0)
1415 return 0;
1416 kw_default_count = res;
1417 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 if (args->defaults)
1419 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001420 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001421 if (num_annotations < 0)
1422 return 0;
1423 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1426 s->lineno))
1427 return 0;
1428
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001429 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001430 docstring = compiler_isdocstring(st);
Thomas Woutersce272b62007-09-19 21:19:28 +00001431 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001432 first_const = st->v.Expr.value->v.Str.s;
1433 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001434 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001435 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001436 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001439 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001441 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001443 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1444 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 }
1446 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001447 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 if (co == NULL)
1449 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Guido van Rossum4f72a782006-10-27 23:31:49 +00001451 arglength = asdl_seq_LEN(args->defaults);
1452 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001454 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001455 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456
Neal Norwitzc1505362006-12-28 06:47:50 +00001457 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1459 ADDOP_I(c, CALL_FUNCTION, 1);
1460 }
1461
1462 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1463}
1464
1465static int
1466compiler_class(struct compiler *c, stmt_ty s)
1467{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001468 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001470 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001471 PySTEntryObject *ste;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001472 int err, i;
1473 asdl_seq* decos = s->v.ClassDef.decorator_list;
1474
1475 if (!compiler_decorators(c, decos))
1476 return 0;
1477
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001478 /* initialize statics */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001479 if (locals == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001480 locals = PyUnicode_FromString("__locals__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001481 if (locals == NULL)
1482 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001485 /* ultimately generate code for:
1486 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1487 where:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001488 <func> is a function/closure created from the class body;
1489 it has a single argument (__locals__) where the dict
1490 (or MutableSequence) representing the locals is passed
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001491 <name> is the class name
1492 <bases> is the positional arguments and *varargs argument
1493 <keywords> is the keyword arguments and **kwds argument
1494 This borrows from compiler_call.
1495 */
1496
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001497 /* 0. Create a fake argument named __locals__ */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001498 ste = PySymtable_Lookup(c->c_st, s);
1499 if (ste == NULL)
1500 return 0;
1501 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001502 err = PyList_Append(ste->ste_varnames, locals);
1503 Py_DECREF(ste);
1504 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001505 return 0;
1506
1507 /* 1. compile the class body into a code object */
1508 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1509 return 0;
1510 /* this block represents what we do in the new scope */
1511 {
1512 /* use the class name for name mangling */
1513 Py_INCREF(s->v.ClassDef.name);
1514 c->u->u_private = s->v.ClassDef.name;
1515 /* force it to have one mandatory argument */
1516 c->u->u_argcount = 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001517 /* load the first argument (__locals__) ... */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001518 ADDOP_I(c, LOAD_FAST, 0);
1519 /* ... and store it into f_locals */
1520 ADDOP_IN_SCOPE(c, STORE_LOCALS);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001521 /* load (global) __name__ ... */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001522 str = PyUnicode_InternFromString("__name__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001523 if (!str || !compiler_nameop(c, str, Load)) {
1524 Py_XDECREF(str);
1525 compiler_exit_scope(c);
1526 return 0;
1527 }
1528 Py_DECREF(str);
1529 /* ... and store it as __module__ */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001530 str = PyUnicode_InternFromString("__module__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001531 if (!str || !compiler_nameop(c, str, Store)) {
1532 Py_XDECREF(str);
1533 compiler_exit_scope(c);
1534 return 0;
1535 }
1536 Py_DECREF(str);
1537 /* compile the body proper */
1538 if (!compiler_body(c, s->v.ClassDef.body)) {
1539 compiler_exit_scope(c);
1540 return 0;
1541 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001542 /* return the (empty) __class__ cell */
1543 str = PyUnicode_InternFromString("__class__");
1544 if (str == NULL) {
1545 compiler_exit_scope(c);
1546 return 0;
1547 }
1548 i = compiler_lookup_arg(c->u->u_cellvars, str);
1549 Py_DECREF(str);
1550 if (i == -1) {
1551 /* This happens when nobody references the cell */
1552 PyErr_Clear();
1553 /* Return None */
1554 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1555 }
1556 else {
1557 /* Return the cell where to store __class__ */
1558 ADDOP_I(c, LOAD_CLOSURE, i);
1559 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001560 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1561 /* create the code object */
1562 co = assemble(c, 1);
1563 }
1564 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001565 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001566 if (co == NULL)
1567 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001569 /* 2. load the 'build_class' function */
1570 ADDOP(c, LOAD_BUILD_CLASS);
1571
1572 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001573 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001574 Py_DECREF(co);
1575
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001576 /* 4. load class name */
1577 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1578
1579 /* 5. generate the rest of the code for the call */
1580 if (!compiler_call_helper(c, 2,
1581 s->v.ClassDef.bases,
1582 s->v.ClassDef.keywords,
1583 s->v.ClassDef.starargs,
1584 s->v.ClassDef.kwargs))
1585 return 0;
1586
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001587 /* 6. apply decorators */
1588 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1589 ADDOP_I(c, CALL_FUNCTION, 1);
1590 }
1591
1592 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1594 return 0;
1595 return 1;
1596}
1597
1598static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001599compiler_ifexp(struct compiler *c, expr_ty e)
1600{
1601 basicblock *end, *next;
1602
1603 assert(e->kind == IfExp_kind);
1604 end = compiler_new_block(c);
1605 if (end == NULL)
1606 return 0;
1607 next = compiler_new_block(c);
1608 if (next == NULL)
1609 return 0;
1610 VISIT(c, expr, e->v.IfExp.test);
1611 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1612 ADDOP(c, POP_TOP);
1613 VISIT(c, expr, e->v.IfExp.body);
1614 ADDOP_JREL(c, JUMP_FORWARD, end);
1615 compiler_use_next_block(c, next);
1616 ADDOP(c, POP_TOP);
1617 VISIT(c, expr, e->v.IfExp.orelse);
1618 compiler_use_next_block(c, end);
1619 return 1;
1620}
1621
1622static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623compiler_lambda(struct compiler *c, expr_ty e)
1624{
1625 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001626 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001627 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001628 arguments_ty args = e->v.Lambda.args;
1629 assert(e->kind == Lambda_kind);
1630
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001631 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001632 name = PyUnicode_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001633 if (!name)
1634 return 0;
1635 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
Guido van Rossum4f72a782006-10-27 23:31:49 +00001637 if (args->kwonlyargs) {
1638 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1639 args->kw_defaults);
1640 if (res < 0) return 0;
1641 kw_default_count = res;
1642 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001643 if (args->defaults)
1644 VISIT_SEQ(c, expr, args->defaults);
1645 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1646 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001647
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001649 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001650 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1651 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001652 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001653 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654 if (co == NULL)
1655 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656
Guido van Rossum4f72a782006-10-27 23:31:49 +00001657 arglength = asdl_seq_LEN(args->defaults);
1658 arglength |= kw_default_count << 8;
1659 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001660 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661
1662 return 1;
1663}
1664
1665static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666compiler_if(struct compiler *c, stmt_ty s)
1667{
1668 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001669 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670 assert(s->kind == If_kind);
1671 end = compiler_new_block(c);
1672 if (end == NULL)
1673 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001674 next = compiler_new_block(c);
1675 if (next == NULL)
1676 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001677
1678 constant = expr_constant(s->v.If.test);
1679 /* constant = 0: "if 0"
1680 * constant = 1: "if 1", "if 2", ...
1681 * constant = -1: rest */
1682 if (constant == 0) {
1683 if (s->v.If.orelse)
1684 VISIT_SEQ(c, stmt, s->v.If.orelse);
1685 } else if (constant == 1) {
1686 VISIT_SEQ(c, stmt, s->v.If.body);
1687 } else {
1688 VISIT(c, expr, s->v.If.test);
1689 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1690 ADDOP(c, POP_TOP);
1691 VISIT_SEQ(c, stmt, s->v.If.body);
1692 ADDOP_JREL(c, JUMP_FORWARD, end);
1693 compiler_use_next_block(c, next);
1694 ADDOP(c, POP_TOP);
1695 if (s->v.If.orelse)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001696 VISIT_SEQ(c, stmt, s->v.If.orelse);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001697 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 compiler_use_next_block(c, end);
1699 return 1;
1700}
1701
1702static int
1703compiler_for(struct compiler *c, stmt_ty s)
1704{
1705 basicblock *start, *cleanup, *end;
1706
1707 start = compiler_new_block(c);
1708 cleanup = compiler_new_block(c);
1709 end = compiler_new_block(c);
1710 if (start == NULL || end == NULL || cleanup == NULL)
1711 return 0;
1712 ADDOP_JREL(c, SETUP_LOOP, end);
1713 if (!compiler_push_fblock(c, LOOP, start))
1714 return 0;
1715 VISIT(c, expr, s->v.For.iter);
1716 ADDOP(c, GET_ITER);
1717 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001718 /* XXX(nnorwitz): is there a better way to handle this?
1719 for loops are special, we want to be able to trace them
1720 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001721 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001722 ADDOP_JREL(c, FOR_ITER, cleanup);
1723 VISIT(c, expr, s->v.For.target);
1724 VISIT_SEQ(c, stmt, s->v.For.body);
1725 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1726 compiler_use_next_block(c, cleanup);
1727 ADDOP(c, POP_BLOCK);
1728 compiler_pop_fblock(c, LOOP, start);
1729 VISIT_SEQ(c, stmt, s->v.For.orelse);
1730 compiler_use_next_block(c, end);
1731 return 1;
1732}
1733
1734static int
1735compiler_while(struct compiler *c, stmt_ty s)
1736{
1737 basicblock *loop, *orelse, *end, *anchor = NULL;
1738 int constant = expr_constant(s->v.While.test);
1739
1740 if (constant == 0)
1741 return 1;
1742 loop = compiler_new_block(c);
1743 end = compiler_new_block(c);
1744 if (constant == -1) {
1745 anchor = compiler_new_block(c);
1746 if (anchor == NULL)
1747 return 0;
1748 }
1749 if (loop == NULL || end == NULL)
1750 return 0;
1751 if (s->v.While.orelse) {
1752 orelse = compiler_new_block(c);
1753 if (orelse == NULL)
1754 return 0;
1755 }
1756 else
1757 orelse = NULL;
1758
1759 ADDOP_JREL(c, SETUP_LOOP, end);
1760 compiler_use_next_block(c, loop);
1761 if (!compiler_push_fblock(c, LOOP, loop))
1762 return 0;
1763 if (constant == -1) {
1764 VISIT(c, expr, s->v.While.test);
1765 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1766 ADDOP(c, POP_TOP);
1767 }
1768 VISIT_SEQ(c, stmt, s->v.While.body);
1769 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1770
1771 /* XXX should the two POP instructions be in a separate block
1772 if there is no else clause ?
1773 */
1774
1775 if (constant == -1) {
1776 compiler_use_next_block(c, anchor);
1777 ADDOP(c, POP_TOP);
1778 ADDOP(c, POP_BLOCK);
1779 }
1780 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001781 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782 VISIT_SEQ(c, stmt, s->v.While.orelse);
1783 compiler_use_next_block(c, end);
1784
1785 return 1;
1786}
1787
1788static int
1789compiler_continue(struct compiler *c)
1790{
1791 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001792 static const char IN_FINALLY_ERROR_MSG[] =
1793 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794 int i;
1795
1796 if (!c->u->u_nfblocks)
1797 return compiler_error(c, LOOP_ERROR_MSG);
1798 i = c->u->u_nfblocks - 1;
1799 switch (c->u->u_fblock[i].fb_type) {
1800 case LOOP:
1801 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1802 break;
1803 case EXCEPT:
1804 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001805 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1806 /* Prevent continue anywhere under a finally
1807 even if hidden in a sub-try or except. */
1808 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1809 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1810 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 if (i == -1)
1812 return compiler_error(c, LOOP_ERROR_MSG);
1813 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1814 break;
1815 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001816 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817 }
1818
1819 return 1;
1820}
1821
1822/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1823
1824 SETUP_FINALLY L
1825 <code for body>
1826 POP_BLOCK
1827 LOAD_CONST <None>
1828 L: <code for finalbody>
1829 END_FINALLY
1830
1831 The special instructions use the block stack. Each block
1832 stack entry contains the instruction that created it (here
1833 SETUP_FINALLY), the level of the value stack at the time the
1834 block stack entry was created, and a label (here L).
1835
1836 SETUP_FINALLY:
1837 Pushes the current value stack level and the label
1838 onto the block stack.
1839 POP_BLOCK:
1840 Pops en entry from the block stack, and pops the value
1841 stack until its level is the same as indicated on the
1842 block stack. (The label is ignored.)
1843 END_FINALLY:
1844 Pops a variable number of entries from the *value* stack
1845 and re-raises the exception they specify. The number of
1846 entries popped depends on the (pseudo) exception type.
1847
1848 The block stack is unwound when an exception is raised:
1849 when a SETUP_FINALLY entry is found, the exception is pushed
1850 onto the value stack (and the exception condition is cleared),
1851 and the interpreter jumps to the label gotten from the block
1852 stack.
1853*/
1854
1855static int
1856compiler_try_finally(struct compiler *c, stmt_ty s)
1857{
1858 basicblock *body, *end;
1859 body = compiler_new_block(c);
1860 end = compiler_new_block(c);
1861 if (body == NULL || end == NULL)
1862 return 0;
1863
1864 ADDOP_JREL(c, SETUP_FINALLY, end);
1865 compiler_use_next_block(c, body);
1866 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1867 return 0;
1868 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1869 ADDOP(c, POP_BLOCK);
1870 compiler_pop_fblock(c, FINALLY_TRY, body);
1871
1872 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1873 compiler_use_next_block(c, end);
1874 if (!compiler_push_fblock(c, FINALLY_END, end))
1875 return 0;
1876 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1877 ADDOP(c, END_FINALLY);
1878 compiler_pop_fblock(c, FINALLY_END, end);
1879
1880 return 1;
1881}
1882
1883/*
1884 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1885 (The contents of the value stack is shown in [], with the top
1886 at the right; 'tb' is trace-back info, 'val' the exception's
1887 associated value, and 'exc' the exception.)
1888
1889 Value stack Label Instruction Argument
1890 [] SETUP_EXCEPT L1
1891 [] <code for S>
1892 [] POP_BLOCK
1893 [] JUMP_FORWARD L0
1894
1895 [tb, val, exc] L1: DUP )
1896 [tb, val, exc, exc] <evaluate E1> )
1897 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1898 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1899 [tb, val, exc, 1] POP )
1900 [tb, val, exc] POP
1901 [tb, val] <assign to V1> (or POP if no V1)
1902 [tb] POP
1903 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001904 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905
1906 [tb, val, exc, 0] L2: POP
1907 [tb, val, exc] DUP
1908 .............................etc.......................
1909
1910 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001911 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
1913 [] L0: <next statement>
1914
1915 Of course, parts are not generated if Vi or Ei is not present.
1916*/
1917static int
1918compiler_try_except(struct compiler *c, stmt_ty s)
1919{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001920 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 int i, n;
1922
1923 body = compiler_new_block(c);
1924 except = compiler_new_block(c);
1925 orelse = compiler_new_block(c);
1926 end = compiler_new_block(c);
1927 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1928 return 0;
1929 ADDOP_JREL(c, SETUP_EXCEPT, except);
1930 compiler_use_next_block(c, body);
1931 if (!compiler_push_fblock(c, EXCEPT, body))
1932 return 0;
1933 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1934 ADDOP(c, POP_BLOCK);
1935 compiler_pop_fblock(c, EXCEPT, body);
1936 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1937 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1938 compiler_use_next_block(c, except);
1939 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001941 s->v.TryExcept.handlers, i);
1942 if (!handler->type && i < n-1)
1943 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001944 c->u->u_lineno_set = 0;
1945 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 except = compiler_new_block(c);
1947 if (except == NULL)
1948 return 0;
1949 if (handler->type) {
1950 ADDOP(c, DUP_TOP);
1951 VISIT(c, expr, handler->type);
1952 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1953 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1954 ADDOP(c, POP_TOP);
1955 }
1956 ADDOP(c, POP_TOP);
1957 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001958 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001959
1960 cleanup_end = compiler_new_block(c);
1961 cleanup_body = compiler_new_block(c);
1962 if(!(cleanup_end || cleanup_body))
1963 return 0;
1964
Guido van Rossum16be03e2007-01-10 18:51:35 +00001965 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001966 ADDOP(c, POP_TOP);
1967
1968 /*
1969 try:
1970 # body
1971 except type as name:
1972 try:
1973 # body
1974 finally:
1975 name = None
1976 del name
1977 */
1978
1979 /* second try: */
1980 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1981 compiler_use_next_block(c, cleanup_body);
1982 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1983 return 0;
1984
1985 /* second # body */
1986 VISIT_SEQ(c, stmt, handler->body);
1987 ADDOP(c, POP_BLOCK);
1988 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
1989
1990 /* finally: */
1991 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1992 compiler_use_next_block(c, cleanup_end);
1993 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
1994 return 0;
1995
1996 /* name = None */
1997 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00001998 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001999
Guido van Rossum16be03e2007-01-10 18:51:35 +00002000 /* del name */
2001 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002002
2003 ADDOP(c, END_FINALLY);
2004 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002005 }
2006 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002007 ADDOP(c, POP_TOP);
2008 ADDOP(c, POP_TOP);
2009 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002010 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011 ADDOP_JREL(c, JUMP_FORWARD, end);
2012 compiler_use_next_block(c, except);
2013 if (handler->type)
2014 ADDOP(c, POP_TOP);
2015 }
2016 ADDOP(c, END_FINALLY);
2017 compiler_use_next_block(c, orelse);
2018 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2019 compiler_use_next_block(c, end);
2020 return 1;
2021}
2022
2023static int
2024compiler_import_as(struct compiler *c, identifier name, identifier asname)
2025{
2026 /* The IMPORT_NAME opcode was already generated. This function
2027 merely needs to bind the result to a name.
2028
2029 If there is a dot in name, we need to split it and emit a
2030 LOAD_ATTR for each name.
2031 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002032 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2033 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002034 if (dot) {
2035 /* Consume the base module name to get the first attribute */
2036 src = dot + 1;
2037 while (dot) {
2038 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002039 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002040 dot = Py_UNICODE_strchr(src, '.');
2041 attr = PyUnicode_FromUnicode(src,
2042 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002043 if (!attr)
2044 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002045 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002046 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 src = dot + 1;
2048 }
2049 }
2050 return compiler_nameop(c, asname, Store);
2051}
2052
2053static int
2054compiler_import(struct compiler *c, stmt_ty s)
2055{
2056 /* The Import node stores a module name like a.b.c as a single
2057 string. This is convenient for all cases except
2058 import a.b.c as d
2059 where we need to parse that string to extract the individual
2060 module names.
2061 XXX Perhaps change the representation to make this case simpler?
2062 */
2063 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002065 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002066 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002067 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002068 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002069
Christian Heimes217cfd12007-12-02 14:31:20 +00002070 level = PyLong_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002071 if (level == NULL)
2072 return 0;
2073
2074 ADDOP_O(c, LOAD_CONST, level, consts);
2075 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2077 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2078
2079 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002080 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002081 if (!r)
2082 return r;
2083 }
2084 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002086 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2087 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002089 tmp = PyUnicode_FromUnicode(base,
2090 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 r = compiler_nameop(c, tmp, Store);
2092 if (dot) {
2093 Py_DECREF(tmp);
2094 }
2095 if (!r)
2096 return r;
2097 }
2098 }
2099 return 1;
2100}
2101
2102static int
2103compiler_from_import(struct compiler *c, stmt_ty s)
2104{
2105 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106
2107 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002108 PyObject *level;
2109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002110 if (!names)
2111 return 0;
2112
Christian Heimes217cfd12007-12-02 14:31:20 +00002113 level = PyLong_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002114 if (!level) {
2115 Py_DECREF(names);
2116 return 0;
2117 }
2118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 /* build up the names */
2120 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002121 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122 Py_INCREF(alias->name);
2123 PyTuple_SET_ITEM(names, i, alias->name);
2124 }
2125
2126 if (s->lineno > c->c_future->ff_lineno) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002127 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2128 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002129 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 Py_DECREF(names);
2131 return compiler_error(c,
2132 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002133 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
2135 }
2136 }
2137
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002138 ADDOP_O(c, LOAD_CONST, level, consts);
2139 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002141 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002142 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2143 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002144 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145 identifier store_name;
2146
Martin v. Löwis5b222132007-06-10 09:51:05 +00002147 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148 assert(n == 1);
2149 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002150 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002151 }
2152
2153 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2154 store_name = alias->name;
2155 if (alias->asname)
2156 store_name = alias->asname;
2157
2158 if (!compiler_nameop(c, store_name, Store)) {
2159 Py_DECREF(names);
2160 return 0;
2161 }
2162 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002163 /* remove imported module */
2164 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 return 1;
2166}
2167
2168static int
2169compiler_assert(struct compiler *c, stmt_ty s)
2170{
2171 static PyObject *assertion_error = NULL;
2172 basicblock *end;
2173
2174 if (Py_OptimizeFlag)
2175 return 1;
2176 if (assertion_error == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002177 assertion_error = PyUnicode_FromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 if (assertion_error == NULL)
2179 return 0;
2180 }
2181 VISIT(c, expr, s->v.Assert.test);
2182 end = compiler_new_block(c);
2183 if (end == NULL)
2184 return 0;
2185 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2186 ADDOP(c, POP_TOP);
2187 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2188 if (s->v.Assert.msg) {
2189 VISIT(c, expr, s->v.Assert.msg);
Collin Winter828f04a2007-08-31 00:04:24 +00002190 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 }
Collin Winter828f04a2007-08-31 00:04:24 +00002192 ADDOP_I(c, RAISE_VARARGS, 1);
Neal Norwitz51abbc72005-12-18 07:06:23 +00002193 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194 ADDOP(c, POP_TOP);
2195 return 1;
2196}
2197
2198static int
2199compiler_visit_stmt(struct compiler *c, stmt_ty s)
2200{
2201 int i, n;
2202
Thomas Wouters89f507f2006-12-13 04:49:30 +00002203 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002205 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002206
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002208 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002210 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002212 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213 if (c->u->u_ste->ste_type != FunctionBlock)
2214 return compiler_error(c, "'return' outside function");
2215 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216 VISIT(c, expr, s->v.Return.value);
2217 }
2218 else
2219 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2220 ADDOP(c, RETURN_VALUE);
2221 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002222 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 VISIT_SEQ(c, expr, s->v.Delete.targets)
2224 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002225 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 n = asdl_seq_LEN(s->v.Assign.targets);
2227 VISIT(c, expr, s->v.Assign.value);
2228 for (i = 0; i < n; i++) {
2229 if (i < n - 1)
2230 ADDOP(c, DUP_TOP);
2231 VISIT(c, expr,
2232 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2233 }
2234 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002235 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002237 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002239 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002241 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002242 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002243 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 n = 0;
Collin Winter828f04a2007-08-31 00:04:24 +00002245 if (s->v.Raise.exc) {
2246 VISIT(c, expr, s->v.Raise.exc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 n++;
Collin Winter828f04a2007-08-31 00:04:24 +00002248 if (s->v.Raise.cause) {
2249 VISIT(c, expr, s->v.Raise.cause);
2250 n++;
2251 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 }
2253 ADDOP_I(c, RAISE_VARARGS, n);
2254 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002255 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002257 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002258 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002259 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002261 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002263 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002265 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002266 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002268 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002270 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 ADDOP(c, PRINT_EXPR);
2272 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002273 else if (s->v.Expr.value->kind != Str_kind &&
2274 s->v.Expr.value->kind != Num_kind) {
2275 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 ADDOP(c, POP_TOP);
2277 }
2278 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002279 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002281 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002282 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 return compiler_error(c, "'break' outside loop");
2284 ADDOP(c, BREAK_LOOP);
2285 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002286 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002288 case With_kind:
2289 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 }
2291 return 1;
2292}
2293
2294static int
2295unaryop(unaryop_ty op)
2296{
2297 switch (op) {
2298 case Invert:
2299 return UNARY_INVERT;
2300 case Not:
2301 return UNARY_NOT;
2302 case UAdd:
2303 return UNARY_POSITIVE;
2304 case USub:
2305 return UNARY_NEGATIVE;
2306 }
2307 return 0;
2308}
2309
2310static int
2311binop(struct compiler *c, operator_ty op)
2312{
2313 switch (op) {
2314 case Add:
2315 return BINARY_ADD;
2316 case Sub:
2317 return BINARY_SUBTRACT;
2318 case Mult:
2319 return BINARY_MULTIPLY;
2320 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002321 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 case Mod:
2323 return BINARY_MODULO;
2324 case Pow:
2325 return BINARY_POWER;
2326 case LShift:
2327 return BINARY_LSHIFT;
2328 case RShift:
2329 return BINARY_RSHIFT;
2330 case BitOr:
2331 return BINARY_OR;
2332 case BitXor:
2333 return BINARY_XOR;
2334 case BitAnd:
2335 return BINARY_AND;
2336 case FloorDiv:
2337 return BINARY_FLOOR_DIVIDE;
2338 }
2339 return 0;
2340}
2341
2342static int
2343cmpop(cmpop_ty op)
2344{
2345 switch (op) {
2346 case Eq:
2347 return PyCmp_EQ;
2348 case NotEq:
2349 return PyCmp_NE;
2350 case Lt:
2351 return PyCmp_LT;
2352 case LtE:
2353 return PyCmp_LE;
2354 case Gt:
2355 return PyCmp_GT;
2356 case GtE:
2357 return PyCmp_GE;
2358 case Is:
2359 return PyCmp_IS;
2360 case IsNot:
2361 return PyCmp_IS_NOT;
2362 case In:
2363 return PyCmp_IN;
2364 case NotIn:
2365 return PyCmp_NOT_IN;
2366 }
2367 return PyCmp_BAD;
2368}
2369
2370static int
2371inplace_binop(struct compiler *c, operator_ty op)
2372{
2373 switch (op) {
2374 case Add:
2375 return INPLACE_ADD;
2376 case Sub:
2377 return INPLACE_SUBTRACT;
2378 case Mult:
2379 return INPLACE_MULTIPLY;
2380 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002381 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002382 case Mod:
2383 return INPLACE_MODULO;
2384 case Pow:
2385 return INPLACE_POWER;
2386 case LShift:
2387 return INPLACE_LSHIFT;
2388 case RShift:
2389 return INPLACE_RSHIFT;
2390 case BitOr:
2391 return INPLACE_OR;
2392 case BitXor:
2393 return INPLACE_XOR;
2394 case BitAnd:
2395 return INPLACE_AND;
2396 case FloorDiv:
2397 return INPLACE_FLOOR_DIVIDE;
2398 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002399 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002400 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 return 0;
2402}
2403
2404static int
2405compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2406{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002407 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002408 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2409
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002410 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002411 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 /* XXX AugStore isn't used anywhere! */
2413
2414 /* First check for assignment to __debug__. Param? */
2415 if ((ctx == Store || ctx == AugStore || ctx == Del)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002416 && !PyUnicode_CompareWithASCIIString(name, "__debug__")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002417 return compiler_error(c, "can not assign to __debug__");
2418 }
2419
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002420 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002421 if (!mangled)
2422 return 0;
2423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002424 op = 0;
2425 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002426 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 switch (scope) {
2428 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002429 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 optype = OP_DEREF;
2431 break;
2432 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002433 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002434 optype = OP_DEREF;
2435 break;
2436 case LOCAL:
2437 if (c->u->u_ste->ste_type == FunctionBlock)
2438 optype = OP_FAST;
2439 break;
2440 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002441 if (c->u->u_ste->ste_type == FunctionBlock &&
2442 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443 optype = OP_GLOBAL;
2444 break;
2445 case GLOBAL_EXPLICIT:
2446 optype = OP_GLOBAL;
2447 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002448 default:
2449 /* scope can be 0 */
2450 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002451 }
2452
2453 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002454 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455
2456 switch (optype) {
2457 case OP_DEREF:
2458 switch (ctx) {
2459 case Load: op = LOAD_DEREF; break;
2460 case Store: op = STORE_DEREF; break;
2461 case AugLoad:
2462 case AugStore:
2463 break;
2464 case Del:
2465 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002466 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002467 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002468 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002469 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002472 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002473 PyErr_SetString(PyExc_SystemError,
2474 "param invalid for deref variable");
2475 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 }
2477 break;
2478 case OP_FAST:
2479 switch (ctx) {
2480 case Load: op = LOAD_FAST; break;
2481 case Store: op = STORE_FAST; break;
2482 case Del: op = DELETE_FAST; break;
2483 case AugLoad:
2484 case AugStore:
2485 break;
2486 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002487 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002488 PyErr_SetString(PyExc_SystemError,
2489 "param invalid for local variable");
2490 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002492 ADDOP_O(c, op, mangled, varnames);
2493 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002494 return 1;
2495 case OP_GLOBAL:
2496 switch (ctx) {
2497 case Load: op = LOAD_GLOBAL; break;
2498 case Store: op = STORE_GLOBAL; break;
2499 case Del: op = DELETE_GLOBAL; break;
2500 case AugLoad:
2501 case AugStore:
2502 break;
2503 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002504 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002505 PyErr_SetString(PyExc_SystemError,
2506 "param invalid for global variable");
2507 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002508 }
2509 break;
2510 case OP_NAME:
2511 switch (ctx) {
2512 case Load: op = LOAD_NAME; break;
2513 case Store: op = STORE_NAME; break;
2514 case Del: op = DELETE_NAME; break;
2515 case AugLoad:
2516 case AugStore:
2517 break;
2518 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002519 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002520 PyErr_SetString(PyExc_SystemError,
2521 "param invalid for name variable");
2522 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523 }
2524 break;
2525 }
2526
2527 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002528 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002529 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002530 if (arg < 0)
2531 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002532 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533}
2534
2535static int
2536compiler_boolop(struct compiler *c, expr_ty e)
2537{
2538 basicblock *end;
2539 int jumpi, i, n;
2540 asdl_seq *s;
2541
2542 assert(e->kind == BoolOp_kind);
2543 if (e->v.BoolOp.op == And)
2544 jumpi = JUMP_IF_FALSE;
2545 else
2546 jumpi = JUMP_IF_TRUE;
2547 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002548 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 return 0;
2550 s = e->v.BoolOp.values;
2551 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002552 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002554 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002555 ADDOP_JREL(c, jumpi, end);
2556 ADDOP(c, POP_TOP)
2557 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002558 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002559 compiler_use_next_block(c, end);
2560 return 1;
2561}
2562
2563static int
2564compiler_list(struct compiler *c, expr_ty e)
2565{
2566 int n = asdl_seq_LEN(e->v.List.elts);
2567 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002568 int i, seen_star = 0;
2569 for (i = 0; i < n; i++) {
2570 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2571 if (elt->kind == Starred_kind && !seen_star) {
2572 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2573 seen_star = 1;
2574 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2575 } else if (elt->kind == Starred_kind) {
2576 return compiler_error(c,
2577 "two starred expressions in assignment");
2578 }
2579 }
2580 if (!seen_star) {
2581 ADDOP_I(c, UNPACK_SEQUENCE, n);
2582 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002583 }
2584 VISIT_SEQ(c, expr, e->v.List.elts);
2585 if (e->v.List.ctx == Load) {
2586 ADDOP_I(c, BUILD_LIST, n);
2587 }
2588 return 1;
2589}
2590
2591static int
2592compiler_tuple(struct compiler *c, expr_ty e)
2593{
2594 int n = asdl_seq_LEN(e->v.Tuple.elts);
2595 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002596 int i, seen_star = 0;
2597 for (i = 0; i < n; i++) {
2598 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2599 if (elt->kind == Starred_kind && !seen_star) {
2600 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2601 seen_star = 1;
2602 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2603 } else if (elt->kind == Starred_kind) {
2604 return compiler_error(c,
2605 "two starred expressions in assignment");
2606 }
2607 }
2608 if (!seen_star) {
2609 ADDOP_I(c, UNPACK_SEQUENCE, n);
2610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 }
2612 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2613 if (e->v.Tuple.ctx == Load) {
2614 ADDOP_I(c, BUILD_TUPLE, n);
2615 }
2616 return 1;
2617}
2618
2619static int
2620compiler_compare(struct compiler *c, expr_ty e)
2621{
2622 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002623 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624
2625 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2626 VISIT(c, expr, e->v.Compare.left);
2627 n = asdl_seq_LEN(e->v.Compare.ops);
2628 assert(n > 0);
2629 if (n > 1) {
2630 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002631 if (cleanup == NULL)
2632 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002633 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002634 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002635 }
2636 for (i = 1; i < n; i++) {
2637 ADDOP(c, DUP_TOP);
2638 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002640 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002641 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2643 NEXT_BLOCK(c);
2644 ADDOP(c, POP_TOP);
2645 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002646 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002647 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002648 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002649 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002650 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002651 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 if (n > 1) {
2653 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002654 if (end == NULL)
2655 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002656 ADDOP_JREL(c, JUMP_FORWARD, end);
2657 compiler_use_next_block(c, cleanup);
2658 ADDOP(c, ROT_TWO);
2659 ADDOP(c, POP_TOP);
2660 compiler_use_next_block(c, end);
2661 }
2662 return 1;
2663}
2664
2665static int
2666compiler_call(struct compiler *c, expr_ty e)
2667{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002668 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002669 return compiler_call_helper(c, 0,
2670 e->v.Call.args,
2671 e->v.Call.keywords,
2672 e->v.Call.starargs,
2673 e->v.Call.kwargs);
2674}
2675
2676/* shared code between compiler_call and compiler_class */
2677static int
2678compiler_call_helper(struct compiler *c,
2679 int n, /* Args already pushed */
2680 asdl_seq *args,
2681 asdl_seq *keywords,
2682 expr_ty starargs,
2683 expr_ty kwargs)
2684{
2685 int code = 0;
2686
2687 n += asdl_seq_LEN(args);
2688 VISIT_SEQ(c, expr, args);
2689 if (keywords) {
2690 VISIT_SEQ(c, keyword, keywords);
2691 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002693 if (starargs) {
2694 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 code |= 1;
2696 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002697 if (kwargs) {
2698 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 code |= 2;
2700 }
2701 switch (code) {
2702 case 0:
2703 ADDOP_I(c, CALL_FUNCTION, n);
2704 break;
2705 case 1:
2706 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2707 break;
2708 case 2:
2709 ADDOP_I(c, CALL_FUNCTION_KW, n);
2710 break;
2711 case 3:
2712 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2713 break;
2714 }
2715 return 1;
2716}
2717
Nick Coghlan650f0d02007-04-15 12:05:43 +00002718
2719/* List and set comprehensions and generator expressions work by creating a
2720 nested function to perform the actual iteration. This means that the
2721 iteration variables don't leak into the current scope.
2722 The defined function is called immediately following its definition, with the
2723 result of that call being the result of the expression.
2724 The LC/SC version returns the populated container, while the GE version is
2725 flagged in symtable.c as a generator, so it returns the generator object
2726 when the function is called.
2727 This code *knows* that the loop cannot contain break, continue, or return,
2728 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2729
2730 Possible cleanups:
2731 - iterate over the generator sequence instead of using recursion
2732*/
2733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002735compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2736 asdl_seq *generators, int gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002737 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738{
2739 /* generate code for the iterator, then each of the ifs,
2740 and then write to the element */
2741
Nick Coghlan650f0d02007-04-15 12:05:43 +00002742 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002744 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745
2746 start = compiler_new_block(c);
2747 skip = compiler_new_block(c);
2748 if_cleanup = compiler_new_block(c);
2749 anchor = compiler_new_block(c);
2750
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002751 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002752 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754
Nick Coghlan650f0d02007-04-15 12:05:43 +00002755 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757 if (gen_index == 0) {
2758 /* Receive outermost iter as an implicit argument */
2759 c->u->u_argcount = 1;
2760 ADDOP_I(c, LOAD_FAST, 0);
2761 }
2762 else {
2763 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002764 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 ADDOP(c, GET_ITER);
2766 }
2767 compiler_use_next_block(c, start);
2768 ADDOP_JREL(c, FOR_ITER, anchor);
2769 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002770 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002771
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002772 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002773 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002775 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 VISIT(c, expr, e);
2777 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2778 NEXT_BLOCK(c);
2779 ADDOP(c, POP_TOP);
2780 }
2781
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002782 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002783 if (!compiler_comprehension_generator(c, tmpname,
2784 generators, gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002785 elt, val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002786 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002787
Nick Coghlan650f0d02007-04-15 12:05:43 +00002788 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002789 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002790 /* comprehension specific code */
2791 switch (type) {
2792 case COMP_GENEXP:
2793 VISIT(c, expr, elt);
2794 ADDOP(c, YIELD_VALUE);
2795 ADDOP(c, POP_TOP);
2796 break;
2797 case COMP_LISTCOMP:
2798 if (!compiler_nameop(c, tmpname, Load))
2799 return 0;
2800 VISIT(c, expr, elt);
2801 ADDOP(c, LIST_APPEND);
2802 break;
2803 case COMP_SETCOMP:
2804 if (!compiler_nameop(c, tmpname, Load))
2805 return 0;
2806 VISIT(c, expr, elt);
2807 ADDOP(c, SET_ADD);
2808 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002809 case COMP_DICTCOMP:
2810 if (!compiler_nameop(c, tmpname, Load))
2811 return 0;
2812 /* With 'd[k] = v', v is evaluated before k, so we do
2813 the same. STORE_SUBSCR requires (item, map, key),
2814 so we still end up ROTing once. */
2815 VISIT(c, expr, val);
2816 ADDOP(c, ROT_TWO);
2817 VISIT(c, expr, elt);
2818 ADDOP(c, STORE_SUBSCR);
2819 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002820 default:
2821 return 0;
2822 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823
2824 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826 for (i = 0; i < n; i++) {
2827 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002828 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 ADDOP(c, POP_TOP);
2832 }
2833 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2834 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835
2836 return 1;
2837}
2838
2839static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002840compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002841 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002842{
2843 PyCodeObject *co = NULL;
2844 identifier tmp = NULL;
2845 expr_ty outermost_iter;
2846
2847 outermost_iter = ((comprehension_ty)
2848 asdl_seq_GET(generators, 0))->iter;
2849
2850 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2851 goto error;
2852
2853 if (type != COMP_GENEXP) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002854 int op;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002855 tmp = compiler_new_tmpname(c);
2856 if (!tmp)
2857 goto error_in_scope;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002858 switch (type) {
2859 case COMP_LISTCOMP:
2860 op = BUILD_LIST;
2861 break;
2862 case COMP_SETCOMP:
2863 op = BUILD_SET;
2864 break;
2865 case COMP_DICTCOMP:
2866 op = BUILD_MAP;
2867 break;
2868 default:
2869 PyErr_Format(PyExc_SystemError,
2870 "unknown comprehension type %d", type);
2871 goto error_in_scope;
2872 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002873
Guido van Rossum992d4a32007-07-11 13:09:30 +00002874 ADDOP_I(c, op, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002875 ADDOP(c, DUP_TOP);
2876 if (!compiler_nameop(c, tmp, Store))
2877 goto error_in_scope;
2878 }
2879
Guido van Rossum992d4a32007-07-11 13:09:30 +00002880 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt,
2881 val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002882 goto error_in_scope;
2883
2884 if (type != COMP_GENEXP) {
2885 ADDOP(c, RETURN_VALUE);
2886 }
2887
2888 co = assemble(c, 1);
2889 compiler_exit_scope(c);
2890 if (co == NULL)
2891 goto error;
2892
2893 if (!compiler_make_closure(c, co, 0))
2894 goto error;
2895 Py_DECREF(co);
2896 Py_XDECREF(tmp);
2897
2898 VISIT(c, expr, outermost_iter);
2899 ADDOP(c, GET_ITER);
2900 ADDOP_I(c, CALL_FUNCTION, 1);
2901 return 1;
2902error_in_scope:
2903 compiler_exit_scope(c);
2904error:
2905 Py_XDECREF(co);
2906 Py_XDECREF(tmp);
2907 return 0;
2908}
2909
2910static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911compiler_genexp(struct compiler *c, expr_ty e)
2912{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002913 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002914 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002915 name = PyUnicode_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002916 if (!name)
2917 return 0;
2918 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002919 assert(e->kind == GeneratorExp_kind);
2920 return compiler_comprehension(c, e, COMP_GENEXP, name,
2921 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002922 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002923}
2924
2925static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002926compiler_listcomp(struct compiler *c, expr_ty e)
2927{
2928 static identifier name;
2929 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002930 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002931 if (!name)
2932 return 0;
2933 }
2934 assert(e->kind == ListComp_kind);
2935 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2936 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002937 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002938}
2939
2940static int
2941compiler_setcomp(struct compiler *c, expr_ty e)
2942{
2943 static identifier name;
2944 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002945 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002946 if (!name)
2947 return 0;
2948 }
2949 assert(e->kind == SetComp_kind);
2950 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2951 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002952 e->v.SetComp.elt, NULL);
2953}
2954
2955
2956static int
2957compiler_dictcomp(struct compiler *c, expr_ty e)
2958{
2959 static identifier name;
2960 if (!name) {
Neal Norwitz41103bf2007-08-24 23:12:06 +00002961 name = PyUnicode_FromString("<dictcomp>");
Guido van Rossum992d4a32007-07-11 13:09:30 +00002962 if (!name)
2963 return 0;
2964 }
2965 assert(e->kind == DictComp_kind);
2966 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2967 e->v.DictComp.generators,
2968 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002969}
2970
2971
2972static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973compiler_visit_keyword(struct compiler *c, keyword_ty k)
2974{
2975 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2976 VISIT(c, expr, k->value);
2977 return 1;
2978}
2979
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002980/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981 whether they are true or false.
2982
2983 Return values: 1 for true, 0 for false, -1 for non-constant.
2984 */
2985
2986static int
2987expr_constant(expr_ty e)
2988{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002989 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002990 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002991 case Ellipsis_kind:
2992 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993 case Num_kind:
2994 return PyObject_IsTrue(e->v.Num.n);
2995 case Str_kind:
2996 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002997 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002998 /* optimize away names that can't be reassigned */
Guido van Rossumbdbb3952007-06-14 00:28:01 +00002999 id = PyString_AS_STRING(
3000 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003001 if (strcmp(id, "True") == 0) return 1;
3002 if (strcmp(id, "False") == 0) return 0;
3003 if (strcmp(id, "None") == 0) return 0;
3004 if (strcmp(id, "__debug__") == 0)
3005 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003006 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003007 default:
3008 return -1;
3009 }
3010}
3011
Guido van Rossumc2e20742006-02-27 22:32:47 +00003012/*
3013 Implements the with statement from PEP 343.
3014
3015 The semantics outlined in that PEP are as follows:
3016
3017 with EXPR as VAR:
3018 BLOCK
3019
3020 It is implemented roughly as:
3021
Thomas Wouters477c8d52006-05-27 19:21:47 +00003022 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003023 exit = context.__exit__ # not calling it
3024 value = context.__enter__()
3025 try:
3026 VAR = value # if VAR present in the syntax
3027 BLOCK
3028 finally:
3029 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003031 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003032 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003033 exit(*exc)
3034 */
3035static int
3036compiler_with(struct compiler *c, stmt_ty s)
3037{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003038 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003039 basicblock *block, *finally;
3040 identifier tmpexit, tmpvalue = NULL;
3041
3042 assert(s->kind == With_kind);
3043
Guido van Rossumc2e20742006-02-27 22:32:47 +00003044 if (!enter_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003045 enter_attr = PyUnicode_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003046 if (!enter_attr)
3047 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003048 }
3049 if (!exit_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003050 exit_attr = PyUnicode_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003051 if (!exit_attr)
3052 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003053 }
3054
3055 block = compiler_new_block(c);
3056 finally = compiler_new_block(c);
3057 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003058 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003059
3060 /* Create a temporary variable to hold context.__exit__ */
3061 tmpexit = compiler_new_tmpname(c);
3062 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003063 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003064 PyArena_AddPyObject(c->c_arena, tmpexit);
3065
3066 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003067 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003068 We need to do this rather than preserving it on the stack
3069 because SETUP_FINALLY remembers the stack level.
3070 We need to do the assignment *inside* the try/finally
3071 so that context.__exit__() is called when the assignment
3072 fails. But we need to call context.__enter__() *before*
3073 the try/finally so that if it fails we won't call
3074 context.__exit__().
3075 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003076 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003077 if (tmpvalue == NULL)
3078 return 0;
3079 PyArena_AddPyObject(c->c_arena, tmpvalue);
3080 }
3081
Thomas Wouters477c8d52006-05-27 19:21:47 +00003082 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003084
3085 /* Squirrel away context.__exit__ */
3086 ADDOP(c, DUP_TOP);
3087 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3088 if (!compiler_nameop(c, tmpexit, Store))
3089 return 0;
3090
3091 /* Call context.__enter__() */
3092 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3093 ADDOP_I(c, CALL_FUNCTION, 0);
3094
3095 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003096 /* Store it in tmpvalue */
3097 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003098 return 0;
3099 }
3100 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003101 /* Discard result from context.__enter__() */
3102 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003103 }
3104
3105 /* Start the try block */
3106 ADDOP_JREL(c, SETUP_FINALLY, finally);
3107
3108 compiler_use_next_block(c, block);
3109 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003110 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003111 }
3112
3113 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003114 /* Bind saved result of context.__enter__() to VAR */
3115 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003116 !compiler_nameop(c, tmpvalue, Del))
3117 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003118 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003119 }
3120
3121 /* BLOCK code */
3122 VISIT_SEQ(c, stmt, s->v.With.body);
3123
3124 /* End of try block; start the finally block */
3125 ADDOP(c, POP_BLOCK);
3126 compiler_pop_fblock(c, FINALLY_TRY, block);
3127
3128 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3129 compiler_use_next_block(c, finally);
3130 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003131 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003132
3133 /* Finally block starts; push tmpexit and issue our magic opcode. */
3134 if (!compiler_nameop(c, tmpexit, Load) ||
3135 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003136 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003137 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003138
3139 /* Finally block ends. */
3140 ADDOP(c, END_FINALLY);
3141 compiler_pop_fblock(c, FINALLY_END, finally);
3142 return 1;
3143}
3144
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145static int
3146compiler_visit_expr(struct compiler *c, expr_ty e)
3147{
3148 int i, n;
3149
Thomas Wouters89f507f2006-12-13 04:49:30 +00003150 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003151 set a new line number for the next instruction.
3152 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003153 if (e->lineno > c->u->u_lineno) {
3154 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003155 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 }
3157 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003158 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003160 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 VISIT(c, expr, e->v.BinOp.left);
3162 VISIT(c, expr, e->v.BinOp.right);
3163 ADDOP(c, binop(c, e->v.BinOp.op));
3164 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003165 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003166 VISIT(c, expr, e->v.UnaryOp.operand);
3167 ADDOP(c, unaryop(e->v.UnaryOp.op));
3168 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003169 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003171 case IfExp_kind:
3172 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003173 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174 n = asdl_seq_LEN(e->v.Dict.values);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003175 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003177 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003178 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003179 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003180 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Christian Heimes99170a52007-12-19 02:07:34 +00003181 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 }
3183 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003184 case Set_kind:
3185 n = asdl_seq_LEN(e->v.Set.elts);
3186 VISIT_SEQ(c, expr, e->v.Set.elts);
3187 ADDOP_I(c, BUILD_SET, n);
3188 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003189 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003190 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003191 case ListComp_kind:
3192 return compiler_listcomp(c, e);
3193 case SetComp_kind:
3194 return compiler_setcomp(c, e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003195 case DictComp_kind:
3196 return compiler_dictcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197 case Yield_kind:
3198 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003199 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003200 if (e->v.Yield.value) {
3201 VISIT(c, expr, e->v.Yield.value);
3202 }
3203 else {
3204 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3205 }
3206 ADDOP(c, YIELD_VALUE);
3207 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003208 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003210 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003212 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003213 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3214 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003215 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3217 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003218 case Bytes_kind:
3219 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
Thomas Wouters00e41de2007-02-23 19:56:57 +00003220 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003221 case Ellipsis_kind:
3222 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3223 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003225 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 if (e->v.Attribute.ctx != AugStore)
3227 VISIT(c, expr, e->v.Attribute.value);
3228 switch (e->v.Attribute.ctx) {
3229 case AugLoad:
3230 ADDOP(c, DUP_TOP);
3231 /* Fall through to load */
3232 case Load:
3233 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3234 break;
3235 case AugStore:
3236 ADDOP(c, ROT_TWO);
3237 /* Fall through to save */
3238 case Store:
3239 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3240 break;
3241 case Del:
3242 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3243 break;
3244 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003245 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003246 PyErr_SetString(PyExc_SystemError,
3247 "param invalid in attribute expression");
3248 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003249 }
3250 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003251 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 switch (e->v.Subscript.ctx) {
3253 case AugLoad:
3254 VISIT(c, expr, e->v.Subscript.value);
3255 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3256 break;
3257 case Load:
3258 VISIT(c, expr, e->v.Subscript.value);
3259 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3260 break;
3261 case AugStore:
3262 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3263 break;
3264 case Store:
3265 VISIT(c, expr, e->v.Subscript.value);
3266 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3267 break;
3268 case Del:
3269 VISIT(c, expr, e->v.Subscript.value);
3270 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3271 break;
3272 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003273 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003274 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003275 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003276 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 }
3278 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003279 case Starred_kind:
3280 switch (e->v.Starred.ctx) {
3281 case Store:
3282 /* In all legitimate cases, the Starred node was already replaced
3283 * by compiler_list/compiler_tuple. XXX: is that okay? */
3284 return compiler_error(c,
3285 "starred assignment target must be in a list or tuple");
3286 default:
3287 return compiler_error(c,
3288 "can use starred expression only as assignment target");
3289 }
3290 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003291 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3293 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003294 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003296 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 return compiler_tuple(c, e);
3298 }
3299 return 1;
3300}
3301
3302static int
3303compiler_augassign(struct compiler *c, stmt_ty s)
3304{
3305 expr_ty e = s->v.AugAssign.target;
3306 expr_ty auge;
3307
3308 assert(s->kind == AugAssign_kind);
3309
3310 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003311 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003313 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003314 if (auge == NULL)
3315 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316 VISIT(c, expr, auge);
3317 VISIT(c, expr, s->v.AugAssign.value);
3318 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3319 auge->v.Attribute.ctx = AugStore;
3320 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 break;
3322 case Subscript_kind:
3323 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003324 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003325 if (auge == NULL)
3326 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 VISIT(c, expr, auge);
3328 VISIT(c, expr, s->v.AugAssign.value);
3329 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003330 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003332 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003333 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003334 if (!compiler_nameop(c, e->v.Name.id, Load))
3335 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 VISIT(c, expr, s->v.AugAssign.value);
3337 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3338 return compiler_nameop(c, e->v.Name.id, Store);
3339 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003340 PyErr_Format(PyExc_SystemError,
3341 "invalid node type (%d) for augmented assignment",
3342 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003343 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003344 }
3345 return 1;
3346}
3347
3348static int
3349compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3350{
3351 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003352 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3353 PyErr_SetString(PyExc_SystemError,
3354 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003355 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003356 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357 f = &c->u->u_fblock[c->u->u_nfblocks++];
3358 f->fb_type = t;
3359 f->fb_block = b;
3360 return 1;
3361}
3362
3363static void
3364compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3365{
3366 struct compiler_unit *u = c->u;
3367 assert(u->u_nfblocks > 0);
3368 u->u_nfblocks--;
3369 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3370 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3371}
3372
Thomas Wouters89f507f2006-12-13 04:49:30 +00003373static int
3374compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003375 int i;
3376 struct compiler_unit *u = c->u;
3377 for (i = 0; i < u->u_nfblocks; ++i) {
3378 if (u->u_fblock[i].fb_type == LOOP)
3379 return 1;
3380 }
3381 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003382}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383/* Raises a SyntaxError and returns 0.
3384 If something goes wrong, a different exception may be raised.
3385*/
3386
3387static int
3388compiler_error(struct compiler *c, const char *errstr)
3389{
3390 PyObject *loc;
3391 PyObject *u = NULL, *v = NULL;
3392
3393 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3394 if (!loc) {
3395 Py_INCREF(Py_None);
3396 loc = Py_None;
3397 }
3398 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3399 Py_None, loc);
3400 if (!u)
3401 goto exit;
3402 v = Py_BuildValue("(zO)", errstr, u);
3403 if (!v)
3404 goto exit;
3405 PyErr_SetObject(PyExc_SyntaxError, v);
3406 exit:
3407 Py_DECREF(loc);
3408 Py_XDECREF(u);
3409 Py_XDECREF(v);
3410 return 0;
3411}
3412
3413static int
3414compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003415 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003416{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003417 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003418
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003419 /* XXX this code is duplicated */
3420 switch (ctx) {
3421 case AugLoad: /* fall through to Load */
3422 case Load: op = BINARY_SUBSCR; break;
3423 case AugStore:/* fall through to Store */
3424 case Store: op = STORE_SUBSCR; break;
3425 case Del: op = DELETE_SUBSCR; break;
3426 case Param:
3427 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003428 "invalid %s kind %d in subscript\n",
3429 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003430 return 0;
3431 }
3432 if (ctx == AugLoad) {
3433 ADDOP_I(c, DUP_TOPX, 2);
3434 }
3435 else if (ctx == AugStore) {
3436 ADDOP(c, ROT_THREE);
3437 }
3438 ADDOP(c, op);
3439 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440}
3441
3442static int
3443compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3444{
3445 int n = 2;
3446 assert(s->kind == Slice_kind);
3447
3448 /* only handles the cases where BUILD_SLICE is emitted */
3449 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003450 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003451 }
3452 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003453 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003455
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003457 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 }
3459 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003460 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 }
3462
3463 if (s->v.Slice.step) {
3464 n++;
3465 VISIT(c, expr, s->v.Slice.step);
3466 }
3467 ADDOP_I(c, BUILD_SLICE, n);
3468 return 1;
3469}
3470
3471static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3473 expr_context_ty ctx)
3474{
3475 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 case Slice_kind:
3477 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003478 case Index_kind:
3479 VISIT(c, expr, s->v.Index.value);
3480 break;
3481 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003482 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003483 PyErr_SetString(PyExc_SystemError,
3484 "extended slice invalid in nested slice");
3485 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 }
3487 return 1;
3488}
3489
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490static int
3491compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3492{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003493 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003494 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003495 case Index_kind:
3496 kindname = "index";
3497 if (ctx != AugStore) {
3498 VISIT(c, expr, s->v.Index.value);
3499 }
3500 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003502 kindname = "slice";
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003503 if (ctx != AugStore) {
3504 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003505 return 0;
3506 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003507 break;
3508 case ExtSlice_kind:
3509 kindname = "extended slice";
3510 if (ctx != AugStore) {
3511 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3512 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003513 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003514 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003515 if (!compiler_visit_nested_slice(c, sub, ctx))
3516 return 0;
3517 }
3518 ADDOP_I(c, BUILD_TUPLE, n);
3519 }
3520 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003521 default:
3522 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003523 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003524 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003526 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003527}
3528
Thomas Wouters89f507f2006-12-13 04:49:30 +00003529/* End of the compiler section, beginning of the assembler section */
3530
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531/* do depth-first search of basic block graph, starting with block.
3532 post records the block indices in post-order.
3533
3534 XXX must handle implicit jumps from one block to next
3535*/
3536
Thomas Wouters89f507f2006-12-13 04:49:30 +00003537struct assembler {
3538 PyObject *a_bytecode; /* string containing bytecode */
3539 int a_offset; /* offset into bytecode */
3540 int a_nblocks; /* number of reachable blocks */
3541 basicblock **a_postorder; /* list of blocks in dfs postorder */
3542 PyObject *a_lnotab; /* string containing lnotab */
3543 int a_lnotab_off; /* offset into lnotab */
3544 int a_lineno; /* last lineno of emitted instruction */
3545 int a_lineno_off; /* bytecode offset of last lineno */
3546};
3547
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548static void
3549dfs(struct compiler *c, basicblock *b, struct assembler *a)
3550{
3551 int i;
3552 struct instr *instr = NULL;
3553
3554 if (b->b_seen)
3555 return;
3556 b->b_seen = 1;
3557 if (b->b_next != NULL)
3558 dfs(c, b->b_next, a);
3559 for (i = 0; i < b->b_iused; i++) {
3560 instr = &b->b_instr[i];
3561 if (instr->i_jrel || instr->i_jabs)
3562 dfs(c, instr->i_target, a);
3563 }
3564 a->a_postorder[a->a_nblocks++] = b;
3565}
3566
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003567static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3569{
3570 int i;
3571 struct instr *instr;
3572 if (b->b_seen || b->b_startdepth >= depth)
3573 return maxdepth;
3574 b->b_seen = 1;
3575 b->b_startdepth = depth;
3576 for (i = 0; i < b->b_iused; i++) {
3577 instr = &b->b_instr[i];
3578 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3579 if (depth > maxdepth)
3580 maxdepth = depth;
3581 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3582 if (instr->i_jrel || instr->i_jabs) {
3583 maxdepth = stackdepth_walk(c, instr->i_target,
3584 depth, maxdepth);
3585 if (instr->i_opcode == JUMP_ABSOLUTE ||
3586 instr->i_opcode == JUMP_FORWARD) {
3587 goto out; /* remaining code is dead */
3588 }
3589 }
3590 }
3591 if (b->b_next)
3592 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3593out:
3594 b->b_seen = 0;
3595 return maxdepth;
3596}
3597
3598/* Find the flow path that needs the largest stack. We assume that
3599 * cycles in the flow graph have no net effect on the stack depth.
3600 */
3601static int
3602stackdepth(struct compiler *c)
3603{
3604 basicblock *b, *entryblock;
3605 entryblock = NULL;
3606 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3607 b->b_seen = 0;
3608 b->b_startdepth = INT_MIN;
3609 entryblock = b;
3610 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003611 if (!entryblock)
3612 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 return stackdepth_walk(c, entryblock, 0, 0);
3614}
3615
3616static int
3617assemble_init(struct assembler *a, int nblocks, int firstlineno)
3618{
3619 memset(a, 0, sizeof(struct assembler));
3620 a->a_lineno = firstlineno;
3621 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3622 if (!a->a_bytecode)
3623 return 0;
3624 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3625 if (!a->a_lnotab)
3626 return 0;
3627 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003628 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003629 if (!a->a_postorder) {
3630 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003632 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003633 return 1;
3634}
3635
3636static void
3637assemble_free(struct assembler *a)
3638{
3639 Py_XDECREF(a->a_bytecode);
3640 Py_XDECREF(a->a_lnotab);
3641 if (a->a_postorder)
3642 PyObject_Free(a->a_postorder);
3643}
3644
3645/* Return the size of a basic block in bytes. */
3646
3647static int
3648instrsize(struct instr *instr)
3649{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003650 if (!instr->i_hasarg)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003651 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003652 if (instr->i_oparg > 0xffff)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003653 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3654 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655}
3656
3657static int
3658blocksize(basicblock *b)
3659{
3660 int i;
3661 int size = 0;
3662
3663 for (i = 0; i < b->b_iused; i++)
3664 size += instrsize(&b->b_instr[i]);
3665 return size;
3666}
3667
3668/* All about a_lnotab.
3669
3670c_lnotab is an array of unsigned bytes disguised as a Python string.
3671It is used to map bytecode offsets to source code line #s (when needed
3672for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003673
Tim Peters2a7f3842001-06-09 09:26:21 +00003674The array is conceptually a list of
3675 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003676pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003677
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003678 byte code offset source code line number
3679 0 1
3680 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003681 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003682 350 307
3683 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003684
3685The first trick is that these numbers aren't stored, only the increments
3686from one row to the next (this doesn't really work, but it's a start):
3687
3688 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3689
3690The second trick is that an unsigned byte can't hold negative values, or
3691values larger than 255, so (a) there's a deep assumption that byte code
3692offsets and their corresponding line #s both increase monotonically, and (b)
3693if at least one column jumps by more than 255 from one row to the next, more
3694than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003695from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003696part. A user of c_lnotab desiring to find the source line number
3697corresponding to a bytecode address A should do something like this
3698
3699 lineno = addr = 0
3700 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003701 addr += addr_incr
3702 if addr > A:
3703 return lineno
3704 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003705
3706In order for this to work, when the addr field increments by more than 255,
3707the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003708increment is < 256. So, in the example above, assemble_lnotab (it used
3709to be called com_set_lineno) should not (as was actually done until 2.2)
3710expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003711 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003712*/
3713
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003714static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003716{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 int d_bytecode, d_lineno;
3718 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003719 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720
3721 d_bytecode = a->a_offset - a->a_lineno_off;
3722 d_lineno = i->i_lineno - a->a_lineno;
3723
3724 assert(d_bytecode >= 0);
3725 assert(d_lineno >= 0);
3726
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003727 /* XXX(nnorwitz): is there a better way to handle this?
3728 for loops are special, we want to be able to trace them
3729 each time around, so we need to set an extra line number. */
3730 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003731 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003732
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003734 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735 nbytes = a->a_lnotab_off + 2 * ncodes;
3736 len = PyString_GET_SIZE(a->a_lnotab);
3737 if (nbytes >= len) {
3738 if (len * 2 < nbytes)
3739 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003740 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 len *= 2;
3742 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3743 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003744 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003745 lnotab = (unsigned char *)
3746 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003747 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003748 *lnotab++ = 255;
3749 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003750 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 d_bytecode -= ncodes * 255;
3752 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003753 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754 assert(d_bytecode <= 255);
3755 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003756 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003757 nbytes = a->a_lnotab_off + 2 * ncodes;
3758 len = PyString_GET_SIZE(a->a_lnotab);
3759 if (nbytes >= len) {
3760 if (len * 2 < nbytes)
3761 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003762 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 len *= 2;
3764 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3765 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003766 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003767 lnotab = (unsigned char *)
3768 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003770 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003772 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003774 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003775 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003776 d_lineno -= ncodes * 255;
3777 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003778 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003779
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 len = PyString_GET_SIZE(a->a_lnotab);
3781 if (a->a_lnotab_off + 2 >= len) {
3782 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003783 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003784 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003785 lnotab = (unsigned char *)
3786 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003787
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 a->a_lnotab_off += 2;
3789 if (d_bytecode) {
3790 *lnotab++ = d_bytecode;
3791 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003792 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003793 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 *lnotab++ = 0;
3795 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797 a->a_lineno = i->i_lineno;
3798 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003799 return 1;
3800}
3801
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802/* assemble_emit()
3803 Extend the bytecode with a new instruction.
3804 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003805*/
3806
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003807static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003809{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003810 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003811 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 char *code;
3813
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003814 size = instrsize(i);
3815 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003817 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003818 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003819 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003820 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 if (a->a_offset + size >= len) {
3822 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003823 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003824 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003825 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3826 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003827 if (size == 6) {
3828 assert(i->i_hasarg);
3829 *code++ = (char)EXTENDED_ARG;
3830 *code++ = ext & 0xff;
3831 *code++ = ext >> 8;
3832 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003835 if (i->i_hasarg) {
3836 assert(size == 3 || size == 6);
3837 *code++ = arg & 0xff;
3838 *code++ = arg >> 8;
3839 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003841}
3842
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003843static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003845{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003847 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003848 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 /* Compute the size of each block and fixup jump args.
3851 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003852start:
3853 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003854 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003855 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 bsize = blocksize(b);
3857 b->b_offset = totsize;
3858 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003859 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003860 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3862 bsize = b->b_offset;
3863 for (i = 0; i < b->b_iused; i++) {
3864 struct instr *instr = &b->b_instr[i];
3865 /* Relative jumps are computed relative to
3866 the instruction pointer after fetching
3867 the jump instruction.
3868 */
3869 bsize += instrsize(instr);
3870 if (instr->i_jabs)
3871 instr->i_oparg = instr->i_target->b_offset;
3872 else if (instr->i_jrel) {
3873 int delta = instr->i_target->b_offset - bsize;
3874 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003875 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003876 else
3877 continue;
3878 if (instr->i_oparg > 0xffff)
3879 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003880 }
3881 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003882
3883 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003884 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003885 with a better solution.
3886
3887 In the meantime, should the goto be dropped in favor
3888 of a loop?
3889
3890 The issue is that in the first loop blocksize() is called
3891 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003892 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003893 i_oparg is calculated in the second loop above.
3894
3895 So we loop until we stop seeing new EXTENDED_ARGs.
3896 The only EXTENDED_ARGs that could be popping up are
3897 ones in jump instructions. So this should converge
3898 fairly quickly.
3899 */
3900 if (last_extended_arg_count != extended_arg_count) {
3901 last_extended_arg_count = extended_arg_count;
3902 goto start;
3903 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003904}
3905
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003906static PyObject *
3907dict_keys_inorder(PyObject *dict, int offset)
3908{
3909 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003910 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003911
3912 tuple = PyTuple_New(size);
3913 if (tuple == NULL)
3914 return NULL;
3915 while (PyDict_Next(dict, &pos, &k, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003916 i = PyLong_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003917 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003918 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003919 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003920 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003921 PyTuple_SET_ITEM(tuple, i - offset, k);
3922 }
3923 return tuple;
3924}
3925
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003926static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003928{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929 PySTEntryObject *ste = c->u->u_ste;
3930 int flags = 0, n;
3931 if (ste->ste_type != ModuleBlock)
3932 flags |= CO_NEWLOCALS;
3933 if (ste->ste_type == FunctionBlock) {
3934 if (!ste->ste_unoptimized)
3935 flags |= CO_OPTIMIZED;
3936 if (ste->ste_nested)
3937 flags |= CO_NESTED;
3938 if (ste->ste_generator)
3939 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 if (ste->ste_varargs)
3942 flags |= CO_VARARGS;
3943 if (ste->ste_varkeywords)
3944 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003945 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003946 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003947
3948 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003949 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003950
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951 n = PyDict_Size(c->u->u_freevars);
3952 if (n < 0)
3953 return -1;
3954 if (n == 0) {
3955 n = PyDict_Size(c->u->u_cellvars);
3956 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003957 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958 if (n == 0) {
3959 flags |= CO_NOFREE;
3960 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003961 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003962
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003963 return flags;
3964}
3965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003966static PyCodeObject *
3967makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003968{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003969 PyObject *tmp;
3970 PyCodeObject *co = NULL;
3971 PyObject *consts = NULL;
3972 PyObject *names = NULL;
3973 PyObject *varnames = NULL;
3974 PyObject *filename = NULL;
3975 PyObject *name = NULL;
3976 PyObject *freevars = NULL;
3977 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003978 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003980
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003981 tmp = dict_keys_inorder(c->u->u_consts, 0);
3982 if (!tmp)
3983 goto error;
3984 consts = PySequence_List(tmp); /* optimize_code requires a list */
3985 Py_DECREF(tmp);
3986
3987 names = dict_keys_inorder(c->u->u_names, 0);
3988 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3989 if (!consts || !names || !varnames)
3990 goto error;
3991
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003992 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3993 if (!cellvars)
3994 goto error;
3995 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3996 if (!freevars)
3997 goto error;
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003998 filename = PyUnicode_DecodeFSDefault(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 if (!filename)
4000 goto error;
4001
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004002 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004003 flags = compute_code_flags(c);
4004 if (flags < 0)
4005 goto error;
4006
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004007 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004008 if (!bytecode)
4009 goto error;
4010
4011 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4012 if (!tmp)
4013 goto error;
4014 Py_DECREF(consts);
4015 consts = tmp;
4016
Guido van Rossum4f72a782006-10-27 23:31:49 +00004017 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4018 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 bytecode, consts, names, varnames,
4020 freevars, cellvars,
4021 filename, c->u->u_name,
4022 c->u->u_firstlineno,
4023 a->a_lnotab);
4024 error:
4025 Py_XDECREF(consts);
4026 Py_XDECREF(names);
4027 Py_XDECREF(varnames);
4028 Py_XDECREF(filename);
4029 Py_XDECREF(name);
4030 Py_XDECREF(freevars);
4031 Py_XDECREF(cellvars);
4032 Py_XDECREF(bytecode);
4033 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004034}
4035
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004036
4037/* For debugging purposes only */
4038#if 0
4039static void
4040dump_instr(const struct instr *i)
4041{
4042 const char *jrel = i->i_jrel ? "jrel " : "";
4043 const char *jabs = i->i_jabs ? "jabs " : "";
4044 char arg[128];
4045
4046 *arg = '\0';
4047 if (i->i_hasarg)
4048 sprintf(arg, "arg: %d ", i->i_oparg);
4049
4050 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4051 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4052}
4053
4054static void
4055dump_basicblock(const basicblock *b)
4056{
4057 const char *seen = b->b_seen ? "seen " : "";
4058 const char *b_return = b->b_return ? "return " : "";
4059 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4060 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4061 if (b->b_instr) {
4062 int i;
4063 for (i = 0; i < b->b_iused; i++) {
4064 fprintf(stderr, " [%02d] ", i);
4065 dump_instr(b->b_instr + i);
4066 }
4067 }
4068}
4069#endif
4070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071static PyCodeObject *
4072assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004073{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074 basicblock *b, *entryblock;
4075 struct assembler a;
4076 int i, j, nblocks;
4077 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004078
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079 /* Make sure every block that falls off the end returns None.
4080 XXX NEXT_BLOCK() isn't quite right, because if the last
4081 block ends with a jump or return b_next shouldn't set.
4082 */
4083 if (!c->u->u_curblock->b_return) {
4084 NEXT_BLOCK(c);
4085 if (addNone)
4086 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4087 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004088 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004089
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004090 nblocks = 0;
4091 entryblock = NULL;
4092 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4093 nblocks++;
4094 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004095 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004096
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004097 /* Set firstlineno if it wasn't explicitly set. */
4098 if (!c->u->u_firstlineno) {
4099 if (entryblock && entryblock->b_instr)
4100 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4101 else
4102 c->u->u_firstlineno = 1;
4103 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4105 goto error;
4106 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004108 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004109 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111 /* Emit code in reverse postorder from dfs. */
4112 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004113 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114 for (j = 0; j < b->b_iused; j++)
4115 if (!assemble_emit(&a, &b->b_instr[j]))
4116 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004117 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004119 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4120 goto error;
4121 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4122 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004123
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004124 co = makecode(c, &a);
4125 error:
4126 assemble_free(&a);
4127 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004128}