blob: 567b2302a9140d623e0585ed591005768a67617c [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
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500182static int compiler_with(struct compiler *, stmt_ty, int);
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);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500188static int compiler_try_except(struct compiler *, stmt_ty);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000189
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190static PyCodeObject *assemble(struct compiler *, int addNone);
191static PyObject *__doc__;
192
Benjamin Petersonb173f782009-05-05 22:31:58 +0000193#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
194
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 /* Name mangling: __private becomes _classname__private.
199 This is independent from how the name is used. */
200 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident);
201 Py_UNICODE *buffer;
202 size_t nlen, plen;
203 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
204 name == NULL || name[0] != '_' || name[1] != '_') {
205 Py_INCREF(ident);
206 return ident;
207 }
208 p = PyUnicode_AS_UNICODE(privateobj);
209 nlen = Py_UNICODE_strlen(name);
210 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 The only time a name with a dot can occur is when
213 we are compiling an import statement that has a
214 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 TODO(jhylton): Decide whether we want to support
217 mangling of the module name, e.g. __M.X.
218 */
219 if ((name[nlen-1] == '_' && name[nlen-2] == '_')
220 || Py_UNICODE_strchr(name, '.')) {
221 Py_INCREF(ident);
222 return ident; /* Don't mangle __whatever__ */
223 }
224 /* Strip leading underscores from class name */
225 while (*p == '_')
226 p++;
227 if (*p == 0) {
228 Py_INCREF(ident);
229 return ident; /* Don't mangle if class is just underscores */
230 }
231 plen = Py_UNICODE_strlen(p);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 assert(1 <= PY_SSIZE_T_MAX - nlen);
234 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen);
237 if (!ident)
238 return 0;
239 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
240 buffer = PyUnicode_AS_UNICODE(ident);
241 buffer[0] = '_';
242 Py_UNICODE_strncpy(buffer+1, p, plen);
243 Py_UNICODE_strcpy(buffer+1+plen, name);
244 return ident;
Michael W. Hudson60934622004-08-12 17:56:29 +0000245}
246
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247static int
248compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000249{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 c->c_stack = PyList_New(0);
253 if (!c->c_stack)
254 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000257}
258
259PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +0000260PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags,
261 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 struct compiler c;
264 PyCodeObject *co = NULL;
265 PyCompilerFlags local_flags;
266 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (!__doc__) {
269 __doc__ = PyUnicode_InternFromString("__doc__");
270 if (!__doc__)
271 return NULL;
272 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 if (!compiler_init(&c))
275 return NULL;
276 c.c_filename = filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500277 c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
278 if (!c.c_filename_obj)
279 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 c.c_arena = arena;
281 c.c_future = PyFuture_FromAST(mod, filename);
282 if (c.c_future == NULL)
283 goto finally;
284 if (!flags) {
285 local_flags.cf_flags = 0;
286 flags = &local_flags;
287 }
288 merged = c.c_future->ff_features | flags->cf_flags;
289 c.c_future->ff_features = merged;
290 flags->cf_flags = merged;
291 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000292 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 c.c_st = PySymtable_Build(mod, filename, c.c_future);
296 if (c.c_st == NULL) {
297 if (!PyErr_Occurred())
298 PyErr_SetString(PyExc_SystemError, "no symtable");
299 goto finally;
300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Thomas Wouters1175c432006-02-27 22:49:54 +0000304 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 compiler_free(&c);
306 assert(co || PyErr_Occurred());
307 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000308}
309
310PyCodeObject *
311PyNode_Compile(struct _node *n, const char *filename)
312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 PyCodeObject *co = NULL;
314 mod_ty mod;
315 PyArena *arena = PyArena_New();
316 if (!arena)
317 return NULL;
318 mod = PyAST_FromNode(n, NULL, filename, arena);
319 if (mod)
320 co = PyAST_Compile(mod, filename, NULL, arena);
321 PyArena_Free(arena);
322 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000323}
324
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000325static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (c->c_st)
329 PySymtable_Free(c->c_st);
330 if (c->c_future)
331 PyObject_Free(c->c_future);
Benjamin Peterson43b06862011-05-27 09:08:01 -0500332 if (c->c_filename_obj)
333 Py_DECREF(c->c_filename_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000335}
336
Guido van Rossum79f25d91997-04-29 20:08:16 +0000337static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000338list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 Py_ssize_t i, n;
341 PyObject *v, *k;
342 PyObject *dict = PyDict_New();
343 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 n = PyList_Size(list);
346 for (i = 0; i < n; i++) {
347 v = PyLong_FromLong(i);
348 if (!v) {
349 Py_DECREF(dict);
350 return NULL;
351 }
352 k = PyList_GET_ITEM(list, i);
353 k = PyTuple_Pack(2, k, k->ob_type);
354 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
355 Py_XDECREF(k);
356 Py_DECREF(v);
357 Py_DECREF(dict);
358 return NULL;
359 }
360 Py_DECREF(k);
361 Py_DECREF(v);
362 }
363 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364}
365
366/* Return new dict containing names from src that match scope(s).
367
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000368src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000370values are integers, starting at offset and increasing by one for
371each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000372*/
373
374static PyObject *
375dictbytype(PyObject *src, int scope_type, int flag, int offset)
376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_ssize_t pos = 0, i = offset, scope;
378 PyObject *k, *v, *dest = PyDict_New();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 assert(offset >= 0);
381 if (dest == NULL)
382 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 while (PyDict_Next(src, &pos, &k, &v)) {
385 /* XXX this should probably be a macro in symtable.h */
386 long vi;
387 assert(PyLong_Check(v));
388 vi = PyLong_AS_LONG(v);
389 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (scope == scope_type || vi & flag) {
392 PyObject *tuple, *item = PyLong_FromLong(i);
393 if (item == NULL) {
394 Py_DECREF(dest);
395 return NULL;
396 }
397 i++;
398 tuple = PyTuple_Pack(2, k, k->ob_type);
399 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
400 Py_DECREF(item);
401 Py_DECREF(dest);
402 Py_XDECREF(tuple);
403 return NULL;
404 }
405 Py_DECREF(item);
406 Py_DECREF(tuple);
407 }
408 }
409 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000410}
411
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412static void
413compiler_unit_check(struct compiler_unit *u)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 basicblock *block;
416 for (block = u->u_blocks; block != NULL; block = block->b_list) {
417 assert((void *)block != (void *)0xcbcbcbcb);
418 assert((void *)block != (void *)0xfbfbfbfb);
419 assert((void *)block != (void *)0xdbdbdbdb);
420 if (block->b_instr != NULL) {
421 assert(block->b_ialloc > 0);
422 assert(block->b_iused > 0);
423 assert(block->b_ialloc >= block->b_iused);
424 }
425 else {
426 assert (block->b_iused == 0);
427 assert (block->b_ialloc == 0);
428 }
429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430}
431
432static void
433compiler_unit_free(struct compiler_unit *u)
434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 compiler_unit_check(u);
438 b = u->u_blocks;
439 while (b != NULL) {
440 if (b->b_instr)
441 PyObject_Free((void *)b->b_instr);
442 next = b->b_list;
443 PyObject_Free((void *)b);
444 b = next;
445 }
446 Py_CLEAR(u->u_ste);
447 Py_CLEAR(u->u_name);
448 Py_CLEAR(u->u_consts);
449 Py_CLEAR(u->u_names);
450 Py_CLEAR(u->u_varnames);
451 Py_CLEAR(u->u_freevars);
452 Py_CLEAR(u->u_cellvars);
453 Py_CLEAR(u->u_private);
454 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000455}
456
457static int
458compiler_enter_scope(struct compiler *c, identifier name, void *key,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
464 struct compiler_unit));
465 if (!u) {
466 PyErr_NoMemory();
467 return 0;
468 }
469 memset(u, 0, sizeof(struct compiler_unit));
470 u->u_argcount = 0;
471 u->u_kwonlyargcount = 0;
472 u->u_ste = PySymtable_Lookup(c->c_st, key);
473 if (!u->u_ste) {
474 compiler_unit_free(u);
475 return 0;
476 }
477 Py_INCREF(name);
478 u->u_name = name;
479 u->u_varnames = list2dict(u->u_ste->ste_varnames);
480 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
481 if (!u->u_varnames || !u->u_cellvars) {
482 compiler_unit_free(u);
483 return 0;
484 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
487 PyDict_Size(u->u_cellvars));
488 if (!u->u_freevars) {
489 compiler_unit_free(u);
490 return 0;
491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 u->u_blocks = NULL;
494 u->u_nfblocks = 0;
495 u->u_firstlineno = lineno;
496 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000497 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 u->u_lineno_set = 0;
499 u->u_consts = PyDict_New();
500 if (!u->u_consts) {
501 compiler_unit_free(u);
502 return 0;
503 }
504 u->u_names = PyDict_New();
505 if (!u->u_names) {
506 compiler_unit_free(u);
507 return 0;
508 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 /* Push the old compiler_unit on the stack. */
513 if (c->u) {
514 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
515 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
516 Py_XDECREF(capsule);
517 compiler_unit_free(u);
518 return 0;
519 }
520 Py_DECREF(capsule);
521 u->u_private = c->u->u_private;
522 Py_XINCREF(u->u_private);
523 }
524 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 c->c_nestlevel++;
527 if (compiler_use_new_block(c) == NULL)
528 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000531}
532
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000533static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000534compiler_exit_scope(struct compiler *c)
535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 int n;
537 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 c->c_nestlevel--;
540 compiler_unit_free(c->u);
541 /* Restore c->u to the parent unit. */
542 n = PyList_GET_SIZE(c->c_stack) - 1;
543 if (n >= 0) {
544 capsule = PyList_GET_ITEM(c->c_stack, n);
545 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
546 assert(c->u);
547 /* we are deleting from a list so this really shouldn't fail */
548 if (PySequence_DelItem(c->c_stack, n) < 0)
549 Py_FatalError("compiler_exit_scope()");
550 compiler_unit_check(c->u);
551 }
552 else
553 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000554
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000555}
556
557/* Allocate a new block and return a pointer to it.
558 Returns NULL on error.
559*/
560
561static basicblock *
562compiler_new_block(struct compiler *c)
563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 basicblock *b;
565 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 u = c->u;
568 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
569 if (b == NULL) {
570 PyErr_NoMemory();
571 return NULL;
572 }
573 memset((void *)b, 0, sizeof(basicblock));
574 /* Extend the singly linked list of blocks with new block. */
575 b->b_list = u->u_blocks;
576 u->u_blocks = b;
577 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578}
579
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000580static basicblock *
581compiler_use_new_block(struct compiler *c)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 basicblock *block = compiler_new_block(c);
584 if (block == NULL)
585 return NULL;
586 c->u->u_curblock = block;
587 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588}
589
590static basicblock *
591compiler_next_block(struct compiler *c)
592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 basicblock *block = compiler_new_block(c);
594 if (block == NULL)
595 return NULL;
596 c->u->u_curblock->b_next = block;
597 c->u->u_curblock = block;
598 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599}
600
601static basicblock *
602compiler_use_next_block(struct compiler *c, basicblock *block)
603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 assert(block != NULL);
605 c->u->u_curblock->b_next = block;
606 c->u->u_curblock = block;
607 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608}
609
610/* Returns the offset of the next instruction in the current block's
611 b_instr array. Resizes the b_instr as necessary.
612 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000613*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614
615static int
616compiler_next_instr(struct compiler *c, basicblock *b)
617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 assert(b != NULL);
619 if (b->b_instr == NULL) {
620 b->b_instr = (struct instr *)PyObject_Malloc(
621 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
622 if (b->b_instr == NULL) {
623 PyErr_NoMemory();
624 return -1;
625 }
626 b->b_ialloc = DEFAULT_BLOCK_SIZE;
627 memset((char *)b->b_instr, 0,
628 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
629 }
630 else if (b->b_iused == b->b_ialloc) {
631 struct instr *tmp;
632 size_t oldsize, newsize;
633 oldsize = b->b_ialloc * sizeof(struct instr);
634 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 if (oldsize > (PY_SIZE_MAX >> 1)) {
637 PyErr_NoMemory();
638 return -1;
639 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (newsize == 0) {
642 PyErr_NoMemory();
643 return -1;
644 }
645 b->b_ialloc <<= 1;
646 tmp = (struct instr *)PyObject_Realloc(
647 (void *)b->b_instr, newsize);
648 if (tmp == NULL) {
649 PyErr_NoMemory();
650 return -1;
651 }
652 b->b_instr = tmp;
653 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
654 }
655 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656}
657
Christian Heimes2202f872008-02-06 14:31:34 +0000658/* Set the i_lineno member of the instruction at offset off if the
659 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000660 already been set. If it has been set, the call has no effect.
661
Christian Heimes2202f872008-02-06 14:31:34 +0000662 The line number is reset in the following cases:
663 - when entering a new scope
664 - on each statement
665 - on each expression that start a new line
666 - before the "except" clause
667 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000668*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000670static void
671compiler_set_lineno(struct compiler *c, int off)
672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 basicblock *b;
674 if (c->u->u_lineno_set)
675 return;
676 c->u->u_lineno_set = 1;
677 b = c->u->u_curblock;
678 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679}
680
681static int
682opcode_stack_effect(int opcode, int oparg)
683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 switch (opcode) {
685 case POP_TOP:
686 return -1;
687 case ROT_TWO:
688 case ROT_THREE:
689 return 0;
690 case DUP_TOP:
691 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000692 case DUP_TOP_TWO:
693 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 case UNARY_POSITIVE:
696 case UNARY_NEGATIVE:
697 case UNARY_NOT:
698 case UNARY_INVERT:
699 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 case SET_ADD:
702 case LIST_APPEND:
703 return -1;
704 case MAP_ADD:
705 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 case BINARY_POWER:
708 case BINARY_MULTIPLY:
709 case BINARY_MODULO:
710 case BINARY_ADD:
711 case BINARY_SUBTRACT:
712 case BINARY_SUBSCR:
713 case BINARY_FLOOR_DIVIDE:
714 case BINARY_TRUE_DIVIDE:
715 return -1;
716 case INPLACE_FLOOR_DIVIDE:
717 case INPLACE_TRUE_DIVIDE:
718 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 case INPLACE_ADD:
721 case INPLACE_SUBTRACT:
722 case INPLACE_MULTIPLY:
723 case INPLACE_MODULO:
724 return -1;
725 case STORE_SUBSCR:
726 return -3;
727 case STORE_MAP:
728 return -2;
729 case DELETE_SUBSCR:
730 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 case BINARY_LSHIFT:
733 case BINARY_RSHIFT:
734 case BINARY_AND:
735 case BINARY_XOR:
736 case BINARY_OR:
737 return -1;
738 case INPLACE_POWER:
739 return -1;
740 case GET_ITER:
741 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 case PRINT_EXPR:
744 return -1;
745 case LOAD_BUILD_CLASS:
746 return 1;
747 case INPLACE_LSHIFT:
748 case INPLACE_RSHIFT:
749 case INPLACE_AND:
750 case INPLACE_XOR:
751 case INPLACE_OR:
752 return -1;
753 case BREAK_LOOP:
754 return 0;
755 case SETUP_WITH:
756 return 7;
757 case WITH_CLEANUP:
758 return -1; /* XXX Sometimes more */
759 case STORE_LOCALS:
760 return -1;
761 case RETURN_VALUE:
762 return -1;
763 case IMPORT_STAR:
764 return -1;
765 case YIELD_VALUE:
766 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 case POP_BLOCK:
769 return 0;
770 case POP_EXCEPT:
771 return 0; /* -3 except if bad bytecode */
772 case END_FINALLY:
773 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 case STORE_NAME:
776 return -1;
777 case DELETE_NAME:
778 return 0;
779 case UNPACK_SEQUENCE:
780 return oparg-1;
781 case UNPACK_EX:
782 return (oparg&0xFF) + (oparg>>8);
783 case FOR_ITER:
784 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 case STORE_ATTR:
787 return -2;
788 case DELETE_ATTR:
789 return -1;
790 case STORE_GLOBAL:
791 return -1;
792 case DELETE_GLOBAL:
793 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 case LOAD_CONST:
795 return 1;
796 case LOAD_NAME:
797 return 1;
798 case BUILD_TUPLE:
799 case BUILD_LIST:
800 case BUILD_SET:
801 return 1-oparg;
802 case BUILD_MAP:
803 return 1;
804 case LOAD_ATTR:
805 return 0;
806 case COMPARE_OP:
807 return -1;
808 case IMPORT_NAME:
809 return -1;
810 case IMPORT_FROM:
811 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 case JUMP_FORWARD:
814 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
815 case JUMP_IF_FALSE_OR_POP: /* "" */
816 case JUMP_ABSOLUTE:
817 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 case POP_JUMP_IF_FALSE:
820 case POP_JUMP_IF_TRUE:
821 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 case LOAD_GLOBAL:
824 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 case CONTINUE_LOOP:
827 return 0;
828 case SETUP_LOOP:
829 return 0;
830 case SETUP_EXCEPT:
831 case SETUP_FINALLY:
832 return 6; /* can push 3 values for the new exception
833 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 case LOAD_FAST:
836 return 1;
837 case STORE_FAST:
838 return -1;
839 case DELETE_FAST:
840 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 case RAISE_VARARGS:
843 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000844#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 case CALL_FUNCTION:
846 return -NARGS(oparg);
847 case CALL_FUNCTION_VAR:
848 case CALL_FUNCTION_KW:
849 return -NARGS(oparg)-1;
850 case CALL_FUNCTION_VAR_KW:
851 return -NARGS(oparg)-2;
852 case MAKE_FUNCTION:
853 return -NARGS(oparg) - ((oparg >> 16) & 0xffff);
854 case MAKE_CLOSURE:
855 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000856#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 case BUILD_SLICE:
858 if (oparg == 3)
859 return -2;
860 else
861 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 case LOAD_CLOSURE:
864 return 1;
865 case LOAD_DEREF:
866 return 1;
867 case STORE_DEREF:
868 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000869 case DELETE_DEREF:
870 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 default:
872 fprintf(stderr, "opcode = %d\n", opcode);
873 Py_FatalError("opcode_stack_effect()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 }
876 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877}
878
879/* Add an opcode with no argument.
880 Returns 0 on failure, 1 on success.
881*/
882
883static int
884compiler_addop(struct compiler *c, int opcode)
885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 basicblock *b;
887 struct instr *i;
888 int off;
889 off = compiler_next_instr(c, c->u->u_curblock);
890 if (off < 0)
891 return 0;
892 b = c->u->u_curblock;
893 i = &b->b_instr[off];
894 i->i_opcode = opcode;
895 i->i_hasarg = 0;
896 if (opcode == RETURN_VALUE)
897 b->b_return = 1;
898 compiler_set_lineno(c, off);
899 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000900}
901
902static int
903compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 PyObject *t, *v;
906 Py_ssize_t arg;
907 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 /* necessary to make sure types aren't coerced (e.g., int and long) */
910 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
911 if (PyFloat_Check(o)) {
912 d = PyFloat_AS_DOUBLE(o);
913 /* all we need is to make the tuple different in either the 0.0
914 * or -0.0 case from all others, just to avoid the "coercion".
915 */
916 if (d == 0.0 && copysign(1.0, d) < 0.0)
917 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
918 else
919 t = PyTuple_Pack(2, o, o->ob_type);
920 }
921 else if (PyComplex_Check(o)) {
922 Py_complex z;
923 int real_negzero, imag_negzero;
924 /* For the complex case we must make complex(x, 0.)
925 different from complex(x, -0.) and complex(0., y)
926 different from complex(-0., y), for any x and y.
927 All four complex zeros must be distinguished.*/
928 z = PyComplex_AsCComplex(o);
929 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
930 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
931 if (real_negzero && imag_negzero) {
932 t = PyTuple_Pack(5, o, o->ob_type,
933 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +0000934 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 else if (imag_negzero) {
936 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +0000937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 else if (real_negzero) {
939 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
940 }
941 else {
942 t = PyTuple_Pack(2, o, o->ob_type);
943 }
944 }
945 else {
946 t = PyTuple_Pack(2, o, o->ob_type);
947 }
948 if (t == NULL)
949 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 v = PyDict_GetItem(dict, t);
952 if (!v) {
953 if (PyErr_Occurred())
954 return -1;
955 arg = PyDict_Size(dict);
956 v = PyLong_FromLong(arg);
957 if (!v) {
958 Py_DECREF(t);
959 return -1;
960 }
961 if (PyDict_SetItem(dict, t, v) < 0) {
962 Py_DECREF(t);
963 Py_DECREF(v);
964 return -1;
965 }
966 Py_DECREF(v);
967 }
968 else
969 arg = PyLong_AsLong(v);
970 Py_DECREF(t);
971 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972}
973
974static int
975compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000977{
978 int arg = compiler_add_o(c, dict, o);
979 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000980 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981 return compiler_addop_i(c, opcode, arg);
982}
983
984static int
985compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000987{
988 int arg;
989 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
990 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000991 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992 arg = compiler_add_o(c, dict, mangled);
993 Py_DECREF(mangled);
994 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +0000995 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000996 return compiler_addop_i(c, opcode, arg);
997}
998
999/* Add an opcode with an integer argument.
1000 Returns 0 on failure, 1 on success.
1001*/
1002
1003static int
1004compiler_addop_i(struct compiler *c, int opcode, int oparg)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 struct instr *i;
1007 int off;
1008 off = compiler_next_instr(c, c->u->u_curblock);
1009 if (off < 0)
1010 return 0;
1011 i = &c->u->u_curblock->b_instr[off];
1012 i->i_opcode = opcode;
1013 i->i_oparg = oparg;
1014 i->i_hasarg = 1;
1015 compiler_set_lineno(c, off);
1016 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017}
1018
1019static int
1020compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 struct instr *i;
1023 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 assert(b != NULL);
1026 off = compiler_next_instr(c, c->u->u_curblock);
1027 if (off < 0)
1028 return 0;
1029 i = &c->u->u_curblock->b_instr[off];
1030 i->i_opcode = opcode;
1031 i->i_target = b;
1032 i->i_hasarg = 1;
1033 if (absolute)
1034 i->i_jabs = 1;
1035 else
1036 i->i_jrel = 1;
1037 compiler_set_lineno(c, off);
1038 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039}
1040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1042 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001043 it as the current block. NEXT_BLOCK() also creates an implicit jump
1044 from the current block to the new block.
1045*/
1046
Thomas Wouters89f507f2006-12-13 04:49:30 +00001047/* The returns inside these macros make it impossible to decref objects
1048 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049*/
1050
1051
1052#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 if (compiler_use_new_block((C)) == NULL) \
1054 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055}
1056
1057#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 if (compiler_next_block((C)) == NULL) \
1059 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001060}
1061
1062#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (!compiler_addop((C), (OP))) \
1064 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065}
1066
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001067#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 if (!compiler_addop((C), (OP))) { \
1069 compiler_exit_scope(c); \
1070 return 0; \
1071 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001072}
1073
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1076 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001077}
1078
1079#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1081 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082}
1083
1084#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 if (!compiler_addop_i((C), (OP), (O))) \
1086 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087}
1088
1089#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 if (!compiler_addop_j((C), (OP), (O), 1)) \
1091 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092}
1093
1094#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 if (!compiler_addop_j((C), (OP), (O), 0)) \
1096 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097}
1098
1099/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1100 the ASDL name to synthesize the name of the C type and the visit function.
1101*/
1102
1103#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (!compiler_visit_ ## TYPE((C), (V))) \
1105 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106}
1107
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001108#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (!compiler_visit_ ## TYPE((C), (V))) { \
1110 compiler_exit_scope(c); \
1111 return 0; \
1112 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001113}
1114
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001115#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (!compiler_visit_slice((C), (V), (CTX))) \
1117 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118}
1119
1120#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 int _i; \
1122 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1123 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1124 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1125 if (!compiler_visit_ ## TYPE((C), elt)) \
1126 return 0; \
1127 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128}
1129
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001130#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 int _i; \
1132 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1133 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1134 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1135 if (!compiler_visit_ ## TYPE((C), elt)) { \
1136 compiler_exit_scope(c); \
1137 return 0; \
1138 } \
1139 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001140}
1141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142static int
1143compiler_isdocstring(stmt_ty s)
1144{
1145 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001146 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 return s->v.Expr.value->kind == Str_kind;
1148}
1149
1150/* Compile a sequence of statements, checking for a docstring. */
1151
1152static int
1153compiler_body(struct compiler *c, asdl_seq *stmts)
1154{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 int i = 0;
1156 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (!asdl_seq_LEN(stmts))
1159 return 1;
1160 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001161 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 /* don't generate docstrings if -OO */
1163 i = 1;
1164 VISIT(c, expr, st->v.Expr.value);
1165 if (!compiler_nameop(c, __doc__, Store))
1166 return 0;
1167 }
1168 for (; i < asdl_seq_LEN(stmts); i++)
1169 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1170 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
1173static PyCodeObject *
1174compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 PyCodeObject *co;
1177 int addNone = 1;
1178 static PyObject *module;
1179 if (!module) {
1180 module = PyUnicode_InternFromString("<module>");
1181 if (!module)
1182 return NULL;
1183 }
1184 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1185 if (!compiler_enter_scope(c, module, mod, 0))
1186 return NULL;
1187 switch (mod->kind) {
1188 case Module_kind:
1189 if (!compiler_body(c, mod->v.Module.body)) {
1190 compiler_exit_scope(c);
1191 return 0;
1192 }
1193 break;
1194 case Interactive_kind:
1195 c->c_interactive = 1;
1196 VISIT_SEQ_IN_SCOPE(c, stmt,
1197 mod->v.Interactive.body);
1198 break;
1199 case Expression_kind:
1200 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1201 addNone = 0;
1202 break;
1203 case Suite_kind:
1204 PyErr_SetString(PyExc_SystemError,
1205 "suite should not be possible");
1206 return 0;
1207 default:
1208 PyErr_Format(PyExc_SystemError,
1209 "module kind %d should not be possible",
1210 mod->kind);
1211 return 0;
1212 }
1213 co = assemble(c, addNone);
1214 compiler_exit_scope(c);
1215 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001216}
1217
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218/* The test for LOCAL must come before the test for FREE in order to
1219 handle classes where name is both local and free. The local var is
1220 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001221*/
1222
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223static int
1224get_ref_type(struct compiler *c, PyObject *name)
1225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 int scope = PyST_GetScope(c->u->u_ste, name);
1227 if (scope == 0) {
1228 char buf[350];
1229 PyOS_snprintf(buf, sizeof(buf),
1230 "unknown scope for %.100s in %.100s(%s) in %s\n"
1231 "symbols: %s\nlocals: %s\nglobals: %s",
1232 PyBytes_AS_STRING(name),
1233 PyBytes_AS_STRING(c->u->u_name),
1234 PyObject_REPR(c->u->u_ste->ste_id),
1235 c->c_filename,
1236 PyObject_REPR(c->u->u_ste->ste_symbols),
1237 PyObject_REPR(c->u->u_varnames),
1238 PyObject_REPR(c->u->u_names)
1239 );
1240 Py_FatalError(buf);
1241 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001244}
1245
1246static int
1247compiler_lookup_arg(PyObject *dict, PyObject *name)
1248{
1249 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001250 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001252 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001253 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001254 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001256 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001257 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001258}
1259
1260static int
1261compiler_make_closure(struct compiler *c, PyCodeObject *co, int args)
1262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 int i, free = PyCode_GetNumFree(co);
1264 if (free == 0) {
1265 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1266 ADDOP_I(c, MAKE_FUNCTION, args);
1267 return 1;
1268 }
1269 for (i = 0; i < free; ++i) {
1270 /* Bypass com_addop_varname because it will generate
1271 LOAD_DEREF but LOAD_CLOSURE is needed.
1272 */
1273 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1274 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 /* Special case: If a class contains a method with a
1277 free variable that has the same name as a method,
1278 the name will be considered free *and* local in the
1279 class. It should be handled by the closure, as
1280 well as by the normal name loookup logic.
1281 */
1282 reftype = get_ref_type(c, name);
1283 if (reftype == CELL)
1284 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1285 else /* (reftype == FREE) */
1286 arg = compiler_lookup_arg(c->u->u_freevars, name);
1287 if (arg == -1) {
1288 fprintf(stderr,
1289 "lookup %s in %s %d %d\n"
1290 "freevars of %s: %s\n",
1291 PyObject_REPR(name),
1292 PyBytes_AS_STRING(c->u->u_name),
1293 reftype, arg,
1294 _PyUnicode_AsString(co->co_name),
1295 PyObject_REPR(co->co_freevars));
1296 Py_FatalError("compiler_make_closure()");
1297 }
1298 ADDOP_I(c, LOAD_CLOSURE, arg);
1299 }
1300 ADDOP_I(c, BUILD_TUPLE, free);
1301 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1302 ADDOP_I(c, MAKE_CLOSURE, args);
1303 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001304}
1305
1306static int
1307compiler_decorators(struct compiler *c, asdl_seq* decos)
1308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (!decos)
1312 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1315 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1316 }
1317 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001318}
1319
1320static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001321compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 int i, default_count = 0;
1325 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1326 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1327 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1328 if (default_) {
1329 ADDOP_O(c, LOAD_CONST, arg->arg, consts);
1330 if (!compiler_visit_expr(c, default_)) {
1331 return -1;
1332 }
1333 default_count++;
1334 }
1335 }
1336 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001337}
1338
1339static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001340compiler_visit_argannotation(struct compiler *c, identifier id,
1341 expr_ty annotation, PyObject *names)
1342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (annotation) {
1344 VISIT(c, expr, annotation);
1345 if (PyList_Append(names, id))
1346 return -1;
1347 }
1348 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001349}
1350
1351static int
1352compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1353 PyObject *names)
1354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 int i, error;
1356 for (i = 0; i < asdl_seq_LEN(args); i++) {
1357 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1358 error = compiler_visit_argannotation(
1359 c,
1360 arg->arg,
1361 arg->annotation,
1362 names);
1363 if (error)
1364 return error;
1365 }
1366 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001367}
1368
1369static int
1370compiler_visit_annotations(struct compiler *c, arguments_ty args,
1371 expr_ty returns)
1372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 /* Push arg annotations and a list of the argument names. Return the #
1374 of items pushed. The expressions are evaluated out-of-order wrt the
1375 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1378 */
1379 static identifier return_str;
1380 PyObject *names;
1381 int len;
1382 names = PyList_New(0);
1383 if (!names)
1384 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (compiler_visit_argannotations(c, args->args, names))
1387 goto error;
1388 if (args->varargannotation &&
1389 compiler_visit_argannotation(c, args->vararg,
1390 args->varargannotation, names))
1391 goto error;
1392 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1393 goto error;
1394 if (args->kwargannotation &&
1395 compiler_visit_argannotation(c, args->kwarg,
1396 args->kwargannotation, names))
1397 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 if (!return_str) {
1400 return_str = PyUnicode_InternFromString("return");
1401 if (!return_str)
1402 goto error;
1403 }
1404 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1405 goto error;
1406 }
1407
1408 len = PyList_GET_SIZE(names);
1409 if (len > 65534) {
1410 /* len must fit in 16 bits, and len is incremented below */
1411 PyErr_SetString(PyExc_SyntaxError,
1412 "too many annotations");
1413 goto error;
1414 }
1415 if (len) {
1416 /* convert names to a tuple and place on stack */
1417 PyObject *elt;
1418 int i;
1419 PyObject *s = PyTuple_New(len);
1420 if (!s)
1421 goto error;
1422 for (i = 0; i < len; i++) {
1423 elt = PyList_GET_ITEM(names, i);
1424 Py_INCREF(elt);
1425 PyTuple_SET_ITEM(s, i, elt);
1426 }
1427 ADDOP_O(c, LOAD_CONST, s, consts);
1428 Py_DECREF(s);
1429 len++; /* include the just-pushed tuple */
1430 }
1431 Py_DECREF(names);
1432 return len;
Neal Norwitzc1505362006-12-28 06:47:50 +00001433
1434error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 Py_DECREF(names);
1436 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001437}
1438
1439static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001440compiler_function(struct compiler *c, stmt_ty s)
1441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 PyCodeObject *co;
1443 PyObject *first_const = Py_None;
1444 arguments_ty args = s->v.FunctionDef.args;
1445 expr_ty returns = s->v.FunctionDef.returns;
1446 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1447 stmt_ty st;
1448 int i, n, docstring, kw_default_count = 0, arglength;
1449 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 if (!compiler_decorators(c, decos))
1454 return 0;
1455 if (args->kwonlyargs) {
1456 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1457 args->kw_defaults);
1458 if (res < 0)
1459 return 0;
1460 kw_default_count = res;
1461 }
1462 if (args->defaults)
1463 VISIT_SEQ(c, expr, args->defaults);
1464 num_annotations = compiler_visit_annotations(c, args, returns);
1465 if (num_annotations < 0)
1466 return 0;
1467 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s,
1470 s->lineno))
1471 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1474 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001475 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 first_const = st->v.Expr.value->v.Str.s;
1477 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1478 compiler_exit_scope(c);
1479 return 0;
1480 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 c->u->u_argcount = asdl_seq_LEN(args->args);
1483 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1484 n = asdl_seq_LEN(s->v.FunctionDef.body);
1485 /* if there was a docstring, we need to skip the first statement */
1486 for (i = docstring; i < n; i++) {
1487 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1488 VISIT_IN_SCOPE(c, stmt, st);
1489 }
1490 co = assemble(c, 1);
1491 compiler_exit_scope(c);
1492 if (co == NULL)
1493 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 arglength = asdl_seq_LEN(args->defaults);
1496 arglength |= kw_default_count << 8;
1497 arglength |= num_annotations << 16;
1498 compiler_make_closure(c, co, arglength);
1499 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 /* decorators */
1502 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1503 ADDOP_I(c, CALL_FUNCTION, 1);
1504 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001507}
1508
1509static int
1510compiler_class(struct compiler *c, stmt_ty s)
1511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 PyCodeObject *co;
1513 PyObject *str;
1514 int i;
1515 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (!compiler_decorators(c, decos))
1518 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 /* ultimately generate code for:
1521 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1522 where:
1523 <func> is a function/closure created from the class body;
1524 it has a single argument (__locals__) where the dict
1525 (or MutableSequence) representing the locals is passed
1526 <name> is the class name
1527 <bases> is the positional arguments and *varargs argument
1528 <keywords> is the keyword arguments and **kwds argument
1529 This borrows from compiler_call.
1530 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 /* 1. compile the class body into a code object */
1533 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno))
1534 return 0;
1535 /* this block represents what we do in the new scope */
1536 {
1537 /* use the class name for name mangling */
1538 Py_INCREF(s->v.ClassDef.name);
1539 Py_XDECREF(c->u->u_private);
1540 c->u->u_private = s->v.ClassDef.name;
1541 /* force it to have one mandatory argument */
1542 c->u->u_argcount = 1;
1543 /* load the first argument (__locals__) ... */
1544 ADDOP_I(c, LOAD_FAST, 0);
1545 /* ... and store it into f_locals */
1546 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1547 /* load (global) __name__ ... */
1548 str = PyUnicode_InternFromString("__name__");
1549 if (!str || !compiler_nameop(c, str, Load)) {
1550 Py_XDECREF(str);
1551 compiler_exit_scope(c);
1552 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001553 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 Py_DECREF(str);
1555 /* ... and store it as __module__ */
1556 str = PyUnicode_InternFromString("__module__");
1557 if (!str || !compiler_nameop(c, str, Store)) {
1558 Py_XDECREF(str);
1559 compiler_exit_scope(c);
1560 return 0;
1561 }
1562 Py_DECREF(str);
1563 /* compile the body proper */
1564 if (!compiler_body(c, s->v.ClassDef.body)) {
1565 compiler_exit_scope(c);
1566 return 0;
1567 }
1568 /* return the (empty) __class__ cell */
Benjamin Petersonf5ff2232011-06-19 19:42:22 -05001569 str = PyUnicode_InternFromString("@__class__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (str == NULL) {
1571 compiler_exit_scope(c);
1572 return 0;
1573 }
1574 i = compiler_lookup_arg(c->u->u_cellvars, str);
1575 Py_DECREF(str);
1576 if (i == -1) {
1577 /* This happens when nobody references the cell */
1578 PyErr_Clear();
1579 /* Return None */
1580 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1581 }
1582 else {
1583 /* Return the cell where to store __class__ */
1584 ADDOP_I(c, LOAD_CLOSURE, i);
1585 }
1586 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1587 /* create the code object */
1588 co = assemble(c, 1);
1589 }
1590 /* leave the new scope */
1591 compiler_exit_scope(c);
1592 if (co == NULL)
1593 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 /* 2. load the 'build_class' function */
1596 ADDOP(c, LOAD_BUILD_CLASS);
1597
1598 /* 3. load a function (or closure) made from the code object */
1599 compiler_make_closure(c, co, 0);
1600 Py_DECREF(co);
1601
1602 /* 4. load class name */
1603 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1604
1605 /* 5. generate the rest of the code for the call */
1606 if (!compiler_call_helper(c, 2,
1607 s->v.ClassDef.bases,
1608 s->v.ClassDef.keywords,
1609 s->v.ClassDef.starargs,
1610 s->v.ClassDef.kwargs))
1611 return 0;
1612
1613 /* 6. apply decorators */
1614 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1615 ADDOP_I(c, CALL_FUNCTION, 1);
1616 }
1617
1618 /* 7. store into <name> */
1619 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1620 return 0;
1621 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001622}
1623
1624static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001625compiler_ifexp(struct compiler *c, expr_ty e)
1626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 basicblock *end, *next;
1628
1629 assert(e->kind == IfExp_kind);
1630 end = compiler_new_block(c);
1631 if (end == NULL)
1632 return 0;
1633 next = compiler_new_block(c);
1634 if (next == NULL)
1635 return 0;
1636 VISIT(c, expr, e->v.IfExp.test);
1637 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1638 VISIT(c, expr, e->v.IfExp.body);
1639 ADDOP_JREL(c, JUMP_FORWARD, end);
1640 compiler_use_next_block(c, next);
1641 VISIT(c, expr, e->v.IfExp.orelse);
1642 compiler_use_next_block(c, end);
1643 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001644}
1645
1646static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001647compiler_lambda(struct compiler *c, expr_ty e)
1648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 PyCodeObject *co;
1650 static identifier name;
1651 int kw_default_count = 0, arglength;
1652 arguments_ty args = e->v.Lambda.args;
1653 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 if (!name) {
1656 name = PyUnicode_InternFromString("<lambda>");
1657 if (!name)
1658 return 0;
1659 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 if (args->kwonlyargs) {
1662 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1663 args->kw_defaults);
1664 if (res < 0) return 0;
1665 kw_default_count = res;
1666 }
1667 if (args->defaults)
1668 VISIT_SEQ(c, expr, args->defaults);
1669 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1670 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 /* Make None the first constant, so the lambda can't have a
1673 docstring. */
1674 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1675 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 c->u->u_argcount = asdl_seq_LEN(args->args);
1678 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1679 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1680 if (c->u->u_ste->ste_generator) {
1681 ADDOP_IN_SCOPE(c, POP_TOP);
1682 }
1683 else {
1684 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1685 }
1686 co = assemble(c, 1);
1687 compiler_exit_scope(c);
1688 if (co == NULL)
1689 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 arglength = asdl_seq_LEN(args->defaults);
1692 arglength |= kw_default_count << 8;
1693 compiler_make_closure(c, co, arglength);
1694 Py_DECREF(co);
1695
1696 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697}
1698
1699static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700compiler_if(struct compiler *c, stmt_ty s)
1701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 basicblock *end, *next;
1703 int constant;
1704 assert(s->kind == If_kind);
1705 end = compiler_new_block(c);
1706 if (end == NULL)
1707 return 0;
1708
Georg Brandl8334fd92010-12-04 10:26:46 +00001709 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 /* constant = 0: "if 0"
1711 * constant = 1: "if 1", "if 2", ...
1712 * constant = -1: rest */
1713 if (constant == 0) {
1714 if (s->v.If.orelse)
1715 VISIT_SEQ(c, stmt, s->v.If.orelse);
1716 } else if (constant == 1) {
1717 VISIT_SEQ(c, stmt, s->v.If.body);
1718 } else {
1719 if (s->v.If.orelse) {
1720 next = compiler_new_block(c);
1721 if (next == NULL)
1722 return 0;
1723 }
1724 else
1725 next = end;
1726 VISIT(c, expr, s->v.If.test);
1727 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1728 VISIT_SEQ(c, stmt, s->v.If.body);
1729 ADDOP_JREL(c, JUMP_FORWARD, end);
1730 if (s->v.If.orelse) {
1731 compiler_use_next_block(c, next);
1732 VISIT_SEQ(c, stmt, s->v.If.orelse);
1733 }
1734 }
1735 compiler_use_next_block(c, end);
1736 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001737}
1738
1739static int
1740compiler_for(struct compiler *c, stmt_ty s)
1741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 start = compiler_new_block(c);
1745 cleanup = compiler_new_block(c);
1746 end = compiler_new_block(c);
1747 if (start == NULL || end == NULL || cleanup == NULL)
1748 return 0;
1749 ADDOP_JREL(c, SETUP_LOOP, end);
1750 if (!compiler_push_fblock(c, LOOP, start))
1751 return 0;
1752 VISIT(c, expr, s->v.For.iter);
1753 ADDOP(c, GET_ITER);
1754 compiler_use_next_block(c, start);
1755 ADDOP_JREL(c, FOR_ITER, cleanup);
1756 VISIT(c, expr, s->v.For.target);
1757 VISIT_SEQ(c, stmt, s->v.For.body);
1758 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1759 compiler_use_next_block(c, cleanup);
1760 ADDOP(c, POP_BLOCK);
1761 compiler_pop_fblock(c, LOOP, start);
1762 VISIT_SEQ(c, stmt, s->v.For.orelse);
1763 compiler_use_next_block(c, end);
1764 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765}
1766
1767static int
1768compiler_while(struct compiler *c, stmt_ty s)
1769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00001771 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 if (constant == 0) {
1774 if (s->v.While.orelse)
1775 VISIT_SEQ(c, stmt, s->v.While.orelse);
1776 return 1;
1777 }
1778 loop = compiler_new_block(c);
1779 end = compiler_new_block(c);
1780 if (constant == -1) {
1781 anchor = compiler_new_block(c);
1782 if (anchor == NULL)
1783 return 0;
1784 }
1785 if (loop == NULL || end == NULL)
1786 return 0;
1787 if (s->v.While.orelse) {
1788 orelse = compiler_new_block(c);
1789 if (orelse == NULL)
1790 return 0;
1791 }
1792 else
1793 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 ADDOP_JREL(c, SETUP_LOOP, end);
1796 compiler_use_next_block(c, loop);
1797 if (!compiler_push_fblock(c, LOOP, loop))
1798 return 0;
1799 if (constant == -1) {
1800 VISIT(c, expr, s->v.While.test);
1801 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1802 }
1803 VISIT_SEQ(c, stmt, s->v.While.body);
1804 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 /* XXX should the two POP instructions be in a separate block
1807 if there is no else clause ?
1808 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 if (constant == -1) {
1811 compiler_use_next_block(c, anchor);
1812 ADDOP(c, POP_BLOCK);
1813 }
1814 compiler_pop_fblock(c, LOOP, loop);
1815 if (orelse != NULL) /* what if orelse is just pass? */
1816 VISIT_SEQ(c, stmt, s->v.While.orelse);
1817 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820}
1821
1822static int
1823compiler_continue(struct compiler *c)
1824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1826 static const char IN_FINALLY_ERROR_MSG[] =
1827 "'continue' not supported inside 'finally' clause";
1828 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 if (!c->u->u_nfblocks)
1831 return compiler_error(c, LOOP_ERROR_MSG);
1832 i = c->u->u_nfblocks - 1;
1833 switch (c->u->u_fblock[i].fb_type) {
1834 case LOOP:
1835 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1836 break;
1837 case EXCEPT:
1838 case FINALLY_TRY:
1839 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1840 /* Prevent continue anywhere under a finally
1841 even if hidden in a sub-try or except. */
1842 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1843 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1844 }
1845 if (i == -1)
1846 return compiler_error(c, LOOP_ERROR_MSG);
1847 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1848 break;
1849 case FINALLY_END:
1850 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854}
1855
1856/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857
1858 SETUP_FINALLY L
1859 <code for body>
1860 POP_BLOCK
1861 LOAD_CONST <None>
1862 L: <code for finalbody>
1863 END_FINALLY
1864
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865 The special instructions use the block stack. Each block
1866 stack entry contains the instruction that created it (here
1867 SETUP_FINALLY), the level of the value stack at the time the
1868 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 Pushes the current value stack level and the label
1872 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 Pops en entry from the block stack, and pops the value
1875 stack until its level is the same as indicated on the
1876 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 Pops a variable number of entries from the *value* stack
1879 and re-raises the exception they specify. The number of
1880 entries popped depends on the (pseudo) exception type.
1881
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882 The block stack is unwound when an exception is raised:
1883 when a SETUP_FINALLY entry is found, the exception is pushed
1884 onto the value stack (and the exception condition is cleared),
1885 and the interpreter jumps to the label gotten from the block
1886 stack.
1887*/
1888
1889static int
1890compiler_try_finally(struct compiler *c, stmt_ty s)
1891{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 basicblock *body, *end;
1893 body = compiler_new_block(c);
1894 end = compiler_new_block(c);
1895 if (body == NULL || end == NULL)
1896 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 ADDOP_JREL(c, SETUP_FINALLY, end);
1899 compiler_use_next_block(c, body);
1900 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1901 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001902 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
1903 if (!compiler_try_except(c, s))
1904 return 0;
1905 }
1906 else {
1907 VISIT_SEQ(c, stmt, s->v.Try.body);
1908 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 ADDOP(c, POP_BLOCK);
1910 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1913 compiler_use_next_block(c, end);
1914 if (!compiler_push_fblock(c, FINALLY_END, end))
1915 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001916 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 ADDOP(c, END_FINALLY);
1918 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001921}
1922
1923/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001924 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001925 (The contents of the value stack is shown in [], with the top
1926 at the right; 'tb' is trace-back info, 'val' the exception's
1927 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928
1929 Value stack Label Instruction Argument
1930 [] SETUP_EXCEPT L1
1931 [] <code for S>
1932 [] POP_BLOCK
1933 [] JUMP_FORWARD L0
1934
1935 [tb, val, exc] L1: DUP )
1936 [tb, val, exc, exc] <evaluate E1> )
1937 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
1938 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
1939 [tb, val, exc] POP
1940 [tb, val] <assign to V1> (or POP if no V1)
1941 [tb] POP
1942 [] <code for S1>
1943 JUMP_FORWARD L0
1944
1945 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001946 .............................etc.......................
1947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
1949
1950 [] L0: <next statement>
1951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001952 Of course, parts are not generated if Vi or Ei is not present.
1953*/
1954static int
1955compiler_try_except(struct compiler *c, stmt_ty s)
1956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 basicblock *body, *orelse, *except, *end;
1958 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 body = compiler_new_block(c);
1961 except = compiler_new_block(c);
1962 orelse = compiler_new_block(c);
1963 end = compiler_new_block(c);
1964 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1965 return 0;
1966 ADDOP_JREL(c, SETUP_EXCEPT, except);
1967 compiler_use_next_block(c, body);
1968 if (!compiler_push_fblock(c, EXCEPT, body))
1969 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001970 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 ADDOP(c, POP_BLOCK);
1972 compiler_pop_fblock(c, EXCEPT, body);
1973 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001974 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 compiler_use_next_block(c, except);
1976 for (i = 0; i < n; i++) {
1977 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001978 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 if (!handler->v.ExceptHandler.type && i < n-1)
1980 return compiler_error(c, "default 'except:' must be last");
1981 c->u->u_lineno_set = 0;
1982 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001983 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 except = compiler_new_block(c);
1985 if (except == NULL)
1986 return 0;
1987 if (handler->v.ExceptHandler.type) {
1988 ADDOP(c, DUP_TOP);
1989 VISIT(c, expr, handler->v.ExceptHandler.type);
1990 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1991 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1992 }
1993 ADDOP(c, POP_TOP);
1994 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05001995 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00001996
Benjamin Peterson74897ba2011-05-27 14:10:24 -05001997 cleanup_end = compiler_new_block(c);
1998 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05001999 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002000 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002001
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002002 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2003 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002004
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002005 /*
2006 try:
2007 # body
2008 except type as name:
2009 try:
2010 # body
2011 finally:
2012 name = None
2013 del name
2014 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002016 /* second try: */
2017 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2018 compiler_use_next_block(c, cleanup_body);
2019 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2020 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002022 /* second # body */
2023 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2024 ADDOP(c, POP_BLOCK);
2025 ADDOP(c, POP_EXCEPT);
2026 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002028 /* finally: */
2029 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2030 compiler_use_next_block(c, cleanup_end);
2031 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2032 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002034 /* name = None */
2035 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2036 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002038 /* del name */
2039 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002041 ADDOP(c, END_FINALLY);
2042 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 }
2044 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002045 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002047 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002048 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002049 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050
Guido van Rossumb940e112007-01-10 16:19:56 +00002051 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002052 ADDOP(c, POP_TOP);
2053 compiler_use_next_block(c, cleanup_body);
2054 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2055 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002057 ADDOP(c, POP_EXCEPT);
2058 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 }
2060 ADDOP_JREL(c, JUMP_FORWARD, end);
2061 compiler_use_next_block(c, except);
2062 }
2063 ADDOP(c, END_FINALLY);
2064 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002065 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 compiler_use_next_block(c, end);
2067 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002068}
2069
2070static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002071compiler_try(struct compiler *c, stmt_ty s) {
2072 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2073 return compiler_try_finally(c, s);
2074 else
2075 return compiler_try_except(c, s);
2076}
2077
2078
2079static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002080compiler_import_as(struct compiler *c, identifier name, identifier asname)
2081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 /* The IMPORT_NAME opcode was already generated. This function
2083 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 If there is a dot in name, we need to split it and emit a
2086 LOAD_ATTR for each name.
2087 */
2088 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name);
2089 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.');
2090 if (dot) {
2091 /* Consume the base module name to get the first attribute */
2092 src = dot + 1;
2093 while (dot) {
2094 /* NB src is only defined when dot != NULL */
2095 PyObject *attr;
2096 dot = Py_UNICODE_strchr(src, '.');
2097 attr = PyUnicode_FromUnicode(src,
2098 dot ? dot - src : Py_UNICODE_strlen(src));
2099 if (!attr)
2100 return -1;
2101 ADDOP_O(c, LOAD_ATTR, attr, names);
2102 Py_DECREF(attr);
2103 src = dot + 1;
2104 }
2105 }
2106 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002107}
2108
2109static int
2110compiler_import(struct compiler *c, stmt_ty s)
2111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 /* The Import node stores a module name like a.b.c as a single
2113 string. This is convenient for all cases except
2114 import a.b.c as d
2115 where we need to parse that string to extract the individual
2116 module names.
2117 XXX Perhaps change the representation to make this case simpler?
2118 */
2119 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 for (i = 0; i < n; i++) {
2122 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2123 int r;
2124 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 level = PyLong_FromLong(0);
2127 if (level == NULL)
2128 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 ADDOP_O(c, LOAD_CONST, level, consts);
2131 Py_DECREF(level);
2132 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2133 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 if (alias->asname) {
2136 r = compiler_import_as(c, alias->name, alias->asname);
2137 if (!r)
2138 return r;
2139 }
2140 else {
2141 identifier tmp = alias->name;
2142 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name);
2143 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.');
2144 if (dot)
2145 tmp = PyUnicode_FromUnicode(base,
2146 dot - base);
2147 r = compiler_nameop(c, tmp, Store);
2148 if (dot) {
2149 Py_DECREF(tmp);
2150 }
2151 if (!r)
2152 return r;
2153 }
2154 }
2155 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002156}
2157
2158static int
2159compiler_from_import(struct compiler *c, stmt_ty s)
2160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 PyObject *names = PyTuple_New(n);
2164 PyObject *level;
2165 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 if (!empty_string) {
2168 empty_string = PyUnicode_FromString("");
2169 if (!empty_string)
2170 return 0;
2171 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 if (!names)
2174 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 level = PyLong_FromLong(s->v.ImportFrom.level);
2177 if (!level) {
2178 Py_DECREF(names);
2179 return 0;
2180 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 /* build up the names */
2183 for (i = 0; i < n; i++) {
2184 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2185 Py_INCREF(alias->name);
2186 PyTuple_SET_ITEM(names, i, alias->name);
2187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2190 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2191 Py_DECREF(level);
2192 Py_DECREF(names);
2193 return compiler_error(c, "from __future__ imports must occur "
2194 "at the beginning of the file");
2195 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 ADDOP_O(c, LOAD_CONST, level, consts);
2198 Py_DECREF(level);
2199 ADDOP_O(c, LOAD_CONST, names, consts);
2200 Py_DECREF(names);
2201 if (s->v.ImportFrom.module) {
2202 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2203 }
2204 else {
2205 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2206 }
2207 for (i = 0; i < n; i++) {
2208 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2209 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') {
2212 assert(n == 1);
2213 ADDOP(c, IMPORT_STAR);
2214 return 1;
2215 }
2216
2217 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2218 store_name = alias->name;
2219 if (alias->asname)
2220 store_name = alias->asname;
2221
2222 if (!compiler_nameop(c, store_name, Store)) {
2223 Py_DECREF(names);
2224 return 0;
2225 }
2226 }
2227 /* remove imported module */
2228 ADDOP(c, POP_TOP);
2229 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230}
2231
2232static int
2233compiler_assert(struct compiler *c, stmt_ty s)
2234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 static PyObject *assertion_error = NULL;
2236 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237
Georg Brandl8334fd92010-12-04 10:26:46 +00002238 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 return 1;
2240 if (assertion_error == NULL) {
2241 assertion_error = PyUnicode_InternFromString("AssertionError");
2242 if (assertion_error == NULL)
2243 return 0;
2244 }
2245 if (s->v.Assert.test->kind == Tuple_kind &&
2246 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2247 const char* msg =
2248 "assertion is always true, perhaps remove parentheses?";
2249 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2250 c->u->u_lineno, NULL, NULL) == -1)
2251 return 0;
2252 }
2253 VISIT(c, expr, s->v.Assert.test);
2254 end = compiler_new_block(c);
2255 if (end == NULL)
2256 return 0;
2257 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2258 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2259 if (s->v.Assert.msg) {
2260 VISIT(c, expr, s->v.Assert.msg);
2261 ADDOP_I(c, CALL_FUNCTION, 1);
2262 }
2263 ADDOP_I(c, RAISE_VARARGS, 1);
2264 compiler_use_next_block(c, end);
2265 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266}
2267
2268static int
2269compiler_visit_stmt(struct compiler *c, stmt_ty s)
2270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 /* Always assign a lineno to the next instruction for a stmt. */
2274 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002275 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 switch (s->kind) {
2279 case FunctionDef_kind:
2280 return compiler_function(c, s);
2281 case ClassDef_kind:
2282 return compiler_class(c, s);
2283 case Return_kind:
2284 if (c->u->u_ste->ste_type != FunctionBlock)
2285 return compiler_error(c, "'return' outside function");
2286 if (s->v.Return.value) {
2287 VISIT(c, expr, s->v.Return.value);
2288 }
2289 else
2290 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2291 ADDOP(c, RETURN_VALUE);
2292 break;
2293 case Delete_kind:
2294 VISIT_SEQ(c, expr, s->v.Delete.targets)
2295 break;
2296 case Assign_kind:
2297 n = asdl_seq_LEN(s->v.Assign.targets);
2298 VISIT(c, expr, s->v.Assign.value);
2299 for (i = 0; i < n; i++) {
2300 if (i < n - 1)
2301 ADDOP(c, DUP_TOP);
2302 VISIT(c, expr,
2303 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2304 }
2305 break;
2306 case AugAssign_kind:
2307 return compiler_augassign(c, s);
2308 case For_kind:
2309 return compiler_for(c, s);
2310 case While_kind:
2311 return compiler_while(c, s);
2312 case If_kind:
2313 return compiler_if(c, s);
2314 case Raise_kind:
2315 n = 0;
2316 if (s->v.Raise.exc) {
2317 VISIT(c, expr, s->v.Raise.exc);
2318 n++;
2319 if (s->v.Raise.cause) {
2320 VISIT(c, expr, s->v.Raise.cause);
2321 n++;
2322 }
2323 }
2324 ADDOP_I(c, RAISE_VARARGS, n);
2325 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002326 case Try_kind:
2327 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 case Assert_kind:
2329 return compiler_assert(c, s);
2330 case Import_kind:
2331 return compiler_import(c, s);
2332 case ImportFrom_kind:
2333 return compiler_from_import(c, s);
2334 case Global_kind:
2335 case Nonlocal_kind:
2336 break;
2337 case Expr_kind:
2338 if (c->c_interactive && c->c_nestlevel <= 1) {
2339 VISIT(c, expr, s->v.Expr.value);
2340 ADDOP(c, PRINT_EXPR);
2341 }
2342 else if (s->v.Expr.value->kind != Str_kind &&
2343 s->v.Expr.value->kind != Num_kind) {
2344 VISIT(c, expr, s->v.Expr.value);
2345 ADDOP(c, POP_TOP);
2346 }
2347 break;
2348 case Pass_kind:
2349 break;
2350 case Break_kind:
2351 if (!compiler_in_loop(c))
2352 return compiler_error(c, "'break' outside loop");
2353 ADDOP(c, BREAK_LOOP);
2354 break;
2355 case Continue_kind:
2356 return compiler_continue(c);
2357 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002358 return compiler_with(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 }
2360 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002361}
2362
2363static int
2364unaryop(unaryop_ty op)
2365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 switch (op) {
2367 case Invert:
2368 return UNARY_INVERT;
2369 case Not:
2370 return UNARY_NOT;
2371 case UAdd:
2372 return UNARY_POSITIVE;
2373 case USub:
2374 return UNARY_NEGATIVE;
2375 default:
2376 PyErr_Format(PyExc_SystemError,
2377 "unary op %d should not be possible", op);
2378 return 0;
2379 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380}
2381
2382static int
2383binop(struct compiler *c, operator_ty op)
2384{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 switch (op) {
2386 case Add:
2387 return BINARY_ADD;
2388 case Sub:
2389 return BINARY_SUBTRACT;
2390 case Mult:
2391 return BINARY_MULTIPLY;
2392 case Div:
2393 return BINARY_TRUE_DIVIDE;
2394 case Mod:
2395 return BINARY_MODULO;
2396 case Pow:
2397 return BINARY_POWER;
2398 case LShift:
2399 return BINARY_LSHIFT;
2400 case RShift:
2401 return BINARY_RSHIFT;
2402 case BitOr:
2403 return BINARY_OR;
2404 case BitXor:
2405 return BINARY_XOR;
2406 case BitAnd:
2407 return BINARY_AND;
2408 case FloorDiv:
2409 return BINARY_FLOOR_DIVIDE;
2410 default:
2411 PyErr_Format(PyExc_SystemError,
2412 "binary op %d should not be possible", op);
2413 return 0;
2414 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415}
2416
2417static int
2418cmpop(cmpop_ty op)
2419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 switch (op) {
2421 case Eq:
2422 return PyCmp_EQ;
2423 case NotEq:
2424 return PyCmp_NE;
2425 case Lt:
2426 return PyCmp_LT;
2427 case LtE:
2428 return PyCmp_LE;
2429 case Gt:
2430 return PyCmp_GT;
2431 case GtE:
2432 return PyCmp_GE;
2433 case Is:
2434 return PyCmp_IS;
2435 case IsNot:
2436 return PyCmp_IS_NOT;
2437 case In:
2438 return PyCmp_IN;
2439 case NotIn:
2440 return PyCmp_NOT_IN;
2441 default:
2442 return PyCmp_BAD;
2443 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002444}
2445
2446static int
2447inplace_binop(struct compiler *c, operator_ty op)
2448{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 switch (op) {
2450 case Add:
2451 return INPLACE_ADD;
2452 case Sub:
2453 return INPLACE_SUBTRACT;
2454 case Mult:
2455 return INPLACE_MULTIPLY;
2456 case Div:
2457 return INPLACE_TRUE_DIVIDE;
2458 case Mod:
2459 return INPLACE_MODULO;
2460 case Pow:
2461 return INPLACE_POWER;
2462 case LShift:
2463 return INPLACE_LSHIFT;
2464 case RShift:
2465 return INPLACE_RSHIFT;
2466 case BitOr:
2467 return INPLACE_OR;
2468 case BitXor:
2469 return INPLACE_XOR;
2470 case BitAnd:
2471 return INPLACE_AND;
2472 case FloorDiv:
2473 return INPLACE_FLOOR_DIVIDE;
2474 default:
2475 PyErr_Format(PyExc_SystemError,
2476 "inplace binary op %d should not be possible", op);
2477 return 0;
2478 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479}
2480
2481static int
2482compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 int op, scope, arg;
2485 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 PyObject *dict = c->u->u_names;
2488 PyObject *mangled;
2489 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 mangled = _Py_Mangle(c->u->u_private, name);
2492 if (!mangled)
2493 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 op = 0;
2496 optype = OP_NAME;
2497 scope = PyST_GetScope(c->u->u_ste, mangled);
2498 switch (scope) {
2499 case FREE:
2500 dict = c->u->u_freevars;
2501 optype = OP_DEREF;
2502 break;
2503 case CELL:
2504 dict = c->u->u_cellvars;
2505 optype = OP_DEREF;
2506 break;
2507 case LOCAL:
2508 if (c->u->u_ste->ste_type == FunctionBlock)
2509 optype = OP_FAST;
2510 break;
2511 case GLOBAL_IMPLICIT:
2512 if (c->u->u_ste->ste_type == FunctionBlock &&
2513 !c->u->u_ste->ste_unoptimized)
2514 optype = OP_GLOBAL;
2515 break;
2516 case GLOBAL_EXPLICIT:
2517 optype = OP_GLOBAL;
2518 break;
2519 default:
2520 /* scope can be 0 */
2521 break;
2522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 /* XXX Leave assert here, but handle __doc__ and the like better */
2525 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 switch (optype) {
2528 case OP_DEREF:
2529 switch (ctx) {
2530 case Load: op = LOAD_DEREF; break;
2531 case Store: op = STORE_DEREF; break;
2532 case AugLoad:
2533 case AugStore:
2534 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002535 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 case Param:
2537 default:
2538 PyErr_SetString(PyExc_SystemError,
2539 "param invalid for deref variable");
2540 return 0;
2541 }
2542 break;
2543 case OP_FAST:
2544 switch (ctx) {
2545 case Load: op = LOAD_FAST; break;
2546 case Store: op = STORE_FAST; break;
2547 case Del: op = DELETE_FAST; break;
2548 case AugLoad:
2549 case AugStore:
2550 break;
2551 case Param:
2552 default:
2553 PyErr_SetString(PyExc_SystemError,
2554 "param invalid for local variable");
2555 return 0;
2556 }
2557 ADDOP_O(c, op, mangled, varnames);
2558 Py_DECREF(mangled);
2559 return 1;
2560 case OP_GLOBAL:
2561 switch (ctx) {
2562 case Load: op = LOAD_GLOBAL; break;
2563 case Store: op = STORE_GLOBAL; break;
2564 case Del: op = DELETE_GLOBAL; break;
2565 case AugLoad:
2566 case AugStore:
2567 break;
2568 case Param:
2569 default:
2570 PyErr_SetString(PyExc_SystemError,
2571 "param invalid for global variable");
2572 return 0;
2573 }
2574 break;
2575 case OP_NAME:
2576 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002577 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 case Store: op = STORE_NAME; break;
2579 case Del: op = DELETE_NAME; break;
2580 case AugLoad:
2581 case AugStore:
2582 break;
2583 case Param:
2584 default:
2585 PyErr_SetString(PyExc_SystemError,
2586 "param invalid for name variable");
2587 return 0;
2588 }
2589 break;
2590 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 assert(op);
2593 arg = compiler_add_o(c, dict, mangled);
2594 Py_DECREF(mangled);
2595 if (arg < 0)
2596 return 0;
2597 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002598}
2599
2600static int
2601compiler_boolop(struct compiler *c, expr_ty e)
2602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 basicblock *end;
2604 int jumpi, i, n;
2605 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 assert(e->kind == BoolOp_kind);
2608 if (e->v.BoolOp.op == And)
2609 jumpi = JUMP_IF_FALSE_OR_POP;
2610 else
2611 jumpi = JUMP_IF_TRUE_OR_POP;
2612 end = compiler_new_block(c);
2613 if (end == NULL)
2614 return 0;
2615 s = e->v.BoolOp.values;
2616 n = asdl_seq_LEN(s) - 1;
2617 assert(n >= 0);
2618 for (i = 0; i < n; ++i) {
2619 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2620 ADDOP_JABS(c, jumpi, end);
2621 }
2622 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2623 compiler_use_next_block(c, end);
2624 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002625}
2626
2627static int
2628compiler_list(struct compiler *c, expr_ty e)
2629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 int n = asdl_seq_LEN(e->v.List.elts);
2631 if (e->v.List.ctx == Store) {
2632 int i, seen_star = 0;
2633 for (i = 0; i < n; i++) {
2634 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2635 if (elt->kind == Starred_kind && !seen_star) {
2636 if ((i >= (1 << 8)) ||
2637 (n-i-1 >= (INT_MAX >> 8)))
2638 return compiler_error(c,
2639 "too many expressions in "
2640 "star-unpacking assignment");
2641 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2642 seen_star = 1;
2643 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2644 } else if (elt->kind == Starred_kind) {
2645 return compiler_error(c,
2646 "two starred expressions in assignment");
2647 }
2648 }
2649 if (!seen_star) {
2650 ADDOP_I(c, UNPACK_SEQUENCE, n);
2651 }
2652 }
2653 VISIT_SEQ(c, expr, e->v.List.elts);
2654 if (e->v.List.ctx == Load) {
2655 ADDOP_I(c, BUILD_LIST, n);
2656 }
2657 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658}
2659
2660static int
2661compiler_tuple(struct compiler *c, expr_ty e)
2662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 int n = asdl_seq_LEN(e->v.Tuple.elts);
2664 if (e->v.Tuple.ctx == Store) {
2665 int i, seen_star = 0;
2666 for (i = 0; i < n; i++) {
2667 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2668 if (elt->kind == Starred_kind && !seen_star) {
2669 if ((i >= (1 << 8)) ||
2670 (n-i-1 >= (INT_MAX >> 8)))
2671 return compiler_error(c,
2672 "too many expressions in "
2673 "star-unpacking assignment");
2674 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2675 seen_star = 1;
2676 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2677 } else if (elt->kind == Starred_kind) {
2678 return compiler_error(c,
2679 "two starred expressions in assignment");
2680 }
2681 }
2682 if (!seen_star) {
2683 ADDOP_I(c, UNPACK_SEQUENCE, n);
2684 }
2685 }
2686 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2687 if (e->v.Tuple.ctx == Load) {
2688 ADDOP_I(c, BUILD_TUPLE, n);
2689 }
2690 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002691}
2692
2693static int
2694compiler_compare(struct compiler *c, expr_ty e)
2695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 int i, n;
2697 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2700 VISIT(c, expr, e->v.Compare.left);
2701 n = asdl_seq_LEN(e->v.Compare.ops);
2702 assert(n > 0);
2703 if (n > 1) {
2704 cleanup = compiler_new_block(c);
2705 if (cleanup == NULL)
2706 return 0;
2707 VISIT(c, expr,
2708 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2709 }
2710 for (i = 1; i < n; i++) {
2711 ADDOP(c, DUP_TOP);
2712 ADDOP(c, ROT_THREE);
2713 ADDOP_I(c, COMPARE_OP,
2714 cmpop((cmpop_ty)(asdl_seq_GET(
2715 e->v.Compare.ops, i - 1))));
2716 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2717 NEXT_BLOCK(c);
2718 if (i < (n - 1))
2719 VISIT(c, expr,
2720 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2721 }
2722 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2723 ADDOP_I(c, COMPARE_OP,
2724 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2725 if (n > 1) {
2726 basicblock *end = compiler_new_block(c);
2727 if (end == NULL)
2728 return 0;
2729 ADDOP_JREL(c, JUMP_FORWARD, end);
2730 compiler_use_next_block(c, cleanup);
2731 ADDOP(c, ROT_TWO);
2732 ADDOP(c, POP_TOP);
2733 compiler_use_next_block(c, end);
2734 }
2735 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002736}
2737
2738static int
2739compiler_call(struct compiler *c, expr_ty e)
2740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 VISIT(c, expr, e->v.Call.func);
2742 return compiler_call_helper(c, 0,
2743 e->v.Call.args,
2744 e->v.Call.keywords,
2745 e->v.Call.starargs,
2746 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002747}
2748
2749/* shared code between compiler_call and compiler_class */
2750static int
2751compiler_call_helper(struct compiler *c,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 int n, /* Args already pushed */
2753 asdl_seq *args,
2754 asdl_seq *keywords,
2755 expr_ty starargs,
2756 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 n += asdl_seq_LEN(args);
2761 VISIT_SEQ(c, expr, args);
2762 if (keywords) {
2763 VISIT_SEQ(c, keyword, keywords);
2764 n |= asdl_seq_LEN(keywords) << 8;
2765 }
2766 if (starargs) {
2767 VISIT(c, expr, starargs);
2768 code |= 1;
2769 }
2770 if (kwargs) {
2771 VISIT(c, expr, kwargs);
2772 code |= 2;
2773 }
2774 switch (code) {
2775 case 0:
2776 ADDOP_I(c, CALL_FUNCTION, n);
2777 break;
2778 case 1:
2779 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2780 break;
2781 case 2:
2782 ADDOP_I(c, CALL_FUNCTION_KW, n);
2783 break;
2784 case 3:
2785 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2786 break;
2787 }
2788 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002789}
2790
Nick Coghlan650f0d02007-04-15 12:05:43 +00002791
2792/* List and set comprehensions and generator expressions work by creating a
2793 nested function to perform the actual iteration. This means that the
2794 iteration variables don't leak into the current scope.
2795 The defined function is called immediately following its definition, with the
2796 result of that call being the result of the expression.
2797 The LC/SC version returns the populated container, while the GE version is
2798 flagged in symtable.c as a generator, so it returns the generator object
2799 when the function is called.
2800 This code *knows* that the loop cannot contain break, continue, or return,
2801 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2802
2803 Possible cleanups:
2804 - iterate over the generator sequence instead of using recursion
2805*/
2806
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002807static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808compiler_comprehension_generator(struct compiler *c,
2809 asdl_seq *generators, int gen_index,
2810 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002811{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 /* generate code for the iterator, then each of the ifs,
2813 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 comprehension_ty gen;
2816 basicblock *start, *anchor, *skip, *if_cleanup;
2817 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 start = compiler_new_block(c);
2820 skip = compiler_new_block(c);
2821 if_cleanup = compiler_new_block(c);
2822 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2825 anchor == NULL)
2826 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 if (gen_index == 0) {
2831 /* Receive outermost iter as an implicit argument */
2832 c->u->u_argcount = 1;
2833 ADDOP_I(c, LOAD_FAST, 0);
2834 }
2835 else {
2836 /* Sub-iter - calculate on the fly */
2837 VISIT(c, expr, gen->iter);
2838 ADDOP(c, GET_ITER);
2839 }
2840 compiler_use_next_block(c, start);
2841 ADDOP_JREL(c, FOR_ITER, anchor);
2842 NEXT_BLOCK(c);
2843 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 /* XXX this needs to be cleaned up...a lot! */
2846 n = asdl_seq_LEN(gen->ifs);
2847 for (i = 0; i < n; i++) {
2848 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2849 VISIT(c, expr, e);
2850 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2851 NEXT_BLOCK(c);
2852 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 if (++gen_index < asdl_seq_LEN(generators))
2855 if (!compiler_comprehension_generator(c,
2856 generators, gen_index,
2857 elt, val, type))
2858 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 /* only append after the last for generator */
2861 if (gen_index >= asdl_seq_LEN(generators)) {
2862 /* comprehension specific code */
2863 switch (type) {
2864 case COMP_GENEXP:
2865 VISIT(c, expr, elt);
2866 ADDOP(c, YIELD_VALUE);
2867 ADDOP(c, POP_TOP);
2868 break;
2869 case COMP_LISTCOMP:
2870 VISIT(c, expr, elt);
2871 ADDOP_I(c, LIST_APPEND, gen_index + 1);
2872 break;
2873 case COMP_SETCOMP:
2874 VISIT(c, expr, elt);
2875 ADDOP_I(c, SET_ADD, gen_index + 1);
2876 break;
2877 case COMP_DICTCOMP:
2878 /* With 'd[k] = v', v is evaluated before k, so we do
2879 the same. */
2880 VISIT(c, expr, val);
2881 VISIT(c, expr, elt);
2882 ADDOP_I(c, MAP_ADD, gen_index + 1);
2883 break;
2884 default:
2885 return 0;
2886 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 compiler_use_next_block(c, skip);
2889 }
2890 compiler_use_next_block(c, if_cleanup);
2891 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2892 compiler_use_next_block(c, anchor);
2893
2894 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002895}
2896
2897static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002898compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00002900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 PyCodeObject *co = NULL;
2902 expr_ty outermost_iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 outermost_iter = ((comprehension_ty)
2905 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
2908 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (type != COMP_GENEXP) {
2911 int op;
2912 switch (type) {
2913 case COMP_LISTCOMP:
2914 op = BUILD_LIST;
2915 break;
2916 case COMP_SETCOMP:
2917 op = BUILD_SET;
2918 break;
2919 case COMP_DICTCOMP:
2920 op = BUILD_MAP;
2921 break;
2922 default:
2923 PyErr_Format(PyExc_SystemError,
2924 "unknown comprehension type %d", type);
2925 goto error_in_scope;
2926 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 ADDOP_I(c, op, 0);
2929 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 if (!compiler_comprehension_generator(c, generators, 0, elt,
2932 val, type))
2933 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002935 if (type != COMP_GENEXP) {
2936 ADDOP(c, RETURN_VALUE);
2937 }
2938
2939 co = assemble(c, 1);
2940 compiler_exit_scope(c);
2941 if (co == NULL)
2942 goto error;
2943
2944 if (!compiler_make_closure(c, co, 0))
2945 goto error;
2946 Py_DECREF(co);
2947
2948 VISIT(c, expr, outermost_iter);
2949 ADDOP(c, GET_ITER);
2950 ADDOP_I(c, CALL_FUNCTION, 1);
2951 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002952error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002954error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 Py_XDECREF(co);
2956 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00002957}
2958
2959static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002960compiler_genexp(struct compiler *c, expr_ty e)
2961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 static identifier name;
2963 if (!name) {
2964 name = PyUnicode_FromString("<genexpr>");
2965 if (!name)
2966 return 0;
2967 }
2968 assert(e->kind == GeneratorExp_kind);
2969 return compiler_comprehension(c, e, COMP_GENEXP, name,
2970 e->v.GeneratorExp.generators,
2971 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972}
2973
2974static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002975compiler_listcomp(struct compiler *c, expr_ty e)
2976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 static identifier name;
2978 if (!name) {
2979 name = PyUnicode_FromString("<listcomp>");
2980 if (!name)
2981 return 0;
2982 }
2983 assert(e->kind == ListComp_kind);
2984 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
2985 e->v.ListComp.generators,
2986 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002987}
2988
2989static int
2990compiler_setcomp(struct compiler *c, expr_ty e)
2991{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 static identifier name;
2993 if (!name) {
2994 name = PyUnicode_FromString("<setcomp>");
2995 if (!name)
2996 return 0;
2997 }
2998 assert(e->kind == SetComp_kind);
2999 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3000 e->v.SetComp.generators,
3001 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003002}
3003
3004
3005static int
3006compiler_dictcomp(struct compiler *c, expr_ty e)
3007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 static identifier name;
3009 if (!name) {
3010 name = PyUnicode_FromString("<dictcomp>");
3011 if (!name)
3012 return 0;
3013 }
3014 assert(e->kind == DictComp_kind);
3015 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3016 e->v.DictComp.generators,
3017 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003018}
3019
3020
3021static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003022compiler_visit_keyword(struct compiler *c, keyword_ty k)
3023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3025 VISIT(c, expr, k->value);
3026 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003027}
3028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030 whether they are true or false.
3031
3032 Return values: 1 for true, 0 for false, -1 for non-constant.
3033 */
3034
3035static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003036expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003038 char *id;
3039 switch (e->kind) {
3040 case Ellipsis_kind:
3041 return 1;
3042 case Num_kind:
3043 return PyObject_IsTrue(e->v.Num.n);
3044 case Str_kind:
3045 return PyObject_IsTrue(e->v.Str.s);
3046 case Name_kind:
3047 /* optimize away names that can't be reassigned */
3048 id = PyBytes_AS_STRING(
Victor Stinnerf3fd7332011-03-02 01:03:11 +00003049 _PyUnicode_AsDefaultEncodedString(e->v.Name.id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 if (strcmp(id, "True") == 0) return 1;
3051 if (strcmp(id, "False") == 0) return 0;
3052 if (strcmp(id, "None") == 0) return 0;
3053 if (strcmp(id, "__debug__") == 0)
Georg Brandl8334fd92010-12-04 10:26:46 +00003054 return ! c->c_optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 /* fall through */
3056 default:
3057 return -1;
3058 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003059}
3060
Guido van Rossumc2e20742006-02-27 22:32:47 +00003061/*
3062 Implements the with statement from PEP 343.
3063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065
3066 with EXPR as VAR:
3067 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068
Guido van Rossumc2e20742006-02-27 22:32:47 +00003069 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070
Thomas Wouters477c8d52006-05-27 19:21:47 +00003071 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003072 exit = context.__exit__ # not calling it
3073 value = context.__enter__()
3074 try:
3075 VAR = value # if VAR present in the syntax
3076 BLOCK
3077 finally:
3078 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003080 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003082 exit(*exc)
3083 */
3084static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003085compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003086{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003087 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003088 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089
3090 assert(s->kind == With_kind);
3091
Guido van Rossumc2e20742006-02-27 22:32:47 +00003092 block = compiler_new_block(c);
3093 finally = compiler_new_block(c);
3094 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003095 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003096
Thomas Wouters477c8d52006-05-27 19:21:47 +00003097 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003098 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003099 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003100
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003101 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003102 compiler_use_next_block(c, block);
3103 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003104 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003105 }
3106
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003107 if (item->optional_vars) {
3108 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003109 }
3110 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003112 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003113 }
3114
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003115 pos++;
3116 if (pos == asdl_seq_LEN(s->v.With.items))
3117 /* BLOCK code */
3118 VISIT_SEQ(c, stmt, s->v.With.body)
3119 else if (!compiler_with(c, s, pos))
3120 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003121
3122 /* End of try block; start the finally block */
3123 ADDOP(c, POP_BLOCK);
3124 compiler_pop_fblock(c, FINALLY_TRY, block);
3125
3126 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3127 compiler_use_next_block(c, finally);
3128 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003129 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003130
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003131 /* Finally block starts; context.__exit__ is on the stack under
3132 the exception or return information. Just issue our magic
3133 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003134 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003135
3136 /* Finally block ends. */
3137 ADDOP(c, END_FINALLY);
3138 compiler_pop_fblock(c, FINALLY_END, finally);
3139 return 1;
3140}
3141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003142static int
3143compiler_visit_expr(struct compiler *c, expr_ty e)
3144{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003145 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003147 /* If expr e has a different line number than the last expr/stmt,
3148 set a new line number for the next instruction.
3149 */
3150 if (e->lineno > c->u->u_lineno) {
3151 c->u->u_lineno = e->lineno;
3152 c->u->u_lineno_set = 0;
3153 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003154 /* Updating the column offset is always harmless. */
3155 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 switch (e->kind) {
3157 case BoolOp_kind:
3158 return compiler_boolop(c, e);
3159 case BinOp_kind:
3160 VISIT(c, expr, e->v.BinOp.left);
3161 VISIT(c, expr, e->v.BinOp.right);
3162 ADDOP(c, binop(c, e->v.BinOp.op));
3163 break;
3164 case UnaryOp_kind:
3165 VISIT(c, expr, e->v.UnaryOp.operand);
3166 ADDOP(c, unaryop(e->v.UnaryOp.op));
3167 break;
3168 case Lambda_kind:
3169 return compiler_lambda(c, e);
3170 case IfExp_kind:
3171 return compiler_ifexp(c, e);
3172 case Dict_kind:
3173 n = asdl_seq_LEN(e->v.Dict.values);
3174 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3175 for (i = 0; i < n; i++) {
3176 VISIT(c, expr,
3177 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3178 VISIT(c, expr,
3179 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3180 ADDOP(c, STORE_MAP);
3181 }
3182 break;
3183 case Set_kind:
3184 n = asdl_seq_LEN(e->v.Set.elts);
3185 VISIT_SEQ(c, expr, e->v.Set.elts);
3186 ADDOP_I(c, BUILD_SET, n);
3187 break;
3188 case GeneratorExp_kind:
3189 return compiler_genexp(c, e);
3190 case ListComp_kind:
3191 return compiler_listcomp(c, e);
3192 case SetComp_kind:
3193 return compiler_setcomp(c, e);
3194 case DictComp_kind:
3195 return compiler_dictcomp(c, e);
3196 case Yield_kind:
3197 if (c->u->u_ste->ste_type != FunctionBlock)
3198 return compiler_error(c, "'yield' outside function");
3199 if (e->v.Yield.value) {
3200 VISIT(c, expr, e->v.Yield.value);
3201 }
3202 else {
3203 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3204 }
3205 ADDOP(c, YIELD_VALUE);
3206 break;
3207 case Compare_kind:
3208 return compiler_compare(c, e);
3209 case Call_kind:
3210 return compiler_call(c, e);
3211 case Num_kind:
3212 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3213 break;
3214 case Str_kind:
3215 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3216 break;
3217 case Bytes_kind:
3218 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3219 break;
3220 case Ellipsis_kind:
3221 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3222 break;
3223 /* The following exprs can be assignment targets. */
3224 case Attribute_kind:
3225 if (e->v.Attribute.ctx != AugStore)
3226 VISIT(c, expr, e->v.Attribute.value);
3227 switch (e->v.Attribute.ctx) {
3228 case AugLoad:
3229 ADDOP(c, DUP_TOP);
3230 /* Fall through to load */
3231 case Load:
3232 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3233 break;
3234 case AugStore:
3235 ADDOP(c, ROT_TWO);
3236 /* Fall through to save */
3237 case Store:
3238 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3239 break;
3240 case Del:
3241 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3242 break;
3243 case Param:
3244 default:
3245 PyErr_SetString(PyExc_SystemError,
3246 "param invalid in attribute expression");
3247 return 0;
3248 }
3249 break;
3250 case Subscript_kind:
3251 switch (e->v.Subscript.ctx) {
3252 case AugLoad:
3253 VISIT(c, expr, e->v.Subscript.value);
3254 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3255 break;
3256 case Load:
3257 VISIT(c, expr, e->v.Subscript.value);
3258 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3259 break;
3260 case AugStore:
3261 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3262 break;
3263 case Store:
3264 VISIT(c, expr, e->v.Subscript.value);
3265 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3266 break;
3267 case Del:
3268 VISIT(c, expr, e->v.Subscript.value);
3269 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3270 break;
3271 case Param:
3272 default:
3273 PyErr_SetString(PyExc_SystemError,
3274 "param invalid in subscript expression");
3275 return 0;
3276 }
3277 break;
3278 case Starred_kind:
3279 switch (e->v.Starred.ctx) {
3280 case Store:
3281 /* In all legitimate cases, the Starred node was already replaced
3282 * by compiler_list/compiler_tuple. XXX: is that okay? */
3283 return compiler_error(c,
3284 "starred assignment target must be in a list or tuple");
3285 default:
3286 return compiler_error(c,
3287 "can use starred expression only as assignment target");
3288 }
3289 break;
3290 case Name_kind:
3291 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3292 /* child nodes of List and Tuple will have expr_context set */
3293 case List_kind:
3294 return compiler_list(c, e);
3295 case Tuple_kind:
3296 return compiler_tuple(c, e);
3297 }
3298 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299}
3300
3301static int
3302compiler_augassign(struct compiler *c, stmt_ty s)
3303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 expr_ty e = s->v.AugAssign.target;
3305 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 switch (e->kind) {
3310 case Attribute_kind:
3311 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3312 AugLoad, e->lineno, e->col_offset, c->c_arena);
3313 if (auge == NULL)
3314 return 0;
3315 VISIT(c, expr, auge);
3316 VISIT(c, expr, s->v.AugAssign.value);
3317 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3318 auge->v.Attribute.ctx = AugStore;
3319 VISIT(c, expr, auge);
3320 break;
3321 case Subscript_kind:
3322 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3323 AugLoad, e->lineno, e->col_offset, c->c_arena);
3324 if (auge == NULL)
3325 return 0;
3326 VISIT(c, expr, auge);
3327 VISIT(c, expr, s->v.AugAssign.value);
3328 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3329 auge->v.Subscript.ctx = AugStore;
3330 VISIT(c, expr, auge);
3331 break;
3332 case Name_kind:
3333 if (!compiler_nameop(c, e->v.Name.id, Load))
3334 return 0;
3335 VISIT(c, expr, s->v.AugAssign.value);
3336 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3337 return compiler_nameop(c, e->v.Name.id, Store);
3338 default:
3339 PyErr_Format(PyExc_SystemError,
3340 "invalid node type (%d) for augmented assignment",
3341 e->kind);
3342 return 0;
3343 }
3344 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003345}
3346
3347static int
3348compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 struct fblockinfo *f;
3351 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3352 PyErr_SetString(PyExc_SystemError,
3353 "too many statically nested blocks");
3354 return 0;
3355 }
3356 f = &c->u->u_fblock[c->u->u_nfblocks++];
3357 f->fb_type = t;
3358 f->fb_block = b;
3359 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003360}
3361
3362static void
3363compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 struct compiler_unit *u = c->u;
3366 assert(u->u_nfblocks > 0);
3367 u->u_nfblocks--;
3368 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3369 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370}
3371
Thomas Wouters89f507f2006-12-13 04:49:30 +00003372static int
3373compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 int i;
3375 struct compiler_unit *u = c->u;
3376 for (i = 0; i < u->u_nfblocks; ++i) {
3377 if (u->u_fblock[i].fb_type == LOOP)
3378 return 1;
3379 }
3380 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003381}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003382/* Raises a SyntaxError and returns 0.
3383 If something goes wrong, a different exception may be raised.
3384*/
3385
3386static int
3387compiler_error(struct compiler *c, const char *errstr)
3388{
Benjamin Peterson43b06862011-05-27 09:08:01 -05003389 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3393 if (!loc) {
3394 Py_INCREF(Py_None);
3395 loc = Py_None;
3396 }
Benjamin Peterson43b06862011-05-27 09:08:01 -05003397 u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003398 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 if (!u)
3400 goto exit;
3401 v = Py_BuildValue("(zO)", errstr, u);
3402 if (!v)
3403 goto exit;
3404 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003405 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 Py_DECREF(loc);
3407 Py_XDECREF(u);
3408 Py_XDECREF(v);
3409 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003410}
3411
3412static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413compiler_handle_subscr(struct compiler *c, const char *kind,
3414 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 /* XXX this code is duplicated */
3419 switch (ctx) {
3420 case AugLoad: /* fall through to Load */
3421 case Load: op = BINARY_SUBSCR; break;
3422 case AugStore:/* fall through to Store */
3423 case Store: op = STORE_SUBSCR; break;
3424 case Del: op = DELETE_SUBSCR; break;
3425 case Param:
3426 PyErr_Format(PyExc_SystemError,
3427 "invalid %s kind %d in subscript\n",
3428 kind, ctx);
3429 return 0;
3430 }
3431 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00003432 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 }
3434 else if (ctx == AugStore) {
3435 ADDOP(c, ROT_THREE);
3436 }
3437 ADDOP(c, op);
3438 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003439}
3440
3441static int
3442compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 int n = 2;
3445 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 /* only handles the cases where BUILD_SLICE is emitted */
3448 if (s->v.Slice.lower) {
3449 VISIT(c, expr, s->v.Slice.lower);
3450 }
3451 else {
3452 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3453 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 if (s->v.Slice.upper) {
3456 VISIT(c, expr, s->v.Slice.upper);
3457 }
3458 else {
3459 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3460 }
3461
3462 if (s->v.Slice.step) {
3463 n++;
3464 VISIT(c, expr, s->v.Slice.step);
3465 }
3466 ADDOP_I(c, BUILD_SLICE, n);
3467 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003468}
3469
3470static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3472 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 switch (s->kind) {
3475 case Slice_kind:
3476 return compiler_slice(c, s, ctx);
3477 case Index_kind:
3478 VISIT(c, expr, s->v.Index.value);
3479 break;
3480 case ExtSlice_kind:
3481 default:
3482 PyErr_SetString(PyExc_SystemError,
3483 "extended slice invalid in nested slice");
3484 return 0;
3485 }
3486 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003487}
3488
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003489static int
3490compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 char * kindname = NULL;
3493 switch (s->kind) {
3494 case Index_kind:
3495 kindname = "index";
3496 if (ctx != AugStore) {
3497 VISIT(c, expr, s->v.Index.value);
3498 }
3499 break;
3500 case Slice_kind:
3501 kindname = "slice";
3502 if (ctx != AugStore) {
3503 if (!compiler_slice(c, s, ctx))
3504 return 0;
3505 }
3506 break;
3507 case ExtSlice_kind:
3508 kindname = "extended slice";
3509 if (ctx != AugStore) {
3510 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3511 for (i = 0; i < n; i++) {
3512 slice_ty sub = (slice_ty)asdl_seq_GET(
3513 s->v.ExtSlice.dims, i);
3514 if (!compiler_visit_nested_slice(c, sub, ctx))
3515 return 0;
3516 }
3517 ADDOP_I(c, BUILD_TUPLE, n);
3518 }
3519 break;
3520 default:
3521 PyErr_Format(PyExc_SystemError,
3522 "invalid subscript kind %d", s->kind);
3523 return 0;
3524 }
3525 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003526}
3527
Thomas Wouters89f507f2006-12-13 04:49:30 +00003528/* End of the compiler section, beginning of the assembler section */
3529
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003530/* do depth-first search of basic block graph, starting with block.
3531 post records the block indices in post-order.
3532
3533 XXX must handle implicit jumps from one block to next
3534*/
3535
Thomas Wouters89f507f2006-12-13 04:49:30 +00003536struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 PyObject *a_bytecode; /* string containing bytecode */
3538 int a_offset; /* offset into bytecode */
3539 int a_nblocks; /* number of reachable blocks */
3540 basicblock **a_postorder; /* list of blocks in dfs postorder */
3541 PyObject *a_lnotab; /* string containing lnotab */
3542 int a_lnotab_off; /* offset into lnotab */
3543 int a_lineno; /* last lineno of emitted instruction */
3544 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003545};
3546
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547static void
3548dfs(struct compiler *c, basicblock *b, struct assembler *a)
3549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 int i;
3551 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 if (b->b_seen)
3554 return;
3555 b->b_seen = 1;
3556 if (b->b_next != NULL)
3557 dfs(c, b->b_next, a);
3558 for (i = 0; i < b->b_iused; i++) {
3559 instr = &b->b_instr[i];
3560 if (instr->i_jrel || instr->i_jabs)
3561 dfs(c, instr->i_target, a);
3562 }
3563 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564}
3565
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003566static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003567stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3568{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 int i, target_depth;
3570 struct instr *instr;
3571 if (b->b_seen || b->b_startdepth >= depth)
3572 return maxdepth;
3573 b->b_seen = 1;
3574 b->b_startdepth = depth;
3575 for (i = 0; i < b->b_iused; i++) {
3576 instr = &b->b_instr[i];
3577 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3578 if (depth > maxdepth)
3579 maxdepth = depth;
3580 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3581 if (instr->i_jrel || instr->i_jabs) {
3582 target_depth = depth;
3583 if (instr->i_opcode == FOR_ITER) {
3584 target_depth = depth-2;
3585 } else if (instr->i_opcode == SETUP_FINALLY ||
3586 instr->i_opcode == SETUP_EXCEPT) {
3587 target_depth = depth+3;
3588 if (target_depth > maxdepth)
3589 maxdepth = target_depth;
3590 }
3591 maxdepth = stackdepth_walk(c, instr->i_target,
3592 target_depth, maxdepth);
3593 if (instr->i_opcode == JUMP_ABSOLUTE ||
3594 instr->i_opcode == JUMP_FORWARD) {
3595 goto out; /* remaining code is dead */
3596 }
3597 }
3598 }
3599 if (b->b_next)
3600 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 b->b_seen = 0;
3603 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003604}
3605
3606/* Find the flow path that needs the largest stack. We assume that
3607 * cycles in the flow graph have no net effect on the stack depth.
3608 */
3609static int
3610stackdepth(struct compiler *c)
3611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 basicblock *b, *entryblock;
3613 entryblock = NULL;
3614 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3615 b->b_seen = 0;
3616 b->b_startdepth = INT_MIN;
3617 entryblock = b;
3618 }
3619 if (!entryblock)
3620 return 0;
3621 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622}
3623
3624static int
3625assemble_init(struct assembler *a, int nblocks, int firstlineno)
3626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 memset(a, 0, sizeof(struct assembler));
3628 a->a_lineno = firstlineno;
3629 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3630 if (!a->a_bytecode)
3631 return 0;
3632 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3633 if (!a->a_lnotab)
3634 return 0;
3635 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3636 PyErr_NoMemory();
3637 return 0;
3638 }
3639 a->a_postorder = (basicblock **)PyObject_Malloc(
3640 sizeof(basicblock *) * nblocks);
3641 if (!a->a_postorder) {
3642 PyErr_NoMemory();
3643 return 0;
3644 }
3645 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646}
3647
3648static void
3649assemble_free(struct assembler *a)
3650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 Py_XDECREF(a->a_bytecode);
3652 Py_XDECREF(a->a_lnotab);
3653 if (a->a_postorder)
3654 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655}
3656
3657/* Return the size of a basic block in bytes. */
3658
3659static int
3660instrsize(struct instr *instr)
3661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 if (!instr->i_hasarg)
3663 return 1; /* 1 byte for the opcode*/
3664 if (instr->i_oparg > 0xffff)
3665 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3666 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003667}
3668
3669static int
3670blocksize(basicblock *b)
3671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 int i;
3673 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 for (i = 0; i < b->b_iused; i++)
3676 size += instrsize(&b->b_instr[i]);
3677 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003678}
3679
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003680/* Appends a pair to the end of the line number table, a_lnotab, representing
3681 the instruction's bytecode offset and line number. See
3682 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003683
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003684static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 int d_bytecode, d_lineno;
3688 int len;
3689 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 d_bytecode = a->a_offset - a->a_lineno_off;
3692 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 assert(d_bytecode >= 0);
3695 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 if(d_bytecode == 0 && d_lineno == 0)
3698 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 if (d_bytecode > 255) {
3701 int j, nbytes, ncodes = d_bytecode / 255;
3702 nbytes = a->a_lnotab_off + 2 * ncodes;
3703 len = PyBytes_GET_SIZE(a->a_lnotab);
3704 if (nbytes >= len) {
3705 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3706 len = nbytes;
3707 else if (len <= INT_MAX / 2)
3708 len *= 2;
3709 else {
3710 PyErr_NoMemory();
3711 return 0;
3712 }
3713 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3714 return 0;
3715 }
3716 lnotab = (unsigned char *)
3717 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3718 for (j = 0; j < ncodes; j++) {
3719 *lnotab++ = 255;
3720 *lnotab++ = 0;
3721 }
3722 d_bytecode -= ncodes * 255;
3723 a->a_lnotab_off += ncodes * 2;
3724 }
3725 assert(d_bytecode <= 255);
3726 if (d_lineno > 255) {
3727 int j, nbytes, ncodes = d_lineno / 255;
3728 nbytes = a->a_lnotab_off + 2 * ncodes;
3729 len = PyBytes_GET_SIZE(a->a_lnotab);
3730 if (nbytes >= len) {
3731 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3732 len = nbytes;
3733 else if (len <= INT_MAX / 2)
3734 len *= 2;
3735 else {
3736 PyErr_NoMemory();
3737 return 0;
3738 }
3739 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3740 return 0;
3741 }
3742 lnotab = (unsigned char *)
3743 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3744 *lnotab++ = d_bytecode;
3745 *lnotab++ = 255;
3746 d_bytecode = 0;
3747 for (j = 1; j < ncodes; j++) {
3748 *lnotab++ = 0;
3749 *lnotab++ = 255;
3750 }
3751 d_lineno -= ncodes * 255;
3752 a->a_lnotab_off += ncodes * 2;
3753 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 len = PyBytes_GET_SIZE(a->a_lnotab);
3756 if (a->a_lnotab_off + 2 >= len) {
3757 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3758 return 0;
3759 }
3760 lnotab = (unsigned char *)
3761 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 a->a_lnotab_off += 2;
3764 if (d_bytecode) {
3765 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003766 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 }
3768 else { /* First line of a block; def stmt, etc. */
3769 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003770 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 }
3772 a->a_lineno = i->i_lineno;
3773 a->a_lineno_off = a->a_offset;
3774 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003775}
3776
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777/* assemble_emit()
3778 Extend the bytecode with a new instruction.
3779 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003780*/
3781
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003782static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003783assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 int size, arg = 0, ext = 0;
3786 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3787 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 size = instrsize(i);
3790 if (i->i_hasarg) {
3791 arg = i->i_oparg;
3792 ext = arg >> 16;
3793 }
3794 if (i->i_lineno && !assemble_lnotab(a, i))
3795 return 0;
3796 if (a->a_offset + size >= len) {
3797 if (len > PY_SSIZE_T_MAX / 2)
3798 return 0;
3799 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3800 return 0;
3801 }
3802 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3803 a->a_offset += size;
3804 if (size == 6) {
3805 assert(i->i_hasarg);
3806 *code++ = (char)EXTENDED_ARG;
3807 *code++ = ext & 0xff;
3808 *code++ = ext >> 8;
3809 arg &= 0xffff;
3810 }
3811 *code++ = i->i_opcode;
3812 if (i->i_hasarg) {
3813 assert(size == 3 || size == 6);
3814 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003815 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 }
3817 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003818}
3819
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003820static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003821assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 basicblock *b;
3824 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3825 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 /* Compute the size of each block and fixup jump args.
3828 Replace block pointer with position in bytecode. */
3829 do {
3830 totsize = 0;
3831 for (i = a->a_nblocks - 1; i >= 0; i--) {
3832 b = a->a_postorder[i];
3833 bsize = blocksize(b);
3834 b->b_offset = totsize;
3835 totsize += bsize;
3836 }
3837 last_extended_arg_count = extended_arg_count;
3838 extended_arg_count = 0;
3839 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3840 bsize = b->b_offset;
3841 for (i = 0; i < b->b_iused; i++) {
3842 struct instr *instr = &b->b_instr[i];
3843 /* Relative jumps are computed relative to
3844 the instruction pointer after fetching
3845 the jump instruction.
3846 */
3847 bsize += instrsize(instr);
3848 if (instr->i_jabs)
3849 instr->i_oparg = instr->i_target->b_offset;
3850 else if (instr->i_jrel) {
3851 int delta = instr->i_target->b_offset - bsize;
3852 instr->i_oparg = delta;
3853 }
3854 else
3855 continue;
3856 if (instr->i_oparg > 0xffff)
3857 extended_arg_count++;
3858 }
3859 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 /* XXX: This is an awful hack that could hurt performance, but
3862 on the bright side it should work until we come up
3863 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 The issue is that in the first loop blocksize() is called
3866 which calls instrsize() which requires i_oparg be set
3867 appropriately. There is a bootstrap problem because
3868 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00003869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 So we loop until we stop seeing new EXTENDED_ARGs.
3871 The only EXTENDED_ARGs that could be popping up are
3872 ones in jump instructions. So this should converge
3873 fairly quickly.
3874 */
3875 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003876}
3877
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003878static PyObject *
3879dict_keys_inorder(PyObject *dict, int offset)
3880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 PyObject *tuple, *k, *v;
3882 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 tuple = PyTuple_New(size);
3885 if (tuple == NULL)
3886 return NULL;
3887 while (PyDict_Next(dict, &pos, &k, &v)) {
3888 i = PyLong_AS_LONG(v);
3889 /* The keys of the dictionary are tuples. (see compiler_add_o)
3890 The object we want is always first, though. */
3891 k = PyTuple_GET_ITEM(k, 0);
3892 Py_INCREF(k);
3893 assert((i - offset) < size);
3894 assert((i - offset) >= 0);
3895 PyTuple_SET_ITEM(tuple, i - offset, k);
3896 }
3897 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003898}
3899
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003900static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003901compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 PySTEntryObject *ste = c->u->u_ste;
3904 int flags = 0, n;
3905 if (ste->ste_type != ModuleBlock)
3906 flags |= CO_NEWLOCALS;
3907 if (ste->ste_type == FunctionBlock) {
3908 if (!ste->ste_unoptimized)
3909 flags |= CO_OPTIMIZED;
3910 if (ste->ste_nested)
3911 flags |= CO_NESTED;
3912 if (ste->ste_generator)
3913 flags |= CO_GENERATOR;
3914 if (ste->ste_varargs)
3915 flags |= CO_VARARGS;
3916 if (ste->ste_varkeywords)
3917 flags |= CO_VARKEYWORDS;
3918 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 /* (Only) inherit compilerflags in PyCF_MASK */
3921 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00003922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 n = PyDict_Size(c->u->u_freevars);
3924 if (n < 0)
3925 return -1;
3926 if (n == 0) {
3927 n = PyDict_Size(c->u->u_cellvars);
3928 if (n < 0)
3929 return -1;
3930 if (n == 0) {
3931 flags |= CO_NOFREE;
3932 }
3933 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00003934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00003936}
3937
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938static PyCodeObject *
3939makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 PyObject *tmp;
3942 PyCodeObject *co = NULL;
3943 PyObject *consts = NULL;
3944 PyObject *names = NULL;
3945 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 PyObject *name = NULL;
3947 PyObject *freevars = NULL;
3948 PyObject *cellvars = NULL;
3949 PyObject *bytecode = NULL;
3950 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 tmp = dict_keys_inorder(c->u->u_consts, 0);
3953 if (!tmp)
3954 goto error;
3955 consts = PySequence_List(tmp); /* optimize_code requires a list */
3956 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 names = dict_keys_inorder(c->u->u_names, 0);
3959 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3960 if (!consts || !names || !varnames)
3961 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3964 if (!cellvars)
3965 goto error;
3966 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3967 if (!freevars)
3968 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 nlocals = PyDict_Size(c->u->u_varnames);
3970 flags = compute_code_flags(c);
3971 if (flags < 0)
3972 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3975 if (!bytecode)
3976 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3979 if (!tmp)
3980 goto error;
3981 Py_DECREF(consts);
3982 consts = tmp;
3983
3984 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3985 nlocals, stackdepth(c), flags,
3986 bytecode, consts, names, varnames,
3987 freevars, cellvars,
Benjamin Peterson43b06862011-05-27 09:08:01 -05003988 c->c_filename_obj, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 c->u->u_firstlineno,
3990 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 Py_XDECREF(consts);
3993 Py_XDECREF(names);
3994 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 Py_XDECREF(name);
3996 Py_XDECREF(freevars);
3997 Py_XDECREF(cellvars);
3998 Py_XDECREF(bytecode);
3999 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004000}
4001
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004002
4003/* For debugging purposes only */
4004#if 0
4005static void
4006dump_instr(const struct instr *i)
4007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 const char *jrel = i->i_jrel ? "jrel " : "";
4009 const char *jabs = i->i_jabs ? "jabs " : "";
4010 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 *arg = '\0';
4013 if (i->i_hasarg)
4014 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4017 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004018}
4019
4020static void
4021dump_basicblock(const basicblock *b)
4022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 const char *seen = b->b_seen ? "seen " : "";
4024 const char *b_return = b->b_return ? "return " : "";
4025 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4026 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4027 if (b->b_instr) {
4028 int i;
4029 for (i = 0; i < b->b_iused; i++) {
4030 fprintf(stderr, " [%02d] ", i);
4031 dump_instr(b->b_instr + i);
4032 }
4033 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004034}
4035#endif
4036
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004037static PyCodeObject *
4038assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 basicblock *b, *entryblock;
4041 struct assembler a;
4042 int i, j, nblocks;
4043 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 /* Make sure every block that falls off the end returns None.
4046 XXX NEXT_BLOCK() isn't quite right, because if the last
4047 block ends with a jump or return b_next shouldn't set.
4048 */
4049 if (!c->u->u_curblock->b_return) {
4050 NEXT_BLOCK(c);
4051 if (addNone)
4052 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4053 ADDOP(c, RETURN_VALUE);
4054 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 nblocks = 0;
4057 entryblock = NULL;
4058 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4059 nblocks++;
4060 entryblock = b;
4061 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 /* Set firstlineno if it wasn't explicitly set. */
4064 if (!c->u->u_firstlineno) {
4065 if (entryblock && entryblock->b_instr)
4066 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4067 else
4068 c->u->u_firstlineno = 1;
4069 }
4070 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4071 goto error;
4072 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 /* Can't modify the bytecode after computing jump offsets. */
4075 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 /* Emit code in reverse postorder from dfs. */
4078 for (i = a.a_nblocks - 1; i >= 0; i--) {
4079 b = a.a_postorder[i];
4080 for (j = 0; j < b->b_iused; j++)
4081 if (!assemble_emit(&a, &b->b_instr[j]))
4082 goto error;
4083 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4086 goto error;
4087 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4088 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004091 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 assemble_free(&a);
4093 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004094}
Georg Brandl8334fd92010-12-04 10:26:46 +00004095
4096#undef PyAST_Compile
4097PyAPI_FUNC(PyCodeObject *)
4098PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4099 PyArena *arena)
4100{
4101 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4102}
4103
4104