blob: 90cf065f9a39e25177a60c724c543a03a95400bd [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Jeremy Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Neal Norwitzf733a012006-10-29 18:30:10 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Jeremy Hylton819de6c2007-02-27 16:13:23 +000011 * this file.
Neal Norwitzf733a012006-10-29 18:30:10 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Neal Norwitzf733a012006-10-29 18:30:10 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Alexandre Vassalottib6465472010-01-11 22:36:12 +000042#define COMP_GENEXP 0
43#define COMP_SETCOMP 1
44#define COMP_DICTCOMP 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_ {
Jeremy Hylton12603c42006-04-01 16:18:02 +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 */
Neal Norwitzf733a012006-10-29 18:30:10 +0000114 /* Pointer to the most recently allocated block. By following b_list
115 members, you can reach all early allocated blocks. */
Jeremy Hylton12603c42006-04-01 16:18:02 +0000116 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000117 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118
119 int u_nfblocks;
120 struct fblockinfo u_fblock[CO_MAXBLOCKS];
121
122 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000123 int u_lineno; /* the lineno for the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124 bool u_lineno_set; /* boolean to indicate whether instr
125 has been generated with current lineno */
126};
127
128/* This struct captures the global state of a compilation.
129
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000130The u pointer points to the current compilation unit, while units
131for enclosing blocks are stored in c_stack. The u and c_stack are
132managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000133*/
134
135struct compiler {
136 const char *c_filename;
137 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000138 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000139 PyCompilerFlags *c_flags;
140
Neal Norwitz4ffedad2006-08-04 04:58:47 +0000141 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000142 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 struct compiler_unit *u; /* compiler state for current block */
145 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147};
148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149static int compiler_enter_scope(struct compiler *, identifier, void *, int);
150static void compiler_free(struct compiler *);
151static basicblock *compiler_new_block(struct compiler *);
152static int compiler_next_instr(struct compiler *, basicblock *);
153static int compiler_addop(struct compiler *, int);
154static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
155static int compiler_addop_i(struct compiler *, int, int);
156static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157static basicblock *compiler_use_new_block(struct compiler *);
158static int compiler_error(struct compiler *, const char *);
159static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
160
161static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
162static int compiler_visit_stmt(struct compiler *, stmt_ty);
163static int compiler_visit_keyword(struct compiler *, keyword_ty);
164static int compiler_visit_expr(struct compiler *, expr_ty);
165static int compiler_augassign(struct compiler *, stmt_ty);
166static int compiler_visit_slice(struct compiler *, slice_ty,
167 expr_context_ty);
168
169static int compiler_push_fblock(struct compiler *, enum fblocktype,
170 basicblock *);
171static void compiler_pop_fblock(struct compiler *, enum fblocktype,
172 basicblock *);
Jeremy Hylton82271f12006-10-04 02:24:52 +0000173/* Returns true if there is a loop on the fblock stack. */
174static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175
176static int inplace_binop(struct compiler *, operator_ty);
177static int expr_constant(expr_ty e);
178
Guido van Rossumc2e20742006-02-27 22:32:47 +0000179static int compiler_with(struct compiler *, stmt_ty);
180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181static PyCodeObject *assemble(struct compiler *, int addNone);
182static PyObject *__doc__;
183
Larry Hastings402b73f2010-03-25 00:54:54 +0000184#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186PyObject *
Anthony Baxter7b782b62006-04-11 12:01:56 +0000187_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000188{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189 /* Name mangling: __private becomes _classname__private.
190 This is independent from how the name is used. */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000191 const char *p, *name = PyString_AsString(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000192 char *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193 size_t nlen, plen;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000194 if (privateobj == NULL || !PyString_Check(privateobj) ||
Neal Norwitz84167d02006-08-12 01:45:47 +0000195 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000196 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000198 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000199 p = PyString_AsString(privateobj);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 nlen = strlen(name);
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000201 /* Don't mangle __id__ or names with dots.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000202
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000203 The only time a name with a dot can occur is when
204 we are compiling an import statement that has a
205 package name.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000206
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000207 TODO(jhylton): Decide whether we want to support
208 mangling of the module name, e.g. __M.X.
209 */
Jeremy Hylton37075c52007-02-27 01:01:59 +0000210 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000211 || strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000212 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000214 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215 /* Strip leading underscores from class name */
216 while (*p == '_')
217 p++;
218 if (*p == '\0') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000219 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 plen = strlen(p);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000223
224 assert(1 <= PY_SSIZE_T_MAX - nlen);
225 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
226
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000227 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 if (!ident)
229 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000231 buffer = PyString_AS_STRING(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000232 buffer[0] = '_';
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 strncpy(buffer+1, p, plen);
234 strcpy(buffer+1+plen, name);
235 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000236}
237
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000238static int
239compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000240{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000241 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000242
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 c->c_stack = PyList_New(0);
244 if (!c->c_stack)
245 return 0;
246
247 return 1;
248}
249
250PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000252 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253{
254 struct compiler c;
255 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000256 PyCompilerFlags local_flags;
257 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000259 if (!__doc__) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000260 __doc__ = PyString_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000261 if (!__doc__)
262 return NULL;
263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264
265 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000266 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000268 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269 c.c_future = PyFuture_FromAST(mod, filename);
270 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000271 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000273 local_flags.cf_flags = 0;
274 flags = &local_flags;
275 }
276 merged = c.c_future->ff_features | flags->cf_flags;
277 c.c_future->ff_features = merged;
278 flags->cf_flags = merged;
279 c.c_flags = flags;
280 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281
282 c.c_st = PySymtable_Build(mod, filename, c.c_future);
283 if (c.c_st == NULL) {
284 if (!PyErr_Occurred())
285 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000286 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287 }
288
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000289 co = compiler_mod(&c, mod);
290
Thomas Wouters1175c432006-02-27 22:49:54 +0000291 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000293 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 return co;
295}
296
297PyCodeObject *
298PyNode_Compile(struct _node *n, const char *filename)
299{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000300 PyCodeObject *co = NULL;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000301 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000302 PyArena *arena = PyArena_New();
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000303 if (!arena)
304 return NULL;
305 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000306 if (mod)
307 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000308 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000309 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000310}
311
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000312static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000314{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315 if (c->c_st)
316 PySymtable_Free(c->c_st);
317 if (c->c_future)
Neal Norwitz14bc4e42006-04-10 06:57:06 +0000318 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000319 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000320}
321
Guido van Rossum79f25d91997-04-29 20:08:16 +0000322static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000324{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000325 Py_ssize_t i, n;
Georg Brandl5c170fd2006-03-17 19:03:25 +0000326 PyObject *v, *k;
327 PyObject *dict = PyDict_New();
328 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000329
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330 n = PyList_Size(list);
331 for (i = 0; i < n; i++) {
332 v = PyInt_FromLong(i);
333 if (!v) {
334 Py_DECREF(dict);
335 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000336 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000337 k = PyList_GET_ITEM(list, i);
Georg Brandl7784f122006-05-26 20:04:44 +0000338 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
340 Py_XDECREF(k);
341 Py_DECREF(v);
342 Py_DECREF(dict);
343 return NULL;
344 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000345 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000347 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348 return dict;
349}
350
351/* Return new dict containing names from src that match scope(s).
352
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000353src is a symbol table dictionary. If the scope of a name matches
354either scope_type or flag is set, insert it into the new dict. The
355values are integers, starting at offset and increasing by one for
356each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357*/
358
359static PyObject *
360dictbytype(PyObject *src, int scope_type, int flag, int offset)
361{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000362 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363 PyObject *k, *v, *dest = PyDict_New();
364
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000365 assert(offset >= 0);
366 if (dest == NULL)
367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368
369 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000370 /* XXX this should probably be a macro in symtable.h */
371 assert(PyInt_Check(v));
372 scope = (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000373
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000374 if (scope == scope_type || PyInt_AS_LONG(v) & flag) {
375 PyObject *tuple, *item = PyInt_FromLong(i);
376 if (item == NULL) {
377 Py_DECREF(dest);
378 return NULL;
379 }
380 i++;
Georg Brandl7784f122006-05-26 20:04:44 +0000381 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000382 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
383 Py_DECREF(item);
384 Py_DECREF(dest);
385 Py_XDECREF(tuple);
386 return NULL;
387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000388 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000389 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000391 }
392 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000393}
394
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395static void
396compiler_unit_check(struct compiler_unit *u)
397{
398 basicblock *block;
399 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Brett Cannona6c41bc2008-02-07 07:47:31 +0000400 assert((void *)block != (void *)0xcbcbcbcb);
401 assert((void *)block != (void *)0xfbfbfbfb);
402 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403 if (block->b_instr != NULL) {
404 assert(block->b_ialloc > 0);
405 assert(block->b_iused > 0);
406 assert(block->b_ialloc >= block->b_iused);
407 }
408 else {
409 assert (block->b_iused == 0);
410 assert (block->b_ialloc == 0);
411 }
412 }
413}
414
415static void
416compiler_unit_free(struct compiler_unit *u)
417{
418 basicblock *b, *next;
419
420 compiler_unit_check(u);
421 b = u->u_blocks;
422 while (b != NULL) {
423 if (b->b_instr)
424 PyObject_Free((void *)b->b_instr);
425 next = b->b_list;
426 PyObject_Free((void *)b);
427 b = next;
428 }
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000429 Py_CLEAR(u->u_ste);
430 Py_CLEAR(u->u_name);
431 Py_CLEAR(u->u_consts);
432 Py_CLEAR(u->u_names);
433 Py_CLEAR(u->u_varnames);
434 Py_CLEAR(u->u_freevars);
435 Py_CLEAR(u->u_cellvars);
436 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 PyObject_Free(u);
438}
439
440static int
441compiler_enter_scope(struct compiler *c, identifier name, void *key,
442 int lineno)
443{
444 struct compiler_unit *u;
445
Anthony Baxter7b782b62006-04-11 12:01:56 +0000446 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000447 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000448 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000449 PyErr_NoMemory();
450 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000451 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000452 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453 u->u_argcount = 0;
454 u->u_ste = PySymtable_Lookup(c->c_st, key);
455 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000456 compiler_unit_free(u);
457 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458 }
459 Py_INCREF(name);
460 u->u_name = name;
461 u->u_varnames = list2dict(u->u_ste->ste_varnames);
462 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Neal Norwitzd12bd012006-07-21 07:59:47 +0000463 if (!u->u_varnames || !u->u_cellvars) {
464 compiler_unit_free(u);
465 return 0;
466 }
467
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000469 PyDict_Size(u->u_cellvars));
Neal Norwitzd12bd012006-07-21 07:59:47 +0000470 if (!u->u_freevars) {
471 compiler_unit_free(u);
472 return 0;
473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474
475 u->u_blocks = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 u->u_nfblocks = 0;
477 u->u_firstlineno = lineno;
478 u->u_lineno = 0;
479 u->u_lineno_set = false;
480 u->u_consts = PyDict_New();
481 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000482 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 return 0;
484 }
485 u->u_names = PyDict_New();
486 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000487 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000488 return 0;
489 }
490
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000491 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
493 /* Push the old compiler_unit on the stack. */
494 if (c->u) {
Larry Hastings402b73f2010-03-25 00:54:54 +0000495 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
496 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
497 Py_XDECREF(capsule);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 return 0;
500 }
Larry Hastings402b73f2010-03-25 00:54:54 +0000501 Py_DECREF(capsule);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000502 u->u_private = c->u->u_private;
503 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 }
505 c->u = u;
506
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000507 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000508 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 return 0;
510
511 return 1;
512}
513
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000514static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515compiler_exit_scope(struct compiler *c)
516{
517 int n;
Larry Hastings402b73f2010-03-25 00:54:54 +0000518 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000520 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000521 compiler_unit_free(c->u);
522 /* Restore c->u to the parent unit. */
523 n = PyList_GET_SIZE(c->c_stack) - 1;
524 if (n >= 0) {
Larry Hastings402b73f2010-03-25 00:54:54 +0000525 capsule = PyList_GET_ITEM(c->c_stack, n);
526 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
Neal Norwitz87557cd2006-08-21 18:01:30 +0000527 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000528 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000530 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 compiler_unit_check(c->u);
532 }
533 else
534 c->u = NULL;
535
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536}
537
538/* Allocate a new block and return a pointer to it.
539 Returns NULL on error.
540*/
541
542static basicblock *
543compiler_new_block(struct compiler *c)
544{
545 basicblock *b;
546 struct compiler_unit *u;
547
548 u = c->u;
549 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000550 if (b == NULL) {
551 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554 memset((void *)b, 0, sizeof(basicblock));
Neal Norwitzf733a012006-10-29 18:30:10 +0000555 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556 b->b_list = u->u_blocks;
557 u->u_blocks = b;
558 return b;
559}
560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561static basicblock *
562compiler_use_new_block(struct compiler *c)
563{
564 basicblock *block = compiler_new_block(c);
565 if (block == NULL)
566 return NULL;
567 c->u->u_curblock = block;
568 return block;
569}
570
571static basicblock *
572compiler_next_block(struct compiler *c)
573{
574 basicblock *block = compiler_new_block(c);
575 if (block == NULL)
576 return NULL;
577 c->u->u_curblock->b_next = block;
578 c->u->u_curblock = block;
579 return block;
580}
581
582static basicblock *
583compiler_use_next_block(struct compiler *c, basicblock *block)
584{
585 assert(block != NULL);
586 c->u->u_curblock->b_next = block;
587 c->u->u_curblock = block;
588 return block;
589}
590
591/* Returns the offset of the next instruction in the current block's
592 b_instr array. Resizes the b_instr as necessary.
593 Returns -1 on failure.
Neal Norwitzf733a012006-10-29 18:30:10 +0000594*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595
596static int
597compiler_next_instr(struct compiler *c, basicblock *b)
598{
599 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000600 if (b->b_instr == NULL) {
Anthony Baxter7b782b62006-04-11 12:01:56 +0000601 b->b_instr = (struct instr *)PyObject_Malloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000602 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 if (b->b_instr == NULL) {
604 PyErr_NoMemory();
605 return -1;
606 }
607 b->b_ialloc = DEFAULT_BLOCK_SIZE;
608 memset((char *)b->b_instr, 0,
609 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000610 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000611 else if (b->b_iused == b->b_ialloc) {
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000612 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 size_t oldsize, newsize;
614 oldsize = b->b_ialloc * sizeof(struct instr);
615 newsize = oldsize << 1;
Gregory P. Smith9d534572008-06-11 07:41:16 +0000616
617 if (oldsize > (PY_SIZE_MAX >> 1)) {
618 PyErr_NoMemory();
619 return -1;
620 }
621
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 if (newsize == 0) {
623 PyErr_NoMemory();
624 return -1;
625 }
626 b->b_ialloc <<= 1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000627 tmp = (struct instr *)PyObject_Realloc(
Jeremy Hylton819de6c2007-02-27 16:13:23 +0000628 (void *)b->b_instr, newsize);
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000629 if (tmp == NULL) {
630 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 return -1;
Neal Norwitzb59d08c2006-07-22 16:20:49 +0000632 }
633 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000634 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
635 }
636 return b->b_iused++;
637}
638
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000639/* Set the i_lineno member of the instruction at offset off if the
640 line number for the current expression/statement has not
Jeremy Hylton12603c42006-04-01 16:18:02 +0000641 already been set. If it has been set, the call has no effect.
642
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +0000643 The line number is reset in the following cases:
644 - when entering a new scope
645 - on each statement
646 - on each expression that start a new line
647 - before the "except" clause
648 - before the "for" and "while" expressions
Neal Norwitzf733a012006-10-29 18:30:10 +0000649*/
Jeremy Hylton12603c42006-04-01 16:18:02 +0000650
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651static void
652compiler_set_lineno(struct compiler *c, int off)
653{
654 basicblock *b;
655 if (c->u->u_lineno_set)
656 return;
657 c->u->u_lineno_set = true;
658 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000659 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660}
661
662static int
663opcode_stack_effect(int opcode, int oparg)
664{
665 switch (opcode) {
666 case POP_TOP:
667 return -1;
668 case ROT_TWO:
669 case ROT_THREE:
670 return 0;
671 case DUP_TOP:
672 return 1;
673 case ROT_FOUR:
674 return 0;
675
676 case UNARY_POSITIVE:
677 case UNARY_NEGATIVE:
678 case UNARY_NOT:
679 case UNARY_CONVERT:
680 case UNARY_INVERT:
681 return 0;
682
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000683 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000684 case LIST_APPEND:
Antoine Pitroud0c35152008-12-17 00:38:28 +0000685 return -1;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000686
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000687 case MAP_ADD:
688 return -2;
689
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000690 case BINARY_POWER:
691 case BINARY_MULTIPLY:
692 case BINARY_DIVIDE:
693 case BINARY_MODULO:
694 case BINARY_ADD:
695 case BINARY_SUBTRACT:
696 case BINARY_SUBSCR:
697 case BINARY_FLOOR_DIVIDE:
698 case BINARY_TRUE_DIVIDE:
699 return -1;
700 case INPLACE_FLOOR_DIVIDE:
701 case INPLACE_TRUE_DIVIDE:
702 return -1;
703
704 case SLICE+0:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000705 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706 case SLICE+1:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 return -1;
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000708 case SLICE+2:
709 return -1;
710 case SLICE+3:
711 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
713 case STORE_SLICE+0:
714 return -2;
715 case STORE_SLICE+1:
716 return -3;
717 case STORE_SLICE+2:
718 return -3;
719 case STORE_SLICE+3:
720 return -4;
721
722 case DELETE_SLICE+0:
723 return -1;
724 case DELETE_SLICE+1:
725 return -2;
726 case DELETE_SLICE+2:
727 return -2;
728 case DELETE_SLICE+3:
729 return -3;
730
731 case INPLACE_ADD:
732 case INPLACE_SUBTRACT:
733 case INPLACE_MULTIPLY:
734 case INPLACE_DIVIDE:
735 case INPLACE_MODULO:
736 return -1;
737 case STORE_SUBSCR:
738 return -3;
Raymond Hettingereffde122007-12-18 18:26:18 +0000739 case STORE_MAP:
740 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741 case DELETE_SUBSCR:
742 return -2;
743
744 case BINARY_LSHIFT:
745 case BINARY_RSHIFT:
746 case BINARY_AND:
747 case BINARY_XOR:
748 case BINARY_OR:
749 return -1;
750 case INPLACE_POWER:
751 return -1;
752 case GET_ITER:
753 return 0;
754
755 case PRINT_EXPR:
756 return -1;
757 case PRINT_ITEM:
758 return -1;
759 case PRINT_NEWLINE:
760 return 0;
761 case PRINT_ITEM_TO:
762 return -2;
763 case PRINT_NEWLINE_TO:
764 return -1;
765 case INPLACE_LSHIFT:
766 case INPLACE_RSHIFT:
767 case INPLACE_AND:
768 case INPLACE_XOR:
769 case INPLACE_OR:
770 return -1;
771 case BREAK_LOOP:
772 return 0;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000773 case SETUP_WITH:
Benjamin Peterson49a6b0e2009-05-25 20:12:57 +0000774 return 4;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000775 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000776 return -1; /* XXX Sometimes more */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777 case LOAD_LOCALS:
778 return 1;
779 case RETURN_VALUE:
780 return -1;
781 case IMPORT_STAR:
782 return -1;
783 case EXEC_STMT:
784 return -3;
785 case YIELD_VALUE:
786 return 0;
787
788 case POP_BLOCK:
789 return 0;
790 case END_FINALLY:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000791 return -3; /* or -1 or -2 if no exception occurred or
792 return/break/continue */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793 case BUILD_CLASS:
794 return -2;
795
796 case STORE_NAME:
797 return -1;
798 case DELETE_NAME:
799 return 0;
800 case UNPACK_SEQUENCE:
801 return oparg-1;
802 case FOR_ITER:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000803 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804
805 case STORE_ATTR:
806 return -2;
807 case DELETE_ATTR:
808 return -1;
809 case STORE_GLOBAL:
810 return -1;
811 case DELETE_GLOBAL:
812 return 0;
813 case DUP_TOPX:
814 return oparg;
815 case LOAD_CONST:
816 return 1;
817 case LOAD_NAME:
818 return 1;
819 case BUILD_TUPLE:
820 case BUILD_LIST:
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000821 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822 return 1-oparg;
823 case BUILD_MAP:
824 return 1;
825 case LOAD_ATTR:
826 return 0;
827 case COMPARE_OP:
828 return -1;
829 case IMPORT_NAME:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000830 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000831 case IMPORT_FROM:
832 return 1;
833
834 case JUMP_FORWARD:
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000835 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
836 case JUMP_IF_FALSE_OR_POP: /* "" */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000837 case JUMP_ABSOLUTE:
838 return 0;
839
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000840 case POP_JUMP_IF_FALSE:
841 case POP_JUMP_IF_TRUE:
842 return -1;
843
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844 case LOAD_GLOBAL:
845 return 1;
846
847 case CONTINUE_LOOP:
848 return 0;
849 case SETUP_LOOP:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 case SETUP_EXCEPT:
851 case SETUP_FINALLY:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853
854 case LOAD_FAST:
855 return 1;
856 case STORE_FAST:
857 return -1;
858 case DELETE_FAST:
859 return 0;
860
861 case RAISE_VARARGS:
862 return -oparg;
863#define NARGS(o) (((o) % 256) + 2*((o) / 256))
864 case CALL_FUNCTION:
865 return -NARGS(oparg);
866 case CALL_FUNCTION_VAR:
867 case CALL_FUNCTION_KW:
868 return -NARGS(oparg)-1;
869 case CALL_FUNCTION_VAR_KW:
870 return -NARGS(oparg)-2;
871#undef NARGS
872 case MAKE_FUNCTION:
873 return -oparg;
874 case BUILD_SLICE:
875 if (oparg == 3)
876 return -2;
877 else
878 return -1;
879
880 case MAKE_CLOSURE:
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +0000881 return -oparg-1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882 case LOAD_CLOSURE:
883 return 1;
884 case LOAD_DEREF:
885 return 1;
886 case STORE_DEREF:
887 return -1;
888 default:
889 fprintf(stderr, "opcode = %d\n", opcode);
890 Py_FatalError("opcode_stack_effect()");
891
892 }
893 return 0; /* not reachable */
894}
895
896/* Add an opcode with no argument.
897 Returns 0 on failure, 1 on success.
898*/
899
900static int
901compiler_addop(struct compiler *c, int opcode)
902{
903 basicblock *b;
904 struct instr *i;
905 int off;
906 off = compiler_next_instr(c, c->u->u_curblock);
907 if (off < 0)
908 return 0;
909 b = c->u->u_curblock;
910 i = &b->b_instr[off];
911 i->i_opcode = opcode;
912 i->i_hasarg = 0;
913 if (opcode == RETURN_VALUE)
914 b->b_return = 1;
915 compiler_set_lineno(c, off);
916 return 1;
917}
918
919static int
920compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
921{
922 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000923 Py_ssize_t arg;
Mark Dickinson105be772008-01-31 22:17:37 +0000924 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000926 /* necessary to make sure types aren't coerced (e.g., int and long) */
Alex Martellid8672aa2007-08-22 21:14:17 +0000927 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
928 if (PyFloat_Check(o)) {
Mark Dickinson105be772008-01-31 22:17:37 +0000929 d = PyFloat_AS_DOUBLE(o);
Mark Dickinson105be772008-01-31 22:17:37 +0000930 /* all we need is to make the tuple different in either the 0.0
931 * or -0.0 case from all others, just to avoid the "coercion".
932 */
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000933 if (d == 0.0 && copysign(1.0, d) < 0.0)
Mark Dickinson105be772008-01-31 22:17:37 +0000934 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
935 else
936 t = PyTuple_Pack(2, o, o->ob_type);
937 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000938#ifndef WITHOUT_COMPLEX
Mark Dickinson105be772008-01-31 22:17:37 +0000939 else if (PyComplex_Check(o)) {
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000940 Py_complex z;
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000941 int real_negzero, imag_negzero;
942 /* For the complex case we must make complex(x, 0.)
943 different from complex(x, -0.) and complex(0., y)
944 different from complex(-0., y), for any x and y.
945 All four complex zeros must be distinguished.*/
Mark Dickinson105be772008-01-31 22:17:37 +0000946 z = PyComplex_AsCComplex(o);
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000947 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
948 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
949 if (real_negzero && imag_negzero) {
950 t = PyTuple_Pack(5, o, o->ob_type,
951 Py_None, Py_None, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000952 }
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000953 else if (imag_negzero) {
954 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000955 }
Mark Dickinsonccc690d2009-11-28 16:32:27 +0000956 else if (real_negzero) {
957 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
Mark Dickinson105be772008-01-31 22:17:37 +0000958 }
959 else {
960 t = PyTuple_Pack(2, o, o->ob_type);
961 }
962 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000963#endif /* WITHOUT_COMPLEX */
Mark Dickinson105be772008-01-31 22:17:37 +0000964 else {
965 t = PyTuple_Pack(2, o, o->ob_type);
Alex Martellid8672aa2007-08-22 21:14:17 +0000966 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000967 if (t == NULL)
Mark Dickinson105be772008-01-31 22:17:37 +0000968 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969
970 v = PyDict_GetItem(dict, t);
971 if (!v) {
972 arg = PyDict_Size(dict);
973 v = PyInt_FromLong(arg);
974 if (!v) {
975 Py_DECREF(t);
976 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 if (PyDict_SetItem(dict, t, v) < 0) {
979 Py_DECREF(t);
980 Py_DECREF(v);
981 return -1;
982 }
983 Py_DECREF(v);
984 }
985 else
986 arg = PyInt_AsLong(v);
987 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000988 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989}
990
991static int
992compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
993 PyObject *o)
994{
995 int arg = compiler_add_o(c, dict, o);
996 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000997 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return compiler_addop_i(c, opcode, arg);
999}
1000
1001static int
1002compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001003 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004{
1005 int arg;
1006 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1007 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 arg = compiler_add_o(c, dict, mangled);
1010 Py_DECREF(mangled);
1011 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001012 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return compiler_addop_i(c, opcode, arg);
1014}
1015
1016/* Add an opcode with an integer argument.
1017 Returns 0 on failure, 1 on success.
1018*/
1019
1020static int
1021compiler_addop_i(struct compiler *c, int opcode, int oparg)
1022{
1023 struct instr *i;
1024 int off;
1025 off = compiler_next_instr(c, c->u->u_curblock);
1026 if (off < 0)
1027 return 0;
1028 i = &c->u->u_curblock->b_instr[off];
1029 i->i_opcode = opcode;
1030 i->i_oparg = oparg;
1031 i->i_hasarg = 1;
1032 compiler_set_lineno(c, off);
1033 return 1;
1034}
1035
1036static int
1037compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1038{
1039 struct instr *i;
1040 int off;
1041
1042 assert(b != NULL);
1043 off = compiler_next_instr(c, c->u->u_curblock);
1044 if (off < 0)
1045 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 i = &c->u->u_curblock->b_instr[off];
1047 i->i_opcode = opcode;
1048 i->i_target = b;
1049 i->i_hasarg = 1;
1050 if (absolute)
1051 i->i_jabs = 1;
1052 else
1053 i->i_jrel = 1;
Jeremy Hylton12603c42006-04-01 16:18:02 +00001054 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 return 1;
1056}
1057
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001058/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1059 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 it as the current block. NEXT_BLOCK() also creates an implicit jump
1061 from the current block to the new block.
1062*/
1063
Neal Norwitzf733a012006-10-29 18:30:10 +00001064/* The returns inside these macros make it impossible to decref objects
1065 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066*/
1067
1068
1069#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001070 if (compiler_use_new_block((C)) == NULL) \
1071 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
1073
1074#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001075 if (compiler_next_block((C)) == NULL) \
1076 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
1079#define ADDOP(C, OP) { \
1080 if (!compiler_addop((C), (OP))) \
1081 return 0; \
1082}
1083
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001084#define ADDOP_IN_SCOPE(C, OP) { \
1085 if (!compiler_addop((C), (OP))) { \
1086 compiler_exit_scope(c); \
1087 return 0; \
1088 } \
1089}
1090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091#define ADDOP_O(C, OP, O, TYPE) { \
1092 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1093 return 0; \
1094}
1095
1096#define ADDOP_NAME(C, OP, O, TYPE) { \
1097 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1098 return 0; \
1099}
1100
1101#define ADDOP_I(C, OP, O) { \
1102 if (!compiler_addop_i((C), (OP), (O))) \
1103 return 0; \
1104}
1105
1106#define ADDOP_JABS(C, OP, O) { \
1107 if (!compiler_addop_j((C), (OP), (O), 1)) \
1108 return 0; \
1109}
1110
1111#define ADDOP_JREL(C, OP, O) { \
1112 if (!compiler_addop_j((C), (OP), (O), 0)) \
1113 return 0; \
1114}
1115
1116/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1117 the ASDL name to synthesize the name of the C type and the visit function.
1118*/
1119
1120#define VISIT(C, TYPE, V) {\
1121 if (!compiler_visit_ ## TYPE((C), (V))) \
1122 return 0; \
1123}
1124
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001125#define VISIT_IN_SCOPE(C, TYPE, V) {\
1126 if (!compiler_visit_ ## TYPE((C), (V))) { \
1127 compiler_exit_scope(c); \
1128 return 0; \
1129 } \
1130}
1131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132#define VISIT_SLICE(C, V, CTX) {\
1133 if (!compiler_visit_slice((C), (V), (CTX))) \
1134 return 0; \
1135}
1136
1137#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001138 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001142 if (!compiler_visit_ ## TYPE((C), elt)) \
1143 return 0; \
1144 } \
1145}
1146
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001147#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001148 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001149 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001150 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001151 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001152 if (!compiler_visit_ ## TYPE((C), elt)) { \
1153 compiler_exit_scope(c); \
1154 return 0; \
1155 } \
1156 } \
1157}
1158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159static int
1160compiler_isdocstring(stmt_ty s)
1161{
1162 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001163 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return s->v.Expr.value->kind == Str_kind;
1165}
1166
1167/* Compile a sequence of statements, checking for a docstring. */
1168
1169static int
1170compiler_body(struct compiler *c, asdl_seq *stmts)
1171{
1172 int i = 0;
1173 stmt_ty st;
1174
1175 if (!asdl_seq_LEN(stmts))
1176 return 1;
Anthony Baxter7b782b62006-04-11 12:01:56 +00001177 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandla5ea6892007-06-01 11:33:33 +00001178 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1179 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 i = 1;
1181 VISIT(c, expr, st->v.Expr.value);
1182 if (!compiler_nameop(c, __doc__, Store))
1183 return 0;
1184 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001185 for (; i < asdl_seq_LEN(stmts); i++)
Anthony Baxter7b782b62006-04-11 12:01:56 +00001186 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return 1;
1188}
1189
1190static PyCodeObject *
1191compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001192{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001194 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 static PyObject *module;
1196 if (!module) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001197 module = PyString_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 if (!module)
1199 return NULL;
1200 }
Neal Norwitzed657552006-07-10 00:04:44 +00001201 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1202 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 switch (mod->kind) {
1205 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001206 if (!compiler_body(c, mod->v.Module.body)) {
1207 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 break;
1211 case Interactive_kind:
1212 c->c_interactive = 1;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001213 VISIT_SEQ_IN_SCOPE(c, stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001214 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 break;
1216 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001217 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001218 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 break;
1220 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001221 PyErr_SetString(PyExc_SystemError,
1222 "suite should not be possible");
1223 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001224 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001225 PyErr_Format(PyExc_SystemError,
1226 "module kind %d should not be possible",
1227 mod->kind);
1228 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001229 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 co = assemble(c, addNone);
1231 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001232 return co;
1233}
1234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235/* The test for LOCAL must come before the test for FREE in order to
1236 handle classes where name is both local and free. The local var is
1237 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001238*/
1239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240static int
1241get_ref_type(struct compiler *c, PyObject *name)
1242{
1243 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001244 if (scope == 0) {
1245 char buf[350];
1246 PyOS_snprintf(buf, sizeof(buf),
1247 "unknown scope for %.100s in %.100s(%s) in %s\n"
Amaury Forgeot d'Arc59ce0422009-01-17 20:18:59 +00001248 "symbols: %s\nlocals: %s\nglobals: %s",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001249 PyString_AS_STRING(name),
1250 PyString_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001251 PyObject_REPR(c->u->u_ste->ste_id),
1252 c->c_filename,
1253 PyObject_REPR(c->u->u_ste->ste_symbols),
1254 PyObject_REPR(c->u->u_varnames),
1255 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001257 Py_FatalError(buf);
1258 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001259
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001260 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261}
1262
1263static int
1264compiler_lookup_arg(PyObject *dict, PyObject *name)
1265{
1266 PyObject *k, *v;
Georg Brandl7784f122006-05-26 20:04:44 +00001267 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001271 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001273 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274 return PyInt_AS_LONG(v);
1275}
1276
1277static int
1278compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1279{
1280 int i, free = PyCode_GetNumFree(co);
1281 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001282 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1283 ADDOP_I(c, MAKE_FUNCTION, args);
1284 return 1;
1285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 for (i = 0; i < free; ++i) {
1287 /* Bypass com_addop_varname because it will generate
1288 LOAD_DEREF but LOAD_CLOSURE is needed.
1289 */
1290 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1291 int arg, reftype;
1292
1293 /* Special case: If a class contains a method with a
1294 free variable that has the same name as a method,
1295 the name will be considered free *and* local in the
1296 class. It should be handled by the closure, as
1297 well as by the normal name loookup logic.
1298 */
1299 reftype = get_ref_type(c, name);
1300 if (reftype == CELL)
1301 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1302 else /* (reftype == FREE) */
1303 arg = compiler_lookup_arg(c->u->u_freevars, name);
1304 if (arg == -1) {
1305 printf("lookup %s in %s %d %d\n"
1306 "freevars of %s: %s\n",
1307 PyObject_REPR(name),
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001308 PyString_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309 reftype, arg,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001310 PyString_AS_STRING(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001311 PyObject_REPR(co->co_freevars));
1312 Py_FatalError("compiler_make_closure()");
1313 }
1314 ADDOP_I(c, LOAD_CLOSURE, arg);
1315 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001316 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001318 ADDOP_I(c, MAKE_CLOSURE, args);
1319 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
1322static int
1323compiler_decorators(struct compiler *c, asdl_seq* decos)
1324{
1325 int i;
1326
1327 if (!decos)
1328 return 1;
1329
1330 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001331 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332 }
1333 return 1;
1334}
1335
1336static int
1337compiler_arguments(struct compiler *c, arguments_ty args)
1338{
1339 int i;
1340 int n = asdl_seq_LEN(args->args);
1341 /* Correctly handle nested argument lists */
1342 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001343 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344 if (arg->kind == Tuple_kind) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001345 PyObject *id = PyString_FromFormat(".%d", i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001346 if (id == NULL) {
1347 return 0;
1348 }
1349 if (!compiler_nameop(c, id, Load)) {
1350 Py_DECREF(id);
1351 return 0;
1352 }
1353 Py_DECREF(id);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001354 VISIT(c, expr, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 }
1356 }
1357 return 1;
1358}
1359
1360static int
1361compiler_function(struct compiler *c, stmt_ty s)
1362{
1363 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001364 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001365 arguments_ty args = s->v.FunctionDef.args;
Christian Heimes5224d282008-02-23 15:01:05 +00001366 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001367 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 int i, n, docstring;
1369
1370 assert(s->kind == FunctionDef_kind);
1371
1372 if (!compiler_decorators(c, decos))
1373 return 0;
1374 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001375 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001376 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1377 s->lineno))
1378 return 0;
1379
Anthony Baxter7b782b62006-04-11 12:01:56 +00001380 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001381 docstring = compiler_isdocstring(st);
Georg Brandl5a5bc7b2007-09-19 06:37:19 +00001382 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001383 first_const = st->v.Expr.value->v.Str.s;
1384 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001385 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001386 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001389 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001390 compiler_arguments(c, args);
1391
1392 c->u->u_argcount = asdl_seq_LEN(args->args);
1393 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001394 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001395 for (i = docstring; i < n; i++) {
Neal Norwitz4ffedad2006-08-04 04:58:47 +00001396 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1397 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001398 }
1399 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001400 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001401 if (co == NULL)
1402 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001404 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001405 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406
1407 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1408 ADDOP_I(c, CALL_FUNCTION, 1);
1409 }
1410
1411 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1412}
1413
1414static int
1415compiler_class(struct compiler *c, stmt_ty s)
1416{
Christian Heimes5224d282008-02-23 15:01:05 +00001417 int n, i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001419 PyObject *str;
Christian Heimes5224d282008-02-23 15:01:05 +00001420 asdl_seq* decos = s->v.ClassDef.decorator_list;
1421
1422 if (!compiler_decorators(c, decos))
1423 return 0;
1424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425 /* push class name on stack, needed by BUILD_CLASS */
1426 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1427 /* push the tuple of base classes on the stack */
1428 n = asdl_seq_LEN(s->v.ClassDef.bases);
1429 if (n > 0)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001430 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431 ADDOP_I(c, BUILD_TUPLE, n);
1432 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1433 s->lineno))
1434 return 0;
Amaury Forgeot d'Arc69b747b2008-03-28 20:30:50 +00001435 Py_XDECREF(c->u->u_private);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001436 c->u->u_private = s->v.ClassDef.name;
1437 Py_INCREF(c->u->u_private);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001438 str = PyString_InternFromString("__name__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439 if (!str || !compiler_nameop(c, str, Load)) {
1440 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001441 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001442 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001443 }
1444
1445 Py_DECREF(str);
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001446 str = PyString_InternFromString("__module__");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447 if (!str || !compiler_nameop(c, str, Store)) {
1448 Py_XDECREF(str);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001449 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001451 }
1452 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001454 if (!compiler_body(c, s->v.ClassDef.body)) {
1455 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001457 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001459 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1460 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001462 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 if (co == NULL)
1464 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001465
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001466 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001467 Py_DECREF(co);
1468
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469 ADDOP_I(c, CALL_FUNCTION, 0);
1470 ADDOP(c, BUILD_CLASS);
Christian Heimes5224d282008-02-23 15:01:05 +00001471 /* apply decorators */
1472 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1473 ADDOP_I(c, CALL_FUNCTION, 1);
1474 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001475 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1476 return 0;
1477 return 1;
1478}
1479
1480static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001481compiler_ifexp(struct compiler *c, expr_ty e)
1482{
1483 basicblock *end, *next;
1484
1485 assert(e->kind == IfExp_kind);
1486 end = compiler_new_block(c);
1487 if (end == NULL)
1488 return 0;
1489 next = compiler_new_block(c);
1490 if (next == NULL)
1491 return 0;
1492 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001493 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001494 VISIT(c, expr, e->v.IfExp.body);
1495 ADDOP_JREL(c, JUMP_FORWARD, end);
1496 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001497 VISIT(c, expr, e->v.IfExp.orelse);
1498 compiler_use_next_block(c, end);
1499 return 1;
1500}
1501
1502static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503compiler_lambda(struct compiler *c, expr_ty e)
1504{
1505 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001506 static identifier name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507 arguments_ty args = e->v.Lambda.args;
1508 assert(e->kind == Lambda_kind);
1509
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001510 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001511 name = PyString_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001512 if (!name)
1513 return 0;
1514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515
1516 if (args->defaults)
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001517 VISIT_SEQ(c, expr, args->defaults);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1519 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001520
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001521 /* unpack nested arguments */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522 compiler_arguments(c, args);
Benjamin Peterson0dee9c12010-03-17 20:41:42 +00001523
1524 /* Make None the first constant, so the lambda can't have a
1525 docstring. */
1526 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1527 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528
1529 c->u->u_argcount = asdl_seq_LEN(args->args);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001530 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson8d5934b2008-12-27 18:24:11 +00001531 if (c->u->u_ste->ste_generator) {
1532 ADDOP_IN_SCOPE(c, POP_TOP);
1533 }
1534 else {
1535 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1536 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001538 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539 if (co == NULL)
1540 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001542 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
Neal Norwitz4737b232005-11-19 23:58:29 +00001543 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001544
1545 return 1;
1546}
1547
1548static int
1549compiler_print(struct compiler *c, stmt_ty s)
1550{
1551 int i, n;
1552 bool dest;
1553
1554 assert(s->kind == Print_kind);
1555 n = asdl_seq_LEN(s->v.Print.values);
1556 dest = false;
1557 if (s->v.Print.dest) {
1558 VISIT(c, expr, s->v.Print.dest);
1559 dest = true;
1560 }
1561 for (i = 0; i < n; i++) {
1562 expr_ty e = (expr_ty)asdl_seq_GET(s->v.Print.values, i);
1563 if (dest) {
1564 ADDOP(c, DUP_TOP);
1565 VISIT(c, expr, e);
1566 ADDOP(c, ROT_TWO);
1567 ADDOP(c, PRINT_ITEM_TO);
1568 }
1569 else {
1570 VISIT(c, expr, e);
1571 ADDOP(c, PRINT_ITEM);
1572 }
1573 }
1574 if (s->v.Print.nl) {
1575 if (dest)
1576 ADDOP(c, PRINT_NEWLINE_TO)
1577 else
1578 ADDOP(c, PRINT_NEWLINE)
1579 }
1580 else if (dest)
1581 ADDOP(c, POP_TOP);
1582 return 1;
1583}
1584
1585static int
1586compiler_if(struct compiler *c, stmt_ty s)
1587{
1588 basicblock *end, *next;
Georg Brandlddbaa662006-06-04 21:56:52 +00001589 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590 assert(s->kind == If_kind);
1591 end = compiler_new_block(c);
1592 if (end == NULL)
1593 return 0;
Georg Brandlddbaa662006-06-04 21:56:52 +00001594
1595 constant = expr_constant(s->v.If.test);
1596 /* constant = 0: "if 0"
1597 * constant = 1: "if 1", "if 2", ...
1598 * constant = -1: rest */
1599 if (constant == 0) {
1600 if (s->v.If.orelse)
1601 VISIT_SEQ(c, stmt, s->v.If.orelse);
1602 } else if (constant == 1) {
1603 VISIT_SEQ(c, stmt, s->v.If.body);
1604 } else {
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001605 if (s->v.If.orelse) {
1606 next = compiler_new_block(c);
1607 if (next == NULL)
1608 return 0;
1609 }
1610 else
1611 next = end;
Georg Brandlddbaa662006-06-04 21:56:52 +00001612 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001613 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Georg Brandlddbaa662006-06-04 21:56:52 +00001614 VISIT_SEQ(c, stmt, s->v.If.body);
1615 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001616 if (s->v.If.orelse) {
1617 compiler_use_next_block(c, next);
Jeremy Hylton819de6c2007-02-27 16:13:23 +00001618 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001619 }
Georg Brandlddbaa662006-06-04 21:56:52 +00001620 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621 compiler_use_next_block(c, end);
1622 return 1;
1623}
1624
1625static int
1626compiler_for(struct compiler *c, stmt_ty s)
1627{
1628 basicblock *start, *cleanup, *end;
1629
1630 start = compiler_new_block(c);
1631 cleanup = compiler_new_block(c);
1632 end = compiler_new_block(c);
1633 if (start == NULL || end == NULL || cleanup == NULL)
1634 return 0;
1635 ADDOP_JREL(c, SETUP_LOOP, end);
1636 if (!compiler_push_fblock(c, LOOP, start))
1637 return 0;
1638 VISIT(c, expr, s->v.For.iter);
1639 ADDOP(c, GET_ITER);
1640 compiler_use_next_block(c, start);
1641 ADDOP_JREL(c, FOR_ITER, cleanup);
1642 VISIT(c, expr, s->v.For.target);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001643 VISIT_SEQ(c, stmt, s->v.For.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1645 compiler_use_next_block(c, cleanup);
1646 ADDOP(c, POP_BLOCK);
1647 compiler_pop_fblock(c, LOOP, start);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001648 VISIT_SEQ(c, stmt, s->v.For.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001649 compiler_use_next_block(c, end);
1650 return 1;
1651}
1652
1653static int
1654compiler_while(struct compiler *c, stmt_ty s)
1655{
1656 basicblock *loop, *orelse, *end, *anchor = NULL;
1657 int constant = expr_constant(s->v.While.test);
1658
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001659 if (constant == 0) {
1660 if (s->v.While.orelse)
1661 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001662 return 1;
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +00001663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664 loop = compiler_new_block(c);
1665 end = compiler_new_block(c);
1666 if (constant == -1) {
1667 anchor = compiler_new_block(c);
1668 if (anchor == NULL)
1669 return 0;
1670 }
1671 if (loop == NULL || end == NULL)
1672 return 0;
1673 if (s->v.While.orelse) {
1674 orelse = compiler_new_block(c);
1675 if (orelse == NULL)
1676 return 0;
1677 }
1678 else
1679 orelse = NULL;
1680
1681 ADDOP_JREL(c, SETUP_LOOP, end);
Nick Coghlanb90f52e2007-08-25 04:35:54 +00001682 compiler_use_next_block(c, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001683 if (!compiler_push_fblock(c, LOOP, loop))
1684 return 0;
1685 if (constant == -1) {
1686 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001687 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001688 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001689 VISIT_SEQ(c, stmt, s->v.While.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1691
1692 /* XXX should the two POP instructions be in a separate block
1693 if there is no else clause ?
1694 */
1695
1696 if (constant == -1) {
1697 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 ADDOP(c, POP_BLOCK);
1699 }
1700 compiler_pop_fblock(c, LOOP, loop);
Jeremy Hylton12603c42006-04-01 16:18:02 +00001701 if (orelse != NULL) /* what if orelse is just pass? */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001702 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 compiler_use_next_block(c, end);
1704
1705 return 1;
1706}
1707
1708static int
1709compiler_continue(struct compiler *c)
1710{
1711 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Neal Norwitz4f096d92006-08-21 19:47:08 +00001712 static const char IN_FINALLY_ERROR_MSG[] =
1713 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714 int i;
1715
1716 if (!c->u->u_nfblocks)
1717 return compiler_error(c, LOOP_ERROR_MSG);
1718 i = c->u->u_nfblocks - 1;
1719 switch (c->u->u_fblock[i].fb_type) {
1720 case LOOP:
1721 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1722 break;
1723 case EXCEPT:
1724 case FINALLY_TRY:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001725 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1726 /* Prevent continue anywhere under a finally
1727 even if hidden in a sub-try or except. */
1728 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1729 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1730 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 if (i == -1)
1732 return compiler_error(c, LOOP_ERROR_MSG);
1733 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1734 break;
1735 case FINALLY_END:
Neal Norwitz4f096d92006-08-21 19:47:08 +00001736 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737 }
1738
1739 return 1;
1740}
1741
1742/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1743
1744 SETUP_FINALLY L
1745 <code for body>
1746 POP_BLOCK
1747 LOAD_CONST <None>
1748 L: <code for finalbody>
1749 END_FINALLY
1750
1751 The special instructions use the block stack. Each block
1752 stack entry contains the instruction that created it (here
1753 SETUP_FINALLY), the level of the value stack at the time the
1754 block stack entry was created, and a label (here L).
1755
1756 SETUP_FINALLY:
1757 Pushes the current value stack level and the label
1758 onto the block stack.
1759 POP_BLOCK:
1760 Pops en entry from the block stack, and pops the value
1761 stack until its level is the same as indicated on the
1762 block stack. (The label is ignored.)
1763 END_FINALLY:
1764 Pops a variable number of entries from the *value* stack
1765 and re-raises the exception they specify. The number of
1766 entries popped depends on the (pseudo) exception type.
1767
1768 The block stack is unwound when an exception is raised:
1769 when a SETUP_FINALLY entry is found, the exception is pushed
1770 onto the value stack (and the exception condition is cleared),
1771 and the interpreter jumps to the label gotten from the block
1772 stack.
1773*/
1774
1775static int
1776compiler_try_finally(struct compiler *c, stmt_ty s)
1777{
1778 basicblock *body, *end;
1779 body = compiler_new_block(c);
1780 end = compiler_new_block(c);
1781 if (body == NULL || end == NULL)
1782 return 0;
1783
1784 ADDOP_JREL(c, SETUP_FINALLY, end);
1785 compiler_use_next_block(c, body);
1786 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1787 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001788 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001789 ADDOP(c, POP_BLOCK);
1790 compiler_pop_fblock(c, FINALLY_TRY, body);
1791
1792 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1793 compiler_use_next_block(c, end);
1794 if (!compiler_push_fblock(c, FINALLY_END, end))
1795 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001796 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001797 ADDOP(c, END_FINALLY);
1798 compiler_pop_fblock(c, FINALLY_END, end);
1799
1800 return 1;
1801}
1802
1803/*
1804 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
1805 (The contents of the value stack is shown in [], with the top
1806 at the right; 'tb' is trace-back info, 'val' the exception's
1807 associated value, and 'exc' the exception.)
1808
1809 Value stack Label Instruction Argument
1810 [] SETUP_EXCEPT L1
1811 [] <code for S>
1812 [] POP_BLOCK
1813 [] JUMP_FORWARD L0
1814
1815 [tb, val, exc] L1: DUP )
1816 [tb, val, exc, exc] <evaluate E1> )
1817 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001818 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819 [tb, val, exc] POP
1820 [tb, val] <assign to V1> (or POP if no V1)
1821 [tb] POP
1822 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001823 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001825 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 .............................etc.......................
1827
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001828 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829
1830 [] L0: <next statement>
1831
1832 Of course, parts are not generated if Vi or Ei is not present.
1833*/
1834static int
1835compiler_try_except(struct compiler *c, stmt_ty s)
1836{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001837 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001838 int i, n;
1839
1840 body = compiler_new_block(c);
1841 except = compiler_new_block(c);
1842 orelse = compiler_new_block(c);
1843 end = compiler_new_block(c);
1844 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1845 return 0;
1846 ADDOP_JREL(c, SETUP_EXCEPT, except);
1847 compiler_use_next_block(c, body);
1848 if (!compiler_push_fblock(c, EXCEPT, body))
1849 return 0;
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001850 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851 ADDOP(c, POP_BLOCK);
1852 compiler_pop_fblock(c, EXCEPT, body);
1853 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1854 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1855 compiler_use_next_block(c, except);
1856 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001857 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858 s->v.TryExcept.handlers, i);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001859 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860 return compiler_error(c, "default 'except:' must be last");
Amaury Forgeot d'Arc6fd03bb2008-02-04 21:45:05 +00001861 c->u->u_lineno_set = false;
1862 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 except = compiler_new_block(c);
1864 if (except == NULL)
1865 return 0;
Georg Brandla48f3ab2008-03-30 06:40:17 +00001866 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867 ADDOP(c, DUP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001868 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00001870 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871 }
1872 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001873 if (handler->v.ExceptHandler.name) {
1874 VISIT(c, expr, handler->v.ExceptHandler.name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875 }
1876 else {
1877 ADDOP(c, POP_TOP);
1878 }
1879 ADDOP(c, POP_TOP);
Georg Brandla48f3ab2008-03-30 06:40:17 +00001880 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 ADDOP_JREL(c, JUMP_FORWARD, end);
1882 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001883 }
1884 ADDOP(c, END_FINALLY);
1885 compiler_use_next_block(c, orelse);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00001886 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887 compiler_use_next_block(c, end);
1888 return 1;
1889}
1890
1891static int
1892compiler_import_as(struct compiler *c, identifier name, identifier asname)
1893{
1894 /* The IMPORT_NAME opcode was already generated. This function
1895 merely needs to bind the result to a name.
1896
1897 If there is a dot in name, we need to split it and emit a
1898 LOAD_ATTR for each name.
1899 */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001900 const char *src = PyString_AS_STRING(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901 const char *dot = strchr(src, '.');
1902 if (dot) {
1903 /* Consume the base module name to get the first attribute */
1904 src = dot + 1;
1905 while (dot) {
1906 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00001907 PyObject *attr;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001908 dot = strchr(src, '.');
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001909 attr = PyString_FromStringAndSize(src,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001910 dot ? dot - src : strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001911 if (!attr)
1912 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00001914 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915 src = dot + 1;
1916 }
1917 }
1918 return compiler_nameop(c, asname, Store);
1919}
1920
1921static int
1922compiler_import(struct compiler *c, stmt_ty s)
1923{
1924 /* The Import node stores a module name like a.b.c as a single
1925 string. This is convenient for all cases except
1926 import a.b.c as d
1927 where we need to parse that string to extract the individual
1928 module names.
1929 XXX Perhaps change the representation to make this case simpler?
1930 */
1931 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001932
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00001934 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001936 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937
Neal Norwitzcbce2802006-04-03 06:26:32 +00001938 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001939 level = PyInt_FromLong(0);
1940 else
1941 level = PyInt_FromLong(-1);
1942
1943 if (level == NULL)
1944 return 0;
1945
1946 ADDOP_O(c, LOAD_CONST, level, consts);
1947 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1949 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
1950
1951 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00001952 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001953 if (!r)
1954 return r;
1955 }
1956 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957 identifier tmp = alias->name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001958 const char *base = PyString_AS_STRING(alias->name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959 char *dot = strchr(base, '.');
1960 if (dot)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001961 tmp = PyString_FromStringAndSize(base,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001962 dot - base);
1963 r = compiler_nameop(c, tmp, Store);
1964 if (dot) {
1965 Py_DECREF(tmp);
1966 }
1967 if (!r)
1968 return r;
1969 }
1970 }
1971 return 1;
1972}
1973
1974static int
1975compiler_from_import(struct compiler *c, stmt_ty s)
1976{
1977 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978
1979 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001980 PyObject *level;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00001981 static PyObject *empty_string;
1982
1983 if (!empty_string) {
1984 empty_string = PyString_FromString("");
1985 if (!empty_string)
1986 return 0;
1987 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001988
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001989 if (!names)
1990 return 0;
1991
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001992 if (s->v.ImportFrom.level == 0 && c->c_flags &&
Neal Norwitzcbce2802006-04-03 06:26:32 +00001993 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001994 level = PyInt_FromLong(-1);
1995 else
1996 level = PyInt_FromLong(s->v.ImportFrom.level);
1997
1998 if (!level) {
1999 Py_DECREF(names);
2000 return 0;
2001 }
2002
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 /* build up the names */
2004 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002005 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002006 Py_INCREF(alias->name);
2007 PyTuple_SET_ITEM(names, i, alias->name);
2008 }
2009
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002010 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2011 !strcmp(PyString_AS_STRING(s->v.ImportFrom.module), "__future__")) {
2012 Py_DECREF(level);
2013 Py_DECREF(names);
2014 return compiler_error(c, "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002015 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 }
2017
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002018 ADDOP_O(c, LOAD_CONST, level, consts);
2019 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002020 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002021 Py_DECREF(names);
Benjamin Petersona72be3b2009-06-13 20:23:33 +00002022 if (s->v.ImportFrom.module) {
2023 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2024 }
2025 else {
2026 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2027 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002028 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002029 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002030 identifier store_name;
2031
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002032 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002033 assert(n == 1);
2034 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002035 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036 }
2037
2038 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2039 store_name = alias->name;
2040 if (alias->asname)
2041 store_name = alias->asname;
2042
2043 if (!compiler_nameop(c, store_name, Store)) {
2044 Py_DECREF(names);
2045 return 0;
2046 }
2047 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002048 /* remove imported module */
2049 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050 return 1;
2051}
2052
2053static int
2054compiler_assert(struct compiler *c, stmt_ty s)
2055{
2056 static PyObject *assertion_error = NULL;
2057 basicblock *end;
2058
2059 if (Py_OptimizeFlag)
2060 return 1;
2061 if (assertion_error == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002062 assertion_error = PyString_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002063 if (assertion_error == NULL)
2064 return 0;
2065 }
Neal Norwitz400aeda2008-03-15 22:03:18 +00002066 if (s->v.Assert.test->kind == Tuple_kind &&
2067 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2068 const char* msg =
2069 "assertion is always true, perhaps remove parentheses?";
2070 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2071 c->u->u_lineno, NULL, NULL) == -1)
2072 return 0;
2073 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002074 VISIT(c, expr, s->v.Assert.test);
2075 end = compiler_new_block(c);
2076 if (end == NULL)
2077 return 0;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002078 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002079 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2080 if (s->v.Assert.msg) {
2081 VISIT(c, expr, s->v.Assert.msg);
2082 ADDOP_I(c, RAISE_VARARGS, 2);
2083 }
2084 else {
2085 ADDOP_I(c, RAISE_VARARGS, 1);
2086 }
Neal Norwitz51abbc72005-12-18 07:06:23 +00002087 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002088 return 1;
2089}
2090
2091static int
2092compiler_visit_stmt(struct compiler *c, stmt_ty s)
2093{
2094 int i, n;
2095
Neal Norwitzf733a012006-10-29 18:30:10 +00002096 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002097 c->u->u_lineno = s->lineno;
2098 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002099
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002100 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002101 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002102 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002103 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002105 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 if (c->u->u_ste->ste_type != FunctionBlock)
2107 return compiler_error(c, "'return' outside function");
2108 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 VISIT(c, expr, s->v.Return.value);
2110 }
2111 else
2112 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2113 ADDOP(c, RETURN_VALUE);
2114 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002115 case Delete_kind:
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002116 VISIT_SEQ(c, expr, s->v.Delete.targets)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002117 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002118 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002119 n = asdl_seq_LEN(s->v.Assign.targets);
2120 VISIT(c, expr, s->v.Assign.value);
2121 for (i = 0; i < n; i++) {
2122 if (i < n - 1)
2123 ADDOP(c, DUP_TOP);
2124 VISIT(c, expr,
2125 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2126 }
2127 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002128 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002130 case Print_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002131 return compiler_print(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002132 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002134 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002135 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002136 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002138 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139 n = 0;
2140 if (s->v.Raise.type) {
2141 VISIT(c, expr, s->v.Raise.type);
2142 n++;
2143 if (s->v.Raise.inst) {
2144 VISIT(c, expr, s->v.Raise.inst);
2145 n++;
2146 if (s->v.Raise.tback) {
2147 VISIT(c, expr, s->v.Raise.tback);
2148 n++;
2149 }
2150 }
2151 }
2152 ADDOP_I(c, RAISE_VARARGS, n);
2153 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002154 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002155 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002156 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002157 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002158 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002160 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002161 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002162 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002163 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002164 case Exec_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 VISIT(c, expr, s->v.Exec.body);
2166 if (s->v.Exec.globals) {
2167 VISIT(c, expr, s->v.Exec.globals);
2168 if (s->v.Exec.locals) {
2169 VISIT(c, expr, s->v.Exec.locals);
2170 } else {
2171 ADDOP(c, DUP_TOP);
2172 }
2173 } else {
2174 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2175 ADDOP(c, DUP_TOP);
2176 }
2177 ADDOP(c, EXEC_STMT);
2178 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002179 case Global_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002181 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 if (c->c_interactive && c->c_nestlevel <= 1) {
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002183 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 ADDOP(c, PRINT_EXPR);
2185 }
Neal Norwitz0cbd8052006-08-04 05:09:28 +00002186 else if (s->v.Expr.value->kind != Str_kind &&
2187 s->v.Expr.value->kind != Num_kind) {
2188 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 ADDOP(c, POP_TOP);
2190 }
2191 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002192 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002194 case Break_kind:
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002195 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 return compiler_error(c, "'break' outside loop");
2197 ADDOP(c, BREAK_LOOP);
2198 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002199 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002201 case With_kind:
2202 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 }
2204 return 1;
2205}
2206
2207static int
2208unaryop(unaryop_ty op)
2209{
2210 switch (op) {
2211 case Invert:
2212 return UNARY_INVERT;
2213 case Not:
2214 return UNARY_NOT;
2215 case UAdd:
2216 return UNARY_POSITIVE;
2217 case USub:
2218 return UNARY_NEGATIVE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002219 default:
2220 PyErr_Format(PyExc_SystemError,
2221 "unary op %d should not be possible", op);
2222 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224}
2225
2226static int
2227binop(struct compiler *c, operator_ty op)
2228{
2229 switch (op) {
2230 case Add:
2231 return BINARY_ADD;
2232 case Sub:
2233 return BINARY_SUBTRACT;
2234 case Mult:
2235 return BINARY_MULTIPLY;
2236 case Div:
2237 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2238 return BINARY_TRUE_DIVIDE;
2239 else
2240 return BINARY_DIVIDE;
2241 case Mod:
2242 return BINARY_MODULO;
2243 case Pow:
2244 return BINARY_POWER;
2245 case LShift:
2246 return BINARY_LSHIFT;
2247 case RShift:
2248 return BINARY_RSHIFT;
2249 case BitOr:
2250 return BINARY_OR;
2251 case BitXor:
2252 return BINARY_XOR;
2253 case BitAnd:
2254 return BINARY_AND;
2255 case FloorDiv:
2256 return BINARY_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002257 default:
2258 PyErr_Format(PyExc_SystemError,
2259 "binary op %d should not be possible", op);
2260 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002261 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262}
2263
2264static int
2265cmpop(cmpop_ty op)
2266{
2267 switch (op) {
2268 case Eq:
2269 return PyCmp_EQ;
2270 case NotEq:
2271 return PyCmp_NE;
2272 case Lt:
2273 return PyCmp_LT;
2274 case LtE:
2275 return PyCmp_LE;
2276 case Gt:
2277 return PyCmp_GT;
2278 case GtE:
2279 return PyCmp_GE;
2280 case Is:
2281 return PyCmp_IS;
2282 case IsNot:
2283 return PyCmp_IS_NOT;
2284 case In:
2285 return PyCmp_IN;
2286 case NotIn:
2287 return PyCmp_NOT_IN;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002288 default:
2289 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291}
2292
2293static int
2294inplace_binop(struct compiler *c, operator_ty op)
2295{
2296 switch (op) {
2297 case Add:
2298 return INPLACE_ADD;
2299 case Sub:
2300 return INPLACE_SUBTRACT;
2301 case Mult:
2302 return INPLACE_MULTIPLY;
2303 case Div:
2304 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2305 return INPLACE_TRUE_DIVIDE;
2306 else
2307 return INPLACE_DIVIDE;
2308 case Mod:
2309 return INPLACE_MODULO;
2310 case Pow:
2311 return INPLACE_POWER;
2312 case LShift:
2313 return INPLACE_LSHIFT;
2314 case RShift:
2315 return INPLACE_RSHIFT;
2316 case BitOr:
2317 return INPLACE_OR;
2318 case BitXor:
2319 return INPLACE_XOR;
2320 case BitAnd:
2321 return INPLACE_AND;
2322 case FloorDiv:
2323 return INPLACE_FLOOR_DIVIDE;
Georg Brandlfc8eef32008-03-28 12:11:56 +00002324 default:
2325 PyErr_Format(PyExc_SystemError,
2326 "inplace binary op %d should not be possible", op);
2327 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329}
2330
2331static int
2332compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2333{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002334 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2336
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002337 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002338 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 /* XXX AugStore isn't used anywhere! */
2340
Neal Norwitz0031ff32008-02-25 01:45:37 +00002341 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002342 if (!mangled)
2343 return 0;
2344
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002345 op = 0;
2346 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002347 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002348 switch (scope) {
2349 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002350 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 optype = OP_DEREF;
2352 break;
2353 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002354 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002355 optype = OP_DEREF;
2356 break;
2357 case LOCAL:
2358 if (c->u->u_ste->ste_type == FunctionBlock)
2359 optype = OP_FAST;
2360 break;
2361 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002362 if (c->u->u_ste->ste_type == FunctionBlock &&
2363 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364 optype = OP_GLOBAL;
2365 break;
2366 case GLOBAL_EXPLICIT:
2367 optype = OP_GLOBAL;
2368 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002369 default:
2370 /* scope can be 0 */
2371 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002372 }
2373
2374 /* XXX Leave assert here, but handle __doc__ and the like better */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002375 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002376
2377 switch (optype) {
2378 case OP_DEREF:
2379 switch (ctx) {
2380 case Load: op = LOAD_DEREF; break;
2381 case Store: op = STORE_DEREF; break;
2382 case AugLoad:
2383 case AugStore:
2384 break;
2385 case Del:
2386 PyErr_Format(PyExc_SyntaxError,
2387 "can not delete variable '%s' referenced "
2388 "in nested scope",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002389 PyString_AS_STRING(name));
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002390 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002391 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002392 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002393 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002394 PyErr_SetString(PyExc_SystemError,
2395 "param invalid for deref variable");
2396 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002397 }
2398 break;
2399 case OP_FAST:
2400 switch (ctx) {
2401 case Load: op = LOAD_FAST; break;
2402 case Store: op = STORE_FAST; break;
2403 case Del: op = DELETE_FAST; break;
2404 case AugLoad:
2405 case AugStore:
2406 break;
2407 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002408 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002409 PyErr_SetString(PyExc_SystemError,
2410 "param invalid for local variable");
2411 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002412 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002413 ADDOP_O(c, op, mangled, varnames);
2414 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415 return 1;
2416 case OP_GLOBAL:
2417 switch (ctx) {
2418 case Load: op = LOAD_GLOBAL; break;
2419 case Store: op = STORE_GLOBAL; break;
2420 case Del: op = DELETE_GLOBAL; break;
2421 case AugLoad:
2422 case AugStore:
2423 break;
2424 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002425 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002426 PyErr_SetString(PyExc_SystemError,
2427 "param invalid for global variable");
2428 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002429 }
2430 break;
2431 case OP_NAME:
2432 switch (ctx) {
2433 case Load: op = LOAD_NAME; break;
2434 case Store: op = STORE_NAME; break;
2435 case Del: op = DELETE_NAME; break;
2436 case AugLoad:
2437 case AugStore:
2438 break;
2439 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002440 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002441 PyErr_SetString(PyExc_SystemError,
2442 "param invalid for name variable");
2443 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444 }
2445 break;
2446 }
2447
2448 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002449 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002450 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002451 if (arg < 0)
2452 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002453 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002454}
2455
2456static int
2457compiler_boolop(struct compiler *c, expr_ty e)
2458{
2459 basicblock *end;
2460 int jumpi, i, n;
2461 asdl_seq *s;
2462
2463 assert(e->kind == BoolOp_kind);
2464 if (e->v.BoolOp.op == And)
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002465 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466 else
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002467 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002468 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002469 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002470 return 0;
2471 s = e->v.BoolOp.values;
2472 n = asdl_seq_LEN(s) - 1;
Neal Norwitzc173b482006-07-30 19:18:13 +00002473 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474 for (i = 0; i < n; ++i) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002475 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002476 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002477 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002478 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 compiler_use_next_block(c, end);
2480 return 1;
2481}
2482
2483static int
2484compiler_list(struct compiler *c, expr_ty e)
2485{
2486 int n = asdl_seq_LEN(e->v.List.elts);
2487 if (e->v.List.ctx == Store) {
2488 ADDOP_I(c, UNPACK_SEQUENCE, n);
2489 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002490 VISIT_SEQ(c, expr, e->v.List.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002491 if (e->v.List.ctx == Load) {
2492 ADDOP_I(c, BUILD_LIST, n);
2493 }
2494 return 1;
2495}
2496
2497static int
2498compiler_tuple(struct compiler *c, expr_ty e)
2499{
2500 int n = asdl_seq_LEN(e->v.Tuple.elts);
2501 if (e->v.Tuple.ctx == Store) {
2502 ADDOP_I(c, UNPACK_SEQUENCE, n);
2503 }
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002504 VISIT_SEQ(c, expr, e->v.Tuple.elts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002505 if (e->v.Tuple.ctx == Load) {
2506 ADDOP_I(c, BUILD_TUPLE, n);
2507 }
2508 return 1;
2509}
2510
2511static int
2512compiler_compare(struct compiler *c, expr_ty e)
2513{
2514 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002515 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002516
2517 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2518 VISIT(c, expr, e->v.Compare.left);
2519 n = asdl_seq_LEN(e->v.Compare.ops);
2520 assert(n > 0);
2521 if (n > 1) {
2522 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002523 if (cleanup == NULL)
2524 return 0;
Anthony Baxter7b782b62006-04-11 12:01:56 +00002525 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002526 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002527 }
2528 for (i = 1; i < n; i++) {
2529 ADDOP(c, DUP_TOP);
2530 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002532 cmpop((cmpop_ty)(asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002533 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002534 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002535 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 if (i < (n - 1))
Anthony Baxter7b782b62006-04-11 12:01:56 +00002537 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002538 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002539 }
Anthony Baxter7b782b62006-04-11 12:01:56 +00002540 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002541 ADDOP_I(c, COMPARE_OP,
Martin v. Löwis0cc56e52006-04-13 12:29:43 +00002542 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002543 if (n > 1) {
2544 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002545 if (end == NULL)
2546 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547 ADDOP_JREL(c, JUMP_FORWARD, end);
2548 compiler_use_next_block(c, cleanup);
2549 ADDOP(c, ROT_TWO);
2550 ADDOP(c, POP_TOP);
2551 compiler_use_next_block(c, end);
2552 }
2553 return 1;
2554}
2555
2556static int
2557compiler_call(struct compiler *c, expr_ty e)
2558{
2559 int n, code = 0;
2560
2561 VISIT(c, expr, e->v.Call.func);
2562 n = asdl_seq_LEN(e->v.Call.args);
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002563 VISIT_SEQ(c, expr, e->v.Call.args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564 if (e->v.Call.keywords) {
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002565 VISIT_SEQ(c, keyword, e->v.Call.keywords);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2567 }
2568 if (e->v.Call.starargs) {
2569 VISIT(c, expr, e->v.Call.starargs);
2570 code |= 1;
2571 }
2572 if (e->v.Call.kwargs) {
2573 VISIT(c, expr, e->v.Call.kwargs);
2574 code |= 2;
2575 }
2576 switch (code) {
2577 case 0:
2578 ADDOP_I(c, CALL_FUNCTION, n);
2579 break;
2580 case 1:
2581 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2582 break;
2583 case 2:
2584 ADDOP_I(c, CALL_FUNCTION_KW, n);
2585 break;
2586 case 3:
2587 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2588 break;
2589 }
2590 return 1;
2591}
2592
2593static int
Antoine Pitroud0c35152008-12-17 00:38:28 +00002594compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
2595 int gen_index, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596{
2597 /* generate code for the iterator, then each of the ifs,
2598 and then write to the element */
2599
2600 comprehension_ty l;
2601 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002602 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603
2604 start = compiler_new_block(c);
2605 skip = compiler_new_block(c);
2606 if_cleanup = compiler_new_block(c);
2607 anchor = compiler_new_block(c);
2608
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002609 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2610 anchor == NULL)
2611 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002612
Anthony Baxter7b782b62006-04-11 12:01:56 +00002613 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 VISIT(c, expr, l->iter);
2615 ADDOP(c, GET_ITER);
2616 compiler_use_next_block(c, start);
2617 ADDOP_JREL(c, FOR_ITER, anchor);
2618 NEXT_BLOCK(c);
2619 VISIT(c, expr, l->target);
2620
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002621 /* XXX this needs to be cleaned up...a lot! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 n = asdl_seq_LEN(l->ifs);
2623 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00002624 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002626 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628 }
2629
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002630 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitroud0c35152008-12-17 00:38:28 +00002631 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002632 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002634 /* only append after the last for generator */
2635 if (gen_index >= asdl_seq_LEN(generators)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002636 VISIT(c, expr, elt);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002637 ADDOP_I(c, LIST_APPEND, gen_index+1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002638
2639 compiler_use_next_block(c, skip);
2640 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002641 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2643 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644
2645 return 1;
2646}
2647
2648static int
2649compiler_listcomp(struct compiler *c, expr_ty e)
2650{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002651 assert(e->kind == ListComp_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002652 ADDOP_I(c, BUILD_LIST, 0);
Antoine Pitroud0c35152008-12-17 00:38:28 +00002653 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2654 e->v.ListComp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655}
2656
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002657/* Dict and set comprehensions and generator expressions work by creating a
2658 nested function to perform the actual iteration. This means that the
2659 iteration variables don't leak into the current scope.
2660 The defined function is called immediately following its definition, with the
2661 result of that call being the result of the expression.
2662 The LC/SC version returns the populated container, while the GE version is
2663 flagged in symtable.c as a generator, so it returns the generator object
2664 when the function is called.
2665 This code *knows* that the loop cannot contain break, continue, or return,
2666 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2667
2668 Possible cleanups:
2669 - iterate over the generator sequence instead of using recursion
2670*/
2671
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002673compiler_comprehension_generator(struct compiler *c,
2674 asdl_seq *generators, int gen_index,
2675 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002676{
2677 /* generate code for the iterator, then each of the ifs,
2678 and then write to the element */
2679
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002680 comprehension_ty gen;
2681 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002682 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002683
2684 start = compiler_new_block(c);
2685 skip = compiler_new_block(c);
2686 if_cleanup = compiler_new_block(c);
2687 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002689 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002690 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691 return 0;
2692
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002693 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2694
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695 if (gen_index == 0) {
2696 /* Receive outermost iter as an implicit argument */
2697 c->u->u_argcount = 1;
2698 ADDOP_I(c, LOAD_FAST, 0);
2699 }
2700 else {
2701 /* Sub-iter - calculate on the fly */
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002702 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 ADDOP(c, GET_ITER);
2704 }
2705 compiler_use_next_block(c, start);
2706 ADDOP_JREL(c, FOR_ITER, anchor);
2707 NEXT_BLOCK(c);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002708 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002709
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002710 /* XXX this needs to be cleaned up...a lot! */
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002711 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002712 for (i = 0; i < n; i++) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002713 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 VISIT(c, expr, e);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002715 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 NEXT_BLOCK(c);
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002717 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002719 if (++gen_index < asdl_seq_LEN(generators))
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002720 if (!compiler_comprehension_generator(c,
2721 generators, gen_index,
2722 elt, val, type))
2723 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002725 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002726 if (gen_index >= asdl_seq_LEN(generators)) {
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002727 /* comprehension specific code */
2728 switch (type) {
2729 case COMP_GENEXP:
2730 VISIT(c, expr, elt);
2731 ADDOP(c, YIELD_VALUE);
2732 ADDOP(c, POP_TOP);
2733 break;
2734 case COMP_SETCOMP:
2735 VISIT(c, expr, elt);
2736 ADDOP_I(c, SET_ADD, gen_index + 1);
2737 break;
2738 case COMP_DICTCOMP:
2739 /* With 'd[k] = v', v is evaluated before k, so we do
2740 the same. */
2741 VISIT(c, expr, val);
2742 VISIT(c, expr, elt);
2743 ADDOP_I(c, MAP_ADD, gen_index + 1);
2744 break;
2745 default:
2746 return 0;
2747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002748
2749 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002750 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002751 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2753 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754
2755 return 1;
2756}
2757
2758static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002759compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2760 asdl_seq *generators, expr_ty elt, expr_ty val)
2761{
2762 PyCodeObject *co = NULL;
2763 expr_ty outermost_iter;
2764
2765 outermost_iter = ((comprehension_ty)
2766 asdl_seq_GET(generators, 0))->iter;
2767
2768 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2769 goto error;
2770
2771 if (type != COMP_GENEXP) {
2772 int op;
2773 switch (type) {
2774 case COMP_SETCOMP:
2775 op = BUILD_SET;
2776 break;
2777 case COMP_DICTCOMP:
2778 op = BUILD_MAP;
2779 break;
2780 default:
2781 PyErr_Format(PyExc_SystemError,
2782 "unknown comprehension type %d", type);
2783 goto error_in_scope;
2784 }
2785
2786 ADDOP_I(c, op, 0);
2787 }
2788
2789 if (!compiler_comprehension_generator(c, generators, 0, elt,
2790 val, type))
2791 goto error_in_scope;
2792
2793 if (type != COMP_GENEXP) {
2794 ADDOP(c, RETURN_VALUE);
2795 }
2796
2797 co = assemble(c, 1);
2798 compiler_exit_scope(c);
2799 if (co == NULL)
2800 goto error;
2801
2802 if (!compiler_make_closure(c, co, 0))
2803 goto error;
2804 Py_DECREF(co);
2805
2806 VISIT(c, expr, outermost_iter);
2807 ADDOP(c, GET_ITER);
2808 ADDOP_I(c, CALL_FUNCTION, 1);
2809 return 1;
2810error_in_scope:
2811 compiler_exit_scope(c);
2812error:
2813 Py_XDECREF(co);
2814 return 0;
2815}
2816
2817static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818compiler_genexp(struct compiler *c, expr_ty e)
2819{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002820 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002821 if (!name) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002822 name = PyString_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002823 if (!name)
2824 return 0;
2825 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002826 assert(e->kind == GeneratorExp_kind);
2827 return compiler_comprehension(c, e, COMP_GENEXP, name,
2828 e->v.GeneratorExp.generators,
2829 e->v.GeneratorExp.elt, NULL);
2830}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002832static int
2833compiler_setcomp(struct compiler *c, expr_ty e)
2834{
2835 static identifier name;
2836 if (!name) {
2837 name = PyString_FromString("<setcomp>");
2838 if (!name)
2839 return 0;
2840 }
2841 assert(e->kind == SetComp_kind);
2842 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2843 e->v.SetComp.generators,
2844 e->v.SetComp.elt, NULL);
2845}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002847static int
2848compiler_dictcomp(struct compiler *c, expr_ty e)
2849{
2850 static identifier name;
2851 if (!name) {
2852 name = PyString_FromString("<dictcomp>");
2853 if (!name)
2854 return 0;
2855 }
2856 assert(e->kind == DictComp_kind);
2857 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2858 e->v.DictComp.generators,
2859 e->v.DictComp.key, e->v.DictComp.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860}
2861
2862static int
2863compiler_visit_keyword(struct compiler *c, keyword_ty k)
2864{
2865 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2866 VISIT(c, expr, k->value);
2867 return 1;
2868}
2869
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002870/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871 whether they are true or false.
2872
2873 Return values: 1 for true, 0 for false, -1 for non-constant.
2874 */
2875
2876static int
2877expr_constant(expr_ty e)
2878{
2879 switch (e->kind) {
2880 case Num_kind:
2881 return PyObject_IsTrue(e->v.Num.n);
2882 case Str_kind:
2883 return PyObject_IsTrue(e->v.Str.s);
Georg Brandlddbaa662006-06-04 21:56:52 +00002884 case Name_kind:
2885 /* __debug__ is not assignable, so we can optimize
2886 * it away in if and while statements */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002887 if (strcmp(PyString_AS_STRING(e->v.Name.id),
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002888 "__debug__") == 0)
Georg Brandlddbaa662006-06-04 21:56:52 +00002889 return ! Py_OptimizeFlag;
2890 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891 default:
2892 return -1;
2893 }
2894}
2895
Guido van Rossumc2e20742006-02-27 22:32:47 +00002896/*
2897 Implements the with statement from PEP 343.
2898
2899 The semantics outlined in that PEP are as follows:
2900
2901 with EXPR as VAR:
2902 BLOCK
2903
2904 It is implemented roughly as:
2905
Guido van Rossumda5b7012006-05-02 19:47:52 +00002906 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002907 exit = context.__exit__ # not calling it
2908 value = context.__enter__()
2909 try:
2910 VAR = value # if VAR present in the syntax
2911 BLOCK
2912 finally:
2913 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002914 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002915 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002916 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002917 exit(*exc)
2918 */
2919static int
2920compiler_with(struct compiler *c, stmt_ty s)
2921{
Guido van Rossumc2e20742006-02-27 22:32:47 +00002922 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002923
2924 assert(s->kind == With_kind);
2925
Guido van Rossumc2e20742006-02-27 22:32:47 +00002926 block = compiler_new_block(c);
2927 finally = compiler_new_block(c);
2928 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002929 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002930
Guido van Rossumda5b7012006-05-02 19:47:52 +00002931 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002932 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002933 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002934
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002935 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002936 compiler_use_next_block(c, block);
Benjamin Peterson565d7852010-02-05 02:12:14 +00002937 /* Note that the block is actually called SETUP_WITH in ceval.c, but
2938 functions the same as SETUP_FINALLY except that exceptions are
2939 normalized. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002940 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002941 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002942 }
2943
2944 if (s->v.With.optional_vars) {
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002945 VISIT(c, expr, s->v.With.optional_vars);
2946 }
2947 else {
2948 /* Discard result from context.__enter__() */
2949 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002950 }
2951
2952 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002953 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002954
2955 /* End of try block; start the finally block */
2956 ADDOP(c, POP_BLOCK);
2957 compiler_pop_fblock(c, FINALLY_TRY, block);
2958
2959 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2960 compiler_use_next_block(c, finally);
2961 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002962 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002963
Nick Coghlan7af53be2008-03-07 14:13:28 +00002964 /* Finally block starts; context.__exit__ is on the stack under
2965 the exception or return information. Just issue our magic
2966 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002967 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002968
2969 /* Finally block ends. */
2970 ADDOP(c, END_FINALLY);
2971 compiler_pop_fblock(c, FINALLY_END, finally);
2972 return 1;
2973}
2974
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002975static int
2976compiler_visit_expr(struct compiler *c, expr_ty e)
2977{
2978 int i, n;
2979
Neal Norwitzf733a012006-10-29 18:30:10 +00002980 /* If expr e has a different line number than the last expr/stmt,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00002981 set a new line number for the next instruction.
2982 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002983 if (e->lineno > c->u->u_lineno) {
2984 c->u->u_lineno = e->lineno;
2985 c->u->u_lineno_set = false;
2986 }
2987 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002988 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002989 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002990 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991 VISIT(c, expr, e->v.BinOp.left);
2992 VISIT(c, expr, e->v.BinOp.right);
2993 ADDOP(c, binop(c, e->v.BinOp.op));
2994 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002995 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996 VISIT(c, expr, e->v.UnaryOp.operand);
2997 ADDOP(c, unaryop(e->v.UnaryOp.op));
2998 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002999 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003001 case IfExp_kind:
3002 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003003 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003004 n = asdl_seq_LEN(e->v.Dict.values);
Raymond Hettinger70fcfd02007-12-19 22:14:34 +00003005 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003007 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003008 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Anthony Baxter7b782b62006-04-11 12:01:56 +00003009 VISIT(c, expr,
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003010 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Raymond Hettingereffde122007-12-18 18:26:18 +00003011 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012 }
3013 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00003014 case Set_kind:
3015 n = asdl_seq_LEN(e->v.Set.elts);
3016 VISIT_SEQ(c, expr, e->v.Set.elts);
3017 ADDOP_I(c, BUILD_SET, n);
3018 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003019 case ListComp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 return compiler_listcomp(c, e);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00003021 case SetComp_kind:
3022 return compiler_setcomp(c, e);
3023 case DictComp_kind:
3024 return compiler_dictcomp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003025 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003026 return compiler_genexp(c, e);
3027 case Yield_kind:
3028 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003029 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 if (e->v.Yield.value) {
3031 VISIT(c, expr, e->v.Yield.value);
3032 }
3033 else {
3034 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3035 }
3036 ADDOP(c, YIELD_VALUE);
3037 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003038 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003039 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003040 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003042 case Repr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 VISIT(c, expr, e->v.Repr.value);
3044 ADDOP(c, UNARY_CONVERT);
3045 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003046 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3048 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003049 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3051 break;
3052 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003053 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054 if (e->v.Attribute.ctx != AugStore)
3055 VISIT(c, expr, e->v.Attribute.value);
3056 switch (e->v.Attribute.ctx) {
3057 case AugLoad:
3058 ADDOP(c, DUP_TOP);
3059 /* Fall through to load */
3060 case Load:
3061 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3062 break;
3063 case AugStore:
3064 ADDOP(c, ROT_TWO);
3065 /* Fall through to save */
3066 case Store:
3067 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3068 break;
3069 case Del:
3070 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3071 break;
3072 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003073 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003074 PyErr_SetString(PyExc_SystemError,
3075 "param invalid in attribute expression");
3076 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003077 }
3078 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003079 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003080 switch (e->v.Subscript.ctx) {
3081 case AugLoad:
3082 VISIT(c, expr, e->v.Subscript.value);
3083 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3084 break;
3085 case Load:
3086 VISIT(c, expr, e->v.Subscript.value);
3087 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3088 break;
3089 case AugStore:
3090 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3091 break;
3092 case Store:
3093 VISIT(c, expr, e->v.Subscript.value);
3094 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3095 break;
3096 case Del:
3097 VISIT(c, expr, e->v.Subscript.value);
3098 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3099 break;
3100 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003101 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003102 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003103 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003104 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003105 }
3106 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003107 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3109 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003110 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003111 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003112 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003113 return compiler_tuple(c, e);
3114 }
3115 return 1;
3116}
3117
3118static int
3119compiler_augassign(struct compiler *c, stmt_ty s)
3120{
3121 expr_ty e = s->v.AugAssign.target;
3122 expr_ty auge;
3123
3124 assert(s->kind == AugAssign_kind);
3125
3126 switch (e->kind) {
Neal Norwitz2585ad52006-06-12 02:09:34 +00003127 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003128 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003129 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003130 if (auge == NULL)
3131 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003132 VISIT(c, expr, auge);
3133 VISIT(c, expr, s->v.AugAssign.value);
3134 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3135 auge->v.Attribute.ctx = AugStore;
3136 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003137 break;
3138 case Subscript_kind:
3139 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003140 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003141 if (auge == NULL)
3142 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 VISIT(c, expr, auge);
3144 VISIT(c, expr, s->v.AugAssign.value);
3145 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003146 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003148 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003149 case Name_kind:
Neil Schemenauer0e07b602006-07-09 16:16:34 +00003150 if (!compiler_nameop(c, e->v.Name.id, Load))
3151 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 VISIT(c, expr, s->v.AugAssign.value);
3153 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3154 return compiler_nameop(c, e->v.Name.id, Store);
3155 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003156 PyErr_Format(PyExc_SystemError,
3157 "invalid node type (%d) for augmented assignment",
3158 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003159 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160 }
3161 return 1;
3162}
3163
3164static int
3165compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3166{
3167 struct fblockinfo *f;
Neal Norwitz21997af2006-10-28 21:19:07 +00003168 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3169 PyErr_SetString(PyExc_SystemError,
3170 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003171 return 0;
Neal Norwitz21997af2006-10-28 21:19:07 +00003172 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003173 f = &c->u->u_fblock[c->u->u_nfblocks++];
3174 f->fb_type = t;
3175 f->fb_block = b;
3176 return 1;
3177}
3178
3179static void
3180compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3181{
3182 struct compiler_unit *u = c->u;
3183 assert(u->u_nfblocks > 0);
3184 u->u_nfblocks--;
3185 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3186 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3187}
3188
Jeremy Hylton82271f12006-10-04 02:24:52 +00003189static int
3190compiler_in_loop(struct compiler *c) {
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003191 int i;
3192 struct compiler_unit *u = c->u;
3193 for (i = 0; i < u->u_nfblocks; ++i) {
3194 if (u->u_fblock[i].fb_type == LOOP)
3195 return 1;
3196 }
3197 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003198}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003199/* Raises a SyntaxError and returns 0.
3200 If something goes wrong, a different exception may be raised.
3201*/
3202
3203static int
3204compiler_error(struct compiler *c, const char *errstr)
3205{
3206 PyObject *loc;
3207 PyObject *u = NULL, *v = NULL;
3208
3209 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3210 if (!loc) {
3211 Py_INCREF(Py_None);
3212 loc = Py_None;
3213 }
3214 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3215 Py_None, loc);
3216 if (!u)
3217 goto exit;
3218 v = Py_BuildValue("(zO)", errstr, u);
3219 if (!v)
3220 goto exit;
3221 PyErr_SetObject(PyExc_SyntaxError, v);
3222 exit:
3223 Py_DECREF(loc);
3224 Py_XDECREF(u);
3225 Py_XDECREF(v);
3226 return 0;
3227}
3228
3229static int
3230compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003231 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003233 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003235 /* XXX this code is duplicated */
3236 switch (ctx) {
3237 case AugLoad: /* fall through to Load */
3238 case Load: op = BINARY_SUBSCR; break;
3239 case AugStore:/* fall through to Store */
3240 case Store: op = STORE_SUBSCR; break;
3241 case Del: op = DELETE_SUBSCR; break;
3242 case Param:
3243 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003244 "invalid %s kind %d in subscript\n",
3245 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003246 return 0;
3247 }
3248 if (ctx == AugLoad) {
3249 ADDOP_I(c, DUP_TOPX, 2);
3250 }
3251 else if (ctx == AugStore) {
3252 ADDOP(c, ROT_THREE);
3253 }
3254 ADDOP(c, op);
3255 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003256}
3257
3258static int
3259compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3260{
3261 int n = 2;
3262 assert(s->kind == Slice_kind);
3263
3264 /* only handles the cases where BUILD_SLICE is emitted */
3265 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003266 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267 }
3268 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003269 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003270 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003271
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003272 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003273 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 }
3275 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003276 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 }
3278
3279 if (s->v.Slice.step) {
3280 n++;
3281 VISIT(c, expr, s->v.Slice.step);
3282 }
3283 ADDOP_I(c, BUILD_SLICE, n);
3284 return 1;
3285}
3286
3287static int
3288compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3289{
3290 int op = 0, slice_offset = 0, stack_count = 0;
3291
3292 assert(s->v.Slice.step == NULL);
3293 if (s->v.Slice.lower) {
3294 slice_offset++;
3295 stack_count++;
3296 if (ctx != AugStore)
3297 VISIT(c, expr, s->v.Slice.lower);
3298 }
3299 if (s->v.Slice.upper) {
3300 slice_offset += 2;
3301 stack_count++;
3302 if (ctx != AugStore)
3303 VISIT(c, expr, s->v.Slice.upper);
3304 }
3305
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003306 if (ctx == AugLoad) {
3307 switch (stack_count) {
3308 case 0: ADDOP(c, DUP_TOP); break;
3309 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3310 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3311 }
3312 }
3313 else if (ctx == AugStore) {
3314 switch (stack_count) {
3315 case 0: ADDOP(c, ROT_TWO); break;
3316 case 1: ADDOP(c, ROT_THREE); break;
3317 case 2: ADDOP(c, ROT_FOUR); break;
3318 }
3319 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003320
3321 switch (ctx) {
3322 case AugLoad: /* fall through to Load */
3323 case Load: op = SLICE; break;
3324 case AugStore:/* fall through to Store */
3325 case Store: op = STORE_SLICE; break;
3326 case Del: op = DELETE_SLICE; break;
Neal Norwitz4737b232005-11-19 23:58:29 +00003327 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003328 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003329 PyErr_SetString(PyExc_SystemError,
3330 "param invalid in simple slice");
3331 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332 }
3333
3334 ADDOP(c, op + slice_offset);
3335 return 1;
3336}
3337
3338static int
3339compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3340 expr_context_ty ctx)
3341{
3342 switch (s->kind) {
3343 case Ellipsis_kind:
3344 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3345 break;
3346 case Slice_kind:
3347 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003348 case Index_kind:
3349 VISIT(c, expr, s->v.Index.value);
3350 break;
3351 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003352 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003353 PyErr_SetString(PyExc_SystemError,
3354 "extended slice invalid in nested slice");
3355 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356 }
3357 return 1;
3358}
3359
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360static int
3361compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3362{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003363 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003364 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003365 case Index_kind:
3366 kindname = "index";
3367 if (ctx != AugStore) {
3368 VISIT(c, expr, s->v.Index.value);
3369 }
3370 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 case Ellipsis_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003372 kindname = "ellipsis";
3373 if (ctx != AugStore) {
3374 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3375 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003376 break;
3377 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003378 kindname = "slice";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003379 if (!s->v.Slice.step)
3380 return compiler_simple_slice(c, s, ctx);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003381 if (ctx != AugStore) {
3382 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 return 0;
3384 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003385 break;
3386 case ExtSlice_kind:
3387 kindname = "extended slice";
3388 if (ctx != AugStore) {
3389 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3390 for (i = 0; i < n; i++) {
Anthony Baxter7b782b62006-04-11 12:01:56 +00003391 slice_ty sub = (slice_ty)asdl_seq_GET(
Jeremy Hylton819de6c2007-02-27 16:13:23 +00003392 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003393 if (!compiler_visit_nested_slice(c, sub, ctx))
3394 return 0;
3395 }
3396 ADDOP_I(c, BUILD_TUPLE, n);
3397 }
3398 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003399 default:
3400 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003401 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003402 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003404 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405}
3406
Neal Norwitzf733a012006-10-29 18:30:10 +00003407
3408/* End of the compiler section, beginning of the assembler section */
3409
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410/* do depth-first search of basic block graph, starting with block.
3411 post records the block indices in post-order.
3412
3413 XXX must handle implicit jumps from one block to next
3414*/
3415
Neal Norwitzf733a012006-10-29 18:30:10 +00003416struct assembler {
3417 PyObject *a_bytecode; /* string containing bytecode */
3418 int a_offset; /* offset into bytecode */
3419 int a_nblocks; /* number of reachable blocks */
3420 basicblock **a_postorder; /* list of blocks in dfs postorder */
3421 PyObject *a_lnotab; /* string containing lnotab */
3422 int a_lnotab_off; /* offset into lnotab */
3423 int a_lineno; /* last lineno of emitted instruction */
3424 int a_lineno_off; /* bytecode offset of last lineno */
3425};
3426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427static void
3428dfs(struct compiler *c, basicblock *b, struct assembler *a)
3429{
3430 int i;
3431 struct instr *instr = NULL;
3432
3433 if (b->b_seen)
3434 return;
3435 b->b_seen = 1;
3436 if (b->b_next != NULL)
3437 dfs(c, b->b_next, a);
3438 for (i = 0; i < b->b_iused; i++) {
3439 instr = &b->b_instr[i];
3440 if (instr->i_jrel || instr->i_jabs)
3441 dfs(c, instr->i_target, a);
3442 }
3443 a->a_postorder[a->a_nblocks++] = b;
3444}
3445
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003446static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003447stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3448{
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003449 int i, target_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450 struct instr *instr;
3451 if (b->b_seen || b->b_startdepth >= depth)
3452 return maxdepth;
3453 b->b_seen = 1;
3454 b->b_startdepth = depth;
3455 for (i = 0; i < b->b_iused; i++) {
3456 instr = &b->b_instr[i];
3457 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3458 if (depth > maxdepth)
3459 maxdepth = depth;
3460 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3461 if (instr->i_jrel || instr->i_jabs) {
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003462 target_depth = depth;
3463 if (instr->i_opcode == FOR_ITER) {
3464 target_depth = depth-2;
3465 } else if (instr->i_opcode == SETUP_FINALLY ||
3466 instr->i_opcode == SETUP_EXCEPT) {
3467 target_depth = depth+3;
3468 if (target_depth > maxdepth)
3469 maxdepth = target_depth;
3470 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 maxdepth = stackdepth_walk(c, instr->i_target,
Neil Schemenauer7fdd1cb2009-10-14 17:17:14 +00003472 target_depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473 if (instr->i_opcode == JUMP_ABSOLUTE ||
3474 instr->i_opcode == JUMP_FORWARD) {
3475 goto out; /* remaining code is dead */
3476 }
3477 }
3478 }
3479 if (b->b_next)
3480 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3481out:
3482 b->b_seen = 0;
3483 return maxdepth;
3484}
3485
3486/* Find the flow path that needs the largest stack. We assume that
3487 * cycles in the flow graph have no net effect on the stack depth.
3488 */
3489static int
3490stackdepth(struct compiler *c)
3491{
3492 basicblock *b, *entryblock;
3493 entryblock = NULL;
3494 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3495 b->b_seen = 0;
3496 b->b_startdepth = INT_MIN;
3497 entryblock = b;
3498 }
Neal Norwitzf71847e2006-07-23 07:51:58 +00003499 if (!entryblock)
3500 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 return stackdepth_walk(c, entryblock, 0, 0);
3502}
3503
3504static int
3505assemble_init(struct assembler *a, int nblocks, int firstlineno)
3506{
3507 memset(a, 0, sizeof(struct assembler));
3508 a->a_lineno = firstlineno;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003509 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 if (!a->a_bytecode)
3511 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003512 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513 if (!a->a_lnotab)
3514 return 0;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003515 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3516 PyErr_NoMemory();
3517 return 0;
3518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003519 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003520 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003521 if (!a->a_postorder) {
3522 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003523 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003524 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525 return 1;
3526}
3527
3528static void
3529assemble_free(struct assembler *a)
3530{
3531 Py_XDECREF(a->a_bytecode);
3532 Py_XDECREF(a->a_lnotab);
3533 if (a->a_postorder)
3534 PyObject_Free(a->a_postorder);
3535}
3536
3537/* Return the size of a basic block in bytes. */
3538
3539static int
3540instrsize(struct instr *instr)
3541{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003542 if (!instr->i_hasarg)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003543 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003544 if (instr->i_oparg > 0xffff)
Raymond Hettingerdff1fd92007-12-20 01:25:05 +00003545 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3546 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547}
3548
3549static int
3550blocksize(basicblock *b)
3551{
3552 int i;
3553 int size = 0;
3554
3555 for (i = 0; i < b->b_iused; i++)
3556 size += instrsize(&b->b_instr[i]);
3557 return size;
3558}
3559
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003560/* Appends a pair to the end of the line number table, a_lnotab, representing
3561 the instruction's bytecode offset and line number. See
3562 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003563
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003564static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003566{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567 int d_bytecode, d_lineno;
3568 int len;
Neal Norwitzb183a252006-04-10 01:03:32 +00003569 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570
3571 d_bytecode = a->a_offset - a->a_lineno_off;
3572 d_lineno = i->i_lineno - a->a_lineno;
3573
3574 assert(d_bytecode >= 0);
3575 assert(d_lineno >= 0);
3576
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003577 if(d_bytecode == 0 && d_lineno == 0)
3578 return 1;
3579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003581 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003583 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003584 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003585 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003586 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003587 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003588 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003589 else {
3590 PyErr_NoMemory();
3591 return 0;
3592 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003593 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003595 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003596 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003597 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003598 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003599 *lnotab++ = 255;
3600 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003601 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003602 d_bytecode -= ncodes * 255;
3603 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003604 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003605 assert(d_bytecode <= 255);
3606 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003607 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003608 nbytes = a->a_lnotab_off + 2 * ncodes;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003609 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 if (nbytes >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003611 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003612 len = nbytes;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003613 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614 len *= 2;
Gregory P. Smith9d534572008-06-11 07:41:16 +00003615 else {
3616 PyErr_NoMemory();
3617 return 0;
3618 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003619 if (_PyString_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003621 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003622 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003623 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003624 *lnotab++ = d_bytecode;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003625 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003627 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 *lnotab++ = 0;
Neal Norwitz84be93b2006-07-16 01:50:38 +00003629 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003630 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631 d_lineno -= ncodes * 255;
3632 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003633 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003634
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003635 len = PyString_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636 if (a->a_lnotab_off + 2 >= len) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003637 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003638 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003639 }
Neal Norwitzb183a252006-04-10 01:03:32 +00003640 lnotab = (unsigned char *)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003641 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003642
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003643 a->a_lnotab_off += 2;
3644 if (d_bytecode) {
3645 *lnotab++ = d_bytecode;
3646 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003647 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003648 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003649 *lnotab++ = 0;
3650 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003651 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003652 a->a_lineno = i->i_lineno;
3653 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003654 return 1;
3655}
3656
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657/* assemble_emit()
3658 Extend the bytecode with a new instruction.
3659 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003660*/
3661
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003662static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003664{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003665 int size, arg = 0, ext = 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003666 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667 char *code;
3668
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003669 size = instrsize(i);
3670 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003672 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003673 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003675 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003676 if (a->a_offset + size >= len) {
Gregory P. Smith9d534572008-06-11 07:41:16 +00003677 if (len > PY_SSIZE_T_MAX / 2)
3678 return 0;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003679 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003680 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003681 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003682 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003684 if (size == 6) {
3685 assert(i->i_hasarg);
3686 *code++ = (char)EXTENDED_ARG;
3687 *code++ = ext & 0xff;
3688 *code++ = ext >> 8;
3689 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003690 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003692 if (i->i_hasarg) {
3693 assert(size == 3 || size == 6);
3694 *code++ = arg & 0xff;
3695 *code++ = arg >> 8;
3696 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003698}
3699
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003700static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003701assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003702{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003703 basicblock *b;
Benjamin Petersoncef97822009-11-20 02:15:50 +00003704 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003705 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003706
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707 /* Compute the size of each block and fixup jump args.
3708 Replace block pointer with position in bytecode. */
Benjamin Petersoncef97822009-11-20 02:15:50 +00003709 do {
3710 totsize = 0;
3711 for (i = a->a_nblocks - 1; i >= 0; i--) {
3712 b = a->a_postorder[i];
3713 bsize = blocksize(b);
3714 b->b_offset = totsize;
3715 totsize += bsize;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003716 }
Benjamin Petersoncef97822009-11-20 02:15:50 +00003717 last_extended_arg_count = extended_arg_count;
3718 extended_arg_count = 0;
3719 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3720 bsize = b->b_offset;
3721 for (i = 0; i < b->b_iused; i++) {
3722 struct instr *instr = &b->b_instr[i];
3723 /* Relative jumps are computed relative to
3724 the instruction pointer after fetching
3725 the jump instruction.
3726 */
3727 bsize += instrsize(instr);
3728 if (instr->i_jabs)
3729 instr->i_oparg = instr->i_target->b_offset;
3730 else if (instr->i_jrel) {
3731 int delta = instr->i_target->b_offset - bsize;
3732 instr->i_oparg = delta;
3733 }
3734 else
3735 continue;
3736 if (instr->i_oparg > 0xffff)
3737 extended_arg_count++;
3738 }
3739 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003740
3741 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003742 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003743 with a better solution.
3744
Neal Norwitzf1d50682005-10-23 23:00:41 +00003745 The issue is that in the first loop blocksize() is called
3746 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003747 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003748 i_oparg is calculated in the second loop above.
3749
3750 So we loop until we stop seeing new EXTENDED_ARGs.
3751 The only EXTENDED_ARGs that could be popping up are
3752 ones in jump instructions. So this should converge
3753 fairly quickly.
3754 */
Benjamin Petersoncef97822009-11-20 02:15:50 +00003755 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003756}
3757
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003758static PyObject *
3759dict_keys_inorder(PyObject *dict, int offset)
3760{
3761 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003762 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003763
3764 tuple = PyTuple_New(size);
3765 if (tuple == NULL)
3766 return NULL;
3767 while (PyDict_Next(dict, &pos, &k, &v)) {
3768 i = PyInt_AS_LONG(v);
Benjamin Petersonda9327f2009-01-31 23:43:25 +00003769 /* The keys of the dictionary are tuples. (see compiler_add_o)
3770 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003771 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003772 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003773 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003774 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003775 PyTuple_SET_ITEM(tuple, i - offset, k);
3776 }
3777 return tuple;
3778}
3779
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003780static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003781compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003782{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783 PySTEntryObject *ste = c->u->u_ste;
3784 int flags = 0, n;
3785 if (ste->ste_type != ModuleBlock)
3786 flags |= CO_NEWLOCALS;
3787 if (ste->ste_type == FunctionBlock) {
3788 if (!ste->ste_unoptimized)
3789 flags |= CO_OPTIMIZED;
3790 if (ste->ste_nested)
3791 flags |= CO_NESTED;
3792 if (ste->ste_generator)
3793 flags |= CO_GENERATOR;
Benjamin Peterson12554cb2009-01-31 23:54:38 +00003794 if (ste->ste_varargs)
3795 flags |= CO_VARARGS;
3796 if (ste->ste_varkeywords)
3797 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003798 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003799
3800 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003801 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003802
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 n = PyDict_Size(c->u->u_freevars);
3804 if (n < 0)
3805 return -1;
3806 if (n == 0) {
3807 n = PyDict_Size(c->u->u_cellvars);
3808 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003809 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 if (n == 0) {
3811 flags |= CO_NOFREE;
3812 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003813 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003814
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003815 return flags;
3816}
3817
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818static PyCodeObject *
3819makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003820{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 PyObject *tmp;
3822 PyCodeObject *co = NULL;
3823 PyObject *consts = NULL;
3824 PyObject *names = NULL;
3825 PyObject *varnames = NULL;
3826 PyObject *filename = NULL;
3827 PyObject *name = NULL;
3828 PyObject *freevars = NULL;
3829 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003830 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003831 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003832
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833 tmp = dict_keys_inorder(c->u->u_consts, 0);
3834 if (!tmp)
3835 goto error;
3836 consts = PySequence_List(tmp); /* optimize_code requires a list */
3837 Py_DECREF(tmp);
3838
3839 names = dict_keys_inorder(c->u->u_names, 0);
3840 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3841 if (!consts || !names || !varnames)
3842 goto error;
3843
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003844 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3845 if (!cellvars)
3846 goto error;
3847 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3848 if (!freevars)
3849 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003850 filename = PyString_FromString(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851 if (!filename)
3852 goto error;
3853
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003854 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003855 flags = compute_code_flags(c);
3856 if (flags < 0)
3857 goto error;
3858
Jeremy Hylton644dddc2006-08-21 16:19:37 +00003859 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860 if (!bytecode)
3861 goto error;
3862
3863 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3864 if (!tmp)
3865 goto error;
3866 Py_DECREF(consts);
3867 consts = tmp;
3868
3869 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3870 bytecode, consts, names, varnames,
3871 freevars, cellvars,
3872 filename, c->u->u_name,
3873 c->u->u_firstlineno,
3874 a->a_lnotab);
3875 error:
3876 Py_XDECREF(consts);
3877 Py_XDECREF(names);
3878 Py_XDECREF(varnames);
3879 Py_XDECREF(filename);
3880 Py_XDECREF(name);
3881 Py_XDECREF(freevars);
3882 Py_XDECREF(cellvars);
3883 Py_XDECREF(bytecode);
3884 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003885}
3886
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003887
3888/* For debugging purposes only */
3889#if 0
3890static void
3891dump_instr(const struct instr *i)
3892{
3893 const char *jrel = i->i_jrel ? "jrel " : "";
3894 const char *jabs = i->i_jabs ? "jabs " : "";
3895 char arg[128];
3896
3897 *arg = '\0';
3898 if (i->i_hasarg)
3899 sprintf(arg, "arg: %d ", i->i_oparg);
3900
3901 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3902 i->i_lineno, i->i_opcode, arg, jabs, jrel);
3903}
3904
3905static void
3906dump_basicblock(const basicblock *b)
3907{
3908 const char *seen = b->b_seen ? "seen " : "";
3909 const char *b_return = b->b_return ? "return " : "";
3910 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3911 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3912 if (b->b_instr) {
3913 int i;
3914 for (i = 0; i < b->b_iused; i++) {
3915 fprintf(stderr, " [%02d] ", i);
3916 dump_instr(b->b_instr + i);
3917 }
3918 }
3919}
3920#endif
3921
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922static PyCodeObject *
3923assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003924{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925 basicblock *b, *entryblock;
3926 struct assembler a;
3927 int i, j, nblocks;
3928 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003929
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930 /* Make sure every block that falls off the end returns None.
3931 XXX NEXT_BLOCK() isn't quite right, because if the last
3932 block ends with a jump or return b_next shouldn't set.
3933 */
3934 if (!c->u->u_curblock->b_return) {
3935 NEXT_BLOCK(c);
3936 if (addNone)
3937 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3938 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003939 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003940
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 nblocks = 0;
3942 entryblock = NULL;
3943 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3944 nblocks++;
3945 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003946 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003947
Neal Norwitzed657552006-07-10 00:04:44 +00003948 /* Set firstlineno if it wasn't explicitly set. */
3949 if (!c->u->u_firstlineno) {
3950 if (entryblock && entryblock->b_instr)
3951 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3952 else
3953 c->u->u_firstlineno = 1;
3954 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003955 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3956 goto error;
3957 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003958
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003960 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962 /* Emit code in reverse postorder from dfs. */
3963 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003964 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 for (j = 0; j < b->b_iused; j++)
3966 if (!assemble_emit(&a, &b->b_instr[j]))
3967 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00003968 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003969
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003970 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003971 goto error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00003972 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003974
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 co = makecode(c, &a);
3976 error:
3977 assemble_free(&a);
3978 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003979}