blob: 34e7603b3a560f8ecedfa7b92d18afca5b22ba11 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +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"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000028#include "ast.h"
29#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000030#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "opcode.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000032
Guido van Rossum8e793d91997-03-03 19:13:14 +000033int Py_OptimizeFlag = 0;
34
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000035#define DEFAULT_BLOCK_SIZE 16
36#define DEFAULT_BLOCKS 8
37#define DEFAULT_CODE_SIZE 128
38#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000039
Nick Coghlan650f0d02007-04-15 12:05:43 +000040#define COMP_GENEXP 0
41#define COMP_LISTCOMP 1
42#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000043#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000044
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000045struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 unsigned i_jabs : 1;
47 unsigned i_jrel : 1;
48 unsigned i_hasarg : 1;
49 unsigned char i_opcode;
50 int i_oparg;
51 struct basicblock_ *i_target; /* target block (if jump instruction) */
52 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000053};
54
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000055typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000056 /* Each basicblock in a compilation unit is linked via b_list in the
57 reverse order that the block are allocated. b_list points to the next
58 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 struct basicblock_ *b_list;
60 /* number of instructions used */
61 int b_iused;
62 /* length of instruction array (b_instr) */
63 int b_ialloc;
64 /* pointer to an array of instructions, initially NULL */
65 struct instr *b_instr;
66 /* If b_next is non-NULL, it is a pointer to the next
67 block reached by normal control flow. */
68 struct basicblock_ *b_next;
69 /* b_seen is used to perform a DFS of basicblocks. */
70 unsigned b_seen : 1;
71 /* b_return is true if a RETURN_VALUE opcode is inserted. */
72 unsigned b_return : 1;
73 /* depth of stack upon entry of block, computed by stackdepth() */
74 int b_startdepth;
75 /* instruction offset for block, computed by assemble_jump_offsets() */
76 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000077} basicblock;
78
79/* fblockinfo tracks the current frame block.
80
Jeremy Hyltone9357b22006-03-01 15:47:05 +000081A frame block is used to handle loops, try/except, and try/finally.
82It's called a frame block to distinguish it from a basic block in the
83compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000084*/
85
86enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
87
88struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 enum fblocktype fb_type;
90 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000091};
92
93/* The following items change on entry and exit of code blocks.
94 They must be saved and restored when returning to a block.
95*/
96struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 PyObject *u_name;
100 /* The following fields are dicts that map objects to
101 the index of them in co_XXX. The index is used as
102 the argument for opcodes that refer to those collections.
103 */
104 PyObject *u_consts; /* all constants */
105 PyObject *u_names; /* all names */
106 PyObject *u_varnames; /* local variables */
107 PyObject *u_cellvars; /* cell variables */
108 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 int u_argcount; /* number of arguments for block */
113 int u_kwonlyargcount; /* number of keyword only arguments for block */
114 /* Pointer to the most recently allocated block. By following b_list
115 members, you can reach all early allocated blocks. */
116 basicblock *u_blocks;
117 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000119 int u_nfblocks;
120 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 int u_firstlineno; /* the first lineno of the block */
123 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000124 int u_col_offset; /* the offset of the current stmt */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 int u_lineno_set; /* boolean to indicate whether instr
126 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127};
128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000130
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000131The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000133managed by compiler_enter_scope() and compiler_exit_scope().
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134*/
135
136struct compiler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 const char *c_filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500138 PyObject *c_filename_obj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 struct symtable *c_st;
140 PyFutureFeatures *c_future; /* pointer to module's __future__ */
141 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000142
Georg Brandl8334fd92010-12-04 10:26:46 +0000143 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 int c_interactive; /* true if in interactive mode */
145 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 struct compiler_unit *u; /* compiler state for current block */
148 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
149 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150};
151
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000152static int compiler_enter_scope(struct compiler *, identifier, void *, int);
153static void compiler_free(struct compiler *);
154static basicblock *compiler_new_block(struct compiler *);
155static int compiler_next_instr(struct compiler *, basicblock *);
156static int compiler_addop(struct compiler *, int);
157static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
158static int compiler_addop_i(struct compiler *, int, int);
159static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160static basicblock *compiler_use_new_block(struct compiler *);
161static int compiler_error(struct compiler *, const char *);
162static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
163
164static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
165static int compiler_visit_stmt(struct compiler *, stmt_ty);
166static int compiler_visit_keyword(struct compiler *, keyword_ty);
167static int compiler_visit_expr(struct compiler *, expr_ty);
168static int compiler_augassign(struct compiler *, stmt_ty);
169static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
172static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000176/* Returns true if there is a loop on the fblock stack. */
177static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178
179static int inplace_binop(struct compiler *, operator_ty);
Georg Brandl8334fd92010-12-04 10:26:46 +0000180static int expr_constant(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181
Guido van Rossumc2e20742006-02-27 22:32:47 +0000182static int compiler_with(struct compiler *, stmt_ty);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000183static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 asdl_seq *args,
185 asdl_seq *keywords,
186 expr_ty starargs,
187 expr_ty kwargs);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static PyCodeObject *assemble(struct compiler *, int addNone);
190static PyObject *__doc__;
191
Benjamin Petersonb173f782009-05-05 22:31:58 +0000192#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
193
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000197 /* Name mangling: __private becomes _classname__private.
198 This is independent from how the name is used. */
199 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
200 Py_UNICODE *buffer;
201 size_t nlen, plen;
202 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
203 name == NULL || name[0] != '_' || name[1] != '_') {
204 Py_INCREF(ident);
205 return ident;
206 }
207 p = PyUnicode_AS_UNICODE(privateobj);
208 nlen = Py_UNICODE_strlen(name);
209 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 The only time a name with a dot can occur is when
212 we are compiling an import statement that has a
213 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 TODO(jhylton): Decide whether we want to support
216 mangling of the module name, e.g. __M.X.
217 */
218 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
219 || Py_UNICODE_strchr(name, '.')) {
220 Py_INCREF(ident);
221 return ident; /* Don't mangle __whatever__ */
222 }
223 /* Strip leading underscores from class name */
224 while (*p == '_')
225 p++;
226 if (*p == 0) {
227 Py_INCREF(ident);
228 return ident; /* Don't mangle if class is just underscores */
229 }
230 plen = Py_UNICODE_strlen(p);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 assert(1 <= PY_SSIZE_T_MAX - nlen);
233 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
236 if (!ident)
237 return 0;
238 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
239 buffer = PyUnicode_AS_UNICODE(ident);
240 buffer[0] = '_';
241 Py_UNICODE_strncpy(buffer+1, p, plen);
242 Py_UNICODE_strcpy(buffer+1+plen, name);
243 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000244}
245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246static int
247compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 c->c_stack = PyList_New(0);
252 if (!c->c_stack)
253 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256}
257
258PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +0000259PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags,
260 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 struct compiler c;
263 PyCodeObject *co = NULL;
264 PyCompilerFlags local_flags;
265 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 if (!__doc__) {
268 __doc__ = PyUnicode_InternFromString("__doc__");
269 if (!__doc__)
270 return NULL;
271 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (!compiler_init(&c))
274 return NULL;
275 c.c_filename = filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500276 c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
277 if (!c.c_filename_obj)
278 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 c.c_arena = arena;
280 c.c_future = PyFuture_FromAST(mod, filename);
281 if (c.c_future == NULL)
282 goto finally;
283 if (!flags) {
284 local_flags.cf_flags = 0;
285 flags = &local_flags;
286 }
287 merged = c.c_future->ff_features | flags->cf_flags;
288 c.c_future->ff_features = merged;
289 flags->cf_flags = merged;
290 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000291 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 c.c_st = PySymtable_Build(mod, filename, c.c_future);
295 if (c.c_st == NULL) {
296 if (!PyErr_Occurred())
297 PyErr_SetString(PyExc_SystemError, "no symtable");
298 goto finally;
299 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000302
Thomas Wouters1175c432006-02-27 22:49:54 +0000303 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 compiler_free(&c);
305 assert(co || PyErr_Occurred());
306 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000307}
308
309PyCodeObject *
310PyNode_Compile(struct _node *n, const char *filename)
311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 PyCodeObject *co = NULL;
313 mod_ty mod;
314 PyArena *arena = PyArena_New();
315 if (!arena)
316 return NULL;
317 mod = PyAST_FromNode(n, NULL, filename, arena);
318 if (mod)
319 co = PyAST_Compile(mod, filename, NULL, arena);
320 PyArena_Free(arena);
321 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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 if (c->c_st)
328 PySymtable_Free(c->c_st);
329 if (c->c_future)
330 PyObject_Free(c->c_future);
Benjamin Peterson43b06862011-05-27 09:08:01 -0500331 if (c->c_filename_obj)
332 Py_DECREF(c->c_filename_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000334}
335
Guido van Rossum79f25d91997-04-29 20:08:16 +0000336static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 Py_ssize_t i, n;
340 PyObject *v, *k;
341 PyObject *dict = PyDict_New();
342 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 n = PyList_Size(list);
345 for (i = 0; i < n; i++) {
346 v = PyLong_FromLong(i);
347 if (!v) {
348 Py_DECREF(dict);
349 return NULL;
350 }
351 k = PyList_GET_ITEM(list, i);
352 k = PyTuple_Pack(2, k, k->ob_type);
353 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
354 Py_XDECREF(k);
355 Py_DECREF(v);
356 Py_DECREF(dict);
357 return NULL;
358 }
359 Py_DECREF(k);
360 Py_DECREF(v);
361 }
362 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
365/* Return new dict containing names from src that match scope(s).
366
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000367src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000369values are integers, starting at offset and increasing by one for
370each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371*/
372
373static PyObject *
374dictbytype(PyObject *src, int scope_type, int flag, int offset)
375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_ssize_t pos = 0, i = offset, scope;
377 PyObject *k, *v, *dest = PyDict_New();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 assert(offset >= 0);
380 if (dest == NULL)
381 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 while (PyDict_Next(src, &pos, &k, &v)) {
384 /* XXX this should probably be a macro in symtable.h */
385 long vi;
386 assert(PyLong_Check(v));
387 vi = PyLong_AS_LONG(v);
388 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (scope == scope_type || vi & flag) {
391 PyObject *tuple, *item = PyLong_FromLong(i);
392 if (item == NULL) {
393 Py_DECREF(dest);
394 return NULL;
395 }
396 i++;
397 tuple = PyTuple_Pack(2, k, k->ob_type);
398 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
399 Py_DECREF(item);
400 Py_DECREF(dest);
401 Py_XDECREF(tuple);
402 return NULL;
403 }
404 Py_DECREF(item);
405 Py_DECREF(tuple);
406 }
407 }
408 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000409}
410
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000411static void
412compiler_unit_check(struct compiler_unit *u)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 basicblock *block;
415 for (block = u->u_blocks; block != NULL; block = block->b_list) {
416 assert((void *)block != (void *)0xcbcbcbcb);
417 assert((void *)block != (void *)0xfbfbfbfb);
418 assert((void *)block != (void *)0xdbdbdbdb);
419 if (block->b_instr != NULL) {
420 assert(block->b_ialloc > 0);
421 assert(block->b_iused > 0);
422 assert(block->b_ialloc >= block->b_iused);
423 }
424 else {
425 assert (block->b_iused == 0);
426 assert (block->b_ialloc == 0);
427 }
428 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429}
430
431static void
432compiler_unit_free(struct compiler_unit *u)
433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 compiler_unit_check(u);
437 b = u->u_blocks;
438 while (b != NULL) {
439 if (b->b_instr)
440 PyObject_Free((void *)b->b_instr);
441 next = b->b_list;
442 PyObject_Free((void *)b);
443 b = next;
444 }
445 Py_CLEAR(u->u_ste);
446 Py_CLEAR(u->u_name);
447 Py_CLEAR(u->u_consts);
448 Py_CLEAR(u->u_names);
449 Py_CLEAR(u->u_varnames);
450 Py_CLEAR(u->u_freevars);
451 Py_CLEAR(u->u_cellvars);
452 Py_CLEAR(u->u_private);
453 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454}
455
456static int
457compiler_enter_scope(struct compiler *c, identifier name, void *key,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
463 struct compiler_unit));
464 if (!u) {
465 PyErr_NoMemory();
466 return 0;
467 }
468 memset(u, 0, sizeof(struct compiler_unit));
469 u->u_argcount = 0;
470 u->u_kwonlyargcount = 0;
471 u->u_ste = PySymtable_Lookup(c->c_st, key);
472 if (!u->u_ste) {
473 compiler_unit_free(u);
474 return 0;
475 }
476 Py_INCREF(name);
477 u->u_name = name;
478 u->u_varnames = list2dict(u->u_ste->ste_varnames);
479 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
480 if (!u->u_varnames || !u->u_cellvars) {
481 compiler_unit_free(u);
482 return 0;
483 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
486 PyDict_Size(u->u_cellvars));
487 if (!u->u_freevars) {
488 compiler_unit_free(u);
489 return 0;
490 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 u->u_blocks = NULL;
493 u->u_nfblocks = 0;
494 u->u_firstlineno = lineno;
495 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000496 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 u->u_lineno_set = 0;
498 u->u_consts = PyDict_New();
499 if (!u->u_consts) {
500 compiler_unit_free(u);
501 return 0;
502 }
503 u->u_names = PyDict_New();
504 if (!u->u_names) {
505 compiler_unit_free(u);
506 return 0;
507 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* Push the old compiler_unit on the stack. */
512 if (c->u) {
513 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
514 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
515 Py_XDECREF(capsule);
516 compiler_unit_free(u);
517 return 0;
518 }
519 Py_DECREF(capsule);
520 u->u_private = c->u->u_private;
521 Py_XINCREF(u->u_private);
522 }
523 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 c->c_nestlevel++;
526 if (compiler_use_new_block(c) == NULL)
527 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000532static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533compiler_exit_scope(struct compiler *c)
534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 int n;
536 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 c->c_nestlevel--;
539 compiler_unit_free(c->u);
540 /* Restore c->u to the parent unit. */
541 n = PyList_GET_SIZE(c->c_stack) - 1;
542 if (n >= 0) {
543 capsule = PyList_GET_ITEM(c->c_stack, n);
544 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
545 assert(c->u);
546 /* we are deleting from a list so this really shouldn't fail */
547 if (PySequence_DelItem(c->c_stack, n) < 0)
548 Py_FatalError("compiler_exit_scope()");
549 compiler_unit_check(c->u);
550 }
551 else
552 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554}
555
556/* Allocate a new block and return a pointer to it.
557 Returns NULL on error.
558*/
559
560static basicblock *
561compiler_new_block(struct compiler *c)
562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 basicblock *b;
564 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 u = c->u;
567 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
568 if (b == NULL) {
569 PyErr_NoMemory();
570 return NULL;
571 }
572 memset((void *)b, 0, sizeof(basicblock));
573 /* Extend the singly linked list of blocks with new block. */
574 b->b_list = u->u_blocks;
575 u->u_blocks = b;
576 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577}
578
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579static basicblock *
580compiler_use_new_block(struct compiler *c)
581{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 basicblock *block = compiler_new_block(c);
583 if (block == NULL)
584 return NULL;
585 c->u->u_curblock = block;
586 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000587}
588
589static basicblock *
590compiler_next_block(struct compiler *c)
591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 basicblock *block = compiler_new_block(c);
593 if (block == NULL)
594 return NULL;
595 c->u->u_curblock->b_next = block;
596 c->u->u_curblock = block;
597 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598}
599
600static basicblock *
601compiler_use_next_block(struct compiler *c, basicblock *block)
602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 assert(block != NULL);
604 c->u->u_curblock->b_next = block;
605 c->u->u_curblock = block;
606 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607}
608
609/* Returns the offset of the next instruction in the current block's
610 b_instr array. Resizes the b_instr as necessary.
611 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000612*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613
614static int
615compiler_next_instr(struct compiler *c, basicblock *b)
616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 assert(b != NULL);
618 if (b->b_instr == NULL) {
619 b->b_instr = (struct instr *)PyObject_Malloc(
620 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
621 if (b->b_instr == NULL) {
622 PyErr_NoMemory();
623 return -1;
624 }
625 b->b_ialloc = DEFAULT_BLOCK_SIZE;
626 memset((char *)b->b_instr, 0,
627 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
628 }
629 else if (b->b_iused == b->b_ialloc) {
630 struct instr *tmp;
631 size_t oldsize, newsize;
632 oldsize = b->b_ialloc * sizeof(struct instr);
633 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (oldsize > (PY_SIZE_MAX >> 1)) {
636 PyErr_NoMemory();
637 return -1;
638 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 if (newsize == 0) {
641 PyErr_NoMemory();
642 return -1;
643 }
644 b->b_ialloc <<= 1;
645 tmp = (struct instr *)PyObject_Realloc(
646 (void *)b->b_instr, newsize);
647 if (tmp == NULL) {
648 PyErr_NoMemory();
649 return -1;
650 }
651 b->b_instr = tmp;
652 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
653 }
654 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655}
656
Christian Heimes2202f872008-02-06 14:31:34 +0000657/* Set the i_lineno member of the instruction at offset off if the
658 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659 already been set. If it has been set, the call has no effect.
660
Christian Heimes2202f872008-02-06 14:31:34 +0000661 The line number is reset in the following cases:
662 - when entering a new scope
663 - on each statement
664 - on each expression that start a new line
665 - before the "except" clause
666 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000667*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669static void
670compiler_set_lineno(struct compiler *c, int off)
671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 basicblock *b;
673 if (c->u->u_lineno_set)
674 return;
675 c->u->u_lineno_set = 1;
676 b = c->u->u_curblock;
677 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000678}
679
680static int
681opcode_stack_effect(int opcode, int oparg)
682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 switch (opcode) {
684 case POP_TOP:
685 return -1;
686 case ROT_TWO:
687 case ROT_THREE:
688 return 0;
689 case DUP_TOP:
690 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000691 case DUP_TOP_TWO:
692 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 case UNARY_POSITIVE:
695 case UNARY_NEGATIVE:
696 case UNARY_NOT:
697 case UNARY_INVERT:
698 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 case SET_ADD:
701 case LIST_APPEND:
702 return -1;
703 case MAP_ADD:
704 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 case BINARY_POWER:
707 case BINARY_MULTIPLY:
708 case BINARY_MODULO:
709 case BINARY_ADD:
710 case BINARY_SUBTRACT:
711 case BINARY_SUBSCR:
712 case BINARY_FLOOR_DIVIDE:
713 case BINARY_TRUE_DIVIDE:
714 return -1;
715 case INPLACE_FLOOR_DIVIDE:
716 case INPLACE_TRUE_DIVIDE:
717 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 case INPLACE_ADD:
720 case INPLACE_SUBTRACT:
721 case INPLACE_MULTIPLY:
722 case INPLACE_MODULO:
723 return -1;
724 case STORE_SUBSCR:
725 return -3;
726 case STORE_MAP:
727 return -2;
728 case DELETE_SUBSCR:
729 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 case BINARY_LSHIFT:
732 case BINARY_RSHIFT:
733 case BINARY_AND:
734 case BINARY_XOR:
735 case BINARY_OR:
736 return -1;
737 case INPLACE_POWER:
738 return -1;
739 case GET_ITER:
740 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 case PRINT_EXPR:
743 return -1;
744 case LOAD_BUILD_CLASS:
745 return 1;
746 case INPLACE_LSHIFT:
747 case INPLACE_RSHIFT:
748 case INPLACE_AND:
749 case INPLACE_XOR:
750 case INPLACE_OR:
751 return -1;
752 case BREAK_LOOP:
753 return 0;
754 case SETUP_WITH:
755 return 7;
756 case WITH_CLEANUP:
757 return -1; /* XXX Sometimes more */
758 case STORE_LOCALS:
759 return -1;
760 case RETURN_VALUE:
761 return -1;
762 case IMPORT_STAR:
763 return -1;
764 case YIELD_VALUE:
765 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 case POP_BLOCK:
768 return 0;
769 case POP_EXCEPT:
770 return 0; /* -3 except if bad bytecode */
771 case END_FINALLY:
772 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 case STORE_NAME:
775 return -1;
776 case DELETE_NAME:
777 return 0;
778 case UNPACK_SEQUENCE:
779 return oparg-1;
780 case UNPACK_EX:
781 return (oparg&0xFF) + (oparg>>8);
782 case FOR_ITER:
783 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 case STORE_ATTR:
786 return -2;
787 case DELETE_ATTR:
788 return -1;
789 case STORE_GLOBAL:
790 return -1;
791 case DELETE_GLOBAL:
792 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 case LOAD_CONST:
794 return 1;
795 case LOAD_NAME:
796 return 1;
797 case BUILD_TUPLE:
798 case BUILD_LIST:
799 case BUILD_SET:
800 return 1-oparg;
801 case BUILD_MAP:
802 return 1;
803 case LOAD_ATTR:
804 return 0;
805 case COMPARE_OP:
806 return -1;
807 case IMPORT_NAME:
808 return -1;
809 case IMPORT_FROM:
810 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 case JUMP_FORWARD:
813 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
814 case JUMP_IF_FALSE_OR_POP: /* "" */
815 case JUMP_ABSOLUTE:
816 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 case POP_JUMP_IF_FALSE:
819 case POP_JUMP_IF_TRUE:
820 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 case LOAD_GLOBAL:
823 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 case CONTINUE_LOOP:
826 return 0;
827 case SETUP_LOOP:
828 return 0;
829 case SETUP_EXCEPT:
830 case SETUP_FINALLY:
831 return 6; /* can push 3 values for the new exception
832 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 case LOAD_FAST:
835 return 1;
836 case STORE_FAST:
837 return -1;
838 case DELETE_FAST:
839 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 case RAISE_VARARGS:
842 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000843#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 case CALL_FUNCTION:
845 return -NARGS(oparg);
846 case CALL_FUNCTION_VAR:
847 case CALL_FUNCTION_KW:
848 return -NARGS(oparg)-1;
849 case CALL_FUNCTION_VAR_KW:
850 return -NARGS(oparg)-2;
851 case MAKE_FUNCTION:
852 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
853 case MAKE_CLOSURE:
854 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000855#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 case BUILD_SLICE:
857 if (oparg == 3)
858 return -2;
859 else
860 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 case LOAD_CLOSURE:
863 return 1;
864 case LOAD_DEREF:
865 return 1;
866 case STORE_DEREF:
867 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000868 case DELETE_DEREF:
869 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 default:
871 fprintf(stderr, "opcode = %d\n", opcode);
872 Py_FatalError("opcode_stack_effect()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 }
875 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000876}
877
878/* Add an opcode with no argument.
879 Returns 0 on failure, 1 on success.
880*/
881
882static int
883compiler_addop(struct compiler *c, int opcode)
884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 basicblock *b;
886 struct instr *i;
887 int off;
888 off = compiler_next_instr(c, c->u->u_curblock);
889 if (off < 0)
890 return 0;
891 b = c->u->u_curblock;
892 i = &b->b_instr[off];
893 i->i_opcode = opcode;
894 i->i_hasarg = 0;
895 if (opcode == RETURN_VALUE)
896 b->b_return = 1;
897 compiler_set_lineno(c, off);
898 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899}
900
901static int
902compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 PyObject *t, *v;
905 Py_ssize_t arg;
906 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 /* necessary to make sure types aren't coerced (e.g., int and long) */
909 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
910 if (PyFloat_Check(o)) {
911 d = PyFloat_AS_DOUBLE(o);
912 /* all we need is to make the tuple different in either the 0.0
913 * or -0.0 case from all others, just to avoid the "coercion".
914 */
915 if (d == 0.0 && copysign(1.0, d) < 0.0)
916 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
917 else
918 t = PyTuple_Pack(2, o, o->ob_type);
919 }
920 else if (PyComplex_Check(o)) {
921 Py_complex z;
922 int real_negzero, imag_negzero;
923 /* For the complex case we must make complex(x, 0.)
924 different from complex(x, -0.) and complex(0., y)
925 different from complex(-0., y), for any x and y.
926 All four complex zeros must be distinguished.*/
927 z = PyComplex_AsCComplex(o);
928 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
929 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
930 if (real_negzero && imag_negzero) {
931 t = PyTuple_Pack(5, o, o->ob_type,
932 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +0000933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 else if (imag_negzero) {
935 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000936 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 else if (real_negzero) {
938 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
939 }
940 else {
941 t = PyTuple_Pack(2, o, o->ob_type);
942 }
943 }
944 else {
945 t = PyTuple_Pack(2, o, o->ob_type);
946 }
947 if (t == NULL)
948 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 v = PyDict_GetItem(dict, t);
951 if (!v) {
952 if (PyErr_Occurred())
953 return -1;
954 arg = PyDict_Size(dict);
955 v = PyLong_FromLong(arg);
956 if (!v) {
957 Py_DECREF(t);
958 return -1;
959 }
960 if (PyDict_SetItem(dict, t, v) < 0) {
961 Py_DECREF(t);
962 Py_DECREF(v);
963 return -1;
964 }
965 Py_DECREF(v);
966 }
967 else
968 arg = PyLong_AsLong(v);
969 Py_DECREF(t);
970 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971}
972
973static int
974compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976{
977 int arg = compiler_add_o(c, dict, o);
978 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000979 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980 return compiler_addop_i(c, opcode, arg);
981}
982
983static int
984compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000986{
987 int arg;
988 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
989 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000990 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991 arg = compiler_add_o(c, dict, mangled);
992 Py_DECREF(mangled);
993 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000994 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000995 return compiler_addop_i(c, opcode, arg);
996}
997
998/* Add an opcode with an integer argument.
999 Returns 0 on failure, 1 on success.
1000*/
1001
1002static int
1003compiler_addop_i(struct compiler *c, int opcode, int oparg)
1004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 struct instr *i;
1006 int off;
1007 off = compiler_next_instr(c, c->u->u_curblock);
1008 if (off < 0)
1009 return 0;
1010 i = &c->u->u_curblock->b_instr[off];
1011 i->i_opcode = opcode;
1012 i->i_oparg = oparg;
1013 i->i_hasarg = 1;
1014 compiler_set_lineno(c, off);
1015 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016}
1017
1018static int
1019compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 struct instr *i;
1022 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 assert(b != NULL);
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_target = b;
1031 i->i_hasarg = 1;
1032 if (absolute)
1033 i->i_jabs = 1;
1034 else
1035 i->i_jrel = 1;
1036 compiler_set_lineno(c, off);
1037 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001038}
1039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1041 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042 it as the current block. NEXT_BLOCK() also creates an implicit jump
1043 from the current block to the new block.
1044*/
1045
Thomas Wouters89f507f2006-12-13 04:49:30 +00001046/* The returns inside these macros make it impossible to decref objects
1047 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048*/
1049
1050
1051#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (compiler_use_new_block((C)) == NULL) \
1053 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054}
1055
1056#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 if (compiler_next_block((C)) == NULL) \
1058 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001059}
1060
1061#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 if (!compiler_addop((C), (OP))) \
1063 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001064}
1065
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001066#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (!compiler_addop((C), (OP))) { \
1068 compiler_exit_scope(c); \
1069 return 0; \
1070 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001071}
1072
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1075 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076}
1077
1078#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1080 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081}
1082
1083#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 if (!compiler_addop_i((C), (OP), (O))) \
1085 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086}
1087
1088#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 if (!compiler_addop_j((C), (OP), (O), 1)) \
1090 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091}
1092
1093#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 if (!compiler_addop_j((C), (OP), (O), 0)) \
1095 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096}
1097
1098/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1099 the ASDL name to synthesize the name of the C type and the visit function.
1100*/
1101
1102#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 if (!compiler_visit_ ## TYPE((C), (V))) \
1104 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105}
1106
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001107#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 if (!compiler_visit_ ## TYPE((C), (V))) { \
1109 compiler_exit_scope(c); \
1110 return 0; \
1111 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001112}
1113
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (!compiler_visit_slice((C), (V), (CTX))) \
1116 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001117}
1118
1119#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 int _i; \
1121 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1122 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1123 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1124 if (!compiler_visit_ ## TYPE((C), elt)) \
1125 return 0; \
1126 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001127}
1128
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001129#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 int _i; \
1131 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1132 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1133 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1134 if (!compiler_visit_ ## TYPE((C), elt)) { \
1135 compiler_exit_scope(c); \
1136 return 0; \
1137 } \
1138 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001139}
1140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141static int
1142compiler_isdocstring(stmt_ty s)
1143{
1144 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001145 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146 return s->v.Expr.value->kind == Str_kind;
1147}
1148
1149/* Compile a sequence of statements, checking for a docstring. */
1150
1151static int
1152compiler_body(struct compiler *c, asdl_seq *stmts)
1153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 int i = 0;
1155 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (!asdl_seq_LEN(stmts))
1158 return 1;
1159 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001160 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 /* don't generate docstrings if -OO */
1162 i = 1;
1163 VISIT(c, expr, st->v.Expr.value);
1164 if (!compiler_nameop(c, __doc__, Store))
1165 return 0;
1166 }
1167 for (; i < asdl_seq_LEN(stmts); i++)
1168 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1169 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001170}
1171
1172static PyCodeObject *
1173compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001174{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 PyCodeObject *co;
1176 int addNone = 1;
1177 static PyObject *module;
1178 if (!module) {
1179 module = PyUnicode_InternFromString("<module>");
1180 if (!module)
1181 return NULL;
1182 }
1183 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1184 if (!compiler_enter_scope(c, module, mod, 0))
1185 return NULL;
1186 switch (mod->kind) {
1187 case Module_kind:
1188 if (!compiler_body(c, mod->v.Module.body)) {
1189 compiler_exit_scope(c);
1190 return 0;
1191 }
1192 break;
1193 case Interactive_kind:
1194 c->c_interactive = 1;
1195 VISIT_SEQ_IN_SCOPE(c, stmt,
1196 mod->v.Interactive.body);
1197 break;
1198 case Expression_kind:
1199 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1200 addNone = 0;
1201 break;
1202 case Suite_kind:
1203 PyErr_SetString(PyExc_SystemError,
1204 "suite should not be possible");
1205 return 0;
1206 default:
1207 PyErr_Format(PyExc_SystemError,
1208 "module kind %d should not be possible",
1209 mod->kind);
1210 return 0;
1211 }
1212 co = assemble(c, addNone);
1213 compiler_exit_scope(c);
1214 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001215}
1216
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001217/* The test for LOCAL must come before the test for FREE in order to
1218 handle classes where name is both local and free. The local var is
1219 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001220*/
1221
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222static int
1223get_ref_type(struct compiler *c, PyObject *name)
1224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 int scope = PyST_GetScope(c->u->u_ste, name);
1226 if (scope == 0) {
1227 char buf[350];
1228 PyOS_snprintf(buf, sizeof(buf),
1229 "unknown scope for %.100s in %.100s(%s) in %s\n"
1230 "symbols: %s\nlocals: %s\nglobals: %s",
1231 PyBytes_AS_STRING(name),
1232 PyBytes_AS_STRING(c->u->u_name),
1233 PyObject_REPR(c->u->u_ste->ste_id),
1234 c->c_filename,
1235 PyObject_REPR(c->u->u_ste->ste_symbols),
1236 PyObject_REPR(c->u->u_varnames),
1237 PyObject_REPR(c->u->u_names)
1238 );
1239 Py_FatalError(buf);
1240 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001243}
1244
1245static int
1246compiler_lookup_arg(PyObject *dict, PyObject *name)
1247{
1248 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001249 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001251 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001253 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001254 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001255 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001256 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
1259static int
1260compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1261{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 int i, free = PyCode_GetNumFree(co);
1263 if (free == 0) {
1264 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1265 ADDOP_I(c, MAKE_FUNCTION, args);
1266 return 1;
1267 }
1268 for (i = 0; i < free; ++i) {
1269 /* Bypass com_addop_varname because it will generate
1270 LOAD_DEREF but LOAD_CLOSURE is needed.
1271 */
1272 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1273 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 /* Special case: If a class contains a method with a
1276 free variable that has the same name as a method,
1277 the name will be considered free *and* local in the
1278 class. It should be handled by the closure, as
1279 well as by the normal name loookup logic.
1280 */
1281 reftype = get_ref_type(c, name);
1282 if (reftype == CELL)
1283 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1284 else /* (reftype == FREE) */
1285 arg = compiler_lookup_arg(c->u->u_freevars, name);
1286 if (arg == -1) {
1287 fprintf(stderr,
1288 "lookup %s in %s %d %d\n"
1289 "freevars of %s: %s\n",
1290 PyObject_REPR(name),
1291 PyBytes_AS_STRING(c->u->u_name),
1292 reftype, arg,
1293 _PyUnicode_AsString(co->co_name),
1294 PyObject_REPR(co->co_freevars));
1295 Py_FatalError("compiler_make_closure()");
1296 }
1297 ADDOP_I(c, LOAD_CLOSURE, arg);
1298 }
1299 ADDOP_I(c, BUILD_TUPLE, free);
1300 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1301 ADDOP_I(c, MAKE_CLOSURE, args);
1302 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001303}
1304
1305static int
1306compiler_decorators(struct compiler *c, asdl_seq* decos)
1307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 if (!decos)
1311 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1314 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1315 }
1316 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317}
1318
1319static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001320compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 int i, default_count = 0;
1324 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1325 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1326 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1327 if (default_) {
1328 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1329 if (!compiler_visit_expr(c, default_)) {
1330 return -1;
1331 }
1332 default_count++;
1333 }
1334 }
1335 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001336}
1337
1338static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001339compiler_visit_argannotation(struct compiler *c, identifier id,
1340 expr_ty annotation, PyObject *names)
1341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (annotation) {
1343 VISIT(c, expr, annotation);
1344 if (PyList_Append(names, id))
1345 return -1;
1346 }
1347 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001348}
1349
1350static int
1351compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1352 PyObject *names)
1353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 int i, error;
1355 for (i = 0; i < asdl_seq_LEN(args); i++) {
1356 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1357 error = compiler_visit_argannotation(
1358 c,
1359 arg->arg,
1360 arg->annotation,
1361 names);
1362 if (error)
1363 return error;
1364 }
1365 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001366}
1367
1368static int
1369compiler_visit_annotations(struct compiler *c, arguments_ty args,
1370 expr_ty returns)
1371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* Push arg annotations and a list of the argument names. Return the #
1373 of items pushed. The expressions are evaluated out-of-order wrt the
1374 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1377 */
1378 static identifier return_str;
1379 PyObject *names;
1380 int len;
1381 names = PyList_New(0);
1382 if (!names)
1383 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 if (compiler_visit_argannotations(c, args->args, names))
1386 goto error;
1387 if (args->varargannotation &&
1388 compiler_visit_argannotation(c, args->vararg,
1389 args->varargannotation, names))
1390 goto error;
1391 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1392 goto error;
1393 if (args->kwargannotation &&
1394 compiler_visit_argannotation(c, args->kwarg,
1395 args->kwargannotation, names))
1396 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (!return_str) {
1399 return_str = PyUnicode_InternFromString("return");
1400 if (!return_str)
1401 goto error;
1402 }
1403 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1404 goto error;
1405 }
1406
1407 len = PyList_GET_SIZE(names);
1408 if (len > 65534) {
1409 /* len must fit in 16 bits, and len is incremented below */
1410 PyErr_SetString(PyExc_SyntaxError,
1411 "too many annotations");
1412 goto error;
1413 }
1414 if (len) {
1415 /* convert names to a tuple and place on stack */
1416 PyObject *elt;
1417 int i;
1418 PyObject *s = PyTuple_New(len);
1419 if (!s)
1420 goto error;
1421 for (i = 0; i < len; i++) {
1422 elt = PyList_GET_ITEM(names, i);
1423 Py_INCREF(elt);
1424 PyTuple_SET_ITEM(s, i, elt);
1425 }
1426 ADDOP_O(c, LOAD_CONST, s, consts);
1427 Py_DECREF(s);
1428 len++; /* include the just-pushed tuple */
1429 }
1430 Py_DECREF(names);
1431 return len;
Neal Norwitzc1505362006-12-28 06:47:50 +00001432
1433error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 Py_DECREF(names);
1435 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001436}
1437
1438static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439compiler_function(struct compiler *c, stmt_ty s)
1440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 PyCodeObject *co;
1442 PyObject *first_const = Py_None;
1443 arguments_ty args = s->v.FunctionDef.args;
1444 expr_ty returns = s->v.FunctionDef.returns;
1445 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1446 stmt_ty st;
1447 int i, n, docstring, kw_default_count = 0, arglength;
1448 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 if (!compiler_decorators(c, decos))
1453 return 0;
1454 if (args->kwonlyargs) {
1455 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1456 args->kw_defaults);
1457 if (res < 0)
1458 return 0;
1459 kw_default_count = res;
1460 }
1461 if (args->defaults)
1462 VISIT_SEQ(c, expr, args->defaults);
1463 num_annotations = compiler_visit_annotations(c, args, returns);
1464 if (num_annotations < 0)
1465 return 0;
1466 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1469 s->lineno))
1470 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1473 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001474 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 first_const = st->v.Expr.value->v.Str.s;
1476 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1477 compiler_exit_scope(c);
1478 return 0;
1479 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 c->u->u_argcount = asdl_seq_LEN(args->args);
1482 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1483 n = asdl_seq_LEN(s->v.FunctionDef.body);
1484 /* if there was a docstring, we need to skip the first statement */
1485 for (i = docstring; i < n; i++) {
1486 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1487 VISIT_IN_SCOPE(c, stmt, st);
1488 }
1489 co = assemble(c, 1);
1490 compiler_exit_scope(c);
1491 if (co == NULL)
1492 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 arglength = asdl_seq_LEN(args->defaults);
1495 arglength |= kw_default_count << 8;
1496 arglength |= num_annotations << 16;
1497 compiler_make_closure(c, co, arglength);
1498 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 /* decorators */
1501 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1502 ADDOP_I(c, CALL_FUNCTION, 1);
1503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
1508static int
1509compiler_class(struct compiler *c, stmt_ty s)
1510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 PyCodeObject *co;
1512 PyObject *str;
1513 int i;
1514 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (!compiler_decorators(c, decos))
1517 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 /* ultimately generate code for:
1520 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1521 where:
1522 <func> is a function/closure created from the class body;
1523 it has a single argument (__locals__) where the dict
1524 (or MutableSequence) representing the locals is passed
1525 <name> is the class name
1526 <bases> is the positional arguments and *varargs argument
1527 <keywords> is the keyword arguments and **kwds argument
1528 This borrows from compiler_call.
1529 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 /* 1. compile the class body into a code object */
1532 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1533 return 0;
1534 /* this block represents what we do in the new scope */
1535 {
1536 /* use the class name for name mangling */
1537 Py_INCREF(s->v.ClassDef.name);
1538 Py_XDECREF(c->u->u_private);
1539 c->u->u_private = s->v.ClassDef.name;
1540 /* force it to have one mandatory argument */
1541 c->u->u_argcount = 1;
1542 /* load the first argument (__locals__) ... */
1543 ADDOP_I(c, LOAD_FAST, 0);
1544 /* ... and store it into f_locals */
1545 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1546 /* load (global) __name__ ... */
1547 str = PyUnicode_InternFromString("__name__");
1548 if (!str || !compiler_nameop(c, str, Load)) {
1549 Py_XDECREF(str);
1550 compiler_exit_scope(c);
1551 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001552 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 Py_DECREF(str);
1554 /* ... and store it as __module__ */
1555 str = PyUnicode_InternFromString("__module__");
1556 if (!str || !compiler_nameop(c, str, Store)) {
1557 Py_XDECREF(str);
1558 compiler_exit_scope(c);
1559 return 0;
1560 }
1561 Py_DECREF(str);
1562 /* compile the body proper */
1563 if (!compiler_body(c, s->v.ClassDef.body)) {
1564 compiler_exit_scope(c);
1565 return 0;
1566 }
1567 /* return the (empty) __class__ cell */
1568 str = PyUnicode_InternFromString("__class__");
1569 if (str == NULL) {
1570 compiler_exit_scope(c);
1571 return 0;
1572 }
1573 i = compiler_lookup_arg(c->u->u_cellvars, str);
1574 Py_DECREF(str);
1575 if (i == -1) {
1576 /* This happens when nobody references the cell */
1577 PyErr_Clear();
1578 /* Return None */
1579 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1580 }
1581 else {
1582 /* Return the cell where to store __class__ */
1583 ADDOP_I(c, LOAD_CLOSURE, i);
1584 }
1585 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1586 /* create the code object */
1587 co = assemble(c, 1);
1588 }
1589 /* leave the new scope */
1590 compiler_exit_scope(c);
1591 if (co == NULL)
1592 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 /* 2. load the 'build_class' function */
1595 ADDOP(c, LOAD_BUILD_CLASS);
1596
1597 /* 3. load a function (or closure) made from the code object */
1598 compiler_make_closure(c, co, 0);
1599 Py_DECREF(co);
1600
1601 /* 4. load class name */
1602 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1603
1604 /* 5. generate the rest of the code for the call */
1605 if (!compiler_call_helper(c, 2,
1606 s->v.ClassDef.bases,
1607 s->v.ClassDef.keywords,
1608 s->v.ClassDef.starargs,
1609 s->v.ClassDef.kwargs))
1610 return 0;
1611
1612 /* 6. apply decorators */
1613 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1614 ADDOP_I(c, CALL_FUNCTION, 1);
1615 }
1616
1617 /* 7. store into <name> */
1618 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1619 return 0;
1620 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621}
1622
1623static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001624compiler_ifexp(struct compiler *c, expr_ty e)
1625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001626 basicblock *end, *next;
1627
1628 assert(e->kind == IfExp_kind);
1629 end = compiler_new_block(c);
1630 if (end == NULL)
1631 return 0;
1632 next = compiler_new_block(c);
1633 if (next == NULL)
1634 return 0;
1635 VISIT(c, expr, e->v.IfExp.test);
1636 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1637 VISIT(c, expr, e->v.IfExp.body);
1638 ADDOP_JREL(c, JUMP_FORWARD, end);
1639 compiler_use_next_block(c, next);
1640 VISIT(c, expr, e->v.IfExp.orelse);
1641 compiler_use_next_block(c, end);
1642 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001643}
1644
1645static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646compiler_lambda(struct compiler *c, expr_ty e)
1647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 PyCodeObject *co;
1649 static identifier name;
1650 int kw_default_count = 0, arglength;
1651 arguments_ty args = e->v.Lambda.args;
1652 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (!name) {
1655 name = PyUnicode_InternFromString("<lambda>");
1656 if (!name)
1657 return 0;
1658 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (args->kwonlyargs) {
1661 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1662 args->kw_defaults);
1663 if (res < 0) return 0;
1664 kw_default_count = res;
1665 }
1666 if (args->defaults)
1667 VISIT_SEQ(c, expr, args->defaults);
1668 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1669 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 /* Make None the first constant, so the lambda can't have a
1672 docstring. */
1673 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1674 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 c->u->u_argcount = asdl_seq_LEN(args->args);
1677 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1678 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1679 if (c->u->u_ste->ste_generator) {
1680 ADDOP_IN_SCOPE(c, POP_TOP);
1681 }
1682 else {
1683 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1684 }
1685 co = assemble(c, 1);
1686 compiler_exit_scope(c);
1687 if (co == NULL)
1688 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 arglength = asdl_seq_LEN(args->defaults);
1691 arglength |= kw_default_count << 8;
1692 compiler_make_closure(c, co, arglength);
1693 Py_DECREF(co);
1694
1695 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696}
1697
1698static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699compiler_if(struct compiler *c, stmt_ty s)
1700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 basicblock *end, *next;
1702 int constant;
1703 assert(s->kind == If_kind);
1704 end = compiler_new_block(c);
1705 if (end == NULL)
1706 return 0;
1707
Georg Brandl8334fd92010-12-04 10:26:46 +00001708 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 /* constant = 0: "if 0"
1710 * constant = 1: "if 1", "if 2", ...
1711 * constant = -1: rest */
1712 if (constant == 0) {
1713 if (s->v.If.orelse)
1714 VISIT_SEQ(c, stmt, s->v.If.orelse);
1715 } else if (constant == 1) {
1716 VISIT_SEQ(c, stmt, s->v.If.body);
1717 } else {
1718 if (s->v.If.orelse) {
1719 next = compiler_new_block(c);
1720 if (next == NULL)
1721 return 0;
1722 }
1723 else
1724 next = end;
1725 VISIT(c, expr, s->v.If.test);
1726 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1727 VISIT_SEQ(c, stmt, s->v.If.body);
1728 ADDOP_JREL(c, JUMP_FORWARD, end);
1729 if (s->v.If.orelse) {
1730 compiler_use_next_block(c, next);
1731 VISIT_SEQ(c, stmt, s->v.If.orelse);
1732 }
1733 }
1734 compiler_use_next_block(c, end);
1735 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736}
1737
1738static int
1739compiler_for(struct compiler *c, stmt_ty s)
1740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 start = compiler_new_block(c);
1744 cleanup = compiler_new_block(c);
1745 end = compiler_new_block(c);
1746 if (start == NULL || end == NULL || cleanup == NULL)
1747 return 0;
1748 ADDOP_JREL(c, SETUP_LOOP, end);
1749 if (!compiler_push_fblock(c, LOOP, start))
1750 return 0;
1751 VISIT(c, expr, s->v.For.iter);
1752 ADDOP(c, GET_ITER);
1753 compiler_use_next_block(c, start);
1754 ADDOP_JREL(c, FOR_ITER, cleanup);
1755 VISIT(c, expr, s->v.For.target);
1756 VISIT_SEQ(c, stmt, s->v.For.body);
1757 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1758 compiler_use_next_block(c, cleanup);
1759 ADDOP(c, POP_BLOCK);
1760 compiler_pop_fblock(c, LOOP, start);
1761 VISIT_SEQ(c, stmt, s->v.For.orelse);
1762 compiler_use_next_block(c, end);
1763 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764}
1765
1766static int
1767compiler_while(struct compiler *c, stmt_ty s)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00001770 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 if (constant == 0) {
1773 if (s->v.While.orelse)
1774 VISIT_SEQ(c, stmt, s->v.While.orelse);
1775 return 1;
1776 }
1777 loop = compiler_new_block(c);
1778 end = compiler_new_block(c);
1779 if (constant == -1) {
1780 anchor = compiler_new_block(c);
1781 if (anchor == NULL)
1782 return 0;
1783 }
1784 if (loop == NULL || end == NULL)
1785 return 0;
1786 if (s->v.While.orelse) {
1787 orelse = compiler_new_block(c);
1788 if (orelse == NULL)
1789 return 0;
1790 }
1791 else
1792 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 ADDOP_JREL(c, SETUP_LOOP, end);
1795 compiler_use_next_block(c, loop);
1796 if (!compiler_push_fblock(c, LOOP, loop))
1797 return 0;
1798 if (constant == -1) {
1799 VISIT(c, expr, s->v.While.test);
1800 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1801 }
1802 VISIT_SEQ(c, stmt, s->v.While.body);
1803 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 /* XXX should the two POP instructions be in a separate block
1806 if there is no else clause ?
1807 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 if (constant == -1) {
1810 compiler_use_next_block(c, anchor);
1811 ADDOP(c, POP_BLOCK);
1812 }
1813 compiler_pop_fblock(c, LOOP, loop);
1814 if (orelse != NULL) /* what if orelse is just pass? */
1815 VISIT_SEQ(c, stmt, s->v.While.orelse);
1816 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819}
1820
1821static int
1822compiler_continue(struct compiler *c)
1823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1825 static const char IN_FINALLY_ERROR_MSG[] =
1826 "'continue' not supported inside 'finally' clause";
1827 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 if (!c->u->u_nfblocks)
1830 return compiler_error(c, LOOP_ERROR_MSG);
1831 i = c->u->u_nfblocks - 1;
1832 switch (c->u->u_fblock[i].fb_type) {
1833 case LOOP:
1834 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1835 break;
1836 case EXCEPT:
1837 case FINALLY_TRY:
1838 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1839 /* Prevent continue anywhere under a finally
1840 even if hidden in a sub-try or except. */
1841 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1842 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1843 }
1844 if (i == -1)
1845 return compiler_error(c, LOOP_ERROR_MSG);
1846 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1847 break;
1848 case FINALLY_END:
1849 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1850 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853}
1854
1855/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856
1857 SETUP_FINALLY L
1858 <code for body>
1859 POP_BLOCK
1860 LOAD_CONST <None>
1861 L: <code for finalbody>
1862 END_FINALLY
1863
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864 The special instructions use the block stack. Each block
1865 stack entry contains the instruction that created it (here
1866 SETUP_FINALLY), the level of the value stack at the time the
1867 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 Pushes the current value stack level and the label
1871 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 Pops en entry from the block stack, and pops the value
1874 stack until its level is the same as indicated on the
1875 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 Pops a variable number of entries from the *value* stack
1878 and re-raises the exception they specify. The number of
1879 entries popped depends on the (pseudo) exception type.
1880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881 The block stack is unwound when an exception is raised:
1882 when a SETUP_FINALLY entry is found, the exception is pushed
1883 onto the value stack (and the exception condition is cleared),
1884 and the interpreter jumps to the label gotten from the block
1885 stack.
1886*/
1887
1888static int
1889compiler_try_finally(struct compiler *c, stmt_ty s)
1890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 basicblock *body, *end;
1892 body = compiler_new_block(c);
1893 end = compiler_new_block(c);
1894 if (body == NULL || end == NULL)
1895 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 ADDOP_JREL(c, SETUP_FINALLY, end);
1898 compiler_use_next_block(c, body);
1899 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1900 return 0;
1901 VISIT_SEQ(c, stmt, s->v.TryFinally.body);
1902 ADDOP(c, POP_BLOCK);
1903 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1906 compiler_use_next_block(c, end);
1907 if (!compiler_push_fblock(c, FINALLY_END, end))
1908 return 0;
1909 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody);
1910 ADDOP(c, END_FINALLY);
1911 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914}
1915
1916/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001917 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918 (The contents of the value stack is shown in [], with the top
1919 at the right; 'tb' is trace-back info, 'val' the exception's
1920 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921
1922 Value stack Label Instruction Argument
1923 [] SETUP_EXCEPT L1
1924 [] <code for S>
1925 [] POP_BLOCK
1926 [] JUMP_FORWARD L0
1927
1928 [tb, val, exc] L1: DUP )
1929 [tb, val, exc, exc] <evaluate E1> )
1930 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1931 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1932 [tb, val, exc] POP
1933 [tb, val] <assign to V1> (or POP if no V1)
1934 [tb] POP
1935 [] <code for S1>
1936 JUMP_FORWARD L0
1937
1938 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939 .............................etc.......................
1940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1942
1943 [] L0: <next statement>
1944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945 Of course, parts are not generated if Vi or Ei is not present.
1946*/
1947static int
1948compiler_try_except(struct compiler *c, stmt_ty s)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 basicblock *body, *orelse, *except, *end;
1951 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 body = compiler_new_block(c);
1954 except = compiler_new_block(c);
1955 orelse = compiler_new_block(c);
1956 end = compiler_new_block(c);
1957 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1958 return 0;
1959 ADDOP_JREL(c, SETUP_EXCEPT, except);
1960 compiler_use_next_block(c, body);
1961 if (!compiler_push_fblock(c, EXCEPT, body))
1962 return 0;
1963 VISIT_SEQ(c, stmt, s->v.TryExcept.body);
1964 ADDOP(c, POP_BLOCK);
1965 compiler_pop_fblock(c, EXCEPT, body);
1966 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1967 n = asdl_seq_LEN(s->v.TryExcept.handlers);
1968 compiler_use_next_block(c, except);
1969 for (i = 0; i < n; i++) {
1970 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1971 s->v.TryExcept.handlers, i);
1972 if (!handler->v.ExceptHandler.type && i < n-1)
1973 return compiler_error(c, "default 'except:' must be last");
1974 c->u->u_lineno_set = 0;
1975 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001976 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 except = compiler_new_block(c);
1978 if (except == NULL)
1979 return 0;
1980 if (handler->v.ExceptHandler.type) {
1981 ADDOP(c, DUP_TOP);
1982 VISIT(c, expr, handler->v.ExceptHandler.type);
1983 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1984 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1985 }
1986 ADDOP(c, POP_TOP);
1987 if (handler->v.ExceptHandler.name) {
1988 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001990 cleanup_end = compiler_new_block(c);
1991 cleanup_body = compiler_new_block(c);
1992 if(!(cleanup_end || cleanup_body))
1993 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
1996 ADDOP(c, POP_TOP);
1997
1998 /*
1999 try:
2000 # body
2001 except type as name:
2002 try:
2003 # body
2004 finally:
2005 name = None
2006 del name
2007 */
2008
2009 /* second try: */
2010 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2011 compiler_use_next_block(c, cleanup_body);
2012 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2013 return 0;
2014
2015 /* second # body */
2016 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2017 ADDOP(c, POP_BLOCK);
2018 ADDOP(c, POP_EXCEPT);
2019 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2020
2021 /* finally: */
2022 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2023 compiler_use_next_block(c, cleanup_end);
2024 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2025 return 0;
2026
2027 /* name = None */
2028 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2029 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2030
2031 /* del name */
2032 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2033
2034 ADDOP(c, END_FINALLY);
2035 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2036 }
2037 else {
2038 basicblock *cleanup_body;
2039
2040 cleanup_body = compiler_new_block(c);
2041 if(!cleanup_body)
2042 return 0;
2043
Guido van Rossumb940e112007-01-10 16:19:56 +00002044 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 ADDOP(c, POP_TOP);
2046 compiler_use_next_block(c, cleanup_body);
2047 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2048 return 0;
2049 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2050 ADDOP(c, POP_EXCEPT);
2051 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2052 }
2053 ADDOP_JREL(c, JUMP_FORWARD, end);
2054 compiler_use_next_block(c, except);
2055 }
2056 ADDOP(c, END_FINALLY);
2057 compiler_use_next_block(c, orelse);
2058 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse);
2059 compiler_use_next_block(c, end);
2060 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002061}
2062
2063static int
2064compiler_import_as(struct compiler *c, identifier name, identifier asname)
2065{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 /* The IMPORT_NAME opcode was already generated. This function
2067 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 If there is a dot in name, we need to split it and emit a
2070 LOAD_ATTR for each name.
2071 */
2072 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2073 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
2074 if (dot) {
2075 /* Consume the base module name to get the first attribute */
2076 src = dot + 1;
2077 while (dot) {
2078 /* NB src is only defined when dot != NULL */
2079 PyObject *attr;
2080 dot = Py_UNICODE_strchr(src, '.');
2081 attr = PyUnicode_FromUnicode(src,
2082 dot ? dot - src : Py_UNICODE_strlen(src));
2083 if (!attr)
2084 return -1;
2085 ADDOP_O(c, LOAD_ATTR, attr, names);
2086 Py_DECREF(attr);
2087 src = dot + 1;
2088 }
2089 }
2090 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091}
2092
2093static int
2094compiler_import(struct compiler *c, stmt_ty s)
2095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 /* The Import node stores a module name like a.b.c as a single
2097 string. This is convenient for all cases except
2098 import a.b.c as d
2099 where we need to parse that string to extract the individual
2100 module names.
2101 XXX Perhaps change the representation to make this case simpler?
2102 */
2103 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 for (i = 0; i < n; i++) {
2106 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2107 int r;
2108 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 level = PyLong_FromLong(0);
2111 if (level == NULL)
2112 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 ADDOP_O(c, LOAD_CONST, level, consts);
2115 Py_DECREF(level);
2116 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2117 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 if (alias->asname) {
2120 r = compiler_import_as(c, alias->name, alias->asname);
2121 if (!r)
2122 return r;
2123 }
2124 else {
2125 identifier tmp = alias->name;
2126 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2127 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
2128 if (dot)
2129 tmp = PyUnicode_FromUnicode(base,
2130 dot - base);
2131 r = compiler_nameop(c, tmp, Store);
2132 if (dot) {
2133 Py_DECREF(tmp);
2134 }
2135 if (!r)
2136 return r;
2137 }
2138 }
2139 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002140}
2141
2142static int
2143compiler_from_import(struct compiler *c, stmt_ty s)
2144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 PyObject *names = PyTuple_New(n);
2148 PyObject *level;
2149 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 if (!empty_string) {
2152 empty_string = PyUnicode_FromString("");
2153 if (!empty_string)
2154 return 0;
2155 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 if (!names)
2158 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160 level = PyLong_FromLong(s->v.ImportFrom.level);
2161 if (!level) {
2162 Py_DECREF(names);
2163 return 0;
2164 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 /* build up the names */
2167 for (i = 0; i < n; i++) {
2168 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2169 Py_INCREF(alias->name);
2170 PyTuple_SET_ITEM(names, i, alias->name);
2171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2174 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2175 Py_DECREF(level);
2176 Py_DECREF(names);
2177 return compiler_error(c, "from __future__ imports must occur "
2178 "at the beginning of the file");
2179 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 ADDOP_O(c, LOAD_CONST, level, consts);
2182 Py_DECREF(level);
2183 ADDOP_O(c, LOAD_CONST, names, consts);
2184 Py_DECREF(names);
2185 if (s->v.ImportFrom.module) {
2186 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2187 }
2188 else {
2189 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2190 }
2191 for (i = 0; i < n; i++) {
2192 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2193 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
2196 assert(n == 1);
2197 ADDOP(c, IMPORT_STAR);
2198 return 1;
2199 }
2200
2201 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2202 store_name = alias->name;
2203 if (alias->asname)
2204 store_name = alias->asname;
2205
2206 if (!compiler_nameop(c, store_name, Store)) {
2207 Py_DECREF(names);
2208 return 0;
2209 }
2210 }
2211 /* remove imported module */
2212 ADDOP(c, POP_TOP);
2213 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002214}
2215
2216static int
2217compiler_assert(struct compiler *c, stmt_ty s)
2218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 static PyObject *assertion_error = NULL;
2220 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002221
Georg Brandl8334fd92010-12-04 10:26:46 +00002222 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 return 1;
2224 if (assertion_error == NULL) {
2225 assertion_error = PyUnicode_InternFromString("AssertionError");
2226 if (assertion_error == NULL)
2227 return 0;
2228 }
2229 if (s->v.Assert.test->kind == Tuple_kind &&
2230 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2231 const char* msg =
2232 "assertion is always true, perhaps remove parentheses?";
2233 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2234 c->u->u_lineno, NULL, NULL) == -1)
2235 return 0;
2236 }
2237 VISIT(c, expr, s->v.Assert.test);
2238 end = compiler_new_block(c);
2239 if (end == NULL)
2240 return 0;
2241 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2242 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2243 if (s->v.Assert.msg) {
2244 VISIT(c, expr, s->v.Assert.msg);
2245 ADDOP_I(c, CALL_FUNCTION, 1);
2246 }
2247 ADDOP_I(c, RAISE_VARARGS, 1);
2248 compiler_use_next_block(c, end);
2249 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002250}
2251
2252static int
2253compiler_visit_stmt(struct compiler *c, stmt_ty s)
2254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 /* Always assign a lineno to the next instruction for a stmt. */
2258 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002259 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 switch (s->kind) {
2263 case FunctionDef_kind:
2264 return compiler_function(c, s);
2265 case ClassDef_kind:
2266 return compiler_class(c, s);
2267 case Return_kind:
2268 if (c->u->u_ste->ste_type != FunctionBlock)
2269 return compiler_error(c, "'return' outside function");
2270 if (s->v.Return.value) {
2271 VISIT(c, expr, s->v.Return.value);
2272 }
2273 else
2274 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2275 ADDOP(c, RETURN_VALUE);
2276 break;
2277 case Delete_kind:
2278 VISIT_SEQ(c, expr, s->v.Delete.targets)
2279 break;
2280 case Assign_kind:
2281 n = asdl_seq_LEN(s->v.Assign.targets);
2282 VISIT(c, expr, s->v.Assign.value);
2283 for (i = 0; i < n; i++) {
2284 if (i < n - 1)
2285 ADDOP(c, DUP_TOP);
2286 VISIT(c, expr,
2287 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2288 }
2289 break;
2290 case AugAssign_kind:
2291 return compiler_augassign(c, s);
2292 case For_kind:
2293 return compiler_for(c, s);
2294 case While_kind:
2295 return compiler_while(c, s);
2296 case If_kind:
2297 return compiler_if(c, s);
2298 case Raise_kind:
2299 n = 0;
2300 if (s->v.Raise.exc) {
2301 VISIT(c, expr, s->v.Raise.exc);
2302 n++;
2303 if (s->v.Raise.cause) {
2304 VISIT(c, expr, s->v.Raise.cause);
2305 n++;
2306 }
2307 }
2308 ADDOP_I(c, RAISE_VARARGS, n);
2309 break;
2310 case TryExcept_kind:
2311 return compiler_try_except(c, s);
2312 case TryFinally_kind:
2313 return compiler_try_finally(c, s);
2314 case Assert_kind:
2315 return compiler_assert(c, s);
2316 case Import_kind:
2317 return compiler_import(c, s);
2318 case ImportFrom_kind:
2319 return compiler_from_import(c, s);
2320 case Global_kind:
2321 case Nonlocal_kind:
2322 break;
2323 case Expr_kind:
2324 if (c->c_interactive && c->c_nestlevel <= 1) {
2325 VISIT(c, expr, s->v.Expr.value);
2326 ADDOP(c, PRINT_EXPR);
2327 }
2328 else if (s->v.Expr.value->kind != Str_kind &&
2329 s->v.Expr.value->kind != Num_kind) {
2330 VISIT(c, expr, s->v.Expr.value);
2331 ADDOP(c, POP_TOP);
2332 }
2333 break;
2334 case Pass_kind:
2335 break;
2336 case Break_kind:
2337 if (!compiler_in_loop(c))
2338 return compiler_error(c, "'break' outside loop");
2339 ADDOP(c, BREAK_LOOP);
2340 break;
2341 case Continue_kind:
2342 return compiler_continue(c);
2343 case With_kind:
2344 return compiler_with(c, s);
2345 }
2346 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002347}
2348
2349static int
2350unaryop(unaryop_ty op)
2351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 switch (op) {
2353 case Invert:
2354 return UNARY_INVERT;
2355 case Not:
2356 return UNARY_NOT;
2357 case UAdd:
2358 return UNARY_POSITIVE;
2359 case USub:
2360 return UNARY_NEGATIVE;
2361 default:
2362 PyErr_Format(PyExc_SystemError,
2363 "unary op %d should not be possible", op);
2364 return 0;
2365 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002366}
2367
2368static int
2369binop(struct compiler *c, operator_ty op)
2370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 switch (op) {
2372 case Add:
2373 return BINARY_ADD;
2374 case Sub:
2375 return BINARY_SUBTRACT;
2376 case Mult:
2377 return BINARY_MULTIPLY;
2378 case Div:
2379 return BINARY_TRUE_DIVIDE;
2380 case Mod:
2381 return BINARY_MODULO;
2382 case Pow:
2383 return BINARY_POWER;
2384 case LShift:
2385 return BINARY_LSHIFT;
2386 case RShift:
2387 return BINARY_RSHIFT;
2388 case BitOr:
2389 return BINARY_OR;
2390 case BitXor:
2391 return BINARY_XOR;
2392 case BitAnd:
2393 return BINARY_AND;
2394 case FloorDiv:
2395 return BINARY_FLOOR_DIVIDE;
2396 default:
2397 PyErr_Format(PyExc_SystemError,
2398 "binary op %d should not be possible", op);
2399 return 0;
2400 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002401}
2402
2403static int
2404cmpop(cmpop_ty op)
2405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 switch (op) {
2407 case Eq:
2408 return PyCmp_EQ;
2409 case NotEq:
2410 return PyCmp_NE;
2411 case Lt:
2412 return PyCmp_LT;
2413 case LtE:
2414 return PyCmp_LE;
2415 case Gt:
2416 return PyCmp_GT;
2417 case GtE:
2418 return PyCmp_GE;
2419 case Is:
2420 return PyCmp_IS;
2421 case IsNot:
2422 return PyCmp_IS_NOT;
2423 case In:
2424 return PyCmp_IN;
2425 case NotIn:
2426 return PyCmp_NOT_IN;
2427 default:
2428 return PyCmp_BAD;
2429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002430}
2431
2432static int
2433inplace_binop(struct compiler *c, operator_ty op)
2434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 switch (op) {
2436 case Add:
2437 return INPLACE_ADD;
2438 case Sub:
2439 return INPLACE_SUBTRACT;
2440 case Mult:
2441 return INPLACE_MULTIPLY;
2442 case Div:
2443 return INPLACE_TRUE_DIVIDE;
2444 case Mod:
2445 return INPLACE_MODULO;
2446 case Pow:
2447 return INPLACE_POWER;
2448 case LShift:
2449 return INPLACE_LSHIFT;
2450 case RShift:
2451 return INPLACE_RSHIFT;
2452 case BitOr:
2453 return INPLACE_OR;
2454 case BitXor:
2455 return INPLACE_XOR;
2456 case BitAnd:
2457 return INPLACE_AND;
2458 case FloorDiv:
2459 return INPLACE_FLOOR_DIVIDE;
2460 default:
2461 PyErr_Format(PyExc_SystemError,
2462 "inplace binary op %d should not be possible", op);
2463 return 0;
2464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002465}
2466
2467static int
2468compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 int op, scope, arg;
2471 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 PyObject *dict = c->u->u_names;
2474 PyObject *mangled;
2475 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 mangled = _Py_Mangle(c->u->u_private, name);
2478 if (!mangled)
2479 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 op = 0;
2482 optype = OP_NAME;
2483 scope = PyST_GetScope(c->u->u_ste, mangled);
2484 switch (scope) {
2485 case FREE:
2486 dict = c->u->u_freevars;
2487 optype = OP_DEREF;
2488 break;
2489 case CELL:
2490 dict = c->u->u_cellvars;
2491 optype = OP_DEREF;
2492 break;
2493 case LOCAL:
2494 if (c->u->u_ste->ste_type == FunctionBlock)
2495 optype = OP_FAST;
2496 break;
2497 case GLOBAL_IMPLICIT:
2498 if (c->u->u_ste->ste_type == FunctionBlock &&
2499 !c->u->u_ste->ste_unoptimized)
2500 optype = OP_GLOBAL;
2501 break;
2502 case GLOBAL_EXPLICIT:
2503 optype = OP_GLOBAL;
2504 break;
2505 default:
2506 /* scope can be 0 */
2507 break;
2508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 /* XXX Leave assert here, but handle __doc__ and the like better */
2511 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 switch (optype) {
2514 case OP_DEREF:
2515 switch (ctx) {
2516 case Load: op = LOAD_DEREF; break;
2517 case Store: op = STORE_DEREF; break;
2518 case AugLoad:
2519 case AugStore:
2520 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002521 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 case Param:
2523 default:
2524 PyErr_SetString(PyExc_SystemError,
2525 "param invalid for deref variable");
2526 return 0;
2527 }
2528 break;
2529 case OP_FAST:
2530 switch (ctx) {
2531 case Load: op = LOAD_FAST; break;
2532 case Store: op = STORE_FAST; break;
2533 case Del: op = DELETE_FAST; break;
2534 case AugLoad:
2535 case AugStore:
2536 break;
2537 case Param:
2538 default:
2539 PyErr_SetString(PyExc_SystemError,
2540 "param invalid for local variable");
2541 return 0;
2542 }
2543 ADDOP_O(c, op, mangled, varnames);
2544 Py_DECREF(mangled);
2545 return 1;
2546 case OP_GLOBAL:
2547 switch (ctx) {
2548 case Load: op = LOAD_GLOBAL; break;
2549 case Store: op = STORE_GLOBAL; break;
2550 case Del: op = DELETE_GLOBAL; break;
2551 case AugLoad:
2552 case AugStore:
2553 break;
2554 case Param:
2555 default:
2556 PyErr_SetString(PyExc_SystemError,
2557 "param invalid for global variable");
2558 return 0;
2559 }
2560 break;
2561 case OP_NAME:
2562 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002563 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 case Store: op = STORE_NAME; break;
2565 case Del: op = DELETE_NAME; break;
2566 case AugLoad:
2567 case AugStore:
2568 break;
2569 case Param:
2570 default:
2571 PyErr_SetString(PyExc_SystemError,
2572 "param invalid for name variable");
2573 return 0;
2574 }
2575 break;
2576 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 assert(op);
2579 arg = compiler_add_o(c, dict, mangled);
2580 Py_DECREF(mangled);
2581 if (arg < 0)
2582 return 0;
2583 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002584}
2585
2586static int
2587compiler_boolop(struct compiler *c, expr_ty e)
2588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 basicblock *end;
2590 int jumpi, i, n;
2591 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002593 assert(e->kind == BoolOp_kind);
2594 if (e->v.BoolOp.op == And)
2595 jumpi = JUMP_IF_FALSE_OR_POP;
2596 else
2597 jumpi = JUMP_IF_TRUE_OR_POP;
2598 end = compiler_new_block(c);
2599 if (end == NULL)
2600 return 0;
2601 s = e->v.BoolOp.values;
2602 n = asdl_seq_LEN(s) - 1;
2603 assert(n >= 0);
2604 for (i = 0; i < n; ++i) {
2605 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2606 ADDOP_JABS(c, jumpi, end);
2607 }
2608 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2609 compiler_use_next_block(c, end);
2610 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002611}
2612
2613static int
2614compiler_list(struct compiler *c, expr_ty e)
2615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 int n = asdl_seq_LEN(e->v.List.elts);
2617 if (e->v.List.ctx == Store) {
2618 int i, seen_star = 0;
2619 for (i = 0; i < n; i++) {
2620 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2621 if (elt->kind == Starred_kind && !seen_star) {
2622 if ((i >= (1 << 8)) ||
2623 (n-i-1 >= (INT_MAX >> 8)))
2624 return compiler_error(c,
2625 "too many expressions in "
2626 "star-unpacking assignment");
2627 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2628 seen_star = 1;
2629 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2630 } else if (elt->kind == Starred_kind) {
2631 return compiler_error(c,
2632 "two starred expressions in assignment");
2633 }
2634 }
2635 if (!seen_star) {
2636 ADDOP_I(c, UNPACK_SEQUENCE, n);
2637 }
2638 }
2639 VISIT_SEQ(c, expr, e->v.List.elts);
2640 if (e->v.List.ctx == Load) {
2641 ADDOP_I(c, BUILD_LIST, n);
2642 }
2643 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002644}
2645
2646static int
2647compiler_tuple(struct compiler *c, expr_ty e)
2648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 int n = asdl_seq_LEN(e->v.Tuple.elts);
2650 if (e->v.Tuple.ctx == Store) {
2651 int i, seen_star = 0;
2652 for (i = 0; i < n; i++) {
2653 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2654 if (elt->kind == Starred_kind && !seen_star) {
2655 if ((i >= (1 << 8)) ||
2656 (n-i-1 >= (INT_MAX >> 8)))
2657 return compiler_error(c,
2658 "too many expressions in "
2659 "star-unpacking assignment");
2660 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2661 seen_star = 1;
2662 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2663 } else if (elt->kind == Starred_kind) {
2664 return compiler_error(c,
2665 "two starred expressions in assignment");
2666 }
2667 }
2668 if (!seen_star) {
2669 ADDOP_I(c, UNPACK_SEQUENCE, n);
2670 }
2671 }
2672 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2673 if (e->v.Tuple.ctx == Load) {
2674 ADDOP_I(c, BUILD_TUPLE, n);
2675 }
2676 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002677}
2678
2679static int
2680compiler_compare(struct compiler *c, expr_ty e)
2681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 int i, n;
2683 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2686 VISIT(c, expr, e->v.Compare.left);
2687 n = asdl_seq_LEN(e->v.Compare.ops);
2688 assert(n > 0);
2689 if (n > 1) {
2690 cleanup = compiler_new_block(c);
2691 if (cleanup == NULL)
2692 return 0;
2693 VISIT(c, expr,
2694 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2695 }
2696 for (i = 1; i < n; i++) {
2697 ADDOP(c, DUP_TOP);
2698 ADDOP(c, ROT_THREE);
2699 ADDOP_I(c, COMPARE_OP,
2700 cmpop((cmpop_ty)(asdl_seq_GET(
2701 e->v.Compare.ops, i - 1))));
2702 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2703 NEXT_BLOCK(c);
2704 if (i < (n - 1))
2705 VISIT(c, expr,
2706 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2707 }
2708 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2709 ADDOP_I(c, COMPARE_OP,
2710 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2711 if (n > 1) {
2712 basicblock *end = compiler_new_block(c);
2713 if (end == NULL)
2714 return 0;
2715 ADDOP_JREL(c, JUMP_FORWARD, end);
2716 compiler_use_next_block(c, cleanup);
2717 ADDOP(c, ROT_TWO);
2718 ADDOP(c, POP_TOP);
2719 compiler_use_next_block(c, end);
2720 }
2721 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002722}
2723
2724static int
2725compiler_call(struct compiler *c, expr_ty e)
2726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 VISIT(c, expr, e->v.Call.func);
2728 return compiler_call_helper(c, 0,
2729 e->v.Call.args,
2730 e->v.Call.keywords,
2731 e->v.Call.starargs,
2732 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002733}
2734
2735/* shared code between compiler_call and compiler_class */
2736static int
2737compiler_call_helper(struct compiler *c,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 int n, /* Args already pushed */
2739 asdl_seq *args,
2740 asdl_seq *keywords,
2741 expr_ty starargs,
2742 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 n += asdl_seq_LEN(args);
2747 VISIT_SEQ(c, expr, args);
2748 if (keywords) {
2749 VISIT_SEQ(c, keyword, keywords);
2750 n |= asdl_seq_LEN(keywords) << 8;
2751 }
2752 if (starargs) {
2753 VISIT(c, expr, starargs);
2754 code |= 1;
2755 }
2756 if (kwargs) {
2757 VISIT(c, expr, kwargs);
2758 code |= 2;
2759 }
2760 switch (code) {
2761 case 0:
2762 ADDOP_I(c, CALL_FUNCTION, n);
2763 break;
2764 case 1:
2765 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2766 break;
2767 case 2:
2768 ADDOP_I(c, CALL_FUNCTION_KW, n);
2769 break;
2770 case 3:
2771 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2772 break;
2773 }
2774 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002775}
2776
Nick Coghlan650f0d02007-04-15 12:05:43 +00002777
2778/* List and set comprehensions and generator expressions work by creating a
2779 nested function to perform the actual iteration. This means that the
2780 iteration variables don't leak into the current scope.
2781 The defined function is called immediately following its definition, with the
2782 result of that call being the result of the expression.
2783 The LC/SC version returns the populated container, while the GE version is
2784 flagged in symtable.c as a generator, so it returns the generator object
2785 when the function is called.
2786 This code *knows* that the loop cannot contain break, continue, or return,
2787 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2788
2789 Possible cleanups:
2790 - iterate over the generator sequence instead of using recursion
2791*/
2792
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002793static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794compiler_comprehension_generator(struct compiler *c,
2795 asdl_seq *generators, int gen_index,
2796 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 /* generate code for the iterator, then each of the ifs,
2799 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 comprehension_ty gen;
2802 basicblock *start, *anchor, *skip, *if_cleanup;
2803 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 start = compiler_new_block(c);
2806 skip = compiler_new_block(c);
2807 if_cleanup = compiler_new_block(c);
2808 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2811 anchor == NULL)
2812 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 if (gen_index == 0) {
2817 /* Receive outermost iter as an implicit argument */
2818 c->u->u_argcount = 1;
2819 ADDOP_I(c, LOAD_FAST, 0);
2820 }
2821 else {
2822 /* Sub-iter - calculate on the fly */
2823 VISIT(c, expr, gen->iter);
2824 ADDOP(c, GET_ITER);
2825 }
2826 compiler_use_next_block(c, start);
2827 ADDOP_JREL(c, FOR_ITER, anchor);
2828 NEXT_BLOCK(c);
2829 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 /* XXX this needs to be cleaned up...a lot! */
2832 n = asdl_seq_LEN(gen->ifs);
2833 for (i = 0; i < n; i++) {
2834 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2835 VISIT(c, expr, e);
2836 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2837 NEXT_BLOCK(c);
2838 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 if (++gen_index < asdl_seq_LEN(generators))
2841 if (!compiler_comprehension_generator(c,
2842 generators, gen_index,
2843 elt, val, type))
2844 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 /* only append after the last for generator */
2847 if (gen_index >= asdl_seq_LEN(generators)) {
2848 /* comprehension specific code */
2849 switch (type) {
2850 case COMP_GENEXP:
2851 VISIT(c, expr, elt);
2852 ADDOP(c, YIELD_VALUE);
2853 ADDOP(c, POP_TOP);
2854 break;
2855 case COMP_LISTCOMP:
2856 VISIT(c, expr, elt);
2857 ADDOP_I(c, LIST_APPEND, gen_index + 1);
2858 break;
2859 case COMP_SETCOMP:
2860 VISIT(c, expr, elt);
2861 ADDOP_I(c, SET_ADD, gen_index + 1);
2862 break;
2863 case COMP_DICTCOMP:
2864 /* With 'd[k] = v', v is evaluated before k, so we do
2865 the same. */
2866 VISIT(c, expr, val);
2867 VISIT(c, expr, elt);
2868 ADDOP_I(c, MAP_ADD, gen_index + 1);
2869 break;
2870 default:
2871 return 0;
2872 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 compiler_use_next_block(c, skip);
2875 }
2876 compiler_use_next_block(c, if_cleanup);
2877 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2878 compiler_use_next_block(c, anchor);
2879
2880 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002881}
2882
2883static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002884compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 PyCodeObject *co = NULL;
2888 expr_ty outermost_iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 outermost_iter = ((comprehension_ty)
2891 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2894 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 if (type != COMP_GENEXP) {
2897 int op;
2898 switch (type) {
2899 case COMP_LISTCOMP:
2900 op = BUILD_LIST;
2901 break;
2902 case COMP_SETCOMP:
2903 op = BUILD_SET;
2904 break;
2905 case COMP_DICTCOMP:
2906 op = BUILD_MAP;
2907 break;
2908 default:
2909 PyErr_Format(PyExc_SystemError,
2910 "unknown comprehension type %d", type);
2911 goto error_in_scope;
2912 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 ADDOP_I(c, op, 0);
2915 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 if (!compiler_comprehension_generator(c, generators, 0, elt,
2918 val, type))
2919 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 if (type != COMP_GENEXP) {
2922 ADDOP(c, RETURN_VALUE);
2923 }
2924
2925 co = assemble(c, 1);
2926 compiler_exit_scope(c);
2927 if (co == NULL)
2928 goto error;
2929
2930 if (!compiler_make_closure(c, co, 0))
2931 goto error;
2932 Py_DECREF(co);
2933
2934 VISIT(c, expr, outermost_iter);
2935 ADDOP(c, GET_ITER);
2936 ADDOP_I(c, CALL_FUNCTION, 1);
2937 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002938error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002940error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 Py_XDECREF(co);
2942 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002943}
2944
2945static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002946compiler_genexp(struct compiler *c, expr_ty e)
2947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 static identifier name;
2949 if (!name) {
2950 name = PyUnicode_FromString("<genexpr>");
2951 if (!name)
2952 return 0;
2953 }
2954 assert(e->kind == GeneratorExp_kind);
2955 return compiler_comprehension(c, e, COMP_GENEXP, name,
2956 e->v.GeneratorExp.generators,
2957 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958}
2959
2960static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002961compiler_listcomp(struct compiler *c, expr_ty e)
2962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 static identifier name;
2964 if (!name) {
2965 name = PyUnicode_FromString("<listcomp>");
2966 if (!name)
2967 return 0;
2968 }
2969 assert(e->kind == ListComp_kind);
2970 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2971 e->v.ListComp.generators,
2972 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002973}
2974
2975static int
2976compiler_setcomp(struct compiler *c, expr_ty e)
2977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 static identifier name;
2979 if (!name) {
2980 name = PyUnicode_FromString("<setcomp>");
2981 if (!name)
2982 return 0;
2983 }
2984 assert(e->kind == SetComp_kind);
2985 return compiler_comprehension(c, e, COMP_SETCOMP, name,
2986 e->v.SetComp.generators,
2987 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002988}
2989
2990
2991static int
2992compiler_dictcomp(struct compiler *c, expr_ty e)
2993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 static identifier name;
2995 if (!name) {
2996 name = PyUnicode_FromString("<dictcomp>");
2997 if (!name)
2998 return 0;
2999 }
3000 assert(e->kind == DictComp_kind);
3001 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3002 e->v.DictComp.generators,
3003 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003004}
3005
3006
3007static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003008compiler_visit_keyword(struct compiler *c, keyword_ty k)
3009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3011 VISIT(c, expr, k->value);
3012 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003013}
3014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016 whether they are true or false.
3017
3018 Return values: 1 for true, 0 for false, -1 for non-constant.
3019 */
3020
3021static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003022expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 char *id;
3025 switch (e->kind) {
3026 case Ellipsis_kind:
3027 return 1;
3028 case Num_kind:
3029 return PyObject_IsTrue(e->v.Num.n);
3030 case Str_kind:
3031 return PyObject_IsTrue(e->v.Str.s);
3032 case Name_kind:
3033 /* optimize away names that can't be reassigned */
3034 id = PyBytes_AS_STRING(
Victor Stinnerf3fd7332011-03-02 01:03:11 +00003035 _PyUnicode_AsDefaultEncodedString(e->v.Name.id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 if (strcmp(id, "True") == 0) return 1;
3037 if (strcmp(id, "False") == 0) return 0;
3038 if (strcmp(id, "None") == 0) return 0;
3039 if (strcmp(id, "__debug__") == 0)
Georg Brandl8334fd92010-12-04 10:26:46 +00003040 return ! c->c_optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 /* fall through */
3042 default:
3043 return -1;
3044 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003045}
3046
Guido van Rossumc2e20742006-02-27 22:32:47 +00003047/*
3048 Implements the with statement from PEP 343.
3049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003051
3052 with EXPR as VAR:
3053 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054
Guido van Rossumc2e20742006-02-27 22:32:47 +00003055 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056
Thomas Wouters477c8d52006-05-27 19:21:47 +00003057 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003058 exit = context.__exit__ # not calling it
3059 value = context.__enter__()
3060 try:
3061 VAR = value # if VAR present in the syntax
3062 BLOCK
3063 finally:
3064 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003066 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003068 exit(*exc)
3069 */
3070static int
3071compiler_with(struct compiler *c, stmt_ty s)
3072{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003073 basicblock *block, *finally;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003074
3075 assert(s->kind == With_kind);
3076
Guido van Rossumc2e20742006-02-27 22:32:47 +00003077 block = compiler_new_block(c);
3078 finally = compiler_new_block(c);
3079 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003080 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003081
Thomas Wouters477c8d52006-05-27 19:21:47 +00003082 /* Evaluate EXPR */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003083 VISIT(c, expr, s->v.With.context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003084 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003085
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003086 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003087 compiler_use_next_block(c, block);
3088 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003089 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003090 }
3091
3092 if (s->v.With.optional_vars) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003093 VISIT(c, expr, s->v.With.optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003094 }
3095 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003097 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003098 }
3099
3100 /* BLOCK code */
3101 VISIT_SEQ(c, stmt, s->v.With.body);
3102
3103 /* End of try block; start the finally block */
3104 ADDOP(c, POP_BLOCK);
3105 compiler_pop_fblock(c, FINALLY_TRY, block);
3106
3107 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3108 compiler_use_next_block(c, finally);
3109 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003110 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003111
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003112 /* Finally block starts; context.__exit__ is on the stack under
3113 the exception or return information. Just issue our magic
3114 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003115 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003116
3117 /* Finally block ends. */
3118 ADDOP(c, END_FINALLY);
3119 compiler_pop_fblock(c, FINALLY_END, finally);
3120 return 1;
3121}
3122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003123static int
3124compiler_visit_expr(struct compiler *c, expr_ty e)
3125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 /* If expr e has a different line number than the last expr/stmt,
3129 set a new line number for the next instruction.
3130 */
3131 if (e->lineno > c->u->u_lineno) {
3132 c->u->u_lineno = e->lineno;
3133 c->u->u_lineno_set = 0;
3134 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003135 /* Updating the column offset is always harmless. */
3136 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 switch (e->kind) {
3138 case BoolOp_kind:
3139 return compiler_boolop(c, e);
3140 case BinOp_kind:
3141 VISIT(c, expr, e->v.BinOp.left);
3142 VISIT(c, expr, e->v.BinOp.right);
3143 ADDOP(c, binop(c, e->v.BinOp.op));
3144 break;
3145 case UnaryOp_kind:
3146 VISIT(c, expr, e->v.UnaryOp.operand);
3147 ADDOP(c, unaryop(e->v.UnaryOp.op));
3148 break;
3149 case Lambda_kind:
3150 return compiler_lambda(c, e);
3151 case IfExp_kind:
3152 return compiler_ifexp(c, e);
3153 case Dict_kind:
3154 n = asdl_seq_LEN(e->v.Dict.values);
3155 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3156 for (i = 0; i < n; i++) {
3157 VISIT(c, expr,
3158 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3159 VISIT(c, expr,
3160 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3161 ADDOP(c, STORE_MAP);
3162 }
3163 break;
3164 case Set_kind:
3165 n = asdl_seq_LEN(e->v.Set.elts);
3166 VISIT_SEQ(c, expr, e->v.Set.elts);
3167 ADDOP_I(c, BUILD_SET, n);
3168 break;
3169 case GeneratorExp_kind:
3170 return compiler_genexp(c, e);
3171 case ListComp_kind:
3172 return compiler_listcomp(c, e);
3173 case SetComp_kind:
3174 return compiler_setcomp(c, e);
3175 case DictComp_kind:
3176 return compiler_dictcomp(c, e);
3177 case Yield_kind:
3178 if (c->u->u_ste->ste_type != FunctionBlock)
3179 return compiler_error(c, "'yield' outside function");
3180 if (e->v.Yield.value) {
3181 VISIT(c, expr, e->v.Yield.value);
3182 }
3183 else {
3184 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3185 }
3186 ADDOP(c, YIELD_VALUE);
3187 break;
3188 case Compare_kind:
3189 return compiler_compare(c, e);
3190 case Call_kind:
3191 return compiler_call(c, e);
3192 case Num_kind:
3193 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3194 break;
3195 case Str_kind:
3196 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3197 break;
3198 case Bytes_kind:
3199 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3200 break;
3201 case Ellipsis_kind:
3202 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3203 break;
3204 /* The following exprs can be assignment targets. */
3205 case Attribute_kind:
3206 if (e->v.Attribute.ctx != AugStore)
3207 VISIT(c, expr, e->v.Attribute.value);
3208 switch (e->v.Attribute.ctx) {
3209 case AugLoad:
3210 ADDOP(c, DUP_TOP);
3211 /* Fall through to load */
3212 case Load:
3213 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3214 break;
3215 case AugStore:
3216 ADDOP(c, ROT_TWO);
3217 /* Fall through to save */
3218 case Store:
3219 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3220 break;
3221 case Del:
3222 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3223 break;
3224 case Param:
3225 default:
3226 PyErr_SetString(PyExc_SystemError,
3227 "param invalid in attribute expression");
3228 return 0;
3229 }
3230 break;
3231 case Subscript_kind:
3232 switch (e->v.Subscript.ctx) {
3233 case AugLoad:
3234 VISIT(c, expr, e->v.Subscript.value);
3235 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3236 break;
3237 case Load:
3238 VISIT(c, expr, e->v.Subscript.value);
3239 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3240 break;
3241 case AugStore:
3242 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3243 break;
3244 case Store:
3245 VISIT(c, expr, e->v.Subscript.value);
3246 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3247 break;
3248 case Del:
3249 VISIT(c, expr, e->v.Subscript.value);
3250 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3251 break;
3252 case Param:
3253 default:
3254 PyErr_SetString(PyExc_SystemError,
3255 "param invalid in subscript expression");
3256 return 0;
3257 }
3258 break;
3259 case Starred_kind:
3260 switch (e->v.Starred.ctx) {
3261 case Store:
3262 /* In all legitimate cases, the Starred node was already replaced
3263 * by compiler_list/compiler_tuple. XXX: is that okay? */
3264 return compiler_error(c,
3265 "starred assignment target must be in a list or tuple");
3266 default:
3267 return compiler_error(c,
3268 "can use starred expression only as assignment target");
3269 }
3270 break;
3271 case Name_kind:
3272 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3273 /* child nodes of List and Tuple will have expr_context set */
3274 case List_kind:
3275 return compiler_list(c, e);
3276 case Tuple_kind:
3277 return compiler_tuple(c, e);
3278 }
3279 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003280}
3281
3282static int
3283compiler_augassign(struct compiler *c, stmt_ty s)
3284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 expr_ty e = s->v.AugAssign.target;
3286 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 switch (e->kind) {
3291 case Attribute_kind:
3292 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3293 AugLoad, e->lineno, e->col_offset, c->c_arena);
3294 if (auge == NULL)
3295 return 0;
3296 VISIT(c, expr, auge);
3297 VISIT(c, expr, s->v.AugAssign.value);
3298 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3299 auge->v.Attribute.ctx = AugStore;
3300 VISIT(c, expr, auge);
3301 break;
3302 case Subscript_kind:
3303 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3304 AugLoad, e->lineno, e->col_offset, c->c_arena);
3305 if (auge == NULL)
3306 return 0;
3307 VISIT(c, expr, auge);
3308 VISIT(c, expr, s->v.AugAssign.value);
3309 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3310 auge->v.Subscript.ctx = AugStore;
3311 VISIT(c, expr, auge);
3312 break;
3313 case Name_kind:
3314 if (!compiler_nameop(c, e->v.Name.id, Load))
3315 return 0;
3316 VISIT(c, expr, s->v.AugAssign.value);
3317 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3318 return compiler_nameop(c, e->v.Name.id, Store);
3319 default:
3320 PyErr_Format(PyExc_SystemError,
3321 "invalid node type (%d) for augmented assignment",
3322 e->kind);
3323 return 0;
3324 }
3325 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326}
3327
3328static int
3329compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 struct fblockinfo *f;
3332 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3333 PyErr_SetString(PyExc_SystemError,
3334 "too many statically nested blocks");
3335 return 0;
3336 }
3337 f = &c->u->u_fblock[c->u->u_nfblocks++];
3338 f->fb_type = t;
3339 f->fb_block = b;
3340 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003341}
3342
3343static void
3344compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 struct compiler_unit *u = c->u;
3347 assert(u->u_nfblocks > 0);
3348 u->u_nfblocks--;
3349 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3350 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003351}
3352
Thomas Wouters89f507f2006-12-13 04:49:30 +00003353static int
3354compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 int i;
3356 struct compiler_unit *u = c->u;
3357 for (i = 0; i < u->u_nfblocks; ++i) {
3358 if (u->u_fblock[i].fb_type == LOOP)
3359 return 1;
3360 }
3361 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003362}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003363/* Raises a SyntaxError and returns 0.
3364 If something goes wrong, a different exception may be raised.
3365*/
3366
3367static int
3368compiler_error(struct compiler *c, const char *errstr)
3369{
Benjamin Peterson43b06862011-05-27 09:08:01 -05003370 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3374 if (!loc) {
3375 Py_INCREF(Py_None);
3376 loc = Py_None;
3377 }
Benjamin Peterson43b06862011-05-27 09:08:01 -05003378 u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003379 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 if (!u)
3381 goto exit;
3382 v = Py_BuildValue("(zO)", errstr, u);
3383 if (!v)
3384 goto exit;
3385 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003387 Py_DECREF(loc);
3388 Py_XDECREF(u);
3389 Py_XDECREF(v);
3390 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391}
3392
3393static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394compiler_handle_subscr(struct compiler *c, const char *kind,
3395 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 /* XXX this code is duplicated */
3400 switch (ctx) {
3401 case AugLoad: /* fall through to Load */
3402 case Load: op = BINARY_SUBSCR; break;
3403 case AugStore:/* fall through to Store */
3404 case Store: op = STORE_SUBSCR; break;
3405 case Del: op = DELETE_SUBSCR; break;
3406 case Param:
3407 PyErr_Format(PyExc_SystemError,
3408 "invalid %s kind %d in subscript\n",
3409 kind, ctx);
3410 return 0;
3411 }
3412 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00003413 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 }
3415 else if (ctx == AugStore) {
3416 ADDOP(c, ROT_THREE);
3417 }
3418 ADDOP(c, op);
3419 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420}
3421
3422static int
3423compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 int n = 2;
3426 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 /* only handles the cases where BUILD_SLICE is emitted */
3429 if (s->v.Slice.lower) {
3430 VISIT(c, expr, s->v.Slice.lower);
3431 }
3432 else {
3433 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3434 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 if (s->v.Slice.upper) {
3437 VISIT(c, expr, s->v.Slice.upper);
3438 }
3439 else {
3440 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3441 }
3442
3443 if (s->v.Slice.step) {
3444 n++;
3445 VISIT(c, expr, s->v.Slice.step);
3446 }
3447 ADDOP_I(c, BUILD_SLICE, n);
3448 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003449}
3450
3451static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3453 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 switch (s->kind) {
3456 case Slice_kind:
3457 return compiler_slice(c, s, ctx);
3458 case Index_kind:
3459 VISIT(c, expr, s->v.Index.value);
3460 break;
3461 case ExtSlice_kind:
3462 default:
3463 PyErr_SetString(PyExc_SystemError,
3464 "extended slice invalid in nested slice");
3465 return 0;
3466 }
3467 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468}
3469
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470static int
3471compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 char * kindname = NULL;
3474 switch (s->kind) {
3475 case Index_kind:
3476 kindname = "index";
3477 if (ctx != AugStore) {
3478 VISIT(c, expr, s->v.Index.value);
3479 }
3480 break;
3481 case Slice_kind:
3482 kindname = "slice";
3483 if (ctx != AugStore) {
3484 if (!compiler_slice(c, s, ctx))
3485 return 0;
3486 }
3487 break;
3488 case ExtSlice_kind:
3489 kindname = "extended slice";
3490 if (ctx != AugStore) {
3491 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3492 for (i = 0; i < n; i++) {
3493 slice_ty sub = (slice_ty)asdl_seq_GET(
3494 s->v.ExtSlice.dims, i);
3495 if (!compiler_visit_nested_slice(c, sub, ctx))
3496 return 0;
3497 }
3498 ADDOP_I(c, BUILD_TUPLE, n);
3499 }
3500 break;
3501 default:
3502 PyErr_Format(PyExc_SystemError,
3503 "invalid subscript kind %d", s->kind);
3504 return 0;
3505 }
3506 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003507}
3508
Thomas Wouters89f507f2006-12-13 04:49:30 +00003509/* End of the compiler section, beginning of the assembler section */
3510
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003511/* do depth-first search of basic block graph, starting with block.
3512 post records the block indices in post-order.
3513
3514 XXX must handle implicit jumps from one block to next
3515*/
3516
Thomas Wouters89f507f2006-12-13 04:49:30 +00003517struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 PyObject *a_bytecode; /* string containing bytecode */
3519 int a_offset; /* offset into bytecode */
3520 int a_nblocks; /* number of reachable blocks */
3521 basicblock **a_postorder; /* list of blocks in dfs postorder */
3522 PyObject *a_lnotab; /* string containing lnotab */
3523 int a_lnotab_off; /* offset into lnotab */
3524 int a_lineno; /* last lineno of emitted instruction */
3525 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003526};
3527
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528static void
3529dfs(struct compiler *c, basicblock *b, struct assembler *a)
3530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 int i;
3532 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 if (b->b_seen)
3535 return;
3536 b->b_seen = 1;
3537 if (b->b_next != NULL)
3538 dfs(c, b->b_next, a);
3539 for (i = 0; i < b->b_iused; i++) {
3540 instr = &b->b_instr[i];
3541 if (instr->i_jrel || instr->i_jabs)
3542 dfs(c, instr->i_target, a);
3543 }
3544 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003545}
3546
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003547static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003548stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 int i, target_depth;
3551 struct instr *instr;
3552 if (b->b_seen || b->b_startdepth >= depth)
3553 return maxdepth;
3554 b->b_seen = 1;
3555 b->b_startdepth = depth;
3556 for (i = 0; i < b->b_iused; i++) {
3557 instr = &b->b_instr[i];
3558 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3559 if (depth > maxdepth)
3560 maxdepth = depth;
3561 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3562 if (instr->i_jrel || instr->i_jabs) {
3563 target_depth = depth;
3564 if (instr->i_opcode == FOR_ITER) {
3565 target_depth = depth-2;
3566 } else if (instr->i_opcode == SETUP_FINALLY ||
3567 instr->i_opcode == SETUP_EXCEPT) {
3568 target_depth = depth+3;
3569 if (target_depth > maxdepth)
3570 maxdepth = target_depth;
3571 }
3572 maxdepth = stackdepth_walk(c, instr->i_target,
3573 target_depth, maxdepth);
3574 if (instr->i_opcode == JUMP_ABSOLUTE ||
3575 instr->i_opcode == JUMP_FORWARD) {
3576 goto out; /* remaining code is dead */
3577 }
3578 }
3579 }
3580 if (b->b_next)
3581 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003582out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 b->b_seen = 0;
3584 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585}
3586
3587/* Find the flow path that needs the largest stack. We assume that
3588 * cycles in the flow graph have no net effect on the stack depth.
3589 */
3590static int
3591stackdepth(struct compiler *c)
3592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 basicblock *b, *entryblock;
3594 entryblock = NULL;
3595 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3596 b->b_seen = 0;
3597 b->b_startdepth = INT_MIN;
3598 entryblock = b;
3599 }
3600 if (!entryblock)
3601 return 0;
3602 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603}
3604
3605static int
3606assemble_init(struct assembler *a, int nblocks, int firstlineno)
3607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 memset(a, 0, sizeof(struct assembler));
3609 a->a_lineno = firstlineno;
3610 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3611 if (!a->a_bytecode)
3612 return 0;
3613 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3614 if (!a->a_lnotab)
3615 return 0;
3616 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3617 PyErr_NoMemory();
3618 return 0;
3619 }
3620 a->a_postorder = (basicblock **)PyObject_Malloc(
3621 sizeof(basicblock *) * nblocks);
3622 if (!a->a_postorder) {
3623 PyErr_NoMemory();
3624 return 0;
3625 }
3626 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003627}
3628
3629static void
3630assemble_free(struct assembler *a)
3631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 Py_XDECREF(a->a_bytecode);
3633 Py_XDECREF(a->a_lnotab);
3634 if (a->a_postorder)
3635 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636}
3637
3638/* Return the size of a basic block in bytes. */
3639
3640static int
3641instrsize(struct instr *instr)
3642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 if (!instr->i_hasarg)
3644 return 1; /* 1 byte for the opcode*/
3645 if (instr->i_oparg > 0xffff)
3646 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3647 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003648}
3649
3650static int
3651blocksize(basicblock *b)
3652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 int i;
3654 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 for (i = 0; i < b->b_iused; i++)
3657 size += instrsize(&b->b_instr[i]);
3658 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003659}
3660
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003661/* Appends a pair to the end of the line number table, a_lnotab, representing
3662 the instruction's bytecode offset and line number. See
3663 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003664
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003665static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003666assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 int d_bytecode, d_lineno;
3669 int len;
3670 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 d_bytecode = a->a_offset - a->a_lineno_off;
3673 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 assert(d_bytecode >= 0);
3676 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 if(d_bytecode == 0 && d_lineno == 0)
3679 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 if (d_bytecode > 255) {
3682 int j, nbytes, ncodes = d_bytecode / 255;
3683 nbytes = a->a_lnotab_off + 2 * ncodes;
3684 len = PyBytes_GET_SIZE(a->a_lnotab);
3685 if (nbytes >= len) {
3686 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3687 len = nbytes;
3688 else if (len <= INT_MAX / 2)
3689 len *= 2;
3690 else {
3691 PyErr_NoMemory();
3692 return 0;
3693 }
3694 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3695 return 0;
3696 }
3697 lnotab = (unsigned char *)
3698 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3699 for (j = 0; j < ncodes; j++) {
3700 *lnotab++ = 255;
3701 *lnotab++ = 0;
3702 }
3703 d_bytecode -= ncodes * 255;
3704 a->a_lnotab_off += ncodes * 2;
3705 }
3706 assert(d_bytecode <= 255);
3707 if (d_lineno > 255) {
3708 int j, nbytes, ncodes = d_lineno / 255;
3709 nbytes = a->a_lnotab_off + 2 * ncodes;
3710 len = PyBytes_GET_SIZE(a->a_lnotab);
3711 if (nbytes >= len) {
3712 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3713 len = nbytes;
3714 else if (len <= INT_MAX / 2)
3715 len *= 2;
3716 else {
3717 PyErr_NoMemory();
3718 return 0;
3719 }
3720 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3721 return 0;
3722 }
3723 lnotab = (unsigned char *)
3724 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3725 *lnotab++ = d_bytecode;
3726 *lnotab++ = 255;
3727 d_bytecode = 0;
3728 for (j = 1; j < ncodes; j++) {
3729 *lnotab++ = 0;
3730 *lnotab++ = 255;
3731 }
3732 d_lineno -= ncodes * 255;
3733 a->a_lnotab_off += ncodes * 2;
3734 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 len = PyBytes_GET_SIZE(a->a_lnotab);
3737 if (a->a_lnotab_off + 2 >= len) {
3738 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3739 return 0;
3740 }
3741 lnotab = (unsigned char *)
3742 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 a->a_lnotab_off += 2;
3745 if (d_bytecode) {
3746 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003747 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 }
3749 else { /* First line of a block; def stmt, etc. */
3750 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003751 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 }
3753 a->a_lineno = i->i_lineno;
3754 a->a_lineno_off = a->a_offset;
3755 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003756}
3757
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003758/* assemble_emit()
3759 Extend the bytecode with a new instruction.
3760 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003761*/
3762
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003763static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003764assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 int size, arg = 0, ext = 0;
3767 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3768 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 size = instrsize(i);
3771 if (i->i_hasarg) {
3772 arg = i->i_oparg;
3773 ext = arg >> 16;
3774 }
3775 if (i->i_lineno && !assemble_lnotab(a, i))
3776 return 0;
3777 if (a->a_offset + size >= len) {
3778 if (len > PY_SSIZE_T_MAX / 2)
3779 return 0;
3780 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3781 return 0;
3782 }
3783 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3784 a->a_offset += size;
3785 if (size == 6) {
3786 assert(i->i_hasarg);
3787 *code++ = (char)EXTENDED_ARG;
3788 *code++ = ext & 0xff;
3789 *code++ = ext >> 8;
3790 arg &= 0xffff;
3791 }
3792 *code++ = i->i_opcode;
3793 if (i->i_hasarg) {
3794 assert(size == 3 || size == 6);
3795 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003796 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
3798 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003799}
3800
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003801static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003802assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 basicblock *b;
3805 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3806 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 /* Compute the size of each block and fixup jump args.
3809 Replace block pointer with position in bytecode. */
3810 do {
3811 totsize = 0;
3812 for (i = a->a_nblocks - 1; i >= 0; i--) {
3813 b = a->a_postorder[i];
3814 bsize = blocksize(b);
3815 b->b_offset = totsize;
3816 totsize += bsize;
3817 }
3818 last_extended_arg_count = extended_arg_count;
3819 extended_arg_count = 0;
3820 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3821 bsize = b->b_offset;
3822 for (i = 0; i < b->b_iused; i++) {
3823 struct instr *instr = &b->b_instr[i];
3824 /* Relative jumps are computed relative to
3825 the instruction pointer after fetching
3826 the jump instruction.
3827 */
3828 bsize += instrsize(instr);
3829 if (instr->i_jabs)
3830 instr->i_oparg = instr->i_target->b_offset;
3831 else if (instr->i_jrel) {
3832 int delta = instr->i_target->b_offset - bsize;
3833 instr->i_oparg = delta;
3834 }
3835 else
3836 continue;
3837 if (instr->i_oparg > 0xffff)
3838 extended_arg_count++;
3839 }
3840 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 /* XXX: This is an awful hack that could hurt performance, but
3843 on the bright side it should work until we come up
3844 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 The issue is that in the first loop blocksize() is called
3847 which calls instrsize() which requires i_oparg be set
3848 appropriately. There is a bootstrap problem because
3849 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 So we loop until we stop seeing new EXTENDED_ARGs.
3852 The only EXTENDED_ARGs that could be popping up are
3853 ones in jump instructions. So this should converge
3854 fairly quickly.
3855 */
3856 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003857}
3858
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003859static PyObject *
3860dict_keys_inorder(PyObject *dict, int offset)
3861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 PyObject *tuple, *k, *v;
3863 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 tuple = PyTuple_New(size);
3866 if (tuple == NULL)
3867 return NULL;
3868 while (PyDict_Next(dict, &pos, &k, &v)) {
3869 i = PyLong_AS_LONG(v);
3870 /* The keys of the dictionary are tuples. (see compiler_add_o)
3871 The object we want is always first, though. */
3872 k = PyTuple_GET_ITEM(k, 0);
3873 Py_INCREF(k);
3874 assert((i - offset) < size);
3875 assert((i - offset) >= 0);
3876 PyTuple_SET_ITEM(tuple, i - offset, k);
3877 }
3878 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003879}
3880
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003881static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003882compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 PySTEntryObject *ste = c->u->u_ste;
3885 int flags = 0, n;
3886 if (ste->ste_type != ModuleBlock)
3887 flags |= CO_NEWLOCALS;
3888 if (ste->ste_type == FunctionBlock) {
3889 if (!ste->ste_unoptimized)
3890 flags |= CO_OPTIMIZED;
3891 if (ste->ste_nested)
3892 flags |= CO_NESTED;
3893 if (ste->ste_generator)
3894 flags |= CO_GENERATOR;
3895 if (ste->ste_varargs)
3896 flags |= CO_VARARGS;
3897 if (ste->ste_varkeywords)
3898 flags |= CO_VARKEYWORDS;
3899 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 /* (Only) inherit compilerflags in PyCF_MASK */
3902 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 n = PyDict_Size(c->u->u_freevars);
3905 if (n < 0)
3906 return -1;
3907 if (n == 0) {
3908 n = PyDict_Size(c->u->u_cellvars);
3909 if (n < 0)
3910 return -1;
3911 if (n == 0) {
3912 flags |= CO_NOFREE;
3913 }
3914 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003917}
3918
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003919static PyCodeObject *
3920makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 PyObject *tmp;
3923 PyCodeObject *co = NULL;
3924 PyObject *consts = NULL;
3925 PyObject *names = NULL;
3926 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 PyObject *name = NULL;
3928 PyObject *freevars = NULL;
3929 PyObject *cellvars = NULL;
3930 PyObject *bytecode = NULL;
3931 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 tmp = dict_keys_inorder(c->u->u_consts, 0);
3934 if (!tmp)
3935 goto error;
3936 consts = PySequence_List(tmp); /* optimize_code requires a list */
3937 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 names = dict_keys_inorder(c->u->u_names, 0);
3940 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3941 if (!consts || !names || !varnames)
3942 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3945 if (!cellvars)
3946 goto error;
3947 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3948 if (!freevars)
3949 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 nlocals = PyDict_Size(c->u->u_varnames);
3951 flags = compute_code_flags(c);
3952 if (flags < 0)
3953 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3956 if (!bytecode)
3957 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3960 if (!tmp)
3961 goto error;
3962 Py_DECREF(consts);
3963 consts = tmp;
3964
3965 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3966 nlocals, stackdepth(c), flags,
3967 bytecode, consts, names, varnames,
3968 freevars, cellvars,
Benjamin Peterson43b06862011-05-27 09:08:01 -05003969 c->c_filename_obj, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 c->u->u_firstlineno,
3971 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003972 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 Py_XDECREF(consts);
3974 Py_XDECREF(names);
3975 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 Py_XDECREF(name);
3977 Py_XDECREF(freevars);
3978 Py_XDECREF(cellvars);
3979 Py_XDECREF(bytecode);
3980 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003981}
3982
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003983
3984/* For debugging purposes only */
3985#if 0
3986static void
3987dump_instr(const struct instr *i)
3988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 const char *jrel = i->i_jrel ? "jrel " : "";
3990 const char *jabs = i->i_jabs ? "jabs " : "";
3991 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 *arg = '\0';
3994 if (i->i_hasarg)
3995 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
3998 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003999}
4000
4001static void
4002dump_basicblock(const basicblock *b)
4003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 const char *seen = b->b_seen ? "seen " : "";
4005 const char *b_return = b->b_return ? "return " : "";
4006 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4007 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4008 if (b->b_instr) {
4009 int i;
4010 for (i = 0; i < b->b_iused; i++) {
4011 fprintf(stderr, " [%02d] ", i);
4012 dump_instr(b->b_instr + i);
4013 }
4014 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004015}
4016#endif
4017
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004018static PyCodeObject *
4019assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 basicblock *b, *entryblock;
4022 struct assembler a;
4023 int i, j, nblocks;
4024 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 /* Make sure every block that falls off the end returns None.
4027 XXX NEXT_BLOCK() isn't quite right, because if the last
4028 block ends with a jump or return b_next shouldn't set.
4029 */
4030 if (!c->u->u_curblock->b_return) {
4031 NEXT_BLOCK(c);
4032 if (addNone)
4033 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4034 ADDOP(c, RETURN_VALUE);
4035 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 nblocks = 0;
4038 entryblock = NULL;
4039 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4040 nblocks++;
4041 entryblock = b;
4042 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 /* Set firstlineno if it wasn't explicitly set. */
4045 if (!c->u->u_firstlineno) {
4046 if (entryblock && entryblock->b_instr)
4047 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4048 else
4049 c->u->u_firstlineno = 1;
4050 }
4051 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4052 goto error;
4053 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004055 /* Can't modify the bytecode after computing jump offsets. */
4056 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 /* Emit code in reverse postorder from dfs. */
4059 for (i = a.a_nblocks - 1; i >= 0; i--) {
4060 b = a.a_postorder[i];
4061 for (j = 0; j < b->b_iused; j++)
4062 if (!assemble_emit(&a, &b->b_instr[j]))
4063 goto error;
4064 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4067 goto error;
4068 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4069 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004072 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 assemble_free(&a);
4074 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004075}
Georg Brandl8334fd92010-12-04 10:26:46 +00004076
4077#undef PyAST_Compile
4078PyAPI_FUNC(PyCodeObject *)
4079PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4080 PyArena *arena)
4081{
4082 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4083}
4084
4085