blob: 081268bcacc5a7ab3fd1411b9484d8ee27df0cfd [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Jeremy Hyltone9357b22006-03-01 15:47:05 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Guido van Rossumd8faa362007-04-27 19:54:29 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "Python-ast.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000027#include "node.h"
Neal Norwitzadb69fc2005-12-17 20:54:49 +000028#include "pyarena.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029#include "ast.h"
30#include "code.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "compile.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000032#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000033#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000034
Guido van Rossum8e793d91997-03-03 19:13:14 +000035int Py_OptimizeFlag = 0;
36
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000037#define DEFAULT_BLOCK_SIZE 16
38#define DEFAULT_BLOCKS 8
39#define DEFAULT_CODE_SIZE 128
40#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000041
Nick Coghlan650f0d02007-04-15 12:05:43 +000042#define COMP_GENEXP 0
43#define COMP_LISTCOMP 1
44#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000045#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047struct instr {
Neal Norwitz08b401f2006-01-07 21:24:09 +000048 unsigned i_jabs : 1;
49 unsigned i_jrel : 1;
50 unsigned i_hasarg : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000051 unsigned char i_opcode;
52 int i_oparg;
53 struct basicblock_ *i_target; /* target block (if jump instruction) */
54 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000055};
56
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000057typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000058 /* Each basicblock in a compilation unit is linked via b_list in the
59 reverse order that the block are allocated. b_list points to the next
60 block, not to be confused with b_next, which is next by control flow. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000061 struct basicblock_ *b_list;
62 /* number of instructions used */
63 int b_iused;
64 /* length of instruction array (b_instr) */
65 int b_ialloc;
66 /* pointer to an array of instructions, initially NULL */
67 struct instr *b_instr;
68 /* If b_next is non-NULL, it is a pointer to the next
69 block reached by normal control flow. */
70 struct basicblock_ *b_next;
71 /* b_seen is used to perform a DFS of basicblocks. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000072 unsigned b_seen : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000073 /* b_return is true if a RETURN_VALUE opcode is inserted. */
Neal Norwitz08b401f2006-01-07 21:24:09 +000074 unsigned b_return : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075 /* depth of stack upon entry of block, computed by stackdepth() */
76 int b_startdepth;
77 /* instruction offset for block, computed by assemble_jump_offsets() */
Jeremy Hyltone9357b22006-03-01 15:47:05 +000078 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000079} basicblock;
80
81/* fblockinfo tracks the current frame block.
82
Jeremy Hyltone9357b22006-03-01 15:47:05 +000083A frame block is used to handle loops, try/except, and try/finally.
84It's called a frame block to distinguish it from a basic block in the
85compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000086*/
87
88enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
89
90struct fblockinfo {
Jeremy Hyltone9357b22006-03-01 15:47:05 +000091 enum fblocktype fb_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000092 basicblock *fb_block;
93};
94
95/* The following items change on entry and exit of code blocks.
96 They must be saved and restored when returning to a block.
97*/
98struct compiler_unit {
99 PySTEntryObject *u_ste;
100
101 PyObject *u_name;
102 /* The following fields are dicts that map objects to
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000103 the index of them in co_XXX. The index is used as
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000104 the argument for opcodes that refer to those collections.
105 */
106 PyObject *u_consts; /* all constants */
107 PyObject *u_names; /* all names */
108 PyObject *u_varnames; /* local variables */
109 PyObject *u_cellvars; /* cell variables */
110 PyObject *u_freevars; /* free variables */
111
112 PyObject *u_private; /* for private name mangling */
113
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000114 int u_argcount; /* number of arguments for block */
Guido van Rossum4f72a782006-10-27 23:31:49 +0000115 int u_kwonlyargcount; /* number of keyword only arguments for block */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000116 /* Pointer to the most recently allocated block. By following b_list
117 members, you can reach all early allocated blocks. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000118 basicblock *u_blocks;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000120
121 int u_nfblocks;
122 struct fblockinfo u_fblock[CO_MAXBLOCKS];
123
124 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000125 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000126 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127 has been generated with current lineno */
128};
129
130/* This struct captures the global state of a compilation.
131
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000132The u pointer points to the current compilation unit, while units
133for enclosing blocks are stored in c_stack. The u and c_stack are
134managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135*/
136
137struct compiler {
138 const char *c_filename;
139 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000141 PyCompilerFlags *c_flags;
142
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000143 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000144 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000145
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000148 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149};
150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151static int compiler_enter_scope(struct compiler *, identifier, void *, int);
152static void compiler_free(struct compiler *);
153static basicblock *compiler_new_block(struct compiler *);
154static int compiler_next_instr(struct compiler *, basicblock *);
155static int compiler_addop(struct compiler *, int);
156static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
157static int compiler_addop_i(struct compiler *, int, int);
158static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000159static basicblock *compiler_use_new_block(struct compiler *);
160static int compiler_error(struct compiler *, const char *);
161static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
162
163static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
164static int compiler_visit_stmt(struct compiler *, stmt_ty);
165static int compiler_visit_keyword(struct compiler *, keyword_ty);
166static int compiler_visit_expr(struct compiler *, expr_ty);
167static int compiler_augassign(struct compiler *, stmt_ty);
168static int compiler_visit_slice(struct compiler *, slice_ty,
169 expr_context_ty);
170
171static int compiler_push_fblock(struct compiler *, enum fblocktype,
172 basicblock *);
173static void compiler_pop_fblock(struct compiler *, enum fblocktype,
174 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175/* Returns true if there is a loop on the fblock stack. */
176static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177
178static int inplace_binop(struct compiler *, operator_ty);
179static int expr_constant(expr_ty e);
180
Guido van Rossumc2e20742006-02-27 22:32:47 +0000181static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000182static int compiler_call_helper(struct compiler *c, int n,
183 asdl_seq *args,
184 asdl_seq *keywords,
185 expr_ty starargs,
186 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000187
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static PyCodeObject *assemble(struct compiler *, int addNone);
189static PyObject *__doc__;
190
Benjamin Petersonb173f782009-05-05 22:31:58 +0000191#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
192
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000194_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000195{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196 /* Name mangling: __private becomes _classname__private.
197 This is independent from how the name is used. */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000198 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
199 Py_UNICODE *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200 size_t nlen, plen;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000201 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000202 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000203 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000205 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000206 p = PyUnicode_AS_UNICODE(privateobj);
207 nlen = Py_UNICODE_strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000208 /* Don't mangle __id__ or names with dots.
209
210 The only time a name with a dot can occur is when
211 we are compiling an import statement that has a
212 package name.
213
214 TODO(jhylton): Decide whether we want to support
215 mangling of the module name, e.g. __M.X.
216 */
217 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Martin v. Löwis5b222132007-06-10 09:51:05 +0000218 || Py_UNICODE_strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000219 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 /* Strip leading underscores from class name */
223 while (*p == '_')
224 p++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000225 if (*p == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000226 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000229 plen = Py_UNICODE_strlen(p);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000230
231 assert(1 <= PY_SSIZE_T_MAX - nlen);
232 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
233
Martin v. Löwis5b222132007-06-10 09:51:05 +0000234 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000235 if (!ident)
236 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000237 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000238 buffer = PyUnicode_AS_UNICODE(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000239 buffer[0] = '_';
Martin v. Löwis5b222132007-06-10 09:51:05 +0000240 Py_UNICODE_strncpy(buffer+1, p, plen);
241 Py_UNICODE_strcpy(buffer+1+plen, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000242 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000243}
244
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245static int
246compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000247{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000249
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 c->c_stack = PyList_New(0);
251 if (!c->c_stack)
252 return 0;
253
254 return 1;
255}
256
257PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000258PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000259 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260{
261 struct compiler c;
262 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000263 PyCompilerFlags local_flags;
264 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000266 if (!__doc__) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000267 __doc__ = PyUnicode_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000268 if (!__doc__)
269 return NULL;
270 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271
272 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000273 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000274 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000275 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276 c.c_future = PyFuture_FromAST(mod, filename);
277 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000278 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000279 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000280 local_flags.cf_flags = 0;
281 flags = &local_flags;
282 }
283 merged = c.c_future->ff_features | flags->cf_flags;
284 c.c_future->ff_features = merged;
285 flags->cf_flags = merged;
286 c.c_flags = flags;
287 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288
289 c.c_st = PySymtable_Build(mod, filename, c.c_future);
290 if (c.c_st == NULL) {
291 if (!PyErr_Occurred())
292 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000293 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294 }
295
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296 co = compiler_mod(&c, mod);
297
Thomas Wouters1175c432006-02-27 22:49:54 +0000298 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000299 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000300 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301 return co;
302}
303
304PyCodeObject *
305PyNode_Compile(struct _node *n, const char *filename)
306{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000307 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000308 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000309 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000310 if (!arena)
311 return NULL;
312 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000313 if (mod)
314 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000315 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000316 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000317}
318
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000319static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000321{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322 if (c->c_st)
323 PySymtable_Free(c->c_st);
324 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000327}
328
Guido van Rossum79f25d91997-04-29 20:08:16 +0000329static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000331{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000332 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333 PyObject *v, *k;
334 PyObject *dict = PyDict_New();
335 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 n = PyList_Size(list);
338 for (i = 0; i < n; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000339 v = PyLong_FromLong(i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000340 if (!v) {
341 Py_DECREF(dict);
342 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000343 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000344 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000345 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
347 Py_XDECREF(k);
348 Py_DECREF(v);
349 Py_DECREF(dict);
350 return NULL;
351 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000352 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000354 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355 return dict;
356}
357
358/* Return new dict containing names from src that match scope(s).
359
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000360src is a symbol table dictionary. If the scope of a name matches
361either scope_type or flag is set, insert it into the new dict. The
362values are integers, starting at offset and increasing by one for
363each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364*/
365
366static PyObject *
367dictbytype(PyObject *src, int scope_type, int flag, int offset)
368{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000369 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370 PyObject *k, *v, *dest = PyDict_New();
371
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000372 assert(offset >= 0);
373 if (dest == NULL)
374 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375
376 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000377 /* XXX this should probably be a macro in symtable.h */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000378 long vi;
Christian Heimes217cfd12007-12-02 14:31:20 +0000379 assert(PyLong_Check(v));
380 vi = PyLong_AS_LONG(v);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000381 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000383 if (scope == scope_type || vi & flag) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000384 PyObject *tuple, *item = PyLong_FromLong(i);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000385 if (item == NULL) {
386 Py_DECREF(dest);
387 return NULL;
388 }
389 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000390 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000391 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
392 Py_DECREF(item);
393 Py_DECREF(dest);
394 Py_XDECREF(tuple);
395 return NULL;
396 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000398 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400 }
401 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000402}
403
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404static void
405compiler_unit_check(struct compiler_unit *u)
406{
407 basicblock *block;
408 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Christian Heimes77c02eb2008-02-09 02:18:51 +0000409 assert((void *)block != (void *)0xcbcbcbcb);
410 assert((void *)block != (void *)0xfbfbfbfb);
411 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412 if (block->b_instr != NULL) {
413 assert(block->b_ialloc > 0);
414 assert(block->b_iused > 0);
415 assert(block->b_ialloc >= block->b_iused);
416 }
417 else {
418 assert (block->b_iused == 0);
419 assert (block->b_ialloc == 0);
420 }
421 }
422}
423
424static void
425compiler_unit_free(struct compiler_unit *u)
426{
427 basicblock *b, *next;
428
429 compiler_unit_check(u);
430 b = u->u_blocks;
431 while (b != NULL) {
432 if (b->b_instr)
433 PyObject_Free((void *)b->b_instr);
434 next = b->b_list;
435 PyObject_Free((void *)b);
436 b = next;
437 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000438 Py_CLEAR(u->u_ste);
439 Py_CLEAR(u->u_name);
440 Py_CLEAR(u->u_consts);
441 Py_CLEAR(u->u_names);
442 Py_CLEAR(u->u_varnames);
443 Py_CLEAR(u->u_freevars);
444 Py_CLEAR(u->u_cellvars);
445 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446 PyObject_Free(u);
447}
448
449static int
450compiler_enter_scope(struct compiler *c, identifier name, void *key,
451 int lineno)
452{
453 struct compiler_unit *u;
454
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000456 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000457 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000458 PyErr_NoMemory();
459 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000460 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000461 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000463 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464 u->u_ste = PySymtable_Lookup(c->c_st, key);
465 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000466 compiler_unit_free(u);
467 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468 }
469 Py_INCREF(name);
470 u->u_name = name;
471 u->u_varnames = list2dict(u->u_ste->ste_varnames);
472 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000473 if (!u->u_varnames || !u->u_cellvars) {
474 compiler_unit_free(u);
475 return 0;
476 }
477
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000479 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000480 if (!u->u_freevars) {
481 compiler_unit_free(u);
482 return 0;
483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
485 u->u_blocks = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486 u->u_nfblocks = 0;
487 u->u_firstlineno = lineno;
488 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000489 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490 u->u_consts = PyDict_New();
491 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000492 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493 return 0;
494 }
495 u->u_names = PyDict_New();
496 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000497 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498 return 0;
499 }
500
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000501 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502
503 /* Push the old compiler_unit on the stack. */
504 if (c->u) {
Benjamin Petersonb173f782009-05-05 22:31:58 +0000505 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
506 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
507 Py_XDECREF(capsule);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000508 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509 return 0;
510 }
Benjamin Petersonb173f782009-05-05 22:31:58 +0000511 Py_DECREF(capsule);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000512 u->u_private = c->u->u_private;
513 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000514 }
515 c->u = u;
516
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000517 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000518 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519 return 0;
520
521 return 1;
522}
523
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000524static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525compiler_exit_scope(struct compiler *c)
526{
527 int n;
Benjamin Petersonb173f782009-05-05 22:31:58 +0000528 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000530 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531 compiler_unit_free(c->u);
532 /* Restore c->u to the parent unit. */
533 n = PyList_GET_SIZE(c->c_stack) - 1;
534 if (n >= 0) {
Benjamin Petersonb173f782009-05-05 22:31:58 +0000535 capsule = PyList_GET_ITEM(c->c_stack, n);
536 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000537 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000538 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000539 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000540 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000541 compiler_unit_check(c->u);
542 }
543 else
544 c->u = NULL;
545
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546}
547
548/* Allocate a new block and return a pointer to it.
549 Returns NULL on error.
550*/
551
552static basicblock *
553compiler_new_block(struct compiler *c)
554{
555 basicblock *b;
556 struct compiler_unit *u;
557
558 u = c->u;
559 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000560 if (b == NULL) {
561 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000563 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000564 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000565 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566 b->b_list = u->u_blocks;
567 u->u_blocks = b;
568 return b;
569}
570
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571static basicblock *
572compiler_use_new_block(struct compiler *c)
573{
574 basicblock *block = compiler_new_block(c);
575 if (block == NULL)
576 return NULL;
577 c->u->u_curblock = block;
578 return block;
579}
580
581static basicblock *
582compiler_next_block(struct compiler *c)
583{
584 basicblock *block = compiler_new_block(c);
585 if (block == NULL)
586 return NULL;
587 c->u->u_curblock->b_next = block;
588 c->u->u_curblock = block;
589 return block;
590}
591
592static basicblock *
593compiler_use_next_block(struct compiler *c, basicblock *block)
594{
595 assert(block != NULL);
596 c->u->u_curblock->b_next = block;
597 c->u->u_curblock = block;
598 return block;
599}
600
601/* Returns the offset of the next instruction in the current block's
602 b_instr array. Resizes the b_instr as necessary.
603 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000604*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
606static int
607compiler_next_instr(struct compiler *c, basicblock *b)
608{
609 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000610 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000612 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 if (b->b_instr == NULL) {
614 PyErr_NoMemory();
615 return -1;
616 }
617 b->b_ialloc = DEFAULT_BLOCK_SIZE;
618 memset((char *)b->b_instr, 0,
619 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000620 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000622 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623 size_t oldsize, newsize;
624 oldsize = b->b_ialloc * sizeof(struct instr);
625 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000626
627 if (oldsize > (PY_SIZE_MAX >> 1)) {
628 PyErr_NoMemory();
629 return -1;
630 }
631
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632 if (newsize == 0) {
633 PyErr_NoMemory();
634 return -1;
635 }
636 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000638 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000639 if (tmp == NULL) {
640 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000642 }
643 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000644 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
645 }
646 return b->b_iused++;
647}
648
Christian Heimes2202f872008-02-06 14:31:34 +0000649/* Set the i_lineno member of the instruction at offset off if the
650 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651 already been set. If it has been set, the call has no effect.
652
Christian Heimes2202f872008-02-06 14:31:34 +0000653 The line number is reset in the following cases:
654 - when entering a new scope
655 - on each statement
656 - on each expression that start a new line
657 - before the "except" clause
658 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000659*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000661static void
662compiler_set_lineno(struct compiler *c, int off)
663{
664 basicblock *b;
665 if (c->u->u_lineno_set)
666 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000667 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000669 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670}
671
672static int
673opcode_stack_effect(int opcode, int oparg)
674{
675 switch (opcode) {
676 case POP_TOP:
677 return -1;
678 case ROT_TWO:
679 case ROT_THREE:
680 return 0;
681 case DUP_TOP:
682 return 1;
683 case ROT_FOUR:
684 return 0;
685
686 case UNARY_POSITIVE:
687 case UNARY_NEGATIVE:
688 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689 case UNARY_INVERT:
690 return 0;
691
Nick Coghlan650f0d02007-04-15 12:05:43 +0000692 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000693 case LIST_APPEND:
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000694 return -1;
695 case MAP_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000696 return -2;
697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698 case BINARY_POWER:
699 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700 case BINARY_MODULO:
701 case BINARY_ADD:
702 case BINARY_SUBTRACT:
703 case BINARY_SUBSCR:
704 case BINARY_FLOOR_DIVIDE:
705 case BINARY_TRUE_DIVIDE:
706 return -1;
707 case INPLACE_FLOOR_DIVIDE:
708 case INPLACE_TRUE_DIVIDE:
709 return -1;
710
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711 case INPLACE_ADD:
712 case INPLACE_SUBTRACT:
713 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 case INPLACE_MODULO:
715 return -1;
716 case STORE_SUBSCR:
717 return -3;
Christian Heimes99170a52007-12-19 02:07:34 +0000718 case STORE_MAP:
719 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 case DELETE_SUBSCR:
721 return -2;
722
723 case BINARY_LSHIFT:
724 case BINARY_RSHIFT:
725 case BINARY_AND:
726 case BINARY_XOR:
727 case BINARY_OR:
728 return -1;
729 case INPLACE_POWER:
730 return -1;
731 case GET_ITER:
732 return 0;
733
734 case PRINT_EXPR:
735 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000736 case LOAD_BUILD_CLASS:
737 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 case INPLACE_LSHIFT:
739 case INPLACE_RSHIFT:
740 case INPLACE_AND:
741 case INPLACE_XOR:
742 case INPLACE_OR:
743 return -1;
744 case BREAK_LOOP:
745 return 0;
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000746 case SETUP_WITH:
747 return 7;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000748 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000749 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000750 case STORE_LOCALS:
751 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000752 case RETURN_VALUE:
753 return -1;
754 case IMPORT_STAR:
755 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 case YIELD_VALUE:
757 return 0;
758
759 case POP_BLOCK:
760 return 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000761 case POP_EXCEPT:
762 return 0; /* -3 except if bad bytecode */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763 case END_FINALLY:
764 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765
766 case STORE_NAME:
767 return -1;
768 case DELETE_NAME:
769 return 0;
770 case UNPACK_SEQUENCE:
771 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000772 case UNPACK_EX:
773 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774 case FOR_ITER:
Neil Schemenauere441cb72009-10-14 19:14:38 +0000775 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776
777 case STORE_ATTR:
778 return -2;
779 case DELETE_ATTR:
780 return -1;
781 case STORE_GLOBAL:
782 return -1;
783 case DELETE_GLOBAL:
784 return 0;
785 case DUP_TOPX:
786 return oparg;
787 case LOAD_CONST:
788 return 1;
789 case LOAD_NAME:
790 return 1;
791 case BUILD_TUPLE:
792 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000793 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000794 return 1-oparg;
795 case BUILD_MAP:
796 return 1;
797 case LOAD_ATTR:
798 return 0;
799 case COMPARE_OP:
800 return -1;
801 case IMPORT_NAME:
Neil Schemenauere441cb72009-10-14 19:14:38 +0000802 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803 case IMPORT_FROM:
804 return 1;
805
806 case JUMP_FORWARD:
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000807 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
808 case JUMP_IF_FALSE_OR_POP: /* "" */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000809 case JUMP_ABSOLUTE:
810 return 0;
811
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000812 case POP_JUMP_IF_FALSE:
813 case POP_JUMP_IF_TRUE:
814 return -1;
815
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000816 case LOAD_GLOBAL:
817 return 1;
818
819 case CONTINUE_LOOP:
820 return 0;
821 case SETUP_LOOP:
822 return 0;
823 case SETUP_EXCEPT:
824 case SETUP_FINALLY:
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000825 return 6; /* can push 3 values for the new exception
826 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827
828 case LOAD_FAST:
829 return 1;
830 case STORE_FAST:
831 return -1;
832 case DELETE_FAST:
833 return 0;
834
835 case RAISE_VARARGS:
836 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000837#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000838 case CALL_FUNCTION:
839 return -NARGS(oparg);
840 case CALL_FUNCTION_VAR:
841 case CALL_FUNCTION_KW:
842 return -NARGS(oparg)-1;
843 case CALL_FUNCTION_VAR_KW:
844 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000846 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000847 case MAKE_CLOSURE:
848 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000849#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850 case BUILD_SLICE:
851 if (oparg == 3)
852 return -2;
853 else
854 return -1;
855
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856 case LOAD_CLOSURE:
857 return 1;
858 case LOAD_DEREF:
859 return 1;
860 case STORE_DEREF:
861 return -1;
862 default:
863 fprintf(stderr, "opcode = %d\n", opcode);
864 Py_FatalError("opcode_stack_effect()");
865
866 }
867 return 0; /* not reachable */
868}
869
870/* Add an opcode with no argument.
871 Returns 0 on failure, 1 on success.
872*/
873
874static int
875compiler_addop(struct compiler *c, int opcode)
876{
877 basicblock *b;
878 struct instr *i;
879 int off;
880 off = compiler_next_instr(c, c->u->u_curblock);
881 if (off < 0)
882 return 0;
883 b = c->u->u_curblock;
884 i = &b->b_instr[off];
885 i->i_opcode = opcode;
886 i->i_hasarg = 0;
887 if (opcode == RETURN_VALUE)
888 b->b_return = 1;
889 compiler_set_lineno(c, off);
890 return 1;
891}
892
893static int
894compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
895{
896 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000897 Py_ssize_t arg;
Mark Dickinson42e30552009-10-15 19:55:18 +0000898 unsigned char *p;
Christian Heimes400adb02008-02-01 08:12:03 +0000899 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000901 /* necessary to make sure types aren't coerced (e.g., int and long) */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000902 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
903 if (PyFloat_Check(o)) {
Christian Heimes400adb02008-02-01 08:12:03 +0000904 d = PyFloat_AS_DOUBLE(o);
905 p = (unsigned char*) &d;
906 /* all we need is to make the tuple different in either the 0.0
907 * or -0.0 case from all others, just to avoid the "coercion".
908 */
909 if (*p==0 && p[sizeof(double)-1]==0)
910 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
911 else
912 t = PyTuple_Pack(2, o, o->ob_type);
913 }
Mark Dickinson42e30552009-10-15 19:55:18 +0000914#ifndef WITHOUT_COMPLEX
Christian Heimes400adb02008-02-01 08:12:03 +0000915 else if (PyComplex_Check(o)) {
Mark Dickinson42e30552009-10-15 19:55:18 +0000916 Py_complex z;
917 int real_part_zero, imag_part_zero;
918 unsigned char *q;
Christian Heimes400adb02008-02-01 08:12:03 +0000919 /* complex case is even messier: we need to make complex(x,
920 0.) different from complex(x, -0.) and complex(0., y)
921 different from complex(-0., y), for any x and y. In
922 particular, all four complex zeros should be
923 distinguished.*/
924 z = PyComplex_AsCComplex(o);
925 p = (unsigned char*) &(z.real);
926 q = (unsigned char*) &(z.imag);
927 /* all that matters here is that on IEEE platforms
928 real_part_zero will be true if z.real == 0., and false if
929 z.real == -0. In fact, real_part_zero will also be true
930 for some other rarely occurring nonzero floats, but this
931 doesn't matter. Similar comments apply to
932 imag_part_zero. */
933 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
934 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
935 if (real_part_zero && imag_part_zero) {
936 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
937 }
938 else if (real_part_zero && !imag_part_zero) {
939 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
940 }
941 else if (!real_part_zero && imag_part_zero) {
942 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
943 }
944 else {
945 t = PyTuple_Pack(2, o, o->ob_type);
946 }
947 }
Mark Dickinson42e30552009-10-15 19:55:18 +0000948#endif /* WITHOUT_COMPLEX */
Christian Heimes400adb02008-02-01 08:12:03 +0000949 else {
950 t = PyTuple_Pack(2, o, o->ob_type);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000951 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000952 if (t == NULL)
Christian Heimes400adb02008-02-01 08:12:03 +0000953 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
955 v = PyDict_GetItem(dict, t);
956 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000957 if (PyErr_Occurred())
958 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000959 arg = PyDict_Size(dict);
Christian Heimes217cfd12007-12-02 14:31:20 +0000960 v = PyLong_FromLong(arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961 if (!v) {
962 Py_DECREF(t);
963 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000964 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965 if (PyDict_SetItem(dict, t, v) < 0) {
966 Py_DECREF(t);
967 Py_DECREF(v);
968 return -1;
969 }
970 Py_DECREF(v);
971 }
972 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000973 arg = PyLong_AsLong(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000975 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976}
977
978static int
979compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
980 PyObject *o)
981{
982 int arg = compiler_add_o(c, dict, o);
983 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000984 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000985 return compiler_addop_i(c, opcode, arg);
986}
987
988static int
989compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000990 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991{
992 int arg;
993 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
994 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000995 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 arg = compiler_add_o(c, dict, mangled);
997 Py_DECREF(mangled);
998 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000999 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000 return compiler_addop_i(c, opcode, arg);
1001}
1002
1003/* Add an opcode with an integer argument.
1004 Returns 0 on failure, 1 on success.
1005*/
1006
1007static int
1008compiler_addop_i(struct compiler *c, int opcode, int oparg)
1009{
1010 struct instr *i;
1011 int off;
1012 off = compiler_next_instr(c, c->u->u_curblock);
1013 if (off < 0)
1014 return 0;
1015 i = &c->u->u_curblock->b_instr[off];
1016 i->i_opcode = opcode;
1017 i->i_oparg = oparg;
1018 i->i_hasarg = 1;
1019 compiler_set_lineno(c, off);
1020 return 1;
1021}
1022
1023static int
1024compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1025{
1026 struct instr *i;
1027 int off;
1028
1029 assert(b != NULL);
1030 off = compiler_next_instr(c, c->u->u_curblock);
1031 if (off < 0)
1032 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033 i = &c->u->u_curblock->b_instr[off];
1034 i->i_opcode = opcode;
1035 i->i_target = b;
1036 i->i_hasarg = 1;
1037 if (absolute)
1038 i->i_jabs = 1;
1039 else
1040 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001041 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 return 1;
1043}
1044
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001045/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1046 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047 it as the current block. NEXT_BLOCK() also creates an implicit jump
1048 from the current block to the new block.
1049*/
1050
Thomas Wouters89f507f2006-12-13 04:49:30 +00001051/* The returns inside these macros make it impossible to decref objects
1052 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053*/
1054
1055
1056#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001057 if (compiler_use_new_block((C)) == NULL) \
1058 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059}
1060
1061#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001062 if (compiler_next_block((C)) == NULL) \
1063 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064}
1065
1066#define ADDOP(C, OP) { \
1067 if (!compiler_addop((C), (OP))) \
1068 return 0; \
1069}
1070
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001071#define ADDOP_IN_SCOPE(C, OP) { \
1072 if (!compiler_addop((C), (OP))) { \
1073 compiler_exit_scope(c); \
1074 return 0; \
1075 } \
1076}
1077
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001078#define ADDOP_O(C, OP, O, TYPE) { \
1079 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1080 return 0; \
1081}
1082
1083#define ADDOP_NAME(C, OP, O, TYPE) { \
1084 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1085 return 0; \
1086}
1087
1088#define ADDOP_I(C, OP, O) { \
1089 if (!compiler_addop_i((C), (OP), (O))) \
1090 return 0; \
1091}
1092
1093#define ADDOP_JABS(C, OP, O) { \
1094 if (!compiler_addop_j((C), (OP), (O), 1)) \
1095 return 0; \
1096}
1097
1098#define ADDOP_JREL(C, OP, O) { \
1099 if (!compiler_addop_j((C), (OP), (O), 0)) \
1100 return 0; \
1101}
1102
1103/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1104 the ASDL name to synthesize the name of the C type and the visit function.
1105*/
1106
1107#define VISIT(C, TYPE, V) {\
1108 if (!compiler_visit_ ## TYPE((C), (V))) \
1109 return 0; \
1110}
1111
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001112#define VISIT_IN_SCOPE(C, TYPE, V) {\
1113 if (!compiler_visit_ ## TYPE((C), (V))) { \
1114 compiler_exit_scope(c); \
1115 return 0; \
1116 } \
1117}
1118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119#define VISIT_SLICE(C, V, CTX) {\
1120 if (!compiler_visit_slice((C), (V), (CTX))) \
1121 return 0; \
1122}
1123
1124#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001125 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001127 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001128 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129 if (!compiler_visit_ ## TYPE((C), elt)) \
1130 return 0; \
1131 } \
1132}
1133
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001134#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001135 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001136 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001137 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001138 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001139 if (!compiler_visit_ ## TYPE((C), elt)) { \
1140 compiler_exit_scope(c); \
1141 return 0; \
1142 } \
1143 } \
1144}
1145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146static int
1147compiler_isdocstring(stmt_ty s)
1148{
1149 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001150 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 return s->v.Expr.value->kind == Str_kind;
1152}
1153
1154/* Compile a sequence of statements, checking for a docstring. */
1155
1156static int
1157compiler_body(struct compiler *c, asdl_seq *stmts)
1158{
1159 int i = 0;
1160 stmt_ty st;
1161
1162 if (!asdl_seq_LEN(stmts))
1163 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001164 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001165 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1166 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001167 i = 1;
1168 VISIT(c, expr, st->v.Expr.value);
1169 if (!compiler_nameop(c, __doc__, Store))
1170 return 0;
1171 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001172 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001173 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174 return 1;
1175}
1176
1177static PyCodeObject *
1178compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001179{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001180 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001181 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 static PyObject *module;
1183 if (!module) {
Christian Heimesfe82e772008-01-28 02:38:20 +00001184 module = PyUnicode_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 if (!module)
1186 return NULL;
1187 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001188 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1189 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001190 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191 switch (mod->kind) {
1192 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001193 if (!compiler_body(c, mod->v.Module.body)) {
1194 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001196 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001197 break;
1198 case Interactive_kind:
1199 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001201 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001202 break;
1203 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001204 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001205 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001206 break;
1207 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001208 PyErr_SetString(PyExc_SystemError,
1209 "suite should not be possible");
1210 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001211 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001212 PyErr_Format(PyExc_SystemError,
1213 "module kind %d should not be possible",
1214 mod->kind);
1215 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001216 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217 co = assemble(c, addNone);
1218 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001219 return co;
1220}
1221
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222/* The test for LOCAL must come before the test for FREE in order to
1223 handle classes where name is both local and free. The local var is
1224 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001225*/
1226
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001227static int
1228get_ref_type(struct compiler *c, PyObject *name)
1229{
1230 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001231 if (scope == 0) {
1232 char buf[350];
1233 PyOS_snprintf(buf, sizeof(buf),
1234 "unknown scope for %.100s in %.100s(%s) in %s\n"
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001235 "symbols: %s\nlocals: %s\nglobals: %s",
Christian Heimes72b710a2008-05-26 13:28:38 +00001236 PyBytes_AS_STRING(name),
1237 PyBytes_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001238 PyObject_REPR(c->u->u_ste->ste_id),
1239 c->c_filename,
1240 PyObject_REPR(c->u->u_ste->ste_symbols),
1241 PyObject_REPR(c->u->u_varnames),
1242 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001244 Py_FatalError(buf);
1245 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001246
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001247 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001248}
1249
1250static int
1251compiler_lookup_arg(PyObject *dict, PyObject *name)
1252{
1253 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001254 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001256 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001258 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001260 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001261 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264static int
1265compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1266{
1267 int i, free = PyCode_GetNumFree(co);
1268 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1270 ADDOP_I(c, MAKE_FUNCTION, args);
1271 return 1;
1272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273 for (i = 0; i < free; ++i) {
1274 /* Bypass com_addop_varname because it will generate
1275 LOAD_DEREF but LOAD_CLOSURE is needed.
1276 */
1277 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1278 int arg, reftype;
1279
1280 /* Special case: If a class contains a method with a
1281 free variable that has the same name as a method,
1282 the name will be considered free *and* local in the
1283 class. It should be handled by the closure, as
1284 well as by the normal name loookup logic.
1285 */
1286 reftype = get_ref_type(c, name);
1287 if (reftype == CELL)
1288 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1289 else /* (reftype == FREE) */
1290 arg = compiler_lookup_arg(c->u->u_freevars, name);
1291 if (arg == -1) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001292 fprintf(stderr,
1293 "lookup %s in %s %d %d\n"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 "freevars of %s: %s\n",
1295 PyObject_REPR(name),
Christian Heimes72b710a2008-05-26 13:28:38 +00001296 PyBytes_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001297 reftype, arg,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001298 _PyUnicode_AsString(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299 PyObject_REPR(co->co_freevars));
1300 Py_FatalError("compiler_make_closure()");
1301 }
1302 ADDOP_I(c, LOAD_CLOSURE, arg);
1303 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001304 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001306 ADDOP_I(c, MAKE_CLOSURE, args);
1307 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308}
1309
1310static int
1311compiler_decorators(struct compiler *c, asdl_seq* decos)
1312{
1313 int i;
1314
1315 if (!decos)
1316 return 1;
1317
1318 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320 }
1321 return 1;
1322}
1323
1324static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001325compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1326 asdl_seq *kw_defaults)
1327{
1328 int i, default_count = 0;
1329 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001330 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1332 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001333 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001334 if (!compiler_visit_expr(c, default_)) {
1335 return -1;
1336 }
1337 default_count++;
1338 }
1339 }
1340 return default_count;
1341}
1342
1343static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001344compiler_visit_argannotation(struct compiler *c, identifier id,
1345 expr_ty annotation, PyObject *names)
1346{
1347 if (annotation) {
1348 VISIT(c, expr, annotation);
1349 if (PyList_Append(names, id))
1350 return -1;
1351 }
1352 return 0;
1353}
1354
1355static int
1356compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1357 PyObject *names)
1358{
1359 int i, error;
1360 for (i = 0; i < asdl_seq_LEN(args); i++) {
1361 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001362 error = compiler_visit_argannotation(
1363 c,
1364 arg->arg,
1365 arg->annotation,
1366 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001367 if (error)
1368 return error;
1369 }
1370 return 0;
1371}
1372
1373static int
1374compiler_visit_annotations(struct compiler *c, arguments_ty args,
1375 expr_ty returns)
1376{
Guido van Rossum0240b922007-02-26 21:23:50 +00001377 /* Push arg annotations and a list of the argument names. Return the #
1378 of items pushed. The expressions are evaluated out-of-order wrt the
1379 source code.
1380
1381 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1382 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001383 static identifier return_str;
1384 PyObject *names;
1385 int len;
1386 names = PyList_New(0);
1387 if (!names)
1388 return -1;
1389
1390 if (compiler_visit_argannotations(c, args->args, names))
1391 goto error;
1392 if (args->varargannotation &&
1393 compiler_visit_argannotation(c, args->vararg,
1394 args->varargannotation, names))
1395 goto error;
1396 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1397 goto error;
1398 if (args->kwargannotation &&
1399 compiler_visit_argannotation(c, args->kwarg,
1400 args->kwargannotation, names))
1401 goto error;
1402
1403 if (!return_str) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001404 return_str = PyUnicode_InternFromString("return");
Neal Norwitzc1505362006-12-28 06:47:50 +00001405 if (!return_str)
1406 goto error;
1407 }
1408 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1409 goto error;
1410 }
1411
1412 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001413 if (len > 65534) {
1414 /* len must fit in 16 bits, and len is incremented below */
1415 PyErr_SetString(PyExc_SyntaxError,
1416 "too many annotations");
1417 goto error;
1418 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001419 if (len) {
1420 /* convert names to a tuple and place on stack */
1421 PyObject *elt;
1422 int i;
1423 PyObject *s = PyTuple_New(len);
1424 if (!s)
1425 goto error;
1426 for (i = 0; i < len; i++) {
1427 elt = PyList_GET_ITEM(names, i);
1428 Py_INCREF(elt);
1429 PyTuple_SET_ITEM(s, i, elt);
1430 }
1431 ADDOP_O(c, LOAD_CONST, s, consts);
1432 Py_DECREF(s);
1433 len++; /* include the just-pushed tuple */
1434 }
1435 Py_DECREF(names);
1436 return len;
1437
1438error:
1439 Py_DECREF(names);
1440 return -1;
1441}
1442
1443static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444compiler_function(struct compiler *c, stmt_ty s)
1445{
1446 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001447 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001448 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001449 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001450 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001451 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001452 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001453 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454
1455 assert(s->kind == FunctionDef_kind);
1456
1457 if (!compiler_decorators(c, decos))
1458 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001459 if (args->kwonlyargs) {
1460 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1461 args->kw_defaults);
1462 if (res < 0)
1463 return 0;
1464 kw_default_count = res;
1465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466 if (args->defaults)
1467 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001468 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001469 if (num_annotations < 0)
1470 return 0;
1471 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001473 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1474 s->lineno))
1475 return 0;
1476
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001477 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001478 docstring = compiler_isdocstring(st);
Thomas Woutersce272b62007-09-19 21:19:28 +00001479 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001480 first_const = st->v.Expr.value->v.Str.s;
1481 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001482 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001483 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001484 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001487 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001489 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001491 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1492 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 }
1494 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001495 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001496 if (co == NULL)
1497 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Guido van Rossum4f72a782006-10-27 23:31:49 +00001499 arglength = asdl_seq_LEN(args->defaults);
1500 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001501 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001502 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001503 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
Neal Norwitzc1505362006-12-28 06:47:50 +00001505 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1507 ADDOP_I(c, CALL_FUNCTION, 1);
1508 }
1509
1510 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1511}
1512
1513static int
1514compiler_class(struct compiler *c, stmt_ty s)
1515{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001517 PyObject *str;
Benjamin Petersondcaf3292009-03-03 00:54:05 +00001518 int i;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001519 asdl_seq* decos = s->v.ClassDef.decorator_list;
1520
1521 if (!compiler_decorators(c, decos))
1522 return 0;
1523
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001524 /* ultimately generate code for:
1525 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1526 where:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001527 <func> is a function/closure created from the class body;
1528 it has a single argument (__locals__) where the dict
1529 (or MutableSequence) representing the locals is passed
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001530 <name> is the class name
1531 <bases> is the positional arguments and *varargs argument
1532 <keywords> is the keyword arguments and **kwds argument
1533 This borrows from compiler_call.
1534 */
1535
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001536 /* 1. compile the class body into a code object */
1537 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1538 return 0;
1539 /* this block represents what we do in the new scope */
1540 {
1541 /* use the class name for name mangling */
1542 Py_INCREF(s->v.ClassDef.name);
Neal Norwitz36e63102008-04-01 08:08:09 +00001543 Py_XDECREF(c->u->u_private);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001544 c->u->u_private = s->v.ClassDef.name;
1545 /* force it to have one mandatory argument */
1546 c->u->u_argcount = 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001547 /* load the first argument (__locals__) ... */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001548 ADDOP_I(c, LOAD_FAST, 0);
1549 /* ... and store it into f_locals */
1550 ADDOP_IN_SCOPE(c, STORE_LOCALS);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001551 /* load (global) __name__ ... */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001552 str = PyUnicode_InternFromString("__name__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001553 if (!str || !compiler_nameop(c, str, Load)) {
1554 Py_XDECREF(str);
1555 compiler_exit_scope(c);
1556 return 0;
1557 }
1558 Py_DECREF(str);
1559 /* ... and store it as __module__ */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001560 str = PyUnicode_InternFromString("__module__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001561 if (!str || !compiler_nameop(c, str, Store)) {
1562 Py_XDECREF(str);
1563 compiler_exit_scope(c);
1564 return 0;
1565 }
1566 Py_DECREF(str);
1567 /* compile the body proper */
1568 if (!compiler_body(c, s->v.ClassDef.body)) {
1569 compiler_exit_scope(c);
1570 return 0;
1571 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001572 /* return the (empty) __class__ cell */
1573 str = PyUnicode_InternFromString("__class__");
1574 if (str == NULL) {
1575 compiler_exit_scope(c);
1576 return 0;
1577 }
1578 i = compiler_lookup_arg(c->u->u_cellvars, str);
1579 Py_DECREF(str);
1580 if (i == -1) {
1581 /* This happens when nobody references the cell */
1582 PyErr_Clear();
1583 /* Return None */
1584 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1585 }
1586 else {
1587 /* Return the cell where to store __class__ */
1588 ADDOP_I(c, LOAD_CLOSURE, i);
1589 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001590 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1591 /* create the code object */
1592 co = assemble(c, 1);
1593 }
1594 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001595 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596 if (co == NULL)
1597 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001599 /* 2. load the 'build_class' function */
1600 ADDOP(c, LOAD_BUILD_CLASS);
1601
1602 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001603 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001604 Py_DECREF(co);
1605
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001606 /* 4. load class name */
1607 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1608
1609 /* 5. generate the rest of the code for the call */
1610 if (!compiler_call_helper(c, 2,
1611 s->v.ClassDef.bases,
1612 s->v.ClassDef.keywords,
1613 s->v.ClassDef.starargs,
1614 s->v.ClassDef.kwargs))
1615 return 0;
1616
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001617 /* 6. apply decorators */
1618 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1619 ADDOP_I(c, CALL_FUNCTION, 1);
1620 }
1621
1622 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1624 return 0;
1625 return 1;
1626}
1627
1628static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001629compiler_ifexp(struct compiler *c, expr_ty e)
1630{
1631 basicblock *end, *next;
1632
1633 assert(e->kind == IfExp_kind);
1634 end = compiler_new_block(c);
1635 if (end == NULL)
1636 return 0;
1637 next = compiler_new_block(c);
1638 if (next == NULL)
1639 return 0;
1640 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001641 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001642 VISIT(c, expr, e->v.IfExp.body);
1643 ADDOP_JREL(c, JUMP_FORWARD, end);
1644 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001645 VISIT(c, expr, e->v.IfExp.orelse);
1646 compiler_use_next_block(c, end);
1647 return 1;
1648}
1649
1650static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651compiler_lambda(struct compiler *c, expr_ty e)
1652{
1653 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001654 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001655 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001656 arguments_ty args = e->v.Lambda.args;
1657 assert(e->kind == Lambda_kind);
1658
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001659 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001660 name = PyUnicode_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001661 if (!name)
1662 return 0;
1663 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664
Guido van Rossum4f72a782006-10-27 23:31:49 +00001665 if (args->kwonlyargs) {
1666 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1667 args->kw_defaults);
1668 if (res < 0) return 0;
1669 kw_default_count = res;
1670 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001671 if (args->defaults)
1672 VISIT_SEQ(c, expr, args->defaults);
1673 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1674 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001675
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001677 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001678 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson371ccfb2008-12-27 19:03:36 +00001679 if (c->u->u_ste->ste_generator) {
1680 ADDOP_IN_SCOPE(c, POP_TOP);
1681 }
1682 else {
1683 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1684 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001686 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687 if (co == NULL)
1688 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689
Guido van Rossum4f72a782006-10-27 23:31:49 +00001690 arglength = asdl_seq_LEN(args->defaults);
1691 arglength |= kw_default_count << 8;
1692 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001693 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694
1695 return 1;
1696}
1697
1698static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699compiler_if(struct compiler *c, stmt_ty s)
1700{
1701 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001702 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703 assert(s->kind == If_kind);
1704 end = compiler_new_block(c);
1705 if (end == NULL)
1706 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001707
1708 constant = expr_constant(s->v.If.test);
1709 /* constant = 0: "if 0"
1710 * constant = 1: "if 1", "if 2", ...
1711 * constant = -1: rest */
1712 if (constant == 0) {
1713 if (s->v.If.orelse)
1714 VISIT_SEQ(c, stmt, s->v.If.orelse);
1715 } else if (constant == 1) {
1716 VISIT_SEQ(c, stmt, s->v.If.body);
1717 } else {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001718 if (s->v.If.orelse) {
1719 next = compiler_new_block(c);
1720 if (next == NULL)
1721 return 0;
1722 }
1723 else
1724 next = end;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001725 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001726 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001727 VISIT_SEQ(c, stmt, s->v.If.body);
1728 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001729 if (s->v.If.orelse) {
1730 compiler_use_next_block(c, next);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001731 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001732 }
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001734 compiler_use_next_block(c, end);
1735 return 1;
1736}
1737
1738static int
1739compiler_for(struct compiler *c, stmt_ty s)
1740{
1741 basicblock *start, *cleanup, *end;
1742
1743 start = compiler_new_block(c);
1744 cleanup = compiler_new_block(c);
1745 end = compiler_new_block(c);
1746 if (start == NULL || end == NULL || cleanup == NULL)
1747 return 0;
1748 ADDOP_JREL(c, SETUP_LOOP, end);
1749 if (!compiler_push_fblock(c, LOOP, start))
1750 return 0;
1751 VISIT(c, expr, s->v.For.iter);
1752 ADDOP(c, GET_ITER);
1753 compiler_use_next_block(c, start);
1754 ADDOP_JREL(c, FOR_ITER, cleanup);
1755 VISIT(c, expr, s->v.For.target);
1756 VISIT_SEQ(c, stmt, s->v.For.body);
1757 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1758 compiler_use_next_block(c, cleanup);
1759 ADDOP(c, POP_BLOCK);
1760 compiler_pop_fblock(c, LOOP, start);
1761 VISIT_SEQ(c, stmt, s->v.For.orelse);
1762 compiler_use_next_block(c, end);
1763 return 1;
1764}
1765
1766static int
1767compiler_while(struct compiler *c, stmt_ty s)
1768{
1769 basicblock *loop, *orelse, *end, *anchor = NULL;
1770 int constant = expr_constant(s->v.While.test);
1771
Christian Heimes969fe572008-01-25 11:23:10 +00001772 if (constant == 0) {
1773 if (s->v.While.orelse)
1774 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775 return 1;
Christian Heimes969fe572008-01-25 11:23:10 +00001776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777 loop = compiler_new_block(c);
1778 end = compiler_new_block(c);
1779 if (constant == -1) {
1780 anchor = compiler_new_block(c);
1781 if (anchor == NULL)
1782 return 0;
1783 }
1784 if (loop == NULL || end == NULL)
1785 return 0;
1786 if (s->v.While.orelse) {
1787 orelse = compiler_new_block(c);
1788 if (orelse == NULL)
1789 return 0;
1790 }
1791 else
1792 orelse = NULL;
1793
1794 ADDOP_JREL(c, SETUP_LOOP, end);
1795 compiler_use_next_block(c, loop);
1796 if (!compiler_push_fblock(c, LOOP, loop))
1797 return 0;
1798 if (constant == -1) {
1799 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001800 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001801 }
1802 VISIT_SEQ(c, stmt, s->v.While.body);
1803 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1804
1805 /* XXX should the two POP instructions be in a separate block
1806 if there is no else clause ?
1807 */
1808
1809 if (constant == -1) {
1810 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811 ADDOP(c, POP_BLOCK);
1812 }
1813 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001814 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001815 VISIT_SEQ(c, stmt, s->v.While.orelse);
1816 compiler_use_next_block(c, end);
1817
1818 return 1;
1819}
1820
1821static int
1822compiler_continue(struct compiler *c)
1823{
1824 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001825 static const char IN_FINALLY_ERROR_MSG[] =
1826 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001827 int i;
1828
1829 if (!c->u->u_nfblocks)
1830 return compiler_error(c, LOOP_ERROR_MSG);
1831 i = c->u->u_nfblocks - 1;
1832 switch (c->u->u_fblock[i].fb_type) {
1833 case LOOP:
1834 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1835 break;
1836 case EXCEPT:
1837 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001838 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1839 /* Prevent continue anywhere under a finally
1840 even if hidden in a sub-try or except. */
1841 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1842 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001844 if (i == -1)
1845 return compiler_error(c, LOOP_ERROR_MSG);
1846 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1847 break;
1848 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001849 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850 }
1851
1852 return 1;
1853}
1854
1855/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1856
1857 SETUP_FINALLY L
1858 <code for body>
1859 POP_BLOCK
1860 LOAD_CONST <None>
1861 L: <code for finalbody>
1862 END_FINALLY
1863
1864 The special instructions use the block stack. Each block
1865 stack entry contains the instruction that created it (here
1866 SETUP_FINALLY), the level of the value stack at the time the
1867 block stack entry was created, and a label (here L).
1868
1869 SETUP_FINALLY:
1870 Pushes the current value stack level and the label
1871 onto the block stack.
1872 POP_BLOCK:
1873 Pops en entry from the block stack, and pops the value
1874 stack until its level is the same as indicated on the
1875 block stack. (The label is ignored.)
1876 END_FINALLY:
1877 Pops a variable number of entries from the *value* stack
1878 and re-raises the exception they specify. The number of
1879 entries popped depends on the (pseudo) exception type.
1880
1881 The block stack is unwound when an exception is raised:
1882 when a SETUP_FINALLY entry is found, the exception is pushed
1883 onto the value stack (and the exception condition is cleared),
1884 and the interpreter jumps to the label gotten from the block
1885 stack.
1886*/
1887
1888static int
1889compiler_try_finally(struct compiler *c, stmt_ty s)
1890{
1891 basicblock *body, *end;
1892 body = compiler_new_block(c);
1893 end = compiler_new_block(c);
1894 if (body == NULL || end == NULL)
1895 return 0;
1896
1897 ADDOP_JREL(c, SETUP_FINALLY, end);
1898 compiler_use_next_block(c, body);
1899 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1900 return 0;
1901 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1902 ADDOP(c, POP_BLOCK);
1903 compiler_pop_fblock(c, FINALLY_TRY, body);
1904
1905 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1906 compiler_use_next_block(c, end);
1907 if (!compiler_push_fblock(c, FINALLY_END, end))
1908 return 0;
1909 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1910 ADDOP(c, END_FINALLY);
1911 compiler_pop_fblock(c, FINALLY_END, end);
1912
1913 return 1;
1914}
1915
1916/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001917 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 (The contents of the value stack is shown in [], with the top
1919 at the right; 'tb' is trace-back info, 'val' the exception's
1920 associated value, and 'exc' the exception.)
1921
1922 Value stack Label Instruction Argument
1923 [] SETUP_EXCEPT L1
1924 [] <code for S>
1925 [] POP_BLOCK
1926 [] JUMP_FORWARD L0
1927
1928 [tb, val, exc] L1: DUP )
1929 [tb, val, exc, exc] <evaluate E1> )
1930 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001931 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001932 [tb, val, exc] POP
1933 [tb, val] <assign to V1> (or POP if no V1)
1934 [tb] POP
1935 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001936 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001938 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 .............................etc.......................
1940
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001941 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942
1943 [] L0: <next statement>
1944
1945 Of course, parts are not generated if Vi or Ei is not present.
1946*/
1947static int
1948compiler_try_except(struct compiler *c, stmt_ty s)
1949{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001950 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 int i, n;
1952
1953 body = compiler_new_block(c);
1954 except = compiler_new_block(c);
1955 orelse = compiler_new_block(c);
1956 end = compiler_new_block(c);
1957 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1958 return 0;
1959 ADDOP_JREL(c, SETUP_EXCEPT, except);
1960 compiler_use_next_block(c, body);
1961 if (!compiler_push_fblock(c, EXCEPT, body))
1962 return 0;
1963 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1964 ADDOP(c, POP_BLOCK);
1965 compiler_pop_fblock(c, EXCEPT, body);
1966 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1967 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1968 compiler_use_next_block(c, except);
1969 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001970 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971 s->v.TryExcept.handlers, i);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001972 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001973 return compiler_error(c, "default 'except:' must be last");
Christian Heimes2202f872008-02-06 14:31:34 +00001974 c->u->u_lineno_set = 0;
1975 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 except = compiler_new_block(c);
1977 if (except == NULL)
1978 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001979 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001980 ADDOP(c, DUP_TOP);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001981 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001982 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001983 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001984 }
1985 ADDOP(c, POP_TOP);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001986 if (handler->v.ExceptHandler.name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001987 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001988
1989 cleanup_end = compiler_new_block(c);
1990 cleanup_body = compiler_new_block(c);
1991 if(!(cleanup_end || cleanup_body))
1992 return 0;
1993
Neal Norwitzad74aa82008-03-31 05:14:30 +00001994 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001995 ADDOP(c, POP_TOP);
1996
1997 /*
1998 try:
1999 # body
2000 except type as name:
2001 try:
2002 # body
2003 finally:
2004 name = None
2005 del name
2006 */
2007
2008 /* second try: */
2009 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2010 compiler_use_next_block(c, cleanup_body);
2011 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2012 return 0;
2013
2014 /* second # body */
Neal Norwitzad74aa82008-03-31 05:14:30 +00002015 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Guido van Rossumb940e112007-01-10 16:19:56 +00002016 ADDOP(c, POP_BLOCK);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002017 ADDOP(c, POP_EXCEPT);
Guido van Rossumb940e112007-01-10 16:19:56 +00002018 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2019
2020 /* finally: */
2021 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2022 compiler_use_next_block(c, cleanup_end);
2023 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2024 return 0;
2025
2026 /* name = None */
2027 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Neal Norwitzad74aa82008-03-31 05:14:30 +00002028 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002029
Guido van Rossum16be03e2007-01-10 18:51:35 +00002030 /* del name */
Neal Norwitzad74aa82008-03-31 05:14:30 +00002031 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002032
2033 ADDOP(c, END_FINALLY);
2034 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002035 }
2036 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002037 basicblock *cleanup_body;
2038
2039 cleanup_body = compiler_new_block(c);
2040 if(!cleanup_body)
2041 return 0;
2042
2043 ADDOP(c, POP_TOP);
Guido van Rossumb940e112007-01-10 16:19:56 +00002044 ADDOP(c, POP_TOP);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002045 compiler_use_next_block(c, cleanup_body);
2046 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2047 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002048 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002049 ADDOP(c, POP_EXCEPT);
2050 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002051 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002052 ADDOP_JREL(c, JUMP_FORWARD, end);
2053 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 }
2055 ADDOP(c, END_FINALLY);
2056 compiler_use_next_block(c, orelse);
2057 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2058 compiler_use_next_block(c, end);
2059 return 1;
2060}
2061
2062static int
2063compiler_import_as(struct compiler *c, identifier name, identifier asname)
2064{
2065 /* The IMPORT_NAME opcode was already generated. This function
2066 merely needs to bind the result to a name.
2067
2068 If there is a dot in name, we need to split it and emit a
2069 LOAD_ATTR for each name.
2070 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002071 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2072 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 if (dot) {
2074 /* Consume the base module name to get the first attribute */
2075 src = dot + 1;
2076 while (dot) {
2077 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002078 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002079 dot = Py_UNICODE_strchr(src, '.');
2080 attr = PyUnicode_FromUnicode(src,
2081 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002082 if (!attr)
2083 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002085 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002086 src = dot + 1;
2087 }
2088 }
2089 return compiler_nameop(c, asname, Store);
2090}
2091
2092static int
2093compiler_import(struct compiler *c, stmt_ty s)
2094{
2095 /* The Import node stores a module name like a.b.c as a single
2096 string. This is convenient for all cases except
2097 import a.b.c as d
2098 where we need to parse that string to extract the individual
2099 module names.
2100 XXX Perhaps change the representation to make this case simpler?
2101 */
2102 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002104 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002105 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002106 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002107 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002108
Christian Heimes217cfd12007-12-02 14:31:20 +00002109 level = PyLong_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002110 if (level == NULL)
2111 return 0;
2112
2113 ADDOP_O(c, LOAD_CONST, level, consts);
2114 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002115 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2116 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2117
2118 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002119 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002120 if (!r)
2121 return r;
2122 }
2123 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002124 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002125 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2126 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002128 tmp = PyUnicode_FromUnicode(base,
2129 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 r = compiler_nameop(c, tmp, Store);
2131 if (dot) {
2132 Py_DECREF(tmp);
2133 }
2134 if (!r)
2135 return r;
2136 }
2137 }
2138 return 1;
2139}
2140
2141static int
2142compiler_from_import(struct compiler *c, stmt_ty s)
2143{
2144 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145
2146 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002147 PyObject *level;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002148 static PyObject *empty_string;
2149
2150 if (!empty_string) {
2151 empty_string = PyUnicode_FromString("");
2152 if (!empty_string)
2153 return 0;
2154 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156 if (!names)
2157 return 0;
2158
Christian Heimes217cfd12007-12-02 14:31:20 +00002159 level = PyLong_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002160 if (!level) {
2161 Py_DECREF(names);
2162 return 0;
2163 }
2164
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165 /* build up the names */
2166 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002167 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 Py_INCREF(alias->name);
2169 PyTuple_SET_ITEM(names, i, alias->name);
2170 }
2171
Benjamin Peterson78565b22009-06-28 19:19:51 +00002172 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2173 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2174 Py_DECREF(level);
2175 Py_DECREF(names);
2176 return compiler_error(c, "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002177 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002178 }
2179
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002180 ADDOP_O(c, LOAD_CONST, level, consts);
2181 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002182 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002183 Py_DECREF(names);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002184 if (s->v.ImportFrom.module) {
2185 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2186 }
2187 else {
2188 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2189 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002190 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002191 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 identifier store_name;
2193
Martin v. Löwis5b222132007-06-10 09:51:05 +00002194 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 assert(n == 1);
2196 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002197 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 }
2199
2200 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2201 store_name = alias->name;
2202 if (alias->asname)
2203 store_name = alias->asname;
2204
2205 if (!compiler_nameop(c, store_name, Store)) {
2206 Py_DECREF(names);
2207 return 0;
2208 }
2209 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002210 /* remove imported module */
2211 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002212 return 1;
2213}
2214
2215static int
2216compiler_assert(struct compiler *c, stmt_ty s)
2217{
2218 static PyObject *assertion_error = NULL;
2219 basicblock *end;
2220
2221 if (Py_OptimizeFlag)
2222 return 1;
2223 if (assertion_error == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +00002224 assertion_error = PyUnicode_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002225 if (assertion_error == NULL)
2226 return 0;
2227 }
Christian Heimes08976cb2008-03-16 00:32:36 +00002228 if (s->v.Assert.test->kind == Tuple_kind &&
2229 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2230 const char* msg =
2231 "assertion is always true, perhaps remove parentheses?";
2232 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2233 c->u->u_lineno, NULL, NULL) == -1)
2234 return 0;
2235 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 VISIT(c, expr, s->v.Assert.test);
2237 end = compiler_new_block(c);
2238 if (end == NULL)
2239 return 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002240 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2242 if (s->v.Assert.msg) {
2243 VISIT(c, expr, s->v.Assert.msg);
Collin Winter828f04a2007-08-31 00:04:24 +00002244 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002245 }
Collin Winter828f04a2007-08-31 00:04:24 +00002246 ADDOP_I(c, RAISE_VARARGS, 1);
Neal Norwitz51abbc72005-12-18 07:06:23 +00002247 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 return 1;
2249}
2250
2251static int
2252compiler_visit_stmt(struct compiler *c, stmt_ty s)
2253{
2254 int i, n;
2255
Thomas Wouters89f507f2006-12-13 04:49:30 +00002256 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002257 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002258 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002259
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002261 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002263 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002264 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002265 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266 if (c->u->u_ste->ste_type != FunctionBlock)
2267 return compiler_error(c, "'return' outside function");
2268 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 VISIT(c, expr, s->v.Return.value);
2270 }
2271 else
2272 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2273 ADDOP(c, RETURN_VALUE);
2274 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002275 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276 VISIT_SEQ(c, expr, s->v.Delete.targets)
2277 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002278 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 n = asdl_seq_LEN(s->v.Assign.targets);
2280 VISIT(c, expr, s->v.Assign.value);
2281 for (i = 0; i < n; i++) {
2282 if (i < n - 1)
2283 ADDOP(c, DUP_TOP);
2284 VISIT(c, expr,
2285 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2286 }
2287 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002288 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002290 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002291 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002292 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002294 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002295 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002296 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002297 n = 0;
Collin Winter828f04a2007-08-31 00:04:24 +00002298 if (s->v.Raise.exc) {
2299 VISIT(c, expr, s->v.Raise.exc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 n++;
Collin Winter828f04a2007-08-31 00:04:24 +00002301 if (s->v.Raise.cause) {
2302 VISIT(c, expr, s->v.Raise.cause);
2303 n++;
2304 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 }
2306 ADDOP_I(c, RAISE_VARARGS, n);
2307 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002308 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002309 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002310 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002312 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002313 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002314 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002316 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002317 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002318 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002319 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002321 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002323 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 ADDOP(c, PRINT_EXPR);
2325 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002326 else if (s->v.Expr.value->kind != Str_kind &&
2327 s->v.Expr.value->kind != Num_kind) {
2328 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002329 ADDOP(c, POP_TOP);
2330 }
2331 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002332 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002334 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002335 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 return compiler_error(c, "'break' outside loop");
2337 ADDOP(c, BREAK_LOOP);
2338 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002339 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002341 case With_kind:
2342 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 }
2344 return 1;
2345}
2346
2347static int
2348unaryop(unaryop_ty op)
2349{
2350 switch (op) {
2351 case Invert:
2352 return UNARY_INVERT;
2353 case Not:
2354 return UNARY_NOT;
2355 case UAdd:
2356 return UNARY_POSITIVE;
2357 case USub:
2358 return UNARY_NEGATIVE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002359 default:
2360 PyErr_Format(PyExc_SystemError,
2361 "unary op %d should not be possible", op);
2362 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002363 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002364}
2365
2366static int
2367binop(struct compiler *c, operator_ty op)
2368{
2369 switch (op) {
2370 case Add:
2371 return BINARY_ADD;
2372 case Sub:
2373 return BINARY_SUBTRACT;
2374 case Mult:
2375 return BINARY_MULTIPLY;
2376 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002377 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002378 case Mod:
2379 return BINARY_MODULO;
2380 case Pow:
2381 return BINARY_POWER;
2382 case LShift:
2383 return BINARY_LSHIFT;
2384 case RShift:
2385 return BINARY_RSHIFT;
2386 case BitOr:
2387 return BINARY_OR;
2388 case BitXor:
2389 return BINARY_XOR;
2390 case BitAnd:
2391 return BINARY_AND;
2392 case FloorDiv:
2393 return BINARY_FLOOR_DIVIDE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002394 default:
2395 PyErr_Format(PyExc_SystemError,
2396 "binary op %d should not be possible", op);
2397 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002398 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399}
2400
2401static int
2402cmpop(cmpop_ty op)
2403{
2404 switch (op) {
2405 case Eq:
2406 return PyCmp_EQ;
2407 case NotEq:
2408 return PyCmp_NE;
2409 case Lt:
2410 return PyCmp_LT;
2411 case LtE:
2412 return PyCmp_LE;
2413 case Gt:
2414 return PyCmp_GT;
2415 case GtE:
2416 return PyCmp_GE;
2417 case Is:
2418 return PyCmp_IS;
2419 case IsNot:
2420 return PyCmp_IS_NOT;
2421 case In:
2422 return PyCmp_IN;
2423 case NotIn:
2424 return PyCmp_NOT_IN;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002425 default:
2426 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
2429
2430static int
2431inplace_binop(struct compiler *c, operator_ty op)
2432{
2433 switch (op) {
2434 case Add:
2435 return INPLACE_ADD;
2436 case Sub:
2437 return INPLACE_SUBTRACT;
2438 case Mult:
2439 return INPLACE_MULTIPLY;
2440 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002441 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002442 case Mod:
2443 return INPLACE_MODULO;
2444 case Pow:
2445 return INPLACE_POWER;
2446 case LShift:
2447 return INPLACE_LSHIFT;
2448 case RShift:
2449 return INPLACE_RSHIFT;
2450 case BitOr:
2451 return INPLACE_OR;
2452 case BitXor:
2453 return INPLACE_XOR;
2454 case BitAnd:
2455 return INPLACE_AND;
2456 case FloorDiv:
2457 return INPLACE_FLOOR_DIVIDE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002458 default:
2459 PyErr_Format(PyExc_SystemError,
2460 "inplace binary op %d should not be possible", op);
2461 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002463}
2464
2465static int
2466compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2467{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002468 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002469 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2470
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002471 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002472 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 /* XXX AugStore isn't used anywhere! */
2474
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002475 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002476 if (!mangled)
2477 return 0;
2478
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479 op = 0;
2480 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002481 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 switch (scope) {
2483 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002484 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 optype = OP_DEREF;
2486 break;
2487 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002488 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489 optype = OP_DEREF;
2490 break;
2491 case LOCAL:
2492 if (c->u->u_ste->ste_type == FunctionBlock)
2493 optype = OP_FAST;
2494 break;
2495 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002496 if (c->u->u_ste->ste_type == FunctionBlock &&
2497 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498 optype = OP_GLOBAL;
2499 break;
2500 case GLOBAL_EXPLICIT:
2501 optype = OP_GLOBAL;
2502 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002503 default:
2504 /* scope can be 0 */
2505 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002506 }
2507
2508 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002509 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002510
2511 switch (optype) {
2512 case OP_DEREF:
2513 switch (ctx) {
2514 case Load: op = LOAD_DEREF; break;
2515 case Store: op = STORE_DEREF; break;
2516 case AugLoad:
2517 case AugStore:
2518 break;
2519 case Del:
2520 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002521 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002523 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002524 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002527 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002528 PyErr_SetString(PyExc_SystemError,
2529 "param invalid for deref variable");
2530 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002531 }
2532 break;
2533 case OP_FAST:
2534 switch (ctx) {
2535 case Load: op = LOAD_FAST; break;
2536 case Store: op = STORE_FAST; break;
2537 case Del: op = DELETE_FAST; break;
2538 case AugLoad:
2539 case AugStore:
2540 break;
2541 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002542 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002543 PyErr_SetString(PyExc_SystemError,
2544 "param invalid for local variable");
2545 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002546 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002547 ADDOP_O(c, op, mangled, varnames);
2548 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 return 1;
2550 case OP_GLOBAL:
2551 switch (ctx) {
2552 case Load: op = LOAD_GLOBAL; break;
2553 case Store: op = STORE_GLOBAL; break;
2554 case Del: op = DELETE_GLOBAL; break;
2555 case AugLoad:
2556 case AugStore:
2557 break;
2558 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002559 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002560 PyErr_SetString(PyExc_SystemError,
2561 "param invalid for global variable");
2562 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002563 }
2564 break;
2565 case OP_NAME:
2566 switch (ctx) {
2567 case Load: op = LOAD_NAME; break;
2568 case Store: op = STORE_NAME; break;
2569 case Del: op = DELETE_NAME; break;
2570 case AugLoad:
2571 case AugStore:
2572 break;
2573 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002574 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002575 PyErr_SetString(PyExc_SystemError,
2576 "param invalid for name variable");
2577 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002578 }
2579 break;
2580 }
2581
2582 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002583 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002584 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002585 if (arg < 0)
2586 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002587 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002588}
2589
2590static int
2591compiler_boolop(struct compiler *c, expr_ty e)
2592{
2593 basicblock *end;
2594 int jumpi, i, n;
2595 asdl_seq *s;
2596
2597 assert(e->kind == BoolOp_kind);
2598 if (e->v.BoolOp.op == And)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002599 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002600 else
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002601 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002602 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002603 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604 return 0;
2605 s = e->v.BoolOp.values;
2606 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002607 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002608 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002609 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002610 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002612 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 compiler_use_next_block(c, end);
2614 return 1;
2615}
2616
2617static int
2618compiler_list(struct compiler *c, expr_ty e)
2619{
2620 int n = asdl_seq_LEN(e->v.List.elts);
2621 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002622 int i, seen_star = 0;
2623 for (i = 0; i < n; i++) {
2624 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2625 if (elt->kind == Starred_kind && !seen_star) {
Thomas Woutersdeef6742008-03-14 17:16:59 +00002626 if ((i >= (1 << 8)) ||
2627 (n-i-1 >= (INT_MAX >> 8)))
2628 return compiler_error(c,
2629 "too many expressions in "
2630 "star-unpacking assignment");
Guido van Rossum0368b722007-05-11 16:50:42 +00002631 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2632 seen_star = 1;
2633 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2634 } else if (elt->kind == Starred_kind) {
2635 return compiler_error(c,
2636 "two starred expressions in assignment");
2637 }
2638 }
2639 if (!seen_star) {
2640 ADDOP_I(c, UNPACK_SEQUENCE, n);
2641 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002642 }
2643 VISIT_SEQ(c, expr, e->v.List.elts);
2644 if (e->v.List.ctx == Load) {
2645 ADDOP_I(c, BUILD_LIST, n);
2646 }
2647 return 1;
2648}
2649
2650static int
2651compiler_tuple(struct compiler *c, expr_ty e)
2652{
2653 int n = asdl_seq_LEN(e->v.Tuple.elts);
2654 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002655 int i, seen_star = 0;
2656 for (i = 0; i < n; i++) {
2657 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2658 if (elt->kind == Starred_kind && !seen_star) {
Thomas Woutersdeef6742008-03-14 17:16:59 +00002659 if ((i >= (1 << 8)) ||
2660 (n-i-1 >= (INT_MAX >> 8)))
2661 return compiler_error(c,
2662 "too many expressions in "
2663 "star-unpacking assignment");
Guido van Rossum0368b722007-05-11 16:50:42 +00002664 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2665 seen_star = 1;
2666 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2667 } else if (elt->kind == Starred_kind) {
2668 return compiler_error(c,
2669 "two starred expressions in assignment");
2670 }
2671 }
2672 if (!seen_star) {
2673 ADDOP_I(c, UNPACK_SEQUENCE, n);
2674 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002675 }
2676 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2677 if (e->v.Tuple.ctx == Load) {
2678 ADDOP_I(c, BUILD_TUPLE, n);
2679 }
2680 return 1;
2681}
2682
2683static int
2684compiler_compare(struct compiler *c, expr_ty e)
2685{
2686 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002687 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002688
2689 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2690 VISIT(c, expr, e->v.Compare.left);
2691 n = asdl_seq_LEN(e->v.Compare.ops);
2692 assert(n > 0);
2693 if (n > 1) {
2694 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002695 if (cleanup == NULL)
2696 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002697 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002698 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699 }
2700 for (i = 1; i < n; i++) {
2701 ADDOP(c, DUP_TOP);
2702 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002703 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002704 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002705 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002706 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002707 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002708 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002709 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002710 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002712 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002713 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002714 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002715 if (n > 1) {
2716 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002717 if (end == NULL)
2718 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 ADDOP_JREL(c, JUMP_FORWARD, end);
2720 compiler_use_next_block(c, cleanup);
2721 ADDOP(c, ROT_TWO);
2722 ADDOP(c, POP_TOP);
2723 compiler_use_next_block(c, end);
2724 }
2725 return 1;
2726}
2727
2728static int
2729compiler_call(struct compiler *c, expr_ty e)
2730{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002731 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002732 return compiler_call_helper(c, 0,
2733 e->v.Call.args,
2734 e->v.Call.keywords,
2735 e->v.Call.starargs,
2736 e->v.Call.kwargs);
2737}
2738
2739/* shared code between compiler_call and compiler_class */
2740static int
2741compiler_call_helper(struct compiler *c,
2742 int n, /* Args already pushed */
2743 asdl_seq *args,
2744 asdl_seq *keywords,
2745 expr_ty starargs,
2746 expr_ty kwargs)
2747{
2748 int code = 0;
2749
2750 n += asdl_seq_LEN(args);
2751 VISIT_SEQ(c, expr, args);
2752 if (keywords) {
2753 VISIT_SEQ(c, keyword, keywords);
2754 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002755 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002756 if (starargs) {
2757 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 code |= 1;
2759 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002760 if (kwargs) {
2761 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002762 code |= 2;
2763 }
2764 switch (code) {
2765 case 0:
2766 ADDOP_I(c, CALL_FUNCTION, n);
2767 break;
2768 case 1:
2769 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2770 break;
2771 case 2:
2772 ADDOP_I(c, CALL_FUNCTION_KW, n);
2773 break;
2774 case 3:
2775 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2776 break;
2777 }
2778 return 1;
2779}
2780
Nick Coghlan650f0d02007-04-15 12:05:43 +00002781
2782/* List and set comprehensions and generator expressions work by creating a
2783 nested function to perform the actual iteration. This means that the
2784 iteration variables don't leak into the current scope.
2785 The defined function is called immediately following its definition, with the
2786 result of that call being the result of the expression.
2787 The LC/SC version returns the populated container, while the GE version is
2788 flagged in symtable.c as a generator, so it returns the generator object
2789 when the function is called.
2790 This code *knows* that the loop cannot contain break, continue, or return,
2791 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2792
2793 Possible cleanups:
2794 - iterate over the generator sequence instead of using recursion
2795*/
2796
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797static int
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002798compiler_comprehension_generator(struct compiler *c,
Nick Coghlan650f0d02007-04-15 12:05:43 +00002799 asdl_seq *generators, int gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002800 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801{
2802 /* generate code for the iterator, then each of the ifs,
2803 and then write to the element */
2804
Nick Coghlan650f0d02007-04-15 12:05:43 +00002805 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002806 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002807 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808
2809 start = compiler_new_block(c);
2810 skip = compiler_new_block(c);
2811 if_cleanup = compiler_new_block(c);
2812 anchor = compiler_new_block(c);
2813
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002814 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002815 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002816 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817
Nick Coghlan650f0d02007-04-15 12:05:43 +00002818 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820 if (gen_index == 0) {
2821 /* Receive outermost iter as an implicit argument */
2822 c->u->u_argcount = 1;
2823 ADDOP_I(c, LOAD_FAST, 0);
2824 }
2825 else {
2826 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002827 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828 ADDOP(c, GET_ITER);
2829 }
2830 compiler_use_next_block(c, start);
2831 ADDOP_JREL(c, FOR_ITER, anchor);
2832 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002833 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002835 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002836 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002838 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 VISIT(c, expr, e);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002840 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841 NEXT_BLOCK(c);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002843
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002844 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002845 if (!compiler_comprehension_generator(c,
Nick Coghlan650f0d02007-04-15 12:05:43 +00002846 generators, gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002847 elt, val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002848 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002849
Nick Coghlan650f0d02007-04-15 12:05:43 +00002850 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002851 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002852 /* comprehension specific code */
2853 switch (type) {
2854 case COMP_GENEXP:
2855 VISIT(c, expr, elt);
2856 ADDOP(c, YIELD_VALUE);
2857 ADDOP(c, POP_TOP);
2858 break;
2859 case COMP_LISTCOMP:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002860 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002861 ADDOP_I(c, LIST_APPEND, gen_index + 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002862 break;
2863 case COMP_SETCOMP:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002864 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002865 ADDOP_I(c, SET_ADD, gen_index + 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002866 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002867 case COMP_DICTCOMP:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002868 /* With 'd[k] = v', v is evaluated before k, so we do
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002869 the same. */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002870 VISIT(c, expr, val);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002871 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002872 ADDOP_I(c, MAP_ADD, gen_index + 1);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002873 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002874 default:
2875 return 0;
2876 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002877
2878 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002879 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002880 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2882 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883
2884 return 1;
2885}
2886
2887static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002888compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002889 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002890{
2891 PyCodeObject *co = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002892 expr_ty outermost_iter;
2893
2894 outermost_iter = ((comprehension_ty)
2895 asdl_seq_GET(generators, 0))->iter;
2896
2897 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2898 goto error;
2899
2900 if (type != COMP_GENEXP) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002901 int op;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002902 switch (type) {
2903 case COMP_LISTCOMP:
2904 op = BUILD_LIST;
2905 break;
2906 case COMP_SETCOMP:
2907 op = BUILD_SET;
2908 break;
2909 case COMP_DICTCOMP:
2910 op = BUILD_MAP;
2911 break;
2912 default:
2913 PyErr_Format(PyExc_SystemError,
2914 "unknown comprehension type %d", type);
2915 goto error_in_scope;
2916 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002917
Guido van Rossum992d4a32007-07-11 13:09:30 +00002918 ADDOP_I(c, op, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002919 }
2920
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002921 if (!compiler_comprehension_generator(c, generators, 0, elt,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002922 val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002923 goto error_in_scope;
2924
2925 if (type != COMP_GENEXP) {
2926 ADDOP(c, RETURN_VALUE);
2927 }
2928
2929 co = assemble(c, 1);
2930 compiler_exit_scope(c);
2931 if (co == NULL)
2932 goto error;
2933
2934 if (!compiler_make_closure(c, co, 0))
2935 goto error;
2936 Py_DECREF(co);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002937
2938 VISIT(c, expr, outermost_iter);
2939 ADDOP(c, GET_ITER);
2940 ADDOP_I(c, CALL_FUNCTION, 1);
2941 return 1;
2942error_in_scope:
2943 compiler_exit_scope(c);
2944error:
2945 Py_XDECREF(co);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002946 return 0;
2947}
2948
2949static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950compiler_genexp(struct compiler *c, expr_ty e)
2951{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002952 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002953 if (!name) {
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00002954 name = PyUnicode_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002955 if (!name)
2956 return 0;
2957 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002958 assert(e->kind == GeneratorExp_kind);
2959 return compiler_comprehension(c, e, COMP_GENEXP, name,
2960 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002961 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002962}
2963
2964static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002965compiler_listcomp(struct compiler *c, expr_ty e)
2966{
2967 static identifier name;
2968 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002969 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002970 if (!name)
2971 return 0;
2972 }
2973 assert(e->kind == ListComp_kind);
2974 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2975 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002976 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002977}
2978
2979static int
2980compiler_setcomp(struct compiler *c, expr_ty e)
2981{
2982 static identifier name;
2983 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002984 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002985 if (!name)
2986 return 0;
2987 }
2988 assert(e->kind == SetComp_kind);
2989 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2990 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002991 e->v.SetComp.elt, NULL);
2992}
2993
2994
2995static int
2996compiler_dictcomp(struct compiler *c, expr_ty e)
2997{
2998 static identifier name;
2999 if (!name) {
Neal Norwitz41103bf2007-08-24 23:12:06 +00003000 name = PyUnicode_FromString("<dictcomp>");
Guido van Rossum992d4a32007-07-11 13:09:30 +00003001 if (!name)
3002 return 0;
3003 }
3004 assert(e->kind == DictComp_kind);
3005 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3006 e->v.DictComp.generators,
3007 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003008}
3009
3010
3011static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003012compiler_visit_keyword(struct compiler *c, keyword_ty k)
3013{
3014 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3015 VISIT(c, expr, k->value);
3016 return 1;
3017}
3018
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003019/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020 whether they are true or false.
3021
3022 Return values: 1 for true, 0 for false, -1 for non-constant.
3023 */
3024
3025static int
3026expr_constant(expr_ty e)
3027{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003028 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003029 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00003030 case Ellipsis_kind:
3031 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 case Num_kind:
3033 return PyObject_IsTrue(e->v.Num.n);
3034 case Str_kind:
3035 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003036 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003037 /* optimize away names that can't be reassigned */
Christian Heimes72b710a2008-05-26 13:28:38 +00003038 id = PyBytes_AS_STRING(
Guido van Rossumbdbb3952007-06-14 00:28:01 +00003039 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003040 if (strcmp(id, "True") == 0) return 1;
3041 if (strcmp(id, "False") == 0) return 0;
3042 if (strcmp(id, "None") == 0) return 0;
3043 if (strcmp(id, "__debug__") == 0)
3044 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003045 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003046 default:
3047 return -1;
3048 }
3049}
3050
Guido van Rossumc2e20742006-02-27 22:32:47 +00003051/*
3052 Implements the with statement from PEP 343.
3053
3054 The semantics outlined in that PEP are as follows:
3055
3056 with EXPR as VAR:
3057 BLOCK
3058
3059 It is implemented roughly as:
3060
Thomas Wouters477c8d52006-05-27 19:21:47 +00003061 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003062 exit = context.__exit__ # not calling it
3063 value = context.__enter__()
3064 try:
3065 VAR = value # if VAR present in the syntax
3066 BLOCK
3067 finally:
3068 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003069 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003070 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003071 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003072 exit(*exc)
3073 */
3074static int
3075compiler_with(struct compiler *c, stmt_ty s)
3076{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003077 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003078
3079 assert(s->kind == With_kind);
3080
Guido van Rossumc2e20742006-02-27 22:32:47 +00003081 block = compiler_new_block(c);
3082 finally = compiler_new_block(c);
3083 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003084 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003085
Thomas Wouters477c8d52006-05-27 19:21:47 +00003086 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003087 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003088 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003090 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003091 compiler_use_next_block(c, block);
3092 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003093 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003094 }
3095
3096 if (s->v.With.optional_vars) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003097 VISIT(c, expr, s->v.With.optional_vars);
3098 }
3099 else {
3100 /* Discard result from context.__enter__() */
3101 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003102 }
3103
3104 /* BLOCK code */
3105 VISIT_SEQ(c, stmt, s->v.With.body);
3106
3107 /* End of try block; start the finally block */
3108 ADDOP(c, POP_BLOCK);
3109 compiler_pop_fblock(c, FINALLY_TRY, block);
3110
3111 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3112 compiler_use_next_block(c, finally);
3113 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003114 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003115
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003116 /* Finally block starts; context.__exit__ is on the stack under
3117 the exception or return information. Just issue our magic
3118 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003119 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003120
3121 /* Finally block ends. */
3122 ADDOP(c, END_FINALLY);
3123 compiler_pop_fblock(c, FINALLY_END, finally);
3124 return 1;
3125}
3126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127static int
3128compiler_visit_expr(struct compiler *c, expr_ty e)
3129{
3130 int i, n;
3131
Thomas Wouters89f507f2006-12-13 04:49:30 +00003132 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003133 set a new line number for the next instruction.
3134 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003135 if (e->lineno > c->u->u_lineno) {
3136 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003137 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 }
3139 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003140 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003142 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003143 VISIT(c, expr, e->v.BinOp.left);
3144 VISIT(c, expr, e->v.BinOp.right);
3145 ADDOP(c, binop(c, e->v.BinOp.op));
3146 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003147 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148 VISIT(c, expr, e->v.UnaryOp.operand);
3149 ADDOP(c, unaryop(e->v.UnaryOp.op));
3150 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003151 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003152 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003153 case IfExp_kind:
3154 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003155 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003156 n = asdl_seq_LEN(e->v.Dict.values);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003157 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003158 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003159 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003160 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003161 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003162 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Christian Heimes99170a52007-12-19 02:07:34 +00003163 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003164 }
3165 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003166 case Set_kind:
3167 n = asdl_seq_LEN(e->v.Set.elts);
3168 VISIT_SEQ(c, expr, e->v.Set.elts);
3169 ADDOP_I(c, BUILD_SET, n);
3170 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003171 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003173 case ListComp_kind:
3174 return compiler_listcomp(c, e);
3175 case SetComp_kind:
3176 return compiler_setcomp(c, e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003177 case DictComp_kind:
3178 return compiler_dictcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179 case Yield_kind:
3180 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003181 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 if (e->v.Yield.value) {
3183 VISIT(c, expr, e->v.Yield.value);
3184 }
3185 else {
3186 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3187 }
3188 ADDOP(c, YIELD_VALUE);
3189 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003190 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003191 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003192 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003193 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003194 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3196 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003197 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3199 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003200 case Bytes_kind:
3201 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
Thomas Wouters00e41de2007-02-23 19:56:57 +00003202 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003203 case Ellipsis_kind:
3204 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3205 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003207 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003208 if (e->v.Attribute.ctx != AugStore)
3209 VISIT(c, expr, e->v.Attribute.value);
3210 switch (e->v.Attribute.ctx) {
3211 case AugLoad:
3212 ADDOP(c, DUP_TOP);
3213 /* Fall through to load */
3214 case Load:
3215 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3216 break;
3217 case AugStore:
3218 ADDOP(c, ROT_TWO);
3219 /* Fall through to save */
3220 case Store:
3221 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3222 break;
3223 case Del:
3224 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3225 break;
3226 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003227 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003228 PyErr_SetString(PyExc_SystemError,
3229 "param invalid in attribute expression");
3230 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003231 }
3232 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003233 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 switch (e->v.Subscript.ctx) {
3235 case AugLoad:
3236 VISIT(c, expr, e->v.Subscript.value);
3237 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3238 break;
3239 case Load:
3240 VISIT(c, expr, e->v.Subscript.value);
3241 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3242 break;
3243 case AugStore:
3244 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3245 break;
3246 case Store:
3247 VISIT(c, expr, e->v.Subscript.value);
3248 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3249 break;
3250 case Del:
3251 VISIT(c, expr, e->v.Subscript.value);
3252 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3253 break;
3254 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003255 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003256 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003257 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003258 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 }
3260 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003261 case Starred_kind:
3262 switch (e->v.Starred.ctx) {
3263 case Store:
3264 /* In all legitimate cases, the Starred node was already replaced
3265 * by compiler_list/compiler_tuple. XXX: is that okay? */
3266 return compiler_error(c,
3267 "starred assignment target must be in a list or tuple");
3268 default:
3269 return compiler_error(c,
3270 "can use starred expression only as assignment target");
3271 }
3272 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003273 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3275 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003276 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003278 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003279 return compiler_tuple(c, e);
3280 }
3281 return 1;
3282}
3283
3284static int
3285compiler_augassign(struct compiler *c, stmt_ty s)
3286{
3287 expr_ty e = s->v.AugAssign.target;
3288 expr_ty auge;
3289
3290 assert(s->kind == AugAssign_kind);
3291
3292 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003293 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003294 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003295 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003296 if (auge == NULL)
3297 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003298 VISIT(c, expr, auge);
3299 VISIT(c, expr, s->v.AugAssign.value);
3300 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3301 auge->v.Attribute.ctx = AugStore;
3302 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303 break;
3304 case Subscript_kind:
3305 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003306 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003307 if (auge == NULL)
3308 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003309 VISIT(c, expr, auge);
3310 VISIT(c, expr, s->v.AugAssign.value);
3311 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003312 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003313 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003314 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003315 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003316 if (!compiler_nameop(c, e->v.Name.id, Load))
3317 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318 VISIT(c, expr, s->v.AugAssign.value);
3319 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3320 return compiler_nameop(c, e->v.Name.id, Store);
3321 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003322 PyErr_Format(PyExc_SystemError,
3323 "invalid node type (%d) for augmented assignment",
3324 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003325 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326 }
3327 return 1;
3328}
3329
3330static int
3331compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3332{
3333 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003334 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3335 PyErr_SetString(PyExc_SystemError,
3336 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003337 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003339 f = &c->u->u_fblock[c->u->u_nfblocks++];
3340 f->fb_type = t;
3341 f->fb_block = b;
3342 return 1;
3343}
3344
3345static void
3346compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3347{
3348 struct compiler_unit *u = c->u;
3349 assert(u->u_nfblocks > 0);
3350 u->u_nfblocks--;
3351 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3352 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3353}
3354
Thomas Wouters89f507f2006-12-13 04:49:30 +00003355static int
3356compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003357 int i;
3358 struct compiler_unit *u = c->u;
3359 for (i = 0; i < u->u_nfblocks; ++i) {
3360 if (u->u_fblock[i].fb_type == LOOP)
3361 return 1;
3362 }
3363 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003364}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003365/* Raises a SyntaxError and returns 0.
3366 If something goes wrong, a different exception may be raised.
3367*/
3368
3369static int
3370compiler_error(struct compiler *c, const char *errstr)
3371{
3372 PyObject *loc;
3373 PyObject *u = NULL, *v = NULL;
3374
3375 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3376 if (!loc) {
3377 Py_INCREF(Py_None);
3378 loc = Py_None;
3379 }
3380 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3381 Py_None, loc);
3382 if (!u)
3383 goto exit;
3384 v = Py_BuildValue("(zO)", errstr, u);
3385 if (!v)
3386 goto exit;
3387 PyErr_SetObject(PyExc_SyntaxError, v);
3388 exit:
3389 Py_DECREF(loc);
3390 Py_XDECREF(u);
3391 Py_XDECREF(v);
3392 return 0;
3393}
3394
3395static int
3396compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003397 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003399 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003400
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003401 /* XXX this code is duplicated */
3402 switch (ctx) {
3403 case AugLoad: /* fall through to Load */
3404 case Load: op = BINARY_SUBSCR; break;
3405 case AugStore:/* fall through to Store */
3406 case Store: op = STORE_SUBSCR; break;
3407 case Del: op = DELETE_SUBSCR; break;
3408 case Param:
3409 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003410 "invalid %s kind %d in subscript\n",
3411 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003412 return 0;
3413 }
3414 if (ctx == AugLoad) {
3415 ADDOP_I(c, DUP_TOPX, 2);
3416 }
3417 else if (ctx == AugStore) {
3418 ADDOP(c, ROT_THREE);
3419 }
3420 ADDOP(c, op);
3421 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003422}
3423
3424static int
3425compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3426{
3427 int n = 2;
3428 assert(s->kind == Slice_kind);
3429
3430 /* only handles the cases where BUILD_SLICE is emitted */
3431 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003432 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433 }
3434 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003435 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003437
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003439 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003440 }
3441 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003442 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443 }
3444
3445 if (s->v.Slice.step) {
3446 n++;
3447 VISIT(c, expr, s->v.Slice.step);
3448 }
3449 ADDOP_I(c, BUILD_SLICE, n);
3450 return 1;
3451}
3452
3453static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3455 expr_context_ty ctx)
3456{
3457 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003458 case Slice_kind:
3459 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003460 case Index_kind:
3461 VISIT(c, expr, s->v.Index.value);
3462 break;
3463 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003464 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003465 PyErr_SetString(PyExc_SystemError,
3466 "extended slice invalid in nested slice");
3467 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468 }
3469 return 1;
3470}
3471
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003472static int
3473compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3474{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003475 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003477 case Index_kind:
3478 kindname = "index";
3479 if (ctx != AugStore) {
3480 VISIT(c, expr, s->v.Index.value);
3481 }
3482 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003483 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003484 kindname = "slice";
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003485 if (ctx != AugStore) {
3486 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487 return 0;
3488 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003489 break;
3490 case ExtSlice_kind:
3491 kindname = "extended slice";
3492 if (ctx != AugStore) {
3493 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3494 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003495 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003496 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003497 if (!compiler_visit_nested_slice(c, sub, ctx))
3498 return 0;
3499 }
3500 ADDOP_I(c, BUILD_TUPLE, n);
3501 }
3502 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003503 default:
3504 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003505 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003506 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003508 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003509}
3510
Thomas Wouters89f507f2006-12-13 04:49:30 +00003511/* End of the compiler section, beginning of the assembler section */
3512
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513/* do depth-first search of basic block graph, starting with block.
3514 post records the block indices in post-order.
3515
3516 XXX must handle implicit jumps from one block to next
3517*/
3518
Thomas Wouters89f507f2006-12-13 04:49:30 +00003519struct assembler {
3520 PyObject *a_bytecode; /* string containing bytecode */
3521 int a_offset; /* offset into bytecode */
3522 int a_nblocks; /* number of reachable blocks */
3523 basicblock **a_postorder; /* list of blocks in dfs postorder */
3524 PyObject *a_lnotab; /* string containing lnotab */
3525 int a_lnotab_off; /* offset into lnotab */
3526 int a_lineno; /* last lineno of emitted instruction */
3527 int a_lineno_off; /* bytecode offset of last lineno */
3528};
3529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530static void
3531dfs(struct compiler *c, basicblock *b, struct assembler *a)
3532{
3533 int i;
3534 struct instr *instr = NULL;
3535
3536 if (b->b_seen)
3537 return;
3538 b->b_seen = 1;
3539 if (b->b_next != NULL)
3540 dfs(c, b->b_next, a);
3541 for (i = 0; i < b->b_iused; i++) {
3542 instr = &b->b_instr[i];
3543 if (instr->i_jrel || instr->i_jabs)
3544 dfs(c, instr->i_target, a);
3545 }
3546 a->a_postorder[a->a_nblocks++] = b;
3547}
3548
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003549static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3551{
Neil Schemenauere441cb72009-10-14 19:14:38 +00003552 int i, target_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553 struct instr *instr;
3554 if (b->b_seen || b->b_startdepth >= depth)
3555 return maxdepth;
3556 b->b_seen = 1;
3557 b->b_startdepth = depth;
3558 for (i = 0; i < b->b_iused; i++) {
3559 instr = &b->b_instr[i];
3560 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3561 if (depth > maxdepth)
3562 maxdepth = depth;
3563 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3564 if (instr->i_jrel || instr->i_jabs) {
Neil Schemenauere441cb72009-10-14 19:14:38 +00003565 target_depth = depth;
3566 if (instr->i_opcode == FOR_ITER) {
3567 target_depth = depth-2;
3568 } else if (instr->i_opcode == SETUP_FINALLY ||
3569 instr->i_opcode == SETUP_EXCEPT) {
3570 target_depth = depth+3;
3571 if (target_depth > maxdepth)
3572 maxdepth = target_depth;
3573 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574 maxdepth = stackdepth_walk(c, instr->i_target,
Neil Schemenauere441cb72009-10-14 19:14:38 +00003575 target_depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576 if (instr->i_opcode == JUMP_ABSOLUTE ||
3577 instr->i_opcode == JUMP_FORWARD) {
3578 goto out; /* remaining code is dead */
3579 }
3580 }
3581 }
3582 if (b->b_next)
3583 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3584out:
3585 b->b_seen = 0;
3586 return maxdepth;
3587}
3588
3589/* Find the flow path that needs the largest stack. We assume that
3590 * cycles in the flow graph have no net effect on the stack depth.
3591 */
3592static int
3593stackdepth(struct compiler *c)
3594{
3595 basicblock *b, *entryblock;
3596 entryblock = NULL;
3597 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3598 b->b_seen = 0;
3599 b->b_startdepth = INT_MIN;
3600 entryblock = b;
3601 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003602 if (!entryblock)
3603 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604 return stackdepth_walk(c, entryblock, 0, 0);
3605}
3606
3607static int
3608assemble_init(struct assembler *a, int nblocks, int firstlineno)
3609{
3610 memset(a, 0, sizeof(struct assembler));
3611 a->a_lineno = firstlineno;
Christian Heimes72b710a2008-05-26 13:28:38 +00003612 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003613 if (!a->a_bytecode)
3614 return 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003615 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 if (!a->a_lnotab)
3617 return 0;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003618 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3619 PyErr_NoMemory();
3620 return 0;
3621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003623 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003624 if (!a->a_postorder) {
3625 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003627 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628 return 1;
3629}
3630
3631static void
3632assemble_free(struct assembler *a)
3633{
3634 Py_XDECREF(a->a_bytecode);
3635 Py_XDECREF(a->a_lnotab);
3636 if (a->a_postorder)
3637 PyObject_Free(a->a_postorder);
3638}
3639
3640/* Return the size of a basic block in bytes. */
3641
3642static int
3643instrsize(struct instr *instr)
3644{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003645 if (!instr->i_hasarg)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003646 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003647 if (instr->i_oparg > 0xffff)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003648 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3649 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003650}
3651
3652static int
3653blocksize(basicblock *b)
3654{
3655 int i;
3656 int size = 0;
3657
3658 for (i = 0; i < b->b_iused; i++)
3659 size += instrsize(&b->b_instr[i]);
3660 return size;
3661}
3662
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003663/* Appends a pair to the end of the line number table, a_lnotab, representing
3664 the instruction's bytecode offset and line number. See
3665 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003666
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003667static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003668assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003669{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670 int d_bytecode, d_lineno;
3671 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003672 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003673
3674 d_bytecode = a->a_offset - a->a_lineno_off;
3675 d_lineno = i->i_lineno - a->a_lineno;
3676
3677 assert(d_bytecode >= 0);
3678 assert(d_lineno >= 0);
3679
Christian Heimes2202f872008-02-06 14:31:34 +00003680 if(d_bytecode == 0 && d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003681 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003683 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003684 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685 nbytes = a->a_lnotab_off + 2 * ncodes;
Christian Heimes72b710a2008-05-26 13:28:38 +00003686 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 if (nbytes >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003688 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003689 len = nbytes;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003690 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003691 len *= 2;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003692 else {
3693 PyErr_NoMemory();
3694 return 0;
3695 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003696 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003697 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003698 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003699 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003700 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003701 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702 *lnotab++ = 255;
3703 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003704 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003705 d_bytecode -= ncodes * 255;
3706 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003707 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003708 assert(d_bytecode <= 255);
3709 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003710 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003711 nbytes = a->a_lnotab_off + 2 * ncodes;
Christian Heimes72b710a2008-05-26 13:28:38 +00003712 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003713 if (nbytes >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003714 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715 len = nbytes;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003716 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003717 len *= 2;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003718 else {
3719 PyErr_NoMemory();
3720 return 0;
3721 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003722 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003724 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003725 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003726 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003728 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003729 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003730 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003731 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003732 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003734 d_lineno -= ncodes * 255;
3735 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003736 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003737
Christian Heimes72b710a2008-05-26 13:28:38 +00003738 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003739 if (a->a_lnotab_off + 2 >= len) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003740 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003741 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003742 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003743 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003744 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003745
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003746 a->a_lnotab_off += 2;
3747 if (d_bytecode) {
3748 *lnotab++ = d_bytecode;
3749 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003750 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003751 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003752 *lnotab++ = 0;
3753 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003754 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003755 a->a_lineno = i->i_lineno;
3756 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003757 return 1;
3758}
3759
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003760/* assemble_emit()
3761 Extend the bytecode with a new instruction.
3762 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003763*/
3764
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003765static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003766assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003767{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003768 int size, arg = 0, ext = 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003769 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 char *code;
3771
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003772 size = instrsize(i);
3773 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003775 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003776 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003778 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003779 if (a->a_offset + size >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003780 if (len > PY_SSIZE_T_MAX / 2)
3781 return 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003782 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003783 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003784 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003785 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003787 if (size == 6) {
3788 assert(i->i_hasarg);
3789 *code++ = (char)EXTENDED_ARG;
3790 *code++ = ext & 0xff;
3791 *code++ = ext >> 8;
3792 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003793 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003795 if (i->i_hasarg) {
3796 assert(size == 3 || size == 6);
3797 *code++ = arg & 0xff;
3798 *code++ = arg >> 8;
3799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003800 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003801}
3802
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003803static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003804assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003805{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003807 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003808 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003809
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 /* Compute the size of each block and fixup jump args.
3811 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003812start:
3813 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003815 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 bsize = blocksize(b);
3817 b->b_offset = totsize;
3818 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003819 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003820 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3822 bsize = b->b_offset;
3823 for (i = 0; i < b->b_iused; i++) {
3824 struct instr *instr = &b->b_instr[i];
3825 /* Relative jumps are computed relative to
3826 the instruction pointer after fetching
3827 the jump instruction.
3828 */
3829 bsize += instrsize(instr);
3830 if (instr->i_jabs)
3831 instr->i_oparg = instr->i_target->b_offset;
3832 else if (instr->i_jrel) {
3833 int delta = instr->i_target->b_offset - bsize;
3834 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003835 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003836 else
3837 continue;
3838 if (instr->i_oparg > 0xffff)
3839 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003840 }
3841 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003842
3843 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003844 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003845 with a better solution.
3846
3847 In the meantime, should the goto be dropped in favor
3848 of a loop?
3849
3850 The issue is that in the first loop blocksize() is called
3851 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003852 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003853 i_oparg is calculated in the second loop above.
3854
3855 So we loop until we stop seeing new EXTENDED_ARGs.
3856 The only EXTENDED_ARGs that could be popping up are
3857 ones in jump instructions. So this should converge
3858 fairly quickly.
3859 */
3860 if (last_extended_arg_count != extended_arg_count) {
3861 last_extended_arg_count = extended_arg_count;
3862 goto start;
3863 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864}
3865
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003866static PyObject *
3867dict_keys_inorder(PyObject *dict, int offset)
3868{
3869 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003870 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003871
3872 tuple = PyTuple_New(size);
3873 if (tuple == NULL)
3874 return NULL;
3875 while (PyDict_Next(dict, &pos, &k, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003876 i = PyLong_AS_LONG(v);
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003877 /* The keys of the dictionary are tuples. (see compiler_add_o)
3878 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003879 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003880 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003881 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003882 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003883 PyTuple_SET_ITEM(tuple, i - offset, k);
3884 }
3885 return tuple;
3886}
3887
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003888static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003889compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003890{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003891 PySTEntryObject *ste = c->u->u_ste;
3892 int flags = 0, n;
3893 if (ste->ste_type != ModuleBlock)
3894 flags |= CO_NEWLOCALS;
3895 if (ste->ste_type == FunctionBlock) {
3896 if (!ste->ste_unoptimized)
3897 flags |= CO_OPTIMIZED;
3898 if (ste->ste_nested)
3899 flags |= CO_NESTED;
3900 if (ste->ste_generator)
3901 flags |= CO_GENERATOR;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003902 if (ste->ste_varargs)
3903 flags |= CO_VARARGS;
3904 if (ste->ste_varkeywords)
3905 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003906 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003907
3908 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003909 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 n = PyDict_Size(c->u->u_freevars);
3912 if (n < 0)
3913 return -1;
3914 if (n == 0) {
3915 n = PyDict_Size(c->u->u_cellvars);
3916 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003917 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003918 if (n == 0) {
3919 flags |= CO_NOFREE;
3920 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003921 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003922
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003923 return flags;
3924}
3925
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003926static PyCodeObject *
3927makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003928{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929 PyObject *tmp;
3930 PyCodeObject *co = NULL;
3931 PyObject *consts = NULL;
3932 PyObject *names = NULL;
3933 PyObject *varnames = NULL;
3934 PyObject *filename = NULL;
3935 PyObject *name = NULL;
3936 PyObject *freevars = NULL;
3937 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003938 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003940
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003941 tmp = dict_keys_inorder(c->u->u_consts, 0);
3942 if (!tmp)
3943 goto error;
3944 consts = PySequence_List(tmp); /* optimize_code requires a list */
3945 Py_DECREF(tmp);
3946
3947 names = dict_keys_inorder(c->u->u_names, 0);
3948 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3949 if (!consts || !names || !varnames)
3950 goto error;
3951
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003952 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3953 if (!cellvars)
3954 goto error;
3955 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3956 if (!freevars)
3957 goto error;
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003958 filename = PyUnicode_DecodeFSDefault(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003959 if (!filename)
3960 goto error;
3961
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003962 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963 flags = compute_code_flags(c);
3964 if (flags < 0)
3965 goto error;
3966
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003967 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003968 if (!bytecode)
3969 goto error;
3970
3971 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3972 if (!tmp)
3973 goto error;
3974 Py_DECREF(consts);
3975 consts = tmp;
3976
Guido van Rossum4f72a782006-10-27 23:31:49 +00003977 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3978 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003979 bytecode, consts, names, varnames,
3980 freevars, cellvars,
3981 filename, c->u->u_name,
3982 c->u->u_firstlineno,
3983 a->a_lnotab);
3984 error:
3985 Py_XDECREF(consts);
3986 Py_XDECREF(names);
3987 Py_XDECREF(varnames);
3988 Py_XDECREF(filename);
3989 Py_XDECREF(name);
3990 Py_XDECREF(freevars);
3991 Py_XDECREF(cellvars);
3992 Py_XDECREF(bytecode);
3993 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003994}
3995
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003996
3997/* For debugging purposes only */
3998#if 0
3999static void
4000dump_instr(const struct instr *i)
4001{
4002 const char *jrel = i->i_jrel ? "jrel " : "";
4003 const char *jabs = i->i_jabs ? "jabs " : "";
4004 char arg[128];
4005
4006 *arg = '\0';
4007 if (i->i_hasarg)
4008 sprintf(arg, "arg: %d ", i->i_oparg);
4009
4010 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4011 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4012}
4013
4014static void
4015dump_basicblock(const basicblock *b)
4016{
4017 const char *seen = b->b_seen ? "seen " : "";
4018 const char *b_return = b->b_return ? "return " : "";
4019 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4020 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4021 if (b->b_instr) {
4022 int i;
4023 for (i = 0; i < b->b_iused; i++) {
4024 fprintf(stderr, " [%02d] ", i);
4025 dump_instr(b->b_instr + i);
4026 }
4027 }
4028}
4029#endif
4030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004031static PyCodeObject *
4032assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004033{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004034 basicblock *b, *entryblock;
4035 struct assembler a;
4036 int i, j, nblocks;
4037 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004039 /* Make sure every block that falls off the end returns None.
4040 XXX NEXT_BLOCK() isn't quite right, because if the last
4041 block ends with a jump or return b_next shouldn't set.
4042 */
4043 if (!c->u->u_curblock->b_return) {
4044 NEXT_BLOCK(c);
4045 if (addNone)
4046 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4047 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004048 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050 nblocks = 0;
4051 entryblock = NULL;
4052 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4053 nblocks++;
4054 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004055 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004056
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004057 /* Set firstlineno if it wasn't explicitly set. */
4058 if (!c->u->u_firstlineno) {
4059 if (entryblock && entryblock->b_instr)
4060 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4061 else
4062 c->u->u_firstlineno = 1;
4063 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4065 goto error;
4066 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004067
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004068 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004069 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004070
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004071 /* Emit code in reverse postorder from dfs. */
4072 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004073 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004074 for (j = 0; j < b->b_iused; j++)
4075 if (!assemble_emit(&a, &b->b_instr[j]))
4076 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004077 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004078
Christian Heimes72b710a2008-05-26 13:28:38 +00004079 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080 goto error;
Christian Heimes72b710a2008-05-26 13:28:38 +00004081 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004082 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004084 co = makecode(c, &a);
4085 error:
4086 assemble_free(&a);
4087 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004088}