blob: 3cf71ef09e2d27b3e93fbd16be1558f406d4aca7 [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;
1501 if (args->varargannotation &&
1502 compiler_visit_argannotation(c, args->vararg,
1503 args->varargannotation, names))
1504 goto error;
1505 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1506 goto error;
1507 if (args->kwargannotation &&
1508 compiler_visit_argannotation(c, args->kwarg,
1509 args->kwargannotation, names))
1510 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;
1568 if (args->kwonlyargs) {
1569 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1570 args->kw_defaults);
1571 if (res < 0)
1572 return 0;
1573 kw_default_count = res;
1574 }
1575 if (args->defaults)
1576 VISIT_SEQ(c, expr, args->defaults);
1577 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 if (args->kwonlyargs) {
1798 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1799 args->kw_defaults);
1800 if (res < 0) return 0;
1801 kw_default_count = res;
1802 }
1803 if (args->defaults)
1804 VISIT_SEQ(c, expr, args->defaults);
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 op = 0;
2639 optype = OP_NAME;
2640 scope = PyST_GetScope(c->u->u_ste, mangled);
2641 switch (scope) {
2642 case FREE:
2643 dict = c->u->u_freevars;
2644 optype = OP_DEREF;
2645 break;
2646 case CELL:
2647 dict = c->u->u_cellvars;
2648 optype = OP_DEREF;
2649 break;
2650 case LOCAL:
2651 if (c->u->u_ste->ste_type == FunctionBlock)
2652 optype = OP_FAST;
2653 break;
2654 case GLOBAL_IMPLICIT:
2655 if (c->u->u_ste->ste_type == FunctionBlock &&
2656 !c->u->u_ste->ste_unoptimized)
2657 optype = OP_GLOBAL;
2658 break;
2659 case GLOBAL_EXPLICIT:
2660 optype = OP_GLOBAL;
2661 break;
2662 default:
2663 /* scope can be 0 */
2664 break;
2665 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002668 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 switch (optype) {
2671 case OP_DEREF:
2672 switch (ctx) {
2673 case Load: op = LOAD_DEREF; break;
2674 case Store: op = STORE_DEREF; break;
2675 case AugLoad:
2676 case AugStore:
2677 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002678 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 case Param:
2680 default:
2681 PyErr_SetString(PyExc_SystemError,
2682 "param invalid for deref variable");
2683 return 0;
2684 }
2685 break;
2686 case OP_FAST:
2687 switch (ctx) {
2688 case Load: op = LOAD_FAST; break;
2689 case Store: op = STORE_FAST; break;
2690 case Del: op = DELETE_FAST; break;
2691 case AugLoad:
2692 case AugStore:
2693 break;
2694 case Param:
2695 default:
2696 PyErr_SetString(PyExc_SystemError,
2697 "param invalid for local variable");
2698 return 0;
2699 }
2700 ADDOP_O(c, op, mangled, varnames);
2701 Py_DECREF(mangled);
2702 return 1;
2703 case OP_GLOBAL:
2704 switch (ctx) {
2705 case Load: op = LOAD_GLOBAL; break;
2706 case Store: op = STORE_GLOBAL; break;
2707 case Del: op = DELETE_GLOBAL; break;
2708 case AugLoad:
2709 case AugStore:
2710 break;
2711 case Param:
2712 default:
2713 PyErr_SetString(PyExc_SystemError,
2714 "param invalid for global variable");
2715 return 0;
2716 }
2717 break;
2718 case OP_NAME:
2719 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002720 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 case Store: op = STORE_NAME; break;
2722 case Del: op = DELETE_NAME; break;
2723 case AugLoad:
2724 case AugStore:
2725 break;
2726 case Param:
2727 default:
2728 PyErr_SetString(PyExc_SystemError,
2729 "param invalid for name variable");
2730 return 0;
2731 }
2732 break;
2733 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 assert(op);
2736 arg = compiler_add_o(c, dict, mangled);
2737 Py_DECREF(mangled);
2738 if (arg < 0)
2739 return 0;
2740 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741}
2742
2743static int
2744compiler_boolop(struct compiler *c, expr_ty e)
2745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 basicblock *end;
2747 int jumpi, i, n;
2748 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 assert(e->kind == BoolOp_kind);
2751 if (e->v.BoolOp.op == And)
2752 jumpi = JUMP_IF_FALSE_OR_POP;
2753 else
2754 jumpi = JUMP_IF_TRUE_OR_POP;
2755 end = compiler_new_block(c);
2756 if (end == NULL)
2757 return 0;
2758 s = e->v.BoolOp.values;
2759 n = asdl_seq_LEN(s) - 1;
2760 assert(n >= 0);
2761 for (i = 0; i < n; ++i) {
2762 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2763 ADDOP_JABS(c, jumpi, end);
2764 }
2765 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2766 compiler_use_next_block(c, end);
2767 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002768}
2769
2770static int
2771compiler_list(struct compiler *c, expr_ty e)
2772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 int n = asdl_seq_LEN(e->v.List.elts);
2774 if (e->v.List.ctx == Store) {
2775 int i, seen_star = 0;
2776 for (i = 0; i < n; i++) {
2777 expr_ty elt = asdl_seq_GET(e->v.List.elts, i);
2778 if (elt->kind == Starred_kind && !seen_star) {
2779 if ((i >= (1 << 8)) ||
2780 (n-i-1 >= (INT_MAX >> 8)))
2781 return compiler_error(c,
2782 "too many expressions in "
2783 "star-unpacking assignment");
2784 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2785 seen_star = 1;
2786 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2787 } else if (elt->kind == Starred_kind) {
2788 return compiler_error(c,
2789 "two starred expressions in assignment");
2790 }
2791 }
2792 if (!seen_star) {
2793 ADDOP_I(c, UNPACK_SEQUENCE, n);
2794 }
2795 }
2796 VISIT_SEQ(c, expr, e->v.List.elts);
2797 if (e->v.List.ctx == Load) {
2798 ADDOP_I(c, BUILD_LIST, n);
2799 }
2800 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002801}
2802
2803static int
2804compiler_tuple(struct compiler *c, expr_ty e)
2805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 int n = asdl_seq_LEN(e->v.Tuple.elts);
2807 if (e->v.Tuple.ctx == Store) {
2808 int i, seen_star = 0;
2809 for (i = 0; i < n; i++) {
2810 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i);
2811 if (elt->kind == Starred_kind && !seen_star) {
2812 if ((i >= (1 << 8)) ||
2813 (n-i-1 >= (INT_MAX >> 8)))
2814 return compiler_error(c,
2815 "too many expressions in "
2816 "star-unpacking assignment");
2817 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2818 seen_star = 1;
2819 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value);
2820 } else if (elt->kind == Starred_kind) {
2821 return compiler_error(c,
2822 "two starred expressions in assignment");
2823 }
2824 }
2825 if (!seen_star) {
2826 ADDOP_I(c, UNPACK_SEQUENCE, n);
2827 }
2828 }
2829 VISIT_SEQ(c, expr, e->v.Tuple.elts);
2830 if (e->v.Tuple.ctx == Load) {
2831 ADDOP_I(c, BUILD_TUPLE, n);
2832 }
2833 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834}
2835
2836static int
2837compiler_compare(struct compiler *c, expr_ty e)
2838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 int i, n;
2840 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
2843 VISIT(c, expr, e->v.Compare.left);
2844 n = asdl_seq_LEN(e->v.Compare.ops);
2845 assert(n > 0);
2846 if (n > 1) {
2847 cleanup = compiler_new_block(c);
2848 if (cleanup == NULL)
2849 return 0;
2850 VISIT(c, expr,
2851 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
2852 }
2853 for (i = 1; i < n; i++) {
2854 ADDOP(c, DUP_TOP);
2855 ADDOP(c, ROT_THREE);
2856 ADDOP_I(c, COMPARE_OP,
2857 cmpop((cmpop_ty)(asdl_seq_GET(
2858 e->v.Compare.ops, i - 1))));
2859 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
2860 NEXT_BLOCK(c);
2861 if (i < (n - 1))
2862 VISIT(c, expr,
2863 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2864 }
2865 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
2866 ADDOP_I(c, COMPARE_OP,
2867 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
2868 if (n > 1) {
2869 basicblock *end = compiler_new_block(c);
2870 if (end == NULL)
2871 return 0;
2872 ADDOP_JREL(c, JUMP_FORWARD, end);
2873 compiler_use_next_block(c, cleanup);
2874 ADDOP(c, ROT_TWO);
2875 ADDOP(c, POP_TOP);
2876 compiler_use_next_block(c, end);
2877 }
2878 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879}
2880
2881static int
2882compiler_call(struct compiler *c, expr_ty e)
2883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 VISIT(c, expr, e->v.Call.func);
2885 return compiler_call_helper(c, 0,
2886 e->v.Call.args,
2887 e->v.Call.keywords,
2888 e->v.Call.starargs,
2889 e->v.Call.kwargs);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002890}
2891
2892/* shared code between compiler_call and compiler_class */
2893static int
2894compiler_call_helper(struct compiler *c,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002895 int n, /* Args already pushed */
2896 asdl_seq *args,
2897 asdl_seq *keywords,
2898 expr_ty starargs,
2899 expr_ty kwargs)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 int code = 0;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 n += asdl_seq_LEN(args);
2904 VISIT_SEQ(c, expr, args);
2905 if (keywords) {
2906 VISIT_SEQ(c, keyword, keywords);
2907 n |= asdl_seq_LEN(keywords) << 8;
2908 }
2909 if (starargs) {
2910 VISIT(c, expr, starargs);
2911 code |= 1;
2912 }
2913 if (kwargs) {
2914 VISIT(c, expr, kwargs);
2915 code |= 2;
2916 }
2917 switch (code) {
2918 case 0:
2919 ADDOP_I(c, CALL_FUNCTION, n);
2920 break;
2921 case 1:
2922 ADDOP_I(c, CALL_FUNCTION_VAR, n);
2923 break;
2924 case 2:
2925 ADDOP_I(c, CALL_FUNCTION_KW, n);
2926 break;
2927 case 3:
2928 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
2929 break;
2930 }
2931 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002932}
2933
Nick Coghlan650f0d02007-04-15 12:05:43 +00002934
2935/* List and set comprehensions and generator expressions work by creating a
2936 nested function to perform the actual iteration. This means that the
2937 iteration variables don't leak into the current scope.
2938 The defined function is called immediately following its definition, with the
2939 result of that call being the result of the expression.
2940 The LC/SC version returns the populated container, while the GE version is
2941 flagged in symtable.c as a generator, so it returns the generator object
2942 when the function is called.
2943 This code *knows* that the loop cannot contain break, continue, or return,
2944 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
2945
2946 Possible cleanups:
2947 - iterate over the generator sequence instead of using recursion
2948*/
2949
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002950static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951compiler_comprehension_generator(struct compiler *c,
2952 asdl_seq *generators, int gen_index,
2953 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 /* generate code for the iterator, then each of the ifs,
2956 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 comprehension_ty gen;
2959 basicblock *start, *anchor, *skip, *if_cleanup;
2960 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 start = compiler_new_block(c);
2963 skip = compiler_new_block(c);
2964 if_cleanup = compiler_new_block(c);
2965 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 if (start == NULL || skip == NULL || if_cleanup == NULL ||
2968 anchor == NULL)
2969 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 if (gen_index == 0) {
2974 /* Receive outermost iter as an implicit argument */
2975 c->u->u_argcount = 1;
2976 ADDOP_I(c, LOAD_FAST, 0);
2977 }
2978 else {
2979 /* Sub-iter - calculate on the fly */
2980 VISIT(c, expr, gen->iter);
2981 ADDOP(c, GET_ITER);
2982 }
2983 compiler_use_next_block(c, start);
2984 ADDOP_JREL(c, FOR_ITER, anchor);
2985 NEXT_BLOCK(c);
2986 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 /* XXX this needs to be cleaned up...a lot! */
2989 n = asdl_seq_LEN(gen->ifs);
2990 for (i = 0; i < n; i++) {
2991 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
2992 VISIT(c, expr, e);
2993 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
2994 NEXT_BLOCK(c);
2995 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 if (++gen_index < asdl_seq_LEN(generators))
2998 if (!compiler_comprehension_generator(c,
2999 generators, gen_index,
3000 elt, val, type))
3001 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 /* only append after the last for generator */
3004 if (gen_index >= asdl_seq_LEN(generators)) {
3005 /* comprehension specific code */
3006 switch (type) {
3007 case COMP_GENEXP:
3008 VISIT(c, expr, elt);
3009 ADDOP(c, YIELD_VALUE);
3010 ADDOP(c, POP_TOP);
3011 break;
3012 case COMP_LISTCOMP:
3013 VISIT(c, expr, elt);
3014 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3015 break;
3016 case COMP_SETCOMP:
3017 VISIT(c, expr, elt);
3018 ADDOP_I(c, SET_ADD, gen_index + 1);
3019 break;
3020 case COMP_DICTCOMP:
3021 /* With 'd[k] = v', v is evaluated before k, so we do
3022 the same. */
3023 VISIT(c, expr, val);
3024 VISIT(c, expr, elt);
3025 ADDOP_I(c, MAP_ADD, gen_index + 1);
3026 break;
3027 default:
3028 return 0;
3029 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 compiler_use_next_block(c, skip);
3032 }
3033 compiler_use_next_block(c, if_cleanup);
3034 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3035 compiler_use_next_block(c, anchor);
3036
3037 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003038}
3039
3040static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003041compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 asdl_seq *generators, expr_ty elt, expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 PyCodeObject *co = NULL;
3045 expr_ty outermost_iter;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003046 PyObject *qualname = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 outermost_iter = ((comprehension_ty)
3049 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003050
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003051 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3052 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 if (type != COMP_GENEXP) {
3056 int op;
3057 switch (type) {
3058 case COMP_LISTCOMP:
3059 op = BUILD_LIST;
3060 break;
3061 case COMP_SETCOMP:
3062 op = BUILD_SET;
3063 break;
3064 case COMP_DICTCOMP:
3065 op = BUILD_MAP;
3066 break;
3067 default:
3068 PyErr_Format(PyExc_SystemError,
3069 "unknown comprehension type %d", type);
3070 goto error_in_scope;
3071 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 ADDOP_I(c, op, 0);
3074 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 if (!compiler_comprehension_generator(c, generators, 0, elt,
3077 val, type))
3078 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 if (type != COMP_GENEXP) {
3081 ADDOP(c, RETURN_VALUE);
3082 }
3083
3084 co = assemble(c, 1);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003085 qualname = compiler_scope_qualname(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 compiler_exit_scope(c);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003087 if (qualname == NULL || co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 goto error;
3089
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003090 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003092 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 Py_DECREF(co);
3094
3095 VISIT(c, expr, outermost_iter);
3096 ADDOP(c, GET_ITER);
3097 ADDOP_I(c, CALL_FUNCTION, 1);
3098 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003099error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003101error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003102 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 Py_XDECREF(co);
3104 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003105}
3106
3107static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003108compiler_genexp(struct compiler *c, expr_ty e)
3109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 static identifier name;
3111 if (!name) {
3112 name = PyUnicode_FromString("<genexpr>");
3113 if (!name)
3114 return 0;
3115 }
3116 assert(e->kind == GeneratorExp_kind);
3117 return compiler_comprehension(c, e, COMP_GENEXP, name,
3118 e->v.GeneratorExp.generators,
3119 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120}
3121
3122static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003123compiler_listcomp(struct compiler *c, expr_ty e)
3124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 static identifier name;
3126 if (!name) {
3127 name = PyUnicode_FromString("<listcomp>");
3128 if (!name)
3129 return 0;
3130 }
3131 assert(e->kind == ListComp_kind);
3132 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3133 e->v.ListComp.generators,
3134 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003135}
3136
3137static int
3138compiler_setcomp(struct compiler *c, expr_ty e)
3139{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 static identifier name;
3141 if (!name) {
3142 name = PyUnicode_FromString("<setcomp>");
3143 if (!name)
3144 return 0;
3145 }
3146 assert(e->kind == SetComp_kind);
3147 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3148 e->v.SetComp.generators,
3149 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003150}
3151
3152
3153static int
3154compiler_dictcomp(struct compiler *c, expr_ty e)
3155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 static identifier name;
3157 if (!name) {
3158 name = PyUnicode_FromString("<dictcomp>");
3159 if (!name)
3160 return 0;
3161 }
3162 assert(e->kind == DictComp_kind);
3163 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3164 e->v.DictComp.generators,
3165 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003166}
3167
3168
3169static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003170compiler_visit_keyword(struct compiler *c, keyword_ty k)
3171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003172 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3173 VISIT(c, expr, k->value);
3174 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003175}
3176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003178 whether they are true or false.
3179
3180 Return values: 1 for true, 0 for false, -1 for non-constant.
3181 */
3182
3183static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003184expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003185{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 char *id;
3187 switch (e->kind) {
3188 case Ellipsis_kind:
3189 return 1;
3190 case Num_kind:
3191 return PyObject_IsTrue(e->v.Num.n);
3192 case Str_kind:
3193 return PyObject_IsTrue(e->v.Str.s);
3194 case Name_kind:
3195 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003196 id = PyUnicode_AsUTF8(e->v.Name.id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 if (strcmp(id, "True") == 0) return 1;
3198 if (strcmp(id, "False") == 0) return 0;
3199 if (strcmp(id, "None") == 0) return 0;
3200 if (strcmp(id, "__debug__") == 0)
Georg Brandl8334fd92010-12-04 10:26:46 +00003201 return ! c->c_optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 /* fall through */
3203 default:
3204 return -1;
3205 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003206}
3207
Guido van Rossumc2e20742006-02-27 22:32:47 +00003208/*
3209 Implements the with statement from PEP 343.
3210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003212
3213 with EXPR as VAR:
3214 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215
Guido van Rossumc2e20742006-02-27 22:32:47 +00003216 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217
Thomas Wouters477c8d52006-05-27 19:21:47 +00003218 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003219 exit = context.__exit__ # not calling it
3220 value = context.__enter__()
3221 try:
3222 VAR = value # if VAR present in the syntax
3223 BLOCK
3224 finally:
3225 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003227 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003229 exit(*exc)
3230 */
3231static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003232compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003233{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003234 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003235 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003236
3237 assert(s->kind == With_kind);
3238
Guido van Rossumc2e20742006-02-27 22:32:47 +00003239 block = compiler_new_block(c);
3240 finally = compiler_new_block(c);
3241 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003242 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003243
Thomas Wouters477c8d52006-05-27 19:21:47 +00003244 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003245 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003246 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003247
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003248 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003249 compiler_use_next_block(c, block);
3250 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003251 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003252 }
3253
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003254 if (item->optional_vars) {
3255 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003256 }
3257 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003259 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003260 }
3261
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003262 pos++;
3263 if (pos == asdl_seq_LEN(s->v.With.items))
3264 /* BLOCK code */
3265 VISIT_SEQ(c, stmt, s->v.With.body)
3266 else if (!compiler_with(c, s, pos))
3267 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003268
3269 /* End of try block; start the finally block */
3270 ADDOP(c, POP_BLOCK);
3271 compiler_pop_fblock(c, FINALLY_TRY, block);
3272
3273 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3274 compiler_use_next_block(c, finally);
3275 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003276 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003277
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003278 /* Finally block starts; context.__exit__ is on the stack under
3279 the exception or return information. Just issue our magic
3280 opcode. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003281 ADDOP(c, WITH_CLEANUP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003282
3283 /* Finally block ends. */
3284 ADDOP(c, END_FINALLY);
3285 compiler_pop_fblock(c, FINALLY_END, finally);
3286 return 1;
3287}
3288
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289static int
3290compiler_visit_expr(struct compiler *c, expr_ty e)
3291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 int i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 /* If expr e has a different line number than the last expr/stmt,
3295 set a new line number for the next instruction.
3296 */
3297 if (e->lineno > c->u->u_lineno) {
3298 c->u->u_lineno = e->lineno;
3299 c->u->u_lineno_set = 0;
3300 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003301 /* Updating the column offset is always harmless. */
3302 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 switch (e->kind) {
3304 case BoolOp_kind:
3305 return compiler_boolop(c, e);
3306 case BinOp_kind:
3307 VISIT(c, expr, e->v.BinOp.left);
3308 VISIT(c, expr, e->v.BinOp.right);
3309 ADDOP(c, binop(c, e->v.BinOp.op));
3310 break;
3311 case UnaryOp_kind:
3312 VISIT(c, expr, e->v.UnaryOp.operand);
3313 ADDOP(c, unaryop(e->v.UnaryOp.op));
3314 break;
3315 case Lambda_kind:
3316 return compiler_lambda(c, e);
3317 case IfExp_kind:
3318 return compiler_ifexp(c, e);
3319 case Dict_kind:
3320 n = asdl_seq_LEN(e->v.Dict.values);
3321 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3322 for (i = 0; i < n; i++) {
3323 VISIT(c, expr,
3324 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3325 VISIT(c, expr,
3326 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3327 ADDOP(c, STORE_MAP);
3328 }
3329 break;
3330 case Set_kind:
3331 n = asdl_seq_LEN(e->v.Set.elts);
3332 VISIT_SEQ(c, expr, e->v.Set.elts);
3333 ADDOP_I(c, BUILD_SET, n);
3334 break;
3335 case GeneratorExp_kind:
3336 return compiler_genexp(c, e);
3337 case ListComp_kind:
3338 return compiler_listcomp(c, e);
3339 case SetComp_kind:
3340 return compiler_setcomp(c, e);
3341 case DictComp_kind:
3342 return compiler_dictcomp(c, e);
3343 case Yield_kind:
3344 if (c->u->u_ste->ste_type != FunctionBlock)
3345 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003346 if (e->v.Yield.value) {
3347 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 }
3349 else {
3350 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3351 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003352 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003354 case YieldFrom_kind:
3355 if (c->u->u_ste->ste_type != FunctionBlock)
3356 return compiler_error(c, "'yield' outside function");
3357 VISIT(c, expr, e->v.YieldFrom.value);
3358 ADDOP(c, GET_ITER);
3359 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3360 ADDOP(c, YIELD_FROM);
3361 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 case Compare_kind:
3363 return compiler_compare(c, e);
3364 case Call_kind:
3365 return compiler_call(c, e);
3366 case Num_kind:
3367 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3368 break;
3369 case Str_kind:
3370 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3371 break;
3372 case Bytes_kind:
3373 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3374 break;
3375 case Ellipsis_kind:
3376 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3377 break;
3378 /* The following exprs can be assignment targets. */
3379 case Attribute_kind:
3380 if (e->v.Attribute.ctx != AugStore)
3381 VISIT(c, expr, e->v.Attribute.value);
3382 switch (e->v.Attribute.ctx) {
3383 case AugLoad:
3384 ADDOP(c, DUP_TOP);
3385 /* Fall through to load */
3386 case Load:
3387 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3388 break;
3389 case AugStore:
3390 ADDOP(c, ROT_TWO);
3391 /* Fall through to save */
3392 case Store:
3393 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3394 break;
3395 case Del:
3396 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3397 break;
3398 case Param:
3399 default:
3400 PyErr_SetString(PyExc_SystemError,
3401 "param invalid in attribute expression");
3402 return 0;
3403 }
3404 break;
3405 case Subscript_kind:
3406 switch (e->v.Subscript.ctx) {
3407 case AugLoad:
3408 VISIT(c, expr, e->v.Subscript.value);
3409 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3410 break;
3411 case Load:
3412 VISIT(c, expr, e->v.Subscript.value);
3413 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3414 break;
3415 case AugStore:
3416 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3417 break;
3418 case Store:
3419 VISIT(c, expr, e->v.Subscript.value);
3420 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3421 break;
3422 case Del:
3423 VISIT(c, expr, e->v.Subscript.value);
3424 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3425 break;
3426 case Param:
3427 default:
3428 PyErr_SetString(PyExc_SystemError,
3429 "param invalid in subscript expression");
3430 return 0;
3431 }
3432 break;
3433 case Starred_kind:
3434 switch (e->v.Starred.ctx) {
3435 case Store:
3436 /* In all legitimate cases, the Starred node was already replaced
3437 * by compiler_list/compiler_tuple. XXX: is that okay? */
3438 return compiler_error(c,
3439 "starred assignment target must be in a list or tuple");
3440 default:
3441 return compiler_error(c,
3442 "can use starred expression only as assignment target");
3443 }
3444 break;
3445 case Name_kind:
3446 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3447 /* child nodes of List and Tuple will have expr_context set */
3448 case List_kind:
3449 return compiler_list(c, e);
3450 case Tuple_kind:
3451 return compiler_tuple(c, e);
3452 }
3453 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003454}
3455
3456static int
3457compiler_augassign(struct compiler *c, stmt_ty s)
3458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 expr_ty e = s->v.AugAssign.target;
3460 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 switch (e->kind) {
3465 case Attribute_kind:
3466 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3467 AugLoad, e->lineno, e->col_offset, c->c_arena);
3468 if (auge == NULL)
3469 return 0;
3470 VISIT(c, expr, auge);
3471 VISIT(c, expr, s->v.AugAssign.value);
3472 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3473 auge->v.Attribute.ctx = AugStore;
3474 VISIT(c, expr, auge);
3475 break;
3476 case Subscript_kind:
3477 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3478 AugLoad, e->lineno, e->col_offset, c->c_arena);
3479 if (auge == NULL)
3480 return 0;
3481 VISIT(c, expr, auge);
3482 VISIT(c, expr, s->v.AugAssign.value);
3483 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3484 auge->v.Subscript.ctx = AugStore;
3485 VISIT(c, expr, auge);
3486 break;
3487 case Name_kind:
3488 if (!compiler_nameop(c, e->v.Name.id, Load))
3489 return 0;
3490 VISIT(c, expr, s->v.AugAssign.value);
3491 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3492 return compiler_nameop(c, e->v.Name.id, Store);
3493 default:
3494 PyErr_Format(PyExc_SystemError,
3495 "invalid node type (%d) for augmented assignment",
3496 e->kind);
3497 return 0;
3498 }
3499 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003500}
3501
3502static int
3503compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 struct fblockinfo *f;
3506 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3507 PyErr_SetString(PyExc_SystemError,
3508 "too many statically nested blocks");
3509 return 0;
3510 }
3511 f = &c->u->u_fblock[c->u->u_nfblocks++];
3512 f->fb_type = t;
3513 f->fb_block = b;
3514 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003515}
3516
3517static void
3518compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 struct compiler_unit *u = c->u;
3521 assert(u->u_nfblocks > 0);
3522 u->u_nfblocks--;
3523 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
3524 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003525}
3526
Thomas Wouters89f507f2006-12-13 04:49:30 +00003527static int
3528compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 int i;
3530 struct compiler_unit *u = c->u;
3531 for (i = 0; i < u->u_nfblocks; ++i) {
3532 if (u->u_fblock[i].fb_type == LOOP)
3533 return 1;
3534 }
3535 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00003536}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537/* Raises a SyntaxError and returns 0.
3538 If something goes wrong, a different exception may be raised.
3539*/
3540
3541static int
3542compiler_error(struct compiler *c, const char *errstr)
3543{
Benjamin Peterson43b06862011-05-27 09:08:01 -05003544 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno);
3548 if (!loc) {
3549 Py_INCREF(Py_None);
3550 loc = Py_None;
3551 }
Benjamin Peterson43b06862011-05-27 09:08:01 -05003552 u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003553 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 if (!u)
3555 goto exit;
3556 v = Py_BuildValue("(zO)", errstr, u);
3557 if (!v)
3558 goto exit;
3559 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003560 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 Py_DECREF(loc);
3562 Py_XDECREF(u);
3563 Py_XDECREF(v);
3564 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003565}
3566
3567static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568compiler_handle_subscr(struct compiler *c, const char *kind,
3569 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 /* XXX this code is duplicated */
3574 switch (ctx) {
3575 case AugLoad: /* fall through to Load */
3576 case Load: op = BINARY_SUBSCR; break;
3577 case AugStore:/* fall through to Store */
3578 case Store: op = STORE_SUBSCR; break;
3579 case Del: op = DELETE_SUBSCR; break;
3580 case Param:
3581 PyErr_Format(PyExc_SystemError,
3582 "invalid %s kind %d in subscript\n",
3583 kind, ctx);
3584 return 0;
3585 }
3586 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00003587 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 }
3589 else if (ctx == AugStore) {
3590 ADDOP(c, ROT_THREE);
3591 }
3592 ADDOP(c, op);
3593 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003594}
3595
3596static int
3597compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 int n = 2;
3600 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 /* only handles the cases where BUILD_SLICE is emitted */
3603 if (s->v.Slice.lower) {
3604 VISIT(c, expr, s->v.Slice.lower);
3605 }
3606 else {
3607 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3608 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 if (s->v.Slice.upper) {
3611 VISIT(c, expr, s->v.Slice.upper);
3612 }
3613 else {
3614 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3615 }
3616
3617 if (s->v.Slice.step) {
3618 n++;
3619 VISIT(c, expr, s->v.Slice.step);
3620 }
3621 ADDOP_I(c, BUILD_SLICE, n);
3622 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003623}
3624
3625static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626compiler_visit_nested_slice(struct compiler *c, slice_ty s,
3627 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 switch (s->kind) {
3630 case Slice_kind:
3631 return compiler_slice(c, s, ctx);
3632 case Index_kind:
3633 VISIT(c, expr, s->v.Index.value);
3634 break;
3635 case ExtSlice_kind:
3636 default:
3637 PyErr_SetString(PyExc_SystemError,
3638 "extended slice invalid in nested slice");
3639 return 0;
3640 }
3641 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003642}
3643
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644static int
3645compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
3646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 char * kindname = NULL;
3648 switch (s->kind) {
3649 case Index_kind:
3650 kindname = "index";
3651 if (ctx != AugStore) {
3652 VISIT(c, expr, s->v.Index.value);
3653 }
3654 break;
3655 case Slice_kind:
3656 kindname = "slice";
3657 if (ctx != AugStore) {
3658 if (!compiler_slice(c, s, ctx))
3659 return 0;
3660 }
3661 break;
3662 case ExtSlice_kind:
3663 kindname = "extended slice";
3664 if (ctx != AugStore) {
3665 int i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
3666 for (i = 0; i < n; i++) {
3667 slice_ty sub = (slice_ty)asdl_seq_GET(
3668 s->v.ExtSlice.dims, i);
3669 if (!compiler_visit_nested_slice(c, sub, ctx))
3670 return 0;
3671 }
3672 ADDOP_I(c, BUILD_TUPLE, n);
3673 }
3674 break;
3675 default:
3676 PyErr_Format(PyExc_SystemError,
3677 "invalid subscript kind %d", s->kind);
3678 return 0;
3679 }
3680 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003681}
3682
Thomas Wouters89f507f2006-12-13 04:49:30 +00003683/* End of the compiler section, beginning of the assembler section */
3684
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003685/* do depth-first search of basic block graph, starting with block.
3686 post records the block indices in post-order.
3687
3688 XXX must handle implicit jumps from one block to next
3689*/
3690
Thomas Wouters89f507f2006-12-13 04:49:30 +00003691struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 PyObject *a_bytecode; /* string containing bytecode */
3693 int a_offset; /* offset into bytecode */
3694 int a_nblocks; /* number of reachable blocks */
3695 basicblock **a_postorder; /* list of blocks in dfs postorder */
3696 PyObject *a_lnotab; /* string containing lnotab */
3697 int a_lnotab_off; /* offset into lnotab */
3698 int a_lineno; /* last lineno of emitted instruction */
3699 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00003700};
3701
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003702static void
3703dfs(struct compiler *c, basicblock *b, struct assembler *a)
3704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 int i;
3706 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 if (b->b_seen)
3709 return;
3710 b->b_seen = 1;
3711 if (b->b_next != NULL)
3712 dfs(c, b->b_next, a);
3713 for (i = 0; i < b->b_iused; i++) {
3714 instr = &b->b_instr[i];
3715 if (instr->i_jrel || instr->i_jabs)
3716 dfs(c, instr->i_target, a);
3717 }
3718 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003719}
3720
Neal Norwitz2744c6c2005-11-13 01:08:38 +00003721static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003722stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
3723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 int i, target_depth;
3725 struct instr *instr;
3726 if (b->b_seen || b->b_startdepth >= depth)
3727 return maxdepth;
3728 b->b_seen = 1;
3729 b->b_startdepth = depth;
3730 for (i = 0; i < b->b_iused; i++) {
3731 instr = &b->b_instr[i];
3732 depth += opcode_stack_effect(instr->i_opcode, instr->i_oparg);
3733 if (depth > maxdepth)
3734 maxdepth = depth;
3735 assert(depth >= 0); /* invalid code or bug in stackdepth() */
3736 if (instr->i_jrel || instr->i_jabs) {
3737 target_depth = depth;
3738 if (instr->i_opcode == FOR_ITER) {
3739 target_depth = depth-2;
3740 } else if (instr->i_opcode == SETUP_FINALLY ||
3741 instr->i_opcode == SETUP_EXCEPT) {
3742 target_depth = depth+3;
3743 if (target_depth > maxdepth)
3744 maxdepth = target_depth;
3745 }
3746 maxdepth = stackdepth_walk(c, instr->i_target,
3747 target_depth, maxdepth);
3748 if (instr->i_opcode == JUMP_ABSOLUTE ||
3749 instr->i_opcode == JUMP_FORWARD) {
3750 goto out; /* remaining code is dead */
3751 }
3752 }
3753 }
3754 if (b->b_next)
3755 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003756out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 b->b_seen = 0;
3758 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003759}
3760
3761/* Find the flow path that needs the largest stack. We assume that
3762 * cycles in the flow graph have no net effect on the stack depth.
3763 */
3764static int
3765stackdepth(struct compiler *c)
3766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 basicblock *b, *entryblock;
3768 entryblock = NULL;
3769 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3770 b->b_seen = 0;
3771 b->b_startdepth = INT_MIN;
3772 entryblock = b;
3773 }
3774 if (!entryblock)
3775 return 0;
3776 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003777}
3778
3779static int
3780assemble_init(struct assembler *a, int nblocks, int firstlineno)
3781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 memset(a, 0, sizeof(struct assembler));
3783 a->a_lineno = firstlineno;
3784 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3785 if (!a->a_bytecode)
3786 return 0;
3787 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3788 if (!a->a_lnotab)
3789 return 0;
3790 if (nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
3791 PyErr_NoMemory();
3792 return 0;
3793 }
3794 a->a_postorder = (basicblock **)PyObject_Malloc(
3795 sizeof(basicblock *) * nblocks);
3796 if (!a->a_postorder) {
3797 PyErr_NoMemory();
3798 return 0;
3799 }
3800 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003801}
3802
3803static void
3804assemble_free(struct assembler *a)
3805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 Py_XDECREF(a->a_bytecode);
3807 Py_XDECREF(a->a_lnotab);
3808 if (a->a_postorder)
3809 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003810}
3811
3812/* Return the size of a basic block in bytes. */
3813
3814static int
3815instrsize(struct instr *instr)
3816{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 if (!instr->i_hasarg)
3818 return 1; /* 1 byte for the opcode*/
3819 if (instr->i_oparg > 0xffff)
3820 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
3821 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003822}
3823
3824static int
3825blocksize(basicblock *b)
3826{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003827 int i;
3828 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 for (i = 0; i < b->b_iused; i++)
3831 size += instrsize(&b->b_instr[i]);
3832 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003833}
3834
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003835/* Appends a pair to the end of the line number table, a_lnotab, representing
3836 the instruction's bytecode offset and line number. See
3837 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00003838
Guido van Rossumf68d8e52001-04-14 17:55:09 +00003839static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003840assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 int d_bytecode, d_lineno;
3843 int len;
3844 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 d_bytecode = a->a_offset - a->a_lineno_off;
3847 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 assert(d_bytecode >= 0);
3850 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 if(d_bytecode == 0 && d_lineno == 0)
3853 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00003854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 if (d_bytecode > 255) {
3856 int j, nbytes, ncodes = d_bytecode / 255;
3857 nbytes = a->a_lnotab_off + 2 * ncodes;
3858 len = PyBytes_GET_SIZE(a->a_lnotab);
3859 if (nbytes >= len) {
3860 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3861 len = nbytes;
3862 else if (len <= INT_MAX / 2)
3863 len *= 2;
3864 else {
3865 PyErr_NoMemory();
3866 return 0;
3867 }
3868 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3869 return 0;
3870 }
3871 lnotab = (unsigned char *)
3872 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3873 for (j = 0; j < ncodes; j++) {
3874 *lnotab++ = 255;
3875 *lnotab++ = 0;
3876 }
3877 d_bytecode -= ncodes * 255;
3878 a->a_lnotab_off += ncodes * 2;
3879 }
3880 assert(d_bytecode <= 255);
3881 if (d_lineno > 255) {
3882 int j, nbytes, ncodes = d_lineno / 255;
3883 nbytes = a->a_lnotab_off + 2 * ncodes;
3884 len = PyBytes_GET_SIZE(a->a_lnotab);
3885 if (nbytes >= len) {
3886 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
3887 len = nbytes;
3888 else if (len <= INT_MAX / 2)
3889 len *= 2;
3890 else {
3891 PyErr_NoMemory();
3892 return 0;
3893 }
3894 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
3895 return 0;
3896 }
3897 lnotab = (unsigned char *)
3898 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
3899 *lnotab++ = d_bytecode;
3900 *lnotab++ = 255;
3901 d_bytecode = 0;
3902 for (j = 1; j < ncodes; j++) {
3903 *lnotab++ = 0;
3904 *lnotab++ = 255;
3905 }
3906 d_lineno -= ncodes * 255;
3907 a->a_lnotab_off += ncodes * 2;
3908 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 len = PyBytes_GET_SIZE(a->a_lnotab);
3911 if (a->a_lnotab_off + 2 >= len) {
3912 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
3913 return 0;
3914 }
3915 lnotab = (unsigned char *)
3916 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 a->a_lnotab_off += 2;
3919 if (d_bytecode) {
3920 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003921 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 }
3923 else { /* First line of a block; def stmt, etc. */
3924 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003925 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 }
3927 a->a_lineno = i->i_lineno;
3928 a->a_lineno_off = a->a_offset;
3929 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003930}
3931
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003932/* assemble_emit()
3933 Extend the bytecode with a new instruction.
3934 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00003935*/
3936
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003937static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003938assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00003939{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 int size, arg = 0, ext = 0;
3941 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
3942 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 size = instrsize(i);
3945 if (i->i_hasarg) {
3946 arg = i->i_oparg;
3947 ext = arg >> 16;
3948 }
3949 if (i->i_lineno && !assemble_lnotab(a, i))
3950 return 0;
3951 if (a->a_offset + size >= len) {
3952 if (len > PY_SSIZE_T_MAX / 2)
3953 return 0;
3954 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
3955 return 0;
3956 }
3957 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
3958 a->a_offset += size;
3959 if (size == 6) {
3960 assert(i->i_hasarg);
3961 *code++ = (char)EXTENDED_ARG;
3962 *code++ = ext & 0xff;
3963 *code++ = ext >> 8;
3964 arg &= 0xffff;
3965 }
3966 *code++ = i->i_opcode;
3967 if (i->i_hasarg) {
3968 assert(size == 3 || size == 6);
3969 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02003970 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 }
3972 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003973}
3974
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00003975static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00003977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 basicblock *b;
3979 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
3980 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00003981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 /* Compute the size of each block and fixup jump args.
3983 Replace block pointer with position in bytecode. */
3984 do {
3985 totsize = 0;
3986 for (i = a->a_nblocks - 1; i >= 0; i--) {
3987 b = a->a_postorder[i];
3988 bsize = blocksize(b);
3989 b->b_offset = totsize;
3990 totsize += bsize;
3991 }
3992 last_extended_arg_count = extended_arg_count;
3993 extended_arg_count = 0;
3994 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
3995 bsize = b->b_offset;
3996 for (i = 0; i < b->b_iused; i++) {
3997 struct instr *instr = &b->b_instr[i];
3998 /* Relative jumps are computed relative to
3999 the instruction pointer after fetching
4000 the jump instruction.
4001 */
4002 bsize += instrsize(instr);
4003 if (instr->i_jabs)
4004 instr->i_oparg = instr->i_target->b_offset;
4005 else if (instr->i_jrel) {
4006 int delta = instr->i_target->b_offset - bsize;
4007 instr->i_oparg = delta;
4008 }
4009 else
4010 continue;
4011 if (instr->i_oparg > 0xffff)
4012 extended_arg_count++;
4013 }
4014 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 /* XXX: This is an awful hack that could hurt performance, but
4017 on the bright side it should work until we come up
4018 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 The issue is that in the first loop blocksize() is called
4021 which calls instrsize() which requires i_oparg be set
4022 appropriately. There is a bootstrap problem because
4023 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 So we loop until we stop seeing new EXTENDED_ARGs.
4026 The only EXTENDED_ARGs that could be popping up are
4027 ones in jump instructions. So this should converge
4028 fairly quickly.
4029 */
4030 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004031}
4032
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004033static PyObject *
4034dict_keys_inorder(PyObject *dict, int offset)
4035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 PyObject *tuple, *k, *v;
4037 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 tuple = PyTuple_New(size);
4040 if (tuple == NULL)
4041 return NULL;
4042 while (PyDict_Next(dict, &pos, &k, &v)) {
4043 i = PyLong_AS_LONG(v);
4044 /* The keys of the dictionary are tuples. (see compiler_add_o)
4045 The object we want is always first, though. */
4046 k = PyTuple_GET_ITEM(k, 0);
4047 Py_INCREF(k);
4048 assert((i - offset) < size);
4049 assert((i - offset) >= 0);
4050 PyTuple_SET_ITEM(tuple, i - offset, k);
4051 }
4052 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004053}
4054
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004055static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004056compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 PySTEntryObject *ste = c->u->u_ste;
4059 int flags = 0, n;
4060 if (ste->ste_type != ModuleBlock)
4061 flags |= CO_NEWLOCALS;
4062 if (ste->ste_type == FunctionBlock) {
4063 if (!ste->ste_unoptimized)
4064 flags |= CO_OPTIMIZED;
4065 if (ste->ste_nested)
4066 flags |= CO_NESTED;
4067 if (ste->ste_generator)
4068 flags |= CO_GENERATOR;
4069 if (ste->ste_varargs)
4070 flags |= CO_VARARGS;
4071 if (ste->ste_varkeywords)
4072 flags |= CO_VARKEYWORDS;
4073 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 /* (Only) inherit compilerflags in PyCF_MASK */
4076 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 n = PyDict_Size(c->u->u_freevars);
4079 if (n < 0)
4080 return -1;
4081 if (n == 0) {
4082 n = PyDict_Size(c->u->u_cellvars);
4083 if (n < 0)
4084 return -1;
4085 if (n == 0) {
4086 flags |= CO_NOFREE;
4087 }
4088 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004091}
4092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004093static PyCodeObject *
4094makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004095{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 PyObject *tmp;
4097 PyCodeObject *co = NULL;
4098 PyObject *consts = NULL;
4099 PyObject *names = NULL;
4100 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 PyObject *name = NULL;
4102 PyObject *freevars = NULL;
4103 PyObject *cellvars = NULL;
4104 PyObject *bytecode = NULL;
4105 int nlocals, flags;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 tmp = dict_keys_inorder(c->u->u_consts, 0);
4108 if (!tmp)
4109 goto error;
4110 consts = PySequence_List(tmp); /* optimize_code requires a list */
4111 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 names = dict_keys_inorder(c->u->u_names, 0);
4114 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4115 if (!consts || !names || !varnames)
4116 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4119 if (!cellvars)
4120 goto error;
4121 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4122 if (!freevars)
4123 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 nlocals = PyDict_Size(c->u->u_varnames);
4125 flags = compute_code_flags(c);
4126 if (flags < 0)
4127 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4130 if (!bytecode)
4131 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4134 if (!tmp)
4135 goto error;
4136 Py_DECREF(consts);
4137 consts = tmp;
4138
4139 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4140 nlocals, stackdepth(c), flags,
4141 bytecode, consts, names, varnames,
4142 freevars, cellvars,
Benjamin Peterson43b06862011-05-27 09:08:01 -05004143 c->c_filename_obj, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 c->u->u_firstlineno,
4145 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004146 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 Py_XDECREF(consts);
4148 Py_XDECREF(names);
4149 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 Py_XDECREF(name);
4151 Py_XDECREF(freevars);
4152 Py_XDECREF(cellvars);
4153 Py_XDECREF(bytecode);
4154 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004155}
4156
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004157
4158/* For debugging purposes only */
4159#if 0
4160static void
4161dump_instr(const struct instr *i)
4162{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 const char *jrel = i->i_jrel ? "jrel " : "";
4164 const char *jabs = i->i_jabs ? "jabs " : "";
4165 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 *arg = '\0';
4168 if (i->i_hasarg)
4169 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4172 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004173}
4174
4175static void
4176dump_basicblock(const basicblock *b)
4177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 const char *seen = b->b_seen ? "seen " : "";
4179 const char *b_return = b->b_return ? "return " : "";
4180 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4181 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4182 if (b->b_instr) {
4183 int i;
4184 for (i = 0; i < b->b_iused; i++) {
4185 fprintf(stderr, " [%02d] ", i);
4186 dump_instr(b->b_instr + i);
4187 }
4188 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004189}
4190#endif
4191
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004192static PyCodeObject *
4193assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 basicblock *b, *entryblock;
4196 struct assembler a;
4197 int i, j, nblocks;
4198 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 /* Make sure every block that falls off the end returns None.
4201 XXX NEXT_BLOCK() isn't quite right, because if the last
4202 block ends with a jump or return b_next shouldn't set.
4203 */
4204 if (!c->u->u_curblock->b_return) {
4205 NEXT_BLOCK(c);
4206 if (addNone)
4207 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4208 ADDOP(c, RETURN_VALUE);
4209 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 nblocks = 0;
4212 entryblock = NULL;
4213 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4214 nblocks++;
4215 entryblock = b;
4216 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 /* Set firstlineno if it wasn't explicitly set. */
4219 if (!c->u->u_firstlineno) {
4220 if (entryblock && entryblock->b_instr)
4221 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4222 else
4223 c->u->u_firstlineno = 1;
4224 }
4225 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4226 goto error;
4227 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 /* Can't modify the bytecode after computing jump offsets. */
4230 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 /* Emit code in reverse postorder from dfs. */
4233 for (i = a.a_nblocks - 1; i >= 0; i--) {
4234 b = a.a_postorder[i];
4235 for (j = 0; j < b->b_iused; j++)
4236 if (!assemble_emit(&a, &b->b_instr[j]))
4237 goto error;
4238 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4241 goto error;
4242 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4243 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004246 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 assemble_free(&a);
4248 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004249}
Georg Brandl8334fd92010-12-04 10:26:46 +00004250
4251#undef PyAST_Compile
4252PyAPI_FUNC(PyCodeObject *)
4253PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4254 PyArena *arena)
4255{
4256 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4257}
4258
4259