blob: c78949d887873f8ed617b358d2376b00cf84220e [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 */
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 int u_tmpname; /* temporary variables for list comps */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
122 int u_nfblocks;
123 struct fblockinfo u_fblock[CO_MAXBLOCKS];
124
125 int u_firstlineno; /* the first lineno of the block */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000126 int u_lineno; /* the lineno for the current stmt */
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000127 int u_lineno_set; /* boolean to indicate whether instr
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128 has been generated with current lineno */
129};
130
131/* This struct captures the global state of a compilation.
132
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000133The u pointer points to the current compilation unit, while units
134for enclosing blocks are stored in c_stack. The u and c_stack are
135managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136*/
137
138struct compiler {
139 const char *c_filename;
140 struct symtable *c_st;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141 PyFutureFeatures *c_future; /* pointer to module's __future__ */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142 PyCompilerFlags *c_flags;
143
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144 int c_interactive; /* true if in interactive mode */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000145 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149 char *c_encoding; /* source encoding (a borrowed reference) */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000150 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151};
152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153static int compiler_enter_scope(struct compiler *, identifier, void *, int);
154static void compiler_free(struct compiler *);
155static basicblock *compiler_new_block(struct compiler *);
156static int compiler_next_instr(struct compiler *, basicblock *);
157static int compiler_addop(struct compiler *, int);
158static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
159static int compiler_addop_i(struct compiler *, int, int);
160static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161static basicblock *compiler_use_new_block(struct compiler *);
162static int compiler_error(struct compiler *, const char *);
163static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
164
165static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
166static int compiler_visit_stmt(struct compiler *, stmt_ty);
167static int compiler_visit_keyword(struct compiler *, keyword_ty);
168static int compiler_visit_expr(struct compiler *, expr_ty);
169static int compiler_augassign(struct compiler *, stmt_ty);
170static int compiler_visit_slice(struct compiler *, slice_ty,
171 expr_context_ty);
172
173static int compiler_push_fblock(struct compiler *, enum fblocktype,
174 basicblock *);
175static void compiler_pop_fblock(struct compiler *, enum fblocktype,
176 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000177/* Returns true if there is a loop on the fblock stack. */
178static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000179
180static int inplace_binop(struct compiler *, operator_ty);
181static int expr_constant(expr_ty e);
182
Guido van Rossumc2e20742006-02-27 22:32:47 +0000183static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000184static int compiler_call_helper(struct compiler *c, int n,
185 asdl_seq *args,
186 asdl_seq *keywords,
187 expr_ty starargs,
188 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190static PyCodeObject *assemble(struct compiler *, int addNone);
191static PyObject *__doc__;
192
Benjamin Petersonb173f782009-05-05 22:31:58 +0000193#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000197{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000198 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000200 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
201 Py_UNICODE *buffer;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202 size_t nlen, plen;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000203 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000204 name == NULL || name[0] != '_' || name[1] != '_') {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000205 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206 return ident;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000207 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000208 p = PyUnicode_AS_UNICODE(privateobj);
209 nlen = Py_UNICODE_strlen(name);
Guido van Rossumd8faa362007-04-27 19:54:29 +0000210 /* Don't mangle __id__ or names with dots.
211
212 The only time a name with a dot can occur is when
213 we are compiling an import statement that has a
214 package name.
215
216 TODO(jhylton): Decide whether we want to support
217 mangling of the module name, e.g. __M.X.
218 */
219 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
Martin v. Löwis5b222132007-06-10 09:51:05 +0000220 || Py_UNICODE_strchr(name, '.')) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000221 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222 return ident; /* Don't mangle __whatever__ */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000223 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224 /* Strip leading underscores from class name */
225 while (*p == '_')
226 p++;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000227 if (*p == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000228 Py_INCREF(ident);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 return ident; /* Don't mangle if class is just underscores */
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000230 }
Martin v. Löwis5b222132007-06-10 09:51:05 +0000231 plen = Py_UNICODE_strlen(p);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000232
233 assert(1 <= PY_SSIZE_T_MAX - nlen);
234 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
235
Martin v. Löwis5b222132007-06-10 09:51:05 +0000236 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000237 if (!ident)
238 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000239 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
Martin v. Löwis5b222132007-06-10 09:51:05 +0000240 buffer = PyUnicode_AS_UNICODE(ident);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000241 buffer[0] = '_';
Martin v. Löwis5b222132007-06-10 09:51:05 +0000242 Py_UNICODE_strncpy(buffer+1, p, plen);
243 Py_UNICODE_strcpy(buffer+1+plen, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000245}
246
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247static int
248compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000249{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000251
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252 c->c_stack = PyList_New(0);
253 if (!c->c_stack)
254 return 0;
255
256 return 1;
257}
258
259PyCodeObject *
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000260PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000261 PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262{
263 struct compiler c;
264 PyCodeObject *co = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000265 PyCompilerFlags local_flags;
266 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000268 if (!__doc__) {
Martin v. Löwis5b222132007-06-10 09:51:05 +0000269 __doc__ = PyUnicode_InternFromString("__doc__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000270 if (!__doc__)
271 return NULL;
272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
274 if (!compiler_init(&c))
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000275 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000276 c.c_filename = filename;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000277 c.c_arena = arena;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278 c.c_future = PyFuture_FromAST(mod, filename);
279 if (c.c_future == NULL)
Thomas Wouters1175c432006-02-27 22:49:54 +0000280 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000281 if (!flags) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000282 local_flags.cf_flags = 0;
283 flags = &local_flags;
284 }
285 merged = c.c_future->ff_features | flags->cf_flags;
286 c.c_future->ff_features = merged;
287 flags->cf_flags = merged;
288 c.c_flags = flags;
289 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290
291 c.c_st = PySymtable_Build(mod, filename, c.c_future);
292 if (c.c_st == NULL) {
293 if (!PyErr_Occurred())
294 PyErr_SetString(PyExc_SystemError, "no symtable");
Thomas Wouters1175c432006-02-27 22:49:54 +0000295 goto finally;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000296 }
297
298 /* XXX initialize to NULL for now, need to handle */
299 c.c_encoding = NULL;
300
301 co = compiler_mod(&c, mod);
302
Thomas Wouters1175c432006-02-27 22:49:54 +0000303 finally:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304 compiler_free(&c);
Thomas Woutersbfe51ea2006-02-27 22:48:55 +0000305 assert(co || PyErr_Occurred());
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000306 return co;
307}
308
309PyCodeObject *
310PyNode_Compile(struct _node *n, const char *filename)
311{
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000312 PyCodeObject *co = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000313 mod_ty mod;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000314 PyArena *arena = PyArena_New();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000315 if (!arena)
316 return NULL;
317 mod = PyAST_FromNode(n, NULL, filename, arena);
Neal Norwitzadb69fc2005-12-17 20:54:49 +0000318 if (mod)
319 co = PyAST_Compile(mod, filename, NULL, arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000320 PyArena_Free(arena);
Raymond Hettinger37a724d2003-09-15 21:43:16 +0000321 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000322}
323
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000324static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000326{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000327 if (c->c_st)
328 PySymtable_Free(c->c_st);
329 if (c->c_future)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 PyObject_Free(c->c_future);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000332}
333
Guido van Rossum79f25d91997-04-29 20:08:16 +0000334static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000335list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000336{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000337 Py_ssize_t i, n;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 PyObject *v, *k;
339 PyObject *dict = PyDict_New();
340 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000341
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342 n = PyList_Size(list);
343 for (i = 0; i < n; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000344 v = PyLong_FromLong(i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 if (!v) {
346 Py_DECREF(dict);
347 return NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000348 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000349 k = PyList_GET_ITEM(list, i);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000350 k = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
352 Py_XDECREF(k);
353 Py_DECREF(v);
354 Py_DECREF(dict);
355 return NULL;
356 }
Neal Norwitz4737b232005-11-19 23:58:29 +0000357 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358 Py_DECREF(v);
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000359 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360 return dict;
361}
362
363/* Return new dict containing names from src that match scope(s).
364
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000365src is a symbol table dictionary. If the scope of a name matches
366either scope_type or flag is set, insert it into the new dict. The
367values are integers, starting at offset and increasing by one for
368each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369*/
370
371static PyObject *
372dictbytype(PyObject *src, int scope_type, int flag, int offset)
373{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000374 Py_ssize_t pos = 0, i = offset, scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000375 PyObject *k, *v, *dest = PyDict_New();
376
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000377 assert(offset >= 0);
378 if (dest == NULL)
379 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380
381 while (PyDict_Next(src, &pos, &k, &v)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000382 /* XXX this should probably be a macro in symtable.h */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000383 long vi;
Christian Heimes217cfd12007-12-02 14:31:20 +0000384 assert(PyLong_Check(v));
385 vi = PyLong_AS_LONG(v);
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000386 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000387
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000388 if (scope == scope_type || vi & flag) {
Christian Heimes217cfd12007-12-02 14:31:20 +0000389 PyObject *tuple, *item = PyLong_FromLong(i);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000390 if (item == NULL) {
391 Py_DECREF(dest);
392 return NULL;
393 }
394 i++;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000395 tuple = PyTuple_Pack(2, k, k->ob_type);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000396 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
397 Py_DECREF(item);
398 Py_DECREF(dest);
399 Py_XDECREF(tuple);
400 return NULL;
401 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402 Py_DECREF(item);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000403 Py_DECREF(tuple);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000404 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000405 }
406 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000407}
408
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409static void
410compiler_unit_check(struct compiler_unit *u)
411{
412 basicblock *block;
413 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Christian Heimes77c02eb2008-02-09 02:18:51 +0000414 assert((void *)block != (void *)0xcbcbcbcb);
415 assert((void *)block != (void *)0xfbfbfbfb);
416 assert((void *)block != (void *)0xdbdbdbdb);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000417 if (block->b_instr != NULL) {
418 assert(block->b_ialloc > 0);
419 assert(block->b_iused > 0);
420 assert(block->b_ialloc >= block->b_iused);
421 }
422 else {
423 assert (block->b_iused == 0);
424 assert (block->b_ialloc == 0);
425 }
426 }
427}
428
429static void
430compiler_unit_free(struct compiler_unit *u)
431{
432 basicblock *b, *next;
433
434 compiler_unit_check(u);
435 b = u->u_blocks;
436 while (b != NULL) {
437 if (b->b_instr)
438 PyObject_Free((void *)b->b_instr);
439 next = b->b_list;
440 PyObject_Free((void *)b);
441 b = next;
442 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000443 Py_CLEAR(u->u_ste);
444 Py_CLEAR(u->u_name);
445 Py_CLEAR(u->u_consts);
446 Py_CLEAR(u->u_names);
447 Py_CLEAR(u->u_varnames);
448 Py_CLEAR(u->u_freevars);
449 Py_CLEAR(u->u_cellvars);
450 Py_CLEAR(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000451 PyObject_Free(u);
452}
453
454static int
455compiler_enter_scope(struct compiler *c, identifier name, void *key,
456 int lineno)
457{
458 struct compiler_unit *u;
459
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000460 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000461 struct compiler_unit));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000462 if (!u) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000463 PyErr_NoMemory();
464 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000465 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000466 memset(u, 0, sizeof(struct compiler_unit));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467 u->u_argcount = 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +0000468 u->u_kwonlyargcount = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469 u->u_ste = PySymtable_Lookup(c->c_st, key);
470 if (!u->u_ste) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000471 compiler_unit_free(u);
472 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473 }
474 Py_INCREF(name);
475 u->u_name = name;
476 u->u_varnames = list2dict(u->u_ste->ste_varnames);
477 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000478 if (!u->u_varnames || !u->u_cellvars) {
479 compiler_unit_free(u);
480 return 0;
481 }
482
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000483 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000484 PyDict_Size(u->u_cellvars));
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000485 if (!u->u_freevars) {
486 compiler_unit_free(u);
487 return 0;
488 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489
490 u->u_blocks = NULL;
491 u->u_tmpname = 0;
492 u->u_nfblocks = 0;
493 u->u_firstlineno = lineno;
494 u->u_lineno = 0;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000495 u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496 u->u_consts = PyDict_New();
497 if (!u->u_consts) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000498 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499 return 0;
500 }
501 u->u_names = PyDict_New();
502 if (!u->u_names) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000503 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504 return 0;
505 }
506
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000507 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508
509 /* Push the old compiler_unit on the stack. */
510 if (c->u) {
Benjamin Petersonb173f782009-05-05 22:31:58 +0000511 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
512 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
513 Py_XDECREF(capsule);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000514 compiler_unit_free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000515 return 0;
516 }
Benjamin Petersonb173f782009-05-05 22:31:58 +0000517 Py_DECREF(capsule);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000518 u->u_private = c->u->u_private;
519 Py_XINCREF(u->u_private);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000520 }
521 c->u = u;
522
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000523 c->c_nestlevel++;
Martin v. Löwis94962612006-01-02 21:15:05 +0000524 if (compiler_use_new_block(c) == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525 return 0;
526
527 return 1;
528}
529
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000530static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531compiler_exit_scope(struct compiler *c)
532{
533 int n;
Benjamin Petersonb173f782009-05-05 22:31:58 +0000534 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000535
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000536 c->c_nestlevel--;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537 compiler_unit_free(c->u);
538 /* Restore c->u to the parent unit. */
539 n = PyList_GET_SIZE(c->c_stack) - 1;
540 if (n >= 0) {
Benjamin Petersonb173f782009-05-05 22:31:58 +0000541 capsule = PyList_GET_ITEM(c->c_stack, n);
542 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000543 assert(c->u);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000544 /* we are deleting from a list so this really shouldn't fail */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000545 if (PySequence_DelItem(c->c_stack, n) < 0)
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000546 Py_FatalError("compiler_exit_scope()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000547 compiler_unit_check(c->u);
548 }
549 else
550 c->u = NULL;
551
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552}
553
Guido van Rossumc2e20742006-02-27 22:32:47 +0000554/* Allocate a new "anonymous" local variable.
555 Used by list comprehensions and with statements.
556*/
557
558static PyObject *
559compiler_new_tmpname(struct compiler *c)
560{
561 char tmpname[256];
562 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->u->u_tmpname);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000563 return PyUnicode_FromString(tmpname);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000564}
565
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566/* Allocate a new block and return a pointer to it.
567 Returns NULL on error.
568*/
569
570static basicblock *
571compiler_new_block(struct compiler *c)
572{
573 basicblock *b;
574 struct compiler_unit *u;
575
576 u = c->u;
577 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
Neal Norwitz87b801c2005-12-18 04:42:47 +0000578 if (b == NULL) {
579 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580 return NULL;
Neal Norwitz87b801c2005-12-18 04:42:47 +0000581 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000582 memset((void *)b, 0, sizeof(basicblock));
Thomas Wouters89f507f2006-12-13 04:49:30 +0000583 /* Extend the singly linked list of blocks with new block. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584 b->b_list = u->u_blocks;
585 u->u_blocks = b;
586 return b;
587}
588
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000589static basicblock *
590compiler_use_new_block(struct compiler *c)
591{
592 basicblock *block = compiler_new_block(c);
593 if (block == NULL)
594 return NULL;
595 c->u->u_curblock = block;
596 return block;
597}
598
599static basicblock *
600compiler_next_block(struct compiler *c)
601{
602 basicblock *block = compiler_new_block(c);
603 if (block == NULL)
604 return NULL;
605 c->u->u_curblock->b_next = block;
606 c->u->u_curblock = block;
607 return block;
608}
609
610static basicblock *
611compiler_use_next_block(struct compiler *c, basicblock *block)
612{
613 assert(block != NULL);
614 c->u->u_curblock->b_next = block;
615 c->u->u_curblock = block;
616 return block;
617}
618
619/* Returns the offset of the next instruction in the current block's
620 b_instr array. Resizes the b_instr as necessary.
621 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000622*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623
624static int
625compiler_next_instr(struct compiler *c, basicblock *b)
626{
627 assert(b != NULL);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000628 if (b->b_instr == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 b->b_instr = (struct instr *)PyObject_Malloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000630 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631 if (b->b_instr == NULL) {
632 PyErr_NoMemory();
633 return -1;
634 }
635 b->b_ialloc = DEFAULT_BLOCK_SIZE;
636 memset((char *)b->b_instr, 0,
637 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000638 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 else if (b->b_iused == b->b_ialloc) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 struct instr *tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000641 size_t oldsize, newsize;
642 oldsize = b->b_ialloc * sizeof(struct instr);
643 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000644
645 if (oldsize > (PY_SIZE_MAX >> 1)) {
646 PyErr_NoMemory();
647 return -1;
648 }
649
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000650 if (newsize == 0) {
651 PyErr_NoMemory();
652 return -1;
653 }
654 b->b_ialloc <<= 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000655 tmp = (struct instr *)PyObject_Realloc(
Guido van Rossumd8faa362007-04-27 19:54:29 +0000656 (void *)b->b_instr, newsize);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657 if (tmp == NULL) {
658 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659 return -1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660 }
661 b->b_instr = tmp;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000662 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
663 }
664 return b->b_iused++;
665}
666
Christian Heimes2202f872008-02-06 14:31:34 +0000667/* Set the i_lineno member of the instruction at offset off if the
668 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669 already been set. If it has been set, the call has no effect.
670
Christian Heimes2202f872008-02-06 14:31:34 +0000671 The line number is reset in the following cases:
672 - when entering a new scope
673 - on each statement
674 - on each expression that start a new line
675 - before the "except" clause
676 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000677*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679static void
680compiler_set_lineno(struct compiler *c, int off)
681{
682 basicblock *b;
683 if (c->u->u_lineno_set)
684 return;
Neal Norwitz6baa4c42007-02-26 19:14:12 +0000685 c->u->u_lineno_set = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 b = c->u->u_curblock;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000687 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688}
689
690static int
691opcode_stack_effect(int opcode, int oparg)
692{
693 switch (opcode) {
694 case POP_TOP:
695 return -1;
696 case ROT_TWO:
697 case ROT_THREE:
698 return 0;
699 case DUP_TOP:
700 return 1;
701 case ROT_FOUR:
702 return 0;
703
704 case UNARY_POSITIVE:
705 case UNARY_NEGATIVE:
706 case UNARY_NOT:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707 case UNARY_INVERT:
708 return 0;
709
Nick Coghlan650f0d02007-04-15 12:05:43 +0000710 case SET_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000711 case LIST_APPEND:
Antoine Pitrouf289ae62008-12-18 11:06:25 +0000712 return -1;
713 case MAP_ADD:
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000714 return -2;
715
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 case BINARY_POWER:
717 case BINARY_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 case BINARY_MODULO:
719 case BINARY_ADD:
720 case BINARY_SUBTRACT:
721 case BINARY_SUBSCR:
722 case BINARY_FLOOR_DIVIDE:
723 case BINARY_TRUE_DIVIDE:
724 return -1;
725 case INPLACE_FLOOR_DIVIDE:
726 case INPLACE_TRUE_DIVIDE:
727 return -1;
728
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729 case INPLACE_ADD:
730 case INPLACE_SUBTRACT:
731 case INPLACE_MULTIPLY:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732 case INPLACE_MODULO:
733 return -1;
734 case STORE_SUBSCR:
735 return -3;
Christian Heimes99170a52007-12-19 02:07:34 +0000736 case STORE_MAP:
737 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738 case DELETE_SUBSCR:
739 return -2;
740
741 case BINARY_LSHIFT:
742 case BINARY_RSHIFT:
743 case BINARY_AND:
744 case BINARY_XOR:
745 case BINARY_OR:
746 return -1;
747 case INPLACE_POWER:
748 return -1;
749 case GET_ITER:
750 return 0;
751
752 case PRINT_EXPR:
753 return -1;
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000754 case LOAD_BUILD_CLASS:
755 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000756 case INPLACE_LSHIFT:
757 case INPLACE_RSHIFT:
758 case INPLACE_AND:
759 case INPLACE_XOR:
760 case INPLACE_OR:
761 return -1;
762 case BREAK_LOOP:
763 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +0000764 case WITH_CLEANUP:
Guido van Rossumf6694362006-03-10 02:28:35 +0000765 return -1; /* XXX Sometimes more */
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000766 case STORE_LOCALS:
767 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768 case RETURN_VALUE:
769 return -1;
770 case IMPORT_STAR:
771 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000772 case YIELD_VALUE:
773 return 0;
774
775 case POP_BLOCK:
776 return 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000777 case POP_EXCEPT:
778 return 0; /* -3 except if bad bytecode */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779 case END_FINALLY:
780 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781
782 case STORE_NAME:
783 return -1;
784 case DELETE_NAME:
785 return 0;
786 case UNPACK_SEQUENCE:
787 return oparg-1;
Guido van Rossum0368b722007-05-11 16:50:42 +0000788 case UNPACK_EX:
789 return (oparg&0xFF) + (oparg>>8);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790 case FOR_ITER:
791 return 1;
792
793 case STORE_ATTR:
794 return -2;
795 case DELETE_ATTR:
796 return -1;
797 case STORE_GLOBAL:
798 return -1;
799 case DELETE_GLOBAL:
800 return 0;
801 case DUP_TOPX:
802 return oparg;
803 case LOAD_CONST:
804 return 1;
805 case LOAD_NAME:
806 return 1;
807 case BUILD_TUPLE:
808 case BUILD_LIST:
Guido van Rossum86e58e22006-08-28 15:27:34 +0000809 case BUILD_SET:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000810 return 1-oparg;
811 case BUILD_MAP:
812 return 1;
813 case LOAD_ATTR:
814 return 0;
815 case COMPARE_OP:
816 return -1;
817 case IMPORT_NAME:
818 return 0;
819 case IMPORT_FROM:
820 return 1;
821
822 case JUMP_FORWARD:
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000823 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
824 case JUMP_IF_FALSE_OR_POP: /* "" */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825 case JUMP_ABSOLUTE:
826 return 0;
827
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000828 case POP_JUMP_IF_FALSE:
829 case POP_JUMP_IF_TRUE:
830 return -1;
831
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832 case LOAD_GLOBAL:
833 return 1;
834
835 case CONTINUE_LOOP:
836 return 0;
837 case SETUP_LOOP:
838 return 0;
839 case SETUP_EXCEPT:
840 case SETUP_FINALLY:
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000841 return 6; /* can push 3 values for the new exception
842 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843
844 case LOAD_FAST:
845 return 1;
846 case STORE_FAST:
847 return -1;
848 case DELETE_FAST:
849 return 0;
850
851 case RAISE_VARARGS:
852 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000853#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854 case CALL_FUNCTION:
855 return -NARGS(oparg);
856 case CALL_FUNCTION_VAR:
857 case CALL_FUNCTION_KW:
858 return -NARGS(oparg)-1;
859 case CALL_FUNCTION_VAR_KW:
860 return -NARGS(oparg)-2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861 case MAKE_FUNCTION:
Neal Norwitzc1505362006-12-28 06:47:50 +0000862 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum0240b922007-02-26 21:23:50 +0000863 case MAKE_CLOSURE:
864 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000865#undef NARGS
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000866 case BUILD_SLICE:
867 if (oparg == 3)
868 return -2;
869 else
870 return -1;
871
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000872 case LOAD_CLOSURE:
873 return 1;
874 case LOAD_DEREF:
875 return 1;
876 case STORE_DEREF:
877 return -1;
878 default:
879 fprintf(stderr, "opcode = %d\n", opcode);
880 Py_FatalError("opcode_stack_effect()");
881
882 }
883 return 0; /* not reachable */
884}
885
886/* Add an opcode with no argument.
887 Returns 0 on failure, 1 on success.
888*/
889
890static int
891compiler_addop(struct compiler *c, int opcode)
892{
893 basicblock *b;
894 struct instr *i;
895 int off;
896 off = compiler_next_instr(c, c->u->u_curblock);
897 if (off < 0)
898 return 0;
899 b = c->u->u_curblock;
900 i = &b->b_instr[off];
901 i->i_opcode = opcode;
902 i->i_hasarg = 0;
903 if (opcode == RETURN_VALUE)
904 b->b_return = 1;
905 compiler_set_lineno(c, off);
906 return 1;
907}
908
909static int
910compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
911{
912 PyObject *t, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000913 Py_ssize_t arg;
Christian Heimes400adb02008-02-01 08:12:03 +0000914 unsigned char *p, *q;
915 Py_complex z;
916 double d;
917 int real_part_zero, imag_part_zero;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000919 /* necessary to make sure types aren't coerced (e.g., int and long) */
Guido van Rossum04110fb2007-08-24 16:32:05 +0000920 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
921 if (PyFloat_Check(o)) {
Christian Heimes400adb02008-02-01 08:12:03 +0000922 d = PyFloat_AS_DOUBLE(o);
923 p = (unsigned char*) &d;
924 /* all we need is to make the tuple different in either the 0.0
925 * or -0.0 case from all others, just to avoid the "coercion".
926 */
927 if (*p==0 && p[sizeof(double)-1]==0)
928 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
929 else
930 t = PyTuple_Pack(2, o, o->ob_type);
931 }
932 else if (PyComplex_Check(o)) {
933 /* complex case is even messier: we need to make complex(x,
934 0.) different from complex(x, -0.) and complex(0., y)
935 different from complex(-0., y), for any x and y. In
936 particular, all four complex zeros should be
937 distinguished.*/
938 z = PyComplex_AsCComplex(o);
939 p = (unsigned char*) &(z.real);
940 q = (unsigned char*) &(z.imag);
941 /* all that matters here is that on IEEE platforms
942 real_part_zero will be true if z.real == 0., and false if
943 z.real == -0. In fact, real_part_zero will also be true
944 for some other rarely occurring nonzero floats, but this
945 doesn't matter. Similar comments apply to
946 imag_part_zero. */
947 real_part_zero = *p==0 && p[sizeof(double)-1]==0;
948 imag_part_zero = *q==0 && q[sizeof(double)-1]==0;
949 if (real_part_zero && imag_part_zero) {
950 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_True);
951 }
952 else if (real_part_zero && !imag_part_zero) {
953 t = PyTuple_Pack(4, o, o->ob_type, Py_True, Py_False);
954 }
955 else if (!real_part_zero && imag_part_zero) {
956 t = PyTuple_Pack(4, o, o->ob_type, Py_False, Py_True);
957 }
958 else {
959 t = PyTuple_Pack(2, o, o->ob_type);
960 }
961 }
962 else {
963 t = PyTuple_Pack(2, o, o->ob_type);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000964 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000965 if (t == NULL)
Christian Heimes400adb02008-02-01 08:12:03 +0000966 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967
968 v = PyDict_GetItem(dict, t);
969 if (!v) {
Jeremy Hyltonfbfe0932006-08-23 18:13:39 +0000970 if (PyErr_Occurred())
971 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972 arg = PyDict_Size(dict);
Christian Heimes217cfd12007-12-02 14:31:20 +0000973 v = PyLong_FromLong(arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974 if (!v) {
975 Py_DECREF(t);
976 return -1;
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000977 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978 if (PyDict_SetItem(dict, t, v) < 0) {
979 Py_DECREF(t);
980 Py_DECREF(v);
981 return -1;
982 }
983 Py_DECREF(v);
984 }
985 else
Christian Heimes217cfd12007-12-02 14:31:20 +0000986 arg = PyLong_AsLong(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987 Py_DECREF(t);
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000988 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989}
990
991static int
992compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
993 PyObject *o)
994{
995 int arg = compiler_add_o(c, dict, o);
996 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000997 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998 return compiler_addop_i(c, opcode, arg);
999}
1000
1001static int
1002compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001003 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004{
1005 int arg;
1006 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1007 if (!mangled)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001008 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009 arg = compiler_add_o(c, dict, mangled);
1010 Py_DECREF(mangled);
1011 if (arg < 0)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001012 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013 return compiler_addop_i(c, opcode, arg);
1014}
1015
1016/* Add an opcode with an integer argument.
1017 Returns 0 on failure, 1 on success.
1018*/
1019
1020static int
1021compiler_addop_i(struct compiler *c, int opcode, int oparg)
1022{
1023 struct instr *i;
1024 int off;
1025 off = compiler_next_instr(c, c->u->u_curblock);
1026 if (off < 0)
1027 return 0;
1028 i = &c->u->u_curblock->b_instr[off];
1029 i->i_opcode = opcode;
1030 i->i_oparg = oparg;
1031 i->i_hasarg = 1;
1032 compiler_set_lineno(c, off);
1033 return 1;
1034}
1035
1036static int
1037compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1038{
1039 struct instr *i;
1040 int off;
1041
1042 assert(b != NULL);
1043 off = compiler_next_instr(c, c->u->u_curblock);
1044 if (off < 0)
1045 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046 i = &c->u->u_curblock->b_instr[off];
1047 i->i_opcode = opcode;
1048 i->i_target = b;
1049 i->i_hasarg = 1;
1050 if (absolute)
1051 i->i_jabs = 1;
1052 else
1053 i->i_jrel = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001054 compiler_set_lineno(c, off);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055 return 1;
1056}
1057
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001058/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1059 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060 it as the current block. NEXT_BLOCK() also creates an implicit jump
1061 from the current block to the new block.
1062*/
1063
Thomas Wouters89f507f2006-12-13 04:49:30 +00001064/* The returns inside these macros make it impossible to decref objects
1065 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066*/
1067
1068
1069#define NEW_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001070 if (compiler_use_new_block((C)) == NULL) \
1071 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072}
1073
1074#define NEXT_BLOCK(C) { \
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001075 if (compiler_next_block((C)) == NULL) \
1076 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
1079#define ADDOP(C, OP) { \
1080 if (!compiler_addop((C), (OP))) \
1081 return 0; \
1082}
1083
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001084#define ADDOP_IN_SCOPE(C, OP) { \
1085 if (!compiler_addop((C), (OP))) { \
1086 compiler_exit_scope(c); \
1087 return 0; \
1088 } \
1089}
1090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091#define ADDOP_O(C, OP, O, TYPE) { \
1092 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1093 return 0; \
1094}
1095
1096#define ADDOP_NAME(C, OP, O, TYPE) { \
1097 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1098 return 0; \
1099}
1100
1101#define ADDOP_I(C, OP, O) { \
1102 if (!compiler_addop_i((C), (OP), (O))) \
1103 return 0; \
1104}
1105
1106#define ADDOP_JABS(C, OP, O) { \
1107 if (!compiler_addop_j((C), (OP), (O), 1)) \
1108 return 0; \
1109}
1110
1111#define ADDOP_JREL(C, OP, O) { \
1112 if (!compiler_addop_j((C), (OP), (O), 0)) \
1113 return 0; \
1114}
1115
1116/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1117 the ASDL name to synthesize the name of the C type and the visit function.
1118*/
1119
1120#define VISIT(C, TYPE, V) {\
1121 if (!compiler_visit_ ## TYPE((C), (V))) \
1122 return 0; \
1123}
1124
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001125#define VISIT_IN_SCOPE(C, TYPE, V) {\
1126 if (!compiler_visit_ ## TYPE((C), (V))) { \
1127 compiler_exit_scope(c); \
1128 return 0; \
1129 } \
1130}
1131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132#define VISIT_SLICE(C, V, CTX) {\
1133 if (!compiler_visit_slice((C), (V), (CTX))) \
1134 return 0; \
1135}
1136
1137#define VISIT_SEQ(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001138 int _i; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001140 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001141 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142 if (!compiler_visit_ ## TYPE((C), elt)) \
1143 return 0; \
1144 } \
1145}
1146
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001147#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001148 int _i; \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001149 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Neal Norwitz08b401f2006-01-07 21:24:09 +00001150 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001151 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001152 if (!compiler_visit_ ## TYPE((C), elt)) { \
1153 compiler_exit_scope(c); \
1154 return 0; \
1155 } \
1156 } \
1157}
1158
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159static int
1160compiler_isdocstring(stmt_ty s)
1161{
1162 if (s->kind != Expr_kind)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001163 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164 return s->v.Expr.value->kind == Str_kind;
1165}
1166
1167/* Compile a sequence of statements, checking for a docstring. */
1168
1169static int
1170compiler_body(struct compiler *c, asdl_seq *stmts)
1171{
1172 int i = 0;
1173 stmt_ty st;
1174
1175 if (!asdl_seq_LEN(stmts))
1176 return 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001177 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001178 if (compiler_isdocstring(st) && Py_OptimizeFlag < 2) {
1179 /* don't generate docstrings if -OO */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001180 i = 1;
1181 VISIT(c, expr, st->v.Expr.value);
1182 if (!compiler_nameop(c, __doc__, Store))
1183 return 0;
1184 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001185 for (; i < asdl_seq_LEN(stmts); i++)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001187 return 1;
1188}
1189
1190static PyCodeObject *
1191compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001192{
Guido van Rossum79f25d91997-04-29 20:08:16 +00001193 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001194 int addNone = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001195 static PyObject *module;
1196 if (!module) {
Christian Heimesfe82e772008-01-28 02:38:20 +00001197 module = PyUnicode_InternFromString("<module>");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001198 if (!module)
1199 return NULL;
1200 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001201 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1202 if (!compiler_enter_scope(c, module, mod, 0))
Guido van Rossumd076c731998-10-07 19:42:25 +00001203 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204 switch (mod->kind) {
1205 case Module_kind:
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001206 if (!compiler_body(c, mod->v.Module.body)) {
1207 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001209 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210 break;
1211 case Interactive_kind:
1212 c->c_interactive = 1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213 VISIT_SEQ_IN_SCOPE(c, stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00001214 mod->v.Interactive.body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215 break;
1216 case Expression_kind:
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001217 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001218 addNone = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219 break;
1220 case Suite_kind:
Neal Norwitz4737b232005-11-19 23:58:29 +00001221 PyErr_SetString(PyExc_SystemError,
1222 "suite should not be possible");
1223 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001224 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00001225 PyErr_Format(PyExc_SystemError,
1226 "module kind %d should not be possible",
1227 mod->kind);
1228 return 0;
Guido van Rossumd076c731998-10-07 19:42:25 +00001229 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230 co = assemble(c, addNone);
1231 compiler_exit_scope(c);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001232 return co;
1233}
1234
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235/* The test for LOCAL must come before the test for FREE in order to
1236 handle classes where name is both local and free. The local var is
1237 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001238*/
1239
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240static int
1241get_ref_type(struct compiler *c, PyObject *name)
1242{
1243 int scope = PyST_GetScope(c->u->u_ste, name);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001244 if (scope == 0) {
1245 char buf[350];
1246 PyOS_snprintf(buf, sizeof(buf),
1247 "unknown scope for %.100s in %.100s(%s) in %s\n"
Benjamin Petersonb58dda72009-01-18 22:27:04 +00001248 "symbols: %s\nlocals: %s\nglobals: %s",
Christian Heimes72b710a2008-05-26 13:28:38 +00001249 PyBytes_AS_STRING(name),
1250 PyBytes_AS_STRING(c->u->u_name),
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001251 PyObject_REPR(c->u->u_ste->ste_id),
1252 c->c_filename,
1253 PyObject_REPR(c->u->u_ste->ste_symbols),
1254 PyObject_REPR(c->u->u_varnames),
1255 PyObject_REPR(c->u->u_names)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001256 );
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001257 Py_FatalError(buf);
1258 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001259
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001260 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261}
1262
1263static int
1264compiler_lookup_arg(PyObject *dict, PyObject *name)
1265{
1266 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001267 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001268 if (k == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001269 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001270 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001271 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001272 if (v == NULL)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001273 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001274 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275}
1276
1277static int
1278compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1279{
1280 int i, free = PyCode_GetNumFree(co);
1281 if (free == 0) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001282 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1283 ADDOP_I(c, MAKE_FUNCTION, args);
1284 return 1;
1285 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286 for (i = 0; i < free; ++i) {
1287 /* Bypass com_addop_varname because it will generate
1288 LOAD_DEREF but LOAD_CLOSURE is needed.
1289 */
1290 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1291 int arg, reftype;
1292
1293 /* Special case: If a class contains a method with a
1294 free variable that has the same name as a method,
1295 the name will be considered free *and* local in the
1296 class. It should be handled by the closure, as
1297 well as by the normal name loookup logic.
1298 */
1299 reftype = get_ref_type(c, name);
1300 if (reftype == CELL)
1301 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1302 else /* (reftype == FREE) */
1303 arg = compiler_lookup_arg(c->u->u_freevars, name);
1304 if (arg == -1) {
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001305 fprintf(stderr,
1306 "lookup %s in %s %d %d\n"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307 "freevars of %s: %s\n",
1308 PyObject_REPR(name),
Christian Heimes72b710a2008-05-26 13:28:38 +00001309 PyBytes_AS_STRING(c->u->u_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310 reftype, arg,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001311 _PyUnicode_AsString(co->co_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 PyObject_REPR(co->co_freevars));
1313 Py_FatalError("compiler_make_closure()");
1314 }
1315 ADDOP_I(c, LOAD_CLOSURE, arg);
1316 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001317 ADDOP_I(c, BUILD_TUPLE, free);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001319 ADDOP_I(c, MAKE_CLOSURE, args);
1320 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001321}
1322
1323static int
1324compiler_decorators(struct compiler *c, asdl_seq* decos)
1325{
1326 int i;
1327
1328 if (!decos)
1329 return 1;
1330
1331 for (i = 0; i < asdl_seq_LEN(decos); i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333 }
1334 return 1;
1335}
1336
1337static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001338compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1339 asdl_seq *kw_defaults)
1340{
1341 int i, default_count = 0;
1342 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
Neal Norwitzc1505362006-12-28 06:47:50 +00001343 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001344 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1345 if (default_) {
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001346 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001347 if (!compiler_visit_expr(c, default_)) {
1348 return -1;
1349 }
1350 default_count++;
1351 }
1352 }
1353 return default_count;
1354}
1355
1356static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001357compiler_visit_argannotation(struct compiler *c, identifier id,
1358 expr_ty annotation, PyObject *names)
1359{
1360 if (annotation) {
1361 VISIT(c, expr, annotation);
1362 if (PyList_Append(names, id))
1363 return -1;
1364 }
1365 return 0;
1366}
1367
1368static int
1369compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1370 PyObject *names)
1371{
1372 int i, error;
1373 for (i = 0; i < asdl_seq_LEN(args); i++) {
1374 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001375 error = compiler_visit_argannotation(
1376 c,
1377 arg->arg,
1378 arg->annotation,
1379 names);
Neal Norwitzc1505362006-12-28 06:47:50 +00001380 if (error)
1381 return error;
1382 }
1383 return 0;
1384}
1385
1386static int
1387compiler_visit_annotations(struct compiler *c, arguments_ty args,
1388 expr_ty returns)
1389{
Guido van Rossum0240b922007-02-26 21:23:50 +00001390 /* Push arg annotations and a list of the argument names. Return the #
1391 of items pushed. The expressions are evaluated out-of-order wrt the
1392 source code.
1393
1394 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1395 */
Neal Norwitzc1505362006-12-28 06:47:50 +00001396 static identifier return_str;
1397 PyObject *names;
1398 int len;
1399 names = PyList_New(0);
1400 if (!names)
1401 return -1;
1402
1403 if (compiler_visit_argannotations(c, args->args, names))
1404 goto error;
1405 if (args->varargannotation &&
1406 compiler_visit_argannotation(c, args->vararg,
1407 args->varargannotation, names))
1408 goto error;
1409 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1410 goto error;
1411 if (args->kwargannotation &&
1412 compiler_visit_argannotation(c, args->kwarg,
1413 args->kwargannotation, names))
1414 goto error;
1415
1416 if (!return_str) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001417 return_str = PyUnicode_InternFromString("return");
Neal Norwitzc1505362006-12-28 06:47:50 +00001418 if (!return_str)
1419 goto error;
1420 }
1421 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1422 goto error;
1423 }
1424
1425 len = PyList_GET_SIZE(names);
Guido van Rossum0240b922007-02-26 21:23:50 +00001426 if (len > 65534) {
1427 /* len must fit in 16 bits, and len is incremented below */
1428 PyErr_SetString(PyExc_SyntaxError,
1429 "too many annotations");
1430 goto error;
1431 }
Neal Norwitzc1505362006-12-28 06:47:50 +00001432 if (len) {
1433 /* convert names to a tuple and place on stack */
1434 PyObject *elt;
1435 int i;
1436 PyObject *s = PyTuple_New(len);
1437 if (!s)
1438 goto error;
1439 for (i = 0; i < len; i++) {
1440 elt = PyList_GET_ITEM(names, i);
1441 Py_INCREF(elt);
1442 PyTuple_SET_ITEM(s, i, elt);
1443 }
1444 ADDOP_O(c, LOAD_CONST, s, consts);
1445 Py_DECREF(s);
1446 len++; /* include the just-pushed tuple */
1447 }
1448 Py_DECREF(names);
1449 return len;
1450
1451error:
1452 Py_DECREF(names);
1453 return -1;
1454}
1455
1456static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001457compiler_function(struct compiler *c, stmt_ty s)
1458{
1459 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001460 PyObject *first_const = Py_None;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001461 arguments_ty args = s->v.FunctionDef.args;
Neal Norwitzc1505362006-12-28 06:47:50 +00001462 expr_ty returns = s->v.FunctionDef.returns;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001463 asdl_seq* decos = s->v.FunctionDef.decorator_list;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001464 stmt_ty st;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001465 int i, n, docstring, kw_default_count = 0, arglength;
Neal Norwitzc1505362006-12-28 06:47:50 +00001466 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467
1468 assert(s->kind == FunctionDef_kind);
1469
1470 if (!compiler_decorators(c, decos))
1471 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001472 if (args->kwonlyargs) {
1473 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1474 args->kw_defaults);
1475 if (res < 0)
1476 return 0;
1477 kw_default_count = res;
1478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001479 if (args->defaults)
1480 VISIT_SEQ(c, expr, args->defaults);
Neal Norwitzc1505362006-12-28 06:47:50 +00001481 num_annotations = compiler_visit_annotations(c, args, returns);
Guido van Rossum0240b922007-02-26 21:23:50 +00001482 if (num_annotations < 0)
1483 return 0;
1484 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001485
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1487 s->lineno))
1488 return 0;
1489
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001491 docstring = compiler_isdocstring(st);
Thomas Woutersce272b62007-09-19 21:19:28 +00001492 if (docstring && Py_OptimizeFlag < 2)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001493 first_const = st->v.Expr.value->v.Str.s;
1494 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001495 compiler_exit_scope(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001496 return 0;
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001500 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001501 n = asdl_seq_LEN(s->v.FunctionDef.body);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001502 /* if there was a docstring, we need to skip the first statement */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001503 for (i = docstring; i < n; i++) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001504 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1505 VISIT_IN_SCOPE(c, stmt, st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506 }
1507 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001508 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001509 if (co == NULL)
1510 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001511
Guido van Rossum4f72a782006-10-27 23:31:49 +00001512 arglength = asdl_seq_LEN(args->defaults);
1513 arglength |= kw_default_count << 8;
Neal Norwitzc1505362006-12-28 06:47:50 +00001514 arglength |= num_annotations << 16;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001515 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001516 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517
Neal Norwitzc1505362006-12-28 06:47:50 +00001518 /* decorators */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001519 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1520 ADDOP_I(c, CALL_FUNCTION, 1);
1521 }
1522
1523 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1524}
1525
1526static int
1527compiler_class(struct compiler *c, stmt_ty s)
1528{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529 PyCodeObject *co;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001530 PyObject *str;
Benjamin Petersondcaf3292009-03-03 00:54:05 +00001531 int i;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001532 asdl_seq* decos = s->v.ClassDef.decorator_list;
1533
1534 if (!compiler_decorators(c, decos))
1535 return 0;
1536
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001537 /* ultimately generate code for:
1538 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1539 where:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001540 <func> is a function/closure created from the class body;
1541 it has a single argument (__locals__) where the dict
1542 (or MutableSequence) representing the locals is passed
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001543 <name> is the class name
1544 <bases> is the positional arguments and *varargs argument
1545 <keywords> is the keyword arguments and **kwds argument
1546 This borrows from compiler_call.
1547 */
1548
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001549 /* 1. compile the class body into a code object */
1550 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1551 return 0;
1552 /* this block represents what we do in the new scope */
1553 {
1554 /* use the class name for name mangling */
1555 Py_INCREF(s->v.ClassDef.name);
Neal Norwitz36e63102008-04-01 08:08:09 +00001556 Py_XDECREF(c->u->u_private);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001557 c->u->u_private = s->v.ClassDef.name;
1558 /* force it to have one mandatory argument */
1559 c->u->u_argcount = 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001560 /* load the first argument (__locals__) ... */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001561 ADDOP_I(c, LOAD_FAST, 0);
1562 /* ... and store it into f_locals */
1563 ADDOP_IN_SCOPE(c, STORE_LOCALS);
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001564 /* load (global) __name__ ... */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001565 str = PyUnicode_InternFromString("__name__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001566 if (!str || !compiler_nameop(c, str, Load)) {
1567 Py_XDECREF(str);
1568 compiler_exit_scope(c);
1569 return 0;
1570 }
1571 Py_DECREF(str);
1572 /* ... and store it as __module__ */
Martin v. Löwis5b222132007-06-10 09:51:05 +00001573 str = PyUnicode_InternFromString("__module__");
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001574 if (!str || !compiler_nameop(c, str, Store)) {
1575 Py_XDECREF(str);
1576 compiler_exit_scope(c);
1577 return 0;
1578 }
1579 Py_DECREF(str);
1580 /* compile the body proper */
1581 if (!compiler_body(c, s->v.ClassDef.body)) {
1582 compiler_exit_scope(c);
1583 return 0;
1584 }
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001585 /* return the (empty) __class__ cell */
1586 str = PyUnicode_InternFromString("__class__");
1587 if (str == NULL) {
1588 compiler_exit_scope(c);
1589 return 0;
1590 }
1591 i = compiler_lookup_arg(c->u->u_cellvars, str);
1592 Py_DECREF(str);
1593 if (i == -1) {
1594 /* This happens when nobody references the cell */
1595 PyErr_Clear();
1596 /* Return None */
1597 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1598 }
1599 else {
1600 /* Return the cell where to store __class__ */
1601 ADDOP_I(c, LOAD_CLOSURE, i);
1602 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001603 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1604 /* create the code object */
1605 co = assemble(c, 1);
1606 }
1607 /* leave the new scope */
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001608 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001609 if (co == NULL)
1610 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001612 /* 2. load the 'build_class' function */
1613 ADDOP(c, LOAD_BUILD_CLASS);
1614
1615 /* 3. load a function (or closure) made from the code object */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001616 compiler_make_closure(c, co, 0);
Neal Norwitz4737b232005-11-19 23:58:29 +00001617 Py_DECREF(co);
1618
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001619 /* 4. load class name */
1620 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1621
1622 /* 5. generate the rest of the code for the call */
1623 if (!compiler_call_helper(c, 2,
1624 s->v.ClassDef.bases,
1625 s->v.ClassDef.keywords,
1626 s->v.ClassDef.starargs,
1627 s->v.ClassDef.kwargs))
1628 return 0;
1629
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001630 /* 6. apply decorators */
1631 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1632 ADDOP_I(c, CALL_FUNCTION, 1);
1633 }
1634
1635 /* 7. store into <name> */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1637 return 0;
1638 return 1;
1639}
1640
1641static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001642compiler_ifexp(struct compiler *c, expr_ty e)
1643{
1644 basicblock *end, *next;
1645
1646 assert(e->kind == IfExp_kind);
1647 end = compiler_new_block(c);
1648 if (end == NULL)
1649 return 0;
1650 next = compiler_new_block(c);
1651 if (next == NULL)
1652 return 0;
1653 VISIT(c, expr, e->v.IfExp.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001654 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001655 VISIT(c, expr, e->v.IfExp.body);
1656 ADDOP_JREL(c, JUMP_FORWARD, end);
1657 compiler_use_next_block(c, next);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001658 VISIT(c, expr, e->v.IfExp.orelse);
1659 compiler_use_next_block(c, end);
1660 return 1;
1661}
1662
1663static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664compiler_lambda(struct compiler *c, expr_ty e)
1665{
1666 PyCodeObject *co;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001667 static identifier name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001668 int kw_default_count = 0, arglength;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669 arguments_ty args = e->v.Lambda.args;
1670 assert(e->kind == Lambda_kind);
1671
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001672 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00001673 name = PyUnicode_InternFromString("<lambda>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00001674 if (!name)
1675 return 0;
1676 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677
Guido van Rossum4f72a782006-10-27 23:31:49 +00001678 if (args->kwonlyargs) {
1679 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1680 args->kw_defaults);
1681 if (res < 0) return 0;
1682 kw_default_count = res;
1683 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001684 if (args->defaults)
1685 VISIT_SEQ(c, expr, args->defaults);
1686 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1687 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689 c->u->u_argcount = asdl_seq_LEN(args->args);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001690 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001691 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
Benjamin Peterson371ccfb2008-12-27 19:03:36 +00001692 if (c->u->u_ste->ste_generator) {
1693 ADDOP_IN_SCOPE(c, POP_TOP);
1694 }
1695 else {
1696 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1697 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698 co = assemble(c, 1);
Neil Schemenauerc396d9e2005-10-25 06:30:14 +00001699 compiler_exit_scope(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700 if (co == NULL)
1701 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001702
Guido van Rossum4f72a782006-10-27 23:31:49 +00001703 arglength = asdl_seq_LEN(args->defaults);
1704 arglength |= kw_default_count << 8;
1705 compiler_make_closure(c, co, arglength);
Neal Norwitz4737b232005-11-19 23:58:29 +00001706 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707
1708 return 1;
1709}
1710
1711static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712compiler_if(struct compiler *c, stmt_ty s)
1713{
1714 basicblock *end, *next;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001715 int constant;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716 assert(s->kind == If_kind);
1717 end = compiler_new_block(c);
1718 if (end == NULL)
1719 return 0;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001720
1721 constant = expr_constant(s->v.If.test);
1722 /* constant = 0: "if 0"
1723 * constant = 1: "if 1", "if 2", ...
1724 * constant = -1: rest */
1725 if (constant == 0) {
1726 if (s->v.If.orelse)
1727 VISIT_SEQ(c, stmt, s->v.If.orelse);
1728 } else if (constant == 1) {
1729 VISIT_SEQ(c, stmt, s->v.If.body);
1730 } else {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001731 if (s->v.If.orelse) {
1732 next = compiler_new_block(c);
1733 if (next == NULL)
1734 return 0;
1735 }
1736 else
1737 next = end;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001738 VISIT(c, expr, s->v.If.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001739 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001740 VISIT_SEQ(c, stmt, s->v.If.body);
1741 ADDOP_JREL(c, JUMP_FORWARD, end);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001742 if (s->v.If.orelse) {
1743 compiler_use_next_block(c, next);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001744 VISIT_SEQ(c, stmt, s->v.If.orelse);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001745 }
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001746 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747 compiler_use_next_block(c, end);
1748 return 1;
1749}
1750
1751static int
1752compiler_for(struct compiler *c, stmt_ty s)
1753{
1754 basicblock *start, *cleanup, *end;
1755
1756 start = compiler_new_block(c);
1757 cleanup = compiler_new_block(c);
1758 end = compiler_new_block(c);
1759 if (start == NULL || end == NULL || cleanup == NULL)
1760 return 0;
1761 ADDOP_JREL(c, SETUP_LOOP, end);
1762 if (!compiler_push_fblock(c, LOOP, start))
1763 return 0;
1764 VISIT(c, expr, s->v.For.iter);
1765 ADDOP(c, GET_ITER);
1766 compiler_use_next_block(c, start);
Christian Heimes2202f872008-02-06 14:31:34 +00001767 /* for expressions must be traced on each iteration,
1768 so we need to set an extra line number. */
Neal Norwitz6baa4c42007-02-26 19:14:12 +00001769 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001770 ADDOP_JREL(c, FOR_ITER, cleanup);
1771 VISIT(c, expr, s->v.For.target);
1772 VISIT_SEQ(c, stmt, s->v.For.body);
1773 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1774 compiler_use_next_block(c, cleanup);
1775 ADDOP(c, POP_BLOCK);
1776 compiler_pop_fblock(c, LOOP, start);
1777 VISIT_SEQ(c, stmt, s->v.For.orelse);
1778 compiler_use_next_block(c, end);
1779 return 1;
1780}
1781
1782static int
1783compiler_while(struct compiler *c, stmt_ty s)
1784{
1785 basicblock *loop, *orelse, *end, *anchor = NULL;
1786 int constant = expr_constant(s->v.While.test);
1787
Christian Heimes969fe572008-01-25 11:23:10 +00001788 if (constant == 0) {
1789 if (s->v.While.orelse)
1790 VISIT_SEQ(c, stmt, s->v.While.orelse);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001791 return 1;
Christian Heimes969fe572008-01-25 11:23:10 +00001792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793 loop = compiler_new_block(c);
1794 end = compiler_new_block(c);
1795 if (constant == -1) {
1796 anchor = compiler_new_block(c);
1797 if (anchor == NULL)
1798 return 0;
1799 }
1800 if (loop == NULL || end == NULL)
1801 return 0;
1802 if (s->v.While.orelse) {
1803 orelse = compiler_new_block(c);
1804 if (orelse == NULL)
1805 return 0;
1806 }
1807 else
1808 orelse = NULL;
1809
1810 ADDOP_JREL(c, SETUP_LOOP, end);
1811 compiler_use_next_block(c, loop);
1812 if (!compiler_push_fblock(c, LOOP, loop))
1813 return 0;
1814 if (constant == -1) {
Christian Heimes2202f872008-02-06 14:31:34 +00001815 /* while expressions must be traced on each iteration,
1816 so we need to set an extra line number. */
1817 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818 VISIT(c, expr, s->v.While.test);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001819 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820 }
1821 VISIT_SEQ(c, stmt, s->v.While.body);
1822 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
1823
1824 /* XXX should the two POP instructions be in a separate block
1825 if there is no else clause ?
1826 */
1827
1828 if (constant == -1) {
1829 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830 ADDOP(c, POP_BLOCK);
1831 }
1832 compiler_pop_fblock(c, LOOP, loop);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001833 if (orelse != NULL) /* what if orelse is just pass? */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001834 VISIT_SEQ(c, stmt, s->v.While.orelse);
1835 compiler_use_next_block(c, end);
1836
1837 return 1;
1838}
1839
1840static int
1841compiler_continue(struct compiler *c)
1842{
1843 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
Thomas Wouters89f507f2006-12-13 04:49:30 +00001844 static const char IN_FINALLY_ERROR_MSG[] =
1845 "'continue' not supported inside 'finally' clause";
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001846 int i;
1847
1848 if (!c->u->u_nfblocks)
1849 return compiler_error(c, LOOP_ERROR_MSG);
1850 i = c->u->u_nfblocks - 1;
1851 switch (c->u->u_fblock[i].fb_type) {
1852 case LOOP:
1853 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1854 break;
1855 case EXCEPT:
1856 case FINALLY_TRY:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001857 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1858 /* Prevent continue anywhere under a finally
1859 even if hidden in a sub-try or except. */
1860 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1861 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1862 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001863 if (i == -1)
1864 return compiler_error(c, LOOP_ERROR_MSG);
1865 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1866 break;
1867 case FINALLY_END:
Thomas Wouters89f507f2006-12-13 04:49:30 +00001868 return compiler_error(c, IN_FINALLY_ERROR_MSG);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 }
1870
1871 return 1;
1872}
1873
1874/* Code generated for "try: <body> finally: <finalbody>" is as follows:
1875
1876 SETUP_FINALLY L
1877 <code for body>
1878 POP_BLOCK
1879 LOAD_CONST <None>
1880 L: <code for finalbody>
1881 END_FINALLY
1882
1883 The special instructions use the block stack. Each block
1884 stack entry contains the instruction that created it (here
1885 SETUP_FINALLY), the level of the value stack at the time the
1886 block stack entry was created, and a label (here L).
1887
1888 SETUP_FINALLY:
1889 Pushes the current value stack level and the label
1890 onto the block stack.
1891 POP_BLOCK:
1892 Pops en entry from the block stack, and pops the value
1893 stack until its level is the same as indicated on the
1894 block stack. (The label is ignored.)
1895 END_FINALLY:
1896 Pops a variable number of entries from the *value* stack
1897 and re-raises the exception they specify. The number of
1898 entries popped depends on the (pseudo) exception type.
1899
1900 The block stack is unwound when an exception is raised:
1901 when a SETUP_FINALLY entry is found, the exception is pushed
1902 onto the value stack (and the exception condition is cleared),
1903 and the interpreter jumps to the label gotten from the block
1904 stack.
1905*/
1906
1907static int
1908compiler_try_finally(struct compiler *c, stmt_ty s)
1909{
1910 basicblock *body, *end;
1911 body = compiler_new_block(c);
1912 end = compiler_new_block(c);
1913 if (body == NULL || end == NULL)
1914 return 0;
1915
1916 ADDOP_JREL(c, SETUP_FINALLY, end);
1917 compiler_use_next_block(c, body);
1918 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1919 return 0;
1920 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1921 ADDOP(c, POP_BLOCK);
1922 compiler_pop_fblock(c, FINALLY_TRY, body);
1923
1924 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1925 compiler_use_next_block(c, end);
1926 if (!compiler_push_fblock(c, FINALLY_END, end))
1927 return 0;
1928 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1929 ADDOP(c, END_FINALLY);
1930 compiler_pop_fblock(c, FINALLY_END, end);
1931
1932 return 1;
1933}
1934
1935/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001936 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001937 (The contents of the value stack is shown in [], with the top
1938 at the right; 'tb' is trace-back info, 'val' the exception's
1939 associated value, and 'exc' the exception.)
1940
1941 Value stack Label Instruction Argument
1942 [] SETUP_EXCEPT L1
1943 [] <code for S>
1944 [] POP_BLOCK
1945 [] JUMP_FORWARD L0
1946
1947 [tb, val, exc] L1: DUP )
1948 [tb, val, exc, exc] <evaluate E1> )
1949 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001950 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951 [tb, val, exc] POP
1952 [tb, val] <assign to V1> (or POP if no V1)
1953 [tb] POP
1954 [] <code for S1>
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001955 JUMP_FORWARD L0
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001956
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001957 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958 .............................etc.......................
1959
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001960 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001961
1962 [] L0: <next statement>
1963
1964 Of course, parts are not generated if Vi or Ei is not present.
1965*/
1966static int
1967compiler_try_except(struct compiler *c, stmt_ty s)
1968{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00001969 basicblock *body, *orelse, *except, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001970 int i, n;
1971
1972 body = compiler_new_block(c);
1973 except = compiler_new_block(c);
1974 orelse = compiler_new_block(c);
1975 end = compiler_new_block(c);
1976 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1977 return 0;
1978 ADDOP_JREL(c, SETUP_EXCEPT, except);
1979 compiler_use_next_block(c, body);
1980 if (!compiler_push_fblock(c, EXCEPT, body))
1981 return 0;
1982 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1983 ADDOP(c, POP_BLOCK);
1984 compiler_pop_fblock(c, EXCEPT, body);
1985 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1986 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1987 compiler_use_next_block(c, except);
1988 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001989 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001990 s->v.TryExcept.handlers, i);
Neal Norwitzad74aa82008-03-31 05:14:30 +00001991 if (!handler->v.ExceptHandler.type && i < n-1)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001992 return compiler_error(c, "default 'except:' must be last");
Christian Heimes2202f872008-02-06 14:31:34 +00001993 c->u->u_lineno_set = 0;
1994 c->u->u_lineno = handler->lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001995 except = compiler_new_block(c);
1996 if (except == NULL)
1997 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00001998 if (handler->v.ExceptHandler.type) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999 ADDOP(c, DUP_TOP);
Neal Norwitzad74aa82008-03-31 05:14:30 +00002000 VISIT(c, expr, handler->v.ExceptHandler.type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002002 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002003 }
2004 ADDOP(c, POP_TOP);
Neal Norwitzad74aa82008-03-31 05:14:30 +00002005 if (handler->v.ExceptHandler.name) {
Guido van Rossumb940e112007-01-10 16:19:56 +00002006 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002007
2008 cleanup_end = compiler_new_block(c);
2009 cleanup_body = compiler_new_block(c);
2010 if(!(cleanup_end || cleanup_body))
2011 return 0;
2012
Neal Norwitzad74aa82008-03-31 05:14:30 +00002013 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002014 ADDOP(c, POP_TOP);
2015
2016 /*
2017 try:
2018 # body
2019 except type as name:
2020 try:
2021 # body
2022 finally:
2023 name = None
2024 del name
2025 */
2026
2027 /* second try: */
2028 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2029 compiler_use_next_block(c, cleanup_body);
2030 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2031 return 0;
2032
2033 /* second # body */
Neal Norwitzad74aa82008-03-31 05:14:30 +00002034 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Guido van Rossumb940e112007-01-10 16:19:56 +00002035 ADDOP(c, POP_BLOCK);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002036 ADDOP(c, POP_EXCEPT);
Guido van Rossumb940e112007-01-10 16:19:56 +00002037 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2038
2039 /* finally: */
2040 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2041 compiler_use_next_block(c, cleanup_end);
2042 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2043 return 0;
2044
2045 /* name = None */
2046 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Neal Norwitzad74aa82008-03-31 05:14:30 +00002047 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Guido van Rossumb940e112007-01-10 16:19:56 +00002048
Guido van Rossum16be03e2007-01-10 18:51:35 +00002049 /* del name */
Neal Norwitzad74aa82008-03-31 05:14:30 +00002050 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Guido van Rossumb940e112007-01-10 16:19:56 +00002051
2052 ADDOP(c, END_FINALLY);
2053 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002054 }
2055 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002056 basicblock *cleanup_body;
2057
2058 cleanup_body = compiler_new_block(c);
2059 if(!cleanup_body)
2060 return 0;
2061
2062 ADDOP(c, POP_TOP);
Guido van Rossumb940e112007-01-10 16:19:56 +00002063 ADDOP(c, POP_TOP);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002064 compiler_use_next_block(c, cleanup_body);
2065 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2066 return 0;
Neal Norwitzad74aa82008-03-31 05:14:30 +00002067 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002068 ADDOP(c, POP_EXCEPT);
2069 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002070 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002071 ADDOP_JREL(c, JUMP_FORWARD, end);
2072 compiler_use_next_block(c, except);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002073 }
2074 ADDOP(c, END_FINALLY);
2075 compiler_use_next_block(c, orelse);
2076 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2077 compiler_use_next_block(c, end);
2078 return 1;
2079}
2080
2081static int
2082compiler_import_as(struct compiler *c, identifier name, identifier asname)
2083{
2084 /* The IMPORT_NAME opcode was already generated. This function
2085 merely needs to bind the result to a name.
2086
2087 If there is a dot in name, we need to split it and emit a
2088 LOAD_ATTR for each name.
2089 */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002090 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2091 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092 if (dot) {
2093 /* Consume the base module name to get the first attribute */
2094 src = dot + 1;
2095 while (dot) {
2096 /* NB src is only defined when dot != NULL */
Armin Rigo31441302005-10-21 12:57:31 +00002097 PyObject *attr;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002098 dot = Py_UNICODE_strchr(src, '.');
2099 attr = PyUnicode_FromUnicode(src,
2100 dot ? dot - src : Py_UNICODE_strlen(src));
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002101 if (!attr)
2102 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002103 ADDOP_O(c, LOAD_ATTR, attr, names);
Neal Norwitz7bcabc62005-11-20 23:58:38 +00002104 Py_DECREF(attr);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002105 src = dot + 1;
2106 }
2107 }
2108 return compiler_nameop(c, asname, Store);
2109}
2110
2111static int
2112compiler_import(struct compiler *c, stmt_ty s)
2113{
2114 /* The Import node stores a module name like a.b.c as a single
2115 string. This is convenient for all cases except
2116 import a.b.c as d
2117 where we need to parse that string to extract the individual
2118 module names.
2119 XXX Perhaps change the representation to make this case simpler?
2120 */
2121 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002123 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002124 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125 int r;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002126 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127
Christian Heimes217cfd12007-12-02 14:31:20 +00002128 level = PyLong_FromLong(0);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002129 if (level == NULL)
2130 return 0;
2131
2132 ADDOP_O(c, LOAD_CONST, level, consts);
2133 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2135 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2136
2137 if (alias->asname) {
Neil Schemenauerac699ef2005-10-23 03:45:42 +00002138 r = compiler_import_as(c, alias->name, alias->asname);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002139 if (!r)
2140 return r;
2141 }
2142 else {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002143 identifier tmp = alias->name;
Martin v. Löwis5b222132007-06-10 09:51:05 +00002144 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2145 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146 if (dot)
Martin v. Löwis5b222132007-06-10 09:51:05 +00002147 tmp = PyUnicode_FromUnicode(base,
2148 dot - base);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002149 r = compiler_nameop(c, tmp, Store);
2150 if (dot) {
2151 Py_DECREF(tmp);
2152 }
2153 if (!r)
2154 return r;
2155 }
2156 }
2157 return 1;
2158}
2159
2160static int
2161compiler_from_import(struct compiler *c, stmt_ty s)
2162{
2163 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002164
2165 PyObject *names = PyTuple_New(n);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002166 PyObject *level;
2167
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168 if (!names)
2169 return 0;
2170
Christian Heimes217cfd12007-12-02 14:31:20 +00002171 level = PyLong_FromLong(s->v.ImportFrom.level);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002172 if (!level) {
2173 Py_DECREF(names);
2174 return 0;
2175 }
2176
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177 /* build up the names */
2178 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002179 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180 Py_INCREF(alias->name);
2181 PyTuple_SET_ITEM(names, i, alias->name);
2182 }
2183
2184 if (s->lineno > c->c_future->ff_lineno) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002185 if (!PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module,
2186 "__future__")) {
Neal Norwitzd9cf85f2006-03-02 08:08:42 +00002187 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188 Py_DECREF(names);
2189 return compiler_error(c,
2190 "from __future__ imports must occur "
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002191 "at the beginning of the file");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192
2193 }
2194 }
2195
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002196 ADDOP_O(c, LOAD_CONST, level, consts);
2197 Py_DECREF(level);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 ADDOP_O(c, LOAD_CONST, names, consts);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00002199 Py_DECREF(names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002200 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2201 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002202 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002203 identifier store_name;
2204
Martin v. Löwis5b222132007-06-10 09:51:05 +00002205 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002206 assert(n == 1);
2207 ADDOP(c, IMPORT_STAR);
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002208 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002209 }
2210
2211 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2212 store_name = alias->name;
2213 if (alias->asname)
2214 store_name = alias->asname;
2215
2216 if (!compiler_nameop(c, store_name, Store)) {
2217 Py_DECREF(names);
2218 return 0;
2219 }
2220 }
Neal Norwitz28b32ac2005-12-06 07:41:30 +00002221 /* remove imported module */
2222 ADDOP(c, POP_TOP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223 return 1;
2224}
2225
2226static int
2227compiler_assert(struct compiler *c, stmt_ty s)
2228{
2229 static PyObject *assertion_error = NULL;
2230 basicblock *end;
2231
2232 if (Py_OptimizeFlag)
2233 return 1;
2234 if (assertion_error == NULL) {
Christian Heimesfe82e772008-01-28 02:38:20 +00002235 assertion_error = PyUnicode_InternFromString("AssertionError");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 if (assertion_error == NULL)
2237 return 0;
2238 }
Christian Heimes08976cb2008-03-16 00:32:36 +00002239 if (s->v.Assert.test->kind == Tuple_kind &&
2240 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2241 const char* msg =
2242 "assertion is always true, perhaps remove parentheses?";
2243 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2244 c->u->u_lineno, NULL, NULL) == -1)
2245 return 0;
2246 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002247 VISIT(c, expr, s->v.Assert.test);
2248 end = compiler_new_block(c);
2249 if (end == NULL)
2250 return 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002251 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002252 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2253 if (s->v.Assert.msg) {
2254 VISIT(c, expr, s->v.Assert.msg);
Collin Winter828f04a2007-08-31 00:04:24 +00002255 ADDOP_I(c, CALL_FUNCTION, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256 }
Collin Winter828f04a2007-08-31 00:04:24 +00002257 ADDOP_I(c, RAISE_VARARGS, 1);
Neal Norwitz51abbc72005-12-18 07:06:23 +00002258 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002259 return 1;
2260}
2261
2262static int
2263compiler_visit_stmt(struct compiler *c, stmt_ty s)
2264{
2265 int i, n;
2266
Thomas Wouters89f507f2006-12-13 04:49:30 +00002267 /* Always assign a lineno to the next instruction for a stmt. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 c->u->u_lineno = s->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00002269 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002270
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 switch (s->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002272 case FunctionDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002273 return compiler_function(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002274 case ClassDef_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275 return compiler_class(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002276 case Return_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002277 if (c->u->u_ste->ste_type != FunctionBlock)
2278 return compiler_error(c, "'return' outside function");
2279 if (s->v.Return.value) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280 VISIT(c, expr, s->v.Return.value);
2281 }
2282 else
2283 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2284 ADDOP(c, RETURN_VALUE);
2285 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002286 case Delete_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002287 VISIT_SEQ(c, expr, s->v.Delete.targets)
2288 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002289 case Assign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290 n = asdl_seq_LEN(s->v.Assign.targets);
2291 VISIT(c, expr, s->v.Assign.value);
2292 for (i = 0; i < n; i++) {
2293 if (i < n - 1)
2294 ADDOP(c, DUP_TOP);
2295 VISIT(c, expr,
2296 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2297 }
2298 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002299 case AugAssign_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300 return compiler_augassign(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002301 case For_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002302 return compiler_for(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002303 case While_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002304 return compiler_while(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002305 case If_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002306 return compiler_if(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002307 case Raise_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002308 n = 0;
Collin Winter828f04a2007-08-31 00:04:24 +00002309 if (s->v.Raise.exc) {
2310 VISIT(c, expr, s->v.Raise.exc);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 n++;
Collin Winter828f04a2007-08-31 00:04:24 +00002312 if (s->v.Raise.cause) {
2313 VISIT(c, expr, s->v.Raise.cause);
2314 n++;
2315 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002316 }
2317 ADDOP_I(c, RAISE_VARARGS, n);
2318 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002319 case TryExcept_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002320 return compiler_try_except(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002321 case TryFinally_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002322 return compiler_try_finally(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002323 case Assert_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324 return compiler_assert(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002325 case Import_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002326 return compiler_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002327 case ImportFrom_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002328 return compiler_from_import(c, s);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002329 case Global_kind:
Jeremy Hylton81e95022007-02-27 06:50:52 +00002330 case Nonlocal_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002332 case Expr_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002333 if (c->c_interactive && c->c_nestlevel <= 1) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002334 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002335 ADDOP(c, PRINT_EXPR);
2336 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002337 else if (s->v.Expr.value->kind != Str_kind &&
2338 s->v.Expr.value->kind != Num_kind) {
2339 VISIT(c, expr, s->v.Expr.value);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002340 ADDOP(c, POP_TOP);
2341 }
2342 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002343 case Pass_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002344 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002345 case Break_kind:
Guido van Rossumd8faa362007-04-27 19:54:29 +00002346 if (!compiler_in_loop(c))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347 return compiler_error(c, "'break' outside loop");
2348 ADDOP(c, BREAK_LOOP);
2349 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002350 case Continue_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002351 return compiler_continue(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002352 case With_kind:
2353 return compiler_with(c, s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002354 }
2355 return 1;
2356}
2357
2358static int
2359unaryop(unaryop_ty op)
2360{
2361 switch (op) {
2362 case Invert:
2363 return UNARY_INVERT;
2364 case Not:
2365 return UNARY_NOT;
2366 case UAdd:
2367 return UNARY_POSITIVE;
2368 case USub:
2369 return UNARY_NEGATIVE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002370 default:
2371 PyErr_Format(PyExc_SystemError,
2372 "unary op %d should not be possible", op);
2373 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002374 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002375}
2376
2377static int
2378binop(struct compiler *c, operator_ty op)
2379{
2380 switch (op) {
2381 case Add:
2382 return BINARY_ADD;
2383 case Sub:
2384 return BINARY_SUBTRACT;
2385 case Mult:
2386 return BINARY_MULTIPLY;
2387 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002388 return BINARY_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002389 case Mod:
2390 return BINARY_MODULO;
2391 case Pow:
2392 return BINARY_POWER;
2393 case LShift:
2394 return BINARY_LSHIFT;
2395 case RShift:
2396 return BINARY_RSHIFT;
2397 case BitOr:
2398 return BINARY_OR;
2399 case BitXor:
2400 return BINARY_XOR;
2401 case BitAnd:
2402 return BINARY_AND;
2403 case FloorDiv:
2404 return BINARY_FLOOR_DIVIDE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002405 default:
2406 PyErr_Format(PyExc_SystemError,
2407 "binary op %d should not be possible", op);
2408 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002410}
2411
2412static int
2413cmpop(cmpop_ty op)
2414{
2415 switch (op) {
2416 case Eq:
2417 return PyCmp_EQ;
2418 case NotEq:
2419 return PyCmp_NE;
2420 case Lt:
2421 return PyCmp_LT;
2422 case LtE:
2423 return PyCmp_LE;
2424 case Gt:
2425 return PyCmp_GT;
2426 case GtE:
2427 return PyCmp_GE;
2428 case Is:
2429 return PyCmp_IS;
2430 case IsNot:
2431 return PyCmp_IS_NOT;
2432 case In:
2433 return PyCmp_IN;
2434 case NotIn:
2435 return PyCmp_NOT_IN;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002436 default:
2437 return PyCmp_BAD;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002438 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439}
2440
2441static int
2442inplace_binop(struct compiler *c, operator_ty op)
2443{
2444 switch (op) {
2445 case Add:
2446 return INPLACE_ADD;
2447 case Sub:
2448 return INPLACE_SUBTRACT;
2449 case Mult:
2450 return INPLACE_MULTIPLY;
2451 case Div:
Guido van Rossum45aecf42006-03-15 04:58:47 +00002452 return INPLACE_TRUE_DIVIDE;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002453 case Mod:
2454 return INPLACE_MODULO;
2455 case Pow:
2456 return INPLACE_POWER;
2457 case LShift:
2458 return INPLACE_LSHIFT;
2459 case RShift:
2460 return INPLACE_RSHIFT;
2461 case BitOr:
2462 return INPLACE_OR;
2463 case BitXor:
2464 return INPLACE_XOR;
2465 case BitAnd:
2466 return INPLACE_AND;
2467 case FloorDiv:
2468 return INPLACE_FLOOR_DIVIDE;
Martin v. Löwis618dc5e2008-03-30 20:03:44 +00002469 default:
2470 PyErr_Format(PyExc_SystemError,
2471 "inplace binary op %d should not be possible", op);
2472 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002473 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002474}
2475
2476static int
2477compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2478{
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002479 int op, scope, arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002480 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2481
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002482 PyObject *dict = c->u->u_names;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002483 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002484 /* XXX AugStore isn't used anywhere! */
2485
Guido van Rossumcd16bf62007-06-13 18:07:49 +00002486 mangled = _Py_Mangle(c->u->u_private, name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002487 if (!mangled)
2488 return 0;
2489
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490 op = 0;
2491 optype = OP_NAME;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002492 scope = PyST_GetScope(c->u->u_ste, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002493 switch (scope) {
2494 case FREE:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002495 dict = c->u->u_freevars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002496 optype = OP_DEREF;
2497 break;
2498 case CELL:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002499 dict = c->u->u_cellvars;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002500 optype = OP_DEREF;
2501 break;
2502 case LOCAL:
2503 if (c->u->u_ste->ste_type == FunctionBlock)
2504 optype = OP_FAST;
2505 break;
2506 case GLOBAL_IMPLICIT:
Neil Schemenauerd403c452005-10-23 04:24:49 +00002507 if (c->u->u_ste->ste_type == FunctionBlock &&
2508 !c->u->u_ste->ste_unoptimized)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509 optype = OP_GLOBAL;
2510 break;
2511 case GLOBAL_EXPLICIT:
2512 optype = OP_GLOBAL;
2513 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002514 default:
2515 /* scope can be 0 */
2516 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002517 }
2518
2519 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwis5b222132007-06-10 09:51:05 +00002520 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002521
2522 switch (optype) {
2523 case OP_DEREF:
2524 switch (ctx) {
2525 case Load: op = LOAD_DEREF; break;
2526 case Store: op = STORE_DEREF; break;
2527 case AugLoad:
2528 case AugStore:
2529 break;
2530 case Del:
2531 PyErr_Format(PyExc_SyntaxError,
Walter Dörwald573c08c2007-05-25 15:46:59 +00002532 "can not delete variable '%S' referenced "
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533 "in nested scope",
Walter Dörwald573c08c2007-05-25 15:46:59 +00002534 name);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002535 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002537 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002538 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002539 PyErr_SetString(PyExc_SystemError,
2540 "param invalid for deref variable");
2541 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002542 }
2543 break;
2544 case OP_FAST:
2545 switch (ctx) {
2546 case Load: op = LOAD_FAST; break;
2547 case Store: op = STORE_FAST; break;
2548 case Del: op = DELETE_FAST; break;
2549 case AugLoad:
2550 case AugStore:
2551 break;
2552 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002553 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002554 PyErr_SetString(PyExc_SystemError,
2555 "param invalid for local variable");
2556 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002557 }
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002558 ADDOP_O(c, op, mangled, varnames);
2559 Py_DECREF(mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002560 return 1;
2561 case OP_GLOBAL:
2562 switch (ctx) {
2563 case Load: op = LOAD_GLOBAL; break;
2564 case Store: op = STORE_GLOBAL; break;
2565 case Del: op = DELETE_GLOBAL; break;
2566 case AugLoad:
2567 case AugStore:
2568 break;
2569 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002570 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002571 PyErr_SetString(PyExc_SystemError,
2572 "param invalid for global variable");
2573 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002574 }
2575 break;
2576 case OP_NAME:
2577 switch (ctx) {
2578 case Load: op = LOAD_NAME; break;
2579 case Store: op = STORE_NAME; break;
2580 case Del: op = DELETE_NAME; break;
2581 case AugLoad:
2582 case AugStore:
2583 break;
2584 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00002585 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00002586 PyErr_SetString(PyExc_SystemError,
2587 "param invalid for name variable");
2588 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002589 }
2590 break;
2591 }
2592
2593 assert(op);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002594 arg = compiler_add_o(c, dict, mangled);
Neal Norwitz4737b232005-11-19 23:58:29 +00002595 Py_DECREF(mangled);
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002596 if (arg < 0)
2597 return 0;
Neil Schemenauerdad06a12005-10-23 18:52:36 +00002598 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599}
2600
2601static int
2602compiler_boolop(struct compiler *c, expr_ty e)
2603{
2604 basicblock *end;
2605 int jumpi, i, n;
2606 asdl_seq *s;
2607
2608 assert(e->kind == BoolOp_kind);
2609 if (e->v.BoolOp.op == And)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002610 jumpi = JUMP_IF_FALSE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611 else
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002612 jumpi = JUMP_IF_TRUE_OR_POP;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002613 end = compiler_new_block(c);
Martin v. Löwis94962612006-01-02 21:15:05 +00002614 if (end == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002615 return 0;
2616 s = e->v.BoolOp.values;
2617 n = asdl_seq_LEN(s) - 1;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00002618 assert(n >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002619 for (i = 0; i < n; ++i) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002620 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002621 ADDOP_JABS(c, jumpi, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002623 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002624 compiler_use_next_block(c, end);
2625 return 1;
2626}
2627
2628static int
2629compiler_list(struct compiler *c, expr_ty e)
2630{
2631 int n = asdl_seq_LEN(e->v.List.elts);
2632 if (e->v.List.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002633 int i, seen_star = 0;
2634 for (i = 0; i < n; i++) {
2635 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2636 if (elt->kind == Starred_kind && !seen_star) {
Thomas Woutersdeef6742008-03-14 17:16:59 +00002637 if ((i >= (1 << 8)) ||
2638 (n-i-1 >= (INT_MAX >> 8)))
2639 return compiler_error(c,
2640 "too many expressions in "
2641 "star-unpacking assignment");
Guido van Rossum0368b722007-05-11 16:50:42 +00002642 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2643 seen_star = 1;
2644 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2645 } else if (elt->kind == Starred_kind) {
2646 return compiler_error(c,
2647 "two starred expressions in assignment");
2648 }
2649 }
2650 if (!seen_star) {
2651 ADDOP_I(c, UNPACK_SEQUENCE, n);
2652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002653 }
2654 VISIT_SEQ(c, expr, e->v.List.elts);
2655 if (e->v.List.ctx == Load) {
2656 ADDOP_I(c, BUILD_LIST, n);
2657 }
2658 return 1;
2659}
2660
2661static int
2662compiler_tuple(struct compiler *c, expr_ty e)
2663{
2664 int n = asdl_seq_LEN(e->v.Tuple.elts);
2665 if (e->v.Tuple.ctx == Store) {
Guido van Rossum0368b722007-05-11 16:50:42 +00002666 int i, seen_star = 0;
2667 for (i = 0; i < n; i++) {
2668 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2669 if (elt->kind == Starred_kind && !seen_star) {
Thomas Woutersdeef6742008-03-14 17:16:59 +00002670 if ((i >= (1 << 8)) ||
2671 (n-i-1 >= (INT_MAX >> 8)))
2672 return compiler_error(c,
2673 "too many expressions in "
2674 "star-unpacking assignment");
Guido van Rossum0368b722007-05-11 16:50:42 +00002675 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2676 seen_star = 1;
2677 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2678 } else if (elt->kind == Starred_kind) {
2679 return compiler_error(c,
2680 "two starred expressions in assignment");
2681 }
2682 }
2683 if (!seen_star) {
2684 ADDOP_I(c, UNPACK_SEQUENCE, n);
2685 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686 }
2687 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2688 if (e->v.Tuple.ctx == Load) {
2689 ADDOP_I(c, BUILD_TUPLE, n);
2690 }
2691 return 1;
2692}
2693
2694static int
2695compiler_compare(struct compiler *c, expr_ty e)
2696{
2697 int i, n;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002698 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002699
2700 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2701 VISIT(c, expr, e->v.Compare.left);
2702 n = asdl_seq_LEN(e->v.Compare.ops);
2703 assert(n > 0);
2704 if (n > 1) {
2705 cleanup = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002706 if (cleanup == NULL)
2707 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002708 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002709 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002710 }
2711 for (i = 1; i < n; i++) {
2712 ADDOP(c, DUP_TOP);
2713 ADDOP(c, ROT_THREE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002714 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002715 cmpop((cmpop_ty)(asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00002716 e->v.Compare.ops, i - 1))));
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002717 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718 NEXT_BLOCK(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002719 if (i < (n - 1))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002720 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00002721 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002723 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002724 ADDOP_I(c, COMPARE_OP,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002725 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002726 if (n > 1) {
2727 basicblock *end = compiler_new_block(c);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002728 if (end == NULL)
2729 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002730 ADDOP_JREL(c, JUMP_FORWARD, end);
2731 compiler_use_next_block(c, cleanup);
2732 ADDOP(c, ROT_TWO);
2733 ADDOP(c, POP_TOP);
2734 compiler_use_next_block(c, end);
2735 }
2736 return 1;
2737}
2738
2739static int
2740compiler_call(struct compiler *c, expr_ty e)
2741{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002742 VISIT(c, expr, e->v.Call.func);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002743 return compiler_call_helper(c, 0,
2744 e->v.Call.args,
2745 e->v.Call.keywords,
2746 e->v.Call.starargs,
2747 e->v.Call.kwargs);
2748}
2749
2750/* shared code between compiler_call and compiler_class */
2751static int
2752compiler_call_helper(struct compiler *c,
2753 int n, /* Args already pushed */
2754 asdl_seq *args,
2755 asdl_seq *keywords,
2756 expr_ty starargs,
2757 expr_ty kwargs)
2758{
2759 int code = 0;
2760
2761 n += asdl_seq_LEN(args);
2762 VISIT_SEQ(c, expr, args);
2763 if (keywords) {
2764 VISIT_SEQ(c, keyword, keywords);
2765 n |= asdl_seq_LEN(keywords) << 8;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002766 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002767 if (starargs) {
2768 VISIT(c, expr, starargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002769 code |= 1;
2770 }
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002771 if (kwargs) {
2772 VISIT(c, expr, kwargs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773 code |= 2;
2774 }
2775 switch (code) {
2776 case 0:
2777 ADDOP_I(c, CALL_FUNCTION, n);
2778 break;
2779 case 1:
2780 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2781 break;
2782 case 2:
2783 ADDOP_I(c, CALL_FUNCTION_KW, n);
2784 break;
2785 case 3:
2786 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2787 break;
2788 }
2789 return 1;
2790}
2791
Nick Coghlan650f0d02007-04-15 12:05:43 +00002792
2793/* List and set comprehensions and generator expressions work by creating a
2794 nested function to perform the actual iteration. This means that the
2795 iteration variables don't leak into the current scope.
2796 The defined function is called immediately following its definition, with the
2797 result of that call being the result of the expression.
2798 The LC/SC version returns the populated container, while the GE version is
2799 flagged in symtable.c as a generator, so it returns the generator object
2800 when the function is called.
2801 This code *knows* that the loop cannot contain break, continue, or return,
2802 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2803
2804 Possible cleanups:
2805 - iterate over the generator sequence instead of using recursion
2806*/
2807
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002808static int
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002809compiler_comprehension_generator(struct compiler *c,
Nick Coghlan650f0d02007-04-15 12:05:43 +00002810 asdl_seq *generators, int gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002811 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002812{
2813 /* generate code for the iterator, then each of the ifs,
2814 and then write to the element */
2815
Nick Coghlan650f0d02007-04-15 12:05:43 +00002816 comprehension_ty gen;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002817 basicblock *start, *anchor, *skip, *if_cleanup;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002818 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002819
2820 start = compiler_new_block(c);
2821 skip = compiler_new_block(c);
2822 if_cleanup = compiler_new_block(c);
2823 anchor = compiler_new_block(c);
2824
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002825 if (start == NULL || skip == NULL || if_cleanup == NULL ||
Nick Coghlan650f0d02007-04-15 12:05:43 +00002826 anchor == NULL)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002828
Nick Coghlan650f0d02007-04-15 12:05:43 +00002829 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831 if (gen_index == 0) {
2832 /* Receive outermost iter as an implicit argument */
2833 c->u->u_argcount = 1;
2834 ADDOP_I(c, LOAD_FAST, 0);
2835 }
2836 else {
2837 /* Sub-iter - calculate on the fly */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002838 VISIT(c, expr, gen->iter);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839 ADDOP(c, GET_ITER);
2840 }
2841 compiler_use_next_block(c, start);
2842 ADDOP_JREL(c, FOR_ITER, anchor);
2843 NEXT_BLOCK(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002844 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002846 /* XXX this needs to be cleaned up...a lot! */
Nick Coghlan650f0d02007-04-15 12:05:43 +00002847 n = asdl_seq_LEN(gen->ifs);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848 for (i = 0; i < n; i++) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002849 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002850 VISIT(c, expr, e);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002851 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852 NEXT_BLOCK(c);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002854
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002855 if (++gen_index < asdl_seq_LEN(generators))
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002856 if (!compiler_comprehension_generator(c,
Nick Coghlan650f0d02007-04-15 12:05:43 +00002857 generators, gen_index,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002858 elt, val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002859 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002860
Nick Coghlan650f0d02007-04-15 12:05:43 +00002861 /* only append after the last for generator */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002862 if (gen_index >= asdl_seq_LEN(generators)) {
Nick Coghlan650f0d02007-04-15 12:05:43 +00002863 /* comprehension specific code */
2864 switch (type) {
2865 case COMP_GENEXP:
2866 VISIT(c, expr, elt);
2867 ADDOP(c, YIELD_VALUE);
2868 ADDOP(c, POP_TOP);
2869 break;
2870 case COMP_LISTCOMP:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002871 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002872 ADDOP_I(c, LIST_APPEND, gen_index + 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002873 break;
2874 case COMP_SETCOMP:
Nick Coghlan650f0d02007-04-15 12:05:43 +00002875 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002876 ADDOP_I(c, SET_ADD, gen_index + 1);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002877 break;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002878 case COMP_DICTCOMP:
Guido van Rossum992d4a32007-07-11 13:09:30 +00002879 /* With 'd[k] = v', v is evaluated before k, so we do
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002880 the same. */
Guido van Rossum992d4a32007-07-11 13:09:30 +00002881 VISIT(c, expr, val);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002882 VISIT(c, expr, elt);
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002883 ADDOP_I(c, MAP_ADD, gen_index + 1);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002884 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002885 default:
2886 return 0;
2887 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888
2889 compiler_use_next_block(c, skip);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00002890 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002891 compiler_use_next_block(c, if_cleanup);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2893 compiler_use_next_block(c, anchor);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002894
2895 return 1;
2896}
2897
2898static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002899compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002900 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002901{
2902 PyCodeObject *co = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002903 expr_ty outermost_iter;
2904
2905 outermost_iter = ((comprehension_ty)
2906 asdl_seq_GET(generators, 0))->iter;
2907
2908 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2909 goto error;
2910
2911 if (type != COMP_GENEXP) {
Guido van Rossum992d4a32007-07-11 13:09:30 +00002912 int op;
Guido van Rossum992d4a32007-07-11 13:09:30 +00002913 switch (type) {
2914 case COMP_LISTCOMP:
2915 op = BUILD_LIST;
2916 break;
2917 case COMP_SETCOMP:
2918 op = BUILD_SET;
2919 break;
2920 case COMP_DICTCOMP:
2921 op = BUILD_MAP;
2922 break;
2923 default:
2924 PyErr_Format(PyExc_SystemError,
2925 "unknown comprehension type %d", type);
2926 goto error_in_scope;
2927 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002928
Guido van Rossum992d4a32007-07-11 13:09:30 +00002929 ADDOP_I(c, op, 0);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002930 }
2931
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002932 if (!compiler_comprehension_generator(c, generators, 0, elt,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002933 val, type))
Nick Coghlan650f0d02007-04-15 12:05:43 +00002934 goto error_in_scope;
2935
2936 if (type != COMP_GENEXP) {
2937 ADDOP(c, RETURN_VALUE);
2938 }
2939
2940 co = assemble(c, 1);
2941 compiler_exit_scope(c);
2942 if (co == NULL)
2943 goto error;
2944
2945 if (!compiler_make_closure(c, co, 0))
2946 goto error;
2947 Py_DECREF(co);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002948
2949 VISIT(c, expr, outermost_iter);
2950 ADDOP(c, GET_ITER);
2951 ADDOP_I(c, CALL_FUNCTION, 1);
2952 return 1;
2953error_in_scope:
2954 compiler_exit_scope(c);
2955error:
2956 Py_XDECREF(co);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002957 return 0;
2958}
2959
2960static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961compiler_genexp(struct compiler *c, expr_ty e)
2962{
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002963 static identifier name;
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002964 if (!name) {
Alexandre Vassalottie9f305f2008-05-16 04:39:54 +00002965 name = PyUnicode_FromString("<genexpr>");
Nick Coghlan944d3eb2005-11-16 12:46:55 +00002966 if (!name)
2967 return 0;
2968 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002969 assert(e->kind == GeneratorExp_kind);
2970 return compiler_comprehension(c, e, COMP_GENEXP, name,
2971 e->v.GeneratorExp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002972 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002973}
2974
2975static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002976compiler_listcomp(struct compiler *c, expr_ty e)
2977{
2978 static identifier name;
2979 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002980 name = PyUnicode_FromString("<listcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002981 if (!name)
2982 return 0;
2983 }
2984 assert(e->kind == ListComp_kind);
2985 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2986 e->v.ListComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00002987 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002988}
2989
2990static int
2991compiler_setcomp(struct compiler *c, expr_ty e)
2992{
2993 static identifier name;
2994 if (!name) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00002995 name = PyUnicode_FromString("<setcomp>");
Nick Coghlan650f0d02007-04-15 12:05:43 +00002996 if (!name)
2997 return 0;
2998 }
2999 assert(e->kind == SetComp_kind);
3000 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3001 e->v.SetComp.generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00003002 e->v.SetComp.elt, NULL);
3003}
3004
3005
3006static int
3007compiler_dictcomp(struct compiler *c, expr_ty e)
3008{
3009 static identifier name;
3010 if (!name) {
Neal Norwitz41103bf2007-08-24 23:12:06 +00003011 name = PyUnicode_FromString("<dictcomp>");
Guido van Rossum992d4a32007-07-11 13:09:30 +00003012 if (!name)
3013 return 0;
3014 }
3015 assert(e->kind == DictComp_kind);
3016 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3017 e->v.DictComp.generators,
3018 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003019}
3020
3021
3022static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023compiler_visit_keyword(struct compiler *c, keyword_ty k)
3024{
3025 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3026 VISIT(c, expr, k->value);
3027 return 1;
3028}
3029
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003030/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003031 whether they are true or false.
3032
3033 Return values: 1 for true, 0 for false, -1 for non-constant.
3034 */
3035
3036static int
3037expr_constant(expr_ty e)
3038{
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003039 char *id;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003040 switch (e->kind) {
Georg Brandl52318d62006-09-06 07:06:08 +00003041 case Ellipsis_kind:
3042 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043 case Num_kind:
3044 return PyObject_IsTrue(e->v.Num.n);
3045 case Str_kind:
3046 return PyObject_IsTrue(e->v.Str.s);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003047 case Name_kind:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003048 /* optimize away names that can't be reassigned */
Christian Heimes72b710a2008-05-26 13:28:38 +00003049 id = PyBytes_AS_STRING(
Guido van Rossumbdbb3952007-06-14 00:28:01 +00003050 _PyUnicode_AsDefaultEncodedString(e->v.Name.id, NULL));
Guido van Rossumcd16bf62007-06-13 18:07:49 +00003051 if (strcmp(id, "True") == 0) return 1;
3052 if (strcmp(id, "False") == 0) return 0;
3053 if (strcmp(id, "None") == 0) return 0;
3054 if (strcmp(id, "__debug__") == 0)
3055 return ! Py_OptimizeFlag;
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00003056 /* fall through */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057 default:
3058 return -1;
3059 }
3060}
3061
Guido van Rossumc2e20742006-02-27 22:32:47 +00003062/*
3063 Implements the with statement from PEP 343.
3064
3065 The semantics outlined in that PEP are as follows:
3066
3067 with EXPR as VAR:
3068 BLOCK
3069
3070 It is implemented roughly as:
3071
Thomas Wouters477c8d52006-05-27 19:21:47 +00003072 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 exit = context.__exit__ # not calling it
3074 value = context.__enter__()
3075 try:
3076 VAR = value # if VAR present in the syntax
3077 BLOCK
3078 finally:
3079 if an exception was raised:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003080 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003081 else:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003082 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083 exit(*exc)
3084 */
3085static int
3086compiler_with(struct compiler *c, stmt_ty s)
3087{
Thomas Wouters477c8d52006-05-27 19:21:47 +00003088 static identifier enter_attr, exit_attr;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089 basicblock *block, *finally;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00003090 identifier tmpvalue = NULL, tmpexit = NULL;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003091
3092 assert(s->kind == With_kind);
3093
Guido van Rossumc2e20742006-02-27 22:32:47 +00003094 if (!enter_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003095 enter_attr = PyUnicode_InternFromString("__enter__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003096 if (!enter_attr)
3097 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003098 }
3099 if (!exit_attr) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003100 exit_attr = PyUnicode_InternFromString("__exit__");
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003101 if (!exit_attr)
3102 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003103 }
3104
3105 block = compiler_new_block(c);
3106 finally = compiler_new_block(c);
3107 if (!block || !finally)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003108 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003109
Guido van Rossumc2e20742006-02-27 22:32:47 +00003110 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003111 /* Create a temporary variable to hold context.__enter__().
Guido van Rossumc2e20742006-02-27 22:32:47 +00003112 We need to do this rather than preserving it on the stack
3113 because SETUP_FINALLY remembers the stack level.
3114 We need to do the assignment *inside* the try/finally
3115 so that context.__exit__() is called when the assignment
3116 fails. But we need to call context.__enter__() *before*
3117 the try/finally so that if it fails we won't call
3118 context.__exit__().
3119 */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003120 tmpvalue = compiler_new_tmpname(c);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003121 if (tmpvalue == NULL)
3122 return 0;
3123 PyArena_AddPyObject(c->c_arena, tmpvalue);
3124 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00003125 tmpexit = compiler_new_tmpname(c);
3126 if (tmpexit == NULL)
3127 return 0;
3128 PyArena_AddPyObject(c->c_arena, tmpexit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003129
Thomas Wouters477c8d52006-05-27 19:21:47 +00003130 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003131 VISIT(c, expr, s->v.With.context_expr);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003132
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003133 /* Squirrel away context.__exit__ by stuffing it under context */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003134 ADDOP(c, DUP_TOP);
3135 ADDOP_O(c, LOAD_ATTR, exit_attr, names);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00003136 if (!compiler_nameop(c, tmpexit, Store))
3137 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003138
3139 /* Call context.__enter__() */
3140 ADDOP_O(c, LOAD_ATTR, enter_attr, names);
3141 ADDOP_I(c, CALL_FUNCTION, 0);
3142
3143 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003144 /* Store it in tmpvalue */
3145 if (!compiler_nameop(c, tmpvalue, Store))
Guido van Rossumc2e20742006-02-27 22:32:47 +00003146 return 0;
3147 }
3148 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003149 /* Discard result from context.__enter__() */
3150 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003151 }
3152
3153 /* Start the try block */
3154 ADDOP_JREL(c, SETUP_FINALLY, finally);
3155
3156 compiler_use_next_block(c, block);
3157 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003158 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003159 }
3160
3161 if (s->v.With.optional_vars) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003162 /* Bind saved result of context.__enter__() to VAR */
3163 if (!compiler_nameop(c, tmpvalue, Load) ||
Guido van Rossumc2e20742006-02-27 22:32:47 +00003164 !compiler_nameop(c, tmpvalue, Del))
3165 return 0;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003166 VISIT(c, expr, s->v.With.optional_vars);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003167 }
3168
3169 /* BLOCK code */
3170 VISIT_SEQ(c, stmt, s->v.With.body);
3171
3172 /* End of try block; start the finally block */
3173 ADDOP(c, POP_BLOCK);
3174 compiler_pop_fblock(c, FINALLY_TRY, block);
3175
3176 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3177 compiler_use_next_block(c, finally);
3178 if (!compiler_push_fblock(c, FINALLY_END, finally))
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003179 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003180
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003181 /* Finally block starts; context.__exit__ is on the stack under
3182 the exception or return information. Just issue our magic
3183 opcode. */
Benjamin Petersoneec3d712008-06-11 15:59:43 +00003184 if (!compiler_nameop(c, tmpexit, Load) ||
3185 !compiler_nameop(c, tmpexit, Del))
3186 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003187 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003188
3189 /* Finally block ends. */
3190 ADDOP(c, END_FINALLY);
3191 compiler_pop_fblock(c, FINALLY_END, finally);
3192 return 1;
3193}
3194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003195static int
3196compiler_visit_expr(struct compiler *c, expr_ty e)
3197{
3198 int i, n;
3199
Thomas Wouters89f507f2006-12-13 04:49:30 +00003200 /* If expr e has a different line number than the last expr/stmt,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003201 set a new line number for the next instruction.
3202 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003203 if (e->lineno > c->u->u_lineno) {
3204 c->u->u_lineno = e->lineno;
Neal Norwitz6baa4c42007-02-26 19:14:12 +00003205 c->u->u_lineno_set = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206 }
3207 switch (e->kind) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003208 case BoolOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209 return compiler_boolop(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003210 case BinOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003211 VISIT(c, expr, e->v.BinOp.left);
3212 VISIT(c, expr, e->v.BinOp.right);
3213 ADDOP(c, binop(c, e->v.BinOp.op));
3214 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003215 case UnaryOp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216 VISIT(c, expr, e->v.UnaryOp.operand);
3217 ADDOP(c, unaryop(e->v.UnaryOp.op));
3218 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003219 case Lambda_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003220 return compiler_lambda(c, e);
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00003221 case IfExp_kind:
3222 return compiler_ifexp(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003223 case Dict_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003224 n = asdl_seq_LEN(e->v.Dict.values);
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003225 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003227 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003228 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003229 VISIT(c, expr,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003230 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Christian Heimes99170a52007-12-19 02:07:34 +00003231 ADDOP(c, STORE_MAP);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003232 }
3233 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00003234 case Set_kind:
3235 n = asdl_seq_LEN(e->v.Set.elts);
3236 VISIT_SEQ(c, expr, e->v.Set.elts);
3237 ADDOP_I(c, BUILD_SET, n);
3238 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003239 case GeneratorExp_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003240 return compiler_genexp(c, e);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003241 case ListComp_kind:
3242 return compiler_listcomp(c, e);
3243 case SetComp_kind:
3244 return compiler_setcomp(c, e);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003245 case DictComp_kind:
3246 return compiler_dictcomp(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003247 case Yield_kind:
3248 if (c->u->u_ste->ste_type != FunctionBlock)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003249 return compiler_error(c, "'yield' outside function");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003250 if (e->v.Yield.value) {
3251 VISIT(c, expr, e->v.Yield.value);
3252 }
3253 else {
3254 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3255 }
3256 ADDOP(c, YIELD_VALUE);
3257 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003258 case Compare_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259 return compiler_compare(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003260 case Call_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261 return compiler_call(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003262 case Num_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003263 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3264 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003265 case Str_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3267 break;
Thomas Wouters00e41de2007-02-23 19:56:57 +00003268 case Bytes_kind:
3269 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
Thomas Wouters00e41de2007-02-23 19:56:57 +00003270 break;
Georg Brandl52318d62006-09-06 07:06:08 +00003271 case Ellipsis_kind:
3272 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3273 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274 /* The following exprs can be assignment targets. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003275 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003276 if (e->v.Attribute.ctx != AugStore)
3277 VISIT(c, expr, e->v.Attribute.value);
3278 switch (e->v.Attribute.ctx) {
3279 case AugLoad:
3280 ADDOP(c, DUP_TOP);
3281 /* Fall through to load */
3282 case Load:
3283 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3284 break;
3285 case AugStore:
3286 ADDOP(c, ROT_TWO);
3287 /* Fall through to save */
3288 case Store:
3289 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3290 break;
3291 case Del:
3292 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3293 break;
3294 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003295 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003296 PyErr_SetString(PyExc_SystemError,
3297 "param invalid in attribute expression");
3298 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299 }
3300 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003301 case Subscript_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003302 switch (e->v.Subscript.ctx) {
3303 case AugLoad:
3304 VISIT(c, expr, e->v.Subscript.value);
3305 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3306 break;
3307 case Load:
3308 VISIT(c, expr, e->v.Subscript.value);
3309 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3310 break;
3311 case AugStore:
3312 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3313 break;
3314 case Store:
3315 VISIT(c, expr, e->v.Subscript.value);
3316 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3317 break;
3318 case Del:
3319 VISIT(c, expr, e->v.Subscript.value);
3320 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3321 break;
3322 case Param:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003323 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003324 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003325 "param invalid in subscript expression");
Neal Norwitz4737b232005-11-19 23:58:29 +00003326 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003327 }
3328 break;
Guido van Rossum0368b722007-05-11 16:50:42 +00003329 case Starred_kind:
3330 switch (e->v.Starred.ctx) {
3331 case Store:
3332 /* In all legitimate cases, the Starred node was already replaced
3333 * by compiler_list/compiler_tuple. XXX: is that okay? */
3334 return compiler_error(c,
3335 "starred assignment target must be in a list or tuple");
3336 default:
3337 return compiler_error(c,
3338 "can use starred expression only as assignment target");
3339 }
3340 break;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003341 case Name_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003342 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3343 /* child nodes of List and Tuple will have expr_context set */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003344 case List_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345 return compiler_list(c, e);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003346 case Tuple_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347 return compiler_tuple(c, e);
3348 }
3349 return 1;
3350}
3351
3352static int
3353compiler_augassign(struct compiler *c, stmt_ty s)
3354{
3355 expr_ty e = s->v.AugAssign.target;
3356 expr_ty auge;
3357
3358 assert(s->kind == AugAssign_kind);
3359
3360 switch (e->kind) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003361 case Attribute_kind:
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003363 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003364 if (auge == NULL)
3365 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366 VISIT(c, expr, auge);
3367 VISIT(c, expr, s->v.AugAssign.value);
3368 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3369 auge->v.Attribute.ctx = AugStore;
3370 VISIT(c, expr, auge);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371 break;
3372 case Subscript_kind:
3373 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
Martin v. Löwis49c5da12006-03-01 22:49:05 +00003374 AugLoad, e->lineno, e->col_offset, c->c_arena);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003375 if (auge == NULL)
3376 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003377 VISIT(c, expr, auge);
3378 VISIT(c, expr, s->v.AugAssign.value);
3379 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003380 auge->v.Subscript.ctx = AugStore;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381 VISIT(c, expr, auge);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003382 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003383 case Name_kind:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003384 if (!compiler_nameop(c, e->v.Name.id, Load))
3385 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 VISIT(c, expr, s->v.AugAssign.value);
3387 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3388 return compiler_nameop(c, e->v.Name.id, Store);
3389 default:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003390 PyErr_Format(PyExc_SystemError,
3391 "invalid node type (%d) for augmented assignment",
3392 e->kind);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003393 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003394 }
3395 return 1;
3396}
3397
3398static int
3399compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3400{
3401 struct fblockinfo *f;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003402 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3403 PyErr_SetString(PyExc_SystemError,
3404 "too many statically nested blocks");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003406 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003407 f = &c->u->u_fblock[c->u->u_nfblocks++];
3408 f->fb_type = t;
3409 f->fb_block = b;
3410 return 1;
3411}
3412
3413static void
3414compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3415{
3416 struct compiler_unit *u = c->u;
3417 assert(u->u_nfblocks > 0);
3418 u->u_nfblocks--;
3419 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3420 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
3421}
3422
Thomas Wouters89f507f2006-12-13 04:49:30 +00003423static int
3424compiler_in_loop(struct compiler *c) {
Guido van Rossumd8faa362007-04-27 19:54:29 +00003425 int i;
3426 struct compiler_unit *u = c->u;
3427 for (i = 0; i < u->u_nfblocks; ++i) {
3428 if (u->u_fblock[i].fb_type == LOOP)
3429 return 1;
3430 }
3431 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003432}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003433/* Raises a SyntaxError and returns 0.
3434 If something goes wrong, a different exception may be raised.
3435*/
3436
3437static int
3438compiler_error(struct compiler *c, const char *errstr)
3439{
3440 PyObject *loc;
3441 PyObject *u = NULL, *v = NULL;
3442
3443 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3444 if (!loc) {
3445 Py_INCREF(Py_None);
3446 loc = Py_None;
3447 }
3448 u = Py_BuildValue("(ziOO)", c->c_filename, c->u->u_lineno,
3449 Py_None, loc);
3450 if (!u)
3451 goto exit;
3452 v = Py_BuildValue("(zO)", errstr, u);
3453 if (!v)
3454 goto exit;
3455 PyErr_SetObject(PyExc_SyntaxError, v);
3456 exit:
3457 Py_DECREF(loc);
3458 Py_XDECREF(u);
3459 Py_XDECREF(v);
3460 return 0;
3461}
3462
3463static int
3464compiler_handle_subscr(struct compiler *c, const char *kind,
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003465 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003466{
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003467 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003469 /* XXX this code is duplicated */
3470 switch (ctx) {
3471 case AugLoad: /* fall through to Load */
3472 case Load: op = BINARY_SUBSCR; break;
3473 case AugStore:/* fall through to Store */
3474 case Store: op = STORE_SUBSCR; break;
3475 case Del: op = DELETE_SUBSCR; break;
3476 case Param:
3477 PyErr_Format(PyExc_SystemError,
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003478 "invalid %s kind %d in subscript\n",
3479 kind, ctx);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003480 return 0;
3481 }
3482 if (ctx == AugLoad) {
3483 ADDOP_I(c, DUP_TOPX, 2);
3484 }
3485 else if (ctx == AugStore) {
3486 ADDOP(c, ROT_THREE);
3487 }
3488 ADDOP(c, op);
3489 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003490}
3491
3492static int
3493compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3494{
3495 int n = 2;
3496 assert(s->kind == Slice_kind);
3497
3498 /* only handles the cases where BUILD_SLICE is emitted */
3499 if (s->v.Slice.lower) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003500 VISIT(c, expr, s->v.Slice.lower);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003501 }
3502 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003503 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003504 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506 if (s->v.Slice.upper) {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003507 VISIT(c, expr, s->v.Slice.upper);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003508 }
3509 else {
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003510 ADDOP_O(c, LOAD_CONST, Py_None, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511 }
3512
3513 if (s->v.Slice.step) {
3514 n++;
3515 VISIT(c, expr, s->v.Slice.step);
3516 }
3517 ADDOP_I(c, BUILD_SLICE, n);
3518 return 1;
3519}
3520
3521static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3523 expr_context_ty ctx)
3524{
3525 switch (s->kind) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526 case Slice_kind:
3527 return compiler_slice(c, s, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528 case Index_kind:
3529 VISIT(c, expr, s->v.Index.value);
3530 break;
3531 case ExtSlice_kind:
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003532 default:
Neal Norwitz4737b232005-11-19 23:58:29 +00003533 PyErr_SetString(PyExc_SystemError,
3534 "extended slice invalid in nested slice");
3535 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003536 }
3537 return 1;
3538}
3539
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540static int
3541compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3542{
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003543 char * kindname = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003544 switch (s->kind) {
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003545 case Index_kind:
3546 kindname = "index";
3547 if (ctx != AugStore) {
3548 VISIT(c, expr, s->v.Index.value);
3549 }
3550 break;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551 case Slice_kind:
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003552 kindname = "slice";
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003553 if (ctx != AugStore) {
3554 if (!compiler_slice(c, s, ctx))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003555 return 0;
3556 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003557 break;
3558 case ExtSlice_kind:
3559 kindname = "extended slice";
3560 if (ctx != AugStore) {
3561 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3562 for (i = 0; i < n; i++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003563 slice_ty sub = (slice_ty)asdl_seq_GET(
Guido van Rossumd8faa362007-04-27 19:54:29 +00003564 s->v.ExtSlice.dims, i);
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003565 if (!compiler_visit_nested_slice(c, sub, ctx))
3566 return 0;
3567 }
3568 ADDOP_I(c, BUILD_TUPLE, n);
3569 }
3570 break;
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003571 default:
3572 PyErr_Format(PyExc_SystemError,
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003573 "invalid subscript kind %d", s->kind);
Neal Norwitz4e6bf492005-12-18 05:32:41 +00003574 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003575 }
Nick Coghlaneadee9a2006-03-13 12:31:58 +00003576 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577}
3578
Thomas Wouters89f507f2006-12-13 04:49:30 +00003579/* End of the compiler section, beginning of the assembler section */
3580
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003581/* do depth-first search of basic block graph, starting with block.
3582 post records the block indices in post-order.
3583
3584 XXX must handle implicit jumps from one block to next
3585*/
3586
Thomas Wouters89f507f2006-12-13 04:49:30 +00003587struct assembler {
3588 PyObject *a_bytecode; /* string containing bytecode */
3589 int a_offset; /* offset into bytecode */
3590 int a_nblocks; /* number of reachable blocks */
3591 basicblock **a_postorder; /* list of blocks in dfs postorder */
3592 PyObject *a_lnotab; /* string containing lnotab */
3593 int a_lnotab_off; /* offset into lnotab */
3594 int a_lineno; /* last lineno of emitted instruction */
3595 int a_lineno_off; /* bytecode offset of last lineno */
3596};
3597
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003598static void
3599dfs(struct compiler *c, basicblock *b, struct assembler *a)
3600{
3601 int i;
3602 struct instr *instr = NULL;
3603
3604 if (b->b_seen)
3605 return;
3606 b->b_seen = 1;
3607 if (b->b_next != NULL)
3608 dfs(c, b->b_next, a);
3609 for (i = 0; i < b->b_iused; i++) {
3610 instr = &b->b_instr[i];
3611 if (instr->i_jrel || instr->i_jabs)
3612 dfs(c, instr->i_target, a);
3613 }
3614 a->a_postorder[a->a_nblocks++] = b;
3615}
3616
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003617static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003618stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3619{
3620 int i;
3621 struct instr *instr;
3622 if (b->b_seen || b->b_startdepth >= depth)
3623 return maxdepth;
3624 b->b_seen = 1;
3625 b->b_startdepth = depth;
3626 for (i = 0; i < b->b_iused; i++) {
3627 instr = &b->b_instr[i];
3628 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3629 if (depth > maxdepth)
3630 maxdepth = depth;
3631 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3632 if (instr->i_jrel || instr->i_jabs) {
3633 maxdepth = stackdepth_walk(c, instr->i_target,
3634 depth, maxdepth);
3635 if (instr->i_opcode == JUMP_ABSOLUTE ||
3636 instr->i_opcode == JUMP_FORWARD) {
3637 goto out; /* remaining code is dead */
3638 }
3639 }
3640 }
3641 if (b->b_next)
3642 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
3643out:
3644 b->b_seen = 0;
3645 return maxdepth;
3646}
3647
3648/* Find the flow path that needs the largest stack. We assume that
3649 * cycles in the flow graph have no net effect on the stack depth.
3650 */
3651static int
3652stackdepth(struct compiler *c)
3653{
3654 basicblock *b, *entryblock;
3655 entryblock = NULL;
3656 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3657 b->b_seen = 0;
3658 b->b_startdepth = INT_MIN;
3659 entryblock = b;
3660 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003661 if (!entryblock)
3662 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003663 return stackdepth_walk(c, entryblock, 0, 0);
3664}
3665
3666static int
3667assemble_init(struct assembler *a, int nblocks, int firstlineno)
3668{
3669 memset(a, 0, sizeof(struct assembler));
3670 a->a_lineno = firstlineno;
Christian Heimes72b710a2008-05-26 13:28:38 +00003671 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672 if (!a->a_bytecode)
3673 return 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003674 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003675 if (!a->a_lnotab)
3676 return 0;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003677 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3678 PyErr_NoMemory();
3679 return 0;
3680 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681 a->a_postorder = (basicblock **)PyObject_Malloc(
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003682 sizeof(basicblock *) * nblocks);
Neal Norwitz87b801c2005-12-18 04:42:47 +00003683 if (!a->a_postorder) {
3684 PyErr_NoMemory();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685 return 0;
Neal Norwitz87b801c2005-12-18 04:42:47 +00003686 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003687 return 1;
3688}
3689
3690static void
3691assemble_free(struct assembler *a)
3692{
3693 Py_XDECREF(a->a_bytecode);
3694 Py_XDECREF(a->a_lnotab);
3695 if (a->a_postorder)
3696 PyObject_Free(a->a_postorder);
3697}
3698
3699/* Return the size of a basic block in bytes. */
3700
3701static int
3702instrsize(struct instr *instr)
3703{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003704 if (!instr->i_hasarg)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003705 return 1; /* 1 byte for the opcode*/
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003706 if (instr->i_oparg > 0xffff)
Christian Heimes5fb7c2a2007-12-24 08:52:31 +00003707 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3708 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003709}
3710
3711static int
3712blocksize(basicblock *b)
3713{
3714 int i;
3715 int size = 0;
3716
3717 for (i = 0; i < b->b_iused; i++)
3718 size += instrsize(&b->b_instr[i]);
3719 return size;
3720}
3721
3722/* All about a_lnotab.
3723
3724c_lnotab is an array of unsigned bytes disguised as a Python string.
3725It is used to map bytecode offsets to source code line #s (when needed
3726for tracebacks).
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003727
Tim Peters2a7f3842001-06-09 09:26:21 +00003728The array is conceptually a list of
3729 (bytecode offset increment, line number increment)
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003730pairs. The details are important and delicate, best illustrated by example:
Tim Peters2a7f3842001-06-09 09:26:21 +00003731
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003732 byte code offset source code line number
3733 0 1
3734 6 2
Tim Peters2a7f3842001-06-09 09:26:21 +00003735 50 7
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003736 350 307
3737 361 308
Tim Peters2a7f3842001-06-09 09:26:21 +00003738
3739The first trick is that these numbers aren't stored, only the increments
3740from one row to the next (this doesn't really work, but it's a start):
3741
3742 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
3743
3744The second trick is that an unsigned byte can't hold negative values, or
3745values larger than 255, so (a) there's a deep assumption that byte code
3746offsets and their corresponding line #s both increase monotonically, and (b)
3747if at least one column jumps by more than 255 from one row to the next, more
3748than one pair is written to the table. In case #b, there's no way to know
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003749from looking at the table later how many were written. That's the delicate
Tim Peters2a7f3842001-06-09 09:26:21 +00003750part. A user of c_lnotab desiring to find the source line number
3751corresponding to a bytecode address A should do something like this
3752
3753 lineno = addr = 0
3754 for addr_incr, line_incr in c_lnotab:
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003755 addr += addr_incr
3756 if addr > A:
3757 return lineno
3758 lineno += line_incr
Tim Peters2a7f3842001-06-09 09:26:21 +00003759
3760In order for this to work, when the addr field increments by more than 255,
3761the line # increment in each pair generated must be 0 until the remaining addr
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003762increment is < 256. So, in the example above, assemble_lnotab (it used
3763to be called com_set_lineno) should not (as was actually done until 2.2)
3764expand 300, 300 to 255, 255, 45, 45,
Guido van Rossumd8faa362007-04-27 19:54:29 +00003765 but to 255, 0, 45, 255, 0, 45.
Tim Peters2a7f3842001-06-09 09:26:21 +00003766*/
3767
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003768static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003770{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003771 int d_bytecode, d_lineno;
3772 int len;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003773 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003774
3775 d_bytecode = a->a_offset - a->a_lineno_off;
3776 d_lineno = i->i_lineno - a->a_lineno;
3777
3778 assert(d_bytecode >= 0);
3779 assert(d_lineno >= 0);
3780
Christian Heimes2202f872008-02-06 14:31:34 +00003781 if(d_bytecode == 0 && d_lineno == 0)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003782 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003783
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784 if (d_bytecode > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003785 int j, nbytes, ncodes = d_bytecode / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003786 nbytes = a->a_lnotab_off + 2 * ncodes;
Christian Heimes72b710a2008-05-26 13:28:38 +00003787 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788 if (nbytes >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003789 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790 len = nbytes;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003791 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003792 len *= 2;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003793 else {
3794 PyErr_NoMemory();
3795 return 0;
3796 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003797 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003798 return 0;
Guido van Rossum8b993a91997-01-17 21:04:03 +00003799 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003800 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003801 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003802 for (j = 0; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003803 *lnotab++ = 255;
3804 *lnotab++ = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003805 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003806 d_bytecode -= ncodes * 255;
3807 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003808 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003809 assert(d_bytecode <= 255);
3810 if (d_lineno > 255) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00003811 int j, nbytes, ncodes = d_lineno / 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003812 nbytes = a->a_lnotab_off + 2 * ncodes;
Christian Heimes72b710a2008-05-26 13:28:38 +00003813 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814 if (nbytes >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003815 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003816 len = nbytes;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003817 else if (len <= INT_MAX / 2)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003818 len *= 2;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003819 else {
3820 PyErr_NoMemory();
3821 return 0;
3822 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003823 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003824 return 0;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003825 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003826 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003827 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003828 *lnotab++ = d_bytecode;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003829 *lnotab++ = 255;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003830 d_bytecode = 0;
Neal Norwitz08b401f2006-01-07 21:24:09 +00003831 for (j = 1; j < ncodes; j++) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003832 *lnotab++ = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003833 *lnotab++ = 255;
Guido van Rossumf10570b1995-07-07 22:53:21 +00003834 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835 d_lineno -= ncodes * 255;
3836 a->a_lnotab_off += ncodes * 2;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003837 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003838
Christian Heimes72b710a2008-05-26 13:28:38 +00003839 len = PyBytes_GET_SIZE(a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840 if (a->a_lnotab_off + 2 >= len) {
Christian Heimes72b710a2008-05-26 13:28:38 +00003841 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
Tim Peters51e26512001-09-07 08:45:55 +00003842 return 0;
Tim Peters51e26512001-09-07 08:45:55 +00003843 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003844 lnotab = (unsigned char *)
Christian Heimes72b710a2008-05-26 13:28:38 +00003845 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003847 a->a_lnotab_off += 2;
3848 if (d_bytecode) {
3849 *lnotab++ = d_bytecode;
3850 *lnotab++ = d_lineno;
Jeremy Hyltond5e5a2a2001-08-12 01:54:38 +00003851 }
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003852 else { /* First line of a block; def stmt, etc. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853 *lnotab++ = 0;
3854 *lnotab++ = d_lineno;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003855 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003856 a->a_lineno = i->i_lineno;
3857 a->a_lineno_off = a->a_offset;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003858 return 1;
3859}
3860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861/* assemble_emit()
3862 Extend the bytecode with a new instruction.
3863 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003864*/
3865
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003866static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003867assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003868{
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003869 int size, arg = 0, ext = 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003870 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003871 char *code;
3872
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003873 size = instrsize(i);
3874 if (i->i_hasarg) {
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003875 arg = i->i_oparg;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003876 ext = arg >> 16;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003877 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003878 if (i->i_lineno && !assemble_lnotab(a, i))
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003879 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003880 if (a->a_offset + size >= len) {
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +00003881 if (len > PY_SSIZE_T_MAX / 2)
3882 return 0;
Christian Heimes72b710a2008-05-26 13:28:38 +00003883 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003884 return 0;
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003885 }
Christian Heimes72b710a2008-05-26 13:28:38 +00003886 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003887 a->a_offset += size;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003888 if (size == 6) {
3889 assert(i->i_hasarg);
3890 *code++ = (char)EXTENDED_ARG;
3891 *code++ = ext & 0xff;
3892 *code++ = ext >> 8;
3893 arg &= 0xffff;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003894 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003895 *code++ = i->i_opcode;
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003896 if (i->i_hasarg) {
3897 assert(size == 3 || size == 6);
3898 *code++ = arg & 0xff;
3899 *code++ = arg >> 8;
3900 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003902}
3903
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003904static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003905assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003906{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003907 basicblock *b;
Neal Norwitzf1d50682005-10-23 23:00:41 +00003908 int bsize, totsize, extended_arg_count, last_extended_arg_count = 0;
Guido van Rossumf1aeab71992-03-27 17:28:26 +00003909 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003910
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003911 /* Compute the size of each block and fixup jump args.
3912 Replace block pointer with position in bytecode. */
Neal Norwitzf1d50682005-10-23 23:00:41 +00003913start:
3914 totsize = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003915 for (i = a->a_nblocks - 1; i >= 0; i--) {
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003916 b = a->a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003917 bsize = blocksize(b);
3918 b->b_offset = totsize;
3919 totsize += bsize;
Guido van Rossum25831651993-05-19 14:50:45 +00003920 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003921 extended_arg_count = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003922 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3923 bsize = b->b_offset;
3924 for (i = 0; i < b->b_iused; i++) {
3925 struct instr *instr = &b->b_instr[i];
3926 /* Relative jumps are computed relative to
3927 the instruction pointer after fetching
3928 the jump instruction.
3929 */
3930 bsize += instrsize(instr);
3931 if (instr->i_jabs)
3932 instr->i_oparg = instr->i_target->b_offset;
3933 else if (instr->i_jrel) {
3934 int delta = instr->i_target->b_offset - bsize;
3935 instr->i_oparg = delta;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003936 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003937 else
3938 continue;
3939 if (instr->i_oparg > 0xffff)
3940 extended_arg_count++;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003941 }
3942 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003943
3944 /* XXX: This is an awful hack that could hurt performance, but
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003945 on the bright side it should work until we come up
Neal Norwitzf1d50682005-10-23 23:00:41 +00003946 with a better solution.
3947
3948 In the meantime, should the goto be dropped in favor
3949 of a loop?
3950
3951 The issue is that in the first loop blocksize() is called
3952 which calls instrsize() which requires i_oparg be set
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003953 appropriately. There is a bootstrap problem because
Neal Norwitzf1d50682005-10-23 23:00:41 +00003954 i_oparg is calculated in the second loop above.
3955
3956 So we loop until we stop seeing new EXTENDED_ARGs.
3957 The only EXTENDED_ARGs that could be popping up are
3958 ones in jump instructions. So this should converge
3959 fairly quickly.
3960 */
3961 if (last_extended_arg_count != extended_arg_count) {
3962 last_extended_arg_count = extended_arg_count;
3963 goto start;
3964 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003965}
3966
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003967static PyObject *
3968dict_keys_inorder(PyObject *dict, int offset)
3969{
3970 PyObject *tuple, *k, *v;
Martin v. Löwis18e16552006-02-15 17:27:45 +00003971 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003972
3973 tuple = PyTuple_New(size);
3974 if (tuple == NULL)
3975 return NULL;
3976 while (PyDict_Next(dict, &pos, &k, &v)) {
Christian Heimes217cfd12007-12-02 14:31:20 +00003977 i = PyLong_AS_LONG(v);
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00003978 /* The keys of the dictionary are tuples. (see compiler_add_o)
3979 The object we want is always first, though. */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003980 k = PyTuple_GET_ITEM(k, 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003981 Py_INCREF(k);
Jeremy Hyltonce7ef592001-03-20 00:25:43 +00003982 assert((i - offset) < size);
Jeremy Hyltone9357b22006-03-01 15:47:05 +00003983 assert((i - offset) >= 0);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003984 PyTuple_SET_ITEM(tuple, i - offset, k);
3985 }
3986 return tuple;
3987}
3988
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003989static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003990compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003991{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003992 PySTEntryObject *ste = c->u->u_ste;
3993 int flags = 0, n;
3994 if (ste->ste_type != ModuleBlock)
3995 flags |= CO_NEWLOCALS;
3996 if (ste->ste_type == FunctionBlock) {
3997 if (!ste->ste_unoptimized)
3998 flags |= CO_OPTIMIZED;
3999 if (ste->ste_nested)
4000 flags |= CO_NESTED;
4001 if (ste->ste_generator)
4002 flags |= CO_GENERATOR;
Benjamin Peterson5c6d7872009-02-06 02:40:07 +00004003 if (ste->ste_varargs)
4004 flags |= CO_VARARGS;
4005 if (ste->ste_varkeywords)
4006 flags |= CO_VARKEYWORDS;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004007 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004008
4009 /* (Only) inherit compilerflags in PyCF_MASK */
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004010 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004011
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004012 n = PyDict_Size(c->u->u_freevars);
4013 if (n < 0)
4014 return -1;
4015 if (n == 0) {
4016 n = PyDict_Size(c->u->u_cellvars);
4017 if (n < 0)
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004018 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004019 if (n == 0) {
4020 flags |= CO_NOFREE;
4021 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004022 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004023
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004024 return flags;
4025}
4026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004027static PyCodeObject *
4028makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004029{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004030 PyObject *tmp;
4031 PyCodeObject *co = NULL;
4032 PyObject *consts = NULL;
4033 PyObject *names = NULL;
4034 PyObject *varnames = NULL;
4035 PyObject *filename = NULL;
4036 PyObject *name = NULL;
4037 PyObject *freevars = NULL;
4038 PyObject *cellvars = NULL;
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004039 PyObject *bytecode = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004041
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042 tmp = dict_keys_inorder(c->u->u_consts, 0);
4043 if (!tmp)
4044 goto error;
4045 consts = PySequence_List(tmp); /* optimize_code requires a list */
4046 Py_DECREF(tmp);
4047
4048 names = dict_keys_inorder(c->u->u_names, 0);
4049 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4050 if (!consts || !names || !varnames)
4051 goto error;
4052
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004053 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4054 if (!cellvars)
4055 goto error;
4056 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4057 if (!freevars)
4058 goto error;
Guido van Rossum00bc0e02007-10-15 02:52:41 +00004059 filename = PyUnicode_DecodeFSDefault(c->c_filename);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004060 if (!filename)
4061 goto error;
4062
Jeremy Hyltone9357b22006-03-01 15:47:05 +00004063 nlocals = PyDict_Size(c->u->u_varnames);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004064 flags = compute_code_flags(c);
4065 if (flags < 0)
4066 goto error;
4067
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004068 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069 if (!bytecode)
4070 goto error;
4071
4072 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4073 if (!tmp)
4074 goto error;
4075 Py_DECREF(consts);
4076 consts = tmp;
4077
Guido van Rossum4f72a782006-10-27 23:31:49 +00004078 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4079 nlocals, stackdepth(c), flags,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004080 bytecode, consts, names, varnames,
4081 freevars, cellvars,
4082 filename, c->u->u_name,
4083 c->u->u_firstlineno,
4084 a->a_lnotab);
4085 error:
4086 Py_XDECREF(consts);
4087 Py_XDECREF(names);
4088 Py_XDECREF(varnames);
4089 Py_XDECREF(filename);
4090 Py_XDECREF(name);
4091 Py_XDECREF(freevars);
4092 Py_XDECREF(cellvars);
4093 Py_XDECREF(bytecode);
4094 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004095}
4096
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004097
4098/* For debugging purposes only */
4099#if 0
4100static void
4101dump_instr(const struct instr *i)
4102{
4103 const char *jrel = i->i_jrel ? "jrel " : "";
4104 const char *jabs = i->i_jabs ? "jabs " : "";
4105 char arg[128];
4106
4107 *arg = '\0';
4108 if (i->i_hasarg)
4109 sprintf(arg, "arg: %d ", i->i_oparg);
4110
4111 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4112 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4113}
4114
4115static void
4116dump_basicblock(const basicblock *b)
4117{
4118 const char *seen = b->b_seen ? "seen " : "";
4119 const char *b_return = b->b_return ? "return " : "";
4120 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4121 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4122 if (b->b_instr) {
4123 int i;
4124 for (i = 0; i < b->b_iused; i++) {
4125 fprintf(stderr, " [%02d] ", i);
4126 dump_instr(b->b_instr + i);
4127 }
4128 }
4129}
4130#endif
4131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132static PyCodeObject *
4133assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004134{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004135 basicblock *b, *entryblock;
4136 struct assembler a;
4137 int i, j, nblocks;
4138 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004139
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004140 /* Make sure every block that falls off the end returns None.
4141 XXX NEXT_BLOCK() isn't quite right, because if the last
4142 block ends with a jump or return b_next shouldn't set.
4143 */
4144 if (!c->u->u_curblock->b_return) {
4145 NEXT_BLOCK(c);
4146 if (addNone)
4147 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4148 ADDOP(c, RETURN_VALUE);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004149 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151 nblocks = 0;
4152 entryblock = NULL;
4153 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4154 nblocks++;
4155 entryblock = b;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004156 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004157
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004158 /* Set firstlineno if it wasn't explicitly set. */
4159 if (!c->u->u_firstlineno) {
4160 if (entryblock && entryblock->b_instr)
4161 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4162 else
4163 c->u->u_firstlineno = 1;
4164 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004165 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4166 goto error;
4167 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004168
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004169 /* Can't modify the bytecode after computing jump offsets. */
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004170 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004171
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004172 /* Emit code in reverse postorder from dfs. */
4173 for (i = a.a_nblocks - 1; i >= 0; i--) {
Neal Norwitz08b401f2006-01-07 21:24:09 +00004174 b = a.a_postorder[i];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004175 for (j = 0; j < b->b_iused; j++)
4176 if (!assemble_emit(&a, &b->b_instr[j]))
4177 goto error;
Tim Petersb6c3cea2001-06-26 03:36:28 +00004178 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004179
Christian Heimes72b710a2008-05-26 13:28:38 +00004180 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004181 goto error;
Christian Heimes72b710a2008-05-26 13:28:38 +00004182 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004184
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004185 co = makecode(c, &a);
4186 error:
4187 assemble_free(&a);
4188 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004189}