blob: 119c60f9b23b4dd01c12a846d933e22bf5ed8275 [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
Antoine Pitrouc83ea132010-05-09 14:46:46 +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
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000047 unsigned i_jabs : 1;
48 unsigned i_jrel : 1;
49 unsigned i_hasarg : 1;
50 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. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +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. */
71 unsigned b_seen : 1;
72 /* b_return is true if a RETURN_VALUE opcode is inserted. */
73 unsigned b_return : 1;
74 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
77 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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 enum fblocktype fb_type;
91 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092};
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 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000098 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000100 PyObject *u_name;
101 /* The following fields are dicts that map objects to
102 the index of them in co_XXX. The index is used as
103 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 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000110
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000111 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000113 int u_argcount; /* number of arguments for block */
114 /* Pointer to the most recently allocated block. By following b_list
115 members, you can reach all early allocated blocks. */
116 basicblock *u_blocks;
117 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 int u_nfblocks;
120 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000122 int u_firstlineno; /* the first lineno of the block */
123 int u_lineno; /* the lineno for the current stmt */
124 bool u_lineno_set; /* boolean to indicate whether instr
125 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000126};
127
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000128/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000130The u pointer points to the current compilation unit, while units
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000131for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000133*/
134
135struct compiler {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000136 const char *c_filename;
137 struct symtable *c_st;
138 PyFutureFeatures *c_future; /* pointer to module's __future__ */
139 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000141 int c_interactive; /* true if in interactive mode */
142 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000143
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000144 struct compiler_unit *u; /* compiler state for current block */
145 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
146 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,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000167 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168
169static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000170 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000172 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000189 /* Name mangling: __private becomes _classname__private.
190 This is independent from how the name is used. */
191 const char *p, *name = PyString_AsString(ident);
192 char *buffer;
193 size_t nlen, plen;
194 if (privateobj == NULL || !PyString_Check(privateobj) ||
195 name == NULL || name[0] != '_' || name[1] != '_') {
196 Py_INCREF(ident);
197 return ident;
198 }
199 p = PyString_AsString(privateobj);
200 nlen = strlen(name);
201 /* Don't mangle __id__ or names with dots.
Jeremy Hylton37075c52007-02-27 01:01:59 +0000202
Antoine Pitrouc83ea132010-05-09 14:46:46 +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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000207 TODO(jhylton): Decide whether we want to support
208 mangling of the module name, e.g. __M.X.
209 */
210 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
211 || strchr(name, '.')) {
212 Py_INCREF(ident);
213 return ident; /* Don't mangle __whatever__ */
214 }
215 /* Strip leading underscores from class name */
216 while (*p == '_')
217 p++;
218 if (*p == '\0') {
219 Py_INCREF(ident);
220 return ident; /* Don't mangle if class is just underscores */
221 }
222 plen = strlen(p);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000223
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000224 assert(1 <= PY_SSIZE_T_MAX - nlen);
225 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Gregory P. Smith9d534572008-06-11 07:41:16 +0000226
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000227 ident = PyString_FromStringAndSize(NULL, 1 + nlen + plen);
228 if (!ident)
229 return 0;
230 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
231 buffer = PyString_AS_STRING(ident);
232 buffer[0] = '_';
233 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000241 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000242
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 c->c_stack = PyList_New(0);
244 if (!c->c_stack)
245 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000247 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248}
249
250PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000251PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000252 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000253{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000254 struct compiler c;
255 PyCodeObject *co = NULL;
256 PyCompilerFlags local_flags;
257 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 if (!__doc__) {
260 __doc__ = PyString_InternFromString("__doc__");
261 if (!__doc__)
262 return NULL;
263 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 if (!compiler_init(&c))
266 return NULL;
267 c.c_filename = filename;
268 c.c_arena = arena;
269 c.c_future = PyFuture_FromAST(mod, filename);
270 if (c.c_future == NULL)
271 goto finally;
272 if (!flags) {
273 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000282 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");
286 goto finally;
287 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000289 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
Thomas Wouters1175c432006-02-27 22:49:54 +0000291 finally:
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000292 compiler_free(&c);
293 assert(co || PyErr_Occurred());
294 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295}
296
297PyCodeObject *
298PyNode_Compile(struct _node *n, const char *filename)
299{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000300 PyCodeObject *co = NULL;
301 mod_ty mod;
302 PyArena *arena = PyArena_New();
303 if (!arena)
304 return NULL;
305 mod = PyAST_FromNode(n, NULL, filename, arena);
306 if (mod)
307 co = PyAST_Compile(mod, filename, NULL, arena);
308 PyArena_Free(arena);
309 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 if (c->c_st)
316 PySymtable_Free(c->c_st);
317 if (c->c_future)
318 PyObject_Free(c->c_future);
319 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000325 Py_ssize_t i, n;
326 PyObject *v, *k;
327 PyObject *dict = PyDict_New();
328 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000329
Antoine Pitrouc83ea132010-05-09 14:46:46 +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;
336 }
337 k = PyList_GET_ITEM(list, i);
338 k = PyTuple_Pack(2, k, k->ob_type);
339 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 }
345 Py_DECREF(k);
346 Py_DECREF(v);
347 }
348 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349}
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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000355values 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000362 Py_ssize_t pos = 0, i = offset, scope;
363 PyObject *k, *v, *dest = PyDict_New();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000365 assert(offset >= 0);
366 if (dest == NULL)
367 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000369 while (PyDict_Next(src, &pos, &k, &v)) {
370 /* 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +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++;
381 tuple = PyTuple_Pack(2, k, k->ob_type);
382 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
383 Py_DECREF(item);
384 Py_DECREF(dest);
385 Py_XDECREF(tuple);
386 return NULL;
387 }
388 Py_DECREF(item);
389 Py_DECREF(tuple);
390 }
391 }
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 basicblock *block;
399 for (block = u->u_blocks; block != NULL; block = block->b_list) {
400 assert((void *)block != (void *)0xcbcbcbcb);
401 assert((void *)block != (void *)0xfbfbfbfb);
402 assert((void *)block != (void *)0xdbdbdbdb);
403 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 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000413}
414
415static void
416compiler_unit_free(struct compiler_unit *u)
417{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000420 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 }
429 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);
437 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000438}
439
440static int
441compiler_enter_scope(struct compiler *c, identifier name, void *key,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000442 int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000444 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000446 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
447 struct compiler_unit));
448 if (!u) {
449 PyErr_NoMemory();
450 return 0;
451 }
452 memset(u, 0, sizeof(struct compiler_unit));
453 u->u_argcount = 0;
454 u->u_ste = PySymtable_Lookup(c->c_st, key);
455 if (!u->u_ste) {
456 compiler_unit_free(u);
457 return 0;
458 }
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);
463 if (!u->u_varnames || !u->u_cellvars) {
464 compiler_unit_free(u);
465 return 0;
466 }
Neal Norwitzd12bd012006-07-21 07:59:47 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
469 PyDict_Size(u->u_cellvars));
470 if (!u->u_freevars) {
471 compiler_unit_free(u);
472 return 0;
473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000475 u->u_blocks = NULL;
476 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) {
482 compiler_unit_free(u);
483 return 0;
484 }
485 u->u_names = PyDict_New();
486 if (!u->u_names) {
487 compiler_unit_free(u);
488 return 0;
489 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000491 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000493 /* Push the old compiler_unit on the stack. */
494 if (c->u) {
495 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);
498 compiler_unit_free(u);
499 return 0;
500 }
501 Py_DECREF(capsule);
502 u->u_private = c->u->u_private;
503 Py_XINCREF(u->u_private);
504 }
505 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000507 c->c_nestlevel++;
508 if (compiler_use_new_block(c) == NULL)
509 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000511 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000512}
513
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000514static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515compiler_exit_scope(struct compiler *c)
516{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000517 int n;
518 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000520 c->c_nestlevel--;
521 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) {
525 capsule = PyList_GET_ITEM(c->c_stack, n);
526 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
527 assert(c->u);
528 /* we are deleting from a list so this really shouldn't fail */
529 if (PySequence_DelItem(c->c_stack, n) < 0)
530 Py_FatalError("compiler_exit_scope()");
531 compiler_unit_check(c->u);
532 }
533 else
534 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000545 basicblock *b;
546 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000548 u = c->u;
549 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
550 if (b == NULL) {
551 PyErr_NoMemory();
552 return NULL;
553 }
554 memset((void *)b, 0, sizeof(basicblock));
555 /* Extend the singly linked list of blocks with new block. */
556 b->b_list = u->u_blocks;
557 u->u_blocks = b;
558 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559}
560
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561static basicblock *
562compiler_use_new_block(struct compiler *c)
563{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000564 basicblock *block = compiler_new_block(c);
565 if (block == NULL)
566 return NULL;
567 c->u->u_curblock = block;
568 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569}
570
571static basicblock *
572compiler_next_block(struct compiler *c)
573{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000574 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580}
581
582static basicblock *
583compiler_use_next_block(struct compiler *c, basicblock *block)
584{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000585 assert(block != NULL);
586 c->u->u_curblock->b_next = block;
587 c->u->u_curblock = block;
588 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000599 assert(b != NULL);
600 if (b->b_instr == NULL) {
601 b->b_instr = (struct instr *)PyObject_Malloc(
602 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
603 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);
610 }
611 else if (b->b_iused == b->b_ialloc) {
612 struct instr *tmp;
613 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000617 if (oldsize > (PY_SIZE_MAX >> 1)) {
618 PyErr_NoMemory();
619 return -1;
620 }
Gregory P. Smith9d534572008-06-11 07:41:16 +0000621
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000622 if (newsize == 0) {
623 PyErr_NoMemory();
624 return -1;
625 }
626 b->b_ialloc <<= 1;
627 tmp = (struct instr *)PyObject_Realloc(
628 (void *)b->b_instr, newsize);
629 if (tmp == NULL) {
630 PyErr_NoMemory();
631 return -1;
632 }
633 b->b_instr = tmp;
634 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
635 }
636 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000654 basicblock *b;
655 if (c->u->u_lineno_set)
656 return;
657 c->u->u_lineno_set = true;
658 b = c->u->u_curblock;
659 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000665 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000675
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000676 case UNARY_POSITIVE:
677 case UNARY_NEGATIVE:
678 case UNARY_NOT:
679 case UNARY_CONVERT:
680 case UNARY_INVERT:
681 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000683 case SET_ADD:
684 case LIST_APPEND:
685 return -1;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000686
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000687 case MAP_ADD:
688 return -2;
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000689
Antoine Pitrouc83ea132010-05-09 14:46:46 +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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000704 case SLICE+0:
705 return 0;
706 case SLICE+1:
707 return -1;
708 case SLICE+2:
709 return -1;
710 case SLICE+3:
711 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000713 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000731 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;
739 case STORE_MAP:
740 return -2;
741 case DELETE_SUBSCR:
742 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000744 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000754
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000755 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;
773 case SETUP_WITH:
774 return 4;
775 case WITH_CLEANUP:
776 return -1; /* XXX Sometimes more */
777 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000787
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000788 case POP_BLOCK:
789 return 0;
790 case END_FINALLY:
791 return -3; /* or -1 or -2 if no exception occurred or
792 return/break/continue */
793 case BUILD_CLASS:
794 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 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:
803 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 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:
821 case BUILD_SET:
822 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:
830 return -1;
831 case IMPORT_FROM:
832 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000834 case JUMP_FORWARD:
835 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
836 case JUMP_IF_FALSE_OR_POP: /* "" */
837 case JUMP_ABSOLUTE:
838 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000840 case POP_JUMP_IF_FALSE:
841 case POP_JUMP_IF_TRUE:
842 return -1;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +0000843
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000844 case LOAD_GLOBAL:
845 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000846
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847 case CONTINUE_LOOP:
848 return 0;
849 case SETUP_LOOP:
850 case SETUP_EXCEPT:
851 case SETUP_FINALLY:
852 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000853
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000854 case LOAD_FAST:
855 return 1;
856 case STORE_FAST:
857 return -1;
858 case DELETE_FAST:
859 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000861 case RAISE_VARARGS:
862 return -oparg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863#define NARGS(o) (((o) % 256) + 2*((o) / 256))
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000864 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871#undef NARGS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000872 case MAKE_FUNCTION:
873 return -oparg;
874 case BUILD_SLICE:
875 if (oparg == 3)
876 return -2;
877 else
878 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000879
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000880 case MAKE_CLOSURE:
881 return -oparg-1;
882 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()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000892 }
893 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000894}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000903 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917}
918
919static int
920compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
921{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000922 PyObject *t, *v;
923 Py_ssize_t arg;
924 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000926 /* necessary to make sure types aren't coerced (e.g., int and long) */
927 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
928 if (PyFloat_Check(o)) {
929 d = PyFloat_AS_DOUBLE(o);
930 /* 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 */
933 if (d == 0.0 && copysign(1.0, d) < 0.0)
934 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000939 else if (PyComplex_Check(o)) {
940 Py_complex z;
941 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.*/
946 z = PyComplex_AsCComplex(o);
947 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 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 else if (imag_negzero) {
954 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
955 }
956 else if (real_negzero) {
957 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
958 }
959 else {
960 t = PyTuple_Pack(2, o, o->ob_type);
961 }
962 }
Mark Dickinson026ac7c2009-10-15 17:45:39 +0000963#endif /* WITHOUT_COMPLEX */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 else {
965 t = PyTuple_Pack(2, o, o->ob_type);
966 }
967 if (t == NULL)
968 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000970 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;
977 }
978 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);
988 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989}
990
991static int
992compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994{
995 int arg = compiler_add_o(c, dict, o);
996 if (arg < 0)
Antoine Pitroua4d36a92010-06-22 21:42: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,
Antoine Pitrouc83ea132010-05-09 14:46:46 +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)
Antoine Pitroua4d36a92010-06-22 21:42: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)
Antoine Pitroua4d36a92010-06-22 21:42: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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001023 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001034}
1035
1036static int
1037compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1038{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001039 struct instr *i;
1040 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001042 assert(b != NULL);
1043 off = compiler_next_instr(c, c->u->u_curblock);
1044 if (off < 0)
1045 return 0;
1046 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;
1054 compiler_set_lineno(c, off);
1055 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056}
1057
Antoine Pitrouc83ea132010-05-09 14:46:46 +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) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +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) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 if (compiler_next_block((C)) == NULL) \
1076 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
1079#define ADDOP(C, OP) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001080 if (!compiler_addop((C), (OP))) \
1081 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082}
1083
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001084#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001085 if (!compiler_addop((C), (OP))) { \
1086 compiler_exit_scope(c); \
1087 return 0; \
1088 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001089}
1090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001092 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1093 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094}
1095
1096#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001097 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1098 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099}
1100
1101#define ADDOP_I(C, OP, O) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001102 if (!compiler_addop_i((C), (OP), (O))) \
1103 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104}
1105
1106#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001107 if (!compiler_addop_j((C), (OP), (O), 1)) \
1108 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109}
1110
1111#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001112 if (!compiler_addop_j((C), (OP), (O), 0)) \
1113 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114}
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) {\
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 if (!compiler_visit_ ## TYPE((C), (V))) \
1122 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123}
1124
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001125#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001126 if (!compiler_visit_ ## TYPE((C), (V))) { \
1127 compiler_exit_scope(c); \
1128 return 0; \
1129 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001130}
1131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001133 if (!compiler_visit_slice((C), (V), (CTX))) \
1134 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135}
1136
1137#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001138 int _i; \
1139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1142 if (!compiler_visit_ ## TYPE((C), elt)) \
1143 return 0; \
1144 } \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001145}
1146
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001147#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001148 int _i; \
1149 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1150 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1151 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1152 if (!compiler_visit_ ## TYPE((C), elt)) { \
1153 compiler_exit_scope(c); \
1154 return 0; \
1155 } \
1156 } \
Anthony Baxter7b782b62006-04-11 12:01:56 +00001157}
1158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159static int
1160compiler_isdocstring(stmt_ty s)
1161{
1162 if (s->kind != Expr_kind)
Antoine Pitroua4d36a92010-06-22 21:42: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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001172 int i = 0;
1173 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001175 if (!asdl_seq_LEN(stmts))
1176 return 1;
1177 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1178 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1179 /* don't generate docstrings if -OO */
1180 i = 1;
1181 VISIT(c, expr, st->v.Expr.value);
1182 if (!compiler_nameop(c, __doc__, Store))
1183 return 0;
1184 }
1185 for (; i < asdl_seq_LEN(stmts); i++)
1186 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1187 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188}
1189
1190static PyCodeObject *
1191compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001192{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001193 PyCodeObject *co;
1194 int addNone = 1;
1195 static PyObject *module;
1196 if (!module) {
1197 module = PyString_InternFromString("<module>");
1198 if (!module)
1199 return NULL;
1200 }
1201 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1202 if (!compiler_enter_scope(c, module, mod, 0))
1203 return NULL;
1204 switch (mod->kind) {
1205 case Module_kind:
1206 if (!compiler_body(c, mod->v.Module.body)) {
1207 compiler_exit_scope(c);
1208 return 0;
1209 }
1210 break;
1211 case Interactive_kind:
1212 c->c_interactive = 1;
1213 VISIT_SEQ_IN_SCOPE(c, stmt,
1214 mod->v.Interactive.body);
1215 break;
1216 case Expression_kind:
1217 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1218 addNone = 0;
1219 break;
1220 case Suite_kind:
1221 PyErr_SetString(PyExc_SystemError,
1222 "suite should not be possible");
1223 return 0;
1224 default:
1225 PyErr_Format(PyExc_SystemError,
1226 "module kind %d should not be possible",
1227 mod->kind);
1228 return 0;
1229 }
1230 co = assemble(c, addNone);
1231 compiler_exit_scope(c);
1232 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001233}
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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001243 int scope = PyST_GetScope(c->u->u_ste, name);
1244 if (scope == 0) {
1245 char buf[350];
1246 PyOS_snprintf(buf, sizeof(buf),
1247 "unknown scope for %.100s in %.100s(%s) in %s\n"
1248 "symbols: %s\nlocals: %s\nglobals: %s",
1249 PyString_AS_STRING(name),
1250 PyString_AS_STRING(c->u->u_name),
1251 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)
1256 );
1257 Py_FatalError(buf);
1258 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001259
Antoine Pitrouc83ea132010-05-09 14:46:46 +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)
Antoine Pitroua4d36a92010-06-22 21:42: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)
Antoine Pitroua4d36a92010-06-22 21:42: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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001280 int i, free = PyCode_GetNumFree(co);
1281 if (free == 0) {
1282 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1283 ADDOP_I(c, MAKE_FUNCTION, args);
1284 return 1;
1285 }
1286 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001292
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001293 /* 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),
1308 PyString_AS_STRING(c->u->u_name),
1309 reftype, arg,
1310 PyString_AS_STRING(co->co_name),
1311 PyObject_REPR(co->co_freevars));
1312 Py_FatalError("compiler_make_closure()");
1313 }
1314 ADDOP_I(c, LOAD_CLOSURE, arg);
1315 }
1316 ADDOP_I(c, BUILD_TUPLE, free);
1317 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1318 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001325 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001326
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001327 if (!decos)
1328 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001329
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1331 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1332 }
1333 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334}
1335
1336static int
1337compiler_arguments(struct compiler *c, arguments_ty args)
1338{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001339 int i;
1340 int n = asdl_seq_LEN(args->args);
1341 /* Correctly handle nested argument lists */
1342 for (i = 0; i < n; i++) {
1343 expr_ty arg = (expr_ty)asdl_seq_GET(args->args, i);
1344 if (arg->kind == Tuple_kind) {
1345 PyObject *id = PyString_FromFormat(".%d", i);
1346 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);
1354 VISIT(c, expr, arg);
1355 }
1356 }
1357 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001358}
1359
1360static int
1361compiler_function(struct compiler *c, stmt_ty s)
1362{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001363 PyCodeObject *co;
1364 PyObject *first_const = Py_None;
1365 arguments_ty args = s->v.FunctionDef.args;
1366 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1367 stmt_ty st;
1368 int i, n, docstring;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001370 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001372 if (!compiler_decorators(c, decos))
1373 return 0;
1374 if (args->defaults)
1375 VISIT_SEQ(c, expr, args->defaults);
1376 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1377 s->lineno))
1378 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001379
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001380 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1381 docstring = compiler_isdocstring(st);
1382 if (docstring && Py_OptimizeFlag < 2)
1383 first_const = st->v.Expr.value->v.Str.s;
1384 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1385 compiler_exit_scope(c);
1386 return 0;
1387 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001389 /* unpack nested arguments */
1390 compiler_arguments(c, args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001391
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001392 c->u->u_argcount = asdl_seq_LEN(args->args);
1393 n = asdl_seq_LEN(s->v.FunctionDef.body);
1394 /* if there was a docstring, we need to skip the first statement */
1395 for (i = docstring; i < n; i++) {
1396 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1397 VISIT_IN_SCOPE(c, stmt, st);
1398 }
1399 co = assemble(c, 1);
1400 compiler_exit_scope(c);
1401 if (co == NULL)
1402 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001403
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001404 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1405 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001407 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1408 ADDOP_I(c, CALL_FUNCTION, 1);
1409 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001411 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412}
1413
1414static int
1415compiler_class(struct compiler *c, stmt_ty s)
1416{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 int n, i;
1418 PyCodeObject *co;
1419 PyObject *str;
1420 asdl_seq* decos = s->v.ClassDef.decorator_list;
Christian Heimes5224d282008-02-23 15:01:05 +00001421
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001422 if (!compiler_decorators(c, decos))
1423 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001424
Antoine Pitrouc83ea132010-05-09 14:46:46 +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)
1430 VISIT_SEQ(c, expr, s->v.ClassDef.bases);
1431 ADDOP_I(c, BUILD_TUPLE, n);
1432 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s,
1433 s->lineno))
1434 return 0;
1435 Py_XDECREF(c->u->u_private);
1436 c->u->u_private = s->v.ClassDef.name;
1437 Py_INCREF(c->u->u_private);
1438 str = PyString_InternFromString("__name__");
1439 if (!str || !compiler_nameop(c, str, Load)) {
1440 Py_XDECREF(str);
1441 compiler_exit_scope(c);
1442 return 0;
1443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001445 Py_DECREF(str);
1446 str = PyString_InternFromString("__module__");
1447 if (!str || !compiler_nameop(c, str, Store)) {
1448 Py_XDECREF(str);
1449 compiler_exit_scope(c);
1450 return 0;
1451 }
1452 Py_DECREF(str);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001453
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001454 if (!compiler_body(c, s->v.ClassDef.body)) {
1455 compiler_exit_scope(c);
1456 return 0;
1457 }
Neal Norwitz4737b232005-11-19 23:58:29 +00001458
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001459 ADDOP_IN_SCOPE(c, LOAD_LOCALS);
1460 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1461 co = assemble(c, 1);
1462 compiler_exit_scope(c);
1463 if (co == NULL)
1464 return 0;
1465
1466 compiler_make_closure(c, co, 0);
1467 Py_DECREF(co);
1468
1469 ADDOP_I(c, CALL_FUNCTION, 0);
1470 ADDOP(c, BUILD_CLASS);
1471 /* apply decorators */
1472 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1473 ADDOP_I(c, CALL_FUNCTION, 1);
1474 }
1475 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1476 return 0;
1477 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001478}
1479
1480static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001481compiler_ifexp(struct compiler *c, expr_ty e)
1482{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 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);
1493 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1494 VISIT(c, expr, e->v.IfExp.body);
1495 ADDOP_JREL(c, JUMP_FORWARD, end);
1496 compiler_use_next_block(c, next);
1497 VISIT(c, expr, e->v.IfExp.orelse);
1498 compiler_use_next_block(c, end);
1499 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001500}
1501
1502static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503compiler_lambda(struct compiler *c, expr_ty e)
1504{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001505 PyCodeObject *co;
1506 static identifier name;
1507 arguments_ty args = e->v.Lambda.args;
1508 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001510 if (!name) {
1511 name = PyString_InternFromString("<lambda>");
1512 if (!name)
1513 return 0;
1514 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001516 if (args->defaults)
1517 VISIT_SEQ(c, expr, args->defaults);
1518 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1519 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001520
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001521 /* unpack nested arguments */
1522 compiler_arguments(c, args);
Benjamin Peterson0dee9c12010-03-17 20:41:42 +00001523
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001524 /* 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001529 c->u->u_argcount = asdl_seq_LEN(args->args);
1530 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1531 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 }
1537 co = assemble(c, 1);
1538 compiler_exit_scope(c);
1539 if (co == NULL)
1540 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001542 compiler_make_closure(c, co, asdl_seq_LEN(args->defaults));
1543 Py_DECREF(co);
1544
1545 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001546}
1547
1548static int
1549compiler_print(struct compiler *c, stmt_ty s)
1550{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001551 int i, n;
1552 bool dest;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001554 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001583}
1584
1585static int
1586compiler_if(struct compiler *c, stmt_ty s)
1587{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001588 basicblock *end, *next;
1589 int constant;
1590 assert(s->kind == If_kind);
1591 end = compiler_new_block(c);
1592 if (end == NULL)
1593 return 0;
1594
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 {
1605 if (s->v.If.orelse) {
1606 next = compiler_new_block(c);
1607 if (next == NULL)
1608 return 0;
1609 }
1610 else
1611 next = end;
1612 VISIT(c, expr, s->v.If.test);
1613 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1614 VISIT_SEQ(c, stmt, s->v.If.body);
1615 ADDOP_JREL(c, JUMP_FORWARD, end);
1616 if (s->v.If.orelse) {
1617 compiler_use_next_block(c, next);
1618 VISIT_SEQ(c, stmt, s->v.If.orelse);
1619 }
1620 }
1621 compiler_use_next_block(c, end);
1622 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623}
1624
1625static int
1626compiler_for(struct compiler *c, stmt_ty s)
1627{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001628 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001630 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);
1643 VISIT_SEQ(c, stmt, s->v.For.body);
1644 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);
1648 VISIT_SEQ(c, stmt, s->v.For.orelse);
1649 compiler_use_next_block(c, end);
1650 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651}
1652
1653static int
1654compiler_while(struct compiler *c, stmt_ty s)
1655{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001656 basicblock *loop, *orelse, *end, *anchor = NULL;
1657 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001658
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001659 if (constant == 0) {
1660 if (s->v.While.orelse)
1661 VISIT_SEQ(c, stmt, s->v.While.orelse);
1662 return 1;
1663 }
1664 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001681 ADDOP_JREL(c, SETUP_LOOP, end);
1682 compiler_use_next_block(c, loop);
1683 if (!compiler_push_fblock(c, LOOP, loop))
1684 return 0;
1685 if (constant == -1) {
1686 VISIT(c, expr, s->v.While.test);
1687 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1688 }
1689 VISIT_SEQ(c, stmt, s->v.While.body);
1690 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 /* XXX should the two POP instructions be in a separate block
1693 if there is no else clause ?
1694 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001696 if (constant == -1) {
1697 compiler_use_next_block(c, anchor);
1698 ADDOP(c, POP_BLOCK);
1699 }
1700 compiler_pop_fblock(c, LOOP, loop);
1701 if (orelse != NULL) /* what if orelse is just pass? */
1702 VISIT_SEQ(c, stmt, s->v.While.orelse);
1703 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001705 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706}
1707
1708static int
1709compiler_continue(struct compiler *c)
1710{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001711 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1712 static const char IN_FINALLY_ERROR_MSG[] =
1713 "'continue' not supported inside 'finally' clause";
1714 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001716 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:
1725 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 }
1731 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:
1736 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001739 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740}
1741
1742/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001743
1744 SETUP_FINALLY L
1745 <code for body>
1746 POP_BLOCK
1747 LOAD_CONST <None>
1748 L: <code for finalbody>
1749 END_FINALLY
1750
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751 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).
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001755
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001756 SETUP_FINALLY:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001757 Pushes the current value stack level and the label
1758 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001759 POP_BLOCK:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001760 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.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763 END_FINALLY:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001764 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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001778 basicblock *body, *end;
1779 body = compiler_new_block(c);
1780 end = compiler_new_block(c);
1781 if (body == NULL || end == NULL)
1782 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001784 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;
1788 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1789 ADDOP(c, POP_BLOCK);
1790 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001792 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;
1796 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1797 ADDOP(c, END_FINALLY);
1798 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001800 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801}
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.)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001808
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
1818 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1819 [tb, val, exc] POP
1820 [tb, val] <assign to V1> (or POP if no V1)
1821 [tb] POP
1822 [] <code for S1>
1823 JUMP_FORWARD L0
1824
1825 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826 .............................etc.......................
1827
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001828 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1829
1830 [] L0: <next statement>
1831
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832 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{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001837 basicblock *body, *orelse, *except, *end;
1838 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001840 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;
1850 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1851 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++) {
1857 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1858 s->v.TryExcept.handlers, i);
1859 if (!handler->v.ExceptHandler.type && i < n-1)
1860 return compiler_error(c, "default 'except:' must be last");
1861 c->u->u_lineno_set = false;
1862 c->u->u_lineno = handler->lineno;
1863 except = compiler_new_block(c);
1864 if (except == NULL)
1865 return 0;
1866 if (handler->v.ExceptHandler.type) {
1867 ADDOP(c, DUP_TOP);
1868 VISIT(c, expr, handler->v.ExceptHandler.type);
1869 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1870 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1871 }
1872 ADDOP(c, POP_TOP);
1873 if (handler->v.ExceptHandler.name) {
1874 VISIT(c, expr, handler->v.ExceptHandler.name);
1875 }
1876 else {
1877 ADDOP(c, POP_TOP);
1878 }
1879 ADDOP(c, POP_TOP);
1880 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
1881 ADDOP_JREL(c, JUMP_FORWARD, end);
1882 compiler_use_next_block(c, except);
1883 }
1884 ADDOP(c, END_FINALLY);
1885 compiler_use_next_block(c, orelse);
1886 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
1887 compiler_use_next_block(c, end);
1888 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001889}
1890
1891static int
1892compiler_import_as(struct compiler *c, identifier name, identifier asname)
1893{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001894 /* The IMPORT_NAME opcode was already generated. This function
1895 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001897 If there is a dot in name, we need to split it and emit a
1898 LOAD_ATTR for each name.
1899 */
1900 const char *src = PyString_AS_STRING(name);
1901 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 */
1907 PyObject *attr;
1908 dot = strchr(src, '.');
1909 attr = PyString_FromStringAndSize(src,
1910 dot ? dot - src : strlen(src));
1911 if (!attr)
1912 return -1;
1913 ADDOP_O(c, LOAD_ATTR, attr, names);
1914 Py_DECREF(attr);
1915 src = dot + 1;
1916 }
1917 }
1918 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919}
1920
1921static int
1922compiler_import(struct compiler *c, stmt_ty s)
1923{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001924 /* 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
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001933 for (i = 0; i < n; i++) {
1934 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
1935 int r;
1936 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001938 if (c->c_flags && (c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1939 level = PyInt_FromLong(0);
1940 else
1941 level = PyInt_FromLong(-1);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001942
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001943 if (level == NULL)
1944 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001946 ADDOP_O(c, LOAD_CONST, level, consts);
1947 Py_DECREF(level);
1948 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1949 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001951 if (alias->asname) {
1952 r = compiler_import_as(c, alias->name, alias->asname);
1953 if (!r)
1954 return r;
1955 }
1956 else {
1957 identifier tmp = alias->name;
1958 const char *base = PyString_AS_STRING(alias->name);
1959 char *dot = strchr(base, '.');
1960 if (dot)
1961 tmp = PyString_FromStringAndSize(base,
1962 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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001972}
1973
1974static int
1975compiler_from_import(struct compiler *c, stmt_ty s)
1976{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001977 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001978
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001979 PyObject *names = PyTuple_New(n);
1980 PyObject *level;
1981 static PyObject *empty_string;
Benjamin Petersona72be3b2009-06-13 20:23:33 +00001982
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001983 if (!empty_string) {
1984 empty_string = PyString_FromString("");
1985 if (!empty_string)
1986 return 0;
1987 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001988
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001989 if (!names)
1990 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001991
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001992 if (s->v.ImportFrom.level == 0 && c->c_flags &&
1993 !(c->c_flags->cf_flags & CO_FUTURE_ABSOLUTE_IMPORT))
1994 level = PyInt_FromLong(-1);
1995 else
1996 level = PyInt_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00001997
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001998 if (!level) {
1999 Py_DECREF(names);
2000 return 0;
2001 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002002
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002003 /* build up the names */
2004 for (i = 0; i < n; i++) {
2005 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2006 Py_INCREF(alias->name);
2007 PyTuple_SET_ITEM(names, i, alias->name);
2008 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 "
2015 "at the beginning of the file");
2016 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002018 ADDOP_O(c, LOAD_CONST, level, consts);
2019 Py_DECREF(level);
2020 ADDOP_O(c, LOAD_CONST, names, consts);
2021 Py_DECREF(names);
2022 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 }
2028 for (i = 0; i < n; i++) {
2029 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2030 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002031
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002032 if (i == 0 && *PyString_AS_STRING(alias->name) == '*') {
2033 assert(n == 1);
2034 ADDOP(c, IMPORT_STAR);
2035 return 1;
2036 }
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 }
2048 /* remove imported module */
2049 ADDOP(c, POP_TOP);
2050 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051}
2052
2053static int
2054compiler_assert(struct compiler *c, stmt_ty s)
2055{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002056 static PyObject *assertion_error = NULL;
2057 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002059 if (Py_OptimizeFlag)
2060 return 1;
2061 if (assertion_error == NULL) {
2062 assertion_error = PyString_InternFromString("AssertionError");
2063 if (assertion_error == NULL)
2064 return 0;
2065 }
2066 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 }
2074 VISIT(c, expr, s->v.Assert.test);
2075 end = compiler_new_block(c);
2076 if (end == NULL)
2077 return 0;
2078 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2079 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2080 if (s->v.Assert.msg) {
2081 VISIT(c, expr, s->v.Assert.msg);
Benjamin Peterson0c0d7562011-10-27 08:21:59 -04002082 ADDOP_I(c, CALL_FUNCTION, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002083 }
Benjamin Peterson0c0d7562011-10-27 08:21:59 -04002084 ADDOP_I(c, RAISE_VARARGS, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002085 compiler_use_next_block(c, end);
2086 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087}
2088
2089static int
2090compiler_visit_stmt(struct compiler *c, stmt_ty s)
2091{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002092 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002093
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002094 /* Always assign a lineno to the next instruction for a stmt. */
2095 c->u->u_lineno = s->lineno;
2096 c->u->u_lineno_set = false;
Jeremy Hylton12603c42006-04-01 16:18:02 +00002097
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002098 switch (s->kind) {
2099 case FunctionDef_kind:
2100 return compiler_function(c, s);
2101 case ClassDef_kind:
2102 return compiler_class(c, s);
2103 case Return_kind:
2104 if (c->u->u_ste->ste_type != FunctionBlock)
2105 return compiler_error(c, "'return' outside function");
2106 if (s->v.Return.value) {
2107 VISIT(c, expr, s->v.Return.value);
2108 }
2109 else
2110 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2111 ADDOP(c, RETURN_VALUE);
2112 break;
2113 case Delete_kind:
2114 VISIT_SEQ(c, expr, s->v.Delete.targets)
2115 break;
2116 case Assign_kind:
2117 n = asdl_seq_LEN(s->v.Assign.targets);
2118 VISIT(c, expr, s->v.Assign.value);
2119 for (i = 0; i < n; i++) {
2120 if (i < n - 1)
2121 ADDOP(c, DUP_TOP);
2122 VISIT(c, expr,
2123 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2124 }
2125 break;
2126 case AugAssign_kind:
2127 return compiler_augassign(c, s);
2128 case Print_kind:
2129 return compiler_print(c, s);
2130 case For_kind:
2131 return compiler_for(c, s);
2132 case While_kind:
2133 return compiler_while(c, s);
2134 case If_kind:
2135 return compiler_if(c, s);
2136 case Raise_kind:
2137 n = 0;
2138 if (s->v.Raise.type) {
2139 VISIT(c, expr, s->v.Raise.type);
2140 n++;
2141 if (s->v.Raise.inst) {
2142 VISIT(c, expr, s->v.Raise.inst);
2143 n++;
2144 if (s->v.Raise.tback) {
2145 VISIT(c, expr, s->v.Raise.tback);
2146 n++;
2147 }
2148 }
2149 }
2150 ADDOP_I(c, RAISE_VARARGS, n);
2151 break;
2152 case TryExcept_kind:
2153 return compiler_try_except(c, s);
2154 case TryFinally_kind:
2155 return compiler_try_finally(c, s);
2156 case Assert_kind:
2157 return compiler_assert(c, s);
2158 case Import_kind:
2159 return compiler_import(c, s);
2160 case ImportFrom_kind:
2161 return compiler_from_import(c, s);
2162 case Exec_kind:
2163 VISIT(c, expr, s->v.Exec.body);
2164 if (s->v.Exec.globals) {
2165 VISIT(c, expr, s->v.Exec.globals);
2166 if (s->v.Exec.locals) {
2167 VISIT(c, expr, s->v.Exec.locals);
2168 } else {
2169 ADDOP(c, DUP_TOP);
2170 }
2171 } else {
2172 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2173 ADDOP(c, DUP_TOP);
2174 }
2175 ADDOP(c, EXEC_STMT);
2176 break;
2177 case Global_kind:
2178 break;
2179 case Expr_kind:
2180 if (c->c_interactive && c->c_nestlevel <= 1) {
2181 VISIT(c, expr, s->v.Expr.value);
2182 ADDOP(c, PRINT_EXPR);
2183 }
2184 else if (s->v.Expr.value->kind != Str_kind &&
2185 s->v.Expr.value->kind != Num_kind) {
2186 VISIT(c, expr, s->v.Expr.value);
2187 ADDOP(c, POP_TOP);
2188 }
2189 break;
2190 case Pass_kind:
2191 break;
2192 case Break_kind:
2193 if (!compiler_in_loop(c))
2194 return compiler_error(c, "'break' outside loop");
2195 ADDOP(c, BREAK_LOOP);
2196 break;
2197 case Continue_kind:
2198 return compiler_continue(c);
2199 case With_kind:
2200 return compiler_with(c, s);
2201 }
2202 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203}
2204
2205static int
2206unaryop(unaryop_ty op)
2207{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002208 switch (op) {
2209 case Invert:
2210 return UNARY_INVERT;
2211 case Not:
2212 return UNARY_NOT;
2213 case UAdd:
2214 return UNARY_POSITIVE;
2215 case USub:
2216 return UNARY_NEGATIVE;
2217 default:
2218 PyErr_Format(PyExc_SystemError,
2219 "unary op %d should not be possible", op);
2220 return 0;
2221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002222}
2223
2224static int
2225binop(struct compiler *c, operator_ty op)
2226{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002227 switch (op) {
2228 case Add:
2229 return BINARY_ADD;
2230 case Sub:
2231 return BINARY_SUBTRACT;
2232 case Mult:
2233 return BINARY_MULTIPLY;
2234 case Div:
2235 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2236 return BINARY_TRUE_DIVIDE;
2237 else
2238 return BINARY_DIVIDE;
2239 case Mod:
2240 return BINARY_MODULO;
2241 case Pow:
2242 return BINARY_POWER;
2243 case LShift:
2244 return BINARY_LSHIFT;
2245 case RShift:
2246 return BINARY_RSHIFT;
2247 case BitOr:
2248 return BINARY_OR;
2249 case BitXor:
2250 return BINARY_XOR;
2251 case BitAnd:
2252 return BINARY_AND;
2253 case FloorDiv:
2254 return BINARY_FLOOR_DIVIDE;
2255 default:
2256 PyErr_Format(PyExc_SystemError,
2257 "binary op %d should not be possible", op);
2258 return 0;
2259 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260}
2261
2262static int
2263cmpop(cmpop_ty op)
2264{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002265 switch (op) {
2266 case Eq:
2267 return PyCmp_EQ;
2268 case NotEq:
2269 return PyCmp_NE;
2270 case Lt:
2271 return PyCmp_LT;
2272 case LtE:
2273 return PyCmp_LE;
2274 case Gt:
2275 return PyCmp_GT;
2276 case GtE:
2277 return PyCmp_GE;
2278 case Is:
2279 return PyCmp_IS;
2280 case IsNot:
2281 return PyCmp_IS_NOT;
2282 case In:
2283 return PyCmp_IN;
2284 case NotIn:
2285 return PyCmp_NOT_IN;
2286 default:
2287 return PyCmp_BAD;
2288 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289}
2290
2291static int
2292inplace_binop(struct compiler *c, operator_ty op)
2293{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002294 switch (op) {
2295 case Add:
2296 return INPLACE_ADD;
2297 case Sub:
2298 return INPLACE_SUBTRACT;
2299 case Mult:
2300 return INPLACE_MULTIPLY;
2301 case Div:
2302 if (c->c_flags && c->c_flags->cf_flags & CO_FUTURE_DIVISION)
2303 return INPLACE_TRUE_DIVIDE;
2304 else
2305 return INPLACE_DIVIDE;
2306 case Mod:
2307 return INPLACE_MODULO;
2308 case Pow:
2309 return INPLACE_POWER;
2310 case LShift:
2311 return INPLACE_LSHIFT;
2312 case RShift:
2313 return INPLACE_RSHIFT;
2314 case BitOr:
2315 return INPLACE_OR;
2316 case BitXor:
2317 return INPLACE_XOR;
2318 case BitAnd:
2319 return INPLACE_AND;
2320 case FloorDiv:
2321 return INPLACE_FLOOR_DIVIDE;
2322 default:
2323 PyErr_Format(PyExc_SystemError,
2324 "inplace binary op %d should not be possible", op);
2325 return 0;
2326 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327}
2328
2329static int
2330compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2331{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002332 int op, scope, arg;
2333 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002334
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002335 PyObject *dict = c->u->u_names;
2336 PyObject *mangled;
2337 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002338
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002339 mangled = _Py_Mangle(c->u->u_private, name);
2340 if (!mangled)
2341 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002342
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002343 op = 0;
2344 optype = OP_NAME;
2345 scope = PyST_GetScope(c->u->u_ste, mangled);
2346 switch (scope) {
2347 case FREE:
2348 dict = c->u->u_freevars;
2349 optype = OP_DEREF;
2350 break;
2351 case CELL:
2352 dict = c->u->u_cellvars;
2353 optype = OP_DEREF;
2354 break;
2355 case LOCAL:
2356 if (c->u->u_ste->ste_type == FunctionBlock)
2357 optype = OP_FAST;
2358 break;
2359 case GLOBAL_IMPLICIT:
2360 if (c->u->u_ste->ste_type == FunctionBlock &&
2361 !c->u->u_ste->ste_unoptimized)
2362 optype = OP_GLOBAL;
2363 break;
2364 case GLOBAL_EXPLICIT:
2365 optype = OP_GLOBAL;
2366 break;
2367 default:
2368 /* scope can be 0 */
2369 break;
2370 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002372 /* XXX Leave assert here, but handle __doc__ and the like better */
2373 assert(scope || PyString_AS_STRING(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002375 switch (optype) {
2376 case OP_DEREF:
2377 switch (ctx) {
2378 case Load: op = LOAD_DEREF; break;
2379 case Store: op = STORE_DEREF; break;
2380 case AugLoad:
2381 case AugStore:
2382 break;
2383 case Del:
2384 PyErr_Format(PyExc_SyntaxError,
2385 "can not delete variable '%s' referenced "
2386 "in nested scope",
2387 PyString_AS_STRING(name));
2388 Py_DECREF(mangled);
2389 return 0;
2390 case Param:
2391 default:
2392 PyErr_SetString(PyExc_SystemError,
2393 "param invalid for deref variable");
2394 return 0;
2395 }
2396 break;
2397 case OP_FAST:
2398 switch (ctx) {
2399 case Load: op = LOAD_FAST; break;
2400 case Store: op = STORE_FAST; break;
2401 case Del: op = DELETE_FAST; break;
2402 case AugLoad:
2403 case AugStore:
2404 break;
2405 case Param:
2406 default:
2407 PyErr_SetString(PyExc_SystemError,
2408 "param invalid for local variable");
2409 return 0;
2410 }
2411 ADDOP_O(c, op, mangled, varnames);
2412 Py_DECREF(mangled);
2413 return 1;
2414 case OP_GLOBAL:
2415 switch (ctx) {
2416 case Load: op = LOAD_GLOBAL; break;
2417 case Store: op = STORE_GLOBAL; break;
2418 case Del: op = DELETE_GLOBAL; break;
2419 case AugLoad:
2420 case AugStore:
2421 break;
2422 case Param:
2423 default:
2424 PyErr_SetString(PyExc_SystemError,
2425 "param invalid for global variable");
2426 return 0;
2427 }
2428 break;
2429 case OP_NAME:
2430 switch (ctx) {
2431 case Load: op = LOAD_NAME; break;
2432 case Store: op = STORE_NAME; break;
2433 case Del: op = DELETE_NAME; break;
2434 case AugLoad:
2435 case AugStore:
2436 break;
2437 case Param:
2438 default:
2439 PyErr_SetString(PyExc_SystemError,
2440 "param invalid for name variable");
2441 return 0;
2442 }
2443 break;
2444 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002446 assert(op);
2447 arg = compiler_add_o(c, dict, mangled);
2448 Py_DECREF(mangled);
2449 if (arg < 0)
2450 return 0;
2451 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002452}
2453
2454static int
2455compiler_boolop(struct compiler *c, expr_ty e)
2456{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002457 basicblock *end;
2458 int jumpi, i, n;
2459 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002460
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002461 assert(e->kind == BoolOp_kind);
2462 if (e->v.BoolOp.op == And)
2463 jumpi = JUMP_IF_FALSE_OR_POP;
2464 else
2465 jumpi = JUMP_IF_TRUE_OR_POP;
2466 end = compiler_new_block(c);
2467 if (end == NULL)
2468 return 0;
2469 s = e->v.BoolOp.values;
2470 n = asdl_seq_LEN(s) - 1;
2471 assert(n >= 0);
2472 for (i = 0; i < n; ++i) {
2473 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2474 ADDOP_JABS(c, jumpi, end);
2475 }
2476 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2477 compiler_use_next_block(c, end);
2478 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479}
2480
2481static int
2482compiler_list(struct compiler *c, expr_ty e)
2483{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002484 int n = asdl_seq_LEN(e->v.List.elts);
2485 if (e->v.List.ctx == Store) {
2486 ADDOP_I(c, UNPACK_SEQUENCE, n);
2487 }
2488 VISIT_SEQ(c, expr, e->v.List.elts);
2489 if (e->v.List.ctx == Load) {
2490 ADDOP_I(c, BUILD_LIST, n);
2491 }
2492 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493}
2494
2495static int
2496compiler_tuple(struct compiler *c, expr_ty e)
2497{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002498 int n = asdl_seq_LEN(e->v.Tuple.elts);
2499 if (e->v.Tuple.ctx == Store) {
2500 ADDOP_I(c, UNPACK_SEQUENCE, n);
2501 }
2502 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2503 if (e->v.Tuple.ctx == Load) {
2504 ADDOP_I(c, BUILD_TUPLE, n);
2505 }
2506 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507}
2508
2509static int
2510compiler_compare(struct compiler *c, expr_ty e)
2511{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002512 int i, n;
2513 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002515 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2516 VISIT(c, expr, e->v.Compare.left);
2517 n = asdl_seq_LEN(e->v.Compare.ops);
2518 assert(n > 0);
2519 if (n > 1) {
2520 cleanup = compiler_new_block(c);
2521 if (cleanup == NULL)
2522 return 0;
2523 VISIT(c, expr,
2524 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2525 }
2526 for (i = 1; i < n; i++) {
2527 ADDOP(c, DUP_TOP);
2528 ADDOP(c, ROT_THREE);
2529 ADDOP_I(c, COMPARE_OP,
2530 cmpop((cmpop_ty)(asdl_seq_GET(
2531 e->v.Compare.ops, i - 1))));
2532 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2533 NEXT_BLOCK(c);
2534 if (i < (n - 1))
2535 VISIT(c, expr,
2536 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2537 }
2538 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2539 ADDOP_I(c, COMPARE_OP,
2540 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2541 if (n > 1) {
2542 basicblock *end = compiler_new_block(c);
2543 if (end == NULL)
2544 return 0;
2545 ADDOP_JREL(c, JUMP_FORWARD, end);
2546 compiler_use_next_block(c, cleanup);
2547 ADDOP(c, ROT_TWO);
2548 ADDOP(c, POP_TOP);
2549 compiler_use_next_block(c, end);
2550 }
2551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552}
2553
2554static int
2555compiler_call(struct compiler *c, expr_ty e)
2556{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002557 int n, code = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002559 VISIT(c, expr, e->v.Call.func);
2560 n = asdl_seq_LEN(e->v.Call.args);
2561 VISIT_SEQ(c, expr, e->v.Call.args);
2562 if (e->v.Call.keywords) {
2563 VISIT_SEQ(c, keyword, e->v.Call.keywords);
2564 n |= asdl_seq_LEN(e->v.Call.keywords) << 8;
2565 }
2566 if (e->v.Call.starargs) {
2567 VISIT(c, expr, e->v.Call.starargs);
2568 code |= 1;
2569 }
2570 if (e->v.Call.kwargs) {
2571 VISIT(c, expr, e->v.Call.kwargs);
2572 code |= 2;
2573 }
2574 switch (code) {
2575 case 0:
2576 ADDOP_I(c, CALL_FUNCTION, n);
2577 break;
2578 case 1:
2579 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2580 break;
2581 case 2:
2582 ADDOP_I(c, CALL_FUNCTION_KW, n);
2583 break;
2584 case 3:
2585 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2586 break;
2587 }
2588 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589}
2590
2591static int
Antoine Pitroud0c35152008-12-17 00:38:28 +00002592compiler_listcomp_generator(struct compiler *c, asdl_seq *generators,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002593 int gen_index, expr_ty elt)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002594{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002595 /* generate code for the iterator, then each of the ifs,
2596 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002597
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002598 comprehension_ty l;
2599 basicblock *start, *anchor, *skip, *if_cleanup;
2600 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002601
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002602 start = compiler_new_block(c);
2603 skip = compiler_new_block(c);
2604 if_cleanup = compiler_new_block(c);
2605 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002607 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2608 anchor == NULL)
2609 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002610
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002611 l = (comprehension_ty)asdl_seq_GET(generators, gen_index);
2612 VISIT(c, expr, l->iter);
2613 ADDOP(c, GET_ITER);
2614 compiler_use_next_block(c, start);
2615 ADDOP_JREL(c, FOR_ITER, anchor);
2616 NEXT_BLOCK(c);
2617 VISIT(c, expr, l->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002618
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002619 /* XXX this needs to be cleaned up...a lot! */
2620 n = asdl_seq_LEN(l->ifs);
2621 for (i = 0; i < n; i++) {
2622 expr_ty e = (expr_ty)asdl_seq_GET(l->ifs, i);
2623 VISIT(c, expr, e);
2624 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2625 NEXT_BLOCK(c);
2626 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002627
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002628 if (++gen_index < asdl_seq_LEN(generators))
2629 if (!compiler_listcomp_generator(c, generators, gen_index, elt))
2630 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002632 /* only append after the last for generator */
2633 if (gen_index >= asdl_seq_LEN(generators)) {
2634 VISIT(c, expr, elt);
2635 ADDOP_I(c, LIST_APPEND, gen_index+1);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002636
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002637 compiler_use_next_block(c, skip);
2638 }
2639 compiler_use_next_block(c, if_cleanup);
2640 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2641 compiler_use_next_block(c, anchor);
2642
2643 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644}
2645
2646static int
2647compiler_listcomp(struct compiler *c, expr_ty e)
2648{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002649 assert(e->kind == ListComp_kind);
2650 ADDOP_I(c, BUILD_LIST, 0);
2651 return compiler_listcomp_generator(c, e->v.ListComp.generators, 0,
2652 e->v.ListComp.elt);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653}
2654
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002655/* Dict and set comprehensions and generator expressions work by creating a
2656 nested function to perform the actual iteration. This means that the
2657 iteration variables don't leak into the current scope.
2658 The defined function is called immediately following its definition, with the
2659 result of that call being the result of the expression.
2660 The LC/SC version returns the populated container, while the GE version is
2661 flagged in symtable.c as a generator, so it returns the generator object
2662 when the function is called.
2663 This code *knows* that the loop cannot contain break, continue, or return,
2664 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2665
2666 Possible cleanups:
2667 - iterate over the generator sequence instead of using recursion
2668*/
2669
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002671compiler_comprehension_generator(struct compiler *c,
2672 asdl_seq *generators, int gen_index,
2673 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002674{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002675 /* generate code for the iterator, then each of the ifs,
2676 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002678 comprehension_ty gen;
2679 basicblock *start, *anchor, *skip, *if_cleanup;
2680 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002681
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002682 start = compiler_new_block(c);
2683 skip = compiler_new_block(c);
2684 if_cleanup = compiler_new_block(c);
2685 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002687 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2688 anchor == NULL)
2689 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002690
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002691 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002693 if (gen_index == 0) {
2694 /* Receive outermost iter as an implicit argument */
2695 c->u->u_argcount = 1;
2696 ADDOP_I(c, LOAD_FAST, 0);
2697 }
2698 else {
2699 /* Sub-iter - calculate on the fly */
2700 VISIT(c, expr, gen->iter);
2701 ADDOP(c, GET_ITER);
2702 }
2703 compiler_use_next_block(c, start);
2704 ADDOP_JREL(c, FOR_ITER, anchor);
2705 NEXT_BLOCK(c);
2706 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002708 /* XXX this needs to be cleaned up...a lot! */
2709 n = asdl_seq_LEN(gen->ifs);
2710 for (i = 0; i < n; i++) {
2711 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2712 VISIT(c, expr, e);
2713 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2714 NEXT_BLOCK(c);
2715 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002717 if (++gen_index < asdl_seq_LEN(generators))
2718 if (!compiler_comprehension_generator(c,
2719 generators, gen_index,
2720 elt, val, type))
2721 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002723 /* only append after the last for generator */
2724 if (gen_index >= asdl_seq_LEN(generators)) {
2725 /* comprehension specific code */
2726 switch (type) {
2727 case COMP_GENEXP:
2728 VISIT(c, expr, elt);
2729 ADDOP(c, YIELD_VALUE);
2730 ADDOP(c, POP_TOP);
2731 break;
2732 case COMP_SETCOMP:
2733 VISIT(c, expr, elt);
2734 ADDOP_I(c, SET_ADD, gen_index + 1);
2735 break;
2736 case COMP_DICTCOMP:
2737 /* With 'd[k] = v', v is evaluated before k, so we do
2738 the same. */
2739 VISIT(c, expr, val);
2740 VISIT(c, expr, elt);
2741 ADDOP_I(c, MAP_ADD, gen_index + 1);
2742 break;
2743 default:
2744 return 0;
2745 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002747 compiler_use_next_block(c, skip);
2748 }
2749 compiler_use_next_block(c, if_cleanup);
2750 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2751 compiler_use_next_block(c, anchor);
2752
2753 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002754}
2755
2756static int
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002757compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002758 asdl_seq *generators, expr_ty elt, expr_ty val)
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002759{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002760 PyCodeObject *co = NULL;
2761 expr_ty outermost_iter;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002762
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002763 outermost_iter = ((comprehension_ty)
2764 asdl_seq_GET(generators, 0))->iter;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002765
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002766 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2767 goto error;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002768
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002769 if (type != COMP_GENEXP) {
2770 int op;
2771 switch (type) {
2772 case COMP_SETCOMP:
2773 op = BUILD_SET;
2774 break;
2775 case COMP_DICTCOMP:
2776 op = BUILD_MAP;
2777 break;
2778 default:
2779 PyErr_Format(PyExc_SystemError,
2780 "unknown comprehension type %d", type);
2781 goto error_in_scope;
2782 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002783
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002784 ADDOP_I(c, op, 0);
2785 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002787 if (!compiler_comprehension_generator(c, generators, 0, elt,
2788 val, type))
2789 goto error_in_scope;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002790
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002791 if (type != COMP_GENEXP) {
2792 ADDOP(c, RETURN_VALUE);
2793 }
2794
2795 co = assemble(c, 1);
2796 compiler_exit_scope(c);
2797 if (co == NULL)
2798 goto error;
2799
2800 if (!compiler_make_closure(c, co, 0))
2801 goto error;
2802 Py_DECREF(co);
2803
2804 VISIT(c, expr, outermost_iter);
2805 ADDOP(c, GET_ITER);
2806 ADDOP_I(c, CALL_FUNCTION, 1);
2807 return 1;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002808error_in_scope:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002809 compiler_exit_scope(c);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002810error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002811 Py_XDECREF(co);
2812 return 0;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002813}
2814
2815static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816compiler_genexp(struct compiler *c, expr_ty e)
2817{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002818 static identifier name;
2819 if (!name) {
2820 name = PyString_FromString("<genexpr>");
2821 if (!name)
2822 return 0;
2823 }
2824 assert(e->kind == GeneratorExp_kind);
2825 return compiler_comprehension(c, e, COMP_GENEXP, name,
2826 e->v.GeneratorExp.generators,
2827 e->v.GeneratorExp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002828}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002830static int
2831compiler_setcomp(struct compiler *c, expr_ty e)
2832{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002833 static identifier name;
2834 if (!name) {
2835 name = PyString_FromString("<setcomp>");
2836 if (!name)
2837 return 0;
2838 }
2839 assert(e->kind == SetComp_kind);
2840 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2841 e->v.SetComp.generators,
2842 e->v.SetComp.elt, NULL);
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002843}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002845static int
2846compiler_dictcomp(struct compiler *c, expr_ty e)
2847{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002848 static identifier name;
2849 if (!name) {
2850 name = PyString_FromString("<dictcomp>");
2851 if (!name)
2852 return 0;
2853 }
2854 assert(e->kind == DictComp_kind);
2855 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
2856 e->v.DictComp.generators,
2857 e->v.DictComp.key, e->v.DictComp.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002858}
2859
2860static int
2861compiler_visit_keyword(struct compiler *c, keyword_ty k)
2862{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002863 ADDOP_O(c, LOAD_CONST, k->arg, consts);
2864 VISIT(c, expr, k->value);
2865 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002866}
2867
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002868/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002869 whether they are true or false.
2870
2871 Return values: 1 for true, 0 for false, -1 for non-constant.
2872 */
2873
2874static int
2875expr_constant(expr_ty e)
2876{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002877 switch (e->kind) {
2878 case Num_kind:
2879 return PyObject_IsTrue(e->v.Num.n);
2880 case Str_kind:
2881 return PyObject_IsTrue(e->v.Str.s);
2882 case Name_kind:
2883 /* __debug__ is not assignable, so we can optimize
2884 * it away in if and while statements */
2885 if (strcmp(PyString_AS_STRING(e->v.Name.id),
2886 "__debug__") == 0)
2887 return ! Py_OptimizeFlag;
2888 /* fall through */
2889 default:
2890 return -1;
2891 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
Guido van Rossumc2e20742006-02-27 22:32:47 +00002894/*
2895 Implements the with statement from PEP 343.
2896
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002897 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00002898
2899 with EXPR as VAR:
2900 BLOCK
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002901
Guido van Rossumc2e20742006-02-27 22:32:47 +00002902 It is implemented roughly as:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002903
Guido van Rossumda5b7012006-05-02 19:47:52 +00002904 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00002905 exit = context.__exit__ # not calling it
2906 value = context.__enter__()
2907 try:
2908 VAR = value # if VAR present in the syntax
2909 BLOCK
2910 finally:
2911 if an exception was raised:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002912 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002913 else:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002914 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002915 exit(*exc)
2916 */
2917static int
2918compiler_with(struct compiler *c, stmt_ty s)
2919{
Guido van Rossumc2e20742006-02-27 22:32:47 +00002920 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002921
2922 assert(s->kind == With_kind);
2923
Guido van Rossumc2e20742006-02-27 22:32:47 +00002924 block = compiler_new_block(c);
2925 finally = compiler_new_block(c);
2926 if (!block || !finally)
Antoine Pitroua4d36a92010-06-22 21:42:05 +00002927 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002928
Guido van Rossumda5b7012006-05-02 19:47:52 +00002929 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002930 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002931 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002932
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002933 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002934 compiler_use_next_block(c, block);
Benjamin Peterson565d7852010-02-05 02:12:14 +00002935 /* Note that the block is actually called SETUP_WITH in ceval.c, but
2936 functions the same as SETUP_FINALLY except that exceptions are
2937 normalized. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002938 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitroua4d36a92010-06-22 21:42:05 +00002939 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002940 }
2941
2942 if (s->v.With.optional_vars) {
Antoine Pitroua4d36a92010-06-22 21:42:05 +00002943 VISIT(c, expr, s->v.With.optional_vars);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002944 }
2945 else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002946 /* Discard result from context.__enter__() */
Antoine Pitroua4d36a92010-06-22 21:42:05 +00002947 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002948 }
2949
2950 /* BLOCK code */
Anthony Baxter2c33fc72006-04-12 00:43:09 +00002951 VISIT_SEQ(c, stmt, s->v.With.body);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002952
2953 /* End of try block; start the finally block */
2954 ADDOP(c, POP_BLOCK);
2955 compiler_pop_fblock(c, FINALLY_TRY, block);
2956
2957 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2958 compiler_use_next_block(c, finally);
2959 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitroua4d36a92010-06-22 21:42:05 +00002960 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002961
Nick Coghlan7af53be2008-03-07 14:13:28 +00002962 /* Finally block starts; context.__exit__ is on the stack under
2963 the exception or return information. Just issue our magic
2964 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00002965 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002966
2967 /* Finally block ends. */
2968 ADDOP(c, END_FINALLY);
2969 compiler_pop_fblock(c, FINALLY_END, finally);
2970 return 1;
2971}
2972
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973static int
2974compiler_visit_expr(struct compiler *c, expr_ty e)
2975{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002976 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002978 /* If expr e has a different line number than the last expr/stmt,
2979 set a new line number for the next instruction.
2980 */
2981 if (e->lineno > c->u->u_lineno) {
2982 c->u->u_lineno = e->lineno;
2983 c->u->u_lineno_set = false;
2984 }
2985 switch (e->kind) {
2986 case BoolOp_kind:
2987 return compiler_boolop(c, e);
2988 case BinOp_kind:
2989 VISIT(c, expr, e->v.BinOp.left);
2990 VISIT(c, expr, e->v.BinOp.right);
2991 ADDOP(c, binop(c, e->v.BinOp.op));
2992 break;
2993 case UnaryOp_kind:
2994 VISIT(c, expr, e->v.UnaryOp.operand);
2995 ADDOP(c, unaryop(e->v.UnaryOp.op));
2996 break;
2997 case Lambda_kind:
2998 return compiler_lambda(c, e);
2999 case IfExp_kind:
3000 return compiler_ifexp(c, e);
3001 case Dict_kind:
3002 n = asdl_seq_LEN(e->v.Dict.values);
3003 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3004 for (i = 0; i < n; i++) {
3005 VISIT(c, expr,
3006 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3007 VISIT(c, expr,
3008 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3009 ADDOP(c, STORE_MAP);
3010 }
3011 break;
3012 case Set_kind:
3013 n = asdl_seq_LEN(e->v.Set.elts);
3014 VISIT_SEQ(c, expr, e->v.Set.elts);
3015 ADDOP_I(c, BUILD_SET, n);
3016 break;
3017 case ListComp_kind:
3018 return compiler_listcomp(c, e);
3019 case SetComp_kind:
3020 return compiler_setcomp(c, e);
3021 case DictComp_kind:
3022 return compiler_dictcomp(c, e);
3023 case GeneratorExp_kind:
3024 return compiler_genexp(c, e);
3025 case Yield_kind:
3026 if (c->u->u_ste->ste_type != FunctionBlock)
3027 return compiler_error(c, "'yield' outside function");
3028 if (e->v.Yield.value) {
3029 VISIT(c, expr, e->v.Yield.value);
3030 }
3031 else {
3032 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3033 }
3034 ADDOP(c, YIELD_VALUE);
3035 break;
3036 case Compare_kind:
3037 return compiler_compare(c, e);
3038 case Call_kind:
3039 return compiler_call(c, e);
3040 case Repr_kind:
3041 VISIT(c, expr, e->v.Repr.value);
3042 ADDOP(c, UNARY_CONVERT);
3043 break;
3044 case Num_kind:
3045 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3046 break;
3047 case Str_kind:
3048 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3049 break;
3050 /* The following exprs can be assignment targets. */
3051 case Attribute_kind:
3052 if (e->v.Attribute.ctx != AugStore)
3053 VISIT(c, expr, e->v.Attribute.value);
3054 switch (e->v.Attribute.ctx) {
3055 case AugLoad:
3056 ADDOP(c, DUP_TOP);
3057 /* Fall through to load */
3058 case Load:
3059 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3060 break;
3061 case AugStore:
3062 ADDOP(c, ROT_TWO);
3063 /* Fall through to save */
3064 case Store:
3065 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3066 break;
3067 case Del:
3068 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3069 break;
3070 case Param:
3071 default:
3072 PyErr_SetString(PyExc_SystemError,
3073 "param invalid in attribute expression");
3074 return 0;
3075 }
3076 break;
3077 case Subscript_kind:
3078 switch (e->v.Subscript.ctx) {
3079 case AugLoad:
3080 VISIT(c, expr, e->v.Subscript.value);
3081 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3082 break;
3083 case Load:
3084 VISIT(c, expr, e->v.Subscript.value);
3085 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3086 break;
3087 case AugStore:
3088 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3089 break;
3090 case Store:
3091 VISIT(c, expr, e->v.Subscript.value);
3092 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3093 break;
3094 case Del:
3095 VISIT(c, expr, e->v.Subscript.value);
3096 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3097 break;
3098 case Param:
3099 default:
3100 PyErr_SetString(PyExc_SystemError,
3101 "param invalid in subscript expression");
3102 return 0;
3103 }
3104 break;
3105 case Name_kind:
3106 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3107 /* child nodes of List and Tuple will have expr_context set */
3108 case List_kind:
3109 return compiler_list(c, e);
3110 case Tuple_kind:
3111 return compiler_tuple(c, e);
3112 }
3113 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003114}
3115
3116static int
3117compiler_augassign(struct compiler *c, stmt_ty s)
3118{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003119 expr_ty e = s->v.AugAssign.target;
3120 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003121
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003122 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003124 switch (e->kind) {
3125 case Attribute_kind:
3126 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3127 AugLoad, e->lineno, e->col_offset, c->c_arena);
3128 if (auge == NULL)
3129 return 0;
3130 VISIT(c, expr, auge);
3131 VISIT(c, expr, s->v.AugAssign.value);
3132 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3133 auge->v.Attribute.ctx = AugStore;
3134 VISIT(c, expr, auge);
3135 break;
3136 case Subscript_kind:
3137 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3138 AugLoad, e->lineno, e->col_offset, c->c_arena);
3139 if (auge == NULL)
3140 return 0;
3141 VISIT(c, expr, auge);
3142 VISIT(c, expr, s->v.AugAssign.value);
3143 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3144 auge->v.Subscript.ctx = AugStore;
3145 VISIT(c, expr, auge);
3146 break;
3147 case Name_kind:
3148 if (!compiler_nameop(c, e->v.Name.id, Load))
3149 return 0;
3150 VISIT(c, expr, s->v.AugAssign.value);
3151 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3152 return compiler_nameop(c, e->v.Name.id, Store);
3153 default:
3154 PyErr_Format(PyExc_SystemError,
3155 "invalid node type (%d) for augmented assignment",
3156 e->kind);
3157 return 0;
3158 }
3159 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160}
3161
3162static int
3163compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3164{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003165 struct fblockinfo *f;
3166 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3167 PyErr_SetString(PyExc_SystemError,
3168 "too many statically nested blocks");
3169 return 0;
3170 }
3171 f = &c->u->u_fblock[c->u->u_nfblocks++];
3172 f->fb_type = t;
3173 f->fb_block = b;
3174 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175}
3176
3177static void
3178compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3179{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003180 struct compiler_unit *u = c->u;
3181 assert(u->u_nfblocks > 0);
3182 u->u_nfblocks--;
3183 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3184 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185}
3186
Jeremy Hylton82271f12006-10-04 02:24:52 +00003187static int
3188compiler_in_loop(struct compiler *c) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003189 int i;
3190 struct compiler_unit *u = c->u;
3191 for (i = 0; i < u->u_nfblocks; ++i) {
3192 if (u->u_fblock[i].fb_type == LOOP)
3193 return 1;
3194 }
3195 return 0;
Jeremy Hylton82271f12006-10-04 02:24:52 +00003196}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003197/* Raises a SyntaxError and returns 0.
3198 If something goes wrong, a different exception may be raised.
3199*/
3200
3201static int
3202compiler_error(struct compiler *c, const char *errstr)
3203{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003204 PyObject *loc;
3205 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003207 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3208 if (!loc) {
3209 Py_INCREF(Py_None);
3210 loc = Py_None;
3211 }
3212 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3213 Py_None, loc);
3214 if (!u)
3215 goto exit;
3216 v = Py_BuildValue("(zO)", errstr, u);
3217 if (!v)
3218 goto exit;
3219 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 exit:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003221 Py_DECREF(loc);
3222 Py_XDECREF(u);
3223 Py_XDECREF(v);
3224 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003225}
3226
3227static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003228compiler_handle_subscr(struct compiler *c, const char *kind,
3229 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003231 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003233 /* XXX this code is duplicated */
3234 switch (ctx) {
3235 case AugLoad: /* fall through to Load */
3236 case Load: op = BINARY_SUBSCR; break;
3237 case AugStore:/* fall through to Store */
3238 case Store: op = STORE_SUBSCR; break;
3239 case Del: op = DELETE_SUBSCR; break;
3240 case Param:
3241 PyErr_Format(PyExc_SystemError,
3242 "invalid %s kind %d in subscript\n",
3243 kind, ctx);
3244 return 0;
3245 }
3246 if (ctx == AugLoad) {
3247 ADDOP_I(c, DUP_TOPX, 2);
3248 }
3249 else if (ctx == AugStore) {
3250 ADDOP(c, ROT_THREE);
3251 }
3252 ADDOP(c, op);
3253 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254}
3255
3256static int
3257compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3258{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003259 int n = 2;
3260 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003262 /* only handles the cases where BUILD_SLICE is emitted */
3263 if (s->v.Slice.lower) {
3264 VISIT(c, expr, s->v.Slice.lower);
3265 }
3266 else {
3267 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3268 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003269
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003270 if (s->v.Slice.upper) {
3271 VISIT(c, expr, s->v.Slice.upper);
3272 }
3273 else {
3274 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3275 }
3276
3277 if (s->v.Slice.step) {
3278 n++;
3279 VISIT(c, expr, s->v.Slice.step);
3280 }
3281 ADDOP_I(c, BUILD_SLICE, n);
3282 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003283}
3284
3285static int
3286compiler_simple_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3287{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003288 int op = 0, slice_offset = 0, stack_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003290 assert(s->v.Slice.step == NULL);
3291 if (s->v.Slice.lower) {
3292 slice_offset++;
3293 stack_count++;
3294 if (ctx != AugStore)
3295 VISIT(c, expr, s->v.Slice.lower);
3296 }
3297 if (s->v.Slice.upper) {
3298 slice_offset += 2;
3299 stack_count++;
3300 if (ctx != AugStore)
3301 VISIT(c, expr, s->v.Slice.upper);
3302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003304 if (ctx == AugLoad) {
3305 switch (stack_count) {
3306 case 0: ADDOP(c, DUP_TOP); break;
3307 case 1: ADDOP_I(c, DUP_TOPX, 2); break;
3308 case 2: ADDOP_I(c, DUP_TOPX, 3); break;
3309 }
3310 }
3311 else if (ctx == AugStore) {
3312 switch (stack_count) {
3313 case 0: ADDOP(c, ROT_TWO); break;
3314 case 1: ADDOP(c, ROT_THREE); break;
3315 case 2: ADDOP(c, ROT_FOUR); break;
3316 }
3317 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003319 switch (ctx) {
3320 case AugLoad: /* fall through to Load */
3321 case Load: op = SLICE; break;
3322 case AugStore:/* fall through to Store */
3323 case Store: op = STORE_SLICE; break;
3324 case Del: op = DELETE_SLICE; break;
3325 case Param:
3326 default:
3327 PyErr_SetString(PyExc_SystemError,
3328 "param invalid in simple slice");
3329 return 0;
3330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003332 ADDOP(c, op + slice_offset);
3333 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003334}
3335
3336static int
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003337compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3338 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003340 switch (s->kind) {
3341 case Ellipsis_kind:
3342 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3343 break;
3344 case Slice_kind:
3345 return compiler_slice(c, s, ctx);
3346 case Index_kind:
3347 VISIT(c, expr, s->v.Index.value);
3348 break;
3349 case ExtSlice_kind:
3350 default:
3351 PyErr_SetString(PyExc_SystemError,
3352 "extended slice invalid in nested slice");
3353 return 0;
3354 }
3355 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356}
3357
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003358static int
3359compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3360{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003361 char * kindname = NULL;
3362 switch (s->kind) {
3363 case Index_kind:
3364 kindname = "index";
3365 if (ctx != AugStore) {
3366 VISIT(c, expr, s->v.Index.value);
3367 }
3368 break;
3369 case Ellipsis_kind:
3370 kindname = "ellipsis";
3371 if (ctx != AugStore) {
3372 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3373 }
3374 break;
3375 case Slice_kind:
3376 kindname = "slice";
3377 if (!s->v.Slice.step)
3378 return compiler_simple_slice(c, s, ctx);
3379 if (ctx != AugStore) {
3380 if (!compiler_slice(c, s, ctx))
3381 return 0;
3382 }
3383 break;
3384 case ExtSlice_kind:
3385 kindname = "extended slice";
3386 if (ctx != AugStore) {
3387 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3388 for (i = 0; i < n; i++) {
3389 slice_ty sub = (slice_ty)asdl_seq_GET(
3390 s->v.ExtSlice.dims, i);
3391 if (!compiler_visit_nested_slice(c, sub, ctx))
3392 return 0;
3393 }
3394 ADDOP_I(c, BUILD_TUPLE, n);
3395 }
3396 break;
3397 default:
3398 PyErr_Format(PyExc_SystemError,
3399 "invalid subscript kind %d", s->kind);
3400 return 0;
3401 }
3402 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403}
3404
Neal Norwitzf733a012006-10-29 18:30:10 +00003405
3406/* End of the compiler section, beginning of the assembler section */
3407
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408/* do depth-first search of basic block graph, starting with block.
3409 post records the block indices in post-order.
3410
3411 XXX must handle implicit jumps from one block to next
3412*/
3413
Neal Norwitzf733a012006-10-29 18:30:10 +00003414struct assembler {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003415 PyObject *a_bytecode; /* string containing bytecode */
3416 int a_offset; /* offset into bytecode */
3417 int a_nblocks; /* number of reachable blocks */
3418 basicblock **a_postorder; /* list of blocks in dfs postorder */
3419 PyObject *a_lnotab; /* string containing lnotab */
3420 int a_lnotab_off; /* offset into lnotab */
3421 int a_lineno; /* last lineno of emitted instruction */
3422 int a_lineno_off; /* bytecode offset of last lineno */
Neal Norwitzf733a012006-10-29 18:30:10 +00003423};
3424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425static void
3426dfs(struct compiler *c, basicblock *b, struct assembler *a)
3427{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003428 int i;
3429 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003431 if (b->b_seen)
3432 return;
3433 b->b_seen = 1;
3434 if (b->b_next != NULL)
3435 dfs(c, b->b_next, a);
3436 for (i = 0; i < b->b_iused; i++) {
3437 instr = &b->b_instr[i];
3438 if (instr->i_jrel || instr->i_jabs)
3439 dfs(c, instr->i_target, a);
3440 }
3441 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003442}
3443
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003444static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003445stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3446{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003447 int i, target_depth;
3448 struct instr *instr;
3449 if (b->b_seen || b->b_startdepth >= depth)
3450 return maxdepth;
3451 b->b_seen = 1;
3452 b->b_startdepth = depth;
3453 for (i = 0; i < b->b_iused; i++) {
3454 instr = &b->b_instr[i];
3455 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3456 if (depth > maxdepth)
3457 maxdepth = depth;
3458 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3459 if (instr->i_jrel || instr->i_jabs) {
3460 target_depth = depth;
3461 if (instr->i_opcode == FOR_ITER) {
3462 target_depth = depth-2;
3463 } else if (instr->i_opcode == SETUP_FINALLY ||
3464 instr->i_opcode == SETUP_EXCEPT) {
3465 target_depth = depth+3;
3466 if (target_depth > maxdepth)
3467 maxdepth = target_depth;
3468 }
3469 maxdepth = stackdepth_walk(c, instr->i_target,
3470 target_depth, maxdepth);
3471 if (instr->i_opcode == JUMP_ABSOLUTE ||
3472 instr->i_opcode == JUMP_FORWARD) {
3473 goto out; /* remaining code is dead */
3474 }
3475 }
3476 }
3477 if (b->b_next)
3478 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479out:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003480 b->b_seen = 0;
3481 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482}
3483
3484/* Find the flow path that needs the largest stack. We assume that
3485 * cycles in the flow graph have no net effect on the stack depth.
3486 */
3487static int
3488stackdepth(struct compiler *c)
3489{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003490 basicblock *b, *entryblock;
3491 entryblock = NULL;
3492 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3493 b->b_seen = 0;
3494 b->b_startdepth = INT_MIN;
3495 entryblock = b;
3496 }
3497 if (!entryblock)
3498 return 0;
3499 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500}
3501
3502static int
3503assemble_init(struct assembler *a, int nblocks, int firstlineno)
3504{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003505 memset(a, 0, sizeof(struct assembler));
3506 a->a_lineno = firstlineno;
3507 a->a_bytecode = PyString_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3508 if (!a->a_bytecode)
3509 return 0;
3510 a->a_lnotab = PyString_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3511 if (!a->a_lnotab)
3512 return 0;
3513 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3514 PyErr_NoMemory();
3515 return 0;
3516 }
3517 a->a_postorder = (basicblock **)PyObject_Malloc(
3518 sizeof(basicblock *) * nblocks);
3519 if (!a->a_postorder) {
3520 PyErr_NoMemory();
3521 return 0;
3522 }
3523 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003524}
3525
3526static void
3527assemble_free(struct assembler *a)
3528{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003529 Py_XDECREF(a->a_bytecode);
3530 Py_XDECREF(a->a_lnotab);
3531 if (a->a_postorder)
3532 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533}
3534
3535/* Return the size of a basic block in bytes. */
3536
3537static int
3538instrsize(struct instr *instr)
3539{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003540 if (!instr->i_hasarg)
3541 return 1; /* 1 byte for the opcode*/
3542 if (instr->i_oparg > 0xffff)
3543 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3544 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545}
3546
3547static int
3548blocksize(basicblock *b)
3549{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003550 int i;
3551 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003553 for (i = 0; i < b->b_iused; i++)
3554 size += instrsize(&b->b_instr[i]);
3555 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003556}
3557
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003558/* Appends a pair to the end of the line number table, a_lnotab, representing
3559 the instruction's bytecode offset and line number. See
3560 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003561
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003562static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003563assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003564{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003565 int d_bytecode, d_lineno;
3566 int len;
3567 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003568
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003569 d_bytecode = a->a_offset - a->a_lineno_off;
3570 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003571
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003572 assert(d_bytecode >= 0);
3573 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003575 if(d_bytecode == 0 && d_lineno == 0)
3576 return 1;
Amaury Forgeot d'Arc99af7db2008-02-05 00:26:21 +00003577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003578 if (d_bytecode > 255) {
3579 int j, nbytes, ncodes = d_bytecode / 255;
3580 nbytes = a->a_lnotab_off + 2 * ncodes;
3581 len = PyString_GET_SIZE(a->a_lnotab);
3582 if (nbytes >= len) {
3583 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3584 len = nbytes;
3585 else if (len <= INT_MAX / 2)
3586 len *= 2;
3587 else {
3588 PyErr_NoMemory();
3589 return 0;
3590 }
3591 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3592 return 0;
3593 }
3594 lnotab = (unsigned char *)
3595 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3596 for (j = 0; j < ncodes; j++) {
3597 *lnotab++ = 255;
3598 *lnotab++ = 0;
3599 }
3600 d_bytecode -= ncodes * 255;
3601 a->a_lnotab_off += ncodes * 2;
3602 }
3603 assert(d_bytecode <= 255);
3604 if (d_lineno > 255) {
3605 int j, nbytes, ncodes = d_lineno / 255;
3606 nbytes = a->a_lnotab_off + 2 * ncodes;
3607 len = PyString_GET_SIZE(a->a_lnotab);
3608 if (nbytes >= len) {
3609 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3610 len = nbytes;
3611 else if (len <= INT_MAX / 2)
3612 len *= 2;
3613 else {
3614 PyErr_NoMemory();
3615 return 0;
3616 }
3617 if (_PyString_Resize(&a->a_lnotab, len) < 0)
3618 return 0;
3619 }
3620 lnotab = (unsigned char *)
3621 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3622 *lnotab++ = d_bytecode;
3623 *lnotab++ = 255;
3624 d_bytecode = 0;
3625 for (j = 1; j < ncodes; j++) {
3626 *lnotab++ = 0;
3627 *lnotab++ = 255;
3628 }
3629 d_lineno -= ncodes * 255;
3630 a->a_lnotab_off += ncodes * 2;
3631 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003632
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003633 len = PyString_GET_SIZE(a->a_lnotab);
3634 if (a->a_lnotab_off + 2 >= len) {
3635 if (_PyString_Resize(&a->a_lnotab, len * 2) < 0)
3636 return 0;
3637 }
3638 lnotab = (unsigned char *)
3639 PyString_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003640
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003641 a->a_lnotab_off += 2;
3642 if (d_bytecode) {
3643 *lnotab++ = d_bytecode;
3644 *lnotab++ = d_lineno;
3645 }
3646 else { /* First line of a block; def stmt, etc. */
3647 *lnotab++ = 0;
3648 *lnotab++ = d_lineno;
3649 }
3650 a->a_lineno = i->i_lineno;
3651 a->a_lineno_off = a->a_offset;
3652 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003653}
3654
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655/* assemble_emit()
3656 Extend the bytecode with a new instruction.
3657 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003658*/
3659
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003660static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003661assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003662{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003663 int size, arg = 0, ext = 0;
3664 Py_ssize_t len = PyString_GET_SIZE(a->a_bytecode);
3665 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003667 size = instrsize(i);
3668 if (i->i_hasarg) {
3669 arg = i->i_oparg;
3670 ext = arg >> 16;
3671 }
3672 if (i->i_lineno && !assemble_lnotab(a, i))
3673 return 0;
3674 if (a->a_offset + size >= len) {
3675 if (len > PY_SSIZE_T_MAX / 2)
3676 return 0;
3677 if (_PyString_Resize(&a->a_bytecode, len * 2) < 0)
3678 return 0;
3679 }
3680 code = PyString_AS_STRING(a->a_bytecode) + a->a_offset;
3681 a->a_offset += size;
3682 if (size == 6) {
3683 assert(i->i_hasarg);
3684 *code++ = (char)EXTENDED_ARG;
3685 *code++ = ext & 0xff;
3686 *code++ = ext >> 8;
3687 arg &= 0xffff;
3688 }
3689 *code++ = i->i_opcode;
3690 if (i->i_hasarg) {
3691 assert(size == 3 || size == 6);
3692 *code++ = arg & 0xff;
3693 *code++ = arg >> 8;
3694 }
3695 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003696}
3697
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003698static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003699assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003700{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003701 basicblock *b;
3702 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3703 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003704
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003705 /* Compute the size of each block and fixup jump args.
3706 Replace block pointer with position in bytecode. */
3707 do {
3708 totsize = 0;
3709 for (i = a->a_nblocks - 1; i >= 0; i--) {
3710 b = a->a_postorder[i];
3711 bsize = blocksize(b);
3712 b->b_offset = totsize;
3713 totsize += bsize;
3714 }
3715 last_extended_arg_count = extended_arg_count;
3716 extended_arg_count = 0;
3717 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3718 bsize = b->b_offset;
3719 for (i = 0; i < b->b_iused; i++) {
3720 struct instr *instr = &b->b_instr[i];
3721 /* Relative jumps are computed relative to
3722 the instruction pointer after fetching
3723 the jump instruction.
3724 */
3725 bsize += instrsize(instr);
3726 if (instr->i_jabs)
3727 instr->i_oparg = instr->i_target->b_offset;
3728 else if (instr->i_jrel) {
3729 int delta = instr->i_target->b_offset - bsize;
3730 instr->i_oparg = delta;
3731 }
3732 else
3733 continue;
3734 if (instr->i_oparg > 0xffff)
3735 extended_arg_count++;
3736 }
3737 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003738
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003739 /* XXX: This is an awful hack that could hurt performance, but
3740 on the bright side it should work until we come up
3741 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003742
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003743 The issue is that in the first loop blocksize() is called
3744 which calls instrsize() which requires i_oparg be set
3745 appropriately. There is a bootstrap problem because
3746 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003747
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003748 So we loop until we stop seeing new EXTENDED_ARGs.
3749 The only EXTENDED_ARGs that could be popping up are
3750 ones in jump instructions. So this should converge
3751 fairly quickly.
3752 */
3753 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003754}
3755
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003756static PyObject *
3757dict_keys_inorder(PyObject *dict, int offset)
3758{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003759 PyObject *tuple, *k, *v;
3760 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003762 tuple = PyTuple_New(size);
3763 if (tuple == NULL)
3764 return NULL;
3765 while (PyDict_Next(dict, &pos, &k, &v)) {
3766 i = PyInt_AS_LONG(v);
3767 /* The keys of the dictionary are tuples. (see compiler_add_o)
3768 The object we want is always first, though. */
3769 k = PyTuple_GET_ITEM(k, 0);
3770 Py_INCREF(k);
3771 assert((i - offset) < size);
3772 assert((i - offset) >= 0);
3773 PyTuple_SET_ITEM(tuple, i - offset, k);
3774 }
3775 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003776}
3777
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003778static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003780{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003781 PySTEntryObject *ste = c->u->u_ste;
3782 int flags = 0, n;
3783 if (ste->ste_type != ModuleBlock)
3784 flags |= CO_NEWLOCALS;
3785 if (ste->ste_type == FunctionBlock) {
3786 if (!ste->ste_unoptimized)
3787 flags |= CO_OPTIMIZED;
3788 if (ste->ste_nested)
3789 flags |= CO_NESTED;
3790 if (ste->ste_generator)
3791 flags |= CO_GENERATOR;
3792 if (ste->ste_varargs)
3793 flags |= CO_VARARGS;
3794 if (ste->ste_varkeywords)
3795 flags |= CO_VARKEYWORDS;
3796 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003797
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003798 /* (Only) inherit compilerflags in PyCF_MASK */
3799 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003800
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003801 n = PyDict_Size(c->u->u_freevars);
3802 if (n < 0)
3803 return -1;
3804 if (n == 0) {
3805 n = PyDict_Size(c->u->u_cellvars);
3806 if (n < 0)
3807 return -1;
3808 if (n == 0) {
3809 flags |= CO_NOFREE;
3810 }
3811 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003812
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003813 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003814}
3815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816static PyCodeObject *
3817makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003818{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003819 PyObject *tmp;
3820 PyCodeObject *co = NULL;
3821 PyObject *consts = NULL;
3822 PyObject *names = NULL;
3823 PyObject *varnames = NULL;
3824 PyObject *filename = NULL;
3825 PyObject *name = NULL;
3826 PyObject *freevars = NULL;
3827 PyObject *cellvars = NULL;
3828 PyObject *bytecode = NULL;
3829 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003830
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003831 tmp = dict_keys_inorder(c->u->u_consts, 0);
3832 if (!tmp)
3833 goto error;
3834 consts = PySequence_List(tmp); /* optimize_code requires a list */
3835 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003837 names = dict_keys_inorder(c->u->u_names, 0);
3838 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3839 if (!consts || !names || !varnames)
3840 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003841
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003842 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3843 if (!cellvars)
3844 goto error;
3845 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3846 if (!freevars)
3847 goto error;
3848 filename = PyString_FromString(c->c_filename);
3849 if (!filename)
3850 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003852 nlocals = PyDict_Size(c->u->u_varnames);
3853 flags = compute_code_flags(c);
3854 if (flags < 0)
3855 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003857 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3858 if (!bytecode)
3859 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003860
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003861 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3862 if (!tmp)
3863 goto error;
3864 Py_DECREF(consts);
3865 consts = tmp;
3866
3867 co = PyCode_New(c->u->u_argcount, nlocals, stackdepth(c), flags,
3868 bytecode, consts, names, varnames,
3869 freevars, cellvars,
3870 filename, c->u->u_name,
3871 c->u->u_firstlineno,
3872 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003873 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003874 Py_XDECREF(consts);
3875 Py_XDECREF(names);
3876 Py_XDECREF(varnames);
3877 Py_XDECREF(filename);
3878 Py_XDECREF(name);
3879 Py_XDECREF(freevars);
3880 Py_XDECREF(cellvars);
3881 Py_XDECREF(bytecode);
3882 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003883}
3884
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003885
3886/* For debugging purposes only */
3887#if 0
3888static void
3889dump_instr(const struct instr *i)
3890{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003891 const char *jrel = i->i_jrel ? "jrel " : "";
3892 const char *jabs = i->i_jabs ? "jabs " : "";
3893 char arg[128];
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003894
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003895 *arg = '\0';
3896 if (i->i_hasarg)
3897 sprintf(arg, "arg: %d ", i->i_oparg);
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003899 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3900 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003901}
3902
3903static void
3904dump_basicblock(const basicblock *b)
3905{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003906 const char *seen = b->b_seen ? "seen " : "";
3907 const char *b_return = b->b_return ? "return " : "";
3908 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
3909 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
3910 if (b->b_instr) {
3911 int i;
3912 for (i = 0; i < b->b_iused; i++) {
3913 fprintf(stderr, " [%02d] ", i);
3914 dump_instr(b->b_instr + i);
3915 }
3916 }
Neal Norwitz4ffedad2006-08-04 04:58:47 +00003917}
3918#endif
3919
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003920static PyCodeObject *
3921assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003922{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003923 basicblock *b, *entryblock;
3924 struct assembler a;
3925 int i, j, nblocks;
3926 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003927
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003928 /* Make sure every block that falls off the end returns None.
3929 XXX NEXT_BLOCK() isn't quite right, because if the last
3930 block ends with a jump or return b_next shouldn't set.
3931 */
3932 if (!c->u->u_curblock->b_return) {
3933 NEXT_BLOCK(c);
3934 if (addNone)
3935 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3936 ADDOP(c, RETURN_VALUE);
3937 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003939 nblocks = 0;
3940 entryblock = NULL;
3941 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3942 nblocks++;
3943 entryblock = b;
3944 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003946 /* Set firstlineno if it wasn't explicitly set. */
3947 if (!c->u->u_firstlineno) {
3948 if (entryblock && entryblock->b_instr)
3949 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
3950 else
3951 c->u->u_firstlineno = 1;
3952 }
3953 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
3954 goto error;
3955 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003956
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003957 /* Can't modify the bytecode after computing jump offsets. */
3958 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00003959
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003960 /* Emit code in reverse postorder from dfs. */
3961 for (i = a.a_nblocks - 1; i >= 0; i--) {
3962 b = a.a_postorder[i];
3963 for (j = 0; j < b->b_iused; j++)
3964 if (!assemble_emit(&a, &b->b_instr[j]))
3965 goto error;
3966 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00003967
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003968 if (_PyString_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
3969 goto error;
3970 if (_PyString_Resize(&a.a_bytecode, a.a_offset) < 0)
3971 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003972
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003973 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974 error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003975 assemble_free(&a);
3976 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003977}