blob: 787dfe37d0e5ad58795d474027669072de3a7c21 [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:
775 return 1;
776
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:
802 return 0;
803 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;
Christian Heimes400adb02008-02-01 08:12:03 +0000898 unsigned char *p, *q;
899 Py_complex z;
900 double d;
901 int real_part_zero, imag_part_zero;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000903 /* necessary to make sure types aren't coerced (e.g., int and long) */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000904 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
905 if (PyFloat_Check(o)) {
Christian Heimes400adb02008-02-01 08:12:03 +0000906 d = PyFloat_AS_DOUBLE(o);
907 p = (unsigned char*) &d;
908 /* all we need is to make the tuple different in either the 0.0
909 * or -0.0 case from all others, just to avoid the "coercion".
910 */
911 if (*p==0 && p[sizeof(double)-1]==0)
912 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
913 else
914 t = PyTuple_Pack(2, o, o->ob_type);
915 }
916 else if (PyComplex_Check(o)) {
917 /* complex case is even messier: we need to make complex(x,
918 0.) different from complex(x, -0.) and complex(0., y)
919 different from complex(-0., y), for any x and y. In
920 particular, all four complex zeros should be
921 distinguished.*/
922 z = PyComplex_AsCComplex(o);
923 p = (unsigned char*) &(z.real);
924 q = (unsigned char*) &(z.imag);
925 /* all that matters here is that on IEEE platforms
926 real_part_zero will be true if z.real == 0., and false if
927 z.real == -0. In fact, real_part_zero will also be true
928 for some other rarely occurring nonzero floats, but this
929 doesn't matter. Similar comments apply to
930 imag_part_zero. */
931 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
932 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
933 if (real_part_zero && imag_part_zero) {
934 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
935 }
936 else if (real_part_zero && !imag_part_zero) {
937 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
938 }
939 else if (!real_part_zero && imag_part_zero) {
940 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
941 }
942 else {
943 t = PyTuple_Pack(2, o, o->ob_type);
944 }
945 }
946 else {
947 t = PyTuple_Pack(2, o, o->ob_type);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000948 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000949 if (t == NULL)
Christian Heimes400adb02008-02-01 08:12:03 +0000950 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951
952 v = PyDict_GetItem(dict, t);
953 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000954 if (PyErr_Occurred())
955 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956 arg = PyDict_Size(dict);
Christian Heimes217cfd12007-12-02 14:31:20 +0000957 v = PyLong_FromLong(arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958 if (!v) {
959 Py_DECREF(t);
960 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000961 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962 if (PyDict_SetItem(dict, t, v) < 0) {
963 Py_DECREF(t);
964 Py_DECREF(v);
965 return -1;
966 }
967 Py_DECREF(v);
968 }
969 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000970 arg = PyLong_AsLong(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000972 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000973}
974
975static int
976compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
977 PyObject *o)
978{
979 int arg = compiler_add_o(c, dict, o);
980 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000981 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000982 return compiler_addop_i(c, opcode, arg);
983}
984
985static int
986compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000987 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000988{
989 int arg;
990 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
991 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000992 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993 arg = compiler_add_o(c, dict, mangled);
994 Py_DECREF(mangled);
995 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000996 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997 return compiler_addop_i(c, opcode, arg);
998}
999
1000/* Add an opcode with an integer argument.
1001 Returns 0 on failure, 1 on success.
1002*/
1003
1004static int
1005compiler_addop_i(struct compiler *c, int opcode, int oparg)
1006{
1007 struct instr *i;
1008 int off;
1009 off = compiler_next_instr(c, c->u->u_curblock);
1010 if (off < 0)
1011 return 0;
1012 i = &c->u->u_curblock->b_instr[off];
1013 i->i_opcode = opcode;
1014 i->i_oparg = oparg;
1015 i->i_hasarg = 1;
1016 compiler_set_lineno(c, off);
1017 return 1;
1018}
1019
1020static int
1021compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1022{
1023 struct instr *i;
1024 int off;
1025
1026 assert(b != NULL);
1027 off = compiler_next_instr(c, c->u->u_curblock);
1028 if (off < 0)
1029 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001030 i = &c->u->u_curblock->b_instr[off];
1031 i->i_opcode = opcode;
1032 i->i_target = b;
1033 i->i_hasarg = 1;
1034 if (absolute)
1035 i->i_jabs = 1;
1036 else
1037 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001038 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 return 1;
1040}
1041
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001042/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1043 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044 it as the current block. NEXT_BLOCK() also creates an implicit jump
1045 from the current block to the new block.
1046*/
1047
Thomas Wouters89f507f2006-12-13 04:49:30 +00001048/* The returns inside these macros make it impossible to decref objects
1049 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001050*/
1051
1052
1053#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001054 if (compiler_use_new_block((C)) == NULL) \
1055 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001056}
1057
1058#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001059 if (compiler_next_block((C)) == NULL) \
1060 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061}
1062
1063#define ADDOP(C, OP) { \
1064 if (!compiler_addop((C), (OP))) \
1065 return 0; \
1066}
1067
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001068#define ADDOP_IN_SCOPE(C, OP) { \
1069 if (!compiler_addop((C), (OP))) { \
1070 compiler_exit_scope(c); \
1071 return 0; \
1072 } \
1073}
1074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001075#define ADDOP_O(C, OP, O, TYPE) { \
1076 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1077 return 0; \
1078}
1079
1080#define ADDOP_NAME(C, OP, O, TYPE) { \
1081 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1082 return 0; \
1083}
1084
1085#define ADDOP_I(C, OP, O) { \
1086 if (!compiler_addop_i((C), (OP), (O))) \
1087 return 0; \
1088}
1089
1090#define ADDOP_JABS(C, OP, O) { \
1091 if (!compiler_addop_j((C), (OP), (O), 1)) \
1092 return 0; \
1093}
1094
1095#define ADDOP_JREL(C, OP, O) { \
1096 if (!compiler_addop_j((C), (OP), (O), 0)) \
1097 return 0; \
1098}
1099
1100/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1101 the ASDL name to synthesize the name of the C type and the visit function.
1102*/
1103
1104#define VISIT(C, TYPE, V) {\
1105 if (!compiler_visit_ ## TYPE((C), (V))) \
1106 return 0; \
1107}
1108
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001109#define VISIT_IN_SCOPE(C, TYPE, V) {\
1110 if (!compiler_visit_ ## TYPE((C), (V))) { \
1111 compiler_exit_scope(c); \
1112 return 0; \
1113 } \
1114}
1115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116#define VISIT_SLICE(C, V, CTX) {\
1117 if (!compiler_visit_slice((C), (V), (CTX))) \
1118 return 0; \
1119}
1120
1121#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001122 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001124 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001125 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001126 if (!compiler_visit_ ## TYPE((C), elt)) \
1127 return 0; \
1128 } \
1129}
1130
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001131#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001132 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001133 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001134 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001135 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001136 if (!compiler_visit_ ## TYPE((C), elt)) { \
1137 compiler_exit_scope(c); \
1138 return 0; \
1139 } \
1140 } \
1141}
1142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143static int
1144compiler_isdocstring(stmt_ty s)
1145{
1146 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001147 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 return s->v.Expr.value->kind == Str_kind;
1149}
1150
1151/* Compile a sequence of statements, checking for a docstring. */
1152
1153static int
1154compiler_body(struct compiler *c, asdl_seq *stmts)
1155{
1156 int i = 0;
1157 stmt_ty st;
1158
1159 if (!asdl_seq_LEN(stmts))
1160 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001161 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001162 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1163 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 i = 1;
1165 VISIT(c, expr, st->v.Expr.value);
1166 if (!compiler_nameop(c, __doc__, Store))
1167 return 0;
1168 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001169 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001170 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171 return 1;
1172}
1173
1174static PyCodeObject *
1175compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001176{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001177 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001178 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001179 static PyObject *module;
1180 if (!module) {
Christian Heimesfe82e772008-01-28 02:38:20 +00001181 module = PyUnicode_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182 if (!module)
1183 return NULL;
1184 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001185 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1186 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001187 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 switch (mod->kind) {
1189 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001190 if (!compiler_body(c, mod->v.Module.body)) {
1191 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001193 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001194 break;
1195 case Interactive_kind:
1196 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001197 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001198 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001199 break;
1200 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001201 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001202 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001203 break;
1204 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001205 PyErr_SetString(PyExc_SystemError,
1206 "suite should not be possible");
1207 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001208 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001209 PyErr_Format(PyExc_SystemError,
1210 "module kind %d should not be possible",
1211 mod->kind);
1212 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001213 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214 co = assemble(c, addNone);
1215 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001216 return co;
1217}
1218
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219/* The test for LOCAL must come before the test for FREE in order to
1220 handle classes where name is both local and free. The local var is
1221 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001222*/
1223
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001224static int
1225get_ref_type(struct compiler *c, PyObject *name)
1226{
1227 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001228 if (scope == 0) {
1229 char buf[350];
1230 PyOS_snprintf(buf, sizeof(buf),
1231 "unknown scope for %.100s in %.100s(%s) in %s\n"
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001232 "symbols: %s\nlocals: %s\nglobals: %s",
Christian Heimes72b710a2008-05-26 13:28:38 +00001233 PyBytes_AS_STRING(name),
1234 PyBytes_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001235 PyObject_REPR(c->u->u_ste->ste_id),
1236 c->c_filename,
1237 PyObject_REPR(c->u->u_ste->ste_symbols),
1238 PyObject_REPR(c->u->u_varnames),
1239 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001241 Py_FatalError(buf);
1242 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001243
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001244 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245}
1246
1247static int
1248compiler_lookup_arg(PyObject *dict, PyObject *name)
1249{
1250 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001251 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001253 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001255 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001257 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001258 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001259}
1260
1261static int
1262compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1263{
1264 int i, free = PyCode_GetNumFree(co);
1265 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001266 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1267 ADDOP_I(c, MAKE_FUNCTION, args);
1268 return 1;
1269 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 for (i = 0; i < free; ++i) {
1271 /* Bypass com_addop_varname because it will generate
1272 LOAD_DEREF but LOAD_CLOSURE is needed.
1273 */
1274 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1275 int arg, reftype;
1276
1277 /* Special case: If a class contains a method with a
1278 free variable that has the same name as a method,
1279 the name will be considered free *and* local in the
1280 class. It should be handled by the closure, as
1281 well as by the normal name loookup logic.
1282 */
1283 reftype = get_ref_type(c, name);
1284 if (reftype == CELL)
1285 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1286 else /* (reftype == FREE) */
1287 arg = compiler_lookup_arg(c->u->u_freevars, name);
1288 if (arg == -1) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001289 fprintf(stderr,
1290 "lookup %s in %s %d %d\n"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001291 "freevars of %s: %s\n",
1292 PyObject_REPR(name),
Christian Heimes72b710a2008-05-26 13:28:38 +00001293 PyBytes_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294 reftype, arg,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001295 _PyUnicode_AsString(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001296 PyObject_REPR(co->co_freevars));
1297 Py_FatalError("compiler_make_closure()");
1298 }
1299 ADDOP_I(c, LOAD_CLOSURE, arg);
1300 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001301 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001302 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001303 ADDOP_I(c, MAKE_CLOSURE, args);
1304 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001305}
1306
1307static int
1308compiler_decorators(struct compiler *c, asdl_seq* decos)
1309{
1310 int i;
1311
1312 if (!decos)
1313 return 1;
1314
1315 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001316 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317 }
1318 return 1;
1319}
1320
1321static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1323 asdl_seq *kw_defaults)
1324{
1325 int i, default_count = 0;
1326 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001327 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001328 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1329 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001330 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001331 if (!compiler_visit_expr(c, default_)) {
1332 return -1;
1333 }
1334 default_count++;
1335 }
1336 }
1337 return default_count;
1338}
1339
1340static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001341compiler_visit_argannotation(struct compiler *c, identifier id,
1342 expr_ty annotation, PyObject *names)
1343{
1344 if (annotation) {
1345 VISIT(c, expr, annotation);
1346 if (PyList_Append(names, id))
1347 return -1;
1348 }
1349 return 0;
1350}
1351
1352static int
1353compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1354 PyObject *names)
1355{
1356 int i, error;
1357 for (i = 0; i < asdl_seq_LEN(args); i++) {
1358 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001359 error = compiler_visit_argannotation(
1360 c,
1361 arg->arg,
1362 arg->annotation,
1363 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001364 if (error)
1365 return error;
1366 }
1367 return 0;
1368}
1369
1370static int
1371compiler_visit_annotations(struct compiler *c, arguments_ty args,
1372 expr_ty returns)
1373{
Guido van Rossum0240b922007-02-26 21:23:50 +00001374 /* Push arg annotations and a list of the argument names. Return the #
1375 of items pushed. The expressions are evaluated out-of-order wrt the
1376 source code.
1377
1378 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1379 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 static identifier return_str;
1381 PyObject *names;
1382 int len;
1383 names = PyList_New(0);
1384 if (!names)
1385 return -1;
1386
1387 if (compiler_visit_argannotations(c, args->args, names))
1388 goto error;
1389 if (args->varargannotation &&
1390 compiler_visit_argannotation(c, args->vararg,
1391 args->varargannotation, names))
1392 goto error;
1393 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1394 goto error;
1395 if (args->kwargannotation &&
1396 compiler_visit_argannotation(c, args->kwarg,
1397 args->kwargannotation, names))
1398 goto error;
1399
1400 if (!return_str) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001401 return_str = PyUnicode_InternFromString("return");
Neal Norwitzc1505362006-12-28 06:47:50 +00001402 if (!return_str)
1403 goto error;
1404 }
1405 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1406 goto error;
1407 }
1408
1409 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001410 if (len > 65534) {
1411 /* len must fit in 16 bits, and len is incremented below */
1412 PyErr_SetString(PyExc_SyntaxError,
1413 "too many annotations");
1414 goto error;
1415 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001416 if (len) {
1417 /* convert names to a tuple and place on stack */
1418 PyObject *elt;
1419 int i;
1420 PyObject *s = PyTuple_New(len);
1421 if (!s)
1422 goto error;
1423 for (i = 0; i < len; i++) {
1424 elt = PyList_GET_ITEM(names, i);
1425 Py_INCREF(elt);
1426 PyTuple_SET_ITEM(s, i, elt);
1427 }
1428 ADDOP_O(c, LOAD_CONST, s, consts);
1429 Py_DECREF(s);
1430 len++; /* include the just-pushed tuple */
1431 }
1432 Py_DECREF(names);
1433 return len;
1434
1435error:
1436 Py_DECREF(names);
1437 return -1;
1438}
1439
1440static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441compiler_function(struct compiler *c, stmt_ty s)
1442{
1443 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001444 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001445 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001446 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001447 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001448 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001449 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001450 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451
1452 assert(s->kind == FunctionDef_kind);
1453
1454 if (!compiler_decorators(c, decos))
1455 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001456 if (args->kwonlyargs) {
1457 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1458 args->kw_defaults);
1459 if (res < 0)
1460 return 0;
1461 kw_default_count = res;
1462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463 if (args->defaults)
1464 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001465 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001466 if (num_annotations < 0)
1467 return 0;
1468 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001470 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1471 s->lineno))
1472 return 0;
1473
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001474 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001475 docstring = compiler_isdocstring(st);
Thomas Woutersce272b62007-09-19 21:19:28 +00001476 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001477 first_const = st->v.Expr.value->v.Str.s;
1478 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001479 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001480 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001481 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001482
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001484 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001486 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001487 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001488 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1489 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001490 }
1491 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001492 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493 if (co == NULL)
1494 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001495
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496 arglength = asdl_seq_LEN(args->defaults);
1497 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001498 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001499 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001500 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501
Neal Norwitzc1505362006-12-28 06:47:50 +00001502 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1504 ADDOP_I(c, CALL_FUNCTION, 1);
1505 }
1506
1507 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1508}
1509
1510static int
1511compiler_class(struct compiler *c, stmt_ty s)
1512{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001514 PyObject *str;
Benjamin Petersondcaf3292009-03-03 00:54:05 +00001515 int i;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001516 asdl_seq* decos = s->v.ClassDef.decorator_list;
1517
1518 if (!compiler_decorators(c, decos))
1519 return 0;
1520
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001521 /* ultimately generate code for:
1522 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1523 where:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001524 <func> is a function/closure created from the class body;
1525 it has a single argument (__locals__) where the dict
1526 (or MutableSequence) representing the locals is passed
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001527 <name> is the class name
1528 <bases> is the positional arguments and *varargs argument
1529 <keywords> is the keyword arguments and **kwds argument
1530 This borrows from compiler_call.
1531 */
1532
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001533 /* 1. compile the class body into a code object */
1534 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1535 return 0;
1536 /* this block represents what we do in the new scope */
1537 {
1538 /* use the class name for name mangling */
1539 Py_INCREF(s->v.ClassDef.name);
Neal Norwitz36e63102008-04-01 08:08:09 +00001540 Py_XDECREF(c->u->u_private);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001541 c->u->u_private = s->v.ClassDef.name;
1542 /* force it to have one mandatory argument */
1543 c->u->u_argcount = 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001544 /* load the first argument (__locals__) ... */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001545 ADDOP_I(c, LOAD_FAST, 0);
1546 /* ... and store it into f_locals */
1547 ADDOP_IN_SCOPE(c, STORE_LOCALS);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001548 /* load (global) __name__ ... */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001549 str = PyUnicode_InternFromString("__name__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001550 if (!str || !compiler_nameop(c, str, Load)) {
1551 Py_XDECREF(str);
1552 compiler_exit_scope(c);
1553 return 0;
1554 }
1555 Py_DECREF(str);
1556 /* ... and store it as __module__ */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001557 str = PyUnicode_InternFromString("__module__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001558 if (!str || !compiler_nameop(c, str, Store)) {
1559 Py_XDECREF(str);
1560 compiler_exit_scope(c);
1561 return 0;
1562 }
1563 Py_DECREF(str);
1564 /* compile the body proper */
1565 if (!compiler_body(c, s->v.ClassDef.body)) {
1566 compiler_exit_scope(c);
1567 return 0;
1568 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001569 /* return the (empty) __class__ cell */
1570 str = PyUnicode_InternFromString("__class__");
1571 if (str == NULL) {
1572 compiler_exit_scope(c);
1573 return 0;
1574 }
1575 i = compiler_lookup_arg(c->u->u_cellvars, str);
1576 Py_DECREF(str);
1577 if (i == -1) {
1578 /* This happens when nobody references the cell */
1579 PyErr_Clear();
1580 /* Return None */
1581 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1582 }
1583 else {
1584 /* Return the cell where to store __class__ */
1585 ADDOP_I(c, LOAD_CLOSURE, i);
1586 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001587 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1588 /* create the code object */
1589 co = assemble(c, 1);
1590 }
1591 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001592 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001593 if (co == NULL)
1594 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001596 /* 2. load the 'build_class' function */
1597 ADDOP(c, LOAD_BUILD_CLASS);
1598
1599 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001600 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001601 Py_DECREF(co);
1602
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001603 /* 4. load class name */
1604 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1605
1606 /* 5. generate the rest of the code for the call */
1607 if (!compiler_call_helper(c, 2,
1608 s->v.ClassDef.bases,
1609 s->v.ClassDef.keywords,
1610 s->v.ClassDef.starargs,
1611 s->v.ClassDef.kwargs))
1612 return 0;
1613
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001614 /* 6. apply decorators */
1615 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1616 ADDOP_I(c, CALL_FUNCTION, 1);
1617 }
1618
1619 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001620 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1621 return 0;
1622 return 1;
1623}
1624
1625static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001626compiler_ifexp(struct compiler *c, expr_ty e)
1627{
1628 basicblock *end, *next;
1629
1630 assert(e->kind == IfExp_kind);
1631 end = compiler_new_block(c);
1632 if (end == NULL)
1633 return 0;
1634 next = compiler_new_block(c);
1635 if (next == NULL)
1636 return 0;
1637 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001638 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001639 VISIT(c, expr, e->v.IfExp.body);
1640 ADDOP_JREL(c, JUMP_FORWARD, end);
1641 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001642 VISIT(c, expr, e->v.IfExp.orelse);
1643 compiler_use_next_block(c, end);
1644 return 1;
1645}
1646
1647static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001648compiler_lambda(struct compiler *c, expr_ty e)
1649{
1650 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001651 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001652 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653 arguments_ty args = e->v.Lambda.args;
1654 assert(e->kind == Lambda_kind);
1655
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001656 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001657 name = PyUnicode_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001658 if (!name)
1659 return 0;
1660 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661
Guido van Rossum4f72a782006-10-27 23:31:49 +00001662 if (args->kwonlyargs) {
1663 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1664 args->kw_defaults);
1665 if (res < 0) return 0;
1666 kw_default_count = res;
1667 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668 if (args->defaults)
1669 VISIT_SEQ(c, expr, args->defaults);
1670 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1671 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001672
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001674 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001675 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson371ccfb2008-12-27 19:03:36 +00001676 if (c->u->u_ste->ste_generator) {
1677 ADDOP_IN_SCOPE(c, POP_TOP);
1678 }
1679 else {
1680 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1681 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001683 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 if (co == NULL)
1685 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686
Guido van Rossum4f72a782006-10-27 23:31:49 +00001687 arglength = asdl_seq_LEN(args->defaults);
1688 arglength |= kw_default_count << 8;
1689 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001690 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691
1692 return 1;
1693}
1694
1695static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696compiler_if(struct compiler *c, stmt_ty s)
1697{
1698 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001699 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 assert(s->kind == If_kind);
1701 end = compiler_new_block(c);
1702 if (end == NULL)
1703 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001704
1705 constant = expr_constant(s->v.If.test);
1706 /* constant = 0: "if 0"
1707 * constant = 1: "if 1", "if 2", ...
1708 * constant = -1: rest */
1709 if (constant == 0) {
1710 if (s->v.If.orelse)
1711 VISIT_SEQ(c, stmt, s->v.If.orelse);
1712 } else if (constant == 1) {
1713 VISIT_SEQ(c, stmt, s->v.If.body);
1714 } else {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001715 if (s->v.If.orelse) {
1716 next = compiler_new_block(c);
1717 if (next == NULL)
1718 return 0;
1719 }
1720 else
1721 next = end;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001722 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001723 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001724 VISIT_SEQ(c, stmt, s->v.If.body);
1725 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001726 if (s->v.If.orelse) {
1727 compiler_use_next_block(c, next);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001728 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001729 }
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001730 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731 compiler_use_next_block(c, end);
1732 return 1;
1733}
1734
1735static int
1736compiler_for(struct compiler *c, stmt_ty s)
1737{
1738 basicblock *start, *cleanup, *end;
1739
1740 start = compiler_new_block(c);
1741 cleanup = compiler_new_block(c);
1742 end = compiler_new_block(c);
1743 if (start == NULL || end == NULL || cleanup == NULL)
1744 return 0;
1745 ADDOP_JREL(c, SETUP_LOOP, end);
1746 if (!compiler_push_fblock(c, LOOP, start))
1747 return 0;
1748 VISIT(c, expr, s->v.For.iter);
1749 ADDOP(c, GET_ITER);
1750 compiler_use_next_block(c, start);
Christian Heimes2202f872008-02-06 14:31:34 +00001751 /* for expressions must be traced on each iteration,
1752 so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001753 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001754 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) {
Christian Heimes2202f872008-02-06 14:31:34 +00001799 /* while expressions must be traced on each iteration,
1800 so we need to set an extra line number. */
1801 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001802 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001803 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804 }
1805 VISIT_SEQ(c, stmt, s->v.While.body);
1806 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1807
1808 /* XXX should the two POP instructions be in a separate block
1809 if there is no else clause ?
1810 */
1811
1812 if (constant == -1) {
1813 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814 ADDOP(c, POP_BLOCK);
1815 }
1816 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001817 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 VISIT_SEQ(c, stmt, s->v.While.orelse);
1819 compiler_use_next_block(c, end);
1820
1821 return 1;
1822}
1823
1824static int
1825compiler_continue(struct compiler *c)
1826{
1827 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001828 static const char IN_FINALLY_ERROR_MSG[] =
1829 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 int i;
1831
1832 if (!c->u->u_nfblocks)
1833 return compiler_error(c, LOOP_ERROR_MSG);
1834 i = c->u->u_nfblocks - 1;
1835 switch (c->u->u_fblock[i].fb_type) {
1836 case LOOP:
1837 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1838 break;
1839 case EXCEPT:
1840 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001841 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1842 /* Prevent continue anywhere under a finally
1843 even if hidden in a sub-try or except. */
1844 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1845 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847 if (i == -1)
1848 return compiler_error(c, LOOP_ERROR_MSG);
1849 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1850 break;
1851 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001852 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853 }
1854
1855 return 1;
1856}
1857
1858/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1859
1860 SETUP_FINALLY L
1861 <code for body>
1862 POP_BLOCK
1863 LOAD_CONST <None>
1864 L: <code for finalbody>
1865 END_FINALLY
1866
1867 The special instructions use the block stack. Each block
1868 stack entry contains the instruction that created it (here
1869 SETUP_FINALLY), the level of the value stack at the time the
1870 block stack entry was created, and a label (here L).
1871
1872 SETUP_FINALLY:
1873 Pushes the current value stack level and the label
1874 onto the block stack.
1875 POP_BLOCK:
1876 Pops en entry from the block stack, and pops the value
1877 stack until its level is the same as indicated on the
1878 block stack. (The label is ignored.)
1879 END_FINALLY:
1880 Pops a variable number of entries from the *value* stack
1881 and re-raises the exception they specify. The number of
1882 entries popped depends on the (pseudo) exception type.
1883
1884 The block stack is unwound when an exception is raised:
1885 when a SETUP_FINALLY entry is found, the exception is pushed
1886 onto the value stack (and the exception condition is cleared),
1887 and the interpreter jumps to the label gotten from the block
1888 stack.
1889*/
1890
1891static int
1892compiler_try_finally(struct compiler *c, stmt_ty s)
1893{
1894 basicblock *body, *end;
1895 body = compiler_new_block(c);
1896 end = compiler_new_block(c);
1897 if (body == NULL || end == NULL)
1898 return 0;
1899
1900 ADDOP_JREL(c, SETUP_FINALLY, end);
1901 compiler_use_next_block(c, body);
1902 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1903 return 0;
1904 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1905 ADDOP(c, POP_BLOCK);
1906 compiler_pop_fblock(c, FINALLY_TRY, body);
1907
1908 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1909 compiler_use_next_block(c, end);
1910 if (!compiler_push_fblock(c, FINALLY_END, end))
1911 return 0;
1912 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1913 ADDOP(c, END_FINALLY);
1914 compiler_pop_fblock(c, FINALLY_END, end);
1915
1916 return 1;
1917}
1918
1919/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001920 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921 (The contents of the value stack is shown in [], with the top
1922 at the right; 'tb' is trace-back info, 'val' the exception's
1923 associated value, and 'exc' the exception.)
1924
1925 Value stack Label Instruction Argument
1926 [] SETUP_EXCEPT L1
1927 [] <code for S>
1928 [] POP_BLOCK
1929 [] JUMP_FORWARD L0
1930
1931 [tb, val, exc] L1: DUP )
1932 [tb, val, exc, exc] <evaluate E1> )
1933 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001934 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935 [tb, val, exc] POP
1936 [tb, val] <assign to V1> (or POP if no V1)
1937 [tb] POP
1938 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001939 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001941 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001942 .............................etc.......................
1943
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001944 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945
1946 [] L0: <next statement>
1947
1948 Of course, parts are not generated if Vi or Ei is not present.
1949*/
1950static int
1951compiler_try_except(struct compiler *c, stmt_ty s)
1952{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001953 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954 int i, n;
1955
1956 body = compiler_new_block(c);
1957 except = compiler_new_block(c);
1958 orelse = compiler_new_block(c);
1959 end = compiler_new_block(c);
1960 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1961 return 0;
1962 ADDOP_JREL(c, SETUP_EXCEPT, except);
1963 compiler_use_next_block(c, body);
1964 if (!compiler_push_fblock(c, EXCEPT, body))
1965 return 0;
1966 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1967 ADDOP(c, POP_BLOCK);
1968 compiler_pop_fblock(c, EXCEPT, body);
1969 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1970 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1971 compiler_use_next_block(c, except);
1972 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001973 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974 s->v.TryExcept.handlers, i);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001975 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001976 return compiler_error(c, "default 'except:' must be last");
Christian Heimes2202f872008-02-06 14:31:34 +00001977 c->u->u_lineno_set = 0;
1978 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001979 except = compiler_new_block(c);
1980 if (except == NULL)
1981 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001982 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001983 ADDOP(c, DUP_TOP);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001984 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001985 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001986 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001987 }
1988 ADDOP(c, POP_TOP);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001989 if (handler->v.ExceptHandler.name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00001990 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001991
1992 cleanup_end = compiler_new_block(c);
1993 cleanup_body = compiler_new_block(c);
1994 if(!(cleanup_end || cleanup_body))
1995 return 0;
1996
Neal Norwitzad74aa82008-03-31 05:14:30 +00001997 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00001998 ADDOP(c, POP_TOP);
1999
2000 /*
2001 try:
2002 # body
2003 except type as name:
2004 try:
2005 # body
2006 finally:
2007 name = None
2008 del name
2009 */
2010
2011 /* second try: */
2012 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2013 compiler_use_next_block(c, cleanup_body);
2014 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2015 return 0;
2016
2017 /* second # body */
Neal Norwitzad74aa82008-03-31 05:14:30 +00002018 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Guido van Rossumb940e112007-01-10 16:19:56 +00002019 ADDOP(c, POP_BLOCK);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002020 ADDOP(c, POP_EXCEPT);
Guido van Rossumb940e112007-01-10 16:19:56 +00002021 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2022
2023 /* finally: */
2024 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2025 compiler_use_next_block(c, cleanup_end);
2026 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2027 return 0;
2028
2029 /* name = None */
2030 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Neal Norwitzad74aa82008-03-31 05:14:30 +00002031 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002032
Guido van Rossum16be03e2007-01-10 18:51:35 +00002033 /* del name */
Neal Norwitzad74aa82008-03-31 05:14:30 +00002034 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002035
2036 ADDOP(c, END_FINALLY);
2037 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002038 }
2039 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002040 basicblock *cleanup_body;
2041
2042 cleanup_body = compiler_new_block(c);
2043 if(!cleanup_body)
2044 return 0;
2045
2046 ADDOP(c, POP_TOP);
Guido van Rossumb940e112007-01-10 16:19:56 +00002047 ADDOP(c, POP_TOP);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002048 compiler_use_next_block(c, cleanup_body);
2049 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2050 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002051 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002052 ADDOP(c, POP_EXCEPT);
2053 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002055 ADDOP_JREL(c, JUMP_FORWARD, end);
2056 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002057 }
2058 ADDOP(c, END_FINALLY);
2059 compiler_use_next_block(c, orelse);
2060 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2061 compiler_use_next_block(c, end);
2062 return 1;
2063}
2064
2065static int
2066compiler_import_as(struct compiler *c, identifier name, identifier asname)
2067{
2068 /* The IMPORT_NAME opcode was already generated. This function
2069 merely needs to bind the result to a name.
2070
2071 If there is a dot in name, we need to split it and emit a
2072 LOAD_ATTR for each name.
2073 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002074 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2075 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002076 if (dot) {
2077 /* Consume the base module name to get the first attribute */
2078 src = dot + 1;
2079 while (dot) {
2080 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002081 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002082 dot = Py_UNICODE_strchr(src, '.');
2083 attr = PyUnicode_FromUnicode(src,
2084 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002085 if (!attr)
2086 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002088 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089 src = dot + 1;
2090 }
2091 }
2092 return compiler_nameop(c, asname, Store);
2093}
2094
2095static int
2096compiler_import(struct compiler *c, stmt_ty s)
2097{
2098 /* The Import node stores a module name like a.b.c as a single
2099 string. This is convenient for all cases except
2100 import a.b.c as d
2101 where we need to parse that string to extract the individual
2102 module names.
2103 XXX Perhaps change the representation to make this case simpler?
2104 */
2105 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002108 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002110 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111
Christian Heimes217cfd12007-12-02 14:31:20 +00002112 level = PyLong_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002113 if (level == NULL)
2114 return 0;
2115
2116 ADDOP_O(c, LOAD_CONST, level, consts);
2117 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2119 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2120
2121 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002122 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002123 if (!r)
2124 return r;
2125 }
2126 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002128 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2129 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002131 tmp = PyUnicode_FromUnicode(base,
2132 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002133 r = compiler_nameop(c, tmp, Store);
2134 if (dot) {
2135 Py_DECREF(tmp);
2136 }
2137 if (!r)
2138 return r;
2139 }
2140 }
2141 return 1;
2142}
2143
2144static int
2145compiler_from_import(struct compiler *c, stmt_ty s)
2146{
2147 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148
2149 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002150 PyObject *level;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002151 static PyObject *empty_string;
2152
2153 if (!empty_string) {
2154 empty_string = PyUnicode_FromString("");
2155 if (!empty_string)
2156 return 0;
2157 }
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002159 if (!names)
2160 return 0;
2161
Christian Heimes217cfd12007-12-02 14:31:20 +00002162 level = PyLong_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002163 if (!level) {
2164 Py_DECREF(names);
2165 return 0;
2166 }
2167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 /* build up the names */
2169 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002170 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171 Py_INCREF(alias->name);
2172 PyTuple_SET_ITEM(names, i, alias->name);
2173 }
2174
Benjamin Peterson78565b22009-06-28 19:19:51 +00002175 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2176 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2177 Py_DECREF(level);
2178 Py_DECREF(names);
2179 return compiler_error(c, "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002180 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 }
2182
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002183 ADDOP_O(c, LOAD_CONST, level, consts);
2184 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002185 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002186 Py_DECREF(names);
Benjamin Peterson78565b22009-06-28 19:19:51 +00002187 if (s->v.ImportFrom.module) {
2188 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2189 }
2190 else {
2191 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2192 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002194 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002195 identifier store_name;
2196
Martin v. Löwis5b222132007-06-10 09:51:05 +00002197 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 assert(n == 1);
2199 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002200 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 }
2202
2203 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2204 store_name = alias->name;
2205 if (alias->asname)
2206 store_name = alias->asname;
2207
2208 if (!compiler_nameop(c, store_name, Store)) {
2209 Py_DECREF(names);
2210 return 0;
2211 }
2212 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002213 /* remove imported module */
2214 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002215 return 1;
2216}
2217
2218static int
2219compiler_assert(struct compiler *c, stmt_ty s)
2220{
2221 static PyObject *assertion_error = NULL;
2222 basicblock *end;
2223
2224 if (Py_OptimizeFlag)
2225 return 1;
2226 if (assertion_error == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +00002227 assertion_error = PyUnicode_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002228 if (assertion_error == NULL)
2229 return 0;
2230 }
Christian Heimes08976cb2008-03-16 00:32:36 +00002231 if (s->v.Assert.test->kind == Tuple_kind &&
2232 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2233 const char* msg =
2234 "assertion is always true, perhaps remove parentheses?";
2235 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2236 c->u->u_lineno, NULL, NULL) == -1)
2237 return 0;
2238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239 VISIT(c, expr, s->v.Assert.test);
2240 end = compiler_new_block(c);
2241 if (end == NULL)
2242 return 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002243 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2245 if (s->v.Assert.msg) {
2246 VISIT(c, expr, s->v.Assert.msg);
Collin Winter828f04a2007-08-31 00:04:24 +00002247 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002248 }
Collin Winter828f04a2007-08-31 00:04:24 +00002249 ADDOP_I(c, RAISE_VARARGS, 1);
Neal Norwitz51abbc72005-12-18 07:06:23 +00002250 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251 return 1;
2252}
2253
2254static int
2255compiler_visit_stmt(struct compiler *c, stmt_ty s)
2256{
2257 int i, n;
2258
Thomas Wouters89f507f2006-12-13 04:49:30 +00002259 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002260 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002261 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002262
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002263 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002264 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002266 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002268 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269 if (c->u->u_ste->ste_type != FunctionBlock)
2270 return compiler_error(c, "'return' outside function");
2271 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272 VISIT(c, expr, s->v.Return.value);
2273 }
2274 else
2275 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2276 ADDOP(c, RETURN_VALUE);
2277 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002278 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002279 VISIT_SEQ(c, expr, s->v.Delete.targets)
2280 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002281 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002282 n = asdl_seq_LEN(s->v.Assign.targets);
2283 VISIT(c, expr, s->v.Assign.value);
2284 for (i = 0; i < n; i++) {
2285 if (i < n - 1)
2286 ADDOP(c, DUP_TOP);
2287 VISIT(c, expr,
2288 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2289 }
2290 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002291 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002292 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002293 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002295 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002296 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002297 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002299 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 n = 0;
Collin Winter828f04a2007-08-31 00:04:24 +00002301 if (s->v.Raise.exc) {
2302 VISIT(c, expr, s->v.Raise.exc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002303 n++;
Collin Winter828f04a2007-08-31 00:04:24 +00002304 if (s->v.Raise.cause) {
2305 VISIT(c, expr, s->v.Raise.cause);
2306 n++;
2307 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 }
2309 ADDOP_I(c, RAISE_VARARGS, n);
2310 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002311 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002312 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002313 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002314 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002315 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002317 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002319 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002321 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002322 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002323 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002324 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002325 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002326 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002327 ADDOP(c, PRINT_EXPR);
2328 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002329 else if (s->v.Expr.value->kind != Str_kind &&
2330 s->v.Expr.value->kind != Num_kind) {
2331 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002332 ADDOP(c, POP_TOP);
2333 }
2334 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002335 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002336 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002337 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002338 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339 return compiler_error(c, "'break' outside loop");
2340 ADDOP(c, BREAK_LOOP);
2341 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002342 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002343 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002344 case With_kind:
2345 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002346 }
2347 return 1;
2348}
2349
2350static int
2351unaryop(unaryop_ty op)
2352{
2353 switch (op) {
2354 case Invert:
2355 return UNARY_INVERT;
2356 case Not:
2357 return UNARY_NOT;
2358 case UAdd:
2359 return UNARY_POSITIVE;
2360 case USub:
2361 return UNARY_NEGATIVE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002362 default:
2363 PyErr_Format(PyExc_SystemError,
2364 "unary op %d should not be possible", op);
2365 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002367}
2368
2369static int
2370binop(struct compiler *c, operator_ty op)
2371{
2372 switch (op) {
2373 case Add:
2374 return BINARY_ADD;
2375 case Sub:
2376 return BINARY_SUBTRACT;
2377 case Mult:
2378 return BINARY_MULTIPLY;
2379 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002380 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002381 case Mod:
2382 return BINARY_MODULO;
2383 case Pow:
2384 return BINARY_POWER;
2385 case LShift:
2386 return BINARY_LSHIFT;
2387 case RShift:
2388 return BINARY_RSHIFT;
2389 case BitOr:
2390 return BINARY_OR;
2391 case BitXor:
2392 return BINARY_XOR;
2393 case BitAnd:
2394 return BINARY_AND;
2395 case FloorDiv:
2396 return BINARY_FLOOR_DIVIDE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002397 default:
2398 PyErr_Format(PyExc_SystemError,
2399 "binary op %d should not be possible", op);
2400 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002402}
2403
2404static int
2405cmpop(cmpop_ty op)
2406{
2407 switch (op) {
2408 case Eq:
2409 return PyCmp_EQ;
2410 case NotEq:
2411 return PyCmp_NE;
2412 case Lt:
2413 return PyCmp_LT;
2414 case LtE:
2415 return PyCmp_LE;
2416 case Gt:
2417 return PyCmp_GT;
2418 case GtE:
2419 return PyCmp_GE;
2420 case Is:
2421 return PyCmp_IS;
2422 case IsNot:
2423 return PyCmp_IS_NOT;
2424 case In:
2425 return PyCmp_IN;
2426 case NotIn:
2427 return PyCmp_NOT_IN;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002428 default:
2429 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
2433static int
2434inplace_binop(struct compiler *c, operator_ty op)
2435{
2436 switch (op) {
2437 case Add:
2438 return INPLACE_ADD;
2439 case Sub:
2440 return INPLACE_SUBTRACT;
2441 case Mult:
2442 return INPLACE_MULTIPLY;
2443 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002444 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002445 case Mod:
2446 return INPLACE_MODULO;
2447 case Pow:
2448 return INPLACE_POWER;
2449 case LShift:
2450 return INPLACE_LSHIFT;
2451 case RShift:
2452 return INPLACE_RSHIFT;
2453 case BitOr:
2454 return INPLACE_OR;
2455 case BitXor:
2456 return INPLACE_XOR;
2457 case BitAnd:
2458 return INPLACE_AND;
2459 case FloorDiv:
2460 return INPLACE_FLOOR_DIVIDE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002461 default:
2462 PyErr_Format(PyExc_SystemError,
2463 "inplace binary op %d should not be possible", op);
2464 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002466}
2467
2468static int
2469compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2470{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002471 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2473
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002474 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002475 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476 /* XXX AugStore isn't used anywhere! */
2477
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002478 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002479 if (!mangled)
2480 return 0;
2481
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482 op = 0;
2483 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002484 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485 switch (scope) {
2486 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002487 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488 optype = OP_DEREF;
2489 break;
2490 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002491 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002492 optype = OP_DEREF;
2493 break;
2494 case LOCAL:
2495 if (c->u->u_ste->ste_type == FunctionBlock)
2496 optype = OP_FAST;
2497 break;
2498 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002499 if (c->u->u_ste->ste_type == FunctionBlock &&
2500 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002501 optype = OP_GLOBAL;
2502 break;
2503 case GLOBAL_EXPLICIT:
2504 optype = OP_GLOBAL;
2505 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002506 default:
2507 /* scope can be 0 */
2508 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 }
2510
2511 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002512 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002513
2514 switch (optype) {
2515 case OP_DEREF:
2516 switch (ctx) {
2517 case Load: op = LOAD_DEREF; break;
2518 case Store: op = STORE_DEREF; break;
2519 case AugLoad:
2520 case AugStore:
2521 break;
2522 case Del:
2523 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002524 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002525 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002526 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002527 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002529 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002530 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002531 PyErr_SetString(PyExc_SystemError,
2532 "param invalid for deref variable");
2533 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002534 }
2535 break;
2536 case OP_FAST:
2537 switch (ctx) {
2538 case Load: op = LOAD_FAST; break;
2539 case Store: op = STORE_FAST; break;
2540 case Del: op = DELETE_FAST; break;
2541 case AugLoad:
2542 case AugStore:
2543 break;
2544 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002545 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002546 PyErr_SetString(PyExc_SystemError,
2547 "param invalid for local variable");
2548 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002549 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002550 ADDOP_O(c, op, mangled, varnames);
2551 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002552 return 1;
2553 case OP_GLOBAL:
2554 switch (ctx) {
2555 case Load: op = LOAD_GLOBAL; break;
2556 case Store: op = STORE_GLOBAL; break;
2557 case Del: op = DELETE_GLOBAL; break;
2558 case AugLoad:
2559 case AugStore:
2560 break;
2561 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002562 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002563 PyErr_SetString(PyExc_SystemError,
2564 "param invalid for global variable");
2565 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002566 }
2567 break;
2568 case OP_NAME:
2569 switch (ctx) {
2570 case Load: op = LOAD_NAME; break;
2571 case Store: op = STORE_NAME; break;
2572 case Del: op = DELETE_NAME; break;
2573 case AugLoad:
2574 case AugStore:
2575 break;
2576 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002577 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002578 PyErr_SetString(PyExc_SystemError,
2579 "param invalid for name variable");
2580 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002581 }
2582 break;
2583 }
2584
2585 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002586 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002587 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002588 if (arg < 0)
2589 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002590 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591}
2592
2593static int
2594compiler_boolop(struct compiler *c, expr_ty e)
2595{
2596 basicblock *end;
2597 int jumpi, i, n;
2598 asdl_seq *s;
2599
2600 assert(e->kind == BoolOp_kind);
2601 if (e->v.BoolOp.op == And)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002602 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002603 else
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002604 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002606 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002607 return 0;
2608 s = e->v.BoolOp.values;
2609 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002610 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002612 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002613 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002614 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002615 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002616 compiler_use_next_block(c, end);
2617 return 1;
2618}
2619
2620static int
2621compiler_list(struct compiler *c, expr_ty e)
2622{
2623 int n = asdl_seq_LEN(e->v.List.elts);
2624 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002625 int i, seen_star = 0;
2626 for (i = 0; i < n; i++) {
2627 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2628 if (elt->kind == Starred_kind && !seen_star) {
Thomas Woutersdeef6742008-03-14 17:16:59 +00002629 if ((i >= (1 << 8)) ||
2630 (n-i-1 >= (INT_MAX >> 8)))
2631 return compiler_error(c,
2632 "too many expressions in "
2633 "star-unpacking assignment");
Guido van Rossum0368b722007-05-11 16:50:42 +00002634 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2635 seen_star = 1;
2636 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2637 } else if (elt->kind == Starred_kind) {
2638 return compiler_error(c,
2639 "two starred expressions in assignment");
2640 }
2641 }
2642 if (!seen_star) {
2643 ADDOP_I(c, UNPACK_SEQUENCE, n);
2644 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645 }
2646 VISIT_SEQ(c, expr, e->v.List.elts);
2647 if (e->v.List.ctx == Load) {
2648 ADDOP_I(c, BUILD_LIST, n);
2649 }
2650 return 1;
2651}
2652
2653static int
2654compiler_tuple(struct compiler *c, expr_ty e)
2655{
2656 int n = asdl_seq_LEN(e->v.Tuple.elts);
2657 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002658 int i, seen_star = 0;
2659 for (i = 0; i < n; i++) {
2660 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2661 if (elt->kind == Starred_kind && !seen_star) {
Thomas Woutersdeef6742008-03-14 17:16:59 +00002662 if ((i >= (1 << 8)) ||
2663 (n-i-1 >= (INT_MAX >> 8)))
2664 return compiler_error(c,
2665 "too many expressions in "
2666 "star-unpacking assignment");
Guido van Rossum0368b722007-05-11 16:50:42 +00002667 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2668 seen_star = 1;
2669 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2670 } else if (elt->kind == Starred_kind) {
2671 return compiler_error(c,
2672 "two starred expressions in assignment");
2673 }
2674 }
2675 if (!seen_star) {
2676 ADDOP_I(c, UNPACK_SEQUENCE, n);
2677 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002678 }
2679 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2680 if (e->v.Tuple.ctx == Load) {
2681 ADDOP_I(c, BUILD_TUPLE, n);
2682 }
2683 return 1;
2684}
2685
2686static int
2687compiler_compare(struct compiler *c, expr_ty e)
2688{
2689 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002690 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691
2692 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2693 VISIT(c, expr, e->v.Compare.left);
2694 n = asdl_seq_LEN(e->v.Compare.ops);
2695 assert(n > 0);
2696 if (n > 1) {
2697 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002698 if (cleanup == NULL)
2699 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002700 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002701 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002702 }
2703 for (i = 1; i < n; i++) {
2704 ADDOP(c, DUP_TOP);
2705 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002706 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002707 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002708 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002709 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002711 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002712 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002713 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002715 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002716 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002717 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 if (n > 1) {
2719 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002720 if (end == NULL)
2721 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 ADDOP_JREL(c, JUMP_FORWARD, end);
2723 compiler_use_next_block(c, cleanup);
2724 ADDOP(c, ROT_TWO);
2725 ADDOP(c, POP_TOP);
2726 compiler_use_next_block(c, end);
2727 }
2728 return 1;
2729}
2730
2731static int
2732compiler_call(struct compiler *c, expr_ty e)
2733{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002735 return compiler_call_helper(c, 0,
2736 e->v.Call.args,
2737 e->v.Call.keywords,
2738 e->v.Call.starargs,
2739 e->v.Call.kwargs);
2740}
2741
2742/* shared code between compiler_call and compiler_class */
2743static int
2744compiler_call_helper(struct compiler *c,
2745 int n, /* Args already pushed */
2746 asdl_seq *args,
2747 asdl_seq *keywords,
2748 expr_ty starargs,
2749 expr_ty kwargs)
2750{
2751 int code = 0;
2752
2753 n += asdl_seq_LEN(args);
2754 VISIT_SEQ(c, expr, args);
2755 if (keywords) {
2756 VISIT_SEQ(c, keyword, keywords);
2757 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002758 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002759 if (starargs) {
2760 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002761 code |= 1;
2762 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002763 if (kwargs) {
2764 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002765 code |= 2;
2766 }
2767 switch (code) {
2768 case 0:
2769 ADDOP_I(c, CALL_FUNCTION, n);
2770 break;
2771 case 1:
2772 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2773 break;
2774 case 2:
2775 ADDOP_I(c, CALL_FUNCTION_KW, n);
2776 break;
2777 case 3:
2778 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2779 break;
2780 }
2781 return 1;
2782}
2783
Nick Coghlan650f0d02007-04-15 12:05:43 +00002784
2785/* List and set comprehensions and generator expressions work by creating a
2786 nested function to perform the actual iteration. This means that the
2787 iteration variables don't leak into the current scope.
2788 The defined function is called immediately following its definition, with the
2789 result of that call being the result of the expression.
2790 The LC/SC version returns the populated container, while the GE version is
2791 flagged in symtable.c as a generator, so it returns the generator object
2792 when the function is called.
2793 This code *knows* that the loop cannot contain break, continue, or return,
2794 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2795
2796 Possible cleanups:
2797 - iterate over the generator sequence instead of using recursion
2798*/
2799
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800static int
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002801compiler_comprehension_generator(struct compiler *c,
Nick Coghlan650f0d02007-04-15 12:05:43 +00002802 asdl_seq *generators, int gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002803 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804{
2805 /* generate code for the iterator, then each of the ifs,
2806 and then write to the element */
2807
Nick Coghlan650f0d02007-04-15 12:05:43 +00002808 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002810 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811
2812 start = compiler_new_block(c);
2813 skip = compiler_new_block(c);
2814 if_cleanup = compiler_new_block(c);
2815 anchor = compiler_new_block(c);
2816
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002817 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002818 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002820
Nick Coghlan650f0d02007-04-15 12:05:43 +00002821 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002822
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823 if (gen_index == 0) {
2824 /* Receive outermost iter as an implicit argument */
2825 c->u->u_argcount = 1;
2826 ADDOP_I(c, LOAD_FAST, 0);
2827 }
2828 else {
2829 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002830 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 ADDOP(c, GET_ITER);
2832 }
2833 compiler_use_next_block(c, start);
2834 ADDOP_JREL(c, FOR_ITER, anchor);
2835 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002836 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002837
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002838 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002839 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002841 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002842 VISIT(c, expr, e);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002843 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844 NEXT_BLOCK(c);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002845 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002846
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002847 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002848 if (!compiler_comprehension_generator(c,
Nick Coghlan650f0d02007-04-15 12:05:43 +00002849 generators, gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002850 elt, val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002851 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852
Nick Coghlan650f0d02007-04-15 12:05:43 +00002853 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002854 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002855 /* comprehension specific code */
2856 switch (type) {
2857 case COMP_GENEXP:
2858 VISIT(c, expr, elt);
2859 ADDOP(c, YIELD_VALUE);
2860 ADDOP(c, POP_TOP);
2861 break;
2862 case COMP_LISTCOMP:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002863 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002864 ADDOP_I(c, LIST_APPEND, gen_index + 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002865 break;
2866 case COMP_SETCOMP:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002867 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002868 ADDOP_I(c, SET_ADD, gen_index + 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002869 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002870 case COMP_DICTCOMP:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002871 /* With 'd[k] = v', v is evaluated before k, so we do
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002872 the same. */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002873 VISIT(c, expr, val);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002874 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002875 ADDOP_I(c, MAP_ADD, gen_index + 1);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002876 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 default:
2878 return 0;
2879 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002880
2881 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002882 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002883 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002884 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2885 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002886
2887 return 1;
2888}
2889
2890static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002891compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002892 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002893{
2894 PyCodeObject *co = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002895 expr_ty outermost_iter;
2896
2897 outermost_iter = ((comprehension_ty)
2898 asdl_seq_GET(generators, 0))->iter;
2899
2900 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2901 goto error;
2902
2903 if (type != COMP_GENEXP) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002904 int op;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002905 switch (type) {
2906 case COMP_LISTCOMP:
2907 op = BUILD_LIST;
2908 break;
2909 case COMP_SETCOMP:
2910 op = BUILD_SET;
2911 break;
2912 case COMP_DICTCOMP:
2913 op = BUILD_MAP;
2914 break;
2915 default:
2916 PyErr_Format(PyExc_SystemError,
2917 "unknown comprehension type %d", type);
2918 goto error_in_scope;
2919 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002920
Guido van Rossum992d4a32007-07-11 13:09:30 +00002921 ADDOP_I(c, op, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002922 }
2923
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002924 if (!compiler_comprehension_generator(c, generators, 0, elt,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002925 val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002926 goto error_in_scope;
2927
2928 if (type != COMP_GENEXP) {
2929 ADDOP(c, RETURN_VALUE);
2930 }
2931
2932 co = assemble(c, 1);
2933 compiler_exit_scope(c);
2934 if (co == NULL)
2935 goto error;
2936
2937 if (!compiler_make_closure(c, co, 0))
2938 goto error;
2939 Py_DECREF(co);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002940
2941 VISIT(c, expr, outermost_iter);
2942 ADDOP(c, GET_ITER);
2943 ADDOP_I(c, CALL_FUNCTION, 1);
2944 return 1;
2945error_in_scope:
2946 compiler_exit_scope(c);
2947error:
2948 Py_XDECREF(co);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002949 return 0;
2950}
2951
2952static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002953compiler_genexp(struct compiler *c, expr_ty e)
2954{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002955 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002956 if (!name) {
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00002957 name = PyUnicode_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002958 if (!name)
2959 return 0;
2960 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002961 assert(e->kind == GeneratorExp_kind);
2962 return compiler_comprehension(c, e, COMP_GENEXP, name,
2963 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002964 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965}
2966
2967static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002968compiler_listcomp(struct compiler *c, expr_ty e)
2969{
2970 static identifier name;
2971 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002972 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002973 if (!name)
2974 return 0;
2975 }
2976 assert(e->kind == ListComp_kind);
2977 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2978 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002979 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002980}
2981
2982static int
2983compiler_setcomp(struct compiler *c, expr_ty e)
2984{
2985 static identifier name;
2986 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002987 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002988 if (!name)
2989 return 0;
2990 }
2991 assert(e->kind == SetComp_kind);
2992 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2993 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002994 e->v.SetComp.elt, NULL);
2995}
2996
2997
2998static int
2999compiler_dictcomp(struct compiler *c, expr_ty e)
3000{
3001 static identifier name;
3002 if (!name) {
Neal Norwitz41103bf2007-08-24 23:12:06 +00003003 name = PyUnicode_FromString("<dictcomp>");
Guido van Rossum992d4a32007-07-11 13:09:30 +00003004 if (!name)
3005 return 0;
3006 }
3007 assert(e->kind == DictComp_kind);
3008 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3009 e->v.DictComp.generators,
3010 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003011}
3012
3013
3014static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003015compiler_visit_keyword(struct compiler *c, keyword_ty k)
3016{
3017 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3018 VISIT(c, expr, k->value);
3019 return 1;
3020}
3021
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003022/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023 whether they are true or false.
3024
3025 Return values: 1 for true, 0 for false, -1 for non-constant.
3026 */
3027
3028static int
3029expr_constant(expr_ty e)
3030{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003031 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003032 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00003033 case Ellipsis_kind:
3034 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003035 case Num_kind:
3036 return PyObject_IsTrue(e->v.Num.n);
3037 case Str_kind:
3038 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003039 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003040 /* optimize away names that can't be reassigned */
Christian Heimes72b710a2008-05-26 13:28:38 +00003041 id = PyBytes_AS_STRING(
Guido van Rossumbdbb3952007-06-14 00:28:01 +00003042 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003043 if (strcmp(id, "True") == 0) return 1;
3044 if (strcmp(id, "False") == 0) return 0;
3045 if (strcmp(id, "None") == 0) return 0;
3046 if (strcmp(id, "__debug__") == 0)
3047 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003048 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003049 default:
3050 return -1;
3051 }
3052}
3053
Guido van Rossumc2e20742006-02-27 22:32:47 +00003054/*
3055 Implements the with statement from PEP 343.
3056
3057 The semantics outlined in that PEP are as follows:
3058
3059 with EXPR as VAR:
3060 BLOCK
3061
3062 It is implemented roughly as:
3063
Thomas Wouters477c8d52006-05-27 19:21:47 +00003064 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065 exit = context.__exit__ # not calling it
3066 value = context.__enter__()
3067 try:
3068 VAR = value # if VAR present in the syntax
3069 BLOCK
3070 finally:
3071 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003072 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003074 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003075 exit(*exc)
3076 */
3077static int
3078compiler_with(struct compiler *c, stmt_ty s)
3079{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003080 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003081
3082 assert(s->kind == With_kind);
3083
Guido van Rossumc2e20742006-02-27 22:32:47 +00003084 block = compiler_new_block(c);
3085 finally = compiler_new_block(c);
3086 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003087 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003088
Thomas Wouters477c8d52006-05-27 19:21:47 +00003089 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003090 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003091 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003092
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003093 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003094 compiler_use_next_block(c, block);
3095 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003096 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003097 }
3098
3099 if (s->v.With.optional_vars) {
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003100 VISIT(c, expr, s->v.With.optional_vars);
3101 }
3102 else {
3103 /* Discard result from context.__enter__() */
3104 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003105 }
3106
3107 /* BLOCK code */
3108 VISIT_SEQ(c, stmt, s->v.With.body);
3109
3110 /* End of try block; start the finally block */
3111 ADDOP(c, POP_BLOCK);
3112 compiler_pop_fblock(c, FINALLY_TRY, block);
3113
3114 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3115 compiler_use_next_block(c, finally);
3116 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003117 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003118
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003119 /* Finally block starts; context.__exit__ is on the stack under
3120 the exception or return information. Just issue our magic
3121 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003122 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003123
3124 /* Finally block ends. */
3125 ADDOP(c, END_FINALLY);
3126 compiler_pop_fblock(c, FINALLY_END, finally);
3127 return 1;
3128}
3129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003130static int
3131compiler_visit_expr(struct compiler *c, expr_ty e)
3132{
3133 int i, n;
3134
Thomas Wouters89f507f2006-12-13 04:49:30 +00003135 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003136 set a new line number for the next instruction.
3137 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138 if (e->lineno > c->u->u_lineno) {
3139 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003140 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003141 }
3142 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003143 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003144 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003145 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146 VISIT(c, expr, e->v.BinOp.left);
3147 VISIT(c, expr, e->v.BinOp.right);
3148 ADDOP(c, binop(c, e->v.BinOp.op));
3149 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003150 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003151 VISIT(c, expr, e->v.UnaryOp.operand);
3152 ADDOP(c, unaryop(e->v.UnaryOp.op));
3153 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003154 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003155 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003156 case IfExp_kind:
3157 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003158 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003159 n = asdl_seq_LEN(e->v.Dict.values);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003160 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003161 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003162 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003163 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003164 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003165 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Christian Heimes99170a52007-12-19 02:07:34 +00003166 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167 }
3168 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003169 case Set_kind:
3170 n = asdl_seq_LEN(e->v.Set.elts);
3171 VISIT_SEQ(c, expr, e->v.Set.elts);
3172 ADDOP_I(c, BUILD_SET, n);
3173 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003174 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003176 case ListComp_kind:
3177 return compiler_listcomp(c, e);
3178 case SetComp_kind:
3179 return compiler_setcomp(c, e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003180 case DictComp_kind:
3181 return compiler_dictcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 case Yield_kind:
3183 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003184 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185 if (e->v.Yield.value) {
3186 VISIT(c, expr, e->v.Yield.value);
3187 }
3188 else {
3189 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3190 }
3191 ADDOP(c, YIELD_VALUE);
3192 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003193 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003194 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003195 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003196 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003197 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003198 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3199 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003200 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003201 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3202 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003203 case Bytes_kind:
3204 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
Thomas Wouters00e41de2007-02-23 19:56:57 +00003205 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003206 case Ellipsis_kind:
3207 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3208 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003210 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 if (e->v.Attribute.ctx != AugStore)
3212 VISIT(c, expr, e->v.Attribute.value);
3213 switch (e->v.Attribute.ctx) {
3214 case AugLoad:
3215 ADDOP(c, DUP_TOP);
3216 /* Fall through to load */
3217 case Load:
3218 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3219 break;
3220 case AugStore:
3221 ADDOP(c, ROT_TWO);
3222 /* Fall through to save */
3223 case Store:
3224 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3225 break;
3226 case Del:
3227 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3228 break;
3229 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003230 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003231 PyErr_SetString(PyExc_SystemError,
3232 "param invalid in attribute expression");
3233 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003234 }
3235 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003236 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237 switch (e->v.Subscript.ctx) {
3238 case AugLoad:
3239 VISIT(c, expr, e->v.Subscript.value);
3240 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3241 break;
3242 case Load:
3243 VISIT(c, expr, e->v.Subscript.value);
3244 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3245 break;
3246 case AugStore:
3247 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3248 break;
3249 case Store:
3250 VISIT(c, expr, e->v.Subscript.value);
3251 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3252 break;
3253 case Del:
3254 VISIT(c, expr, e->v.Subscript.value);
3255 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3256 break;
3257 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003258 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003259 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003260 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003261 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003262 }
3263 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003264 case Starred_kind:
3265 switch (e->v.Starred.ctx) {
3266 case Store:
3267 /* In all legitimate cases, the Starred node was already replaced
3268 * by compiler_list/compiler_tuple. XXX: is that okay? */
3269 return compiler_error(c,
3270 "starred assignment target must be in a list or tuple");
3271 default:
3272 return compiler_error(c,
3273 "can use starred expression only as assignment target");
3274 }
3275 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003276 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003277 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3278 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003279 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003281 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003282 return compiler_tuple(c, e);
3283 }
3284 return 1;
3285}
3286
3287static int
3288compiler_augassign(struct compiler *c, stmt_ty s)
3289{
3290 expr_ty e = s->v.AugAssign.target;
3291 expr_ty auge;
3292
3293 assert(s->kind == AugAssign_kind);
3294
3295 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003296 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003297 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003298 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003299 if (auge == NULL)
3300 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003301 VISIT(c, expr, auge);
3302 VISIT(c, expr, s->v.AugAssign.value);
3303 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3304 auge->v.Attribute.ctx = AugStore;
3305 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306 break;
3307 case Subscript_kind:
3308 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003309 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003310 if (auge == NULL)
3311 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003312 VISIT(c, expr, auge);
3313 VISIT(c, expr, s->v.AugAssign.value);
3314 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003315 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003316 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003317 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003319 if (!compiler_nameop(c, e->v.Name.id, Load))
3320 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321 VISIT(c, expr, s->v.AugAssign.value);
3322 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3323 return compiler_nameop(c, e->v.Name.id, Store);
3324 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003325 PyErr_Format(PyExc_SystemError,
3326 "invalid node type (%d) for augmented assignment",
3327 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003328 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003329 }
3330 return 1;
3331}
3332
3333static int
3334compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3335{
3336 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003337 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3338 PyErr_SetString(PyExc_SystemError,
3339 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003340 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003341 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342 f = &c->u->u_fblock[c->u->u_nfblocks++];
3343 f->fb_type = t;
3344 f->fb_block = b;
3345 return 1;
3346}
3347
3348static void
3349compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3350{
3351 struct compiler_unit *u = c->u;
3352 assert(u->u_nfblocks > 0);
3353 u->u_nfblocks--;
3354 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3355 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3356}
3357
Thomas Wouters89f507f2006-12-13 04:49:30 +00003358static int
3359compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003360 int i;
3361 struct compiler_unit *u = c->u;
3362 for (i = 0; i < u->u_nfblocks; ++i) {
3363 if (u->u_fblock[i].fb_type == LOOP)
3364 return 1;
3365 }
3366 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003367}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003368/* Raises a SyntaxError and returns 0.
3369 If something goes wrong, a different exception may be raised.
3370*/
3371
3372static int
3373compiler_error(struct compiler *c, const char *errstr)
3374{
3375 PyObject *loc;
3376 PyObject *u = NULL, *v = NULL;
3377
3378 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3379 if (!loc) {
3380 Py_INCREF(Py_None);
3381 loc = Py_None;
3382 }
3383 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3384 Py_None, loc);
3385 if (!u)
3386 goto exit;
3387 v = Py_BuildValue("(zO)", errstr, u);
3388 if (!v)
3389 goto exit;
3390 PyErr_SetObject(PyExc_SyntaxError, v);
3391 exit:
3392 Py_DECREF(loc);
3393 Py_XDECREF(u);
3394 Py_XDECREF(v);
3395 return 0;
3396}
3397
3398static int
3399compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003400 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003401{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003402 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003403
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003404 /* XXX this code is duplicated */
3405 switch (ctx) {
3406 case AugLoad: /* fall through to Load */
3407 case Load: op = BINARY_SUBSCR; break;
3408 case AugStore:/* fall through to Store */
3409 case Store: op = STORE_SUBSCR; break;
3410 case Del: op = DELETE_SUBSCR; break;
3411 case Param:
3412 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003413 "invalid %s kind %d in subscript\n",
3414 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003415 return 0;
3416 }
3417 if (ctx == AugLoad) {
3418 ADDOP_I(c, DUP_TOPX, 2);
3419 }
3420 else if (ctx == AugStore) {
3421 ADDOP(c, ROT_THREE);
3422 }
3423 ADDOP(c, op);
3424 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003425}
3426
3427static int
3428compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3429{
3430 int n = 2;
3431 assert(s->kind == Slice_kind);
3432
3433 /* only handles the cases where BUILD_SLICE is emitted */
3434 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003435 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003436 }
3437 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003438 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003440
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003442 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003443 }
3444 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003445 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446 }
3447
3448 if (s->v.Slice.step) {
3449 n++;
3450 VISIT(c, expr, s->v.Slice.step);
3451 }
3452 ADDOP_I(c, BUILD_SLICE, n);
3453 return 1;
3454}
3455
3456static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3458 expr_context_ty ctx)
3459{
3460 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461 case Slice_kind:
3462 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463 case Index_kind:
3464 VISIT(c, expr, s->v.Index.value);
3465 break;
3466 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003467 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003468 PyErr_SetString(PyExc_SystemError,
3469 "extended slice invalid in nested slice");
3470 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003471 }
3472 return 1;
3473}
3474
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003475static int
3476compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3477{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003478 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003479 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003480 case Index_kind:
3481 kindname = "index";
3482 if (ctx != AugStore) {
3483 VISIT(c, expr, s->v.Index.value);
3484 }
3485 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003486 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003487 kindname = "slice";
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003488 if (ctx != AugStore) {
3489 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490 return 0;
3491 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003492 break;
3493 case ExtSlice_kind:
3494 kindname = "extended slice";
3495 if (ctx != AugStore) {
3496 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3497 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003498 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003499 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003500 if (!compiler_visit_nested_slice(c, sub, ctx))
3501 return 0;
3502 }
3503 ADDOP_I(c, BUILD_TUPLE, n);
3504 }
3505 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003506 default:
3507 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003508 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003509 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003511 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003512}
3513
Thomas Wouters89f507f2006-12-13 04:49:30 +00003514/* End of the compiler section, beginning of the assembler section */
3515
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003516/* do depth-first search of basic block graph, starting with block.
3517 post records the block indices in post-order.
3518
3519 XXX must handle implicit jumps from one block to next
3520*/
3521
Thomas Wouters89f507f2006-12-13 04:49:30 +00003522struct assembler {
3523 PyObject *a_bytecode; /* string containing bytecode */
3524 int a_offset; /* offset into bytecode */
3525 int a_nblocks; /* number of reachable blocks */
3526 basicblock **a_postorder; /* list of blocks in dfs postorder */
3527 PyObject *a_lnotab; /* string containing lnotab */
3528 int a_lnotab_off; /* offset into lnotab */
3529 int a_lineno; /* last lineno of emitted instruction */
3530 int a_lineno_off; /* bytecode offset of last lineno */
3531};
3532
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533static void
3534dfs(struct compiler *c, basicblock *b, struct assembler *a)
3535{
3536 int i;
3537 struct instr *instr = NULL;
3538
3539 if (b->b_seen)
3540 return;
3541 b->b_seen = 1;
3542 if (b->b_next != NULL)
3543 dfs(c, b->b_next, a);
3544 for (i = 0; i < b->b_iused; i++) {
3545 instr = &b->b_instr[i];
3546 if (instr->i_jrel || instr->i_jabs)
3547 dfs(c, instr->i_target, a);
3548 }
3549 a->a_postorder[a->a_nblocks++] = b;
3550}
3551
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003552static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003553stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3554{
3555 int i;
3556 struct instr *instr;
3557 if (b->b_seen || b->b_startdepth >= depth)
3558 return maxdepth;
3559 b->b_seen = 1;
3560 b->b_startdepth = depth;
3561 for (i = 0; i < b->b_iused; i++) {
3562 instr = &b->b_instr[i];
3563 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3564 if (depth > maxdepth)
3565 maxdepth = depth;
3566 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3567 if (instr->i_jrel || instr->i_jabs) {
3568 maxdepth = stackdepth_walk(c, instr->i_target,
3569 depth, maxdepth);
3570 if (instr->i_opcode == JUMP_ABSOLUTE ||
3571 instr->i_opcode == JUMP_FORWARD) {
3572 goto out; /* remaining code is dead */
3573 }
3574 }
3575 }
3576 if (b->b_next)
3577 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3578out:
3579 b->b_seen = 0;
3580 return maxdepth;
3581}
3582
3583/* Find the flow path that needs the largest stack. We assume that
3584 * cycles in the flow graph have no net effect on the stack depth.
3585 */
3586static int
3587stackdepth(struct compiler *c)
3588{
3589 basicblock *b, *entryblock;
3590 entryblock = NULL;
3591 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3592 b->b_seen = 0;
3593 b->b_startdepth = INT_MIN;
3594 entryblock = b;
3595 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003596 if (!entryblock)
3597 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598 return stackdepth_walk(c, entryblock, 0, 0);
3599}
3600
3601static int
3602assemble_init(struct assembler *a, int nblocks, int firstlineno)
3603{
3604 memset(a, 0, sizeof(struct assembler));
3605 a->a_lineno = firstlineno;
Christian Heimes72b710a2008-05-26 13:28:38 +00003606 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607 if (!a->a_bytecode)
3608 return 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003609 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003610 if (!a->a_lnotab)
3611 return 0;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003612 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3613 PyErr_NoMemory();
3614 return 0;
3615 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003616 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003617 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003618 if (!a->a_postorder) {
3619 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003620 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622 return 1;
3623}
3624
3625static void
3626assemble_free(struct assembler *a)
3627{
3628 Py_XDECREF(a->a_bytecode);
3629 Py_XDECREF(a->a_lnotab);
3630 if (a->a_postorder)
3631 PyObject_Free(a->a_postorder);
3632}
3633
3634/* Return the size of a basic block in bytes. */
3635
3636static int
3637instrsize(struct instr *instr)
3638{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003639 if (!instr->i_hasarg)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003640 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003641 if (instr->i_oparg > 0xffff)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003642 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3643 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644}
3645
3646static int
3647blocksize(basicblock *b)
3648{
3649 int i;
3650 int size = 0;
3651
3652 for (i = 0; i < b->b_iused; i++)
3653 size += instrsize(&b->b_instr[i]);
3654 return size;
3655}
3656
3657/* All about a_lnotab.
3658
3659c_lnotab is an array of unsigned bytes disguised as a Python string.
3660It is used to map bytecode offsets to source code line #s (when needed
3661for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003662
Tim Peters2a7f3842001-06-09 09:26:21 +00003663The array is conceptually a list of
3664 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003665pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003666
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003667 byte code offset source code line number
3668 0 1
3669 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003670 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003671 350 307
3672 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003673
3674The first trick is that these numbers aren't stored, only the increments
3675from one row to the next (this doesn't really work, but it's a start):
3676
3677 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3678
3679The second trick is that an unsigned byte can't hold negative values, or
3680values larger than 255, so (a) there's a deep assumption that byte code
3681offsets and their corresponding line #s both increase monotonically, and (b)
3682if at least one column jumps by more than 255 from one row to the next, more
3683than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003684from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003685part. A user of c_lnotab desiring to find the source line number
3686corresponding to a bytecode address A should do something like this
3687
3688 lineno = addr = 0
3689 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003690 addr += addr_incr
3691 if addr > A:
3692 return lineno
3693 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003694
3695In order for this to work, when the addr field increments by more than 255,
3696the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003697increment is < 256. So, in the example above, assemble_lnotab (it used
3698to be called com_set_lineno) should not (as was actually done until 2.2)
3699expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003700 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003701*/
3702
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003703static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003704assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003705{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003706 int d_bytecode, d_lineno;
3707 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003708 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709
3710 d_bytecode = a->a_offset - a->a_lineno_off;
3711 d_lineno = i->i_lineno - a->a_lineno;
3712
3713 assert(d_bytecode >= 0);
3714 assert(d_lineno >= 0);
3715
Christian Heimes2202f872008-02-06 14:31:34 +00003716 if(d_bytecode == 0 && d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003717 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003718
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003720 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003721 nbytes = a->a_lnotab_off + 2 * ncodes;
Christian Heimes72b710a2008-05-26 13:28:38 +00003722 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003723 if (nbytes >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003724 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003725 len = nbytes;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003726 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003727 len *= 2;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003728 else {
3729 PyErr_NoMemory();
3730 return 0;
3731 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003732 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003733 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003734 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003735 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003736 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003737 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003738 *lnotab++ = 255;
3739 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003740 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003741 d_bytecode -= ncodes * 255;
3742 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003743 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003744 assert(d_bytecode <= 255);
3745 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003746 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003747 nbytes = a->a_lnotab_off + 2 * ncodes;
Christian Heimes72b710a2008-05-26 13:28:38 +00003748 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003749 if (nbytes >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003750 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003751 len = nbytes;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003752 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003753 len *= 2;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003754 else {
3755 PyErr_NoMemory();
3756 return 0;
3757 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003758 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003760 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003761 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003762 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003763 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003764 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003765 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003766 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003767 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003768 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003769 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003770 d_lineno -= ncodes * 255;
3771 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003772 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003773
Christian Heimes72b710a2008-05-26 13:28:38 +00003774 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003775 if (a->a_lnotab_off + 2 >= len) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003776 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003777 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003778 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003779 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003780 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003781
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782 a->a_lnotab_off += 2;
3783 if (d_bytecode) {
3784 *lnotab++ = d_bytecode;
3785 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003786 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003787 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 *lnotab++ = 0;
3789 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003791 a->a_lineno = i->i_lineno;
3792 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003793 return 1;
3794}
3795
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003796/* assemble_emit()
3797 Extend the bytecode with a new instruction.
3798 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003799*/
3800
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003801static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003803{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003804 int size, arg = 0, ext = 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003805 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 char *code;
3807
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003808 size = instrsize(i);
3809 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003811 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003812 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003813 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003814 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003815 if (a->a_offset + size >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003816 if (len > PY_SSIZE_T_MAX / 2)
3817 return 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003818 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003819 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003820 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003821 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003823 if (size == 6) {
3824 assert(i->i_hasarg);
3825 *code++ = (char)EXTENDED_ARG;
3826 *code++ = ext & 0xff;
3827 *code++ = ext >> 8;
3828 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003829 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003831 if (i->i_hasarg) {
3832 assert(size == 3 || size == 6);
3833 *code++ = arg & 0xff;
3834 *code++ = arg >> 8;
3835 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003836 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003837}
3838
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003839static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003841{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003843 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003844 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003845
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846 /* Compute the size of each block and fixup jump args.
3847 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003848start:
3849 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003851 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003852 bsize = blocksize(b);
3853 b->b_offset = totsize;
3854 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003855 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003856 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003857 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3858 bsize = b->b_offset;
3859 for (i = 0; i < b->b_iused; i++) {
3860 struct instr *instr = &b->b_instr[i];
3861 /* Relative jumps are computed relative to
3862 the instruction pointer after fetching
3863 the jump instruction.
3864 */
3865 bsize += instrsize(instr);
3866 if (instr->i_jabs)
3867 instr->i_oparg = instr->i_target->b_offset;
3868 else if (instr->i_jrel) {
3869 int delta = instr->i_target->b_offset - bsize;
3870 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003871 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003872 else
3873 continue;
3874 if (instr->i_oparg > 0xffff)
3875 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003876 }
3877 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003878
3879 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003880 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003881 with a better solution.
3882
3883 In the meantime, should the goto be dropped in favor
3884 of a loop?
3885
3886 The issue is that in the first loop blocksize() is called
3887 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003888 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003889 i_oparg is calculated in the second loop above.
3890
3891 So we loop until we stop seeing new EXTENDED_ARGs.
3892 The only EXTENDED_ARGs that could be popping up are
3893 ones in jump instructions. So this should converge
3894 fairly quickly.
3895 */
3896 if (last_extended_arg_count != extended_arg_count) {
3897 last_extended_arg_count = extended_arg_count;
3898 goto start;
3899 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003900}
3901
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003902static PyObject *
3903dict_keys_inorder(PyObject *dict, int offset)
3904{
3905 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003906 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003907
3908 tuple = PyTuple_New(size);
3909 if (tuple == NULL)
3910 return NULL;
3911 while (PyDict_Next(dict, &pos, &k, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003912 i = PyLong_AS_LONG(v);
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003913 /* The keys of the dictionary are tuples. (see compiler_add_o)
3914 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003915 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003916 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003917 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003918 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003919 PyTuple_SET_ITEM(tuple, i - offset, k);
3920 }
3921 return tuple;
3922}
3923
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003924static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003925compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003926{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927 PySTEntryObject *ste = c->u->u_ste;
3928 int flags = 0, n;
3929 if (ste->ste_type != ModuleBlock)
3930 flags |= CO_NEWLOCALS;
3931 if (ste->ste_type == FunctionBlock) {
3932 if (!ste->ste_unoptimized)
3933 flags |= CO_OPTIMIZED;
3934 if (ste->ste_nested)
3935 flags |= CO_NESTED;
3936 if (ste->ste_generator)
3937 flags |= CO_GENERATOR;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003938 if (ste->ste_varargs)
3939 flags |= CO_VARARGS;
3940 if (ste->ste_varkeywords)
3941 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003942 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003943
3944 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003945 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003946
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003947 n = PyDict_Size(c->u->u_freevars);
3948 if (n < 0)
3949 return -1;
3950 if (n == 0) {
3951 n = PyDict_Size(c->u->u_cellvars);
3952 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003953 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954 if (n == 0) {
3955 flags |= CO_NOFREE;
3956 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003957 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003958
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003959 return flags;
3960}
3961
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962static PyCodeObject *
3963makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003964{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965 PyObject *tmp;
3966 PyCodeObject *co = NULL;
3967 PyObject *consts = NULL;
3968 PyObject *names = NULL;
3969 PyObject *varnames = NULL;
3970 PyObject *filename = NULL;
3971 PyObject *name = NULL;
3972 PyObject *freevars = NULL;
3973 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003974 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003975 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977 tmp = dict_keys_inorder(c->u->u_consts, 0);
3978 if (!tmp)
3979 goto error;
3980 consts = PySequence_List(tmp); /* optimize_code requires a list */
3981 Py_DECREF(tmp);
3982
3983 names = dict_keys_inorder(c->u->u_names, 0);
3984 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3985 if (!consts || !names || !varnames)
3986 goto error;
3987
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003988 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3989 if (!cellvars)
3990 goto error;
3991 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3992 if (!freevars)
3993 goto error;
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003994 filename = PyUnicode_DecodeFSDefault(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003995 if (!filename)
3996 goto error;
3997
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003998 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003999 flags = compute_code_flags(c);
4000 if (flags < 0)
4001 goto error;
4002
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004003 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004004 if (!bytecode)
4005 goto error;
4006
4007 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4008 if (!tmp)
4009 goto error;
4010 Py_DECREF(consts);
4011 consts = tmp;
4012
Guido van Rossum4f72a782006-10-27 23:31:49 +00004013 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4014 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004015 bytecode, consts, names, varnames,
4016 freevars, cellvars,
4017 filename, c->u->u_name,
4018 c->u->u_firstlineno,
4019 a->a_lnotab);
4020 error:
4021 Py_XDECREF(consts);
4022 Py_XDECREF(names);
4023 Py_XDECREF(varnames);
4024 Py_XDECREF(filename);
4025 Py_XDECREF(name);
4026 Py_XDECREF(freevars);
4027 Py_XDECREF(cellvars);
4028 Py_XDECREF(bytecode);
4029 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004030}
4031
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004032
4033/* For debugging purposes only */
4034#if 0
4035static void
4036dump_instr(const struct instr *i)
4037{
4038 const char *jrel = i->i_jrel ? "jrel " : "";
4039 const char *jabs = i->i_jabs ? "jabs " : "";
4040 char arg[128];
4041
4042 *arg = '\0';
4043 if (i->i_hasarg)
4044 sprintf(arg, "arg: %d ", i->i_oparg);
4045
4046 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4047 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4048}
4049
4050static void
4051dump_basicblock(const basicblock *b)
4052{
4053 const char *seen = b->b_seen ? "seen " : "";
4054 const char *b_return = b->b_return ? "return " : "";
4055 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4056 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4057 if (b->b_instr) {
4058 int i;
4059 for (i = 0; i < b->b_iused; i++) {
4060 fprintf(stderr, " [%02d] ", i);
4061 dump_instr(b->b_instr + i);
4062 }
4063 }
4064}
4065#endif
4066
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004067static PyCodeObject *
4068assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004069{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070 basicblock *b, *entryblock;
4071 struct assembler a;
4072 int i, j, nblocks;
4073 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004074
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004075 /* Make sure every block that falls off the end returns None.
4076 XXX NEXT_BLOCK() isn't quite right, because if the last
4077 block ends with a jump or return b_next shouldn't set.
4078 */
4079 if (!c->u->u_curblock->b_return) {
4080 NEXT_BLOCK(c);
4081 if (addNone)
4082 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4083 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004084 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004086 nblocks = 0;
4087 entryblock = NULL;
4088 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4089 nblocks++;
4090 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004091 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004092
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004093 /* Set firstlineno if it wasn't explicitly set. */
4094 if (!c->u->u_firstlineno) {
4095 if (entryblock && entryblock->b_instr)
4096 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4097 else
4098 c->u->u_firstlineno = 1;
4099 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004100 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4101 goto error;
4102 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004105 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004107 /* Emit code in reverse postorder from dfs. */
4108 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004109 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004110 for (j = 0; j < b->b_iused; j++)
4111 if (!assemble_emit(&a, &b->b_instr[j]))
4112 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004113 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004114
Christian Heimes72b710a2008-05-26 13:28:38 +00004115 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116 goto error;
Christian Heimes72b710a2008-05-26 13:28:38 +00004117 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120 co = makecode(c, &a);
4121 error:
4122 assemble_free(&a);
4123 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004124}