blob: 0aca8bd2e9ca6f93c9907e5c569dfa2be2734713 [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
Antoine Pitrou86a36b52011-11-25 18:56:07 +010093enum {
94 COMPILER_SCOPE_MODULE,
95 COMPILER_SCOPE_CLASS,
96 COMPILER_SCOPE_FUNCTION,
97 COMPILER_SCOPE_COMPREHENSION,
98};
99
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000100/* The following items change on entry and exit of code blocks.
101 They must be saved and restored when returning to a block.
102*/
103struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyObject *u_name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100107 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
108 int u_scope_type;
109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 /* The following fields are dicts that map objects to
111 the index of them in co_XXX. The index is used as
112 the argument for opcodes that refer to those collections.
113 */
114 PyObject *u_consts; /* all constants */
115 PyObject *u_names; /* all names */
116 PyObject *u_varnames; /* local variables */
117 PyObject *u_cellvars; /* cell variables */
118 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 int u_argcount; /* number of arguments for block */
123 int u_kwonlyargcount; /* number of keyword only arguments for block */
124 /* Pointer to the most recently allocated block. By following b_list
125 members, you can reach all early allocated blocks. */
126 basicblock *u_blocks;
127 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 int u_nfblocks;
130 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int u_firstlineno; /* the first lineno of the block */
133 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000134 int u_col_offset; /* the offset of the current stmt */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 int u_lineno_set; /* boolean to indicate whether instr
136 has been generated with current lineno */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137};
138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000141The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000143managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000144
145Note that we don't track recursion levels during compilation - the
146task of detecting and rejecting excessive levels of nesting is
147handled by the symbol analysis pass.
148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149*/
150
151struct compiler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 const char *c_filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500153 PyObject *c_filename_obj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 struct symtable *c_st;
155 PyFutureFeatures *c_future; /* pointer to module's __future__ */
156 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000157
Georg Brandl8334fd92010-12-04 10:26:46 +0000158 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 int c_interactive; /* true if in interactive mode */
160 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 struct compiler_unit *u; /* compiler state for current block */
163 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
164 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000165};
166
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100167static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168static void compiler_free(struct compiler *);
169static basicblock *compiler_new_block(struct compiler *);
170static int compiler_next_instr(struct compiler *, basicblock *);
171static int compiler_addop(struct compiler *, int);
172static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
173static int compiler_addop_i(struct compiler *, int, int);
174static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000175static basicblock *compiler_use_new_block(struct compiler *);
176static int compiler_error(struct compiler *, const char *);
177static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
178
179static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
180static int compiler_visit_stmt(struct compiler *, stmt_ty);
181static int compiler_visit_keyword(struct compiler *, keyword_ty);
182static int compiler_visit_expr(struct compiler *, expr_ty);
183static int compiler_augassign(struct compiler *, stmt_ty);
184static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000185 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186
187static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000191/* Returns true if there is a loop on the fblock stack. */
192static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194static int inplace_binop(struct compiler *, operator_ty);
Georg Brandl8334fd92010-12-04 10:26:46 +0000195static int expr_constant(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500197static int compiler_with(struct compiler *, stmt_ty, int);
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000198static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 asdl_seq *args,
200 asdl_seq *keywords,
201 expr_ty starargs,
202 expr_ty kwargs);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500203static int compiler_try_except(struct compiler *, stmt_ty);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205static PyCodeObject *assemble(struct compiler *, int addNone);
206static PyObject *__doc__;
207
Benjamin Petersonb173f782009-05-05 22:31:58 +0000208#define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
209
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 /* Name mangling: __private becomes _classname__private.
214 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200215 PyObject *result;
216 size_t nlen, plen, ipriv;
217 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200219 PyUnicode_READ_CHAR(ident, 0) != '_' ||
220 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_INCREF(ident);
222 return ident;
223 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200224 nlen = PyUnicode_GET_LENGTH(ident);
225 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 The only time a name with a dot can occur is when
229 we are compiling an import statement that has a
230 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 TODO(jhylton): Decide whether we want to support
233 mangling of the module name, e.g. __M.X.
234 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200235 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
236 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
237 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 Py_INCREF(ident);
239 return ident; /* Don't mangle __whatever__ */
240 }
241 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200242 ipriv = 0;
243 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
244 ipriv++;
245 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 Py_INCREF(ident);
247 return ident; /* Don't mangle if class is just underscores */
248 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200249 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 assert(1 <= PY_SSIZE_T_MAX - nlen);
252 assert(1 + nlen <= PY_SSIZE_T_MAX - plen);
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000253
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200254 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
255 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
256 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
257
258 result = PyUnicode_New(1 + nlen + plen, maxchar);
259 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200261 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
262 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200263 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
264 Py_DECREF(result);
265 return NULL;
266 }
267 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
268 Py_DECREF(result);
269 return NULL;
270 }
Victor Stinner8f825062012-04-27 13:55:39 +0200271 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200272 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000273}
274
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000275static int
276compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 c->c_stack = PyList_New(0);
281 if (!c->c_stack)
282 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285}
286
287PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +0000288PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags,
289 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 struct compiler c;
292 PyCodeObject *co = NULL;
293 PyCompilerFlags local_flags;
294 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (!__doc__) {
297 __doc__ = PyUnicode_InternFromString("__doc__");
298 if (!__doc__)
299 return NULL;
300 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 if (!compiler_init(&c))
303 return NULL;
304 c.c_filename = filename;
Benjamin Peterson43b06862011-05-27 09:08:01 -0500305 c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
306 if (!c.c_filename_obj)
307 goto finally;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 c.c_arena = arena;
309 c.c_future = PyFuture_FromAST(mod, filename);
310 if (c.c_future == NULL)
311 goto finally;
312 if (!flags) {
313 local_flags.cf_flags = 0;
314 flags = &local_flags;
315 }
316 merged = c.c_future->ff_features | flags->cf_flags;
317 c.c_future->ff_features = merged;
318 flags->cf_flags = merged;
319 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000320 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 c.c_st = PySymtable_Build(mod, filename, c.c_future);
324 if (c.c_st == NULL) {
325 if (!PyErr_Occurred())
326 PyErr_SetString(PyExc_SystemError, "no symtable");
327 goto finally;
328 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331
Thomas Wouters1175c432006-02-27 22:49:54 +0000332 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 compiler_free(&c);
334 assert(co || PyErr_Occurred());
335 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336}
337
338PyCodeObject *
339PyNode_Compile(struct _node *n, const char *filename)
340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 PyCodeObject *co = NULL;
342 mod_ty mod;
343 PyArena *arena = PyArena_New();
344 if (!arena)
345 return NULL;
346 mod = PyAST_FromNode(n, NULL, filename, arena);
347 if (mod)
348 co = PyAST_Compile(mod, filename, NULL, arena);
349 PyArena_Free(arena);
350 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000351}
352
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000353static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 if (c->c_st)
357 PySymtable_Free(c->c_st);
358 if (c->c_future)
359 PyObject_Free(c->c_future);
Benjamin Peterson43b06862011-05-27 09:08:01 -0500360 if (c->c_filename_obj)
361 Py_DECREF(c->c_filename_obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000362 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000363}
364
Guido van Rossum79f25d91997-04-29 20:08:16 +0000365static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 Py_ssize_t i, n;
369 PyObject *v, *k;
370 PyObject *dict = PyDict_New();
371 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 n = PyList_Size(list);
374 for (i = 0; i < n; i++) {
375 v = PyLong_FromLong(i);
376 if (!v) {
377 Py_DECREF(dict);
378 return NULL;
379 }
380 k = PyList_GET_ITEM(list, i);
381 k = PyTuple_Pack(2, k, k->ob_type);
382 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
383 Py_XDECREF(k);
384 Py_DECREF(v);
385 Py_DECREF(dict);
386 return NULL;
387 }
388 Py_DECREF(k);
389 Py_DECREF(v);
390 }
391 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392}
393
394/* Return new dict containing names from src that match scope(s).
395
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000396src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000398values are integers, starting at offset and increasing by one for
399each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400*/
401
402static PyObject *
403dictbytype(PyObject *src, int scope_type, int flag, int offset)
404{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700405 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000406 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500407 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 assert(offset >= 0);
410 if (dest == NULL)
411 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412
Meador Inge2ca63152012-07-18 14:20:11 -0500413 /* Sort the keys so that we have a deterministic order on the indexes
414 saved in the returned dictionary. These indexes are used as indexes
415 into the free and cell var storage. Therefore if they aren't
416 deterministic, then the generated bytecode is not deterministic.
417 */
418 sorted_keys = PyDict_Keys(src);
419 if (sorted_keys == NULL)
420 return NULL;
421 if (PyList_Sort(sorted_keys) != 0) {
422 Py_DECREF(sorted_keys);
423 return NULL;
424 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500425 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500426
427 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 /* XXX this should probably be a macro in symtable.h */
429 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500430 k = PyList_GET_ITEM(sorted_keys, key_i);
431 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 assert(PyLong_Check(v));
433 vi = PyLong_AS_LONG(v);
434 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (scope == scope_type || vi & flag) {
437 PyObject *tuple, *item = PyLong_FromLong(i);
438 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500439 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 Py_DECREF(dest);
441 return NULL;
442 }
443 i++;
444 tuple = PyTuple_Pack(2, k, k->ob_type);
445 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500446 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_DECREF(item);
448 Py_DECREF(dest);
449 Py_XDECREF(tuple);
450 return NULL;
451 }
452 Py_DECREF(item);
453 Py_DECREF(tuple);
454 }
455 }
Meador Inge2ca63152012-07-18 14:20:11 -0500456 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000458}
459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460static void
461compiler_unit_check(struct compiler_unit *u)
462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 basicblock *block;
464 for (block = u->u_blocks; block != NULL; block = block->b_list) {
465 assert((void *)block != (void *)0xcbcbcbcb);
466 assert((void *)block != (void *)0xfbfbfbfb);
467 assert((void *)block != (void *)0xdbdbdbdb);
468 if (block->b_instr != NULL) {
469 assert(block->b_ialloc > 0);
470 assert(block->b_iused > 0);
471 assert(block->b_ialloc >= block->b_iused);
472 }
473 else {
474 assert (block->b_iused == 0);
475 assert (block->b_ialloc == 0);
476 }
477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478}
479
480static void
481compiler_unit_free(struct compiler_unit *u)
482{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000483 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 compiler_unit_check(u);
486 b = u->u_blocks;
487 while (b != NULL) {
488 if (b->b_instr)
489 PyObject_Free((void *)b->b_instr);
490 next = b->b_list;
491 PyObject_Free((void *)b);
492 b = next;
493 }
494 Py_CLEAR(u->u_ste);
495 Py_CLEAR(u->u_name);
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100496 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 Py_CLEAR(u->u_consts);
498 Py_CLEAR(u->u_names);
499 Py_CLEAR(u->u_varnames);
500 Py_CLEAR(u->u_freevars);
501 Py_CLEAR(u->u_cellvars);
502 Py_CLEAR(u->u_private);
503 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000504}
505
506static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100507compiler_enter_scope(struct compiler *c, identifier name,
508 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
513 struct compiler_unit));
514 if (!u) {
515 PyErr_NoMemory();
516 return 0;
517 }
518 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100519 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 u->u_argcount = 0;
521 u->u_kwonlyargcount = 0;
522 u->u_ste = PySymtable_Lookup(c->c_st, key);
523 if (!u->u_ste) {
524 compiler_unit_free(u);
525 return 0;
526 }
527 Py_INCREF(name);
528 u->u_name = name;
529 u->u_varnames = list2dict(u->u_ste->ste_varnames);
530 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
531 if (!u->u_varnames || !u->u_cellvars) {
532 compiler_unit_free(u);
533 return 0;
534 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
537 PyDict_Size(u->u_cellvars));
538 if (!u->u_freevars) {
539 compiler_unit_free(u);
540 return 0;
541 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 u->u_blocks = NULL;
544 u->u_nfblocks = 0;
545 u->u_firstlineno = lineno;
546 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000547 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 u->u_lineno_set = 0;
549 u->u_consts = PyDict_New();
550 if (!u->u_consts) {
551 compiler_unit_free(u);
552 return 0;
553 }
554 u->u_names = PyDict_New();
555 if (!u->u_names) {
556 compiler_unit_free(u);
557 return 0;
558 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* Push the old compiler_unit on the stack. */
563 if (c->u) {
564 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_UNIT, NULL);
565 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
566 Py_XDECREF(capsule);
567 compiler_unit_free(u);
568 return 0;
569 }
570 Py_DECREF(capsule);
571 u->u_private = c->u->u_private;
572 Py_XINCREF(u->u_private);
573 }
574 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 c->c_nestlevel++;
577 if (compiler_use_new_block(c) == NULL)
578 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581}
582
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000583static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000584compiler_exit_scope(struct compiler *c)
585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 int n;
587 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 c->c_nestlevel--;
590 compiler_unit_free(c->u);
591 /* Restore c->u to the parent unit. */
592 n = PyList_GET_SIZE(c->c_stack) - 1;
593 if (n >= 0) {
594 capsule = PyList_GET_ITEM(c->c_stack, n);
595 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
596 assert(c->u);
597 /* we are deleting from a list so this really shouldn't fail */
598 if (PySequence_DelItem(c->c_stack, n) < 0)
599 Py_FatalError("compiler_exit_scope()");
600 compiler_unit_check(c->u);
601 }
602 else
603 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605}
606
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100607static PyObject *
608compiler_scope_qualname(struct compiler *c)
609{
610 Py_ssize_t stack_size, i;
611 _Py_static_string(dot, ".");
612 _Py_static_string(locals, "<locals>");
613 struct compiler_unit *u;
614 PyObject *capsule, *name, *seq, *dot_str, *locals_str;
615
616 u = c->u;
617 if (u->u_qualname != NULL) {
618 Py_INCREF(u->u_qualname);
619 return u->u_qualname;
620 }
621
622 seq = PyList_New(0);
623 if (seq == NULL)
624 return NULL;
625
626 stack_size = PyList_GET_SIZE(c->c_stack);
627 for (i = 0; i < stack_size; i++) {
628 capsule = PyList_GET_ITEM(c->c_stack, i);
629 u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSULE_NAME_COMPILER_UNIT);
630 assert(u);
631 if (u->u_scope_type == COMPILER_SCOPE_MODULE)
632 continue;
633 if (PyList_Append(seq, u->u_name))
634 goto _error;
635 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION) {
636 locals_str = _PyUnicode_FromId(&locals);
637 if (locals_str == NULL)
638 goto _error;
639 if (PyList_Append(seq, locals_str))
640 goto _error;
641 }
642 }
643 u = c->u;
644 if (PyList_Append(seq, u->u_name))
645 goto _error;
646 dot_str = _PyUnicode_FromId(&dot);
647 if (dot_str == NULL)
648 goto _error;
649 name = PyUnicode_Join(dot_str, seq);
650 Py_DECREF(seq);
651 u->u_qualname = name;
652 Py_XINCREF(name);
653 return name;
654
655_error:
656 Py_XDECREF(seq);
657 return NULL;
658}
659
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000660/* Allocate a new block and return a pointer to it.
661 Returns NULL on error.
662*/
663
664static basicblock *
665compiler_new_block(struct compiler *c)
666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 basicblock *b;
668 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 u = c->u;
671 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
672 if (b == NULL) {
673 PyErr_NoMemory();
674 return NULL;
675 }
676 memset((void *)b, 0, sizeof(basicblock));
677 /* Extend the singly linked list of blocks with new block. */
678 b->b_list = u->u_blocks;
679 u->u_blocks = b;
680 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000681}
682
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000683static basicblock *
684compiler_use_new_block(struct compiler *c)
685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 basicblock *block = compiler_new_block(c);
687 if (block == NULL)
688 return NULL;
689 c->u->u_curblock = block;
690 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000691}
692
693static basicblock *
694compiler_next_block(struct compiler *c)
695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 basicblock *block = compiler_new_block(c);
697 if (block == NULL)
698 return NULL;
699 c->u->u_curblock->b_next = block;
700 c->u->u_curblock = block;
701 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702}
703
704static basicblock *
705compiler_use_next_block(struct compiler *c, basicblock *block)
706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 assert(block != NULL);
708 c->u->u_curblock->b_next = block;
709 c->u->u_curblock = block;
710 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711}
712
713/* Returns the offset of the next instruction in the current block's
714 b_instr array. Resizes the b_instr as necessary.
715 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000716*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000717
718static int
719compiler_next_instr(struct compiler *c, basicblock *b)
720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 assert(b != NULL);
722 if (b->b_instr == NULL) {
723 b->b_instr = (struct instr *)PyObject_Malloc(
724 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
725 if (b->b_instr == NULL) {
726 PyErr_NoMemory();
727 return -1;
728 }
729 b->b_ialloc = DEFAULT_BLOCK_SIZE;
730 memset((char *)b->b_instr, 0,
731 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
732 }
733 else if (b->b_iused == b->b_ialloc) {
734 struct instr *tmp;
735 size_t oldsize, newsize;
736 oldsize = b->b_ialloc * sizeof(struct instr);
737 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739 if (oldsize > (PY_SIZE_MAX >> 1)) {
740 PyErr_NoMemory();
741 return -1;
742 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 if (newsize == 0) {
745 PyErr_NoMemory();
746 return -1;
747 }
748 b->b_ialloc <<= 1;
749 tmp = (struct instr *)PyObject_Realloc(
750 (void *)b->b_instr, newsize);
751 if (tmp == NULL) {
752 PyErr_NoMemory();
753 return -1;
754 }
755 b->b_instr = tmp;
756 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
757 }
758 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759}
760
Christian Heimes2202f872008-02-06 14:31:34 +0000761/* Set the i_lineno member of the instruction at offset off if the
762 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000763 already been set. If it has been set, the call has no effect.
764
Christian Heimes2202f872008-02-06 14:31:34 +0000765 The line number is reset in the following cases:
766 - when entering a new scope
767 - on each statement
768 - on each expression that start a new line
769 - before the "except" clause
770 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000771*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000772
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773static void
774compiler_set_lineno(struct compiler *c, int off)
775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 basicblock *b;
777 if (c->u->u_lineno_set)
778 return;
779 c->u->u_lineno_set = 1;
780 b = c->u->u_curblock;
781 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782}
783
784static int
785opcode_stack_effect(int opcode, int oparg)
786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787 switch (opcode) {
788 case POP_TOP:
789 return -1;
790 case ROT_TWO:
791 case ROT_THREE:
792 return 0;
793 case DUP_TOP:
794 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000795 case DUP_TOP_TWO:
796 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 case UNARY_POSITIVE:
799 case UNARY_NEGATIVE:
800 case UNARY_NOT:
801 case UNARY_INVERT:
802 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000803
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 case SET_ADD:
805 case LIST_APPEND:
806 return -1;
807 case MAP_ADD:
808 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 case BINARY_POWER:
811 case BINARY_MULTIPLY:
812 case BINARY_MODULO:
813 case BINARY_ADD:
814 case BINARY_SUBTRACT:
815 case BINARY_SUBSCR:
816 case BINARY_FLOOR_DIVIDE:
817 case BINARY_TRUE_DIVIDE:
818 return -1;
819 case INPLACE_FLOOR_DIVIDE:
820 case INPLACE_TRUE_DIVIDE:
821 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 case INPLACE_ADD:
824 case INPLACE_SUBTRACT:
825 case INPLACE_MULTIPLY:
826 case INPLACE_MODULO:
827 return -1;
828 case STORE_SUBSCR:
829 return -3;
830 case STORE_MAP:
831 return -2;
832 case DELETE_SUBSCR:
833 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 case BINARY_LSHIFT:
836 case BINARY_RSHIFT:
837 case BINARY_AND:
838 case BINARY_XOR:
839 case BINARY_OR:
840 return -1;
841 case INPLACE_POWER:
842 return -1;
843 case GET_ITER:
844 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 case PRINT_EXPR:
847 return -1;
848 case LOAD_BUILD_CLASS:
849 return 1;
850 case INPLACE_LSHIFT:
851 case INPLACE_RSHIFT:
852 case INPLACE_AND:
853 case INPLACE_XOR:
854 case INPLACE_OR:
855 return -1;
856 case BREAK_LOOP:
857 return 0;
858 case SETUP_WITH:
859 return 7;
860 case WITH_CLEANUP:
861 return -1; /* XXX Sometimes more */
862 case STORE_LOCALS:
863 return -1;
864 case RETURN_VALUE:
865 return -1;
866 case IMPORT_STAR:
867 return -1;
868 case YIELD_VALUE:
869 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500870 case YIELD_FROM:
871 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 case POP_BLOCK:
873 return 0;
874 case POP_EXCEPT:
875 return 0; /* -3 except if bad bytecode */
876 case END_FINALLY:
877 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 case STORE_NAME:
880 return -1;
881 case DELETE_NAME:
882 return 0;
883 case UNPACK_SEQUENCE:
884 return oparg-1;
885 case UNPACK_EX:
886 return (oparg&0xFF) + (oparg>>8);
887 case FOR_ITER:
888 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 case STORE_ATTR:
891 return -2;
892 case DELETE_ATTR:
893 return -1;
894 case STORE_GLOBAL:
895 return -1;
896 case DELETE_GLOBAL:
897 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 case LOAD_CONST:
899 return 1;
900 case LOAD_NAME:
901 return 1;
902 case BUILD_TUPLE:
903 case BUILD_LIST:
904 case BUILD_SET:
905 return 1-oparg;
906 case BUILD_MAP:
907 return 1;
908 case LOAD_ATTR:
909 return 0;
910 case COMPARE_OP:
911 return -1;
912 case IMPORT_NAME:
913 return -1;
914 case IMPORT_FROM:
915 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 case JUMP_FORWARD:
918 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
919 case JUMP_IF_FALSE_OR_POP: /* "" */
920 case JUMP_ABSOLUTE:
921 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 case POP_JUMP_IF_FALSE:
924 case POP_JUMP_IF_TRUE:
925 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 case LOAD_GLOBAL:
928 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 case CONTINUE_LOOP:
931 return 0;
932 case SETUP_LOOP:
933 return 0;
934 case SETUP_EXCEPT:
935 case SETUP_FINALLY:
936 return 6; /* can push 3 values for the new exception
937 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 case LOAD_FAST:
940 return 1;
941 case STORE_FAST:
942 return -1;
943 case DELETE_FAST:
944 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 case RAISE_VARARGS:
947 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +0000948#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 case CALL_FUNCTION:
950 return -NARGS(oparg);
951 case CALL_FUNCTION_VAR:
952 case CALL_FUNCTION_KW:
953 return -NARGS(oparg)-1;
954 case CALL_FUNCTION_VAR_KW:
955 return -NARGS(oparg)-2;
956 case MAKE_FUNCTION:
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100957 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 case MAKE_CLOSURE:
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100959 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +0000960#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 case BUILD_SLICE:
962 if (oparg == 3)
963 return -2;
964 else
965 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 case LOAD_CLOSURE:
968 return 1;
969 case LOAD_DEREF:
970 return 1;
971 case STORE_DEREF:
972 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000973 case DELETE_DEREF:
974 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 default:
976 fprintf(stderr, "opcode = %d\n", opcode);
977 Py_FatalError("opcode_stack_effect()");
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 }
980 return 0; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981}
982
983/* Add an opcode with no argument.
984 Returns 0 on failure, 1 on success.
985*/
986
987static int
988compiler_addop(struct compiler *c, int opcode)
989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 basicblock *b;
991 struct instr *i;
992 int off;
993 off = compiler_next_instr(c, c->u->u_curblock);
994 if (off < 0)
995 return 0;
996 b = c->u->u_curblock;
997 i = &b->b_instr[off];
998 i->i_opcode = opcode;
999 i->i_hasarg = 0;
1000 if (opcode == RETURN_VALUE)
1001 b->b_return = 1;
1002 compiler_set_lineno(c, off);
1003 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004}
1005
1006static int
1007compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 PyObject *t, *v;
1010 Py_ssize_t arg;
1011 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 /* necessary to make sure types aren't coerced (e.g., int and long) */
1014 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
1015 if (PyFloat_Check(o)) {
1016 d = PyFloat_AS_DOUBLE(o);
1017 /* all we need is to make the tuple different in either the 0.0
1018 * or -0.0 case from all others, just to avoid the "coercion".
1019 */
1020 if (d == 0.0 && copysign(1.0, d) < 0.0)
1021 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1022 else
1023 t = PyTuple_Pack(2, o, o->ob_type);
1024 }
1025 else if (PyComplex_Check(o)) {
1026 Py_complex z;
1027 int real_negzero, imag_negzero;
1028 /* For the complex case we must make complex(x, 0.)
1029 different from complex(x, -0.) and complex(0., y)
1030 different from complex(-0., y), for any x and y.
1031 All four complex zeros must be distinguished.*/
1032 z = PyComplex_AsCComplex(o);
1033 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1034 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1035 if (real_negzero && imag_negzero) {
1036 t = PyTuple_Pack(5, o, o->ob_type,
1037 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +00001038 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 else if (imag_negzero) {
1040 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +00001041 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 else if (real_negzero) {
1043 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1044 }
1045 else {
1046 t = PyTuple_Pack(2, o, o->ob_type);
1047 }
1048 }
1049 else {
1050 t = PyTuple_Pack(2, o, o->ob_type);
1051 }
1052 if (t == NULL)
1053 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 v = PyDict_GetItem(dict, t);
1056 if (!v) {
1057 if (PyErr_Occurred())
1058 return -1;
1059 arg = PyDict_Size(dict);
1060 v = PyLong_FromLong(arg);
1061 if (!v) {
1062 Py_DECREF(t);
1063 return -1;
1064 }
1065 if (PyDict_SetItem(dict, t, v) < 0) {
1066 Py_DECREF(t);
1067 Py_DECREF(v);
1068 return -1;
1069 }
1070 Py_DECREF(v);
1071 }
1072 else
1073 arg = PyLong_AsLong(v);
1074 Py_DECREF(t);
1075 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076}
1077
1078static int
1079compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081{
1082 int arg = compiler_add_o(c, dict, o);
1083 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001084 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085 return compiler_addop_i(c, opcode, arg);
1086}
1087
1088static int
1089compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001091{
1092 int arg;
1093 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1094 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001095 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 arg = compiler_add_o(c, dict, mangled);
1097 Py_DECREF(mangled);
1098 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001099 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001100 return compiler_addop_i(c, opcode, arg);
1101}
1102
1103/* Add an opcode with an integer argument.
1104 Returns 0 on failure, 1 on success.
1105*/
1106
1107static int
1108compiler_addop_i(struct compiler *c, int opcode, int oparg)
1109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001110 struct instr *i;
1111 int off;
1112 off = compiler_next_instr(c, c->u->u_curblock);
1113 if (off < 0)
1114 return 0;
1115 i = &c->u->u_curblock->b_instr[off];
1116 i->i_opcode = opcode;
1117 i->i_oparg = oparg;
1118 i->i_hasarg = 1;
1119 compiler_set_lineno(c, off);
1120 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121}
1122
1123static int
1124compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 struct instr *i;
1127 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 assert(b != NULL);
1130 off = compiler_next_instr(c, c->u->u_curblock);
1131 if (off < 0)
1132 return 0;
1133 i = &c->u->u_curblock->b_instr[off];
1134 i->i_opcode = opcode;
1135 i->i_target = b;
1136 i->i_hasarg = 1;
1137 if (absolute)
1138 i->i_jabs = 1;
1139 else
1140 i->i_jrel = 1;
1141 compiler_set_lineno(c, off);
1142 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143}
1144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1146 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 it as the current block. NEXT_BLOCK() also creates an implicit jump
1148 from the current block to the new block.
1149*/
1150
Thomas Wouters89f507f2006-12-13 04:49:30 +00001151/* The returns inside these macros make it impossible to decref objects
1152 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153*/
1154
1155
1156#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 if (compiler_use_new_block((C)) == NULL) \
1158 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001159}
1160
1161#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 if (compiler_next_block((C)) == NULL) \
1163 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164}
1165
1166#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (!compiler_addop((C), (OP))) \
1168 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169}
1170
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001171#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (!compiler_addop((C), (OP))) { \
1173 compiler_exit_scope(c); \
1174 return 0; \
1175 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001176}
1177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1180 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001181}
1182
1183#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1185 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001186}
1187
1188#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (!compiler_addop_i((C), (OP), (O))) \
1190 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001191}
1192
1193#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (!compiler_addop_j((C), (OP), (O), 1)) \
1195 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001196}
1197
1198#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (!compiler_addop_j((C), (OP), (O), 0)) \
1200 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201}
1202
1203/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1204 the ASDL name to synthesize the name of the C type and the visit function.
1205*/
1206
1207#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (!compiler_visit_ ## TYPE((C), (V))) \
1209 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001210}
1211
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001212#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (!compiler_visit_ ## TYPE((C), (V))) { \
1214 compiler_exit_scope(c); \
1215 return 0; \
1216 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001217}
1218
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 if (!compiler_visit_slice((C), (V), (CTX))) \
1221 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001222}
1223
1224#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 int _i; \
1226 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1227 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1228 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1229 if (!compiler_visit_ ## TYPE((C), elt)) \
1230 return 0; \
1231 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232}
1233
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001234#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 int _i; \
1236 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1237 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1238 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1239 if (!compiler_visit_ ## TYPE((C), elt)) { \
1240 compiler_exit_scope(c); \
1241 return 0; \
1242 } \
1243 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001244}
1245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001246static int
1247compiler_isdocstring(stmt_ty s)
1248{
1249 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001250 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251 return s->v.Expr.value->kind == Str_kind;
1252}
1253
1254/* Compile a sequence of statements, checking for a docstring. */
1255
1256static int
1257compiler_body(struct compiler *c, asdl_seq *stmts)
1258{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 int i = 0;
1260 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (!asdl_seq_LEN(stmts))
1263 return 1;
1264 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001265 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 /* don't generate docstrings if -OO */
1267 i = 1;
1268 VISIT(c, expr, st->v.Expr.value);
1269 if (!compiler_nameop(c, __doc__, Store))
1270 return 0;
1271 }
1272 for (; i < asdl_seq_LEN(stmts); i++)
1273 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1274 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001275}
1276
1277static PyCodeObject *
1278compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 PyCodeObject *co;
1281 int addNone = 1;
1282 static PyObject *module;
1283 if (!module) {
1284 module = PyUnicode_InternFromString("<module>");
1285 if (!module)
1286 return NULL;
1287 }
1288 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001289 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 return NULL;
1291 switch (mod->kind) {
1292 case Module_kind:
1293 if (!compiler_body(c, mod->v.Module.body)) {
1294 compiler_exit_scope(c);
1295 return 0;
1296 }
1297 break;
1298 case Interactive_kind:
1299 c->c_interactive = 1;
1300 VISIT_SEQ_IN_SCOPE(c, stmt,
1301 mod->v.Interactive.body);
1302 break;
1303 case Expression_kind:
1304 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1305 addNone = 0;
1306 break;
1307 case Suite_kind:
1308 PyErr_SetString(PyExc_SystemError,
1309 "suite should not be possible");
1310 return 0;
1311 default:
1312 PyErr_Format(PyExc_SystemError,
1313 "module kind %d should not be possible",
1314 mod->kind);
1315 return 0;
1316 }
1317 co = assemble(c, addNone);
1318 compiler_exit_scope(c);
1319 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001320}
1321
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322/* The test for LOCAL must come before the test for FREE in order to
1323 handle classes where name is both local and free. The local var is
1324 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001325*/
1326
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001327static int
1328get_ref_type(struct compiler *c, PyObject *name)
1329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 int scope = PyST_GetScope(c->u->u_ste, name);
1331 if (scope == 0) {
1332 char buf[350];
1333 PyOS_snprintf(buf, sizeof(buf),
1334 "unknown scope for %.100s in %.100s(%s) in %s\n"
1335 "symbols: %s\nlocals: %s\nglobals: %s",
1336 PyBytes_AS_STRING(name),
1337 PyBytes_AS_STRING(c->u->u_name),
1338 PyObject_REPR(c->u->u_ste->ste_id),
1339 c->c_filename,
1340 PyObject_REPR(c->u->u_ste->ste_symbols),
1341 PyObject_REPR(c->u->u_varnames),
1342 PyObject_REPR(c->u->u_names)
1343 );
1344 Py_FatalError(buf);
1345 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348}
1349
1350static int
1351compiler_lookup_arg(PyObject *dict, PyObject *name)
1352{
1353 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001354 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001356 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001358 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001360 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001361 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362}
1363
1364static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001365compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 int i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001368 if (qualname == NULL)
1369 qualname = co->co_name;
1370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 if (free == 0) {
1372 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001373 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 ADDOP_I(c, MAKE_FUNCTION, args);
1375 return 1;
1376 }
1377 for (i = 0; i < free; ++i) {
1378 /* Bypass com_addop_varname because it will generate
1379 LOAD_DEREF but LOAD_CLOSURE is needed.
1380 */
1381 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1382 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 /* Special case: If a class contains a method with a
1385 free variable that has the same name as a method,
1386 the name will be considered free *and* local in the
1387 class. It should be handled by the closure, as
1388 well as by the normal name loookup logic.
1389 */
1390 reftype = get_ref_type(c, name);
1391 if (reftype == CELL)
1392 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1393 else /* (reftype == FREE) */
1394 arg = compiler_lookup_arg(c->u->u_freevars, name);
1395 if (arg == -1) {
1396 fprintf(stderr,
1397 "lookup %s in %s %d %d\n"
1398 "freevars of %s: %s\n",
1399 PyObject_REPR(name),
1400 PyBytes_AS_STRING(c->u->u_name),
1401 reftype, arg,
1402 _PyUnicode_AsString(co->co_name),
1403 PyObject_REPR(co->co_freevars));
1404 Py_FatalError("compiler_make_closure()");
1405 }
1406 ADDOP_I(c, LOAD_CLOSURE, arg);
1407 }
1408 ADDOP_I(c, BUILD_TUPLE, free);
1409 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001410 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 ADDOP_I(c, MAKE_CLOSURE, args);
1412 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413}
1414
1415static int
1416compiler_decorators(struct compiler *c, asdl_seq* decos)
1417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 if (!decos)
1421 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1424 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1425 }
1426 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427}
1428
1429static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001430compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 int i, default_count = 0;
1434 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1435 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1436 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1437 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001438 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1439 if (!mangled)
1440 return -1;
1441 ADDOP_O(c, LOAD_CONST, mangled, consts);
1442 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443 if (!compiler_visit_expr(c, default_)) {
1444 return -1;
1445 }
1446 default_count++;
1447 }
1448 }
1449 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001450}
1451
1452static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001453compiler_visit_argannotation(struct compiler *c, identifier id,
1454 expr_ty annotation, PyObject *names)
1455{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 if (annotation) {
1457 VISIT(c, expr, annotation);
1458 if (PyList_Append(names, id))
1459 return -1;
1460 }
1461 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001462}
1463
1464static int
1465compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1466 PyObject *names)
1467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 int i, error;
1469 for (i = 0; i < asdl_seq_LEN(args); i++) {
1470 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1471 error = compiler_visit_argannotation(
1472 c,
1473 arg->arg,
1474 arg->annotation,
1475 names);
1476 if (error)
1477 return error;
1478 }
1479 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001480}
1481
1482static int
1483compiler_visit_annotations(struct compiler *c, arguments_ty args,
1484 expr_ty returns)
1485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 /* Push arg annotations and a list of the argument names. Return the #
1487 of items pushed. The expressions are evaluated out-of-order wrt the
1488 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1491 */
1492 static identifier return_str;
1493 PyObject *names;
1494 int len;
1495 names = PyList_New(0);
1496 if (!names)
1497 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 if (compiler_visit_argannotations(c, args->args, names))
1500 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001501 if (args->vararg && args->vararg->annotation &&
1502 compiler_visit_argannotation(c, args->vararg->arg,
1503 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 goto error;
1505 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1506 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001507 if (args->kwarg && args->kwarg->annotation &&
1508 compiler_visit_argannotation(c, args->kwarg->arg,
1509 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 if (!return_str) {
1513 return_str = PyUnicode_InternFromString("return");
1514 if (!return_str)
1515 goto error;
1516 }
1517 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1518 goto error;
1519 }
1520
1521 len = PyList_GET_SIZE(names);
1522 if (len > 65534) {
1523 /* len must fit in 16 bits, and len is incremented below */
1524 PyErr_SetString(PyExc_SyntaxError,
1525 "too many annotations");
1526 goto error;
1527 }
1528 if (len) {
1529 /* convert names to a tuple and place on stack */
1530 PyObject *elt;
1531 int i;
1532 PyObject *s = PyTuple_New(len);
1533 if (!s)
1534 goto error;
1535 for (i = 0; i < len; i++) {
1536 elt = PyList_GET_ITEM(names, i);
1537 Py_INCREF(elt);
1538 PyTuple_SET_ITEM(s, i, elt);
1539 }
1540 ADDOP_O(c, LOAD_CONST, s, consts);
1541 Py_DECREF(s);
1542 len++; /* include the just-pushed tuple */
1543 }
1544 Py_DECREF(names);
1545 return len;
Neal Norwitzc1505362006-12-28 06:47:50 +00001546
1547error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 Py_DECREF(names);
1549 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001550}
1551
1552static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001553compiler_function(struct compiler *c, stmt_ty s)
1554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001556 PyObject *qualname, *first_const = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 arguments_ty args = s->v.FunctionDef.args;
1558 expr_ty returns = s->v.FunctionDef.returns;
1559 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1560 stmt_ty st;
1561 int i, n, docstring, kw_default_count = 0, arglength;
1562 int num_annotations;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 assert(s->kind == FunctionDef_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (!compiler_decorators(c, decos))
1567 return 0;
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05001568 if (args->defaults)
1569 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 if (args->kwonlyargs) {
1571 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1572 args->kw_defaults);
1573 if (res < 0)
1574 return 0;
1575 kw_default_count = res;
1576 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 num_annotations = compiler_visit_annotations(c, args, returns);
1578 if (num_annotations < 0)
1579 return 0;
1580 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001581
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001582 if (!compiler_enter_scope(c, s->v.FunctionDef.name,
1583 COMPILER_SCOPE_FUNCTION, (void *)s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 s->lineno))
1585 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1588 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001589 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 first_const = st->v.Expr.value->v.Str.s;
1591 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1592 compiler_exit_scope(c);
1593 return 0;
1594 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 c->u->u_argcount = asdl_seq_LEN(args->args);
1597 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1598 n = asdl_seq_LEN(s->v.FunctionDef.body);
1599 /* if there was a docstring, we need to skip the first statement */
1600 for (i = docstring; i < n; i++) {
1601 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1602 VISIT_IN_SCOPE(c, stmt, st);
1603 }
1604 co = assemble(c, 1);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001605 qualname = compiler_scope_qualname(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 compiler_exit_scope(c);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001607 if (qualname == NULL || co == NULL) {
1608 Py_XDECREF(qualname);
1609 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001611 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 arglength = asdl_seq_LEN(args->defaults);
1614 arglength |= kw_default_count << 8;
1615 arglength |= num_annotations << 16;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001616 compiler_make_closure(c, co, arglength, qualname);
1617 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 /* decorators */
1621 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1622 ADDOP_I(c, CALL_FUNCTION, 1);
1623 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return compiler_nameop(c, s->v.FunctionDef.name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001626}
1627
1628static int
1629compiler_class(struct compiler *c, stmt_ty s)
1630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 PyCodeObject *co;
1632 PyObject *str;
1633 int i;
1634 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (!compiler_decorators(c, decos))
1637 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 /* ultimately generate code for:
1640 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1641 where:
1642 <func> is a function/closure created from the class body;
1643 it has a single argument (__locals__) where the dict
1644 (or MutableSequence) representing the locals is passed
1645 <name> is the class name
1646 <bases> is the positional arguments and *varargs argument
1647 <keywords> is the keyword arguments and **kwds argument
1648 This borrows from compiler_call.
1649 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001652 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1653 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 return 0;
1655 /* this block represents what we do in the new scope */
1656 {
1657 /* use the class name for name mangling */
1658 Py_INCREF(s->v.ClassDef.name);
1659 Py_XDECREF(c->u->u_private);
1660 c->u->u_private = s->v.ClassDef.name;
1661 /* force it to have one mandatory argument */
1662 c->u->u_argcount = 1;
1663 /* load the first argument (__locals__) ... */
1664 ADDOP_I(c, LOAD_FAST, 0);
1665 /* ... and store it into f_locals */
1666 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1667 /* load (global) __name__ ... */
1668 str = PyUnicode_InternFromString("__name__");
1669 if (!str || !compiler_nameop(c, str, Load)) {
1670 Py_XDECREF(str);
1671 compiler_exit_scope(c);
1672 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 Py_DECREF(str);
1675 /* ... and store it as __module__ */
1676 str = PyUnicode_InternFromString("__module__");
1677 if (!str || !compiler_nameop(c, str, Store)) {
1678 Py_XDECREF(str);
1679 compiler_exit_scope(c);
1680 return 0;
1681 }
1682 Py_DECREF(str);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001683 /* store the __qualname__ */
1684 str = compiler_scope_qualname(c);
1685 if (!str) {
1686 compiler_exit_scope(c);
1687 return 0;
1688 }
1689 ADDOP_O(c, LOAD_CONST, str, consts);
1690 Py_DECREF(str);
1691 str = PyUnicode_InternFromString("__qualname__");
1692 if (!str || !compiler_nameop(c, str, Store)) {
1693 Py_XDECREF(str);
1694 compiler_exit_scope(c);
1695 return 0;
1696 }
1697 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 /* compile the body proper */
1699 if (!compiler_body(c, s->v.ClassDef.body)) {
1700 compiler_exit_scope(c);
1701 return 0;
1702 }
1703 /* return the (empty) __class__ cell */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001704 str = PyUnicode_InternFromString("__class__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 if (str == NULL) {
1706 compiler_exit_scope(c);
1707 return 0;
1708 }
1709 i = compiler_lookup_arg(c->u->u_cellvars, str);
1710 Py_DECREF(str);
1711 if (i == -1) {
1712 /* This happens when nobody references the cell */
1713 PyErr_Clear();
1714 /* Return None */
1715 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1716 }
1717 else {
1718 /* Return the cell where to store __class__ */
1719 ADDOP_I(c, LOAD_CLOSURE, i);
1720 }
1721 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1722 /* create the code object */
1723 co = assemble(c, 1);
1724 }
1725 /* leave the new scope */
1726 compiler_exit_scope(c);
1727 if (co == NULL)
1728 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 /* 2. load the 'build_class' function */
1731 ADDOP(c, LOAD_BUILD_CLASS);
1732
1733 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001734 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 Py_DECREF(co);
1736
1737 /* 4. load class name */
1738 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1739
1740 /* 5. generate the rest of the code for the call */
1741 if (!compiler_call_helper(c, 2,
1742 s->v.ClassDef.bases,
1743 s->v.ClassDef.keywords,
1744 s->v.ClassDef.starargs,
1745 s->v.ClassDef.kwargs))
1746 return 0;
1747
1748 /* 6. apply decorators */
1749 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1750 ADDOP_I(c, CALL_FUNCTION, 1);
1751 }
1752
1753 /* 7. store into <name> */
1754 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1755 return 0;
1756 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001757}
1758
1759static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001760compiler_ifexp(struct compiler *c, expr_ty e)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 basicblock *end, *next;
1763
1764 assert(e->kind == IfExp_kind);
1765 end = compiler_new_block(c);
1766 if (end == NULL)
1767 return 0;
1768 next = compiler_new_block(c);
1769 if (next == NULL)
1770 return 0;
1771 VISIT(c, expr, e->v.IfExp.test);
1772 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1773 VISIT(c, expr, e->v.IfExp.body);
1774 ADDOP_JREL(c, JUMP_FORWARD, end);
1775 compiler_use_next_block(c, next);
1776 VISIT(c, expr, e->v.IfExp.orelse);
1777 compiler_use_next_block(c, end);
1778 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001779}
1780
1781static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782compiler_lambda(struct compiler *c, expr_ty e)
1783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001784 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001785 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 static identifier name;
1787 int kw_default_count = 0, arglength;
1788 arguments_ty args = e->v.Lambda.args;
1789 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 if (!name) {
1792 name = PyUnicode_InternFromString("<lambda>");
1793 if (!name)
1794 return 0;
1795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796
Benjamin Peterson419d9a82013-02-10 09:48:22 -05001797 if (args->defaults)
1798 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 if (args->kwonlyargs) {
1800 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1801 args->kw_defaults);
1802 if (res < 0) return 0;
1803 kw_default_count = res;
1804 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001805 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION,
1806 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 /* Make None the first constant, so the lambda can't have a
1810 docstring. */
1811 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1812 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 c->u->u_argcount = asdl_seq_LEN(args->args);
1815 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1816 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1817 if (c->u->u_ste->ste_generator) {
1818 ADDOP_IN_SCOPE(c, POP_TOP);
1819 }
1820 else {
1821 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1822 }
1823 co = assemble(c, 1);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001824 qualname = compiler_scope_qualname(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 compiler_exit_scope(c);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001826 if (qualname == NULL || co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 arglength = asdl_seq_LEN(args->defaults);
1830 arglength |= kw_default_count << 8;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001831 compiler_make_closure(c, co, arglength, qualname);
1832 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 Py_DECREF(co);
1834
1835 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001836}
1837
1838static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001839compiler_if(struct compiler *c, stmt_ty s)
1840{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 basicblock *end, *next;
1842 int constant;
1843 assert(s->kind == If_kind);
1844 end = compiler_new_block(c);
1845 if (end == NULL)
1846 return 0;
1847
Georg Brandl8334fd92010-12-04 10:26:46 +00001848 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 /* constant = 0: "if 0"
1850 * constant = 1: "if 1", "if 2", ...
1851 * constant = -1: rest */
1852 if (constant == 0) {
1853 if (s->v.If.orelse)
1854 VISIT_SEQ(c, stmt, s->v.If.orelse);
1855 } else if (constant == 1) {
1856 VISIT_SEQ(c, stmt, s->v.If.body);
1857 } else {
1858 if (s->v.If.orelse) {
1859 next = compiler_new_block(c);
1860 if (next == NULL)
1861 return 0;
1862 }
1863 else
1864 next = end;
1865 VISIT(c, expr, s->v.If.test);
1866 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1867 VISIT_SEQ(c, stmt, s->v.If.body);
1868 ADDOP_JREL(c, JUMP_FORWARD, end);
1869 if (s->v.If.orelse) {
1870 compiler_use_next_block(c, next);
1871 VISIT_SEQ(c, stmt, s->v.If.orelse);
1872 }
1873 }
1874 compiler_use_next_block(c, end);
1875 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876}
1877
1878static int
1879compiler_for(struct compiler *c, stmt_ty s)
1880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 start = compiler_new_block(c);
1884 cleanup = compiler_new_block(c);
1885 end = compiler_new_block(c);
1886 if (start == NULL || end == NULL || cleanup == NULL)
1887 return 0;
1888 ADDOP_JREL(c, SETUP_LOOP, end);
1889 if (!compiler_push_fblock(c, LOOP, start))
1890 return 0;
1891 VISIT(c, expr, s->v.For.iter);
1892 ADDOP(c, GET_ITER);
1893 compiler_use_next_block(c, start);
1894 ADDOP_JREL(c, FOR_ITER, cleanup);
1895 VISIT(c, expr, s->v.For.target);
1896 VISIT_SEQ(c, stmt, s->v.For.body);
1897 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1898 compiler_use_next_block(c, cleanup);
1899 ADDOP(c, POP_BLOCK);
1900 compiler_pop_fblock(c, LOOP, start);
1901 VISIT_SEQ(c, stmt, s->v.For.orelse);
1902 compiler_use_next_block(c, end);
1903 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904}
1905
1906static int
1907compiler_while(struct compiler *c, stmt_ty s)
1908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00001910 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (constant == 0) {
1913 if (s->v.While.orelse)
1914 VISIT_SEQ(c, stmt, s->v.While.orelse);
1915 return 1;
1916 }
1917 loop = compiler_new_block(c);
1918 end = compiler_new_block(c);
1919 if (constant == -1) {
1920 anchor = compiler_new_block(c);
1921 if (anchor == NULL)
1922 return 0;
1923 }
1924 if (loop == NULL || end == NULL)
1925 return 0;
1926 if (s->v.While.orelse) {
1927 orelse = compiler_new_block(c);
1928 if (orelse == NULL)
1929 return 0;
1930 }
1931 else
1932 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 ADDOP_JREL(c, SETUP_LOOP, end);
1935 compiler_use_next_block(c, loop);
1936 if (!compiler_push_fblock(c, LOOP, loop))
1937 return 0;
1938 if (constant == -1) {
1939 VISIT(c, expr, s->v.While.test);
1940 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
1941 }
1942 VISIT_SEQ(c, stmt, s->v.While.body);
1943 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 /* XXX should the two POP instructions be in a separate block
1946 if there is no else clause ?
1947 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 if (constant == -1) {
1950 compiler_use_next_block(c, anchor);
1951 ADDOP(c, POP_BLOCK);
1952 }
1953 compiler_pop_fblock(c, LOOP, loop);
1954 if (orelse != NULL) /* what if orelse is just pass? */
1955 VISIT_SEQ(c, stmt, s->v.While.orelse);
1956 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959}
1960
1961static int
1962compiler_continue(struct compiler *c)
1963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
1965 static const char IN_FINALLY_ERROR_MSG[] =
1966 "'continue' not supported inside 'finally' clause";
1967 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 if (!c->u->u_nfblocks)
1970 return compiler_error(c, LOOP_ERROR_MSG);
1971 i = c->u->u_nfblocks - 1;
1972 switch (c->u->u_fblock[i].fb_type) {
1973 case LOOP:
1974 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
1975 break;
1976 case EXCEPT:
1977 case FINALLY_TRY:
1978 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
1979 /* Prevent continue anywhere under a finally
1980 even if hidden in a sub-try or except. */
1981 if (c->u->u_fblock[i].fb_type == FINALLY_END)
1982 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1983 }
1984 if (i == -1)
1985 return compiler_error(c, LOOP_ERROR_MSG);
1986 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
1987 break;
1988 case FINALLY_END:
1989 return compiler_error(c, IN_FINALLY_ERROR_MSG);
1990 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001992 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001993}
1994
1995/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001996
1997 SETUP_FINALLY L
1998 <code for body>
1999 POP_BLOCK
2000 LOAD_CONST <None>
2001 L: <code for finalbody>
2002 END_FINALLY
2003
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002004 The special instructions use the block stack. Each block
2005 stack entry contains the instruction that created it (here
2006 SETUP_FINALLY), the level of the value stack at the time the
2007 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002009 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 Pushes the current value stack level and the label
2011 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002012 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 Pops en entry from the block stack, and pops the value
2014 stack until its level is the same as indicated on the
2015 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002016 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 Pops a variable number of entries from the *value* stack
2018 and re-raises the exception they specify. The number of
2019 entries popped depends on the (pseudo) exception type.
2020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002021 The block stack is unwound when an exception is raised:
2022 when a SETUP_FINALLY entry is found, the exception is pushed
2023 onto the value stack (and the exception condition is cleared),
2024 and the interpreter jumps to the label gotten from the block
2025 stack.
2026*/
2027
2028static int
2029compiler_try_finally(struct compiler *c, stmt_ty s)
2030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 basicblock *body, *end;
2032 body = compiler_new_block(c);
2033 end = compiler_new_block(c);
2034 if (body == NULL || end == NULL)
2035 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 ADDOP_JREL(c, SETUP_FINALLY, end);
2038 compiler_use_next_block(c, body);
2039 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2040 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002041 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2042 if (!compiler_try_except(c, s))
2043 return 0;
2044 }
2045 else {
2046 VISIT_SEQ(c, stmt, s->v.Try.body);
2047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 ADDOP(c, POP_BLOCK);
2049 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2052 compiler_use_next_block(c, end);
2053 if (!compiler_push_fblock(c, FINALLY_END, end))
2054 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002055 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 ADDOP(c, END_FINALLY);
2057 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002060}
2061
2062/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002063 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002064 (The contents of the value stack is shown in [], with the top
2065 at the right; 'tb' is trace-back info, 'val' the exception's
2066 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067
2068 Value stack Label Instruction Argument
2069 [] SETUP_EXCEPT L1
2070 [] <code for S>
2071 [] POP_BLOCK
2072 [] JUMP_FORWARD L0
2073
2074 [tb, val, exc] L1: DUP )
2075 [tb, val, exc, exc] <evaluate E1> )
2076 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2077 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2078 [tb, val, exc] POP
2079 [tb, val] <assign to V1> (or POP if no V1)
2080 [tb] POP
2081 [] <code for S1>
2082 JUMP_FORWARD L0
2083
2084 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002085 .............................etc.......................
2086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2088
2089 [] L0: <next statement>
2090
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002091 Of course, parts are not generated if Vi or Ei is not present.
2092*/
2093static int
2094compiler_try_except(struct compiler *c, stmt_ty s)
2095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 basicblock *body, *orelse, *except, *end;
2097 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 body = compiler_new_block(c);
2100 except = compiler_new_block(c);
2101 orelse = compiler_new_block(c);
2102 end = compiler_new_block(c);
2103 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2104 return 0;
2105 ADDOP_JREL(c, SETUP_EXCEPT, except);
2106 compiler_use_next_block(c, body);
2107 if (!compiler_push_fblock(c, EXCEPT, body))
2108 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002109 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 ADDOP(c, POP_BLOCK);
2111 compiler_pop_fblock(c, EXCEPT, body);
2112 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002113 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 compiler_use_next_block(c, except);
2115 for (i = 0; i < n; i++) {
2116 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002117 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 if (!handler->v.ExceptHandler.type && i < n-1)
2119 return compiler_error(c, "default 'except:' must be last");
2120 c->u->u_lineno_set = 0;
2121 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002122 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 except = compiler_new_block(c);
2124 if (except == NULL)
2125 return 0;
2126 if (handler->v.ExceptHandler.type) {
2127 ADDOP(c, DUP_TOP);
2128 VISIT(c, expr, handler->v.ExceptHandler.type);
2129 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2130 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2131 }
2132 ADDOP(c, POP_TOP);
2133 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002134 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002135
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002136 cleanup_end = compiler_new_block(c);
2137 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002138 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002139 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002140
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002141 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2142 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002144 /*
2145 try:
2146 # body
2147 except type as name:
2148 try:
2149 # body
2150 finally:
2151 name = None
2152 del name
2153 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002155 /* second try: */
2156 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2157 compiler_use_next_block(c, cleanup_body);
2158 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2159 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002160
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002161 /* second # body */
2162 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2163 ADDOP(c, POP_BLOCK);
2164 ADDOP(c, POP_EXCEPT);
2165 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002167 /* finally: */
2168 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2169 compiler_use_next_block(c, cleanup_end);
2170 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2171 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002173 /* name = None */
2174 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2175 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002177 /* del name */
2178 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002180 ADDOP(c, END_FINALLY);
2181 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 }
2183 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002184 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002186 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002187 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002188 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189
Guido van Rossumb940e112007-01-10 16:19:56 +00002190 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002191 ADDOP(c, POP_TOP);
2192 compiler_use_next_block(c, cleanup_body);
2193 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2194 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002196 ADDOP(c, POP_EXCEPT);
2197 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
2199 ADDOP_JREL(c, JUMP_FORWARD, end);
2200 compiler_use_next_block(c, except);
2201 }
2202 ADDOP(c, END_FINALLY);
2203 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002204 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 compiler_use_next_block(c, end);
2206 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002207}
2208
2209static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002210compiler_try(struct compiler *c, stmt_ty s) {
2211 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2212 return compiler_try_finally(c, s);
2213 else
2214 return compiler_try_except(c, s);
2215}
2216
2217
2218static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002219compiler_import_as(struct compiler *c, identifier name, identifier asname)
2220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 /* The IMPORT_NAME opcode was already generated. This function
2222 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 If there is a dot in name, we need to split it and emit a
2225 LOAD_ATTR for each name.
2226 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002227 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2228 PyUnicode_GET_LENGTH(name), 1);
2229 if (dot == -2)
2230 return -1;
2231 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 /* Consume the base module name to get the first attribute */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002233 Py_ssize_t pos = dot + 1;
2234 while (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 PyObject *attr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002236 dot = PyUnicode_FindChar(name, '.', pos,
2237 PyUnicode_GET_LENGTH(name), 1);
2238 if (dot == -2)
2239 return -1;
2240 attr = PyUnicode_Substring(name, pos,
2241 (dot != -1) ? dot :
2242 PyUnicode_GET_LENGTH(name));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 if (!attr)
2244 return -1;
2245 ADDOP_O(c, LOAD_ATTR, attr, names);
2246 Py_DECREF(attr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002247 pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
2249 }
2250 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002251}
2252
2253static int
2254compiler_import(struct compiler *c, stmt_ty s)
2255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002256 /* The Import node stores a module name like a.b.c as a single
2257 string. This is convenient for all cases except
2258 import a.b.c as d
2259 where we need to parse that string to extract the individual
2260 module names.
2261 XXX Perhaps change the representation to make this case simpler?
2262 */
2263 int i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 for (i = 0; i < n; i++) {
2266 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2267 int r;
2268 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 level = PyLong_FromLong(0);
2271 if (level == NULL)
2272 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 ADDOP_O(c, LOAD_CONST, level, consts);
2275 Py_DECREF(level);
2276 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2277 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 if (alias->asname) {
2280 r = compiler_import_as(c, alias->name, alias->asname);
2281 if (!r)
2282 return r;
2283 }
2284 else {
2285 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002286 Py_ssize_t dot = PyUnicode_FindChar(
2287 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2288 if (dot != -1)
2289 tmp = PyUnicode_Substring(alias->name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002291 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 Py_DECREF(tmp);
2293 }
2294 if (!r)
2295 return r;
2296 }
2297 }
2298 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299}
2300
2301static int
2302compiler_from_import(struct compiler *c, stmt_ty s)
2303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002304 int i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyObject *names = PyTuple_New(n);
2307 PyObject *level;
2308 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (!empty_string) {
2311 empty_string = PyUnicode_FromString("");
2312 if (!empty_string)
2313 return 0;
2314 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 if (!names)
2317 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 level = PyLong_FromLong(s->v.ImportFrom.level);
2320 if (!level) {
2321 Py_DECREF(names);
2322 return 0;
2323 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002325 /* build up the names */
2326 for (i = 0; i < n; i++) {
2327 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2328 Py_INCREF(alias->name);
2329 PyTuple_SET_ITEM(names, i, alias->name);
2330 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002332 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2333 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2334 Py_DECREF(level);
2335 Py_DECREF(names);
2336 return compiler_error(c, "from __future__ imports must occur "
2337 "at the beginning of the file");
2338 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 ADDOP_O(c, LOAD_CONST, level, consts);
2341 Py_DECREF(level);
2342 ADDOP_O(c, LOAD_CONST, names, consts);
2343 Py_DECREF(names);
2344 if (s->v.ImportFrom.module) {
2345 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2346 }
2347 else {
2348 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2349 }
2350 for (i = 0; i < n; i++) {
2351 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2352 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002353
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002354 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 assert(n == 1);
2356 ADDOP(c, IMPORT_STAR);
2357 return 1;
2358 }
2359
2360 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2361 store_name = alias->name;
2362 if (alias->asname)
2363 store_name = alias->asname;
2364
2365 if (!compiler_nameop(c, store_name, Store)) {
2366 Py_DECREF(names);
2367 return 0;
2368 }
2369 }
2370 /* remove imported module */
2371 ADDOP(c, POP_TOP);
2372 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002373}
2374
2375static int
2376compiler_assert(struct compiler *c, stmt_ty s)
2377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 static PyObject *assertion_error = NULL;
2379 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002380
Georg Brandl8334fd92010-12-04 10:26:46 +00002381 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 return 1;
2383 if (assertion_error == NULL) {
2384 assertion_error = PyUnicode_InternFromString("AssertionError");
2385 if (assertion_error == NULL)
2386 return 0;
2387 }
2388 if (s->v.Assert.test->kind == Tuple_kind &&
2389 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2390 const char* msg =
2391 "assertion is always true, perhaps remove parentheses?";
2392 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
2393 c->u->u_lineno, NULL, NULL) == -1)
2394 return 0;
2395 }
2396 VISIT(c, expr, s->v.Assert.test);
2397 end = compiler_new_block(c);
2398 if (end == NULL)
2399 return 0;
2400 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2401 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2402 if (s->v.Assert.msg) {
2403 VISIT(c, expr, s->v.Assert.msg);
2404 ADDOP_I(c, CALL_FUNCTION, 1);
2405 }
2406 ADDOP_I(c, RAISE_VARARGS, 1);
2407 compiler_use_next_block(c, end);
2408 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002409}
2410
2411static int
2412compiler_visit_stmt(struct compiler *c, stmt_ty s)
2413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 /* Always assign a lineno to the next instruction for a stmt. */
2417 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002418 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 switch (s->kind) {
2422 case FunctionDef_kind:
2423 return compiler_function(c, s);
2424 case ClassDef_kind:
2425 return compiler_class(c, s);
2426 case Return_kind:
2427 if (c->u->u_ste->ste_type != FunctionBlock)
2428 return compiler_error(c, "'return' outside function");
2429 if (s->v.Return.value) {
2430 VISIT(c, expr, s->v.Return.value);
2431 }
2432 else
2433 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2434 ADDOP(c, RETURN_VALUE);
2435 break;
2436 case Delete_kind:
2437 VISIT_SEQ(c, expr, s->v.Delete.targets)
2438 break;
2439 case Assign_kind:
2440 n = asdl_seq_LEN(s->v.Assign.targets);
2441 VISIT(c, expr, s->v.Assign.value);
2442 for (i = 0; i < n; i++) {
2443 if (i < n - 1)
2444 ADDOP(c, DUP_TOP);
2445 VISIT(c, expr,
2446 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2447 }
2448 break;
2449 case AugAssign_kind:
2450 return compiler_augassign(c, s);
2451 case For_kind:
2452 return compiler_for(c, s);
2453 case While_kind:
2454 return compiler_while(c, s);
2455 case If_kind:
2456 return compiler_if(c, s);
2457 case Raise_kind:
2458 n = 0;
2459 if (s->v.Raise.exc) {
2460 VISIT(c, expr, s->v.Raise.exc);
2461 n++;
2462 if (s->v.Raise.cause) {
2463 VISIT(c, expr, s->v.Raise.cause);
2464 n++;
2465 }
2466 }
2467 ADDOP_I(c, RAISE_VARARGS, n);
2468 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002469 case Try_kind:
2470 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 case Assert_kind:
2472 return compiler_assert(c, s);
2473 case Import_kind:
2474 return compiler_import(c, s);
2475 case ImportFrom_kind:
2476 return compiler_from_import(c, s);
2477 case Global_kind:
2478 case Nonlocal_kind:
2479 break;
2480 case Expr_kind:
2481 if (c->c_interactive && c->c_nestlevel <= 1) {
2482 VISIT(c, expr, s->v.Expr.value);
2483 ADDOP(c, PRINT_EXPR);
2484 }
2485 else if (s->v.Expr.value->kind != Str_kind &&
2486 s->v.Expr.value->kind != Num_kind) {
2487 VISIT(c, expr, s->v.Expr.value);
2488 ADDOP(c, POP_TOP);
2489 }
2490 break;
2491 case Pass_kind:
2492 break;
2493 case Break_kind:
2494 if (!compiler_in_loop(c))
2495 return compiler_error(c, "'break' outside loop");
2496 ADDOP(c, BREAK_LOOP);
2497 break;
2498 case Continue_kind:
2499 return compiler_continue(c);
2500 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002501 return compiler_with(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 }
2503 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504}
2505
2506static int
2507unaryop(unaryop_ty op)
2508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 switch (op) {
2510 case Invert:
2511 return UNARY_INVERT;
2512 case Not:
2513 return UNARY_NOT;
2514 case UAdd:
2515 return UNARY_POSITIVE;
2516 case USub:
2517 return UNARY_NEGATIVE;
2518 default:
2519 PyErr_Format(PyExc_SystemError,
2520 "unary op %d should not be possible", op);
2521 return 0;
2522 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002523}
2524
2525static int
2526binop(struct compiler *c, operator_ty op)
2527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 switch (op) {
2529 case Add:
2530 return BINARY_ADD;
2531 case Sub:
2532 return BINARY_SUBTRACT;
2533 case Mult:
2534 return BINARY_MULTIPLY;
2535 case Div:
2536 return BINARY_TRUE_DIVIDE;
2537 case Mod:
2538 return BINARY_MODULO;
2539 case Pow:
2540 return BINARY_POWER;
2541 case LShift:
2542 return BINARY_LSHIFT;
2543 case RShift:
2544 return BINARY_RSHIFT;
2545 case BitOr:
2546 return BINARY_OR;
2547 case BitXor:
2548 return BINARY_XOR;
2549 case BitAnd:
2550 return BINARY_AND;
2551 case FloorDiv:
2552 return BINARY_FLOOR_DIVIDE;
2553 default:
2554 PyErr_Format(PyExc_SystemError,
2555 "binary op %d should not be possible", op);
2556 return 0;
2557 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002558}
2559
2560static int
2561cmpop(cmpop_ty op)
2562{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 switch (op) {
2564 case Eq:
2565 return PyCmp_EQ;
2566 case NotEq:
2567 return PyCmp_NE;
2568 case Lt:
2569 return PyCmp_LT;
2570 case LtE:
2571 return PyCmp_LE;
2572 case Gt:
2573 return PyCmp_GT;
2574 case GtE:
2575 return PyCmp_GE;
2576 case Is:
2577 return PyCmp_IS;
2578 case IsNot:
2579 return PyCmp_IS_NOT;
2580 case In:
2581 return PyCmp_IN;
2582 case NotIn:
2583 return PyCmp_NOT_IN;
2584 default:
2585 return PyCmp_BAD;
2586 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002587}
2588
2589static int
2590inplace_binop(struct compiler *c, operator_ty op)
2591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 switch (op) {
2593 case Add:
2594 return INPLACE_ADD;
2595 case Sub:
2596 return INPLACE_SUBTRACT;
2597 case Mult:
2598 return INPLACE_MULTIPLY;
2599 case Div:
2600 return INPLACE_TRUE_DIVIDE;
2601 case Mod:
2602 return INPLACE_MODULO;
2603 case Pow:
2604 return INPLACE_POWER;
2605 case LShift:
2606 return INPLACE_LSHIFT;
2607 case RShift:
2608 return INPLACE_RSHIFT;
2609 case BitOr:
2610 return INPLACE_OR;
2611 case BitXor:
2612 return INPLACE_XOR;
2613 case BitAnd:
2614 return INPLACE_AND;
2615 case FloorDiv:
2616 return INPLACE_FLOOR_DIVIDE;
2617 default:
2618 PyErr_Format(PyExc_SystemError,
2619 "inplace binary op %d should not be possible", op);
2620 return 0;
2621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002622}
2623
2624static int
2625compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 int op, scope, arg;
2628 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 PyObject *dict = c->u->u_names;
2631 PyObject *mangled;
2632 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 mangled = _Py_Mangle(c->u->u_private, name);
2635 if (!mangled)
2636 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002637
Benjamin Peterson70b224d2012-12-06 17:49:58 -05002638 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2639 PyUnicode_CompareWithASCIIString(name, "True") &&
2640 PyUnicode_CompareWithASCIIString(name, "False"));
2641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 op = 0;
2643 optype = OP_NAME;
2644 scope = PyST_GetScope(c->u->u_ste, mangled);
2645 switch (scope) {
2646 case FREE:
2647 dict = c->u->u_freevars;
2648 optype = OP_DEREF;
2649 break;
2650 case CELL:
2651 dict = c->u->u_cellvars;
2652 optype = OP_DEREF;
2653 break;
2654 case LOCAL:
2655 if (c->u->u_ste->ste_type == FunctionBlock)
2656 optype = OP_FAST;
2657 break;
2658 case GLOBAL_IMPLICIT:
2659 if (c->u->u_ste->ste_type == FunctionBlock &&
2660 !c->u->u_ste->ste_unoptimized)
2661 optype = OP_GLOBAL;
2662 break;
2663 case GLOBAL_EXPLICIT:
2664 optype = OP_GLOBAL;
2665 break;
2666 default:
2667 /* scope can be 0 */
2668 break;
2669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002672 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 switch (optype) {
2675 case OP_DEREF:
2676 switch (ctx) {
2677 case Load: op = LOAD_DEREF; break;
2678 case Store: op = STORE_DEREF; break;
2679 case AugLoad:
2680 case AugStore:
2681 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002682 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 case Param:
2684 default:
2685 PyErr_SetString(PyExc_SystemError,
2686 "param invalid for deref variable");
2687 return 0;
2688 }
2689 break;
2690 case OP_FAST:
2691 switch (ctx) {
2692 case Load: op = LOAD_FAST; break;
2693 case Store: op = STORE_FAST; break;
2694 case Del: op = DELETE_FAST; break;
2695 case AugLoad:
2696 case AugStore:
2697 break;
2698 case Param:
2699 default:
2700 PyErr_SetString(PyExc_SystemError,
2701 "param invalid for local variable");
2702 return 0;
2703 }
2704 ADDOP_O(c, op, mangled, varnames);
2705 Py_DECREF(mangled);
2706 return 1;
2707 case OP_GLOBAL:
2708 switch (ctx) {
2709 case Load: op = LOAD_GLOBAL; break;
2710 case Store: op = STORE_GLOBAL; break;
2711 case Del: op = DELETE_GLOBAL; break;
2712 case AugLoad:
2713 case AugStore:
2714 break;
2715 case Param:
2716 default:
2717 PyErr_SetString(PyExc_SystemError,
2718 "param invalid for global variable");
2719 return 0;
2720 }
2721 break;
2722 case OP_NAME:
2723 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002724 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 case Store: op = STORE_NAME; break;
2726 case Del: op = DELETE_NAME; break;
2727 case AugLoad:
2728 case AugStore:
2729 break;
2730 case Param:
2731 default:
2732 PyErr_SetString(PyExc_SystemError,
2733 "param invalid for name variable");
2734 return 0;
2735 }
2736 break;
2737 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 assert(op);
2740 arg = compiler_add_o(c, dict, mangled);
2741 Py_DECREF(mangled);
2742 if (arg < 0)
2743 return 0;
2744 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002745}
2746
2747static int
2748compiler_boolop(struct compiler *c, expr_ty e)
2749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 basicblock *end;
2751 int jumpi, i, n;
2752 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 assert(e->kind == BoolOp_kind);
2755 if (e->v.BoolOp.op == And)
2756 jumpi = JUMP_IF_FALSE_OR_POP;
2757 else
2758 jumpi = JUMP_IF_TRUE_OR_POP;
2759 end = compiler_new_block(c);
2760 if (end == NULL)
2761 return 0;
2762 s = e->v.BoolOp.values;
2763 n = asdl_seq_LEN(s) - 1;
2764 assert(n >= 0);
2765 for (i = 0; i < n; ++i) {
2766 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2767 ADDOP_JABS(c, jumpi, end);
2768 }
2769 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2770 compiler_use_next_block(c, end);
2771 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002772}
2773
2774static int
2775compiler_list(struct compiler *c, expr_ty e)
2776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 int n = asdl_seq_LEN(e->v.List.elts);
2778 if (e->v.List.ctx == Store) {
2779 int i, seen_star = 0;
2780 for (i = 0; i < n; i++) {
2781 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2782 if (elt->kind == Starred_kind && !seen_star) {
2783 if ((i >= (1 << 8)) ||
2784 (n-i-1 >= (INT_MAX >> 8)))
2785 return compiler_error(c,
2786 "too many expressions in "
2787 "star-unpacking assignment");
2788 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2789 seen_star = 1;
2790 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2791 } else if (elt->kind == Starred_kind) {
2792 return compiler_error(c,
2793 "two starred expressions in assignment");
2794 }
2795 }
2796 if (!seen_star) {
2797 ADDOP_I(c, UNPACK_SEQUENCE, n);
2798 }
2799 }
2800 VISIT_SEQ(c, expr, e->v.List.elts);
2801 if (e->v.List.ctx == Load) {
2802 ADDOP_I(c, BUILD_LIST, n);
2803 }
2804 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002805}
2806
2807static int
2808compiler_tuple(struct compiler *c, expr_ty e)
2809{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 int n = asdl_seq_LEN(e->v.Tuple.elts);
2811 if (e->v.Tuple.ctx == Store) {
2812 int i, seen_star = 0;
2813 for (i = 0; i < n; i++) {
2814 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2815 if (elt->kind == Starred_kind && !seen_star) {
2816 if ((i >= (1 << 8)) ||
2817 (n-i-1 >= (INT_MAX >> 8)))
2818 return compiler_error(c,
2819 "too many expressions in "
2820 "star-unpacking assignment");
2821 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2822 seen_star = 1;
2823 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2824 } else if (elt->kind == Starred_kind) {
2825 return compiler_error(c,
2826 "two starred expressions in assignment");
2827 }
2828 }
2829 if (!seen_star) {
2830 ADDOP_I(c, UNPACK_SEQUENCE, n);
2831 }
2832 }
2833 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2834 if (e->v.Tuple.ctx == Load) {
2835 ADDOP_I(c, BUILD_TUPLE, n);
2836 }
2837 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002838}
2839
2840static int
2841compiler_compare(struct compiler *c, expr_ty e)
2842{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 int i, n;
2844 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2847 VISIT(c, expr, e->v.Compare.left);
2848 n = asdl_seq_LEN(e->v.Compare.ops);
2849 assert(n > 0);
2850 if (n > 1) {
2851 cleanup = compiler_new_block(c);
2852 if (cleanup == NULL)
2853 return 0;
2854 VISIT(c, expr,
2855 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2856 }
2857 for (i = 1; i < n; i++) {
2858 ADDOP(c, DUP_TOP);
2859 ADDOP(c, ROT_THREE);
2860 ADDOP_I(c, COMPARE_OP,
2861 cmpop((cmpop_ty)(asdl_seq_GET(
2862 e->v.Compare.ops, i - 1))));
2863 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2864 NEXT_BLOCK(c);
2865 if (i < (n - 1))
2866 VISIT(c, expr,
2867 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2868 }
2869 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2870 ADDOP_I(c, COMPARE_OP,
2871 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2872 if (n > 1) {
2873 basicblock *end = compiler_new_block(c);
2874 if (end == NULL)
2875 return 0;
2876 ADDOP_JREL(c, JUMP_FORWARD, end);
2877 compiler_use_next_block(c, cleanup);
2878 ADDOP(c, ROT_TWO);
2879 ADDOP(c, POP_TOP);
2880 compiler_use_next_block(c, end);
2881 }
2882 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002883}
2884
2885static int
2886compiler_call(struct compiler *c, expr_ty e)
2887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 VISIT(c, expr, e->v.Call.func);
2889 return compiler_call_helper(c, 0,
2890 e->v.Call.args,
2891 e->v.Call.keywords,
2892 e->v.Call.starargs,
2893 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002894}
2895
2896/* shared code between compiler_call and compiler_class */
2897static int
2898compiler_call_helper(struct compiler *c,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 int n, /* Args already pushed */
2900 asdl_seq *args,
2901 asdl_seq *keywords,
2902 expr_ty starargs,
2903 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 n += asdl_seq_LEN(args);
2908 VISIT_SEQ(c, expr, args);
2909 if (keywords) {
2910 VISIT_SEQ(c, keyword, keywords);
2911 n |= asdl_seq_LEN(keywords) << 8;
2912 }
2913 if (starargs) {
2914 VISIT(c, expr, starargs);
2915 code |= 1;
2916 }
2917 if (kwargs) {
2918 VISIT(c, expr, kwargs);
2919 code |= 2;
2920 }
2921 switch (code) {
2922 case 0:
2923 ADDOP_I(c, CALL_FUNCTION, n);
2924 break;
2925 case 1:
2926 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2927 break;
2928 case 2:
2929 ADDOP_I(c, CALL_FUNCTION_KW, n);
2930 break;
2931 case 3:
2932 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2933 break;
2934 }
2935 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002936}
2937
Nick Coghlan650f0d02007-04-15 12:05:43 +00002938
2939/* List and set comprehensions and generator expressions work by creating a
2940 nested function to perform the actual iteration. This means that the
2941 iteration variables don't leak into the current scope.
2942 The defined function is called immediately following its definition, with the
2943 result of that call being the result of the expression.
2944 The LC/SC version returns the populated container, while the GE version is
2945 flagged in symtable.c as a generator, so it returns the generator object
2946 when the function is called.
2947 This code *knows* that the loop cannot contain break, continue, or return,
2948 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2949
2950 Possible cleanups:
2951 - iterate over the generator sequence instead of using recursion
2952*/
2953
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955compiler_comprehension_generator(struct compiler *c,
2956 asdl_seq *generators, int gen_index,
2957 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 /* generate code for the iterator, then each of the ifs,
2960 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 comprehension_ty gen;
2963 basicblock *start, *anchor, *skip, *if_cleanup;
2964 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 start = compiler_new_block(c);
2967 skip = compiler_new_block(c);
2968 if_cleanup = compiler_new_block(c);
2969 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2972 anchor == NULL)
2973 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 if (gen_index == 0) {
2978 /* Receive outermost iter as an implicit argument */
2979 c->u->u_argcount = 1;
2980 ADDOP_I(c, LOAD_FAST, 0);
2981 }
2982 else {
2983 /* Sub-iter - calculate on the fly */
2984 VISIT(c, expr, gen->iter);
2985 ADDOP(c, GET_ITER);
2986 }
2987 compiler_use_next_block(c, start);
2988 ADDOP_JREL(c, FOR_ITER, anchor);
2989 NEXT_BLOCK(c);
2990 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 /* XXX this needs to be cleaned up...a lot! */
2993 n = asdl_seq_LEN(gen->ifs);
2994 for (i = 0; i < n; i++) {
2995 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2996 VISIT(c, expr, e);
2997 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2998 NEXT_BLOCK(c);
2999 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 if (++gen_index < asdl_seq_LEN(generators))
3002 if (!compiler_comprehension_generator(c,
3003 generators, gen_index,
3004 elt, val, type))
3005 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 /* only append after the last for generator */
3008 if (gen_index >= asdl_seq_LEN(generators)) {
3009 /* comprehension specific code */
3010 switch (type) {
3011 case COMP_GENEXP:
3012 VISIT(c, expr, elt);
3013 ADDOP(c, YIELD_VALUE);
3014 ADDOP(c, POP_TOP);
3015 break;
3016 case COMP_LISTCOMP:
3017 VISIT(c, expr, elt);
3018 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3019 break;
3020 case COMP_SETCOMP:
3021 VISIT(c, expr, elt);
3022 ADDOP_I(c, SET_ADD, gen_index + 1);
3023 break;
3024 case COMP_DICTCOMP:
3025 /* With 'd[k] = v', v is evaluated before k, so we do
3026 the same. */
3027 VISIT(c, expr, val);
3028 VISIT(c, expr, elt);
3029 ADDOP_I(c, MAP_ADD, gen_index + 1);
3030 break;
3031 default:
3032 return 0;
3033 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 compiler_use_next_block(c, skip);
3036 }
3037 compiler_use_next_block(c, if_cleanup);
3038 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3039 compiler_use_next_block(c, anchor);
3040
3041 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003042}
3043
3044static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003045compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 PyCodeObject *co = NULL;
3049 expr_ty outermost_iter;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003050 PyObject *qualname = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 outermost_iter = ((comprehension_ty)
3053 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003054
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003055 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3056 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 if (type != COMP_GENEXP) {
3060 int op;
3061 switch (type) {
3062 case COMP_LISTCOMP:
3063 op = BUILD_LIST;
3064 break;
3065 case COMP_SETCOMP:
3066 op = BUILD_SET;
3067 break;
3068 case COMP_DICTCOMP:
3069 op = BUILD_MAP;
3070 break;
3071 default:
3072 PyErr_Format(PyExc_SystemError,
3073 "unknown comprehension type %d", type);
3074 goto error_in_scope;
3075 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 ADDOP_I(c, op, 0);
3078 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 if (!compiler_comprehension_generator(c, generators, 0, elt,
3081 val, type))
3082 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 if (type != COMP_GENEXP) {
3085 ADDOP(c, RETURN_VALUE);
3086 }
3087
3088 co = assemble(c, 1);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003089 qualname = compiler_scope_qualname(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 compiler_exit_scope(c);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003091 if (qualname == NULL || co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 goto error;
3093
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003094 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003096 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 Py_DECREF(co);
3098
3099 VISIT(c, expr, outermost_iter);
3100 ADDOP(c, GET_ITER);
3101 ADDOP_I(c, CALL_FUNCTION, 1);
3102 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003103error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003105error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003106 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 Py_XDECREF(co);
3108 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003109}
3110
3111static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003112compiler_genexp(struct compiler *c, expr_ty e)
3113{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 static identifier name;
3115 if (!name) {
3116 name = PyUnicode_FromString("<genexpr>");
3117 if (!name)
3118 return 0;
3119 }
3120 assert(e->kind == GeneratorExp_kind);
3121 return compiler_comprehension(c, e, COMP_GENEXP, name,
3122 e->v.GeneratorExp.generators,
3123 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003124}
3125
3126static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003127compiler_listcomp(struct compiler *c, expr_ty e)
3128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 static identifier name;
3130 if (!name) {
3131 name = PyUnicode_FromString("<listcomp>");
3132 if (!name)
3133 return 0;
3134 }
3135 assert(e->kind == ListComp_kind);
3136 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3137 e->v.ListComp.generators,
3138 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003139}
3140
3141static int
3142compiler_setcomp(struct compiler *c, expr_ty e)
3143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 static identifier name;
3145 if (!name) {
3146 name = PyUnicode_FromString("<setcomp>");
3147 if (!name)
3148 return 0;
3149 }
3150 assert(e->kind == SetComp_kind);
3151 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3152 e->v.SetComp.generators,
3153 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003154}
3155
3156
3157static int
3158compiler_dictcomp(struct compiler *c, expr_ty e)
3159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 static identifier name;
3161 if (!name) {
3162 name = PyUnicode_FromString("<dictcomp>");
3163 if (!name)
3164 return 0;
3165 }
3166 assert(e->kind == DictComp_kind);
3167 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3168 e->v.DictComp.generators,
3169 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003170}
3171
3172
3173static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003174compiler_visit_keyword(struct compiler *c, keyword_ty k)
3175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3177 VISIT(c, expr, k->value);
3178 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179}
3180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003182 whether they are true or false.
3183
3184 Return values: 1 for true, 0 for false, -1 for non-constant.
3185 */
3186
3187static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003188expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 char *id;
3191 switch (e->kind) {
3192 case Ellipsis_kind:
3193 return 1;
3194 case Num_kind:
3195 return PyObject_IsTrue(e->v.Num.n);
3196 case Str_kind:
3197 return PyObject_IsTrue(e->v.Str.s);
3198 case Name_kind:
3199 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003200 id = PyUnicode_AsUTF8(e->v.Name.id);
Benjamin Peterson442f2092012-12-06 17:41:04 -05003201 if (id && strcmp(id, "__debug__") == 0)
3202 return !c->c_optimize;
3203 return -1;
3204 case NameConstant_kind: {
3205 PyObject *o = e->v.NameConstant.value;
3206 if (o == Py_None)
3207 return 0;
3208 else if (o == Py_True)
3209 return 1;
3210 else if (o == Py_False)
3211 return 0;
3212 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 default:
3214 return -1;
3215 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216}
3217
Guido van Rossumc2e20742006-02-27 22:32:47 +00003218/*
3219 Implements the with statement from PEP 343.
3220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003222
3223 with EXPR as VAR:
3224 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225
Guido van Rossumc2e20742006-02-27 22:32:47 +00003226 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227
Thomas Wouters477c8d52006-05-27 19:21:47 +00003228 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003229 exit = context.__exit__ # not calling it
3230 value = context.__enter__()
3231 try:
3232 VAR = value # if VAR present in the syntax
3233 BLOCK
3234 finally:
3235 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003237 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003239 exit(*exc)
3240 */
3241static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003242compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003243{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003244 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003245 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003246
3247 assert(s->kind == With_kind);
3248
Guido van Rossumc2e20742006-02-27 22:32:47 +00003249 block = compiler_new_block(c);
3250 finally = compiler_new_block(c);
3251 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003252 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003253
Thomas Wouters477c8d52006-05-27 19:21:47 +00003254 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003255 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003256 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003257
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003258 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003259 compiler_use_next_block(c, block);
3260 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003261 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003262 }
3263
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003264 if (item->optional_vars) {
3265 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003266 }
3267 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003269 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003270 }
3271
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003272 pos++;
3273 if (pos == asdl_seq_LEN(s->v.With.items))
3274 /* BLOCK code */
3275 VISIT_SEQ(c, stmt, s->v.With.body)
3276 else if (!compiler_with(c, s, pos))
3277 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003278
3279 /* End of try block; start the finally block */
3280 ADDOP(c, POP_BLOCK);
3281 compiler_pop_fblock(c, FINALLY_TRY, block);
3282
3283 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3284 compiler_use_next_block(c, finally);
3285 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003286 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003287
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003288 /* Finally block starts; context.__exit__ is on the stack under
3289 the exception or return information. Just issue our magic
3290 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003291 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003292
3293 /* Finally block ends. */
3294 ADDOP(c, END_FINALLY);
3295 compiler_pop_fblock(c, FINALLY_END, finally);
3296 return 1;
3297}
3298
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299static int
3300compiler_visit_expr(struct compiler *c, expr_ty e)
3301{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 /* If expr e has a different line number than the last expr/stmt,
3305 set a new line number for the next instruction.
3306 */
3307 if (e->lineno > c->u->u_lineno) {
3308 c->u->u_lineno = e->lineno;
3309 c->u->u_lineno_set = 0;
3310 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003311 /* Updating the column offset is always harmless. */
3312 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 switch (e->kind) {
3314 case BoolOp_kind:
3315 return compiler_boolop(c, e);
3316 case BinOp_kind:
3317 VISIT(c, expr, e->v.BinOp.left);
3318 VISIT(c, expr, e->v.BinOp.right);
3319 ADDOP(c, binop(c, e->v.BinOp.op));
3320 break;
3321 case UnaryOp_kind:
3322 VISIT(c, expr, e->v.UnaryOp.operand);
3323 ADDOP(c, unaryop(e->v.UnaryOp.op));
3324 break;
3325 case Lambda_kind:
3326 return compiler_lambda(c, e);
3327 case IfExp_kind:
3328 return compiler_ifexp(c, e);
3329 case Dict_kind:
3330 n = asdl_seq_LEN(e->v.Dict.values);
3331 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3332 for (i = 0; i < n; i++) {
3333 VISIT(c, expr,
3334 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3335 VISIT(c, expr,
3336 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3337 ADDOP(c, STORE_MAP);
3338 }
3339 break;
3340 case Set_kind:
3341 n = asdl_seq_LEN(e->v.Set.elts);
3342 VISIT_SEQ(c, expr, e->v.Set.elts);
3343 ADDOP_I(c, BUILD_SET, n);
3344 break;
3345 case GeneratorExp_kind:
3346 return compiler_genexp(c, e);
3347 case ListComp_kind:
3348 return compiler_listcomp(c, e);
3349 case SetComp_kind:
3350 return compiler_setcomp(c, e);
3351 case DictComp_kind:
3352 return compiler_dictcomp(c, e);
3353 case Yield_kind:
3354 if (c->u->u_ste->ste_type != FunctionBlock)
3355 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003356 if (e->v.Yield.value) {
3357 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 }
3359 else {
3360 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3361 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003362 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003364 case YieldFrom_kind:
3365 if (c->u->u_ste->ste_type != FunctionBlock)
3366 return compiler_error(c, "'yield' outside function");
3367 VISIT(c, expr, e->v.YieldFrom.value);
3368 ADDOP(c, GET_ITER);
3369 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3370 ADDOP(c, YIELD_FROM);
3371 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 case Compare_kind:
3373 return compiler_compare(c, e);
3374 case Call_kind:
3375 return compiler_call(c, e);
3376 case Num_kind:
3377 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3378 break;
3379 case Str_kind:
3380 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3381 break;
3382 case Bytes_kind:
3383 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3384 break;
3385 case Ellipsis_kind:
3386 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3387 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05003388 case NameConstant_kind:
3389 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3390 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 /* The following exprs can be assignment targets. */
3392 case Attribute_kind:
3393 if (e->v.Attribute.ctx != AugStore)
3394 VISIT(c, expr, e->v.Attribute.value);
3395 switch (e->v.Attribute.ctx) {
3396 case AugLoad:
3397 ADDOP(c, DUP_TOP);
3398 /* Fall through to load */
3399 case Load:
3400 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3401 break;
3402 case AugStore:
3403 ADDOP(c, ROT_TWO);
3404 /* Fall through to save */
3405 case Store:
3406 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3407 break;
3408 case Del:
3409 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3410 break;
3411 case Param:
3412 default:
3413 PyErr_SetString(PyExc_SystemError,
3414 "param invalid in attribute expression");
3415 return 0;
3416 }
3417 break;
3418 case Subscript_kind:
3419 switch (e->v.Subscript.ctx) {
3420 case AugLoad:
3421 VISIT(c, expr, e->v.Subscript.value);
3422 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3423 break;
3424 case Load:
3425 VISIT(c, expr, e->v.Subscript.value);
3426 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3427 break;
3428 case AugStore:
3429 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3430 break;
3431 case Store:
3432 VISIT(c, expr, e->v.Subscript.value);
3433 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3434 break;
3435 case Del:
3436 VISIT(c, expr, e->v.Subscript.value);
3437 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3438 break;
3439 case Param:
3440 default:
3441 PyErr_SetString(PyExc_SystemError,
3442 "param invalid in subscript expression");
3443 return 0;
3444 }
3445 break;
3446 case Starred_kind:
3447 switch (e->v.Starred.ctx) {
3448 case Store:
3449 /* In all legitimate cases, the Starred node was already replaced
3450 * by compiler_list/compiler_tuple. XXX: is that okay? */
3451 return compiler_error(c,
3452 "starred assignment target must be in a list or tuple");
3453 default:
3454 return compiler_error(c,
3455 "can use starred expression only as assignment target");
3456 }
3457 break;
3458 case Name_kind:
3459 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3460 /* child nodes of List and Tuple will have expr_context set */
3461 case List_kind:
3462 return compiler_list(c, e);
3463 case Tuple_kind:
3464 return compiler_tuple(c, e);
3465 }
3466 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003467}
3468
3469static int
3470compiler_augassign(struct compiler *c, stmt_ty s)
3471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 expr_ty e = s->v.AugAssign.target;
3473 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 switch (e->kind) {
3478 case Attribute_kind:
3479 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3480 AugLoad, e->lineno, e->col_offset, c->c_arena);
3481 if (auge == NULL)
3482 return 0;
3483 VISIT(c, expr, auge);
3484 VISIT(c, expr, s->v.AugAssign.value);
3485 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3486 auge->v.Attribute.ctx = AugStore;
3487 VISIT(c, expr, auge);
3488 break;
3489 case Subscript_kind:
3490 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3491 AugLoad, e->lineno, e->col_offset, c->c_arena);
3492 if (auge == NULL)
3493 return 0;
3494 VISIT(c, expr, auge);
3495 VISIT(c, expr, s->v.AugAssign.value);
3496 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3497 auge->v.Subscript.ctx = AugStore;
3498 VISIT(c, expr, auge);
3499 break;
3500 case Name_kind:
3501 if (!compiler_nameop(c, e->v.Name.id, Load))
3502 return 0;
3503 VISIT(c, expr, s->v.AugAssign.value);
3504 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3505 return compiler_nameop(c, e->v.Name.id, Store);
3506 default:
3507 PyErr_Format(PyExc_SystemError,
3508 "invalid node type (%d) for augmented assignment",
3509 e->kind);
3510 return 0;
3511 }
3512 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513}
3514
3515static int
3516compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 struct fblockinfo *f;
3519 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3520 PyErr_SetString(PyExc_SystemError,
3521 "too many statically nested blocks");
3522 return 0;
3523 }
3524 f = &c->u->u_fblock[c->u->u_nfblocks++];
3525 f->fb_type = t;
3526 f->fb_block = b;
3527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003528}
3529
3530static void
3531compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 struct compiler_unit *u = c->u;
3534 assert(u->u_nfblocks > 0);
3535 u->u_nfblocks--;
3536 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3537 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538}
3539
Thomas Wouters89f507f2006-12-13 04:49:30 +00003540static int
3541compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 int i;
3543 struct compiler_unit *u = c->u;
3544 for (i = 0; i < u->u_nfblocks; ++i) {
3545 if (u->u_fblock[i].fb_type == LOOP)
3546 return 1;
3547 }
3548 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003549}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550/* Raises a SyntaxError and returns 0.
3551 If something goes wrong, a different exception may be raised.
3552*/
3553
3554static int
3555compiler_error(struct compiler *c, const char *errstr)
3556{
Benjamin Peterson43b06862011-05-27 09:08:01 -05003557 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3561 if (!loc) {
3562 Py_INCREF(Py_None);
3563 loc = Py_None;
3564 }
Benjamin Peterson43b06862011-05-27 09:08:01 -05003565 u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003566 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 if (!u)
3568 goto exit;
3569 v = Py_BuildValue("(zO)", errstr, u);
3570 if (!v)
3571 goto exit;
3572 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003573 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 Py_DECREF(loc);
3575 Py_XDECREF(u);
3576 Py_XDECREF(v);
3577 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003578}
3579
3580static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581compiler_handle_subscr(struct compiler *c, const char *kind,
3582 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 /* XXX this code is duplicated */
3587 switch (ctx) {
3588 case AugLoad: /* fall through to Load */
3589 case Load: op = BINARY_SUBSCR; break;
3590 case AugStore:/* fall through to Store */
3591 case Store: op = STORE_SUBSCR; break;
3592 case Del: op = DELETE_SUBSCR; break;
3593 case Param:
3594 PyErr_Format(PyExc_SystemError,
3595 "invalid %s kind %d in subscript\n",
3596 kind, ctx);
3597 return 0;
3598 }
3599 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00003600 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 }
3602 else if (ctx == AugStore) {
3603 ADDOP(c, ROT_THREE);
3604 }
3605 ADDOP(c, op);
3606 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003607}
3608
3609static int
3610compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 int n = 2;
3613 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 /* only handles the cases where BUILD_SLICE is emitted */
3616 if (s->v.Slice.lower) {
3617 VISIT(c, expr, s->v.Slice.lower);
3618 }
3619 else {
3620 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3621 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 if (s->v.Slice.upper) {
3624 VISIT(c, expr, s->v.Slice.upper);
3625 }
3626 else {
3627 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3628 }
3629
3630 if (s->v.Slice.step) {
3631 n++;
3632 VISIT(c, expr, s->v.Slice.step);
3633 }
3634 ADDOP_I(c, BUILD_SLICE, n);
3635 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003636}
3637
3638static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3640 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 switch (s->kind) {
3643 case Slice_kind:
3644 return compiler_slice(c, s, ctx);
3645 case Index_kind:
3646 VISIT(c, expr, s->v.Index.value);
3647 break;
3648 case ExtSlice_kind:
3649 default:
3650 PyErr_SetString(PyExc_SystemError,
3651 "extended slice invalid in nested slice");
3652 return 0;
3653 }
3654 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003655}
3656
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003657static int
3658compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 char * kindname = NULL;
3661 switch (s->kind) {
3662 case Index_kind:
3663 kindname = "index";
3664 if (ctx != AugStore) {
3665 VISIT(c, expr, s->v.Index.value);
3666 }
3667 break;
3668 case Slice_kind:
3669 kindname = "slice";
3670 if (ctx != AugStore) {
3671 if (!compiler_slice(c, s, ctx))
3672 return 0;
3673 }
3674 break;
3675 case ExtSlice_kind:
3676 kindname = "extended slice";
3677 if (ctx != AugStore) {
3678 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3679 for (i = 0; i < n; i++) {
3680 slice_ty sub = (slice_ty)asdl_seq_GET(
3681 s->v.ExtSlice.dims, i);
3682 if (!compiler_visit_nested_slice(c, sub, ctx))
3683 return 0;
3684 }
3685 ADDOP_I(c, BUILD_TUPLE, n);
3686 }
3687 break;
3688 default:
3689 PyErr_Format(PyExc_SystemError,
3690 "invalid subscript kind %d", s->kind);
3691 return 0;
3692 }
3693 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003694}
3695
Thomas Wouters89f507f2006-12-13 04:49:30 +00003696/* End of the compiler section, beginning of the assembler section */
3697
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003698/* do depth-first search of basic block graph, starting with block.
3699 post records the block indices in post-order.
3700
3701 XXX must handle implicit jumps from one block to next
3702*/
3703
Thomas Wouters89f507f2006-12-13 04:49:30 +00003704struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 PyObject *a_bytecode; /* string containing bytecode */
3706 int a_offset; /* offset into bytecode */
3707 int a_nblocks; /* number of reachable blocks */
3708 basicblock **a_postorder; /* list of blocks in dfs postorder */
3709 PyObject *a_lnotab; /* string containing lnotab */
3710 int a_lnotab_off; /* offset into lnotab */
3711 int a_lineno; /* last lineno of emitted instruction */
3712 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003713};
3714
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003715static void
3716dfs(struct compiler *c, basicblock *b, struct assembler *a)
3717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 int i;
3719 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 if (b->b_seen)
3722 return;
3723 b->b_seen = 1;
3724 if (b->b_next != NULL)
3725 dfs(c, b->b_next, a);
3726 for (i = 0; i < b->b_iused; i++) {
3727 instr = &b->b_instr[i];
3728 if (instr->i_jrel || instr->i_jabs)
3729 dfs(c, instr->i_target, a);
3730 }
3731 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003732}
3733
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003734static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003735stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 int i, target_depth;
3738 struct instr *instr;
3739 if (b->b_seen || b->b_startdepth >= depth)
3740 return maxdepth;
3741 b->b_seen = 1;
3742 b->b_startdepth = depth;
3743 for (i = 0; i < b->b_iused; i++) {
3744 instr = &b->b_instr[i];
3745 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3746 if (depth > maxdepth)
3747 maxdepth = depth;
3748 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3749 if (instr->i_jrel || instr->i_jabs) {
3750 target_depth = depth;
3751 if (instr->i_opcode == FOR_ITER) {
3752 target_depth = depth-2;
3753 } else if (instr->i_opcode == SETUP_FINALLY ||
3754 instr->i_opcode == SETUP_EXCEPT) {
3755 target_depth = depth+3;
3756 if (target_depth > maxdepth)
3757 maxdepth = target_depth;
3758 }
3759 maxdepth = stackdepth_walk(c, instr->i_target,
3760 target_depth, maxdepth);
3761 if (instr->i_opcode == JUMP_ABSOLUTE ||
3762 instr->i_opcode == JUMP_FORWARD) {
3763 goto out; /* remaining code is dead */
3764 }
3765 }
3766 }
3767 if (b->b_next)
3768 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003769out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 b->b_seen = 0;
3771 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003772}
3773
3774/* Find the flow path that needs the largest stack. We assume that
3775 * cycles in the flow graph have no net effect on the stack depth.
3776 */
3777static int
3778stackdepth(struct compiler *c)
3779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 basicblock *b, *entryblock;
3781 entryblock = NULL;
3782 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3783 b->b_seen = 0;
3784 b->b_startdepth = INT_MIN;
3785 entryblock = b;
3786 }
3787 if (!entryblock)
3788 return 0;
3789 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003790}
3791
3792static int
3793assemble_init(struct assembler *a, int nblocks, int firstlineno)
3794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 memset(a, 0, sizeof(struct assembler));
3796 a->a_lineno = firstlineno;
3797 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3798 if (!a->a_bytecode)
3799 return 0;
3800 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3801 if (!a->a_lnotab)
3802 return 0;
3803 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3804 PyErr_NoMemory();
3805 return 0;
3806 }
3807 a->a_postorder = (basicblock **)PyObject_Malloc(
3808 sizeof(basicblock *) * nblocks);
3809 if (!a->a_postorder) {
3810 PyErr_NoMemory();
3811 return 0;
3812 }
3813 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003814}
3815
3816static void
3817assemble_free(struct assembler *a)
3818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 Py_XDECREF(a->a_bytecode);
3820 Py_XDECREF(a->a_lnotab);
3821 if (a->a_postorder)
3822 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003823}
3824
3825/* Return the size of a basic block in bytes. */
3826
3827static int
3828instrsize(struct instr *instr)
3829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 if (!instr->i_hasarg)
3831 return 1; /* 1 byte for the opcode*/
3832 if (instr->i_oparg > 0xffff)
3833 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3834 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003835}
3836
3837static int
3838blocksize(basicblock *b)
3839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 int i;
3841 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 for (i = 0; i < b->b_iused; i++)
3844 size += instrsize(&b->b_instr[i]);
3845 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003846}
3847
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003848/* Appends a pair to the end of the line number table, a_lnotab, representing
3849 the instruction's bytecode offset and line number. See
3850 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003851
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003852static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003853assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 int d_bytecode, d_lineno;
3856 int len;
3857 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 d_bytecode = a->a_offset - a->a_lineno_off;
3860 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 assert(d_bytecode >= 0);
3863 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 if(d_bytecode == 0 && d_lineno == 0)
3866 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003868 if (d_bytecode > 255) {
3869 int j, nbytes, ncodes = d_bytecode / 255;
3870 nbytes = a->a_lnotab_off + 2 * ncodes;
3871 len = PyBytes_GET_SIZE(a->a_lnotab);
3872 if (nbytes >= len) {
3873 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3874 len = nbytes;
3875 else if (len <= INT_MAX / 2)
3876 len *= 2;
3877 else {
3878 PyErr_NoMemory();
3879 return 0;
3880 }
3881 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3882 return 0;
3883 }
3884 lnotab = (unsigned char *)
3885 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3886 for (j = 0; j < ncodes; j++) {
3887 *lnotab++ = 255;
3888 *lnotab++ = 0;
3889 }
3890 d_bytecode -= ncodes * 255;
3891 a->a_lnotab_off += ncodes * 2;
3892 }
3893 assert(d_bytecode <= 255);
3894 if (d_lineno > 255) {
3895 int j, nbytes, ncodes = d_lineno / 255;
3896 nbytes = a->a_lnotab_off + 2 * ncodes;
3897 len = PyBytes_GET_SIZE(a->a_lnotab);
3898 if (nbytes >= len) {
3899 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3900 len = nbytes;
3901 else if (len <= INT_MAX / 2)
3902 len *= 2;
3903 else {
3904 PyErr_NoMemory();
3905 return 0;
3906 }
3907 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3908 return 0;
3909 }
3910 lnotab = (unsigned char *)
3911 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3912 *lnotab++ = d_bytecode;
3913 *lnotab++ = 255;
3914 d_bytecode = 0;
3915 for (j = 1; j < ncodes; j++) {
3916 *lnotab++ = 0;
3917 *lnotab++ = 255;
3918 }
3919 d_lineno -= ncodes * 255;
3920 a->a_lnotab_off += ncodes * 2;
3921 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 len = PyBytes_GET_SIZE(a->a_lnotab);
3924 if (a->a_lnotab_off + 2 >= len) {
3925 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3926 return 0;
3927 }
3928 lnotab = (unsigned char *)
3929 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 a->a_lnotab_off += 2;
3932 if (d_bytecode) {
3933 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003934 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 }
3936 else { /* First line of a block; def stmt, etc. */
3937 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003938 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 }
3940 a->a_lineno = i->i_lineno;
3941 a->a_lineno_off = a->a_offset;
3942 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003943}
3944
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003945/* assemble_emit()
3946 Extend the bytecode with a new instruction.
3947 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003948*/
3949
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003950static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003951assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 int size, arg = 0, ext = 0;
3954 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3955 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 size = instrsize(i);
3958 if (i->i_hasarg) {
3959 arg = i->i_oparg;
3960 ext = arg >> 16;
3961 }
3962 if (i->i_lineno && !assemble_lnotab(a, i))
3963 return 0;
3964 if (a->a_offset + size >= len) {
3965 if (len > PY_SSIZE_T_MAX / 2)
3966 return 0;
3967 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3968 return 0;
3969 }
3970 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3971 a->a_offset += size;
3972 if (size == 6) {
3973 assert(i->i_hasarg);
3974 *code++ = (char)EXTENDED_ARG;
3975 *code++ = ext & 0xff;
3976 *code++ = ext >> 8;
3977 arg &= 0xffff;
3978 }
3979 *code++ = i->i_opcode;
3980 if (i->i_hasarg) {
3981 assert(size == 3 || size == 6);
3982 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003983 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 }
3985 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003986}
3987
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003988static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003989assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 basicblock *b;
3992 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3993 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 /* Compute the size of each block and fixup jump args.
3996 Replace block pointer with position in bytecode. */
3997 do {
3998 totsize = 0;
3999 for (i = a->a_nblocks - 1; i >= 0; i--) {
4000 b = a->a_postorder[i];
4001 bsize = blocksize(b);
4002 b->b_offset = totsize;
4003 totsize += bsize;
4004 }
4005 last_extended_arg_count = extended_arg_count;
4006 extended_arg_count = 0;
4007 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4008 bsize = b->b_offset;
4009 for (i = 0; i < b->b_iused; i++) {
4010 struct instr *instr = &b->b_instr[i];
4011 /* Relative jumps are computed relative to
4012 the instruction pointer after fetching
4013 the jump instruction.
4014 */
4015 bsize += instrsize(instr);
4016 if (instr->i_jabs)
4017 instr->i_oparg = instr->i_target->b_offset;
4018 else if (instr->i_jrel) {
4019 int delta = instr->i_target->b_offset - bsize;
4020 instr->i_oparg = delta;
4021 }
4022 else
4023 continue;
4024 if (instr->i_oparg > 0xffff)
4025 extended_arg_count++;
4026 }
4027 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 /* XXX: This is an awful hack that could hurt performance, but
4030 on the bright side it should work until we come up
4031 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004032
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 The issue is that in the first loop blocksize() is called
4034 which calls instrsize() which requires i_oparg be set
4035 appropriately. There is a bootstrap problem because
4036 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 So we loop until we stop seeing new EXTENDED_ARGs.
4039 The only EXTENDED_ARGs that could be popping up are
4040 ones in jump instructions. So this should converge
4041 fairly quickly.
4042 */
4043 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004044}
4045
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004046static PyObject *
4047dict_keys_inorder(PyObject *dict, int offset)
4048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 PyObject *tuple, *k, *v;
4050 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 tuple = PyTuple_New(size);
4053 if (tuple == NULL)
4054 return NULL;
4055 while (PyDict_Next(dict, &pos, &k, &v)) {
4056 i = PyLong_AS_LONG(v);
4057 /* The keys of the dictionary are tuples. (see compiler_add_o)
4058 The object we want is always first, though. */
4059 k = PyTuple_GET_ITEM(k, 0);
4060 Py_INCREF(k);
4061 assert((i - offset) < size);
4062 assert((i - offset) >= 0);
4063 PyTuple_SET_ITEM(tuple, i - offset, k);
4064 }
4065 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004066}
4067
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004068static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004069compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004070{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 PySTEntryObject *ste = c->u->u_ste;
4072 int flags = 0, n;
4073 if (ste->ste_type != ModuleBlock)
4074 flags |= CO_NEWLOCALS;
4075 if (ste->ste_type == FunctionBlock) {
4076 if (!ste->ste_unoptimized)
4077 flags |= CO_OPTIMIZED;
4078 if (ste->ste_nested)
4079 flags |= CO_NESTED;
4080 if (ste->ste_generator)
4081 flags |= CO_GENERATOR;
4082 if (ste->ste_varargs)
4083 flags |= CO_VARARGS;
4084 if (ste->ste_varkeywords)
4085 flags |= CO_VARKEYWORDS;
4086 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 /* (Only) inherit compilerflags in PyCF_MASK */
4089 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 n = PyDict_Size(c->u->u_freevars);
4092 if (n < 0)
4093 return -1;
4094 if (n == 0) {
4095 n = PyDict_Size(c->u->u_cellvars);
4096 if (n < 0)
4097 return -1;
4098 if (n == 0) {
4099 flags |= CO_NOFREE;
4100 }
4101 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004104}
4105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004106static PyCodeObject *
4107makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 PyObject *tmp;
4110 PyCodeObject *co = NULL;
4111 PyObject *consts = NULL;
4112 PyObject *names = NULL;
4113 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 PyObject *name = NULL;
4115 PyObject *freevars = NULL;
4116 PyObject *cellvars = NULL;
4117 PyObject *bytecode = NULL;
4118 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 tmp = dict_keys_inorder(c->u->u_consts, 0);
4121 if (!tmp)
4122 goto error;
4123 consts = PySequence_List(tmp); /* optimize_code requires a list */
4124 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 names = dict_keys_inorder(c->u->u_names, 0);
4127 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4128 if (!consts || !names || !varnames)
4129 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4132 if (!cellvars)
4133 goto error;
4134 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4135 if (!freevars)
4136 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 nlocals = PyDict_Size(c->u->u_varnames);
4138 flags = compute_code_flags(c);
4139 if (flags < 0)
4140 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4143 if (!bytecode)
4144 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4147 if (!tmp)
4148 goto error;
4149 Py_DECREF(consts);
4150 consts = tmp;
4151
4152 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4153 nlocals, stackdepth(c), flags,
4154 bytecode, consts, names, varnames,
4155 freevars, cellvars,
Benjamin Peterson43b06862011-05-27 09:08:01 -05004156 c->c_filename_obj, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 c->u->u_firstlineno,
4158 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004159 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 Py_XDECREF(consts);
4161 Py_XDECREF(names);
4162 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 Py_XDECREF(name);
4164 Py_XDECREF(freevars);
4165 Py_XDECREF(cellvars);
4166 Py_XDECREF(bytecode);
4167 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004168}
4169
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004170
4171/* For debugging purposes only */
4172#if 0
4173static void
4174dump_instr(const struct instr *i)
4175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 const char *jrel = i->i_jrel ? "jrel " : "";
4177 const char *jabs = i->i_jabs ? "jabs " : "";
4178 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 *arg = '\0';
4181 if (i->i_hasarg)
4182 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4185 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004186}
4187
4188static void
4189dump_basicblock(const basicblock *b)
4190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 const char *seen = b->b_seen ? "seen " : "";
4192 const char *b_return = b->b_return ? "return " : "";
4193 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4194 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4195 if (b->b_instr) {
4196 int i;
4197 for (i = 0; i < b->b_iused; i++) {
4198 fprintf(stderr, " [%02d] ", i);
4199 dump_instr(b->b_instr + i);
4200 }
4201 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004202}
4203#endif
4204
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004205static PyCodeObject *
4206assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 basicblock *b, *entryblock;
4209 struct assembler a;
4210 int i, j, nblocks;
4211 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 /* Make sure every block that falls off the end returns None.
4214 XXX NEXT_BLOCK() isn't quite right, because if the last
4215 block ends with a jump or return b_next shouldn't set.
4216 */
4217 if (!c->u->u_curblock->b_return) {
4218 NEXT_BLOCK(c);
4219 if (addNone)
4220 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4221 ADDOP(c, RETURN_VALUE);
4222 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 nblocks = 0;
4225 entryblock = NULL;
4226 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4227 nblocks++;
4228 entryblock = b;
4229 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 /* Set firstlineno if it wasn't explicitly set. */
4232 if (!c->u->u_firstlineno) {
4233 if (entryblock && entryblock->b_instr)
4234 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4235 else
4236 c->u->u_firstlineno = 1;
4237 }
4238 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4239 goto error;
4240 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 /* Can't modify the bytecode after computing jump offsets. */
4243 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 /* Emit code in reverse postorder from dfs. */
4246 for (i = a.a_nblocks - 1; i >= 0; i--) {
4247 b = a.a_postorder[i];
4248 for (j = 0; j < b->b_iused; j++)
4249 if (!assemble_emit(&a, &b->b_instr[j]))
4250 goto error;
4251 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4254 goto error;
4255 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4256 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004259 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 assemble_free(&a);
4261 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004262}
Georg Brandl8334fd92010-12-04 10:26:46 +00004263
4264#undef PyAST_Compile
4265PyAPI_FUNC(PyCodeObject *)
4266PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4267 PyArena *arena)
4268{
4269 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4270}
4271
4272