blob: 0f5fd2dfde25280e0b5600305d67bef1072a9d2f [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
45
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000046struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000047 unsigned i_jabs : 1;
48 unsigned i_jrel : 1;
49 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000060 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000071 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000072 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000073 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000074 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000077 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078} basicblock;
79
80/* fblockinfo tracks the current frame block.
81
Jeremy Hyltone9357b22006-03-01 15:47:05 +000082A frame block is used to handle loops, try/except, and try/finally.
83It's called a frame block to distinguish it from a basic block in the
84compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085*/
86
87enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
88
89struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000090 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091 basicblock *fb_block;
92};
93
94/* The following items change on entry and exit of code blocks.
95 They must be saved and restored when returning to a block.
96*/
97struct compiler_unit {
98 PySTEntryObject *u_ste;
99
100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000102 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103 the argument for opcodes that refer to those collections.
104 */
105 PyObject *u_consts; /* all constants */
106 PyObject *u_names; /* all names */
107 PyObject *u_varnames; /* local variables */
108 PyObject *u_cellvars; /* cell variables */
109 PyObject *u_freevars; /* free variables */
110
111 PyObject *u_private; /* for private name mangling */
112
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000113 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000114 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 /* Pointer to the most recently allocated block. By following b_list
116 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118 basicblock *u_curblock; /* pointer to current block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000119 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
121 int u_nfblocks;
122 struct fblockinfo u_fblock[CO_MAXBLOCKS];
123
124 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000125 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000126 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 has been generated with current lineno */
128};
129
130/* This struct captures the global state of a compilation.
131
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132The u pointer points to the current compilation unit, while units
133for enclosing blocks are stored in c_stack. The u and c_stack are
134managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135*/
136
137struct compiler {
138 const char *c_filename;
139 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141 PyCompilerFlags *c_flags;
142
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000149 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150};
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152static int compiler_enter_scope(struct compiler *, identifier, void *, int);
153static void compiler_free(struct compiler *);
154static basicblock *compiler_new_block(struct compiler *);
155static int compiler_next_instr(struct compiler *, basicblock *);
156static int compiler_addop(struct compiler *, int);
157static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158static int compiler_addop_i(struct compiler *, int, int);
159static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static basicblock *compiler_use_new_block(struct compiler *);
161static int compiler_error(struct compiler *, const char *);
162static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
163
164static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
165static int compiler_visit_stmt(struct compiler *, stmt_ty);
166static int compiler_visit_keyword(struct compiler *, keyword_ty);
167static int compiler_visit_expr(struct compiler *, expr_ty);
168static int compiler_augassign(struct compiler *, stmt_ty);
169static int compiler_visit_slice(struct compiler *, slice_ty,
170 expr_context_ty);
171
172static int compiler_push_fblock(struct compiler *, enum fblocktype,
173 basicblock *);
174static void compiler_pop_fblock(struct compiler *, enum fblocktype,
175 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176/* Returns true if there is a loop on the fblock stack. */
177static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int inplace_binop(struct compiler *, operator_ty);
180static int expr_constant(expr_ty e);
181
Guido van Rossumc2e20742006-02-27 22:32:47 +0000182static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000183static int compiler_call_helper(struct compiler *c, int n,
184 asdl_seq *args,
185 asdl_seq *keywords,
186 expr_ty starargs,
187 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static PyCodeObject *assemble(struct compiler *, int addNone);
190static PyObject *__doc__;
191
192PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000194{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195 /* Name mangling: __private becomes _classname__private.
196 This is independent from how the name is used. */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000197 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
198 Py_UNICODE *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199 size_t nlen, plen;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000200 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000201 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000202 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000204 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000205 p = PyUnicode_AS_UNICODE(privateobj);
206 nlen = Py_UNICODE_strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000207 /* Don't mangle __id__ or names with dots.
208
209 The only time a name with a dot can occur is when
210 we are compiling an import statement that has a
211 package name.
212
213 TODO(jhylton): Decide whether we want to support
214 mangling of the module name, e.g. __M.X.
215 */
216 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Martin v. Löwis5b222132007-06-10 09:51:05 +0000217 || Py_UNICODE_strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000218 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000220 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221 /* Strip leading underscores from class name */
222 while (*p == '_')
223 p++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000224 if (*p == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000225 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000227 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000228 plen = Py_UNICODE_strlen(p);
229 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000230 if (!ident)
231 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000233 buffer = PyUnicode_AS_UNICODE(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000234 buffer[0] = '_';
Martin v. Löwis5b222132007-06-10 09:51:05 +0000235 Py_UNICODE_strncpy(buffer+1, p, plen);
236 Py_UNICODE_strcpy(buffer+1+plen, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000238}
239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240static int
241compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000242{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 c->c_stack = PyList_New(0);
246 if (!c->c_stack)
247 return 0;
248
249 return 1;
250}
251
252PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000253PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000254 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255{
256 struct compiler c;
257 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000258 PyCompilerFlags local_flags;
259 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000261 if (!__doc__) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000262 __doc__ = PyUnicode_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000263 if (!__doc__)
264 return NULL;
265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266
267 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000268 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000270 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271 c.c_future = PyFuture_FromAST(mod, filename);
272 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000273 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000275 local_flags.cf_flags = 0;
276 flags = &local_flags;
277 }
278 merged = c.c_future->ff_features | flags->cf_flags;
279 c.c_future->ff_features = merged;
280 flags->cf_flags = merged;
281 c.c_flags = flags;
282 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283
284 c.c_st = PySymtable_Build(mod, filename, c.c_future);
285 if (c.c_st == NULL) {
286 if (!PyErr_Occurred())
287 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000288 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 }
290
291 /* XXX initialize to NULL for now, need to handle */
292 c.c_encoding = NULL;
293
294 co = compiler_mod(&c, mod);
295
Thomas Wouters1175c432006-02-27 22:49:54 +0000296 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000298 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 return co;
300}
301
302PyCodeObject *
303PyNode_Compile(struct _node *n, const char *filename)
304{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000305 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000307 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308 if (!arena)
309 return NULL;
310 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000311 if (mod)
312 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000313 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000314 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000315}
316
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000317static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 if (c->c_st)
321 PySymtable_Free(c->c_st);
322 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000325}
326
Guido van Rossum79f25d91997-04-29 20:08:16 +0000327static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000328list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000329{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000330 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000331 PyObject *v, *k;
332 PyObject *dict = PyDict_New();
333 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000334
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335 n = PyList_Size(list);
336 for (i = 0; i < n; i++) {
337 v = PyInt_FromLong(i);
338 if (!v) {
339 Py_DECREF(dict);
340 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000341 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000342 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000343 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
345 Py_XDECREF(k);
346 Py_DECREF(v);
347 Py_DECREF(dict);
348 return NULL;
349 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000350 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000352 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 return dict;
354}
355
356/* Return new dict containing names from src that match scope(s).
357
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000358src is a symbol table dictionary. If the scope of a name matches
359either scope_type or flag is set, insert it into the new dict. The
360values are integers, starting at offset and increasing by one for
361each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000362*/
363
364static PyObject *
365dictbytype(PyObject *src, int scope_type, int flag, int offset)
366{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000367 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368 PyObject *k, *v, *dest = PyDict_New();
369
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000370 assert(offset >= 0);
371 if (dest == NULL)
372 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373
374 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000375 /* XXX this should probably be a macro in symtable.h */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000376 long vi;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000377 assert(PyInt_Check(v));
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000378 vi = PyInt_AS_LONG(v);
379 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000381 if (scope == scope_type || vi & flag) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000382 PyObject *tuple, *item = PyInt_FromLong(i);
383 if (item == NULL) {
384 Py_DECREF(dest);
385 return NULL;
386 }
387 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000388 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000389 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
390 Py_DECREF(item);
391 Py_DECREF(dest);
392 Py_XDECREF(tuple);
393 return NULL;
394 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000396 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398 }
399 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000400}
401
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402static void
403compiler_unit_check(struct compiler_unit *u)
404{
405 basicblock *block;
406 for (block = u->u_blocks; block != NULL; block = block->b_list) {
407 assert(block != (void *)0xcbcbcbcb);
408 assert(block != (void *)0xfbfbfbfb);
409 assert(block != (void *)0xdbdbdbdb);
410 if (block->b_instr != NULL) {
411 assert(block->b_ialloc > 0);
412 assert(block->b_iused > 0);
413 assert(block->b_ialloc >= block->b_iused);
414 }
415 else {
416 assert (block->b_iused == 0);
417 assert (block->b_ialloc == 0);
418 }
419 }
420}
421
422static void
423compiler_unit_free(struct compiler_unit *u)
424{
425 basicblock *b, *next;
426
427 compiler_unit_check(u);
428 b = u->u_blocks;
429 while (b != NULL) {
430 if (b->b_instr)
431 PyObject_Free((void *)b->b_instr);
432 next = b->b_list;
433 PyObject_Free((void *)b);
434 b = next;
435 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000436 Py_CLEAR(u->u_ste);
437 Py_CLEAR(u->u_name);
438 Py_CLEAR(u->u_consts);
439 Py_CLEAR(u->u_names);
440 Py_CLEAR(u->u_varnames);
441 Py_CLEAR(u->u_freevars);
442 Py_CLEAR(u->u_cellvars);
443 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 PyObject_Free(u);
445}
446
447static int
448compiler_enter_scope(struct compiler *c, identifier name, void *key,
449 int lineno)
450{
451 struct compiler_unit *u;
452
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000454 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000455 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000456 PyErr_NoMemory();
457 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000458 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000459 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000461 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 u->u_ste = PySymtable_Lookup(c->c_st, key);
463 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000464 compiler_unit_free(u);
465 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466 }
467 Py_INCREF(name);
468 u->u_name = name;
469 u->u_varnames = list2dict(u->u_ste->ste_varnames);
470 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000471 if (!u->u_varnames || !u->u_cellvars) {
472 compiler_unit_free(u);
473 return 0;
474 }
475
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000477 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 if (!u->u_freevars) {
479 compiler_unit_free(u);
480 return 0;
481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482
483 u->u_blocks = NULL;
484 u->u_tmpname = 0;
485 u->u_nfblocks = 0;
486 u->u_firstlineno = lineno;
487 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000488 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489 u->u_consts = PyDict_New();
490 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000491 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492 return 0;
493 }
494 u->u_names = PyDict_New();
495 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000496 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 return 0;
498 }
499
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000500 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000501
502 /* Push the old compiler_unit on the stack. */
503 if (c->u) {
504 PyObject *wrapper = PyCObject_FromVoidPtr(c->u, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000505 if (!wrapper || PyList_Append(c->c_stack, wrapper) < 0) {
506 Py_XDECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000507 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508 return 0;
509 }
510 Py_DECREF(wrapper);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000511 u->u_private = c->u->u_private;
512 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000513 }
514 c->u = u;
515
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000516 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000517 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518 return 0;
519
520 return 1;
521}
522
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000523static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524compiler_exit_scope(struct compiler *c)
525{
526 int n;
527 PyObject *wrapper;
528
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000529 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530 compiler_unit_free(c->u);
531 /* Restore c->u to the parent unit. */
532 n = PyList_GET_SIZE(c->c_stack) - 1;
533 if (n >= 0) {
534 wrapper = PyList_GET_ITEM(c->c_stack, n);
535 c->u = (struct compiler_unit *)PyCObject_AsVoidPtr(wrapper);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000536 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000537 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000539 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000540 compiler_unit_check(c->u);
541 }
542 else
543 c->u = NULL;
544
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545}
546
Guido van Rossumc2e20742006-02-27 22:32:47 +0000547/* Allocate a new "anonymous" local variable.
548 Used by list comprehensions and with statements.
549*/
550
551static PyObject *
552compiler_new_tmpname(struct compiler *c)
553{
554 char tmpname[256];
555 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000556 return PyUnicode_FromString(tmpname);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000557}
558
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559/* Allocate a new block and return a pointer to it.
560 Returns NULL on error.
561*/
562
563static basicblock *
564compiler_new_block(struct compiler *c)
565{
566 basicblock *b;
567 struct compiler_unit *u;
568
569 u = c->u;
570 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000571 if (b == NULL) {
572 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000574 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000576 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577 b->b_list = u->u_blocks;
578 u->u_blocks = b;
579 return b;
580}
581
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582static basicblock *
583compiler_use_new_block(struct compiler *c)
584{
585 basicblock *block = compiler_new_block(c);
586 if (block == NULL)
587 return NULL;
588 c->u->u_curblock = block;
589 return block;
590}
591
592static basicblock *
593compiler_next_block(struct compiler *c)
594{
595 basicblock *block = compiler_new_block(c);
596 if (block == NULL)
597 return NULL;
598 c->u->u_curblock->b_next = block;
599 c->u->u_curblock = block;
600 return block;
601}
602
603static basicblock *
604compiler_use_next_block(struct compiler *c, basicblock *block)
605{
606 assert(block != NULL);
607 c->u->u_curblock->b_next = block;
608 c->u->u_curblock = block;
609 return block;
610}
611
612/* Returns the offset of the next instruction in the current block's
613 b_instr array. Resizes the b_instr as necessary.
614 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000615*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616
617static int
618compiler_next_instr(struct compiler *c, basicblock *b)
619{
620 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000621 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000622 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000623 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 if (b->b_instr == NULL) {
625 PyErr_NoMemory();
626 return -1;
627 }
628 b->b_ialloc = DEFAULT_BLOCK_SIZE;
629 memset((char *)b->b_instr, 0,
630 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000631 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 size_t oldsize, newsize;
635 oldsize = b->b_ialloc * sizeof(struct instr);
636 newsize = oldsize << 1;
637 if (newsize == 0) {
638 PyErr_NoMemory();
639 return -1;
640 }
641 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000643 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000644 if (tmp == NULL) {
645 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000646 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000647 }
648 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
650 }
651 return b->b_iused++;
652}
653
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654/* Set the i_lineno member of the instruction at offse off if the
655 line number for the current expression/statement (?) has not
656 already been set. If it has been set, the call has no effect.
657
658 Every time a new node is b
Thomas Wouters89f507f2006-12-13 04:49:30 +0000659*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661static void
662compiler_set_lineno(struct compiler *c, int off)
663{
664 basicblock *b;
665 if (c->u->u_lineno_set)
666 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000667 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000669 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670}
671
672static int
673opcode_stack_effect(int opcode, int oparg)
674{
675 switch (opcode) {
676 case POP_TOP:
677 return -1;
678 case ROT_TWO:
679 case ROT_THREE:
680 return 0;
681 case DUP_TOP:
682 return 1;
683 case ROT_FOUR:
684 return 0;
685
686 case UNARY_POSITIVE:
687 case UNARY_NEGATIVE:
688 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 case UNARY_INVERT:
690 return 0;
691
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000693 case LIST_APPEND:
694 return -2;
695
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696 case BINARY_POWER:
697 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 case BINARY_MODULO:
699 case BINARY_ADD:
700 case BINARY_SUBTRACT:
701 case BINARY_SUBSCR:
702 case BINARY_FLOOR_DIVIDE:
703 case BINARY_TRUE_DIVIDE:
704 return -1;
705 case INPLACE_FLOOR_DIVIDE:
706 case INPLACE_TRUE_DIVIDE:
707 return -1;
708
709 case SLICE+0:
710 return 1;
711 case SLICE+1:
712 return 0;
713 case SLICE+2:
714 return 0;
715 case SLICE+3:
716 return -1;
717
718 case STORE_SLICE+0:
719 return -2;
720 case STORE_SLICE+1:
721 return -3;
722 case STORE_SLICE+2:
723 return -3;
724 case STORE_SLICE+3:
725 return -4;
726
727 case DELETE_SLICE+0:
728 return -1;
729 case DELETE_SLICE+1:
730 return -2;
731 case DELETE_SLICE+2:
732 return -2;
733 case DELETE_SLICE+3:
734 return -3;
735
736 case INPLACE_ADD:
737 case INPLACE_SUBTRACT:
738 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739 case INPLACE_MODULO:
740 return -1;
741 case STORE_SUBSCR:
742 return -3;
743 case DELETE_SUBSCR:
744 return -2;
745
746 case BINARY_LSHIFT:
747 case BINARY_RSHIFT:
748 case BINARY_AND:
749 case BINARY_XOR:
750 case BINARY_OR:
751 return -1;
752 case INPLACE_POWER:
753 return -1;
754 case GET_ITER:
755 return 0;
756
757 case PRINT_EXPR:
758 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000759 case LOAD_BUILD_CLASS:
760 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761 case INPLACE_LSHIFT:
762 case INPLACE_RSHIFT:
763 case INPLACE_AND:
764 case INPLACE_XOR:
765 case INPLACE_OR:
766 return -1;
767 case BREAK_LOOP:
768 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000769 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000770 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000771 case STORE_LOCALS:
772 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773 case RETURN_VALUE:
774 return -1;
775 case IMPORT_STAR:
776 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 case YIELD_VALUE:
778 return 0;
779
780 case POP_BLOCK:
781 return 0;
782 case END_FINALLY:
783 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
785 case STORE_NAME:
786 return -1;
787 case DELETE_NAME:
788 return 0;
789 case UNPACK_SEQUENCE:
790 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000791 case UNPACK_EX:
792 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 case FOR_ITER:
794 return 1;
795
796 case STORE_ATTR:
797 return -2;
798 case DELETE_ATTR:
799 return -1;
800 case STORE_GLOBAL:
801 return -1;
802 case DELETE_GLOBAL:
803 return 0;
804 case DUP_TOPX:
805 return oparg;
806 case LOAD_CONST:
807 return 1;
808 case LOAD_NAME:
809 return 1;
810 case BUILD_TUPLE:
811 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000812 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000813 return 1-oparg;
814 case BUILD_MAP:
815 return 1;
Thomas Wouters00e41de2007-02-23 19:56:57 +0000816 case MAKE_BYTES:
817 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818 case LOAD_ATTR:
819 return 0;
820 case COMPARE_OP:
821 return -1;
822 case IMPORT_NAME:
823 return 0;
824 case IMPORT_FROM:
825 return 1;
826
827 case JUMP_FORWARD:
828 case JUMP_IF_FALSE:
829 case JUMP_IF_TRUE:
830 case JUMP_ABSOLUTE:
831 return 0;
832
833 case LOAD_GLOBAL:
834 return 1;
835
836 case CONTINUE_LOOP:
837 return 0;
838 case SETUP_LOOP:
839 return 0;
840 case SETUP_EXCEPT:
841 case SETUP_FINALLY:
842 return 3; /* actually pushed by an exception */
843
844 case LOAD_FAST:
845 return 1;
846 case STORE_FAST:
847 return -1;
848 case DELETE_FAST:
849 return 0;
850
851 case RAISE_VARARGS:
852 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000853#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 case CALL_FUNCTION:
855 return -NARGS(oparg);
856 case CALL_FUNCTION_VAR:
857 case CALL_FUNCTION_KW:
858 return -NARGS(oparg)-1;
859 case CALL_FUNCTION_VAR_KW:
860 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000862 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000863 case MAKE_CLOSURE:
864 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000865#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 case BUILD_SLICE:
867 if (oparg == 3)
868 return -2;
869 else
870 return -1;
871
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 case LOAD_CLOSURE:
873 return 1;
874 case LOAD_DEREF:
875 return 1;
876 case STORE_DEREF:
877 return -1;
878 default:
879 fprintf(stderr, "opcode = %d\n", opcode);
880 Py_FatalError("opcode_stack_effect()");
881
882 }
883 return 0; /* not reachable */
884}
885
886/* Add an opcode with no argument.
887 Returns 0 on failure, 1 on success.
888*/
889
890static int
891compiler_addop(struct compiler *c, int opcode)
892{
893 basicblock *b;
894 struct instr *i;
895 int off;
896 off = compiler_next_instr(c, c->u->u_curblock);
897 if (off < 0)
898 return 0;
899 b = c->u->u_curblock;
900 i = &b->b_instr[off];
901 i->i_opcode = opcode;
902 i->i_hasarg = 0;
903 if (opcode == RETURN_VALUE)
904 b->b_return = 1;
905 compiler_set_lineno(c, off);
906 return 1;
907}
908
909static int
910compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
911{
912 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000914
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000915 /* necessary to make sure types aren't coerced (e.g., int and long) */
916 t = PyTuple_Pack(2, o, o->ob_type);
917 if (t == NULL)
918 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
920 v = PyDict_GetItem(dict, t);
921 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000922 if (PyErr_Occurred())
923 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000924 arg = PyDict_Size(dict);
925 v = PyInt_FromLong(arg);
926 if (!v) {
927 Py_DECREF(t);
928 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000930 if (PyDict_SetItem(dict, t, v) < 0) {
931 Py_DECREF(t);
932 Py_DECREF(v);
933 return -1;
934 }
935 Py_DECREF(v);
936 }
937 else
938 arg = PyInt_AsLong(v);
939 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000940 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000941}
942
943static int
944compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
945 PyObject *o)
946{
947 int arg = compiler_add_o(c, dict, o);
948 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000949 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950 return compiler_addop_i(c, opcode, arg);
951}
952
953static int
954compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000955 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956{
957 int arg;
958 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
959 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000960 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 arg = compiler_add_o(c, dict, mangled);
962 Py_DECREF(mangled);
963 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000964 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 return compiler_addop_i(c, opcode, arg);
966}
967
968/* Add an opcode with an integer argument.
969 Returns 0 on failure, 1 on success.
970*/
971
972static int
973compiler_addop_i(struct compiler *c, int opcode, int oparg)
974{
975 struct instr *i;
976 int off;
977 off = compiler_next_instr(c, c->u->u_curblock);
978 if (off < 0)
979 return 0;
980 i = &c->u->u_curblock->b_instr[off];
981 i->i_opcode = opcode;
982 i->i_oparg = oparg;
983 i->i_hasarg = 1;
984 compiler_set_lineno(c, off);
985 return 1;
986}
987
988static int
989compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
990{
991 struct instr *i;
992 int off;
993
994 assert(b != NULL);
995 off = compiler_next_instr(c, c->u->u_curblock);
996 if (off < 0)
997 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 i = &c->u->u_curblock->b_instr[off];
999 i->i_opcode = opcode;
1000 i->i_target = b;
1001 i->i_hasarg = 1;
1002 if (absolute)
1003 i->i_jabs = 1;
1004 else
1005 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001006 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007 return 1;
1008}
1009
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001010/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1011 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012 it as the current block. NEXT_BLOCK() also creates an implicit jump
1013 from the current block to the new block.
1014*/
1015
Thomas Wouters89f507f2006-12-13 04:49:30 +00001016/* The returns inside these macros make it impossible to decref objects
1017 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018*/
1019
1020
1021#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001022 if (compiler_use_new_block((C)) == NULL) \
1023 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024}
1025
1026#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001027 if (compiler_next_block((C)) == NULL) \
1028 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001029}
1030
1031#define ADDOP(C, OP) { \
1032 if (!compiler_addop((C), (OP))) \
1033 return 0; \
1034}
1035
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001036#define ADDOP_IN_SCOPE(C, OP) { \
1037 if (!compiler_addop((C), (OP))) { \
1038 compiler_exit_scope(c); \
1039 return 0; \
1040 } \
1041}
1042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043#define ADDOP_O(C, OP, O, TYPE) { \
1044 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1045 return 0; \
1046}
1047
1048#define ADDOP_NAME(C, OP, O, TYPE) { \
1049 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1050 return 0; \
1051}
1052
1053#define ADDOP_I(C, OP, O) { \
1054 if (!compiler_addop_i((C), (OP), (O))) \
1055 return 0; \
1056}
1057
1058#define ADDOP_JABS(C, OP, O) { \
1059 if (!compiler_addop_j((C), (OP), (O), 1)) \
1060 return 0; \
1061}
1062
1063#define ADDOP_JREL(C, OP, O) { \
1064 if (!compiler_addop_j((C), (OP), (O), 0)) \
1065 return 0; \
1066}
1067
1068/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1069 the ASDL name to synthesize the name of the C type and the visit function.
1070*/
1071
1072#define VISIT(C, TYPE, V) {\
1073 if (!compiler_visit_ ## TYPE((C), (V))) \
1074 return 0; \
1075}
1076
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001077#define VISIT_IN_SCOPE(C, TYPE, V) {\
1078 if (!compiler_visit_ ## TYPE((C), (V))) { \
1079 compiler_exit_scope(c); \
1080 return 0; \
1081 } \
1082}
1083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084#define VISIT_SLICE(C, V, CTX) {\
1085 if (!compiler_visit_slice((C), (V), (CTX))) \
1086 return 0; \
1087}
1088
1089#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001090 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001092 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001093 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094 if (!compiler_visit_ ## TYPE((C), elt)) \
1095 return 0; \
1096 } \
1097}
1098
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001099#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001100 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001101 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001102 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001103 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001104 if (!compiler_visit_ ## TYPE((C), elt)) { \
1105 compiler_exit_scope(c); \
1106 return 0; \
1107 } \
1108 } \
1109}
1110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111static int
1112compiler_isdocstring(stmt_ty s)
1113{
1114 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001115 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116 return s->v.Expr.value->kind == Str_kind;
1117}
1118
1119/* Compile a sequence of statements, checking for a docstring. */
1120
1121static int
1122compiler_body(struct compiler *c, asdl_seq *stmts)
1123{
1124 int i = 0;
1125 stmt_ty st;
1126
1127 if (!asdl_seq_LEN(stmts))
1128 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001129 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001130 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1131 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132 i = 1;
1133 VISIT(c, expr, st->v.Expr.value);
1134 if (!compiler_nameop(c, __doc__, Store))
1135 return 0;
1136 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001137 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 return 1;
1140}
1141
1142static PyCodeObject *
1143compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001144{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001145 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001146 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 static PyObject *module;
1148 if (!module) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001149 module = PyUnicode_FromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 if (!module)
1151 return NULL;
1152 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001153 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1154 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001155 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156 switch (mod->kind) {
1157 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001158 if (!compiler_body(c, mod->v.Module.body)) {
1159 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001160 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001161 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162 break;
1163 case Interactive_kind:
1164 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001165 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001166 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 break;
1168 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001169 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001170 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 break;
1172 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001173 PyErr_SetString(PyExc_SystemError,
1174 "suite should not be possible");
1175 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001176 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001177 PyErr_Format(PyExc_SystemError,
1178 "module kind %d should not be possible",
1179 mod->kind);
1180 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001181 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 co = assemble(c, addNone);
1183 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001184 return co;
1185}
1186
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187/* The test for LOCAL must come before the test for FREE in order to
1188 handle classes where name is both local and free. The local var is
1189 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001190*/
1191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192static int
1193get_ref_type(struct compiler *c, PyObject *name)
1194{
1195 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001196 if (scope == 0) {
1197 char buf[350];
1198 PyOS_snprintf(buf, sizeof(buf),
1199 "unknown scope for %.100s in %.100s(%s) in %s\n"
1200 "symbols: %s\nlocals: %s\nglobals: %s\n",
1201 PyString_AS_STRING(name),
1202 PyString_AS_STRING(c->u->u_name),
1203 PyObject_REPR(c->u->u_ste->ste_id),
1204 c->c_filename,
1205 PyObject_REPR(c->u->u_ste->ste_symbols),
1206 PyObject_REPR(c->u->u_varnames),
1207 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001209 Py_FatalError(buf);
1210 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001211
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001212 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001213}
1214
1215static int
1216compiler_lookup_arg(PyObject *dict, PyObject *name)
1217{
1218 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001219 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001221 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001223 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001225 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226 return PyInt_AS_LONG(v);
1227}
1228
1229static int
1230compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1231{
1232 int i, free = PyCode_GetNumFree(co);
1233 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001234 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1235 ADDOP_I(c, MAKE_FUNCTION, args);
1236 return 1;
1237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001238 for (i = 0; i < free; ++i) {
1239 /* Bypass com_addop_varname because it will generate
1240 LOAD_DEREF but LOAD_CLOSURE is needed.
1241 */
1242 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1243 int arg, reftype;
1244
1245 /* Special case: If a class contains a method with a
1246 free variable that has the same name as a method,
1247 the name will be considered free *and* local in the
1248 class. It should be handled by the closure, as
1249 well as by the normal name loookup logic.
1250 */
1251 reftype = get_ref_type(c, name);
1252 if (reftype == CELL)
1253 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1254 else /* (reftype == FREE) */
1255 arg = compiler_lookup_arg(c->u->u_freevars, name);
1256 if (arg == -1) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001257 fprintf(stderr,
1258 "lookup %s in %s %d %d\n"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 "freevars of %s: %s\n",
1260 PyObject_REPR(name),
1261 PyString_AS_STRING(c->u->u_name),
1262 reftype, arg,
1263 PyString_AS_STRING(co->co_name),
1264 PyObject_REPR(co->co_freevars));
1265 Py_FatalError("compiler_make_closure()");
1266 }
1267 ADDOP_I(c, LOAD_CLOSURE, arg);
1268 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001271 ADDOP_I(c, MAKE_CLOSURE, args);
1272 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273}
1274
1275static int
1276compiler_decorators(struct compiler *c, asdl_seq* decos)
1277{
1278 int i;
1279
1280 if (!decos)
1281 return 1;
1282
1283 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001285 }
1286 return 1;
1287}
1288
1289static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001290compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1291 asdl_seq *kw_defaults)
1292{
1293 int i, default_count = 0;
1294 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001295 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001296 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1297 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001298 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001299 if (!compiler_visit_expr(c, default_)) {
1300 return -1;
1301 }
1302 default_count++;
1303 }
1304 }
1305 return default_count;
1306}
1307
1308static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001309compiler_visit_argannotation(struct compiler *c, identifier id,
1310 expr_ty annotation, PyObject *names)
1311{
1312 if (annotation) {
1313 VISIT(c, expr, annotation);
1314 if (PyList_Append(names, id))
1315 return -1;
1316 }
1317 return 0;
1318}
1319
1320static int
1321compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1322 PyObject *names)
1323{
1324 int i, error;
1325 for (i = 0; i < asdl_seq_LEN(args); i++) {
1326 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001327 error = compiler_visit_argannotation(
1328 c,
1329 arg->arg,
1330 arg->annotation,
1331 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001332 if (error)
1333 return error;
1334 }
1335 return 0;
1336}
1337
1338static int
1339compiler_visit_annotations(struct compiler *c, arguments_ty args,
1340 expr_ty returns)
1341{
Guido van Rossum0240b922007-02-26 21:23:50 +00001342 /* Push arg annotations and a list of the argument names. Return the #
1343 of items pushed. The expressions are evaluated out-of-order wrt the
1344 source code.
1345
1346 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1347 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001348 static identifier return_str;
1349 PyObject *names;
1350 int len;
1351 names = PyList_New(0);
1352 if (!names)
1353 return -1;
1354
1355 if (compiler_visit_argannotations(c, args->args, names))
1356 goto error;
1357 if (args->varargannotation &&
1358 compiler_visit_argannotation(c, args->vararg,
1359 args->varargannotation, names))
1360 goto error;
1361 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1362 goto error;
1363 if (args->kwargannotation &&
1364 compiler_visit_argannotation(c, args->kwarg,
1365 args->kwargannotation, names))
1366 goto error;
1367
1368 if (!return_str) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001369 return_str = PyUnicode_InternFromString("return");
Neal Norwitzc1505362006-12-28 06:47:50 +00001370 if (!return_str)
1371 goto error;
1372 }
1373 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1374 goto error;
1375 }
1376
1377 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001378 if (len > 65534) {
1379 /* len must fit in 16 bits, and len is incremented below */
1380 PyErr_SetString(PyExc_SyntaxError,
1381 "too many annotations");
1382 goto error;
1383 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001384 if (len) {
1385 /* convert names to a tuple and place on stack */
1386 PyObject *elt;
1387 int i;
1388 PyObject *s = PyTuple_New(len);
1389 if (!s)
1390 goto error;
1391 for (i = 0; i < len; i++) {
1392 elt = PyList_GET_ITEM(names, i);
1393 Py_INCREF(elt);
1394 PyTuple_SET_ITEM(s, i, elt);
1395 }
1396 ADDOP_O(c, LOAD_CONST, s, consts);
1397 Py_DECREF(s);
1398 len++; /* include the just-pushed tuple */
1399 }
1400 Py_DECREF(names);
1401 return len;
1402
1403error:
1404 Py_DECREF(names);
1405 return -1;
1406}
1407
1408static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409compiler_function(struct compiler *c, stmt_ty s)
1410{
1411 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001412 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001414 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001415 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001416 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001417 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001418 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419
1420 assert(s->kind == FunctionDef_kind);
1421
1422 if (!compiler_decorators(c, decos))
1423 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001424 if (args->kwonlyargs) {
1425 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1426 args->kw_defaults);
1427 if (res < 0)
1428 return 0;
1429 kw_default_count = res;
1430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 if (args->defaults)
1432 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001433 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001434 if (num_annotations < 0)
1435 return 0;
1436 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1439 s->lineno))
1440 return 0;
1441
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001442 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001443 docstring = compiler_isdocstring(st);
1444 if (docstring)
1445 first_const = st->v.Expr.value->v.Str.s;
1446 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001447 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001448 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001449 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001454 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001455 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001456 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1457 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 }
1459 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001460 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 if (co == NULL)
1462 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463
Guido van Rossum4f72a782006-10-27 23:31:49 +00001464 arglength = asdl_seq_LEN(args->defaults);
1465 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001468 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469
Neal Norwitzc1505362006-12-28 06:47:50 +00001470 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1472 ADDOP_I(c, CALL_FUNCTION, 1);
1473 }
1474
1475 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1476}
1477
1478static int
1479compiler_class(struct compiler *c, stmt_ty s)
1480{
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001481 static PyObject *locals = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001483 PyObject *str;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001484 PySTEntryObject *ste;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001485 int err, i;
1486 asdl_seq* decos = s->v.ClassDef.decorator_list;
1487
1488 if (!compiler_decorators(c, decos))
1489 return 0;
1490
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001491 /* initialize statics */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001492 if (locals == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001493 locals = PyUnicode_FromString("__locals__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001494 if (locals == NULL)
1495 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001497
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001498 /* ultimately generate code for:
1499 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1500 where:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001501 <func> is a function/closure created from the class body;
1502 it has a single argument (__locals__) where the dict
1503 (or MutableSequence) representing the locals is passed
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001504 <name> is the class name
1505 <bases> is the positional arguments and *varargs argument
1506 <keywords> is the keyword arguments and **kwds argument
1507 This borrows from compiler_call.
1508 */
1509
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001510 /* 0. Create a fake argument named __locals__ */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001511 ste = PySymtable_Lookup(c->c_st, s);
1512 if (ste == NULL)
1513 return 0;
1514 assert(PyList_Check(ste->ste_varnames));
Guido van Rossum3a383622007-03-21 21:26:58 +00001515 err = PyList_Append(ste->ste_varnames, locals);
1516 Py_DECREF(ste);
1517 if (err < 0)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001518 return 0;
1519
1520 /* 1. compile the class body into a code object */
1521 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1522 return 0;
1523 /* this block represents what we do in the new scope */
1524 {
1525 /* use the class name for name mangling */
1526 Py_INCREF(s->v.ClassDef.name);
1527 c->u->u_private = s->v.ClassDef.name;
1528 /* force it to have one mandatory argument */
1529 c->u->u_argcount = 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001530 /* load the first argument (__locals__) ... */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001531 ADDOP_I(c, LOAD_FAST, 0);
1532 /* ... and store it into f_locals */
1533 ADDOP_IN_SCOPE(c, STORE_LOCALS);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001534 /* load (global) __name__ ... */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001535 str = PyUnicode_InternFromString("__name__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001536 if (!str || !compiler_nameop(c, str, Load)) {
1537 Py_XDECREF(str);
1538 compiler_exit_scope(c);
1539 return 0;
1540 }
1541 Py_DECREF(str);
1542 /* ... and store it as __module__ */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001543 str = PyUnicode_InternFromString("__module__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001544 if (!str || !compiler_nameop(c, str, Store)) {
1545 Py_XDECREF(str);
1546 compiler_exit_scope(c);
1547 return 0;
1548 }
1549 Py_DECREF(str);
1550 /* compile the body proper */
1551 if (!compiler_body(c, s->v.ClassDef.body)) {
1552 compiler_exit_scope(c);
1553 return 0;
1554 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001555 /* return the (empty) __class__ cell */
1556 str = PyUnicode_InternFromString("__class__");
1557 if (str == NULL) {
1558 compiler_exit_scope(c);
1559 return 0;
1560 }
1561 i = compiler_lookup_arg(c->u->u_cellvars, str);
1562 Py_DECREF(str);
1563 if (i == -1) {
1564 /* This happens when nobody references the cell */
1565 PyErr_Clear();
1566 /* Return None */
1567 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1568 }
1569 else {
1570 /* Return the cell where to store __class__ */
1571 ADDOP_I(c, LOAD_CLOSURE, i);
1572 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001573 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1574 /* create the code object */
1575 co = assemble(c, 1);
1576 }
1577 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001578 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001579 if (co == NULL)
1580 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001581
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001582 /* 2. load the 'build_class' function */
1583 ADDOP(c, LOAD_BUILD_CLASS);
1584
1585 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001586 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001587 Py_DECREF(co);
1588
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001589 /* 4. load class name */
1590 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1591
1592 /* 5. generate the rest of the code for the call */
1593 if (!compiler_call_helper(c, 2,
1594 s->v.ClassDef.bases,
1595 s->v.ClassDef.keywords,
1596 s->v.ClassDef.starargs,
1597 s->v.ClassDef.kwargs))
1598 return 0;
1599
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001600 /* 6. apply decorators */
1601 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1602 ADDOP_I(c, CALL_FUNCTION, 1);
1603 }
1604
1605 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001606 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1607 return 0;
1608 return 1;
1609}
1610
1611static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001612compiler_ifexp(struct compiler *c, expr_ty e)
1613{
1614 basicblock *end, *next;
1615
1616 assert(e->kind == IfExp_kind);
1617 end = compiler_new_block(c);
1618 if (end == NULL)
1619 return 0;
1620 next = compiler_new_block(c);
1621 if (next == NULL)
1622 return 0;
1623 VISIT(c, expr, e->v.IfExp.test);
1624 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1625 ADDOP(c, POP_TOP);
1626 VISIT(c, expr, e->v.IfExp.body);
1627 ADDOP_JREL(c, JUMP_FORWARD, end);
1628 compiler_use_next_block(c, next);
1629 ADDOP(c, POP_TOP);
1630 VISIT(c, expr, e->v.IfExp.orelse);
1631 compiler_use_next_block(c, end);
1632 return 1;
1633}
1634
1635static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636compiler_lambda(struct compiler *c, expr_ty e)
1637{
1638 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001639 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001640 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641 arguments_ty args = e->v.Lambda.args;
1642 assert(e->kind == Lambda_kind);
1643
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001644 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001645 name = PyUnicode_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001646 if (!name)
1647 return 0;
1648 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649
Guido van Rossum4f72a782006-10-27 23:31:49 +00001650 if (args->kwonlyargs) {
1651 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1652 args->kw_defaults);
1653 if (res < 0) return 0;
1654 kw_default_count = res;
1655 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 if (args->defaults)
1657 VISIT_SEQ(c, expr, args->defaults);
1658 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1659 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001662 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001663 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1664 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001666 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001667 if (co == NULL)
1668 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669
Guido van Rossum4f72a782006-10-27 23:31:49 +00001670 arglength = asdl_seq_LEN(args->defaults);
1671 arglength |= kw_default_count << 8;
1672 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001673 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674
1675 return 1;
1676}
1677
1678static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001679compiler_if(struct compiler *c, stmt_ty s)
1680{
1681 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001682 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 assert(s->kind == If_kind);
1684 end = compiler_new_block(c);
1685 if (end == NULL)
1686 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001687 next = compiler_new_block(c);
1688 if (next == NULL)
1689 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001690
1691 constant = expr_constant(s->v.If.test);
1692 /* constant = 0: "if 0"
1693 * constant = 1: "if 1", "if 2", ...
1694 * constant = -1: rest */
1695 if (constant == 0) {
1696 if (s->v.If.orelse)
1697 VISIT_SEQ(c, stmt, s->v.If.orelse);
1698 } else if (constant == 1) {
1699 VISIT_SEQ(c, stmt, s->v.If.body);
1700 } else {
1701 VISIT(c, expr, s->v.If.test);
1702 ADDOP_JREL(c, JUMP_IF_FALSE, next);
1703 ADDOP(c, POP_TOP);
1704 VISIT_SEQ(c, stmt, s->v.If.body);
1705 ADDOP_JREL(c, JUMP_FORWARD, end);
1706 compiler_use_next_block(c, next);
1707 ADDOP(c, POP_TOP);
1708 if (s->v.If.orelse)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001709 VISIT_SEQ(c, stmt, s->v.If.orelse);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711 compiler_use_next_block(c, end);
1712 return 1;
1713}
1714
1715static int
1716compiler_for(struct compiler *c, stmt_ty s)
1717{
1718 basicblock *start, *cleanup, *end;
1719
1720 start = compiler_new_block(c);
1721 cleanup = compiler_new_block(c);
1722 end = compiler_new_block(c);
1723 if (start == NULL || end == NULL || cleanup == NULL)
1724 return 0;
1725 ADDOP_JREL(c, SETUP_LOOP, end);
1726 if (!compiler_push_fblock(c, LOOP, start))
1727 return 0;
1728 VISIT(c, expr, s->v.For.iter);
1729 ADDOP(c, GET_ITER);
1730 compiler_use_next_block(c, start);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001731 /* XXX(nnorwitz): is there a better way to handle this?
1732 for loops are special, we want to be able to trace them
1733 each time around, so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001734 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001735 ADDOP_JREL(c, FOR_ITER, cleanup);
1736 VISIT(c, expr, s->v.For.target);
1737 VISIT_SEQ(c, stmt, s->v.For.body);
1738 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1739 compiler_use_next_block(c, cleanup);
1740 ADDOP(c, POP_BLOCK);
1741 compiler_pop_fblock(c, LOOP, start);
1742 VISIT_SEQ(c, stmt, s->v.For.orelse);
1743 compiler_use_next_block(c, end);
1744 return 1;
1745}
1746
1747static int
1748compiler_while(struct compiler *c, stmt_ty s)
1749{
1750 basicblock *loop, *orelse, *end, *anchor = NULL;
1751 int constant = expr_constant(s->v.While.test);
1752
1753 if (constant == 0)
1754 return 1;
1755 loop = compiler_new_block(c);
1756 end = compiler_new_block(c);
1757 if (constant == -1) {
1758 anchor = compiler_new_block(c);
1759 if (anchor == NULL)
1760 return 0;
1761 }
1762 if (loop == NULL || end == NULL)
1763 return 0;
1764 if (s->v.While.orelse) {
1765 orelse = compiler_new_block(c);
1766 if (orelse == NULL)
1767 return 0;
1768 }
1769 else
1770 orelse = NULL;
1771
1772 ADDOP_JREL(c, SETUP_LOOP, end);
1773 compiler_use_next_block(c, loop);
1774 if (!compiler_push_fblock(c, LOOP, loop))
1775 return 0;
1776 if (constant == -1) {
1777 VISIT(c, expr, s->v.While.test);
1778 ADDOP_JREL(c, JUMP_IF_FALSE, anchor);
1779 ADDOP(c, POP_TOP);
1780 }
1781 VISIT_SEQ(c, stmt, s->v.While.body);
1782 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1783
1784 /* XXX should the two POP instructions be in a separate block
1785 if there is no else clause ?
1786 */
1787
1788 if (constant == -1) {
1789 compiler_use_next_block(c, anchor);
1790 ADDOP(c, POP_TOP);
1791 ADDOP(c, POP_BLOCK);
1792 }
1793 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001794 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001795 VISIT_SEQ(c, stmt, s->v.While.orelse);
1796 compiler_use_next_block(c, end);
1797
1798 return 1;
1799}
1800
1801static int
1802compiler_continue(struct compiler *c)
1803{
1804 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001805 static const char IN_FINALLY_ERROR_MSG[] =
1806 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807 int i;
1808
1809 if (!c->u->u_nfblocks)
1810 return compiler_error(c, LOOP_ERROR_MSG);
1811 i = c->u->u_nfblocks - 1;
1812 switch (c->u->u_fblock[i].fb_type) {
1813 case LOOP:
1814 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1815 break;
1816 case EXCEPT:
1817 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001818 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1819 /* Prevent continue anywhere under a finally
1820 even if hidden in a sub-try or except. */
1821 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1822 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1823 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824 if (i == -1)
1825 return compiler_error(c, LOOP_ERROR_MSG);
1826 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1827 break;
1828 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001829 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 }
1831
1832 return 1;
1833}
1834
1835/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1836
1837 SETUP_FINALLY L
1838 <code for body>
1839 POP_BLOCK
1840 LOAD_CONST <None>
1841 L: <code for finalbody>
1842 END_FINALLY
1843
1844 The special instructions use the block stack. Each block
1845 stack entry contains the instruction that created it (here
1846 SETUP_FINALLY), the level of the value stack at the time the
1847 block stack entry was created, and a label (here L).
1848
1849 SETUP_FINALLY:
1850 Pushes the current value stack level and the label
1851 onto the block stack.
1852 POP_BLOCK:
1853 Pops en entry from the block stack, and pops the value
1854 stack until its level is the same as indicated on the
1855 block stack. (The label is ignored.)
1856 END_FINALLY:
1857 Pops a variable number of entries from the *value* stack
1858 and re-raises the exception they specify. The number of
1859 entries popped depends on the (pseudo) exception type.
1860
1861 The block stack is unwound when an exception is raised:
1862 when a SETUP_FINALLY entry is found, the exception is pushed
1863 onto the value stack (and the exception condition is cleared),
1864 and the interpreter jumps to the label gotten from the block
1865 stack.
1866*/
1867
1868static int
1869compiler_try_finally(struct compiler *c, stmt_ty s)
1870{
1871 basicblock *body, *end;
1872 body = compiler_new_block(c);
1873 end = compiler_new_block(c);
1874 if (body == NULL || end == NULL)
1875 return 0;
1876
1877 ADDOP_JREL(c, SETUP_FINALLY, end);
1878 compiler_use_next_block(c, body);
1879 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1880 return 0;
1881 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1882 ADDOP(c, POP_BLOCK);
1883 compiler_pop_fblock(c, FINALLY_TRY, body);
1884
1885 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1886 compiler_use_next_block(c, end);
1887 if (!compiler_push_fblock(c, FINALLY_END, end))
1888 return 0;
1889 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1890 ADDOP(c, END_FINALLY);
1891 compiler_pop_fblock(c, FINALLY_END, end);
1892
1893 return 1;
1894}
1895
1896/*
1897 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1898 (The contents of the value stack is shown in [], with the top
1899 at the right; 'tb' is trace-back info, 'val' the exception's
1900 associated value, and 'exc' the exception.)
1901
1902 Value stack Label Instruction Argument
1903 [] SETUP_EXCEPT L1
1904 [] <code for S>
1905 [] POP_BLOCK
1906 [] JUMP_FORWARD L0
1907
1908 [tb, val, exc] L1: DUP )
1909 [tb, val, exc, exc] <evaluate E1> )
1910 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1911 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
1912 [tb, val, exc, 1] POP )
1913 [tb, val, exc] POP
1914 [tb, val] <assign to V1> (or POP if no V1)
1915 [tb] POP
1916 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001917 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918
1919 [tb, val, exc, 0] L2: POP
1920 [tb, val, exc] DUP
1921 .............................etc.......................
1922
1923 [tb, val, exc, 0] Ln+1: POP
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001924 [tb, val, exc] END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925
1926 [] L0: <next statement>
1927
1928 Of course, parts are not generated if Vi or Ei is not present.
1929*/
1930static int
1931compiler_try_except(struct compiler *c, stmt_ty s)
1932{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001933 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934 int i, n;
1935
1936 body = compiler_new_block(c);
1937 except = compiler_new_block(c);
1938 orelse = compiler_new_block(c);
1939 end = compiler_new_block(c);
1940 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1941 return 0;
1942 ADDOP_JREL(c, SETUP_EXCEPT, except);
1943 compiler_use_next_block(c, body);
1944 if (!compiler_push_fblock(c, EXCEPT, body))
1945 return 0;
1946 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1947 ADDOP(c, POP_BLOCK);
1948 compiler_pop_fblock(c, EXCEPT, body);
1949 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1950 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1951 compiler_use_next_block(c, except);
1952 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001953 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 s->v.TryExcept.handlers, i);
1955 if (!handler->type && i < n-1)
1956 return compiler_error(c, "default 'except:' must be last");
Guido van Rossumd8faa362007-04-27 19:54:29 +00001957 c->u->u_lineno_set = 0;
1958 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 except = compiler_new_block(c);
1960 if (except == NULL)
1961 return 0;
1962 if (handler->type) {
1963 ADDOP(c, DUP_TOP);
1964 VISIT(c, expr, handler->type);
1965 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1966 ADDOP_JREL(c, JUMP_IF_FALSE, except);
1967 ADDOP(c, POP_TOP);
1968 }
1969 ADDOP(c, POP_TOP);
1970 if (handler->name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001971 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001972
1973 cleanup_end = compiler_new_block(c);
1974 cleanup_body = compiler_new_block(c);
1975 if(!(cleanup_end || cleanup_body))
1976 return 0;
1977
Guido van Rossum16be03e2007-01-10 18:51:35 +00001978 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001979 ADDOP(c, POP_TOP);
1980
1981 /*
1982 try:
1983 # body
1984 except type as name:
1985 try:
1986 # body
1987 finally:
1988 name = None
1989 del name
1990 */
1991
1992 /* second try: */
1993 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
1994 compiler_use_next_block(c, cleanup_body);
1995 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
1996 return 0;
1997
1998 /* second # body */
1999 VISIT_SEQ(c, stmt, handler->body);
2000 ADDOP(c, POP_BLOCK);
2001 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2002
2003 /* finally: */
2004 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2005 compiler_use_next_block(c, cleanup_end);
2006 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2007 return 0;
2008
2009 /* name = None */
2010 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Guido van Rossum16be03e2007-01-10 18:51:35 +00002011 compiler_nameop(c, handler->name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002012
Guido van Rossum16be03e2007-01-10 18:51:35 +00002013 /* del name */
2014 compiler_nameop(c, handler->name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002015
2016 ADDOP(c, END_FINALLY);
2017 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002018 }
2019 else {
Guido van Rossumb940e112007-01-10 16:19:56 +00002020 ADDOP(c, POP_TOP);
2021 ADDOP(c, POP_TOP);
2022 VISIT_SEQ(c, stmt, handler->body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002023 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002024 ADDOP_JREL(c, JUMP_FORWARD, end);
2025 compiler_use_next_block(c, except);
2026 if (handler->type)
2027 ADDOP(c, POP_TOP);
2028 }
2029 ADDOP(c, END_FINALLY);
2030 compiler_use_next_block(c, orelse);
2031 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2032 compiler_use_next_block(c, end);
2033 return 1;
2034}
2035
2036static int
2037compiler_import_as(struct compiler *c, identifier name, identifier asname)
2038{
2039 /* The IMPORT_NAME opcode was already generated. This function
2040 merely needs to bind the result to a name.
2041
2042 If there is a dot in name, we need to split it and emit a
2043 LOAD_ATTR for each name.
2044 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002045 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2046 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002047 if (dot) {
2048 /* Consume the base module name to get the first attribute */
2049 src = dot + 1;
2050 while (dot) {
2051 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002052 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002053 dot = Py_UNICODE_strchr(src, '.');
2054 attr = PyUnicode_FromUnicode(src,
2055 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002056 if (!attr)
2057 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002059 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060 src = dot + 1;
2061 }
2062 }
2063 return compiler_nameop(c, asname, Store);
2064}
2065
2066static int
2067compiler_import(struct compiler *c, stmt_ty s)
2068{
2069 /* The Import node stores a module name like a.b.c as a single
2070 string. This is convenient for all cases except
2071 import a.b.c as d
2072 where we need to parse that string to extract the individual
2073 module names.
2074 XXX Perhaps change the representation to make this case simpler?
2075 */
2076 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002077
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002078 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002079 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002081 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002082
Guido van Rossum45aecf42006-03-15 04:58:47 +00002083 level = PyInt_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002084 if (level == NULL)
2085 return 0;
2086
2087 ADDOP_O(c, LOAD_CONST, level, consts);
2088 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2090 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2091
2092 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002093 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002094 if (!r)
2095 return r;
2096 }
2097 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002099 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2100 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002101 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002102 tmp = PyUnicode_FromUnicode(base,
2103 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 r = compiler_nameop(c, tmp, Store);
2105 if (dot) {
2106 Py_DECREF(tmp);
2107 }
2108 if (!r)
2109 return r;
2110 }
2111 }
2112 return 1;
2113}
2114
2115static int
2116compiler_from_import(struct compiler *c, stmt_ty s)
2117{
2118 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119
2120 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002121 PyObject *level;
2122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 if (!names)
2124 return 0;
2125
Guido van Rossum45aecf42006-03-15 04:58:47 +00002126 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002127 if (!level) {
2128 Py_DECREF(names);
2129 return 0;
2130 }
2131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132 /* build up the names */
2133 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002134 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 Py_INCREF(alias->name);
2136 PyTuple_SET_ITEM(names, i, alias->name);
2137 }
2138
2139 if (s->lineno > c->c_future->ff_lineno) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002140 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2141 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002142 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 Py_DECREF(names);
2144 return compiler_error(c,
2145 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002146 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002147
2148 }
2149 }
2150
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002151 ADDOP_O(c, LOAD_CONST, level, consts);
2152 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002154 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2156 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002157 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002158 identifier store_name;
2159
Martin v. Löwis5b222132007-06-10 09:51:05 +00002160 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 assert(n == 1);
2162 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002163 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164 }
2165
2166 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2167 store_name = alias->name;
2168 if (alias->asname)
2169 store_name = alias->asname;
2170
2171 if (!compiler_nameop(c, store_name, Store)) {
2172 Py_DECREF(names);
2173 return 0;
2174 }
2175 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002176 /* remove imported module */
2177 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 return 1;
2179}
2180
2181static int
2182compiler_assert(struct compiler *c, stmt_ty s)
2183{
2184 static PyObject *assertion_error = NULL;
2185 basicblock *end;
2186
2187 if (Py_OptimizeFlag)
2188 return 1;
2189 if (assertion_error == NULL) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002190 assertion_error = PyUnicode_FromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002191 if (assertion_error == NULL)
2192 return 0;
2193 }
2194 VISIT(c, expr, s->v.Assert.test);
2195 end = compiler_new_block(c);
2196 if (end == NULL)
2197 return 0;
2198 ADDOP_JREL(c, JUMP_IF_TRUE, end);
2199 ADDOP(c, POP_TOP);
2200 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2201 if (s->v.Assert.msg) {
2202 VISIT(c, expr, s->v.Assert.msg);
2203 ADDOP_I(c, RAISE_VARARGS, 2);
2204 }
2205 else {
2206 ADDOP_I(c, RAISE_VARARGS, 1);
2207 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002208 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 ADDOP(c, POP_TOP);
2210 return 1;
2211}
2212
2213static int
2214compiler_visit_stmt(struct compiler *c, stmt_ty s)
2215{
2216 int i, n;
2217
Thomas Wouters89f507f2006-12-13 04:49:30 +00002218 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002220 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002221
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002223 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002225 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002226 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002227 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 if (c->u->u_ste->ste_type != FunctionBlock)
2229 return compiler_error(c, "'return' outside function");
2230 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002231 VISIT(c, expr, s->v.Return.value);
2232 }
2233 else
2234 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2235 ADDOP(c, RETURN_VALUE);
2236 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002237 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238 VISIT_SEQ(c, expr, s->v.Delete.targets)
2239 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002240 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 n = asdl_seq_LEN(s->v.Assign.targets);
2242 VISIT(c, expr, s->v.Assign.value);
2243 for (i = 0; i < n; i++) {
2244 if (i < n - 1)
2245 ADDOP(c, DUP_TOP);
2246 VISIT(c, expr,
2247 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2248 }
2249 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002250 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002252 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002253 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002254 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002255 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002256 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002258 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 n = 0;
2260 if (s->v.Raise.type) {
2261 VISIT(c, expr, s->v.Raise.type);
2262 n++;
2263 if (s->v.Raise.inst) {
2264 VISIT(c, expr, s->v.Raise.inst);
2265 n++;
2266 if (s->v.Raise.tback) {
2267 VISIT(c, expr, s->v.Raise.tback);
2268 n++;
2269 }
2270 }
2271 }
2272 ADDOP_I(c, RAISE_VARARGS, n);
2273 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002274 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002276 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002278 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002280 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002281 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002282 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002283 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002284 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002285 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002286 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002287 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002288 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002289 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 ADDOP(c, PRINT_EXPR);
2291 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002292 else if (s->v.Expr.value->kind != Str_kind &&
2293 s->v.Expr.value->kind != Num_kind) {
2294 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 ADDOP(c, POP_TOP);
2296 }
2297 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002298 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002300 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002301 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 return compiler_error(c, "'break' outside loop");
2303 ADDOP(c, BREAK_LOOP);
2304 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002305 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002307 case With_kind:
2308 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 }
2310 return 1;
2311}
2312
2313static int
2314unaryop(unaryop_ty op)
2315{
2316 switch (op) {
2317 case Invert:
2318 return UNARY_INVERT;
2319 case Not:
2320 return UNARY_NOT;
2321 case UAdd:
2322 return UNARY_POSITIVE;
2323 case USub:
2324 return UNARY_NEGATIVE;
2325 }
2326 return 0;
2327}
2328
2329static int
2330binop(struct compiler *c, operator_ty op)
2331{
2332 switch (op) {
2333 case Add:
2334 return BINARY_ADD;
2335 case Sub:
2336 return BINARY_SUBTRACT;
2337 case Mult:
2338 return BINARY_MULTIPLY;
2339 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002340 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002341 case Mod:
2342 return BINARY_MODULO;
2343 case Pow:
2344 return BINARY_POWER;
2345 case LShift:
2346 return BINARY_LSHIFT;
2347 case RShift:
2348 return BINARY_RSHIFT;
2349 case BitOr:
2350 return BINARY_OR;
2351 case BitXor:
2352 return BINARY_XOR;
2353 case BitAnd:
2354 return BINARY_AND;
2355 case FloorDiv:
2356 return BINARY_FLOOR_DIVIDE;
2357 }
2358 return 0;
2359}
2360
2361static int
2362cmpop(cmpop_ty op)
2363{
2364 switch (op) {
2365 case Eq:
2366 return PyCmp_EQ;
2367 case NotEq:
2368 return PyCmp_NE;
2369 case Lt:
2370 return PyCmp_LT;
2371 case LtE:
2372 return PyCmp_LE;
2373 case Gt:
2374 return PyCmp_GT;
2375 case GtE:
2376 return PyCmp_GE;
2377 case Is:
2378 return PyCmp_IS;
2379 case IsNot:
2380 return PyCmp_IS_NOT;
2381 case In:
2382 return PyCmp_IN;
2383 case NotIn:
2384 return PyCmp_NOT_IN;
2385 }
2386 return PyCmp_BAD;
2387}
2388
2389static int
2390inplace_binop(struct compiler *c, operator_ty op)
2391{
2392 switch (op) {
2393 case Add:
2394 return INPLACE_ADD;
2395 case Sub:
2396 return INPLACE_SUBTRACT;
2397 case Mult:
2398 return INPLACE_MULTIPLY;
2399 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002400 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 case Mod:
2402 return INPLACE_MODULO;
2403 case Pow:
2404 return INPLACE_POWER;
2405 case LShift:
2406 return INPLACE_LSHIFT;
2407 case RShift:
2408 return INPLACE_RSHIFT;
2409 case BitOr:
2410 return INPLACE_OR;
2411 case BitXor:
2412 return INPLACE_XOR;
2413 case BitAnd:
2414 return INPLACE_AND;
2415 case FloorDiv:
2416 return INPLACE_FLOOR_DIVIDE;
2417 }
Neal Norwitz4737b232005-11-19 23:58:29 +00002418 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002419 "inplace binary op %d should not be possible", op);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002420 return 0;
2421}
2422
2423static int
2424compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2425{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002426 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2428
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002429 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002430 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431 /* XXX AugStore isn't used anywhere! */
2432
2433 /* First check for assignment to __debug__. Param? */
2434 if ((ctx == Store || ctx == AugStore || ctx == Del)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002435 && !PyUnicode_CompareWithASCIIString(name, "__debug__")) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002436 return compiler_error(c, "can not assign to __debug__");
2437 }
2438
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002439 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002440 if (!mangled)
2441 return 0;
2442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443 op = 0;
2444 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002445 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446 switch (scope) {
2447 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002448 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449 optype = OP_DEREF;
2450 break;
2451 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002452 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 optype = OP_DEREF;
2454 break;
2455 case LOCAL:
2456 if (c->u->u_ste->ste_type == FunctionBlock)
2457 optype = OP_FAST;
2458 break;
2459 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002460 if (c->u->u_ste->ste_type == FunctionBlock &&
2461 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 optype = OP_GLOBAL;
2463 break;
2464 case GLOBAL_EXPLICIT:
2465 optype = OP_GLOBAL;
2466 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002467 default:
2468 /* scope can be 0 */
2469 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 }
2471
2472 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002473 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474
2475 switch (optype) {
2476 case OP_DEREF:
2477 switch (ctx) {
2478 case Load: op = LOAD_DEREF; break;
2479 case Store: op = STORE_DEREF; break;
2480 case AugLoad:
2481 case AugStore:
2482 break;
2483 case Del:
2484 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002485 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002487 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002488 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002491 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002492 PyErr_SetString(PyExc_SystemError,
2493 "param invalid for deref variable");
2494 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495 }
2496 break;
2497 case OP_FAST:
2498 switch (ctx) {
2499 case Load: op = LOAD_FAST; break;
2500 case Store: op = STORE_FAST; break;
2501 case Del: op = DELETE_FAST; break;
2502 case AugLoad:
2503 case AugStore:
2504 break;
2505 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002506 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002507 PyErr_SetString(PyExc_SystemError,
2508 "param invalid for local variable");
2509 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002511 ADDOP_O(c, op, mangled, varnames);
2512 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513 return 1;
2514 case OP_GLOBAL:
2515 switch (ctx) {
2516 case Load: op = LOAD_GLOBAL; break;
2517 case Store: op = STORE_GLOBAL; break;
2518 case Del: op = DELETE_GLOBAL; break;
2519 case AugLoad:
2520 case AugStore:
2521 break;
2522 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002523 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002524 PyErr_SetString(PyExc_SystemError,
2525 "param invalid for global variable");
2526 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 }
2528 break;
2529 case OP_NAME:
2530 switch (ctx) {
2531 case Load: op = LOAD_NAME; break;
2532 case Store: op = STORE_NAME; break;
2533 case Del: op = DELETE_NAME; break;
2534 case AugLoad:
2535 case AugStore:
2536 break;
2537 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002538 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002539 PyErr_SetString(PyExc_SystemError,
2540 "param invalid for name variable");
2541 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 }
2543 break;
2544 }
2545
2546 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002547 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002548 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002549 if (arg < 0)
2550 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002551 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
2554static int
2555compiler_boolop(struct compiler *c, expr_ty e)
2556{
2557 basicblock *end;
2558 int jumpi, i, n;
2559 asdl_seq *s;
2560
2561 assert(e->kind == BoolOp_kind);
2562 if (e->v.BoolOp.op == And)
2563 jumpi = JUMP_IF_FALSE;
2564 else
2565 jumpi = JUMP_IF_TRUE;
2566 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002567 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002568 return 0;
2569 s = e->v.BoolOp.values;
2570 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002571 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002572 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002573 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 ADDOP_JREL(c, jumpi, end);
2575 ADDOP(c, POP_TOP)
2576 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002577 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 compiler_use_next_block(c, end);
2579 return 1;
2580}
2581
2582static int
2583compiler_list(struct compiler *c, expr_ty e)
2584{
2585 int n = asdl_seq_LEN(e->v.List.elts);
2586 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002587 int i, seen_star = 0;
2588 for (i = 0; i < n; i++) {
2589 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2590 if (elt->kind == Starred_kind && !seen_star) {
2591 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2592 seen_star = 1;
2593 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2594 } else if (elt->kind == Starred_kind) {
2595 return compiler_error(c,
2596 "two starred expressions in assignment");
2597 }
2598 }
2599 if (!seen_star) {
2600 ADDOP_I(c, UNPACK_SEQUENCE, n);
2601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 }
2603 VISIT_SEQ(c, expr, e->v.List.elts);
2604 if (e->v.List.ctx == Load) {
2605 ADDOP_I(c, BUILD_LIST, n);
2606 }
2607 return 1;
2608}
2609
2610static int
2611compiler_tuple(struct compiler *c, expr_ty e)
2612{
2613 int n = asdl_seq_LEN(e->v.Tuple.elts);
2614 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002615 int i, seen_star = 0;
2616 for (i = 0; i < n; i++) {
2617 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2618 if (elt->kind == Starred_kind && !seen_star) {
2619 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2620 seen_star = 1;
2621 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2622 } else if (elt->kind == Starred_kind) {
2623 return compiler_error(c,
2624 "two starred expressions in assignment");
2625 }
2626 }
2627 if (!seen_star) {
2628 ADDOP_I(c, UNPACK_SEQUENCE, n);
2629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002630 }
2631 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2632 if (e->v.Tuple.ctx == Load) {
2633 ADDOP_I(c, BUILD_TUPLE, n);
2634 }
2635 return 1;
2636}
2637
2638static int
2639compiler_compare(struct compiler *c, expr_ty e)
2640{
2641 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002642 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002643
2644 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2645 VISIT(c, expr, e->v.Compare.left);
2646 n = asdl_seq_LEN(e->v.Compare.ops);
2647 assert(n > 0);
2648 if (n > 1) {
2649 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002650 if (cleanup == NULL)
2651 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002652 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002653 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002654 }
2655 for (i = 1; i < n; i++) {
2656 ADDOP(c, DUP_TOP);
2657 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002659 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002660 e->v.Compare.ops, i - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002661 ADDOP_JREL(c, JUMP_IF_FALSE, cleanup);
2662 NEXT_BLOCK(c);
2663 ADDOP(c, POP_TOP);
2664 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002665 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002666 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002667 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002668 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002670 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002671 if (n > 1) {
2672 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002673 if (end == NULL)
2674 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 ADDOP_JREL(c, JUMP_FORWARD, end);
2676 compiler_use_next_block(c, cleanup);
2677 ADDOP(c, ROT_TWO);
2678 ADDOP(c, POP_TOP);
2679 compiler_use_next_block(c, end);
2680 }
2681 return 1;
2682}
2683
2684static int
2685compiler_call(struct compiler *c, expr_ty e)
2686{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002687 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002688 return compiler_call_helper(c, 0,
2689 e->v.Call.args,
2690 e->v.Call.keywords,
2691 e->v.Call.starargs,
2692 e->v.Call.kwargs);
2693}
2694
2695/* shared code between compiler_call and compiler_class */
2696static int
2697compiler_call_helper(struct compiler *c,
2698 int n, /* Args already pushed */
2699 asdl_seq *args,
2700 asdl_seq *keywords,
2701 expr_ty starargs,
2702 expr_ty kwargs)
2703{
2704 int code = 0;
2705
2706 n += asdl_seq_LEN(args);
2707 VISIT_SEQ(c, expr, args);
2708 if (keywords) {
2709 VISIT_SEQ(c, keyword, keywords);
2710 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002712 if (starargs) {
2713 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 code |= 1;
2715 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002716 if (kwargs) {
2717 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 code |= 2;
2719 }
2720 switch (code) {
2721 case 0:
2722 ADDOP_I(c, CALL_FUNCTION, n);
2723 break;
2724 case 1:
2725 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2726 break;
2727 case 2:
2728 ADDOP_I(c, CALL_FUNCTION_KW, n);
2729 break;
2730 case 3:
2731 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2732 break;
2733 }
2734 return 1;
2735}
2736
Nick Coghlan650f0d02007-04-15 12:05:43 +00002737
2738/* List and set comprehensions and generator expressions work by creating a
2739 nested function to perform the actual iteration. This means that the
2740 iteration variables don't leak into the current scope.
2741 The defined function is called immediately following its definition, with the
2742 result of that call being the result of the expression.
2743 The LC/SC version returns the populated container, while the GE version is
2744 flagged in symtable.c as a generator, so it returns the generator object
2745 when the function is called.
2746 This code *knows* that the loop cannot contain break, continue, or return,
2747 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2748
2749 Possible cleanups:
2750 - iterate over the generator sequence instead of using recursion
2751*/
2752
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002754compiler_comprehension_generator(struct compiler *c, PyObject *tmpname,
2755 asdl_seq *generators, int gen_index,
2756 expr_ty elt, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757{
2758 /* generate code for the iterator, then each of the ifs,
2759 and then write to the element */
2760
Nick Coghlan650f0d02007-04-15 12:05:43 +00002761 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002763 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002764
2765 start = compiler_new_block(c);
2766 skip = compiler_new_block(c);
2767 if_cleanup = compiler_new_block(c);
2768 anchor = compiler_new_block(c);
2769
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002770 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002771 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773
Nick Coghlan650f0d02007-04-15 12:05:43 +00002774 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776 if (gen_index == 0) {
2777 /* Receive outermost iter as an implicit argument */
2778 c->u->u_argcount = 1;
2779 ADDOP_I(c, LOAD_FAST, 0);
2780 }
2781 else {
2782 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002783 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002784 ADDOP(c, GET_ITER);
2785 }
2786 compiler_use_next_block(c, start);
2787 ADDOP_JREL(c, FOR_ITER, anchor);
2788 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002789 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002790
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002791 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002794 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002795 VISIT(c, expr, e);
2796 ADDOP_JREL(c, JUMP_IF_FALSE, if_cleanup);
2797 NEXT_BLOCK(c);
2798 ADDOP(c, POP_TOP);
2799 }
2800
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002801 if (++gen_index < asdl_seq_LEN(generators))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002802 if (!compiler_comprehension_generator(c, tmpname,
2803 generators, gen_index,
2804 elt, type))
2805 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806
Nick Coghlan650f0d02007-04-15 12:05:43 +00002807 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002808 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002809 /* comprehension specific code */
2810 switch (type) {
2811 case COMP_GENEXP:
2812 VISIT(c, expr, elt);
2813 ADDOP(c, YIELD_VALUE);
2814 ADDOP(c, POP_TOP);
2815 break;
2816 case COMP_LISTCOMP:
2817 if (!compiler_nameop(c, tmpname, Load))
2818 return 0;
2819 VISIT(c, expr, elt);
2820 ADDOP(c, LIST_APPEND);
2821 break;
2822 case COMP_SETCOMP:
2823 if (!compiler_nameop(c, tmpname, Load))
2824 return 0;
2825 VISIT(c, expr, elt);
2826 ADDOP(c, SET_ADD);
2827 break;
2828 default:
2829 return 0;
2830 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
2832 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002833 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834 for (i = 0; i < n; i++) {
2835 ADDOP_I(c, JUMP_FORWARD, 1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002836 if (i == 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 compiler_use_next_block(c, if_cleanup);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002838
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 ADDOP(c, POP_TOP);
2840 }
2841 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2842 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
2844 return 1;
2845}
2846
2847static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002848compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2849 asdl_seq *generators, expr_ty elt)
2850{
2851 PyCodeObject *co = NULL;
2852 identifier tmp = NULL;
2853 expr_ty outermost_iter;
2854
2855 outermost_iter = ((comprehension_ty)
2856 asdl_seq_GET(generators, 0))->iter;
2857
2858 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2859 goto error;
2860
2861 if (type != COMP_GENEXP) {
2862 tmp = compiler_new_tmpname(c);
2863 if (!tmp)
2864 goto error_in_scope;
2865
2866 ADDOP_I(c, (type == COMP_LISTCOMP ?
2867 BUILD_LIST : BUILD_SET), 0);
2868 ADDOP(c, DUP_TOP);
2869 if (!compiler_nameop(c, tmp, Store))
2870 goto error_in_scope;
2871 }
2872
2873 if (!compiler_comprehension_generator(c, tmp, generators, 0, elt, type))
2874 goto error_in_scope;
2875
2876 if (type != COMP_GENEXP) {
2877 ADDOP(c, RETURN_VALUE);
2878 }
2879
2880 co = assemble(c, 1);
2881 compiler_exit_scope(c);
2882 if (co == NULL)
2883 goto error;
2884
2885 if (!compiler_make_closure(c, co, 0))
2886 goto error;
2887 Py_DECREF(co);
2888 Py_XDECREF(tmp);
2889
2890 VISIT(c, expr, outermost_iter);
2891 ADDOP(c, GET_ITER);
2892 ADDOP_I(c, CALL_FUNCTION, 1);
2893 return 1;
2894error_in_scope:
2895 compiler_exit_scope(c);
2896error:
2897 Py_XDECREF(co);
2898 Py_XDECREF(tmp);
2899 return 0;
2900}
2901
2902static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002903compiler_genexp(struct compiler *c, expr_ty e)
2904{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002905 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002906 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002907 name = PyUnicode_FromString("<genexp>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002908 if (!name)
2909 return 0;
2910 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002911 assert(e->kind == GeneratorExp_kind);
2912 return compiler_comprehension(c, e, COMP_GENEXP, name,
2913 e->v.GeneratorExp.generators,
2914 e->v.GeneratorExp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002915}
2916
2917static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002918compiler_listcomp(struct compiler *c, expr_ty e)
2919{
2920 static identifier name;
2921 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002922 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002923 if (!name)
2924 return 0;
2925 }
2926 assert(e->kind == ListComp_kind);
2927 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2928 e->v.ListComp.generators,
2929 e->v.ListComp.elt);
2930}
2931
2932static int
2933compiler_setcomp(struct compiler *c, expr_ty e)
2934{
2935 static identifier name;
2936 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002937 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002938 if (!name)
2939 return 0;
2940 }
2941 assert(e->kind == SetComp_kind);
2942 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2943 e->v.SetComp.generators,
2944 e->v.SetComp.elt);
2945}
2946
2947
2948static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002949compiler_visit_keyword(struct compiler *c, keyword_ty k)
2950{
2951 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2952 VISIT(c, expr, k->value);
2953 return 1;
2954}
2955
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002956/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957 whether they are true or false.
2958
2959 Return values: 1 for true, 0 for false, -1 for non-constant.
2960 */
2961
2962static int
2963expr_constant(expr_ty e)
2964{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002965 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00002967 case Ellipsis_kind:
2968 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002969 case Num_kind:
2970 return PyObject_IsTrue(e->v.Num.n);
2971 case Str_kind:
2972 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002973 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002974 /* optimize away names that can't be reassigned */
2975 id = _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL);
2976 if (strcmp(id, "True") == 0) return 1;
2977 if (strcmp(id, "False") == 0) return 0;
2978 if (strcmp(id, "None") == 0) return 0;
2979 if (strcmp(id, "__debug__") == 0)
2980 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00002981 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982 default:
2983 return -1;
2984 }
2985}
2986
Guido van Rossumc2e20742006-02-27 22:32:47 +00002987/*
2988 Implements the with statement from PEP 343.
2989
2990 The semantics outlined in that PEP are as follows:
2991
2992 with EXPR as VAR:
2993 BLOCK
2994
2995 It is implemented roughly as:
2996
Thomas Wouters477c8d52006-05-27 19:21:47 +00002997 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002998 exit = context.__exit__ # not calling it
2999 value = context.__enter__()
3000 try:
3001 VAR = value # if VAR present in the syntax
3002 BLOCK
3003 finally:
3004 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003005 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003006 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003007 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003008 exit(*exc)
3009 */
3010static int
3011compiler_with(struct compiler *c, stmt_ty s)
3012{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003013 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003014 basicblock *block, *finally;
3015 identifier tmpexit, tmpvalue = NULL;
3016
3017 assert(s->kind == With_kind);
3018
Guido van Rossumc2e20742006-02-27 22:32:47 +00003019 if (!enter_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003020 enter_attr = PyUnicode_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003021 if (!enter_attr)
3022 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003023 }
3024 if (!exit_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003025 exit_attr = PyUnicode_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003026 if (!exit_attr)
3027 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003028 }
3029
3030 block = compiler_new_block(c);
3031 finally = compiler_new_block(c);
3032 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003033 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003034
3035 /* Create a temporary variable to hold context.__exit__ */
3036 tmpexit = compiler_new_tmpname(c);
3037 if (tmpexit == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003038 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003039 PyArena_AddPyObject(c->c_arena, tmpexit);
3040
3041 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003042 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003043 We need to do this rather than preserving it on the stack
3044 because SETUP_FINALLY remembers the stack level.
3045 We need to do the assignment *inside* the try/finally
3046 so that context.__exit__() is called when the assignment
3047 fails. But we need to call context.__enter__() *before*
3048 the try/finally so that if it fails we won't call
3049 context.__exit__().
3050 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003051 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003052 if (tmpvalue == NULL)
3053 return 0;
3054 PyArena_AddPyObject(c->c_arena, tmpvalue);
3055 }
3056
Thomas Wouters477c8d52006-05-27 19:21:47 +00003057 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003058 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003059
3060 /* Squirrel away context.__exit__ */
3061 ADDOP(c, DUP_TOP);
3062 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
3063 if (!compiler_nameop(c, tmpexit, Store))
3064 return 0;
3065
3066 /* Call context.__enter__() */
3067 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3068 ADDOP_I(c, CALL_FUNCTION, 0);
3069
3070 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003071 /* Store it in tmpvalue */
3072 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 return 0;
3074 }
3075 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003076 /* Discard result from context.__enter__() */
3077 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003078 }
3079
3080 /* Start the try block */
3081 ADDOP_JREL(c, SETUP_FINALLY, finally);
3082
3083 compiler_use_next_block(c, block);
3084 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003085 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003086 }
3087
3088 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003089 /* Bind saved result of context.__enter__() to VAR */
3090 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003091 !compiler_nameop(c, tmpvalue, Del))
3092 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003093 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003094 }
3095
3096 /* BLOCK code */
3097 VISIT_SEQ(c, stmt, s->v.With.body);
3098
3099 /* End of try block; start the finally block */
3100 ADDOP(c, POP_BLOCK);
3101 compiler_pop_fblock(c, FINALLY_TRY, block);
3102
3103 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3104 compiler_use_next_block(c, finally);
3105 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003106 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003107
3108 /* Finally block starts; push tmpexit and issue our magic opcode. */
3109 if (!compiler_nameop(c, tmpexit, Load) ||
3110 !compiler_nameop(c, tmpexit, Del))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003111 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003112 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003113
3114 /* Finally block ends. */
3115 ADDOP(c, END_FINALLY);
3116 compiler_pop_fblock(c, FINALLY_END, finally);
3117 return 1;
3118}
3119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120static int
3121compiler_visit_expr(struct compiler *c, expr_ty e)
3122{
3123 int i, n;
3124
Thomas Wouters89f507f2006-12-13 04:49:30 +00003125 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003126 set a new line number for the next instruction.
3127 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 if (e->lineno > c->u->u_lineno) {
3129 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003130 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003131 }
3132 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003133 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003134 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003135 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003136 VISIT(c, expr, e->v.BinOp.left);
3137 VISIT(c, expr, e->v.BinOp.right);
3138 ADDOP(c, binop(c, e->v.BinOp.op));
3139 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003140 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 VISIT(c, expr, e->v.UnaryOp.operand);
3142 ADDOP(c, unaryop(e->v.UnaryOp.op));
3143 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003144 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003146 case IfExp_kind:
3147 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003148 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 /* XXX get rid of arg? */
3150 ADDOP_I(c, BUILD_MAP, 0);
3151 n = asdl_seq_LEN(e->v.Dict.values);
3152 /* We must arrange things just right for STORE_SUBSCR.
3153 It wants the stack to look like (value) (dict) (key) */
3154 for (i = 0; i < n; i++) {
3155 ADDOP(c, DUP_TOP);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003156 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003157 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 ADDOP(c, ROT_TWO);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003160 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 ADDOP(c, STORE_SUBSCR);
3162 }
3163 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003164 case Set_kind:
3165 n = asdl_seq_LEN(e->v.Set.elts);
3166 VISIT_SEQ(c, expr, e->v.Set.elts);
3167 ADDOP_I(c, BUILD_SET, n);
3168 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003169 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003171 case ListComp_kind:
3172 return compiler_listcomp(c, e);
3173 case SetComp_kind:
3174 return compiler_setcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 case Yield_kind:
3176 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003177 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178 if (e->v.Yield.value) {
3179 VISIT(c, expr, e->v.Yield.value);
3180 }
3181 else {
3182 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3183 }
3184 ADDOP(c, YIELD_VALUE);
3185 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003186 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003187 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003188 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003190 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3192 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003193 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3195 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003196 case Bytes_kind:
3197 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3198 ADDOP(c, MAKE_BYTES);
3199 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003200 case Ellipsis_kind:
3201 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3202 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003204 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205 if (e->v.Attribute.ctx != AugStore)
3206 VISIT(c, expr, e->v.Attribute.value);
3207 switch (e->v.Attribute.ctx) {
3208 case AugLoad:
3209 ADDOP(c, DUP_TOP);
3210 /* Fall through to load */
3211 case Load:
3212 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3213 break;
3214 case AugStore:
3215 ADDOP(c, ROT_TWO);
3216 /* Fall through to save */
3217 case Store:
3218 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3219 break;
3220 case Del:
3221 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3222 break;
3223 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003224 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003225 PyErr_SetString(PyExc_SystemError,
3226 "param invalid in attribute expression");
3227 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003228 }
3229 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003230 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 switch (e->v.Subscript.ctx) {
3232 case AugLoad:
3233 VISIT(c, expr, e->v.Subscript.value);
3234 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3235 break;
3236 case Load:
3237 VISIT(c, expr, e->v.Subscript.value);
3238 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3239 break;
3240 case AugStore:
3241 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3242 break;
3243 case Store:
3244 VISIT(c, expr, e->v.Subscript.value);
3245 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3246 break;
3247 case Del:
3248 VISIT(c, expr, e->v.Subscript.value);
3249 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3250 break;
3251 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003252 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003253 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003254 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003255 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256 }
3257 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003258 case Starred_kind:
3259 switch (e->v.Starred.ctx) {
3260 case Store:
3261 /* In all legitimate cases, the Starred node was already replaced
3262 * by compiler_list/compiler_tuple. XXX: is that okay? */
3263 return compiler_error(c,
3264 "starred assignment target must be in a list or tuple");
3265 default:
3266 return compiler_error(c,
3267 "can use starred expression only as assignment target");
3268 }
3269 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003270 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003271 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3272 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003273 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003275 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276 return compiler_tuple(c, e);
3277 }
3278 return 1;
3279}
3280
3281static int
3282compiler_augassign(struct compiler *c, stmt_ty s)
3283{
3284 expr_ty e = s->v.AugAssign.target;
3285 expr_ty auge;
3286
3287 assert(s->kind == AugAssign_kind);
3288
3289 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003290 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003291 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003292 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003293 if (auge == NULL)
3294 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003295 VISIT(c, expr, auge);
3296 VISIT(c, expr, s->v.AugAssign.value);
3297 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3298 auge->v.Attribute.ctx = AugStore;
3299 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003300 break;
3301 case Subscript_kind:
3302 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003303 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003304 if (auge == NULL)
3305 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 VISIT(c, expr, auge);
3307 VISIT(c, expr, s->v.AugAssign.value);
3308 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003309 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003311 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003313 if (!compiler_nameop(c, e->v.Name.id, Load))
3314 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 VISIT(c, expr, s->v.AugAssign.value);
3316 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3317 return compiler_nameop(c, e->v.Name.id, Store);
3318 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003319 PyErr_Format(PyExc_SystemError,
3320 "invalid node type (%d) for augmented assignment",
3321 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003322 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003323 }
3324 return 1;
3325}
3326
3327static int
3328compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3329{
3330 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003331 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3332 PyErr_SetString(PyExc_SystemError,
3333 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003335 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003336 f = &c->u->u_fblock[c->u->u_nfblocks++];
3337 f->fb_type = t;
3338 f->fb_block = b;
3339 return 1;
3340}
3341
3342static void
3343compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3344{
3345 struct compiler_unit *u = c->u;
3346 assert(u->u_nfblocks > 0);
3347 u->u_nfblocks--;
3348 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3349 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3350}
3351
Thomas Wouters89f507f2006-12-13 04:49:30 +00003352static int
3353compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003354 int i;
3355 struct compiler_unit *u = c->u;
3356 for (i = 0; i < u->u_nfblocks; ++i) {
3357 if (u->u_fblock[i].fb_type == LOOP)
3358 return 1;
3359 }
3360 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003361}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362/* Raises a SyntaxError and returns 0.
3363 If something goes wrong, a different exception may be raised.
3364*/
3365
3366static int
3367compiler_error(struct compiler *c, const char *errstr)
3368{
3369 PyObject *loc;
3370 PyObject *u = NULL, *v = NULL;
3371
3372 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3373 if (!loc) {
3374 Py_INCREF(Py_None);
3375 loc = Py_None;
3376 }
3377 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3378 Py_None, loc);
3379 if (!u)
3380 goto exit;
3381 v = Py_BuildValue("(zO)", errstr, u);
3382 if (!v)
3383 goto exit;
3384 PyErr_SetObject(PyExc_SyntaxError, v);
3385 exit:
3386 Py_DECREF(loc);
3387 Py_XDECREF(u);
3388 Py_XDECREF(v);
3389 return 0;
3390}
3391
3392static int
3393compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003394 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003395{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003396 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003397
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003398 /* XXX this code is duplicated */
3399 switch (ctx) {
3400 case AugLoad: /* fall through to Load */
3401 case Load: op = BINARY_SUBSCR; break;
3402 case AugStore:/* fall through to Store */
3403 case Store: op = STORE_SUBSCR; break;
3404 case Del: op = DELETE_SUBSCR; break;
3405 case Param:
3406 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003407 "invalid %s kind %d in subscript\n",
3408 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003409 return 0;
3410 }
3411 if (ctx == AugLoad) {
3412 ADDOP_I(c, DUP_TOPX, 2);
3413 }
3414 else if (ctx == AugStore) {
3415 ADDOP(c, ROT_THREE);
3416 }
3417 ADDOP(c, op);
3418 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003419}
3420
3421static int
3422compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3423{
3424 int n = 2;
3425 assert(s->kind == Slice_kind);
3426
3427 /* only handles the cases where BUILD_SLICE is emitted */
3428 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003429 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430 }
3431 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003432 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003434
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003436 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003437 }
3438 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003439 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
3441
3442 if (s->v.Slice.step) {
3443 n++;
3444 VISIT(c, expr, s->v.Slice.step);
3445 }
3446 ADDOP_I(c, BUILD_SLICE, n);
3447 return 1;
3448}
3449
3450static int
3451compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3452{
3453 int op = 0, slice_offset = 0, stack_count = 0;
3454
3455 assert(s->v.Slice.step == NULL);
3456 if (s->v.Slice.lower) {
3457 slice_offset++;
3458 stack_count++;
3459 if (ctx != AugStore)
3460 VISIT(c, expr, s->v.Slice.lower);
3461 }
3462 if (s->v.Slice.upper) {
3463 slice_offset += 2;
3464 stack_count++;
3465 if (ctx != AugStore)
3466 VISIT(c, expr, s->v.Slice.upper);
3467 }
3468
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003469 if (ctx == AugLoad) {
3470 switch (stack_count) {
3471 case 0: ADDOP(c, DUP_TOP); break;
3472 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3473 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3474 }
3475 }
3476 else if (ctx == AugStore) {
3477 switch (stack_count) {
3478 case 0: ADDOP(c, ROT_TWO); break;
3479 case 1: ADDOP(c, ROT_THREE); break;
3480 case 2: ADDOP(c, ROT_FOUR); break;
3481 }
3482 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483
3484 switch (ctx) {
3485 case AugLoad: /* fall through to Load */
3486 case Load: op = SLICE; break;
3487 case AugStore:/* fall through to Store */
3488 case Store: op = STORE_SLICE; break;
3489 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003490 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003491 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003492 PyErr_SetString(PyExc_SystemError,
3493 "param invalid in simple slice");
3494 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003495 }
3496
3497 ADDOP(c, op + slice_offset);
3498 return 1;
3499}
3500
3501static int
3502compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3503 expr_context_ty ctx)
3504{
3505 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 case Slice_kind:
3507 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 case Index_kind:
3509 VISIT(c, expr, s->v.Index.value);
3510 break;
3511 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003512 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003513 PyErr_SetString(PyExc_SystemError,
3514 "extended slice invalid in nested slice");
3515 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516 }
3517 return 1;
3518}
3519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003520static int
3521compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3522{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003523 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003525 case Index_kind:
3526 kindname = "index";
3527 if (ctx != AugStore) {
3528 VISIT(c, expr, s->v.Index.value);
3529 }
3530 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003531 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003532 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533 if (!s->v.Slice.step)
3534 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003535 if (ctx != AugStore) {
3536 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537 return 0;
3538 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003539 break;
3540 case ExtSlice_kind:
3541 kindname = "extended slice";
3542 if (ctx != AugStore) {
3543 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3544 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003545 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003546 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003547 if (!compiler_visit_nested_slice(c, sub, ctx))
3548 return 0;
3549 }
3550 ADDOP_I(c, BUILD_TUPLE, n);
3551 }
3552 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003553 default:
3554 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003555 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003556 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003557 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003558 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559}
3560
Thomas Wouters89f507f2006-12-13 04:49:30 +00003561/* End of the compiler section, beginning of the assembler section */
3562
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563/* do depth-first search of basic block graph, starting with block.
3564 post records the block indices in post-order.
3565
3566 XXX must handle implicit jumps from one block to next
3567*/
3568
Thomas Wouters89f507f2006-12-13 04:49:30 +00003569struct assembler {
3570 PyObject *a_bytecode; /* string containing bytecode */
3571 int a_offset; /* offset into bytecode */
3572 int a_nblocks; /* number of reachable blocks */
3573 basicblock **a_postorder; /* list of blocks in dfs postorder */
3574 PyObject *a_lnotab; /* string containing lnotab */
3575 int a_lnotab_off; /* offset into lnotab */
3576 int a_lineno; /* last lineno of emitted instruction */
3577 int a_lineno_off; /* bytecode offset of last lineno */
3578};
3579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580static void
3581dfs(struct compiler *c, basicblock *b, struct assembler *a)
3582{
3583 int i;
3584 struct instr *instr = NULL;
3585
3586 if (b->b_seen)
3587 return;
3588 b->b_seen = 1;
3589 if (b->b_next != NULL)
3590 dfs(c, b->b_next, a);
3591 for (i = 0; i < b->b_iused; i++) {
3592 instr = &b->b_instr[i];
3593 if (instr->i_jrel || instr->i_jabs)
3594 dfs(c, instr->i_target, a);
3595 }
3596 a->a_postorder[a->a_nblocks++] = b;
3597}
3598
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003599static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3601{
3602 int i;
3603 struct instr *instr;
3604 if (b->b_seen || b->b_startdepth >= depth)
3605 return maxdepth;
3606 b->b_seen = 1;
3607 b->b_startdepth = depth;
3608 for (i = 0; i < b->b_iused; i++) {
3609 instr = &b->b_instr[i];
3610 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3611 if (depth > maxdepth)
3612 maxdepth = depth;
3613 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3614 if (instr->i_jrel || instr->i_jabs) {
3615 maxdepth = stackdepth_walk(c, instr->i_target,
3616 depth, maxdepth);
3617 if (instr->i_opcode == JUMP_ABSOLUTE ||
3618 instr->i_opcode == JUMP_FORWARD) {
3619 goto out; /* remaining code is dead */
3620 }
3621 }
3622 }
3623 if (b->b_next)
3624 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3625out:
3626 b->b_seen = 0;
3627 return maxdepth;
3628}
3629
3630/* Find the flow path that needs the largest stack. We assume that
3631 * cycles in the flow graph have no net effect on the stack depth.
3632 */
3633static int
3634stackdepth(struct compiler *c)
3635{
3636 basicblock *b, *entryblock;
3637 entryblock = NULL;
3638 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3639 b->b_seen = 0;
3640 b->b_startdepth = INT_MIN;
3641 entryblock = b;
3642 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003643 if (!entryblock)
3644 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003645 return stackdepth_walk(c, entryblock, 0, 0);
3646}
3647
3648static int
3649assemble_init(struct assembler *a, int nblocks, int firstlineno)
3650{
3651 memset(a, 0, sizeof(struct assembler));
3652 a->a_lineno = firstlineno;
3653 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3654 if (!a->a_bytecode)
3655 return 0;
3656 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3657 if (!a->a_lnotab)
3658 return 0;
3659 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003660 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003661 if (!a->a_postorder) {
3662 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003664 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003665 return 1;
3666}
3667
3668static void
3669assemble_free(struct assembler *a)
3670{
3671 Py_XDECREF(a->a_bytecode);
3672 Py_XDECREF(a->a_lnotab);
3673 if (a->a_postorder)
3674 PyObject_Free(a->a_postorder);
3675}
3676
3677/* Return the size of a basic block in bytes. */
3678
3679static int
3680instrsize(struct instr *instr)
3681{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003682 if (!instr->i_hasarg)
3683 return 1;
3684 if (instr->i_oparg > 0xffff)
3685 return 6;
3686 return 3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687}
3688
3689static int
3690blocksize(basicblock *b)
3691{
3692 int i;
3693 int size = 0;
3694
3695 for (i = 0; i < b->b_iused; i++)
3696 size += instrsize(&b->b_instr[i]);
3697 return size;
3698}
3699
3700/* All about a_lnotab.
3701
3702c_lnotab is an array of unsigned bytes disguised as a Python string.
3703It is used to map bytecode offsets to source code line #s (when needed
3704for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003705
Tim Peters2a7f3842001-06-09 09:26:21 +00003706The array is conceptually a list of
3707 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003708pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003709
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003710 byte code offset source code line number
3711 0 1
3712 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003713 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003714 350 307
3715 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003716
3717The first trick is that these numbers aren't stored, only the increments
3718from one row to the next (this doesn't really work, but it's a start):
3719
3720 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3721
3722The second trick is that an unsigned byte can't hold negative values, or
3723values larger than 255, so (a) there's a deep assumption that byte code
3724offsets and their corresponding line #s both increase monotonically, and (b)
3725if at least one column jumps by more than 255 from one row to the next, more
3726than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003727from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003728part. A user of c_lnotab desiring to find the source line number
3729corresponding to a bytecode address A should do something like this
3730
3731 lineno = addr = 0
3732 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003733 addr += addr_incr
3734 if addr > A:
3735 return lineno
3736 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003737
3738In order for this to work, when the addr field increments by more than 255,
3739the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003740increment is < 256. So, in the example above, assemble_lnotab (it used
3741to be called com_set_lineno) should not (as was actually done until 2.2)
3742expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003743 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003744*/
3745
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003746static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003748{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 int d_bytecode, d_lineno;
3750 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003751 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752
3753 d_bytecode = a->a_offset - a->a_lineno_off;
3754 d_lineno = i->i_lineno - a->a_lineno;
3755
3756 assert(d_bytecode >= 0);
3757 assert(d_lineno >= 0);
3758
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003759 /* XXX(nnorwitz): is there a better way to handle this?
3760 for loops are special, we want to be able to trace them
3761 each time around, so we need to set an extra line number. */
3762 if (d_lineno == 0 && i->i_opcode != FOR_ITER)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003763 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003764
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003766 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 nbytes = a->a_lnotab_off + 2 * ncodes;
3768 len = PyString_GET_SIZE(a->a_lnotab);
3769 if (nbytes >= len) {
3770 if (len * 2 < nbytes)
3771 len = nbytes;
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003772 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003773 len *= 2;
3774 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3775 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003776 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003777 lnotab = (unsigned char *)
3778 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003779 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003780 *lnotab++ = 255;
3781 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 d_bytecode -= ncodes * 255;
3784 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003785 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 assert(d_bytecode <= 255);
3787 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003788 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003789 nbytes = a->a_lnotab_off + 2 * ncodes;
3790 len = PyString_GET_SIZE(a->a_lnotab);
3791 if (nbytes >= len) {
3792 if (len * 2 < nbytes)
3793 len = nbytes;
Guido van Rossum635abd21997-01-06 22:56:52 +00003794 else
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795 len *= 2;
3796 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3797 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003798 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003799 lnotab = (unsigned char *)
3800 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003802 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003804 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003805 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003806 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003807 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003808 d_lineno -= ncodes * 255;
3809 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003810 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003811
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 len = PyString_GET_SIZE(a->a_lnotab);
3813 if (a->a_lnotab_off + 2 >= len) {
3814 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003815 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003816 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003817 lnotab = (unsigned char *)
3818 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003820 a->a_lnotab_off += 2;
3821 if (d_bytecode) {
3822 *lnotab++ = d_bytecode;
3823 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003824 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003825 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003826 *lnotab++ = 0;
3827 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003828 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829 a->a_lineno = i->i_lineno;
3830 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003831 return 1;
3832}
3833
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003834/* assemble_emit()
3835 Extend the bytecode with a new instruction.
3836 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003837*/
3838
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003839static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003841{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003842 int size, arg = 0, ext = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003843 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003844 char *code;
3845
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003846 size = instrsize(i);
3847 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003849 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 if (a->a_offset + size >= len) {
3854 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003855 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003856 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3858 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003859 if (size == 6) {
3860 assert(i->i_hasarg);
3861 *code++ = (char)EXTENDED_ARG;
3862 *code++ = ext & 0xff;
3863 *code++ = ext >> 8;
3864 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003865 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003866 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003867 if (i->i_hasarg) {
3868 assert(size == 3 || size == 6);
3869 *code++ = arg & 0xff;
3870 *code++ = arg >> 8;
3871 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003872 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003873}
3874
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003875static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003876assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003877{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003879 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003880 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003881
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882 /* Compute the size of each block and fixup jump args.
3883 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003884start:
3885 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003886 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003887 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003888 bsize = blocksize(b);
3889 b->b_offset = totsize;
3890 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003891 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003892 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003893 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3894 bsize = b->b_offset;
3895 for (i = 0; i < b->b_iused; i++) {
3896 struct instr *instr = &b->b_instr[i];
3897 /* Relative jumps are computed relative to
3898 the instruction pointer after fetching
3899 the jump instruction.
3900 */
3901 bsize += instrsize(instr);
3902 if (instr->i_jabs)
3903 instr->i_oparg = instr->i_target->b_offset;
3904 else if (instr->i_jrel) {
3905 int delta = instr->i_target->b_offset - bsize;
3906 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003907 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003908 else
3909 continue;
3910 if (instr->i_oparg > 0xffff)
3911 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003912 }
3913 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003914
3915 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003916 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003917 with a better solution.
3918
3919 In the meantime, should the goto be dropped in favor
3920 of a loop?
3921
3922 The issue is that in the first loop blocksize() is called
3923 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003924 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003925 i_oparg is calculated in the second loop above.
3926
3927 So we loop until we stop seeing new EXTENDED_ARGs.
3928 The only EXTENDED_ARGs that could be popping up are
3929 ones in jump instructions. So this should converge
3930 fairly quickly.
3931 */
3932 if (last_extended_arg_count != extended_arg_count) {
3933 last_extended_arg_count = extended_arg_count;
3934 goto start;
3935 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003936}
3937
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003938static PyObject *
3939dict_keys_inorder(PyObject *dict, int offset)
3940{
3941 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003942 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003943
3944 tuple = PyTuple_New(size);
3945 if (tuple == NULL)
3946 return NULL;
3947 while (PyDict_Next(dict, &pos, &k, &v)) {
3948 i = PyInt_AS_LONG(v);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003949 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003950 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003951 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003952 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003953 PyTuple_SET_ITEM(tuple, i - offset, k);
3954 }
3955 return tuple;
3956}
3957
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003958static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003960{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003961 PySTEntryObject *ste = c->u->u_ste;
3962 int flags = 0, n;
3963 if (ste->ste_type != ModuleBlock)
3964 flags |= CO_NEWLOCALS;
3965 if (ste->ste_type == FunctionBlock) {
3966 if (!ste->ste_unoptimized)
3967 flags |= CO_OPTIMIZED;
3968 if (ste->ste_nested)
3969 flags |= CO_NESTED;
3970 if (ste->ste_generator)
3971 flags |= CO_GENERATOR;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003972 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 if (ste->ste_varargs)
3974 flags |= CO_VARARGS;
3975 if (ste->ste_varkeywords)
3976 flags |= CO_VARKEYWORDS;
Tim Peters5ca576e2001-06-18 22:08:13 +00003977 if (ste->ste_generator)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003978 flags |= CO_GENERATOR;
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003979
3980 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003981 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003982
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003983 n = PyDict_Size(c->u->u_freevars);
3984 if (n < 0)
3985 return -1;
3986 if (n == 0) {
3987 n = PyDict_Size(c->u->u_cellvars);
3988 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003989 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990 if (n == 0) {
3991 flags |= CO_NOFREE;
3992 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003993 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003994
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003995 return flags;
3996}
3997
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003998static PyCodeObject *
3999makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004000{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001 PyObject *tmp;
4002 PyCodeObject *co = NULL;
4003 PyObject *consts = NULL;
4004 PyObject *names = NULL;
4005 PyObject *varnames = NULL;
4006 PyObject *filename = NULL;
4007 PyObject *name = NULL;
4008 PyObject *freevars = NULL;
4009 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004010 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004011 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004012
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013 tmp = dict_keys_inorder(c->u->u_consts, 0);
4014 if (!tmp)
4015 goto error;
4016 consts = PySequence_List(tmp); /* optimize_code requires a list */
4017 Py_DECREF(tmp);
4018
4019 names = dict_keys_inorder(c->u->u_names, 0);
4020 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4021 if (!consts || !names || !varnames)
4022 goto error;
4023
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004024 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4025 if (!cellvars)
4026 goto error;
4027 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4028 if (!freevars)
4029 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 filename = PyString_FromString(c->c_filename);
4031 if (!filename)
4032 goto error;
4033
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004034 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004035 flags = compute_code_flags(c);
4036 if (flags < 0)
4037 goto error;
4038
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004039 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 if (!bytecode)
4041 goto error;
4042
4043 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4044 if (!tmp)
4045 goto error;
4046 Py_DECREF(consts);
4047 consts = tmp;
4048
Guido van Rossum4f72a782006-10-27 23:31:49 +00004049 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4050 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004051 bytecode, consts, names, varnames,
4052 freevars, cellvars,
4053 filename, c->u->u_name,
4054 c->u->u_firstlineno,
4055 a->a_lnotab);
4056 error:
4057 Py_XDECREF(consts);
4058 Py_XDECREF(names);
4059 Py_XDECREF(varnames);
4060 Py_XDECREF(filename);
4061 Py_XDECREF(name);
4062 Py_XDECREF(freevars);
4063 Py_XDECREF(cellvars);
4064 Py_XDECREF(bytecode);
4065 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004066}
4067
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004068
4069/* For debugging purposes only */
4070#if 0
4071static void
4072dump_instr(const struct instr *i)
4073{
4074 const char *jrel = i->i_jrel ? "jrel " : "";
4075 const char *jabs = i->i_jabs ? "jabs " : "";
4076 char arg[128];
4077
4078 *arg = '\0';
4079 if (i->i_hasarg)
4080 sprintf(arg, "arg: %d ", i->i_oparg);
4081
4082 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4083 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4084}
4085
4086static void
4087dump_basicblock(const basicblock *b)
4088{
4089 const char *seen = b->b_seen ? "seen " : "";
4090 const char *b_return = b->b_return ? "return " : "";
4091 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4092 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4093 if (b->b_instr) {
4094 int i;
4095 for (i = 0; i < b->b_iused; i++) {
4096 fprintf(stderr, " [%02d] ", i);
4097 dump_instr(b->b_instr + i);
4098 }
4099 }
4100}
4101#endif
4102
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004103static PyCodeObject *
4104assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004105{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004106 basicblock *b, *entryblock;
4107 struct assembler a;
4108 int i, j, nblocks;
4109 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004111 /* Make sure every block that falls off the end returns None.
4112 XXX NEXT_BLOCK() isn't quite right, because if the last
4113 block ends with a jump or return b_next shouldn't set.
4114 */
4115 if (!c->u->u_curblock->b_return) {
4116 NEXT_BLOCK(c);
4117 if (addNone)
4118 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4119 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004120 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122 nblocks = 0;
4123 entryblock = NULL;
4124 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4125 nblocks++;
4126 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004127 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004128
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004129 /* Set firstlineno if it wasn't explicitly set. */
4130 if (!c->u->u_firstlineno) {
4131 if (entryblock && entryblock->b_instr)
4132 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4133 else
4134 c->u->u_firstlineno = 1;
4135 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4137 goto error;
4138 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004139
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004141 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004143 /* Emit code in reverse postorder from dfs. */
4144 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004145 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146 for (j = 0; j < b->b_iused; j++)
4147 if (!assemble_emit(&a, &b->b_instr[j]))
4148 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004149 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4152 goto error;
4153 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
4154 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004156 co = makecode(c, &a);
4157 error:
4158 assemble_free(&a);
4159 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004160}