blob: 7820f39f878ff800f4765daaca180f5cf0df11b2 [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
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033#define DEFAULT_BLOCK_SIZE 16
34#define DEFAULT_BLOCKS 8
35#define DEFAULT_CODE_SIZE 128
36#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000037
Nick Coghlan650f0d02007-04-15 12:05:43 +000038#define COMP_GENEXP 0
39#define COMP_LISTCOMP 1
40#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000041#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000042
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000043struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 unsigned i_jabs : 1;
45 unsigned i_jrel : 1;
46 unsigned i_hasarg : 1;
47 unsigned char i_opcode;
48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000051};
52
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000053typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 struct basicblock_ *b_list;
58 /* number of instructions used */
59 int b_iused;
60 /* length of instruction array (b_instr) */
61 int b_ialloc;
62 /* pointer to an array of instructions, initially NULL */
63 struct instr *b_instr;
64 /* If b_next is non-NULL, it is a pointer to the next
65 block reached by normal control flow. */
66 struct basicblock_ *b_next;
67 /* b_seen is used to perform a DFS of basicblocks. */
68 unsigned b_seen : 1;
69 /* b_return is true if a RETURN_VALUE opcode is inserted. */
70 unsigned b_return : 1;
71 /* depth of stack upon entry of block, computed by stackdepth() */
72 int b_startdepth;
73 /* instruction offset for block, computed by assemble_jump_offsets() */
74 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000075} basicblock;
76
77/* fblockinfo tracks the current frame block.
78
Jeremy Hyltone9357b22006-03-01 15:47:05 +000079A frame block is used to handle loops, try/except, and try/finally.
80It's called a frame block to distinguish it from a basic block in the
81compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000082*/
83
84enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
85
86struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 enum fblocktype fb_type;
88 basicblock *fb_block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089};
90
Antoine Pitrou86a36b52011-11-25 18:56:07 +010091enum {
92 COMPILER_SCOPE_MODULE,
93 COMPILER_SCOPE_CLASS,
94 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -040095 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -040096 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +010097 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;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400107 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100108 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
Victor Stinnerf8e32212013-11-19 23:56:34 +0100122 Py_ssize_t u_argcount; /* number of arguments for block */
123 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 /* 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 {
Victor Stinner14e461d2013-08-26 22:28:21 +0200152 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 struct symtable *c_st;
154 PyFutureFeatures *c_future; /* pointer to module's __future__ */
155 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156
Georg Brandl8334fd92010-12-04 10:26:46 +0000157 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 int c_interactive; /* true if in interactive mode */
159 int c_nestlevel;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000161 struct compiler_unit *u; /* compiler state for current block */
162 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
163 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164};
165
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100166static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000167static void compiler_free(struct compiler *);
168static basicblock *compiler_new_block(struct compiler *);
169static int compiler_next_instr(struct compiler *, basicblock *);
170static int compiler_addop(struct compiler *, int);
171static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100172static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000174static basicblock *compiler_use_new_block(struct compiler *);
175static int compiler_error(struct compiler *, const char *);
176static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
177
178static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
179static int compiler_visit_stmt(struct compiler *, stmt_ty);
180static int compiler_visit_keyword(struct compiler *, keyword_ty);
181static int compiler_visit_expr(struct compiler *, expr_ty);
182static int compiler_augassign(struct compiler *, stmt_ty);
183static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185
186static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190/* Returns true if there is a loop on the fblock stack. */
191static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000192
193static int inplace_binop(struct compiler *, operator_ty);
Georg Brandl8334fd92010-12-04 10:26:46 +0000194static int expr_constant(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500196static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400197static int compiler_async_with(struct compiler *, stmt_ty, int);
198static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinnerad9a0662013-11-19 22:23:20 +0100199static int compiler_call_helper(struct compiler *c, Py_ssize_t n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400201 asdl_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500202static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400203static int compiler_set_qualname(struct compiler *);
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 Peterson9e77f722015-05-07 18:41:47 -0400208#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000209
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 Pitrou55bff892013-04-06 21:21:04 +0200251 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
252 PyErr_SetString(PyExc_OverflowError,
253 "private identifier too large to be mangled");
254 return NULL;
255 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000256
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200257 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
258 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
259 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
260
261 result = PyUnicode_New(1 + nlen + plen, maxchar);
262 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200264 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
265 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200266 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
267 Py_DECREF(result);
268 return NULL;
269 }
270 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
271 Py_DECREF(result);
272 return NULL;
273 }
Victor Stinner8f825062012-04-27 13:55:39 +0200274 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200275 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000276}
277
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000278static int
279compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000280{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 c->c_stack = PyList_New(0);
284 if (!c->c_stack)
285 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000288}
289
290PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200291PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
292 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000294 struct compiler c;
295 PyCodeObject *co = NULL;
296 PyCompilerFlags local_flags;
297 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 if (!__doc__) {
300 __doc__ = PyUnicode_InternFromString("__doc__");
301 if (!__doc__)
302 return NULL;
303 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (!compiler_init(&c))
306 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200307 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 c.c_filename = filename;
309 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200310 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (c.c_future == NULL)
312 goto finally;
313 if (!flags) {
314 local_flags.cf_flags = 0;
315 flags = &local_flags;
316 }
317 merged = c.c_future->ff_features | flags->cf_flags;
318 c.c_future->ff_features = merged;
319 flags->cf_flags = merged;
320 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000321 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000323
Victor Stinner14e461d2013-08-26 22:28:21 +0200324 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 if (c.c_st == NULL) {
326 if (!PyErr_Occurred())
327 PyErr_SetString(PyExc_SystemError, "no symtable");
328 goto finally;
329 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000332
Thomas Wouters1175c432006-02-27 22:49:54 +0000333 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 compiler_free(&c);
335 assert(co || PyErr_Occurred());
336 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337}
338
339PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200340PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
341 int optimize, PyArena *arena)
342{
343 PyObject *filename;
344 PyCodeObject *co;
345 filename = PyUnicode_DecodeFSDefault(filename_str);
346 if (filename == NULL)
347 return NULL;
348 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
349 Py_DECREF(filename);
350 return co;
351
352}
353
354PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355PyNode_Compile(struct _node *n, const char *filename)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyCodeObject *co = NULL;
358 mod_ty mod;
359 PyArena *arena = PyArena_New();
360 if (!arena)
361 return NULL;
362 mod = PyAST_FromNode(n, NULL, filename, arena);
363 if (mod)
364 co = PyAST_Compile(mod, filename, NULL, arena);
365 PyArena_Free(arena);
366 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000367}
368
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000369static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 if (c->c_st)
373 PySymtable_Free(c->c_st);
374 if (c->c_future)
375 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200376 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000378}
379
Guido van Rossum79f25d91997-04-29 20:08:16 +0000380static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_ssize_t i, n;
384 PyObject *v, *k;
385 PyObject *dict = PyDict_New();
386 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 n = PyList_Size(list);
389 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100390 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 if (!v) {
392 Py_DECREF(dict);
393 return NULL;
394 }
395 k = PyList_GET_ITEM(list, i);
396 k = PyTuple_Pack(2, k, k->ob_type);
397 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
398 Py_XDECREF(k);
399 Py_DECREF(v);
400 Py_DECREF(dict);
401 return NULL;
402 }
403 Py_DECREF(k);
404 Py_DECREF(v);
405 }
406 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407}
408
409/* Return new dict containing names from src that match scope(s).
410
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000411src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000413values are integers, starting at offset and increasing by one for
414each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000415*/
416
417static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100418dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000419{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700420 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500422 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 assert(offset >= 0);
425 if (dest == NULL)
426 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427
Meador Inge2ca63152012-07-18 14:20:11 -0500428 /* Sort the keys so that we have a deterministic order on the indexes
429 saved in the returned dictionary. These indexes are used as indexes
430 into the free and cell var storage. Therefore if they aren't
431 deterministic, then the generated bytecode is not deterministic.
432 */
433 sorted_keys = PyDict_Keys(src);
434 if (sorted_keys == NULL)
435 return NULL;
436 if (PyList_Sort(sorted_keys) != 0) {
437 Py_DECREF(sorted_keys);
438 return NULL;
439 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500440 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500441
442 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 /* XXX this should probably be a macro in symtable.h */
444 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500445 k = PyList_GET_ITEM(sorted_keys, key_i);
446 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 assert(PyLong_Check(v));
448 vi = PyLong_AS_LONG(v);
449 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 if (scope == scope_type || vi & flag) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100452 PyObject *tuple, *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500454 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 Py_DECREF(dest);
456 return NULL;
457 }
458 i++;
459 tuple = PyTuple_Pack(2, k, k->ob_type);
460 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500461 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 Py_DECREF(item);
463 Py_DECREF(dest);
464 Py_XDECREF(tuple);
465 return NULL;
466 }
467 Py_DECREF(item);
468 Py_DECREF(tuple);
469 }
470 }
Meador Inge2ca63152012-07-18 14:20:11 -0500471 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000473}
474
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475static void
476compiler_unit_check(struct compiler_unit *u)
477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 basicblock *block;
479 for (block = u->u_blocks; block != NULL; block = block->b_list) {
480 assert((void *)block != (void *)0xcbcbcbcb);
481 assert((void *)block != (void *)0xfbfbfbfb);
482 assert((void *)block != (void *)0xdbdbdbdb);
483 if (block->b_instr != NULL) {
484 assert(block->b_ialloc > 0);
485 assert(block->b_iused > 0);
486 assert(block->b_ialloc >= block->b_iused);
487 }
488 else {
489 assert (block->b_iused == 0);
490 assert (block->b_ialloc == 0);
491 }
492 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000493}
494
495static void
496compiler_unit_free(struct compiler_unit *u)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 compiler_unit_check(u);
501 b = u->u_blocks;
502 while (b != NULL) {
503 if (b->b_instr)
504 PyObject_Free((void *)b->b_instr);
505 next = b->b_list;
506 PyObject_Free((void *)b);
507 b = next;
508 }
509 Py_CLEAR(u->u_ste);
510 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400511 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 Py_CLEAR(u->u_consts);
513 Py_CLEAR(u->u_names);
514 Py_CLEAR(u->u_varnames);
515 Py_CLEAR(u->u_freevars);
516 Py_CLEAR(u->u_cellvars);
517 Py_CLEAR(u->u_private);
518 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000519}
520
521static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100522compiler_enter_scope(struct compiler *c, identifier name,
523 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
528 struct compiler_unit));
529 if (!u) {
530 PyErr_NoMemory();
531 return 0;
532 }
533 memset(u, 0, sizeof(struct compiler_unit));
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100534 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000535 u->u_argcount = 0;
536 u->u_kwonlyargcount = 0;
537 u->u_ste = PySymtable_Lookup(c->c_st, key);
538 if (!u->u_ste) {
539 compiler_unit_free(u);
540 return 0;
541 }
542 Py_INCREF(name);
543 u->u_name = name;
544 u->u_varnames = list2dict(u->u_ste->ste_varnames);
545 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
546 if (!u->u_varnames || !u->u_cellvars) {
547 compiler_unit_free(u);
548 return 0;
549 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500550 if (u->u_ste->ste_needs_class_closure) {
551 /* Cook up a implicit __class__ cell. */
552 _Py_IDENTIFIER(__class__);
553 PyObject *tuple, *name, *zero;
554 int res;
555 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
556 assert(PyDict_Size(u->u_cellvars) == 0);
557 name = _PyUnicode_FromId(&PyId___class__);
558 if (!name) {
559 compiler_unit_free(u);
560 return 0;
561 }
562 tuple = PyTuple_Pack(2, name, Py_TYPE(name));
563 if (!tuple) {
564 compiler_unit_free(u);
565 return 0;
566 }
567 zero = PyLong_FromLong(0);
568 if (!zero) {
569 Py_DECREF(tuple);
570 compiler_unit_free(u);
571 return 0;
572 }
573 res = PyDict_SetItem(u->u_cellvars, tuple, zero);
574 Py_DECREF(tuple);
575 Py_DECREF(zero);
576 if (res < 0) {
577 compiler_unit_free(u);
578 return 0;
579 }
580 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
583 PyDict_Size(u->u_cellvars));
584 if (!u->u_freevars) {
585 compiler_unit_free(u);
586 return 0;
587 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 u->u_blocks = NULL;
590 u->u_nfblocks = 0;
591 u->u_firstlineno = lineno;
592 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000593 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 u->u_lineno_set = 0;
595 u->u_consts = PyDict_New();
596 if (!u->u_consts) {
597 compiler_unit_free(u);
598 return 0;
599 }
600 u->u_names = PyDict_New();
601 if (!u->u_names) {
602 compiler_unit_free(u);
603 return 0;
604 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608 /* Push the old compiler_unit on the stack. */
609 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400610 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
612 Py_XDECREF(capsule);
613 compiler_unit_free(u);
614 return 0;
615 }
616 Py_DECREF(capsule);
617 u->u_private = c->u->u_private;
618 Py_XINCREF(u->u_private);
619 }
620 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 c->c_nestlevel++;
623 if (compiler_use_new_block(c) == NULL)
624 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400626 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
627 if (!compiler_set_qualname(c))
628 return 0;
629 }
630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000632}
633
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000634static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635compiler_exit_scope(struct compiler *c)
636{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100637 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 c->c_nestlevel--;
641 compiler_unit_free(c->u);
642 /* Restore c->u to the parent unit. */
643 n = PyList_GET_SIZE(c->c_stack) - 1;
644 if (n >= 0) {
645 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400646 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 assert(c->u);
648 /* we are deleting from a list so this really shouldn't fail */
649 if (PySequence_DelItem(c->c_stack, n) < 0)
650 Py_FatalError("compiler_exit_scope()");
651 compiler_unit_check(c->u);
652 }
653 else
654 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000655
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656}
657
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400658static int
659compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100660{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100661 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400662 _Py_static_string(dot_locals, ".<locals>");
663 Py_ssize_t stack_size;
664 struct compiler_unit *u = c->u;
665 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100666
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400667 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100668 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400669 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400670 if (stack_size > 1) {
671 int scope, force_global = 0;
672 struct compiler_unit *parent;
673 PyObject *mangled, *capsule;
674
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400675 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400676 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400677 assert(parent);
678
Yury Selivanov75445082015-05-11 22:57:16 -0400679 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
680 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
681 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400682 assert(u->u_name);
683 mangled = _Py_Mangle(parent->u_private, u->u_name);
684 if (!mangled)
685 return 0;
686 scope = PyST_GetScope(parent->u_ste, mangled);
687 Py_DECREF(mangled);
688 assert(scope != GLOBAL_IMPLICIT);
689 if (scope == GLOBAL_EXPLICIT)
690 force_global = 1;
691 }
692
693 if (!force_global) {
694 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400695 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400696 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
697 dot_locals_str = _PyUnicode_FromId(&dot_locals);
698 if (dot_locals_str == NULL)
699 return 0;
700 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
701 if (base == NULL)
702 return 0;
703 }
704 else {
705 Py_INCREF(parent->u_qualname);
706 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400707 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100708 }
709 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400710
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400711 if (base != NULL) {
712 dot_str = _PyUnicode_FromId(&dot);
713 if (dot_str == NULL) {
714 Py_DECREF(base);
715 return 0;
716 }
717 name = PyUnicode_Concat(base, dot_str);
718 Py_DECREF(base);
719 if (name == NULL)
720 return 0;
721 PyUnicode_Append(&name, u->u_name);
722 if (name == NULL)
723 return 0;
724 }
725 else {
726 Py_INCREF(u->u_name);
727 name = u->u_name;
728 }
729 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100730
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400731 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100732}
733
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734/* Allocate a new block and return a pointer to it.
735 Returns NULL on error.
736*/
737
738static basicblock *
739compiler_new_block(struct compiler *c)
740{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 basicblock *b;
742 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 u = c->u;
745 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
746 if (b == NULL) {
747 PyErr_NoMemory();
748 return NULL;
749 }
750 memset((void *)b, 0, sizeof(basicblock));
751 /* Extend the singly linked list of blocks with new block. */
752 b->b_list = u->u_blocks;
753 u->u_blocks = b;
754 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000755}
756
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000757static basicblock *
758compiler_use_new_block(struct compiler *c)
759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 basicblock *block = compiler_new_block(c);
761 if (block == NULL)
762 return NULL;
763 c->u->u_curblock = block;
764 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765}
766
767static basicblock *
768compiler_next_block(struct compiler *c)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 basicblock *block = compiler_new_block(c);
771 if (block == NULL)
772 return NULL;
773 c->u->u_curblock->b_next = block;
774 c->u->u_curblock = block;
775 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000776}
777
778static basicblock *
779compiler_use_next_block(struct compiler *c, basicblock *block)
780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 assert(block != NULL);
782 c->u->u_curblock->b_next = block;
783 c->u->u_curblock = block;
784 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785}
786
787/* Returns the offset of the next instruction in the current block's
788 b_instr array. Resizes the b_instr as necessary.
789 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000790*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
792static int
793compiler_next_instr(struct compiler *c, basicblock *b)
794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 assert(b != NULL);
796 if (b->b_instr == NULL) {
797 b->b_instr = (struct instr *)PyObject_Malloc(
798 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
799 if (b->b_instr == NULL) {
800 PyErr_NoMemory();
801 return -1;
802 }
803 b->b_ialloc = DEFAULT_BLOCK_SIZE;
804 memset((char *)b->b_instr, 0,
805 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
806 }
807 else if (b->b_iused == b->b_ialloc) {
808 struct instr *tmp;
809 size_t oldsize, newsize;
810 oldsize = b->b_ialloc * sizeof(struct instr);
811 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (oldsize > (PY_SIZE_MAX >> 1)) {
814 PyErr_NoMemory();
815 return -1;
816 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (newsize == 0) {
819 PyErr_NoMemory();
820 return -1;
821 }
822 b->b_ialloc <<= 1;
823 tmp = (struct instr *)PyObject_Realloc(
824 (void *)b->b_instr, newsize);
825 if (tmp == NULL) {
826 PyErr_NoMemory();
827 return -1;
828 }
829 b->b_instr = tmp;
830 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
831 }
832 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833}
834
Christian Heimes2202f872008-02-06 14:31:34 +0000835/* Set the i_lineno member of the instruction at offset off if the
836 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000837 already been set. If it has been set, the call has no effect.
838
Christian Heimes2202f872008-02-06 14:31:34 +0000839 The line number is reset in the following cases:
840 - when entering a new scope
841 - on each statement
842 - on each expression that start a new line
843 - before the "except" clause
844 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000845*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847static void
848compiler_set_lineno(struct compiler *c, int off)
849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 basicblock *b;
851 if (c->u->u_lineno_set)
852 return;
853 c->u->u_lineno_set = 1;
854 b = c->u->u_curblock;
855 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000856}
857
Larry Hastings3a907972013-11-23 14:49:22 -0800858int
859PyCompile_OpcodeStackEffect(int opcode, int oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 switch (opcode) {
862 case POP_TOP:
863 return -1;
864 case ROT_TWO:
865 case ROT_THREE:
866 return 0;
867 case DUP_TOP:
868 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000869 case DUP_TOP_TWO:
870 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 case UNARY_POSITIVE:
873 case UNARY_NEGATIVE:
874 case UNARY_NOT:
875 case UNARY_INVERT:
876 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 case SET_ADD:
879 case LIST_APPEND:
880 return -1;
881 case MAP_ADD:
882 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 case BINARY_POWER:
885 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400886 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 case BINARY_MODULO:
888 case BINARY_ADD:
889 case BINARY_SUBTRACT:
890 case BINARY_SUBSCR:
891 case BINARY_FLOOR_DIVIDE:
892 case BINARY_TRUE_DIVIDE:
893 return -1;
894 case INPLACE_FLOOR_DIVIDE:
895 case INPLACE_TRUE_DIVIDE:
896 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 case INPLACE_ADD:
899 case INPLACE_SUBTRACT:
900 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400901 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 case INPLACE_MODULO:
903 return -1;
904 case STORE_SUBSCR:
905 return -3;
906 case STORE_MAP:
907 return -2;
908 case DELETE_SUBSCR:
909 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 case BINARY_LSHIFT:
912 case BINARY_RSHIFT:
913 case BINARY_AND:
914 case BINARY_XOR:
915 case BINARY_OR:
916 return -1;
917 case INPLACE_POWER:
918 return -1;
919 case GET_ITER:
920 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 case PRINT_EXPR:
923 return -1;
924 case LOAD_BUILD_CLASS:
925 return 1;
926 case INPLACE_LSHIFT:
927 case INPLACE_RSHIFT:
928 case INPLACE_AND:
929 case INPLACE_XOR:
930 case INPLACE_OR:
931 return -1;
932 case BREAK_LOOP:
933 return 0;
934 case SETUP_WITH:
935 return 7;
Yury Selivanov75445082015-05-11 22:57:16 -0400936 case WITH_CLEANUP_START:
937 return 1;
938 case WITH_CLEANUP_FINISH:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 return -1; /* XXX Sometimes more */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 case RETURN_VALUE:
941 return -1;
942 case IMPORT_STAR:
943 return -1;
944 case YIELD_VALUE:
945 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500946 case YIELD_FROM:
947 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 case POP_BLOCK:
949 return 0;
950 case POP_EXCEPT:
951 return 0; /* -3 except if bad bytecode */
952 case END_FINALLY:
953 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 case STORE_NAME:
956 return -1;
957 case DELETE_NAME:
958 return 0;
959 case UNPACK_SEQUENCE:
960 return oparg-1;
961 case UNPACK_EX:
962 return (oparg&0xFF) + (oparg>>8);
963 case FOR_ITER:
964 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 case STORE_ATTR:
967 return -2;
968 case DELETE_ATTR:
969 return -1;
970 case STORE_GLOBAL:
971 return -1;
972 case DELETE_GLOBAL:
973 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 case LOAD_CONST:
975 return 1;
976 case LOAD_NAME:
977 return 1;
978 case BUILD_TUPLE:
979 case BUILD_LIST:
980 case BUILD_SET:
981 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400982 case BUILD_LIST_UNPACK:
983 case BUILD_TUPLE_UNPACK:
984 case BUILD_SET_UNPACK:
985 case BUILD_MAP_UNPACK:
986 return 1 - oparg;
987 case BUILD_MAP_UNPACK_WITH_CALL:
988 return 1 - (oparg & 0xFF);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 case BUILD_MAP:
990 return 1;
991 case LOAD_ATTR:
992 return 0;
993 case COMPARE_OP:
994 return -1;
995 case IMPORT_NAME:
996 return -1;
997 case IMPORT_FROM:
998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 case JUMP_FORWARD:
1001 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
1002 case JUMP_IF_FALSE_OR_POP: /* "" */
1003 case JUMP_ABSOLUTE:
1004 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 case POP_JUMP_IF_FALSE:
1007 case POP_JUMP_IF_TRUE:
1008 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 case LOAD_GLOBAL:
1011 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 case CONTINUE_LOOP:
1014 return 0;
1015 case SETUP_LOOP:
1016 return 0;
1017 case SETUP_EXCEPT:
1018 case SETUP_FINALLY:
1019 return 6; /* can push 3 values for the new exception
1020 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 case LOAD_FAST:
1023 return 1;
1024 case STORE_FAST:
1025 return -1;
1026 case DELETE_FAST:
1027 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 case RAISE_VARARGS:
1030 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +00001031#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 case CALL_FUNCTION:
1033 return -NARGS(oparg);
1034 case CALL_FUNCTION_VAR:
1035 case CALL_FUNCTION_KW:
1036 return -NARGS(oparg)-1;
1037 case CALL_FUNCTION_VAR_KW:
1038 return -NARGS(oparg)-2;
1039 case MAKE_FUNCTION:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001040 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 case MAKE_CLOSURE:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001042 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001043#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case BUILD_SLICE:
1045 if (oparg == 3)
1046 return -2;
1047 else
1048 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 case LOAD_CLOSURE:
1051 return 1;
1052 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001053 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 return 1;
1055 case STORE_DEREF:
1056 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001057 case DELETE_DEREF:
1058 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001059 case GET_AWAITABLE:
1060 return 0;
1061 case SETUP_ASYNC_WITH:
1062 return 6;
1063 case BEFORE_ASYNC_WITH:
1064 return 1;
1065 case GET_AITER:
1066 return 0;
1067 case GET_ANEXT:
1068 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001070 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 }
Larry Hastings3a907972013-11-23 14:49:22 -08001072 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
1074
1075/* Add an opcode with no argument.
1076 Returns 0 on failure, 1 on success.
1077*/
1078
1079static int
1080compiler_addop(struct compiler *c, int opcode)
1081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 basicblock *b;
1083 struct instr *i;
1084 int off;
1085 off = compiler_next_instr(c, c->u->u_curblock);
1086 if (off < 0)
1087 return 0;
1088 b = c->u->u_curblock;
1089 i = &b->b_instr[off];
1090 i->i_opcode = opcode;
1091 i->i_hasarg = 0;
1092 if (opcode == RETURN_VALUE)
1093 b->b_return = 1;
1094 compiler_set_lineno(c, off);
1095 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096}
1097
Victor Stinnerf8e32212013-11-19 23:56:34 +01001098static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001099compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 PyObject *t, *v;
1102 Py_ssize_t arg;
1103 double d;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104
Serhiy Storchaka95949422013-08-27 19:40:23 +03001105 /* necessary to make sure types aren't coerced (e.g., float and complex) */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
1107 if (PyFloat_Check(o)) {
1108 d = PyFloat_AS_DOUBLE(o);
1109 /* all we need is to make the tuple different in either the 0.0
1110 * or -0.0 case from all others, just to avoid the "coercion".
1111 */
1112 if (d == 0.0 && copysign(1.0, d) < 0.0)
1113 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1114 else
1115 t = PyTuple_Pack(2, o, o->ob_type);
1116 }
1117 else if (PyComplex_Check(o)) {
1118 Py_complex z;
1119 int real_negzero, imag_negzero;
1120 /* For the complex case we must make complex(x, 0.)
1121 different from complex(x, -0.) and complex(0., y)
1122 different from complex(-0., y), for any x and y.
1123 All four complex zeros must be distinguished.*/
1124 z = PyComplex_AsCComplex(o);
1125 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1126 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1127 if (real_negzero && imag_negzero) {
1128 t = PyTuple_Pack(5, o, o->ob_type,
1129 Py_None, Py_None, Py_None);
Christian Heimes400adb02008-02-01 08:12:03 +00001130 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 else if (imag_negzero) {
1132 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
Guido van Rossum04110fb2007-08-24 16:32:05 +00001133 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 else if (real_negzero) {
1135 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1136 }
1137 else {
1138 t = PyTuple_Pack(2, o, o->ob_type);
1139 }
1140 }
1141 else {
1142 t = PyTuple_Pack(2, o, o->ob_type);
1143 }
1144 if (t == NULL)
1145 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 v = PyDict_GetItem(dict, t);
1148 if (!v) {
1149 if (PyErr_Occurred())
1150 return -1;
1151 arg = PyDict_Size(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001152 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 if (!v) {
1154 Py_DECREF(t);
1155 return -1;
1156 }
1157 if (PyDict_SetItem(dict, t, v) < 0) {
1158 Py_DECREF(t);
1159 Py_DECREF(v);
1160 return -1;
1161 }
1162 Py_DECREF(v);
1163 }
1164 else
1165 arg = PyLong_AsLong(v);
1166 Py_DECREF(t);
1167 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001168}
1169
1170static int
1171compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001173{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001174 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001176 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001177 return compiler_addop_i(c, opcode, arg);
1178}
1179
1180static int
1181compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001183{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001184 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1186 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001187 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001188 arg = compiler_add_o(c, dict, mangled);
1189 Py_DECREF(mangled);
1190 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001191 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192 return compiler_addop_i(c, opcode, arg);
1193}
1194
1195/* Add an opcode with an integer argument.
1196 Returns 0 on failure, 1 on success.
1197*/
1198
1199static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001200compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 struct instr *i;
1203 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001204
1205 /* Integer arguments are limit to 16-bit. There is an extension for 32-bit
1206 integer arguments. */
Victor Stinnerf8e32212013-11-19 23:56:34 +01001207 assert((-2147483647-1) <= oparg);
1208 assert(oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 off = compiler_next_instr(c, c->u->u_curblock);
1211 if (off < 0)
1212 return 0;
1213 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001214 i->i_opcode = opcode;
1215 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 i->i_hasarg = 1;
1217 compiler_set_lineno(c, off);
1218 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001219}
1220
1221static int
1222compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1223{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 struct instr *i;
1225 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 assert(b != NULL);
1228 off = compiler_next_instr(c, c->u->u_curblock);
1229 if (off < 0)
1230 return 0;
1231 i = &c->u->u_curblock->b_instr[off];
1232 i->i_opcode = opcode;
1233 i->i_target = b;
1234 i->i_hasarg = 1;
1235 if (absolute)
1236 i->i_jabs = 1;
1237 else
1238 i->i_jrel = 1;
1239 compiler_set_lineno(c, off);
1240 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001241}
1242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1244 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245 it as the current block. NEXT_BLOCK() also creates an implicit jump
1246 from the current block to the new block.
1247*/
1248
Thomas Wouters89f507f2006-12-13 04:49:30 +00001249/* The returns inside these macros make it impossible to decref objects
1250 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001251*/
1252
1253
1254#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (compiler_use_new_block((C)) == NULL) \
1256 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
1259#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (compiler_next_block((C)) == NULL) \
1261 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 if (!compiler_addop((C), (OP))) \
1266 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001267}
1268
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001269#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 if (!compiler_addop((C), (OP))) { \
1271 compiler_exit_scope(c); \
1272 return 0; \
1273 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001274}
1275
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1278 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001279}
1280
1281#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1283 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001284}
1285
1286#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (!compiler_addop_i((C), (OP), (O))) \
1288 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001289}
1290
1291#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (!compiler_addop_j((C), (OP), (O), 1)) \
1293 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001294}
1295
1296#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (!compiler_addop_j((C), (OP), (O), 0)) \
1298 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001299}
1300
1301/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1302 the ASDL name to synthesize the name of the C type and the visit function.
1303*/
1304
1305#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 if (!compiler_visit_ ## TYPE((C), (V))) \
1307 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001308}
1309
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001310#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (!compiler_visit_ ## TYPE((C), (V))) { \
1312 compiler_exit_scope(c); \
1313 return 0; \
1314 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001315}
1316
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001317#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (!compiler_visit_slice((C), (V), (CTX))) \
1319 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001320}
1321
1322#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 int _i; \
1324 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1325 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1326 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1327 if (!compiler_visit_ ## TYPE((C), elt)) \
1328 return 0; \
1329 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001330}
1331
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001332#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 int _i; \
1334 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1335 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1336 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1337 if (!compiler_visit_ ## TYPE((C), elt)) { \
1338 compiler_exit_scope(c); \
1339 return 0; \
1340 } \
1341 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001342}
1343
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001344static int
1345compiler_isdocstring(stmt_ty s)
1346{
1347 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001348 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 return s->v.Expr.value->kind == Str_kind;
1350}
1351
1352/* Compile a sequence of statements, checking for a docstring. */
1353
1354static int
1355compiler_body(struct compiler *c, asdl_seq *stmts)
1356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 int i = 0;
1358 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (!asdl_seq_LEN(stmts))
1361 return 1;
1362 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001363 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* don't generate docstrings if -OO */
1365 i = 1;
1366 VISIT(c, expr, st->v.Expr.value);
1367 if (!compiler_nameop(c, __doc__, Store))
1368 return 0;
1369 }
1370 for (; i < asdl_seq_LEN(stmts); i++)
1371 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1372 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373}
1374
1375static PyCodeObject *
1376compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001377{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 PyCodeObject *co;
1379 int addNone = 1;
1380 static PyObject *module;
1381 if (!module) {
1382 module = PyUnicode_InternFromString("<module>");
1383 if (!module)
1384 return NULL;
1385 }
1386 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001387 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 return NULL;
1389 switch (mod->kind) {
1390 case Module_kind:
1391 if (!compiler_body(c, mod->v.Module.body)) {
1392 compiler_exit_scope(c);
1393 return 0;
1394 }
1395 break;
1396 case Interactive_kind:
1397 c->c_interactive = 1;
1398 VISIT_SEQ_IN_SCOPE(c, stmt,
1399 mod->v.Interactive.body);
1400 break;
1401 case Expression_kind:
1402 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1403 addNone = 0;
1404 break;
1405 case Suite_kind:
1406 PyErr_SetString(PyExc_SystemError,
1407 "suite should not be possible");
1408 return 0;
1409 default:
1410 PyErr_Format(PyExc_SystemError,
1411 "module kind %d should not be possible",
1412 mod->kind);
1413 return 0;
1414 }
1415 co = assemble(c, addNone);
1416 compiler_exit_scope(c);
1417 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001418}
1419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420/* The test for LOCAL must come before the test for FREE in order to
1421 handle classes where name is both local and free. The local var is
1422 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001423*/
1424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425static int
1426get_ref_type(struct compiler *c, PyObject *name)
1427{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001428 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001429 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1430 !PyUnicode_CompareWithASCIIString(name, "__class__"))
1431 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001432 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 if (scope == 0) {
1434 char buf[350];
1435 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001436 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001438 PyUnicode_AsUTF8(name),
1439 PyUnicode_AsUTF8(c->u->u_name),
1440 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1441 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1442 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1443 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001444 );
1445 Py_FatalError(buf);
1446 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001449}
1450
1451static int
1452compiler_lookup_arg(PyObject *dict, PyObject *name)
1453{
1454 PyObject *k, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001455 k = PyTuple_Pack(2, name, name->ob_type);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001456 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001457 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001458 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001459 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001460 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001461 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001462 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001463}
1464
1465static int
Victor Stinnerad9a0662013-11-19 22:23:20 +01001466compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001467{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001468 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001469 if (qualname == NULL)
1470 qualname = co->co_name;
1471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (free == 0) {
1473 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001474 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 ADDOP_I(c, MAKE_FUNCTION, args);
1476 return 1;
1477 }
1478 for (i = 0; i < free; ++i) {
1479 /* Bypass com_addop_varname because it will generate
1480 LOAD_DEREF but LOAD_CLOSURE is needed.
1481 */
1482 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1483 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 /* Special case: If a class contains a method with a
1486 free variable that has the same name as a method,
1487 the name will be considered free *and* local in the
1488 class. It should be handled by the closure, as
1489 well as by the normal name loookup logic.
1490 */
1491 reftype = get_ref_type(c, name);
1492 if (reftype == CELL)
1493 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1494 else /* (reftype == FREE) */
1495 arg = compiler_lookup_arg(c->u->u_freevars, name);
1496 if (arg == -1) {
1497 fprintf(stderr,
1498 "lookup %s in %s %d %d\n"
1499 "freevars of %s: %s\n",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001500 PyUnicode_AsUTF8(PyObject_Repr(name)),
1501 PyUnicode_AsUTF8(c->u->u_name),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 reftype, arg,
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001503 PyUnicode_AsUTF8(co->co_name),
1504 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 Py_FatalError("compiler_make_closure()");
1506 }
1507 ADDOP_I(c, LOAD_CLOSURE, arg);
1508 }
1509 ADDOP_I(c, BUILD_TUPLE, free);
1510 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001511 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 ADDOP_I(c, MAKE_CLOSURE, args);
1513 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
1516static int
1517compiler_decorators(struct compiler *c, asdl_seq* decos)
1518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!decos)
1522 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1525 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1526 }
1527 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001528}
1529
1530static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001531compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 int i, default_count = 0;
1535 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1536 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1537 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1538 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001539 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1540 if (!mangled)
1541 return -1;
1542 ADDOP_O(c, LOAD_CONST, mangled, consts);
1543 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (!compiler_visit_expr(c, default_)) {
1545 return -1;
1546 }
1547 default_count++;
1548 }
1549 }
1550 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001551}
1552
1553static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001554compiler_visit_argannotation(struct compiler *c, identifier id,
1555 expr_ty annotation, PyObject *names)
1556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001558 PyObject *mangled;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 VISIT(c, expr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01001560 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001561 if (!mangled)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 return -1;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001563 if (PyList_Append(names, mangled) < 0) {
1564 Py_DECREF(mangled);
1565 return -1;
1566 }
1567 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 }
1569 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001570}
1571
1572static int
1573compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1574 PyObject *names)
1575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 int i, error;
1577 for (i = 0; i < asdl_seq_LEN(args); i++) {
1578 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1579 error = compiler_visit_argannotation(
1580 c,
1581 arg->arg,
1582 arg->annotation,
1583 names);
1584 if (error)
1585 return error;
1586 }
1587 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001588}
1589
1590static int
1591compiler_visit_annotations(struct compiler *c, arguments_ty args,
1592 expr_ty returns)
1593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 /* Push arg annotations and a list of the argument names. Return the #
1595 of items pushed. The expressions are evaluated out-of-order wrt the
1596 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1599 */
1600 static identifier return_str;
1601 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001602 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 names = PyList_New(0);
1604 if (!names)
1605 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 if (compiler_visit_argannotations(c, args->args, names))
1608 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001609 if (args->vararg && args->vararg->annotation &&
1610 compiler_visit_argannotation(c, args->vararg->arg,
1611 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 goto error;
1613 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1614 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001615 if (args->kwarg && args->kwarg->annotation &&
1616 compiler_visit_argannotation(c, args->kwarg->arg,
1617 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 if (!return_str) {
1621 return_str = PyUnicode_InternFromString("return");
1622 if (!return_str)
1623 goto error;
1624 }
1625 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1626 goto error;
1627 }
1628
1629 len = PyList_GET_SIZE(names);
1630 if (len > 65534) {
1631 /* len must fit in 16 bits, and len is incremented below */
1632 PyErr_SetString(PyExc_SyntaxError,
1633 "too many annotations");
1634 goto error;
1635 }
1636 if (len) {
1637 /* convert names to a tuple and place on stack */
1638 PyObject *elt;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001639 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 PyObject *s = PyTuple_New(len);
1641 if (!s)
1642 goto error;
1643 for (i = 0; i < len; i++) {
1644 elt = PyList_GET_ITEM(names, i);
1645 Py_INCREF(elt);
1646 PyTuple_SET_ITEM(s, i, elt);
1647 }
1648 ADDOP_O(c, LOAD_CONST, s, consts);
1649 Py_DECREF(s);
1650 len++; /* include the just-pushed tuple */
1651 }
1652 Py_DECREF(names);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001653
1654 /* We just checked that len <= 65535, see above */
1655 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
Neal Norwitzc1505362006-12-28 06:47:50 +00001656
1657error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 Py_DECREF(names);
1659 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001660}
1661
1662static int
Yury Selivanov75445082015-05-11 22:57:16 -04001663compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001666 PyObject *qualname, *first_const = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001667 arguments_ty args;
1668 expr_ty returns;
1669 identifier name;
1670 asdl_seq* decos;
1671 asdl_seq *body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 stmt_ty st;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001673 Py_ssize_t i, n, arglength;
1674 int docstring, kw_default_count = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 int num_annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001676 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677
Yury Selivanov75445082015-05-11 22:57:16 -04001678
1679 if (is_async) {
1680 assert(s->kind == AsyncFunctionDef_kind);
1681
1682 args = s->v.AsyncFunctionDef.args;
1683 returns = s->v.AsyncFunctionDef.returns;
1684 decos = s->v.AsyncFunctionDef.decorator_list;
1685 name = s->v.AsyncFunctionDef.name;
1686 body = s->v.AsyncFunctionDef.body;
1687
1688 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1689 } else {
1690 assert(s->kind == FunctionDef_kind);
1691
1692 args = s->v.FunctionDef.args;
1693 returns = s->v.FunctionDef.returns;
1694 decos = s->v.FunctionDef.decorator_list;
1695 name = s->v.FunctionDef.name;
1696 body = s->v.FunctionDef.body;
1697
1698 scope_type = COMPILER_SCOPE_FUNCTION;
1699 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (!compiler_decorators(c, decos))
1702 return 0;
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05001703 if (args->defaults)
1704 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 if (args->kwonlyargs) {
1706 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1707 args->kw_defaults);
1708 if (res < 0)
1709 return 0;
1710 kw_default_count = res;
1711 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 num_annotations = compiler_visit_annotations(c, args, returns);
1713 if (num_annotations < 0)
1714 return 0;
1715 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001716
Yury Selivanov75445082015-05-11 22:57:16 -04001717 if (!compiler_enter_scope(c, name,
1718 scope_type, (void *)s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 s->lineno))
1720 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001721
Yury Selivanov75445082015-05-11 22:57:16 -04001722 st = (stmt_ty)asdl_seq_GET(body, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001724 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 first_const = st->v.Expr.value->v.Str.s;
1726 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1727 compiler_exit_scope(c);
1728 return 0;
1729 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 c->u->u_argcount = asdl_seq_LEN(args->args);
1732 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Yury Selivanov75445082015-05-11 22:57:16 -04001733 n = asdl_seq_LEN(body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 /* if there was a docstring, we need to skip the first statement */
1735 for (i = docstring; i < n; i++) {
Yury Selivanov75445082015-05-11 22:57:16 -04001736 st = (stmt_ty)asdl_seq_GET(body, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 VISIT_IN_SCOPE(c, stmt, st);
1738 }
1739 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001740 qualname = c->u->u_qualname;
1741 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001743 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001744 Py_XDECREF(qualname);
1745 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001747 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 arglength = asdl_seq_LEN(args->defaults);
1750 arglength |= kw_default_count << 8;
1751 arglength |= num_annotations << 16;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001752 compiler_make_closure(c, co, arglength, qualname);
1753 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001755
Yury Selivanov75445082015-05-11 22:57:16 -04001756 if (is_async) {
1757 co->co_flags |= CO_COROUTINE;
1758 /* An async function is always a generator, even
1759 if there is no 'yield' expressions in it. */
1760 co->co_flags |= CO_GENERATOR;
1761 }
1762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 /* decorators */
1764 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1765 ADDOP_I(c, CALL_FUNCTION, 1);
1766 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767
Yury Selivanov75445082015-05-11 22:57:16 -04001768 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001769}
1770
1771static int
1772compiler_class(struct compiler *c, stmt_ty s)
1773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 PyCodeObject *co;
1775 PyObject *str;
1776 int i;
1777 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 if (!compiler_decorators(c, decos))
1780 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 /* ultimately generate code for:
1783 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1784 where:
1785 <func> is a function/closure created from the class body;
1786 it has a single argument (__locals__) where the dict
1787 (or MutableSequence) representing the locals is passed
1788 <name> is the class name
1789 <bases> is the positional arguments and *varargs argument
1790 <keywords> is the keyword arguments and **kwds argument
1791 This borrows from compiler_call.
1792 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001795 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1796 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001797 return 0;
1798 /* this block represents what we do in the new scope */
1799 {
1800 /* use the class name for name mangling */
1801 Py_INCREF(s->v.ClassDef.name);
1802 Py_XDECREF(c->u->u_private);
1803 c->u->u_private = s->v.ClassDef.name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001804 /* load (global) __name__ ... */
1805 str = PyUnicode_InternFromString("__name__");
1806 if (!str || !compiler_nameop(c, str, Load)) {
1807 Py_XDECREF(str);
1808 compiler_exit_scope(c);
1809 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 Py_DECREF(str);
1812 /* ... and store it as __module__ */
1813 str = PyUnicode_InternFromString("__module__");
1814 if (!str || !compiler_nameop(c, str, Store)) {
1815 Py_XDECREF(str);
1816 compiler_exit_scope(c);
1817 return 0;
1818 }
1819 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001820 assert(c->u->u_qualname);
1821 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001822 str = PyUnicode_InternFromString("__qualname__");
1823 if (!str || !compiler_nameop(c, str, Store)) {
1824 Py_XDECREF(str);
1825 compiler_exit_scope(c);
1826 return 0;
1827 }
1828 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 /* compile the body proper */
1830 if (!compiler_body(c, s->v.ClassDef.body)) {
1831 compiler_exit_scope(c);
1832 return 0;
1833 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001834 if (c->u->u_ste->ste_needs_class_closure) {
1835 /* return the (empty) __class__ cell */
1836 str = PyUnicode_InternFromString("__class__");
1837 if (str == NULL) {
1838 compiler_exit_scope(c);
1839 return 0;
1840 }
1841 i = compiler_lookup_arg(c->u->u_cellvars, str);
1842 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01001843 if (i < 0) {
1844 compiler_exit_scope(c);
1845 return 0;
1846 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001847 assert(i == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 /* Return the cell where to store __class__ */
1849 ADDOP_I(c, LOAD_CLOSURE, i);
1850 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001851 else {
1852 assert(PyDict_Size(c->u->u_cellvars) == 0);
1853 /* This happens when nobody references the cell. Return None. */
1854 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1855 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1857 /* create the code object */
1858 co = assemble(c, 1);
1859 }
1860 /* leave the new scope */
1861 compiler_exit_scope(c);
1862 if (co == NULL)
1863 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 /* 2. load the 'build_class' function */
1866 ADDOP(c, LOAD_BUILD_CLASS);
1867
1868 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001869 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 Py_DECREF(co);
1871
1872 /* 4. load class name */
1873 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1874
1875 /* 5. generate the rest of the code for the call */
1876 if (!compiler_call_helper(c, 2,
1877 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001878 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 return 0;
1880
1881 /* 6. apply decorators */
1882 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1883 ADDOP_I(c, CALL_FUNCTION, 1);
1884 }
1885
1886 /* 7. store into <name> */
1887 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1888 return 0;
1889 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890}
1891
1892static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001893compiler_ifexp(struct compiler *c, expr_ty e)
1894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 basicblock *end, *next;
1896
1897 assert(e->kind == IfExp_kind);
1898 end = compiler_new_block(c);
1899 if (end == NULL)
1900 return 0;
1901 next = compiler_new_block(c);
1902 if (next == NULL)
1903 return 0;
1904 VISIT(c, expr, e->v.IfExp.test);
1905 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1906 VISIT(c, expr, e->v.IfExp.body);
1907 ADDOP_JREL(c, JUMP_FORWARD, end);
1908 compiler_use_next_block(c, next);
1909 VISIT(c, expr, e->v.IfExp.orelse);
1910 compiler_use_next_block(c, end);
1911 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001912}
1913
1914static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001915compiler_lambda(struct compiler *c, expr_ty e)
1916{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001918 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 static identifier name;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001920 int kw_default_count = 0;
1921 Py_ssize_t arglength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 arguments_ty args = e->v.Lambda.args;
1923 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 if (!name) {
1926 name = PyUnicode_InternFromString("<lambda>");
1927 if (!name)
1928 return 0;
1929 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930
Benjamin Peterson419d9a82013-02-10 09:48:22 -05001931 if (args->defaults)
1932 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 if (args->kwonlyargs) {
1934 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1935 args->kw_defaults);
1936 if (res < 0) return 0;
1937 kw_default_count = res;
1938 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001939 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001940 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 /* Make None the first constant, so the lambda can't have a
1944 docstring. */
1945 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1946 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001947
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 c->u->u_argcount = asdl_seq_LEN(args->args);
1949 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1950 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1951 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001952 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 }
1954 else {
1955 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001956 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001958 qualname = c->u->u_qualname;
1959 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001961 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 arglength = asdl_seq_LEN(args->defaults);
1965 arglength |= kw_default_count << 8;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001966 compiler_make_closure(c, co, arglength, qualname);
1967 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 Py_DECREF(co);
1969
1970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971}
1972
1973static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974compiler_if(struct compiler *c, stmt_ty s)
1975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 basicblock *end, *next;
1977 int constant;
1978 assert(s->kind == If_kind);
1979 end = compiler_new_block(c);
1980 if (end == NULL)
1981 return 0;
1982
Georg Brandl8334fd92010-12-04 10:26:46 +00001983 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 /* constant = 0: "if 0"
1985 * constant = 1: "if 1", "if 2", ...
1986 * constant = -1: rest */
1987 if (constant == 0) {
1988 if (s->v.If.orelse)
1989 VISIT_SEQ(c, stmt, s->v.If.orelse);
1990 } else if (constant == 1) {
1991 VISIT_SEQ(c, stmt, s->v.If.body);
1992 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001993 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001994 next = compiler_new_block(c);
1995 if (next == NULL)
1996 return 0;
1997 }
1998 else
1999 next = end;
2000 VISIT(c, expr, s->v.If.test);
2001 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
2002 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002003 if (asdl_seq_LEN(s->v.If.orelse)) {
2004 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 compiler_use_next_block(c, next);
2006 VISIT_SEQ(c, stmt, s->v.If.orelse);
2007 }
2008 }
2009 compiler_use_next_block(c, end);
2010 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002011}
2012
2013static int
2014compiler_for(struct compiler *c, stmt_ty s)
2015{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 start = compiler_new_block(c);
2019 cleanup = compiler_new_block(c);
2020 end = compiler_new_block(c);
2021 if (start == NULL || end == NULL || cleanup == NULL)
2022 return 0;
2023 ADDOP_JREL(c, SETUP_LOOP, end);
2024 if (!compiler_push_fblock(c, LOOP, start))
2025 return 0;
2026 VISIT(c, expr, s->v.For.iter);
2027 ADDOP(c, GET_ITER);
2028 compiler_use_next_block(c, start);
2029 ADDOP_JREL(c, FOR_ITER, cleanup);
2030 VISIT(c, expr, s->v.For.target);
2031 VISIT_SEQ(c, stmt, s->v.For.body);
2032 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2033 compiler_use_next_block(c, cleanup);
2034 ADDOP(c, POP_BLOCK);
2035 compiler_pop_fblock(c, LOOP, start);
2036 VISIT_SEQ(c, stmt, s->v.For.orelse);
2037 compiler_use_next_block(c, end);
2038 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002039}
2040
Yury Selivanov75445082015-05-11 22:57:16 -04002041
2042static int
2043compiler_async_for(struct compiler *c, stmt_ty s)
2044{
2045 static PyObject *stopiter_error = NULL;
2046 basicblock *try, *except, *end, *after_try, *try_cleanup,
2047 *after_loop, *after_loop_else;
2048
2049 if (stopiter_error == NULL) {
2050 stopiter_error = PyUnicode_InternFromString("StopAsyncIteration");
2051 if (stopiter_error == NULL)
2052 return 0;
2053 }
2054
2055 try = compiler_new_block(c);
2056 except = compiler_new_block(c);
2057 end = compiler_new_block(c);
2058 after_try = compiler_new_block(c);
2059 try_cleanup = compiler_new_block(c);
2060 after_loop = compiler_new_block(c);
2061 after_loop_else = compiler_new_block(c);
2062
2063 if (try == NULL || except == NULL || end == NULL
2064 || after_try == NULL || try_cleanup == NULL)
2065 return 0;
2066
2067 ADDOP_JREL(c, SETUP_LOOP, after_loop);
2068 if (!compiler_push_fblock(c, LOOP, try))
2069 return 0;
2070
2071 VISIT(c, expr, s->v.AsyncFor.iter);
2072 ADDOP(c, GET_AITER);
2073 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2074 ADDOP(c, YIELD_FROM);
2075
2076 compiler_use_next_block(c, try);
2077
2078
2079 ADDOP_JREL(c, SETUP_EXCEPT, except);
2080 if (!compiler_push_fblock(c, EXCEPT, try))
2081 return 0;
2082
2083 ADDOP(c, GET_ANEXT);
2084 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2085 ADDOP(c, YIELD_FROM);
2086 VISIT(c, expr, s->v.AsyncFor.target);
2087 ADDOP(c, POP_BLOCK);
2088 compiler_pop_fblock(c, EXCEPT, try);
2089 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2090
2091
2092 compiler_use_next_block(c, except);
2093 ADDOP(c, DUP_TOP);
2094 ADDOP_O(c, LOAD_GLOBAL, stopiter_error, names);
2095 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2096 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2097
2098 ADDOP(c, POP_TOP);
2099 ADDOP(c, POP_TOP);
2100 ADDOP(c, POP_TOP);
2101 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2102 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2103 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2104
2105
2106 compiler_use_next_block(c, try_cleanup);
2107 ADDOP(c, END_FINALLY);
2108
2109 compiler_use_next_block(c, after_try);
2110 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2111 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2112
2113 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2114 compiler_pop_fblock(c, LOOP, try);
2115
2116 compiler_use_next_block(c, after_loop);
2117 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2118
2119 compiler_use_next_block(c, after_loop_else);
2120 VISIT_SEQ(c, stmt, s->v.For.orelse);
2121
2122 compiler_use_next_block(c, end);
2123
2124 return 1;
2125}
2126
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002127static int
2128compiler_while(struct compiler *c, stmt_ty s)
2129{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00002131 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 if (constant == 0) {
2134 if (s->v.While.orelse)
2135 VISIT_SEQ(c, stmt, s->v.While.orelse);
2136 return 1;
2137 }
2138 loop = compiler_new_block(c);
2139 end = compiler_new_block(c);
2140 if (constant == -1) {
2141 anchor = compiler_new_block(c);
2142 if (anchor == NULL)
2143 return 0;
2144 }
2145 if (loop == NULL || end == NULL)
2146 return 0;
2147 if (s->v.While.orelse) {
2148 orelse = compiler_new_block(c);
2149 if (orelse == NULL)
2150 return 0;
2151 }
2152 else
2153 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 ADDOP_JREL(c, SETUP_LOOP, end);
2156 compiler_use_next_block(c, loop);
2157 if (!compiler_push_fblock(c, LOOP, loop))
2158 return 0;
2159 if (constant == -1) {
2160 VISIT(c, expr, s->v.While.test);
2161 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
2162 }
2163 VISIT_SEQ(c, stmt, s->v.While.body);
2164 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002166 /* XXX should the two POP instructions be in a separate block
2167 if there is no else clause ?
2168 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002169
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002170 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 compiler_use_next_block(c, anchor);
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002172 ADDOP(c, POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 compiler_pop_fblock(c, LOOP, loop);
2174 if (orelse != NULL) /* what if orelse is just pass? */
2175 VISIT_SEQ(c, stmt, s->v.While.orelse);
2176 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002179}
2180
2181static int
2182compiler_continue(struct compiler *c)
2183{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2185 static const char IN_FINALLY_ERROR_MSG[] =
2186 "'continue' not supported inside 'finally' clause";
2187 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 if (!c->u->u_nfblocks)
2190 return compiler_error(c, LOOP_ERROR_MSG);
2191 i = c->u->u_nfblocks - 1;
2192 switch (c->u->u_fblock[i].fb_type) {
2193 case LOOP:
2194 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2195 break;
2196 case EXCEPT:
2197 case FINALLY_TRY:
2198 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2199 /* Prevent continue anywhere under a finally
2200 even if hidden in a sub-try or except. */
2201 if (c->u->u_fblock[i].fb_type == FINALLY_END)
2202 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2203 }
2204 if (i == -1)
2205 return compiler_error(c, LOOP_ERROR_MSG);
2206 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2207 break;
2208 case FINALLY_END:
2209 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2210 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213}
2214
2215/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216
2217 SETUP_FINALLY L
2218 <code for body>
2219 POP_BLOCK
2220 LOAD_CONST <None>
2221 L: <code for finalbody>
2222 END_FINALLY
2223
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002224 The special instructions use the block stack. Each block
2225 stack entry contains the instruction that created it (here
2226 SETUP_FINALLY), the level of the value stack at the time the
2227 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002229 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 Pushes the current value stack level and the label
2231 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002232 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 Pops en entry from the block stack, and pops the value
2234 stack until its level is the same as indicated on the
2235 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002236 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 Pops a variable number of entries from the *value* stack
2238 and re-raises the exception they specify. The number of
2239 entries popped depends on the (pseudo) exception type.
2240
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 The block stack is unwound when an exception is raised:
2242 when a SETUP_FINALLY entry is found, the exception is pushed
2243 onto the value stack (and the exception condition is cleared),
2244 and the interpreter jumps to the label gotten from the block
2245 stack.
2246*/
2247
2248static int
2249compiler_try_finally(struct compiler *c, stmt_ty s)
2250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 basicblock *body, *end;
2252 body = compiler_new_block(c);
2253 end = compiler_new_block(c);
2254 if (body == NULL || end == NULL)
2255 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 ADDOP_JREL(c, SETUP_FINALLY, end);
2258 compiler_use_next_block(c, body);
2259 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2260 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002261 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2262 if (!compiler_try_except(c, s))
2263 return 0;
2264 }
2265 else {
2266 VISIT_SEQ(c, stmt, s->v.Try.body);
2267 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 ADDOP(c, POP_BLOCK);
2269 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2272 compiler_use_next_block(c, end);
2273 if (!compiler_push_fblock(c, FINALLY_END, end))
2274 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002275 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 ADDOP(c, END_FINALLY);
2277 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002280}
2281
2282/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002283 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002284 (The contents of the value stack is shown in [], with the top
2285 at the right; 'tb' is trace-back info, 'val' the exception's
2286 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287
2288 Value stack Label Instruction Argument
2289 [] SETUP_EXCEPT L1
2290 [] <code for S>
2291 [] POP_BLOCK
2292 [] JUMP_FORWARD L0
2293
2294 [tb, val, exc] L1: DUP )
2295 [tb, val, exc, exc] <evaluate E1> )
2296 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2297 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2298 [tb, val, exc] POP
2299 [tb, val] <assign to V1> (or POP if no V1)
2300 [tb] POP
2301 [] <code for S1>
2302 JUMP_FORWARD L0
2303
2304 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002305 .............................etc.......................
2306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2308
2309 [] L0: <next statement>
2310
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002311 Of course, parts are not generated if Vi or Ei is not present.
2312*/
2313static int
2314compiler_try_except(struct compiler *c, stmt_ty s)
2315{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002316 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002317 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 body = compiler_new_block(c);
2320 except = compiler_new_block(c);
2321 orelse = compiler_new_block(c);
2322 end = compiler_new_block(c);
2323 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2324 return 0;
2325 ADDOP_JREL(c, SETUP_EXCEPT, except);
2326 compiler_use_next_block(c, body);
2327 if (!compiler_push_fblock(c, EXCEPT, body))
2328 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002329 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 ADDOP(c, POP_BLOCK);
2331 compiler_pop_fblock(c, EXCEPT, body);
2332 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002333 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 compiler_use_next_block(c, except);
2335 for (i = 0; i < n; i++) {
2336 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002337 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 if (!handler->v.ExceptHandler.type && i < n-1)
2339 return compiler_error(c, "default 'except:' must be last");
2340 c->u->u_lineno_set = 0;
2341 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002342 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 except = compiler_new_block(c);
2344 if (except == NULL)
2345 return 0;
2346 if (handler->v.ExceptHandler.type) {
2347 ADDOP(c, DUP_TOP);
2348 VISIT(c, expr, handler->v.ExceptHandler.type);
2349 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2350 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2351 }
2352 ADDOP(c, POP_TOP);
2353 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002354 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002355
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002356 cleanup_end = compiler_new_block(c);
2357 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002358 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002359 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002360
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002361 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2362 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002364 /*
2365 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002366 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002367 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002368 try:
2369 # body
2370 finally:
2371 name = None
2372 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002373 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002375 /* second try: */
2376 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2377 compiler_use_next_block(c, cleanup_body);
2378 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2379 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002381 /* second # body */
2382 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2383 ADDOP(c, POP_BLOCK);
2384 ADDOP(c, POP_EXCEPT);
2385 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002387 /* finally: */
2388 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2389 compiler_use_next_block(c, cleanup_end);
2390 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2391 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002393 /* name = None */
2394 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2395 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002397 /* del name */
2398 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002400 ADDOP(c, END_FINALLY);
2401 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 }
2403 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002404 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002406 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002407 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002408 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409
Guido van Rossumb940e112007-01-10 16:19:56 +00002410 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002411 ADDOP(c, POP_TOP);
2412 compiler_use_next_block(c, cleanup_body);
2413 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2414 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002416 ADDOP(c, POP_EXCEPT);
2417 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 }
2419 ADDOP_JREL(c, JUMP_FORWARD, end);
2420 compiler_use_next_block(c, except);
2421 }
2422 ADDOP(c, END_FINALLY);
2423 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002424 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 compiler_use_next_block(c, end);
2426 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002427}
2428
2429static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002430compiler_try(struct compiler *c, stmt_ty s) {
2431 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2432 return compiler_try_finally(c, s);
2433 else
2434 return compiler_try_except(c, s);
2435}
2436
2437
2438static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002439compiler_import_as(struct compiler *c, identifier name, identifier asname)
2440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 /* The IMPORT_NAME opcode was already generated. This function
2442 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 If there is a dot in name, we need to split it and emit a
2445 LOAD_ATTR for each name.
2446 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002447 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2448 PyUnicode_GET_LENGTH(name), 1);
2449 if (dot == -2)
2450 return -1;
2451 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002452 /* Consume the base module name to get the first attribute */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002453 Py_ssize_t pos = dot + 1;
2454 while (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 PyObject *attr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002456 dot = PyUnicode_FindChar(name, '.', pos,
2457 PyUnicode_GET_LENGTH(name), 1);
2458 if (dot == -2)
2459 return -1;
2460 attr = PyUnicode_Substring(name, pos,
2461 (dot != -1) ? dot :
2462 PyUnicode_GET_LENGTH(name));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 if (!attr)
2464 return -1;
2465 ADDOP_O(c, LOAD_ATTR, attr, names);
2466 Py_DECREF(attr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002467 pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002468 }
2469 }
2470 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002471}
2472
2473static int
2474compiler_import(struct compiler *c, stmt_ty s)
2475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 /* The Import node stores a module name like a.b.c as a single
2477 string. This is convenient for all cases except
2478 import a.b.c as d
2479 where we need to parse that string to extract the individual
2480 module names.
2481 XXX Perhaps change the representation to make this case simpler?
2482 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002483 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 for (i = 0; i < n; i++) {
2486 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2487 int r;
2488 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 level = PyLong_FromLong(0);
2491 if (level == NULL)
2492 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 ADDOP_O(c, LOAD_CONST, level, consts);
2495 Py_DECREF(level);
2496 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2497 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (alias->asname) {
2500 r = compiler_import_as(c, alias->name, alias->asname);
2501 if (!r)
2502 return r;
2503 }
2504 else {
2505 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002506 Py_ssize_t dot = PyUnicode_FindChar(
2507 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002508 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002509 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002510 if (tmp == NULL)
2511 return 0;
2512 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002514 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 Py_DECREF(tmp);
2516 }
2517 if (!r)
2518 return r;
2519 }
2520 }
2521 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522}
2523
2524static int
2525compiler_from_import(struct compiler *c, stmt_ty s)
2526{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002527 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 PyObject *names = PyTuple_New(n);
2530 PyObject *level;
2531 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 if (!empty_string) {
2534 empty_string = PyUnicode_FromString("");
2535 if (!empty_string)
2536 return 0;
2537 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 if (!names)
2540 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 level = PyLong_FromLong(s->v.ImportFrom.level);
2543 if (!level) {
2544 Py_DECREF(names);
2545 return 0;
2546 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 /* build up the names */
2549 for (i = 0; i < n; i++) {
2550 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2551 Py_INCREF(alias->name);
2552 PyTuple_SET_ITEM(names, i, alias->name);
2553 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2556 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2557 Py_DECREF(level);
2558 Py_DECREF(names);
2559 return compiler_error(c, "from __future__ imports must occur "
2560 "at the beginning of the file");
2561 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 ADDOP_O(c, LOAD_CONST, level, consts);
2564 Py_DECREF(level);
2565 ADDOP_O(c, LOAD_CONST, names, consts);
2566 Py_DECREF(names);
2567 if (s->v.ImportFrom.module) {
2568 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2569 }
2570 else {
2571 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2572 }
2573 for (i = 0; i < n; i++) {
2574 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2575 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002576
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002577 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 assert(n == 1);
2579 ADDOP(c, IMPORT_STAR);
2580 return 1;
2581 }
2582
2583 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2584 store_name = alias->name;
2585 if (alias->asname)
2586 store_name = alias->asname;
2587
2588 if (!compiler_nameop(c, store_name, Store)) {
2589 Py_DECREF(names);
2590 return 0;
2591 }
2592 }
2593 /* remove imported module */
2594 ADDOP(c, POP_TOP);
2595 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596}
2597
2598static int
2599compiler_assert(struct compiler *c, stmt_ty s)
2600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 static PyObject *assertion_error = NULL;
2602 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02002603 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002604
Georg Brandl8334fd92010-12-04 10:26:46 +00002605 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 return 1;
2607 if (assertion_error == NULL) {
2608 assertion_error = PyUnicode_InternFromString("AssertionError");
2609 if (assertion_error == NULL)
2610 return 0;
2611 }
2612 if (s->v.Assert.test->kind == Tuple_kind &&
2613 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02002614 msg = PyUnicode_FromString("assertion is always true, "
2615 "perhaps remove parentheses?");
2616 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002618 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2619 c->c_filename, c->u->u_lineno,
2620 NULL, NULL) == -1) {
2621 Py_DECREF(msg);
2622 return 0;
2623 }
2624 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 }
2626 VISIT(c, expr, s->v.Assert.test);
2627 end = compiler_new_block(c);
2628 if (end == NULL)
2629 return 0;
2630 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2631 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2632 if (s->v.Assert.msg) {
2633 VISIT(c, expr, s->v.Assert.msg);
2634 ADDOP_I(c, CALL_FUNCTION, 1);
2635 }
2636 ADDOP_I(c, RAISE_VARARGS, 1);
2637 compiler_use_next_block(c, end);
2638 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002639}
2640
2641static int
2642compiler_visit_stmt(struct compiler *c, stmt_ty s)
2643{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002644 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 /* Always assign a lineno to the next instruction for a stmt. */
2647 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002648 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 switch (s->kind) {
2652 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04002653 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 case ClassDef_kind:
2655 return compiler_class(c, s);
2656 case Return_kind:
2657 if (c->u->u_ste->ste_type != FunctionBlock)
2658 return compiler_error(c, "'return' outside function");
2659 if (s->v.Return.value) {
2660 VISIT(c, expr, s->v.Return.value);
2661 }
2662 else
2663 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2664 ADDOP(c, RETURN_VALUE);
2665 break;
2666 case Delete_kind:
2667 VISIT_SEQ(c, expr, s->v.Delete.targets)
2668 break;
2669 case Assign_kind:
2670 n = asdl_seq_LEN(s->v.Assign.targets);
2671 VISIT(c, expr, s->v.Assign.value);
2672 for (i = 0; i < n; i++) {
2673 if (i < n - 1)
2674 ADDOP(c, DUP_TOP);
2675 VISIT(c, expr,
2676 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2677 }
2678 break;
2679 case AugAssign_kind:
2680 return compiler_augassign(c, s);
2681 case For_kind:
2682 return compiler_for(c, s);
2683 case While_kind:
2684 return compiler_while(c, s);
2685 case If_kind:
2686 return compiler_if(c, s);
2687 case Raise_kind:
2688 n = 0;
2689 if (s->v.Raise.exc) {
2690 VISIT(c, expr, s->v.Raise.exc);
2691 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002692 if (s->v.Raise.cause) {
2693 VISIT(c, expr, s->v.Raise.cause);
2694 n++;
2695 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01002697 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002699 case Try_kind:
2700 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 case Assert_kind:
2702 return compiler_assert(c, s);
2703 case Import_kind:
2704 return compiler_import(c, s);
2705 case ImportFrom_kind:
2706 return compiler_from_import(c, s);
2707 case Global_kind:
2708 case Nonlocal_kind:
2709 break;
2710 case Expr_kind:
2711 if (c->c_interactive && c->c_nestlevel <= 1) {
2712 VISIT(c, expr, s->v.Expr.value);
2713 ADDOP(c, PRINT_EXPR);
2714 }
2715 else if (s->v.Expr.value->kind != Str_kind &&
2716 s->v.Expr.value->kind != Num_kind) {
2717 VISIT(c, expr, s->v.Expr.value);
2718 ADDOP(c, POP_TOP);
2719 }
2720 break;
2721 case Pass_kind:
2722 break;
2723 case Break_kind:
2724 if (!compiler_in_loop(c))
2725 return compiler_error(c, "'break' outside loop");
2726 ADDOP(c, BREAK_LOOP);
2727 break;
2728 case Continue_kind:
2729 return compiler_continue(c);
2730 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002731 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04002732 case AsyncFunctionDef_kind:
2733 return compiler_function(c, s, 1);
2734 case AsyncWith_kind:
2735 return compiler_async_with(c, s, 0);
2736 case AsyncFor_kind:
2737 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 }
Yury Selivanov75445082015-05-11 22:57:16 -04002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002741}
2742
2743static int
2744unaryop(unaryop_ty op)
2745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 switch (op) {
2747 case Invert:
2748 return UNARY_INVERT;
2749 case Not:
2750 return UNARY_NOT;
2751 case UAdd:
2752 return UNARY_POSITIVE;
2753 case USub:
2754 return UNARY_NEGATIVE;
2755 default:
2756 PyErr_Format(PyExc_SystemError,
2757 "unary op %d should not be possible", op);
2758 return 0;
2759 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002760}
2761
2762static int
2763binop(struct compiler *c, operator_ty op)
2764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 switch (op) {
2766 case Add:
2767 return BINARY_ADD;
2768 case Sub:
2769 return BINARY_SUBTRACT;
2770 case Mult:
2771 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002772 case MatMult:
2773 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 case Div:
2775 return BINARY_TRUE_DIVIDE;
2776 case Mod:
2777 return BINARY_MODULO;
2778 case Pow:
2779 return BINARY_POWER;
2780 case LShift:
2781 return BINARY_LSHIFT;
2782 case RShift:
2783 return BINARY_RSHIFT;
2784 case BitOr:
2785 return BINARY_OR;
2786 case BitXor:
2787 return BINARY_XOR;
2788 case BitAnd:
2789 return BINARY_AND;
2790 case FloorDiv:
2791 return BINARY_FLOOR_DIVIDE;
2792 default:
2793 PyErr_Format(PyExc_SystemError,
2794 "binary op %d should not be possible", op);
2795 return 0;
2796 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002797}
2798
2799static int
2800cmpop(cmpop_ty op)
2801{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 switch (op) {
2803 case Eq:
2804 return PyCmp_EQ;
2805 case NotEq:
2806 return PyCmp_NE;
2807 case Lt:
2808 return PyCmp_LT;
2809 case LtE:
2810 return PyCmp_LE;
2811 case Gt:
2812 return PyCmp_GT;
2813 case GtE:
2814 return PyCmp_GE;
2815 case Is:
2816 return PyCmp_IS;
2817 case IsNot:
2818 return PyCmp_IS_NOT;
2819 case In:
2820 return PyCmp_IN;
2821 case NotIn:
2822 return PyCmp_NOT_IN;
2823 default:
2824 return PyCmp_BAD;
2825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826}
2827
2828static int
2829inplace_binop(struct compiler *c, operator_ty op)
2830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 switch (op) {
2832 case Add:
2833 return INPLACE_ADD;
2834 case Sub:
2835 return INPLACE_SUBTRACT;
2836 case Mult:
2837 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002838 case MatMult:
2839 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 case Div:
2841 return INPLACE_TRUE_DIVIDE;
2842 case Mod:
2843 return INPLACE_MODULO;
2844 case Pow:
2845 return INPLACE_POWER;
2846 case LShift:
2847 return INPLACE_LSHIFT;
2848 case RShift:
2849 return INPLACE_RSHIFT;
2850 case BitOr:
2851 return INPLACE_OR;
2852 case BitXor:
2853 return INPLACE_XOR;
2854 case BitAnd:
2855 return INPLACE_AND;
2856 case FloorDiv:
2857 return INPLACE_FLOOR_DIVIDE;
2858 default:
2859 PyErr_Format(PyExc_SystemError,
2860 "inplace binary op %d should not be possible", op);
2861 return 0;
2862 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002863}
2864
2865static int
2866compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2867{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002868 int op, scope;
2869 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 PyObject *dict = c->u->u_names;
2873 PyObject *mangled;
2874 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 mangled = _Py_Mangle(c->u->u_private, name);
2877 if (!mangled)
2878 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002879
Benjamin Peterson70b224d2012-12-06 17:49:58 -05002880 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2881 PyUnicode_CompareWithASCIIString(name, "True") &&
2882 PyUnicode_CompareWithASCIIString(name, "False"));
2883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 op = 0;
2885 optype = OP_NAME;
2886 scope = PyST_GetScope(c->u->u_ste, mangled);
2887 switch (scope) {
2888 case FREE:
2889 dict = c->u->u_freevars;
2890 optype = OP_DEREF;
2891 break;
2892 case CELL:
2893 dict = c->u->u_cellvars;
2894 optype = OP_DEREF;
2895 break;
2896 case LOCAL:
2897 if (c->u->u_ste->ste_type == FunctionBlock)
2898 optype = OP_FAST;
2899 break;
2900 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04002901 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 optype = OP_GLOBAL;
2903 break;
2904 case GLOBAL_EXPLICIT:
2905 optype = OP_GLOBAL;
2906 break;
2907 default:
2908 /* scope can be 0 */
2909 break;
2910 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002913 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 switch (optype) {
2916 case OP_DEREF:
2917 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002918 case Load:
2919 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
2920 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 case Store: op = STORE_DEREF; break;
2922 case AugLoad:
2923 case AugStore:
2924 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002925 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 case Param:
2927 default:
2928 PyErr_SetString(PyExc_SystemError,
2929 "param invalid for deref variable");
2930 return 0;
2931 }
2932 break;
2933 case OP_FAST:
2934 switch (ctx) {
2935 case Load: op = LOAD_FAST; break;
2936 case Store: op = STORE_FAST; break;
2937 case Del: op = DELETE_FAST; break;
2938 case AugLoad:
2939 case AugStore:
2940 break;
2941 case Param:
2942 default:
2943 PyErr_SetString(PyExc_SystemError,
2944 "param invalid for local variable");
2945 return 0;
2946 }
2947 ADDOP_O(c, op, mangled, varnames);
2948 Py_DECREF(mangled);
2949 return 1;
2950 case OP_GLOBAL:
2951 switch (ctx) {
2952 case Load: op = LOAD_GLOBAL; break;
2953 case Store: op = STORE_GLOBAL; break;
2954 case Del: op = DELETE_GLOBAL; break;
2955 case AugLoad:
2956 case AugStore:
2957 break;
2958 case Param:
2959 default:
2960 PyErr_SetString(PyExc_SystemError,
2961 "param invalid for global variable");
2962 return 0;
2963 }
2964 break;
2965 case OP_NAME:
2966 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002967 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 case Store: op = STORE_NAME; break;
2969 case Del: op = DELETE_NAME; break;
2970 case AugLoad:
2971 case AugStore:
2972 break;
2973 case Param:
2974 default:
2975 PyErr_SetString(PyExc_SystemError,
2976 "param invalid for name variable");
2977 return 0;
2978 }
2979 break;
2980 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 assert(op);
2983 arg = compiler_add_o(c, dict, mangled);
2984 Py_DECREF(mangled);
2985 if (arg < 0)
2986 return 0;
2987 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002988}
2989
2990static int
2991compiler_boolop(struct compiler *c, expr_ty e)
2992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002994 int jumpi;
2995 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 assert(e->kind == BoolOp_kind);
2999 if (e->v.BoolOp.op == And)
3000 jumpi = JUMP_IF_FALSE_OR_POP;
3001 else
3002 jumpi = JUMP_IF_TRUE_OR_POP;
3003 end = compiler_new_block(c);
3004 if (end == NULL)
3005 return 0;
3006 s = e->v.BoolOp.values;
3007 n = asdl_seq_LEN(s) - 1;
3008 assert(n >= 0);
3009 for (i = 0; i < n; ++i) {
3010 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3011 ADDOP_JABS(c, jumpi, end);
3012 }
3013 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3014 compiler_use_next_block(c, end);
3015 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003016}
3017
3018static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003019starunpack_helper(struct compiler *c, asdl_seq *elts,
3020 int single_op, int inner_op, int outer_op)
3021{
3022 Py_ssize_t n = asdl_seq_LEN(elts);
3023 Py_ssize_t i, nsubitems = 0, nseen = 0;
3024 for (i = 0; i < n; i++) {
3025 expr_ty elt = asdl_seq_GET(elts, i);
3026 if (elt->kind == Starred_kind) {
3027 if (nseen) {
3028 ADDOP_I(c, inner_op, nseen);
3029 nseen = 0;
3030 nsubitems++;
3031 }
3032 VISIT(c, expr, elt->v.Starred.value);
3033 nsubitems++;
3034 }
3035 else {
3036 VISIT(c, expr, elt);
3037 nseen++;
3038 }
3039 }
3040 if (nsubitems) {
3041 if (nseen) {
3042 ADDOP_I(c, inner_op, nseen);
3043 nsubitems++;
3044 }
3045 ADDOP_I(c, outer_op, nsubitems);
3046 }
3047 else
3048 ADDOP_I(c, single_op, nseen);
3049 return 1;
3050}
3051
3052static int
3053assignment_helper(struct compiler *c, asdl_seq *elts)
3054{
3055 Py_ssize_t n = asdl_seq_LEN(elts);
3056 Py_ssize_t i;
3057 int seen_star = 0;
3058 for (i = 0; i < n; i++) {
3059 expr_ty elt = asdl_seq_GET(elts, i);
3060 if (elt->kind == Starred_kind && !seen_star) {
3061 if ((i >= (1 << 8)) ||
3062 (n-i-1 >= (INT_MAX >> 8)))
3063 return compiler_error(c,
3064 "too many expressions in "
3065 "star-unpacking assignment");
3066 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3067 seen_star = 1;
3068 asdl_seq_SET(elts, i, elt->v.Starred.value);
3069 }
3070 else if (elt->kind == Starred_kind) {
3071 return compiler_error(c,
3072 "two starred expressions in assignment");
3073 }
3074 }
3075 if (!seen_star) {
3076 ADDOP_I(c, UNPACK_SEQUENCE, n);
3077 }
3078 VISIT_SEQ(c, expr, elts);
3079 return 1;
3080}
3081
3082static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003083compiler_list(struct compiler *c, expr_ty e)
3084{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003085 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003087 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003089 else if (e->v.List.ctx == Load) {
3090 return starunpack_helper(c, elts,
3091 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003093 else
3094 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003096}
3097
3098static int
3099compiler_tuple(struct compiler *c, expr_ty e)
3100{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003101 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003103 return assignment_helper(c, elts);
3104 }
3105 else if (e->v.Tuple.ctx == Load) {
3106 return starunpack_helper(c, elts,
3107 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3108 }
3109 else
3110 VISIT_SEQ(c, expr, elts);
3111 return 1;
3112}
3113
3114static int
3115compiler_set(struct compiler *c, expr_ty e)
3116{
3117 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3118 BUILD_SET, BUILD_SET_UNPACK);
3119}
3120
3121static int
3122compiler_dict(struct compiler *c, expr_ty e)
3123{
3124 Py_ssize_t i, n, containers, elements;
3125 int is_unpacking = 0;
3126 n = asdl_seq_LEN(e->v.Dict.values);
3127 containers = 0;
3128 elements = 0;
3129 for (i = 0; i < n; i++) {
3130 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3131 if (elements == 0xFFFF || (elements && is_unpacking)) {
3132 ADDOP_I(c, BUILD_MAP, elements);
3133 containers++;
3134 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003136 if (is_unpacking) {
3137 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3138 containers++;
3139 }
3140 else {
3141 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3142 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3143 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 }
3145 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003146 if (elements || containers == 0) {
3147 ADDOP_I(c, BUILD_MAP, elements);
3148 containers++;
3149 }
3150 /* If there is more than one dict, they need to be merged into a new
3151 * dict. If there is one dict and it's an unpacking, then it needs
3152 * to be copied into a new dict." */
3153 while (containers > 1 || is_unpacking) {
3154 int oparg = containers < 255 ? containers : 255;
3155 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3156 containers -= (oparg - 1);
3157 is_unpacking = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 }
3159 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160}
3161
3162static int
3163compiler_compare(struct compiler *c, expr_ty e)
3164{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003165 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
3169 VISIT(c, expr, e->v.Compare.left);
3170 n = asdl_seq_LEN(e->v.Compare.ops);
3171 assert(n > 0);
3172 if (n > 1) {
3173 cleanup = compiler_new_block(c);
3174 if (cleanup == NULL)
3175 return 0;
3176 VISIT(c, expr,
3177 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3178 }
3179 for (i = 1; i < n; i++) {
3180 ADDOP(c, DUP_TOP);
3181 ADDOP(c, ROT_THREE);
3182 ADDOP_I(c, COMPARE_OP,
3183 cmpop((cmpop_ty)(asdl_seq_GET(
3184 e->v.Compare.ops, i - 1))));
3185 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3186 NEXT_BLOCK(c);
3187 if (i < (n - 1))
3188 VISIT(c, expr,
3189 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
3190 }
3191 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
3192 ADDOP_I(c, COMPARE_OP,
3193 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
3194 if (n > 1) {
3195 basicblock *end = compiler_new_block(c);
3196 if (end == NULL)
3197 return 0;
3198 ADDOP_JREL(c, JUMP_FORWARD, end);
3199 compiler_use_next_block(c, cleanup);
3200 ADDOP(c, ROT_TWO);
3201 ADDOP(c, POP_TOP);
3202 compiler_use_next_block(c, end);
3203 }
3204 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003205}
3206
3207static int
3208compiler_call(struct compiler *c, expr_ty e)
3209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 VISIT(c, expr, e->v.Call.func);
3211 return compiler_call_helper(c, 0,
3212 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003213 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003214}
3215
3216/* shared code between compiler_call and compiler_class */
3217static int
3218compiler_call_helper(struct compiler *c,
Victor Stinnerad9a0662013-11-19 22:23:20 +01003219 Py_ssize_t n, /* Args already pushed */
3220 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003221 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 int code = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003224 Py_ssize_t nelts, i, nseen, nkw;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003225
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003226 /* the number of tuples and dictionaries on the stack */
3227 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3228
3229 nkw = 0;
3230 nseen = 0; /* the number of positional arguments on the stack */
3231 nelts = asdl_seq_LEN(args);
3232 for (i = 0; i < nelts; i++) {
3233 expr_ty elt = asdl_seq_GET(args, i);
3234 if (elt->kind == Starred_kind) {
3235 /* A star-arg. If we've seen positional arguments,
3236 pack the positional arguments into a
3237 tuple. */
3238 if (nseen) {
3239 ADDOP_I(c, BUILD_TUPLE, nseen);
3240 nseen = 0;
3241 nsubargs++;
3242 }
3243 VISIT(c, expr, elt->v.Starred.value);
3244 nsubargs++;
3245 }
3246 else if (nsubargs) {
3247 /* We've seen star-args already, so we
3248 count towards items-to-pack-into-tuple. */
3249 VISIT(c, expr, elt);
3250 nseen++;
3251 }
3252 else {
3253 /* Positional arguments before star-arguments
3254 are left on the stack. */
3255 VISIT(c, expr, elt);
3256 n++;
3257 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003259 if (nseen) {
3260 /* Pack up any trailing positional arguments. */
3261 ADDOP_I(c, BUILD_TUPLE, nseen);
3262 nsubargs++;
3263 }
3264 if (nsubargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 code |= 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003266 if (nsubargs > 1) {
3267 /* If we ended up with more than one stararg, we need
3268 to concatenate them into a single sequence. */
3269 ADDOP_I(c, BUILD_LIST_UNPACK, nsubargs);
3270 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003272
3273 /* Same dance again for keyword arguments */
3274 nseen = 0; /* the number of keyword arguments on the stack following */
3275 nelts = asdl_seq_LEN(keywords);
3276 for (i = 0; i < nelts; i++) {
3277 keyword_ty kw = asdl_seq_GET(keywords, i);
3278 if (kw->arg == NULL) {
3279 /* A keyword argument unpacking. */
3280 if (nseen) {
3281 ADDOP_I(c, BUILD_MAP, nseen);
3282 nseen = 0;
3283 nsubkwargs++;
3284 }
3285 VISIT(c, expr, kw->value);
3286 nsubkwargs++;
3287 }
3288 else if (nsubkwargs) {
3289 /* A keyword argument and we already have a dict. */
3290 VISIT(c, expr, kw->value);
3291 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3292 nseen++;
3293 }
3294 else {
3295 /* keyword argument */
3296 VISIT(c, keyword, kw)
3297 nkw++;
3298 }
3299 }
3300 if (nseen) {
3301 /* Pack up any trailing keyword arguments. */
3302 ADDOP_I(c, BUILD_MAP, nseen);
3303 nsubkwargs++;
3304 }
3305 if (nsubkwargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 code |= 2;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003307 if (nsubkwargs > 1) {
3308 /* Pack it all up */
3309 int function_pos = n + (code & 1) + nkw + 1;
3310 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8));
3311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003313 assert(n < 1<<8);
3314 assert(nkw < 1<<24);
3315 n |= nkw << 8;
3316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 switch (code) {
3318 case 0:
3319 ADDOP_I(c, CALL_FUNCTION, n);
3320 break;
3321 case 1:
3322 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3323 break;
3324 case 2:
3325 ADDOP_I(c, CALL_FUNCTION_KW, n);
3326 break;
3327 case 3:
3328 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3329 break;
3330 }
3331 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332}
3333
Nick Coghlan650f0d02007-04-15 12:05:43 +00003334
3335/* List and set comprehensions and generator expressions work by creating a
3336 nested function to perform the actual iteration. This means that the
3337 iteration variables don't leak into the current scope.
3338 The defined function is called immediately following its definition, with the
3339 result of that call being the result of the expression.
3340 The LC/SC version returns the populated container, while the GE version is
3341 flagged in symtable.c as a generator, so it returns the generator object
3342 when the function is called.
3343 This code *knows* that the loop cannot contain break, continue, or return,
3344 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3345
3346 Possible cleanups:
3347 - iterate over the generator sequence instead of using recursion
3348*/
3349
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003350static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351compiler_comprehension_generator(struct compiler *c,
3352 asdl_seq *generators, int gen_index,
3353 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 /* generate code for the iterator, then each of the ifs,
3356 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003358 comprehension_ty gen;
3359 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003360 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 start = compiler_new_block(c);
3363 skip = compiler_new_block(c);
3364 if_cleanup = compiler_new_block(c);
3365 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3368 anchor == NULL)
3369 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 if (gen_index == 0) {
3374 /* Receive outermost iter as an implicit argument */
3375 c->u->u_argcount = 1;
3376 ADDOP_I(c, LOAD_FAST, 0);
3377 }
3378 else {
3379 /* Sub-iter - calculate on the fly */
3380 VISIT(c, expr, gen->iter);
3381 ADDOP(c, GET_ITER);
3382 }
3383 compiler_use_next_block(c, start);
3384 ADDOP_JREL(c, FOR_ITER, anchor);
3385 NEXT_BLOCK(c);
3386 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 /* XXX this needs to be cleaned up...a lot! */
3389 n = asdl_seq_LEN(gen->ifs);
3390 for (i = 0; i < n; i++) {
3391 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3392 VISIT(c, expr, e);
3393 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3394 NEXT_BLOCK(c);
3395 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 if (++gen_index < asdl_seq_LEN(generators))
3398 if (!compiler_comprehension_generator(c,
3399 generators, gen_index,
3400 elt, val, type))
3401 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 /* only append after the last for generator */
3404 if (gen_index >= asdl_seq_LEN(generators)) {
3405 /* comprehension specific code */
3406 switch (type) {
3407 case COMP_GENEXP:
3408 VISIT(c, expr, elt);
3409 ADDOP(c, YIELD_VALUE);
3410 ADDOP(c, POP_TOP);
3411 break;
3412 case COMP_LISTCOMP:
3413 VISIT(c, expr, elt);
3414 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3415 break;
3416 case COMP_SETCOMP:
3417 VISIT(c, expr, elt);
3418 ADDOP_I(c, SET_ADD, gen_index + 1);
3419 break;
3420 case COMP_DICTCOMP:
3421 /* With 'd[k] = v', v is evaluated before k, so we do
3422 the same. */
3423 VISIT(c, expr, val);
3424 VISIT(c, expr, elt);
3425 ADDOP_I(c, MAP_ADD, gen_index + 1);
3426 break;
3427 default:
3428 return 0;
3429 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 compiler_use_next_block(c, skip);
3432 }
3433 compiler_use_next_block(c, if_cleanup);
3434 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3435 compiler_use_next_block(c, anchor);
3436
3437 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003438}
3439
3440static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003441compiler_comprehension(struct compiler *c, expr_ty e, int type,
3442 identifier name, asdl_seq *generators, expr_ty elt,
3443 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 PyCodeObject *co = NULL;
3446 expr_ty outermost_iter;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003447 PyObject *qualname = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 outermost_iter = ((comprehension_ty)
3450 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003451
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003452 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3453 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 if (type != COMP_GENEXP) {
3457 int op;
3458 switch (type) {
3459 case COMP_LISTCOMP:
3460 op = BUILD_LIST;
3461 break;
3462 case COMP_SETCOMP:
3463 op = BUILD_SET;
3464 break;
3465 case COMP_DICTCOMP:
3466 op = BUILD_MAP;
3467 break;
3468 default:
3469 PyErr_Format(PyExc_SystemError,
3470 "unknown comprehension type %d", type);
3471 goto error_in_scope;
3472 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 ADDOP_I(c, op, 0);
3475 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 if (!compiler_comprehension_generator(c, generators, 0, elt,
3478 val, type))
3479 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 if (type != COMP_GENEXP) {
3482 ADDOP(c, RETURN_VALUE);
3483 }
3484
3485 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003486 qualname = c->u->u_qualname;
3487 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003489 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 goto error;
3491
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003492 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003494 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 Py_DECREF(co);
3496
3497 VISIT(c, expr, outermost_iter);
3498 ADDOP(c, GET_ITER);
3499 ADDOP_I(c, CALL_FUNCTION, 1);
3500 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003501error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003503error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003504 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 Py_XDECREF(co);
3506 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003507}
3508
3509static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510compiler_genexp(struct compiler *c, expr_ty e)
3511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 static identifier name;
3513 if (!name) {
3514 name = PyUnicode_FromString("<genexpr>");
3515 if (!name)
3516 return 0;
3517 }
3518 assert(e->kind == GeneratorExp_kind);
3519 return compiler_comprehension(c, e, COMP_GENEXP, name,
3520 e->v.GeneratorExp.generators,
3521 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003522}
3523
3524static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003525compiler_listcomp(struct compiler *c, expr_ty e)
3526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 static identifier name;
3528 if (!name) {
3529 name = PyUnicode_FromString("<listcomp>");
3530 if (!name)
3531 return 0;
3532 }
3533 assert(e->kind == ListComp_kind);
3534 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3535 e->v.ListComp.generators,
3536 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003537}
3538
3539static int
3540compiler_setcomp(struct compiler *c, expr_ty e)
3541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 static identifier name;
3543 if (!name) {
3544 name = PyUnicode_FromString("<setcomp>");
3545 if (!name)
3546 return 0;
3547 }
3548 assert(e->kind == SetComp_kind);
3549 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3550 e->v.SetComp.generators,
3551 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003552}
3553
3554
3555static int
3556compiler_dictcomp(struct compiler *c, expr_ty e)
3557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 static identifier name;
3559 if (!name) {
3560 name = PyUnicode_FromString("<dictcomp>");
3561 if (!name)
3562 return 0;
3563 }
3564 assert(e->kind == DictComp_kind);
3565 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3566 e->v.DictComp.generators,
3567 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003568}
3569
3570
3571static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003572compiler_visit_keyword(struct compiler *c, keyword_ty k)
3573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3575 VISIT(c, expr, k->value);
3576 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003577}
3578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003580 whether they are true or false.
3581
3582 Return values: 1 for true, 0 for false, -1 for non-constant.
3583 */
3584
3585static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003586expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 char *id;
3589 switch (e->kind) {
3590 case Ellipsis_kind:
3591 return 1;
3592 case Num_kind:
3593 return PyObject_IsTrue(e->v.Num.n);
3594 case Str_kind:
3595 return PyObject_IsTrue(e->v.Str.s);
3596 case Name_kind:
3597 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003598 id = PyUnicode_AsUTF8(e->v.Name.id);
Benjamin Peterson442f2092012-12-06 17:41:04 -05003599 if (id && strcmp(id, "__debug__") == 0)
3600 return !c->c_optimize;
3601 return -1;
3602 case NameConstant_kind: {
3603 PyObject *o = e->v.NameConstant.value;
3604 if (o == Py_None)
3605 return 0;
3606 else if (o == Py_True)
3607 return 1;
3608 else if (o == Py_False)
3609 return 0;
3610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 default:
3612 return -1;
3613 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003614}
3615
Yury Selivanov75445082015-05-11 22:57:16 -04003616
3617/*
3618 Implements the async with statement.
3619
3620 The semantics outlined in that PEP are as follows:
3621
3622 async with EXPR as VAR:
3623 BLOCK
3624
3625 It is implemented roughly as:
3626
3627 context = EXPR
3628 exit = context.__aexit__ # not calling it
3629 value = await context.__aenter__()
3630 try:
3631 VAR = value # if VAR present in the syntax
3632 BLOCK
3633 finally:
3634 if an exception was raised:
3635 exc = copy of (exception, instance, traceback)
3636 else:
3637 exc = (None, None, None)
3638 if not (await exit(*exc)):
3639 raise
3640 */
3641static int
3642compiler_async_with(struct compiler *c, stmt_ty s, int pos)
3643{
3644 basicblock *block, *finally;
3645 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
3646
3647 assert(s->kind == AsyncWith_kind);
3648
3649 block = compiler_new_block(c);
3650 finally = compiler_new_block(c);
3651 if (!block || !finally)
3652 return 0;
3653
3654 /* Evaluate EXPR */
3655 VISIT(c, expr, item->context_expr);
3656
3657 ADDOP(c, BEFORE_ASYNC_WITH);
3658 ADDOP(c, GET_AWAITABLE);
3659 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3660 ADDOP(c, YIELD_FROM);
3661
3662 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
3663
3664 /* SETUP_ASYNC_WITH pushes a finally block. */
3665 compiler_use_next_block(c, block);
3666 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3667 return 0;
3668 }
3669
3670 if (item->optional_vars) {
3671 VISIT(c, expr, item->optional_vars);
3672 }
3673 else {
3674 /* Discard result from context.__aenter__() */
3675 ADDOP(c, POP_TOP);
3676 }
3677
3678 pos++;
3679 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
3680 /* BLOCK code */
3681 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
3682 else if (!compiler_async_with(c, s, pos))
3683 return 0;
3684
3685 /* End of try block; start the finally block */
3686 ADDOP(c, POP_BLOCK);
3687 compiler_pop_fblock(c, FINALLY_TRY, block);
3688
3689 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3690 compiler_use_next_block(c, finally);
3691 if (!compiler_push_fblock(c, FINALLY_END, finally))
3692 return 0;
3693
3694 /* Finally block starts; context.__exit__ is on the stack under
3695 the exception or return information. Just issue our magic
3696 opcode. */
3697 ADDOP(c, WITH_CLEANUP_START);
3698
3699 ADDOP(c, GET_AWAITABLE);
3700 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3701 ADDOP(c, YIELD_FROM);
3702
3703 ADDOP(c, WITH_CLEANUP_FINISH);
3704
3705 /* Finally block ends. */
3706 ADDOP(c, END_FINALLY);
3707 compiler_pop_fblock(c, FINALLY_END, finally);
3708 return 1;
3709}
3710
3711
Guido van Rossumc2e20742006-02-27 22:32:47 +00003712/*
3713 Implements the with statement from PEP 343.
3714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003716
3717 with EXPR as VAR:
3718 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719
Guido van Rossumc2e20742006-02-27 22:32:47 +00003720 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721
Thomas Wouters477c8d52006-05-27 19:21:47 +00003722 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003723 exit = context.__exit__ # not calling it
3724 value = context.__enter__()
3725 try:
3726 VAR = value # if VAR present in the syntax
3727 BLOCK
3728 finally:
3729 if an exception was raised:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003731 else:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003733 exit(*exc)
3734 */
3735static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003736compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003737{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003738 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003739 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003740
3741 assert(s->kind == With_kind);
3742
Guido van Rossumc2e20742006-02-27 22:32:47 +00003743 block = compiler_new_block(c);
3744 finally = compiler_new_block(c);
3745 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003746 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003747
Thomas Wouters477c8d52006-05-27 19:21:47 +00003748 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003749 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003750 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003751
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003752 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003753 compiler_use_next_block(c, block);
3754 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003755 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003756 }
3757
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003758 if (item->optional_vars) {
3759 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003760 }
3761 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003763 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003764 }
3765
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003766 pos++;
3767 if (pos == asdl_seq_LEN(s->v.With.items))
3768 /* BLOCK code */
3769 VISIT_SEQ(c, stmt, s->v.With.body)
3770 else if (!compiler_with(c, s, pos))
3771 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003772
3773 /* End of try block; start the finally block */
3774 ADDOP(c, POP_BLOCK);
3775 compiler_pop_fblock(c, FINALLY_TRY, block);
3776
3777 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3778 compiler_use_next_block(c, finally);
3779 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003780 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003781
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003782 /* Finally block starts; context.__exit__ is on the stack under
3783 the exception or return information. Just issue our magic
3784 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04003785 ADDOP(c, WITH_CLEANUP_START);
3786 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003787
3788 /* Finally block ends. */
3789 ADDOP(c, END_FINALLY);
3790 compiler_pop_fblock(c, FINALLY_END, finally);
3791 return 1;
3792}
3793
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003794static int
3795compiler_visit_expr(struct compiler *c, expr_ty e)
3796{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 /* If expr e has a different line number than the last expr/stmt,
3798 set a new line number for the next instruction.
3799 */
3800 if (e->lineno > c->u->u_lineno) {
3801 c->u->u_lineno = e->lineno;
3802 c->u->u_lineno_set = 0;
3803 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003804 /* Updating the column offset is always harmless. */
3805 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 switch (e->kind) {
3807 case BoolOp_kind:
3808 return compiler_boolop(c, e);
3809 case BinOp_kind:
3810 VISIT(c, expr, e->v.BinOp.left);
3811 VISIT(c, expr, e->v.BinOp.right);
3812 ADDOP(c, binop(c, e->v.BinOp.op));
3813 break;
3814 case UnaryOp_kind:
3815 VISIT(c, expr, e->v.UnaryOp.operand);
3816 ADDOP(c, unaryop(e->v.UnaryOp.op));
3817 break;
3818 case Lambda_kind:
3819 return compiler_lambda(c, e);
3820 case IfExp_kind:
3821 return compiler_ifexp(c, e);
3822 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003823 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003825 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 case GeneratorExp_kind:
3827 return compiler_genexp(c, e);
3828 case ListComp_kind:
3829 return compiler_listcomp(c, e);
3830 case SetComp_kind:
3831 return compiler_setcomp(c, e);
3832 case DictComp_kind:
3833 return compiler_dictcomp(c, e);
3834 case Yield_kind:
3835 if (c->u->u_ste->ste_type != FunctionBlock)
3836 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003837 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3838 return compiler_error(c, "'yield' inside async function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003839 if (e->v.Yield.value) {
3840 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 }
3842 else {
3843 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3844 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003845 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003847 case YieldFrom_kind:
3848 if (c->u->u_ste->ste_type != FunctionBlock)
3849 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003850
3851 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3852 return compiler_error(c, "'yield from' inside async function");
3853
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003854 VISIT(c, expr, e->v.YieldFrom.value);
3855 ADDOP(c, GET_ITER);
3856 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3857 ADDOP(c, YIELD_FROM);
3858 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003859 case Await_kind:
3860 if (c->u->u_ste->ste_type != FunctionBlock)
3861 return compiler_error(c, "'await' outside function");
3862
3863 /* this check won't be triggered while we have AWAIT token */
3864 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
3865 return compiler_error(c, "'await' outside async function");
3866
3867 VISIT(c, expr, e->v.Await.value);
3868 ADDOP(c, GET_AWAITABLE);
3869 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3870 ADDOP(c, YIELD_FROM);
3871 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 case Compare_kind:
3873 return compiler_compare(c, e);
3874 case Call_kind:
3875 return compiler_call(c, e);
3876 case Num_kind:
3877 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3878 break;
3879 case Str_kind:
3880 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3881 break;
3882 case Bytes_kind:
3883 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3884 break;
3885 case Ellipsis_kind:
3886 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3887 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05003888 case NameConstant_kind:
3889 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3890 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 /* The following exprs can be assignment targets. */
3892 case Attribute_kind:
3893 if (e->v.Attribute.ctx != AugStore)
3894 VISIT(c, expr, e->v.Attribute.value);
3895 switch (e->v.Attribute.ctx) {
3896 case AugLoad:
3897 ADDOP(c, DUP_TOP);
3898 /* Fall through to load */
3899 case Load:
3900 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3901 break;
3902 case AugStore:
3903 ADDOP(c, ROT_TWO);
3904 /* Fall through to save */
3905 case Store:
3906 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3907 break;
3908 case Del:
3909 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3910 break;
3911 case Param:
3912 default:
3913 PyErr_SetString(PyExc_SystemError,
3914 "param invalid in attribute expression");
3915 return 0;
3916 }
3917 break;
3918 case Subscript_kind:
3919 switch (e->v.Subscript.ctx) {
3920 case AugLoad:
3921 VISIT(c, expr, e->v.Subscript.value);
3922 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3923 break;
3924 case Load:
3925 VISIT(c, expr, e->v.Subscript.value);
3926 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3927 break;
3928 case AugStore:
3929 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3930 break;
3931 case Store:
3932 VISIT(c, expr, e->v.Subscript.value);
3933 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3934 break;
3935 case Del:
3936 VISIT(c, expr, e->v.Subscript.value);
3937 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3938 break;
3939 case Param:
3940 default:
3941 PyErr_SetString(PyExc_SystemError,
3942 "param invalid in subscript expression");
3943 return 0;
3944 }
3945 break;
3946 case Starred_kind:
3947 switch (e->v.Starred.ctx) {
3948 case Store:
3949 /* In all legitimate cases, the Starred node was already replaced
3950 * by compiler_list/compiler_tuple. XXX: is that okay? */
3951 return compiler_error(c,
3952 "starred assignment target must be in a list or tuple");
3953 default:
3954 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003955 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 }
3957 break;
3958 case Name_kind:
3959 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3960 /* child nodes of List and Tuple will have expr_context set */
3961 case List_kind:
3962 return compiler_list(c, e);
3963 case Tuple_kind:
3964 return compiler_tuple(c, e);
3965 }
3966 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003967}
3968
3969static int
3970compiler_augassign(struct compiler *c, stmt_ty s)
3971{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 expr_ty e = s->v.AugAssign.target;
3973 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 switch (e->kind) {
3978 case Attribute_kind:
3979 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3980 AugLoad, e->lineno, e->col_offset, c->c_arena);
3981 if (auge == NULL)
3982 return 0;
3983 VISIT(c, expr, auge);
3984 VISIT(c, expr, s->v.AugAssign.value);
3985 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3986 auge->v.Attribute.ctx = AugStore;
3987 VISIT(c, expr, auge);
3988 break;
3989 case Subscript_kind:
3990 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3991 AugLoad, e->lineno, e->col_offset, c->c_arena);
3992 if (auge == NULL)
3993 return 0;
3994 VISIT(c, expr, auge);
3995 VISIT(c, expr, s->v.AugAssign.value);
3996 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3997 auge->v.Subscript.ctx = AugStore;
3998 VISIT(c, expr, auge);
3999 break;
4000 case Name_kind:
4001 if (!compiler_nameop(c, e->v.Name.id, Load))
4002 return 0;
4003 VISIT(c, expr, s->v.AugAssign.value);
4004 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4005 return compiler_nameop(c, e->v.Name.id, Store);
4006 default:
4007 PyErr_Format(PyExc_SystemError,
4008 "invalid node type (%d) for augmented assignment",
4009 e->kind);
4010 return 0;
4011 }
4012 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013}
4014
4015static int
4016compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4017{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 struct fblockinfo *f;
4019 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
4020 PyErr_SetString(PyExc_SystemError,
4021 "too many statically nested blocks");
4022 return 0;
4023 }
4024 f = &c->u->u_fblock[c->u->u_nfblocks++];
4025 f->fb_type = t;
4026 f->fb_block = b;
4027 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004028}
4029
4030static void
4031compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 struct compiler_unit *u = c->u;
4034 assert(u->u_nfblocks > 0);
4035 u->u_nfblocks--;
4036 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4037 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004038}
4039
Thomas Wouters89f507f2006-12-13 04:49:30 +00004040static int
4041compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 int i;
4043 struct compiler_unit *u = c->u;
4044 for (i = 0; i < u->u_nfblocks; ++i) {
4045 if (u->u_fblock[i].fb_type == LOOP)
4046 return 1;
4047 }
4048 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004049}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004050/* Raises a SyntaxError and returns 0.
4051 If something goes wrong, a different exception may be raised.
4052*/
4053
4054static int
4055compiler_error(struct compiler *c, const char *errstr)
4056{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004057 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004059
Victor Stinner14e461d2013-08-26 22:28:21 +02004060 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 if (!loc) {
4062 Py_INCREF(Py_None);
4063 loc = Py_None;
4064 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004065 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004066 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 if (!u)
4068 goto exit;
4069 v = Py_BuildValue("(zO)", errstr, u);
4070 if (!v)
4071 goto exit;
4072 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004073 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 Py_DECREF(loc);
4075 Py_XDECREF(u);
4076 Py_XDECREF(v);
4077 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004078}
4079
4080static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081compiler_handle_subscr(struct compiler *c, const char *kind,
4082 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 /* XXX this code is duplicated */
4087 switch (ctx) {
4088 case AugLoad: /* fall through to Load */
4089 case Load: op = BINARY_SUBSCR; break;
4090 case AugStore:/* fall through to Store */
4091 case Store: op = STORE_SUBSCR; break;
4092 case Del: op = DELETE_SUBSCR; break;
4093 case Param:
4094 PyErr_Format(PyExc_SystemError,
4095 "invalid %s kind %d in subscript\n",
4096 kind, ctx);
4097 return 0;
4098 }
4099 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004100 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 }
4102 else if (ctx == AugStore) {
4103 ADDOP(c, ROT_THREE);
4104 }
4105 ADDOP(c, op);
4106 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004107}
4108
4109static int
4110compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 int n = 2;
4113 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 /* only handles the cases where BUILD_SLICE is emitted */
4116 if (s->v.Slice.lower) {
4117 VISIT(c, expr, s->v.Slice.lower);
4118 }
4119 else {
4120 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4121 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 if (s->v.Slice.upper) {
4124 VISIT(c, expr, s->v.Slice.upper);
4125 }
4126 else {
4127 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4128 }
4129
4130 if (s->v.Slice.step) {
4131 n++;
4132 VISIT(c, expr, s->v.Slice.step);
4133 }
4134 ADDOP_I(c, BUILD_SLICE, n);
4135 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004136}
4137
4138static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4140 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 switch (s->kind) {
4143 case Slice_kind:
4144 return compiler_slice(c, s, ctx);
4145 case Index_kind:
4146 VISIT(c, expr, s->v.Index.value);
4147 break;
4148 case ExtSlice_kind:
4149 default:
4150 PyErr_SetString(PyExc_SystemError,
4151 "extended slice invalid in nested slice");
4152 return 0;
4153 }
4154 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004155}
4156
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157static int
4158compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4159{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004160 char * kindname = NULL;
4161 switch (s->kind) {
4162 case Index_kind:
4163 kindname = "index";
4164 if (ctx != AugStore) {
4165 VISIT(c, expr, s->v.Index.value);
4166 }
4167 break;
4168 case Slice_kind:
4169 kindname = "slice";
4170 if (ctx != AugStore) {
4171 if (!compiler_slice(c, s, ctx))
4172 return 0;
4173 }
4174 break;
4175 case ExtSlice_kind:
4176 kindname = "extended slice";
4177 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004178 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 for (i = 0; i < n; i++) {
4180 slice_ty sub = (slice_ty)asdl_seq_GET(
4181 s->v.ExtSlice.dims, i);
4182 if (!compiler_visit_nested_slice(c, sub, ctx))
4183 return 0;
4184 }
4185 ADDOP_I(c, BUILD_TUPLE, n);
4186 }
4187 break;
4188 default:
4189 PyErr_Format(PyExc_SystemError,
4190 "invalid subscript kind %d", s->kind);
4191 return 0;
4192 }
4193 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004194}
4195
Thomas Wouters89f507f2006-12-13 04:49:30 +00004196/* End of the compiler section, beginning of the assembler section */
4197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198/* do depth-first search of basic block graph, starting with block.
4199 post records the block indices in post-order.
4200
4201 XXX must handle implicit jumps from one block to next
4202*/
4203
Thomas Wouters89f507f2006-12-13 04:49:30 +00004204struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 PyObject *a_bytecode; /* string containing bytecode */
4206 int a_offset; /* offset into bytecode */
4207 int a_nblocks; /* number of reachable blocks */
4208 basicblock **a_postorder; /* list of blocks in dfs postorder */
4209 PyObject *a_lnotab; /* string containing lnotab */
4210 int a_lnotab_off; /* offset into lnotab */
4211 int a_lineno; /* last lineno of emitted instruction */
4212 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004213};
4214
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004215static void
4216dfs(struct compiler *c, basicblock *b, struct assembler *a)
4217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 int i;
4219 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 if (b->b_seen)
4222 return;
4223 b->b_seen = 1;
4224 if (b->b_next != NULL)
4225 dfs(c, b->b_next, a);
4226 for (i = 0; i < b->b_iused; i++) {
4227 instr = &b->b_instr[i];
4228 if (instr->i_jrel || instr->i_jabs)
4229 dfs(c, instr->i_target, a);
4230 }
4231 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004232}
4233
Neal Norwitz2744c6c2005-11-13 01:08:38 +00004234static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004235stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
4236{
Larry Hastings3a907972013-11-23 14:49:22 -08004237 int i, target_depth, effect;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004238 struct instr *instr;
4239 if (b->b_seen || b->b_startdepth >= depth)
4240 return maxdepth;
4241 b->b_seen = 1;
4242 b->b_startdepth = depth;
4243 for (i = 0; i < b->b_iused; i++) {
4244 instr = &b->b_instr[i];
Larry Hastings3a907972013-11-23 14:49:22 -08004245 effect = PyCompile_OpcodeStackEffect(instr->i_opcode, instr->i_oparg);
4246 if (effect == PY_INVALID_STACK_EFFECT) {
4247 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
4248 Py_FatalError("PyCompile_OpcodeStackEffect()");
4249 }
4250 depth += effect;
4251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 if (depth > maxdepth)
4253 maxdepth = depth;
4254 assert(depth >= 0); /* invalid code or bug in stackdepth() */
4255 if (instr->i_jrel || instr->i_jabs) {
4256 target_depth = depth;
4257 if (instr->i_opcode == FOR_ITER) {
4258 target_depth = depth-2;
Antoine Pitrou99614052014-05-23 11:46:03 +02004259 }
4260 else if (instr->i_opcode == SETUP_FINALLY ||
4261 instr->i_opcode == SETUP_EXCEPT) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 target_depth = depth+3;
4263 if (target_depth > maxdepth)
4264 maxdepth = target_depth;
4265 }
Antoine Pitrou99614052014-05-23 11:46:03 +02004266 else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||
4267 instr->i_opcode == JUMP_IF_FALSE_OR_POP)
4268 depth = depth - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 maxdepth = stackdepth_walk(c, instr->i_target,
4270 target_depth, maxdepth);
4271 if (instr->i_opcode == JUMP_ABSOLUTE ||
4272 instr->i_opcode == JUMP_FORWARD) {
4273 goto out; /* remaining code is dead */
4274 }
4275 }
4276 }
4277 if (b->b_next)
4278 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004279out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 b->b_seen = 0;
4281 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004282}
4283
4284/* Find the flow path that needs the largest stack. We assume that
4285 * cycles in the flow graph have no net effect on the stack depth.
4286 */
4287static int
4288stackdepth(struct compiler *c)
4289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 basicblock *b, *entryblock;
4291 entryblock = NULL;
4292 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4293 b->b_seen = 0;
4294 b->b_startdepth = INT_MIN;
4295 entryblock = b;
4296 }
4297 if (!entryblock)
4298 return 0;
4299 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004300}
4301
4302static int
4303assemble_init(struct assembler *a, int nblocks, int firstlineno)
4304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 memset(a, 0, sizeof(struct assembler));
4306 a->a_lineno = firstlineno;
4307 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4308 if (!a->a_bytecode)
4309 return 0;
4310 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4311 if (!a->a_lnotab)
4312 return 0;
Christian Heimes724b8282013-12-04 08:42:46 +01004313 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004314 PyErr_NoMemory();
4315 return 0;
4316 }
4317 a->a_postorder = (basicblock **)PyObject_Malloc(
4318 sizeof(basicblock *) * nblocks);
4319 if (!a->a_postorder) {
4320 PyErr_NoMemory();
4321 return 0;
4322 }
4323 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004324}
4325
4326static void
4327assemble_free(struct assembler *a)
4328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 Py_XDECREF(a->a_bytecode);
4330 Py_XDECREF(a->a_lnotab);
4331 if (a->a_postorder)
4332 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004333}
4334
4335/* Return the size of a basic block in bytes. */
4336
4337static int
4338instrsize(struct instr *instr)
4339{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 if (!instr->i_hasarg)
4341 return 1; /* 1 byte for the opcode*/
4342 if (instr->i_oparg > 0xffff)
4343 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
4344 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004345}
4346
4347static int
4348blocksize(basicblock *b)
4349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 int i;
4351 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 for (i = 0; i < b->b_iused; i++)
4354 size += instrsize(&b->b_instr[i]);
4355 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004356}
4357
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004358/* Appends a pair to the end of the line number table, a_lnotab, representing
4359 the instruction's bytecode offset and line number. See
4360 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00004361
Guido van Rossumf68d8e52001-04-14 17:55:09 +00004362static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004363assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004366 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 d_bytecode = a->a_offset - a->a_lineno_off;
4370 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 assert(d_bytecode >= 0);
4373 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 if(d_bytecode == 0 && d_lineno == 0)
4376 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00004377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004378 if (d_bytecode > 255) {
4379 int j, nbytes, ncodes = d_bytecode / 255;
4380 nbytes = a->a_lnotab_off + 2 * ncodes;
4381 len = PyBytes_GET_SIZE(a->a_lnotab);
4382 if (nbytes >= len) {
4383 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
4384 len = nbytes;
4385 else if (len <= INT_MAX / 2)
4386 len *= 2;
4387 else {
4388 PyErr_NoMemory();
4389 return 0;
4390 }
4391 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4392 return 0;
4393 }
4394 lnotab = (unsigned char *)
4395 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4396 for (j = 0; j < ncodes; j++) {
4397 *lnotab++ = 255;
4398 *lnotab++ = 0;
4399 }
4400 d_bytecode -= ncodes * 255;
4401 a->a_lnotab_off += ncodes * 2;
4402 }
4403 assert(d_bytecode <= 255);
4404 if (d_lineno > 255) {
4405 int j, nbytes, ncodes = d_lineno / 255;
4406 nbytes = a->a_lnotab_off + 2 * ncodes;
4407 len = PyBytes_GET_SIZE(a->a_lnotab);
4408 if (nbytes >= len) {
4409 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
4410 len = nbytes;
4411 else if (len <= INT_MAX / 2)
4412 len *= 2;
4413 else {
4414 PyErr_NoMemory();
4415 return 0;
4416 }
4417 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4418 return 0;
4419 }
4420 lnotab = (unsigned char *)
4421 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4422 *lnotab++ = d_bytecode;
4423 *lnotab++ = 255;
4424 d_bytecode = 0;
4425 for (j = 1; j < ncodes; j++) {
4426 *lnotab++ = 0;
4427 *lnotab++ = 255;
4428 }
4429 d_lineno -= ncodes * 255;
4430 a->a_lnotab_off += ncodes * 2;
4431 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 len = PyBytes_GET_SIZE(a->a_lnotab);
4434 if (a->a_lnotab_off + 2 >= len) {
4435 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
4436 return 0;
4437 }
4438 lnotab = (unsigned char *)
4439 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00004440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004441 a->a_lnotab_off += 2;
4442 if (d_bytecode) {
4443 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004444 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 }
4446 else { /* First line of a block; def stmt, etc. */
4447 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004448 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 }
4450 a->a_lineno = i->i_lineno;
4451 a->a_lineno_off = a->a_offset;
4452 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004453}
4454
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004455/* assemble_emit()
4456 Extend the bytecode with a new instruction.
4457 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00004458*/
4459
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004460static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004461assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004462{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 int size, arg = 0, ext = 0;
4464 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4465 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 size = instrsize(i);
4468 if (i->i_hasarg) {
4469 arg = i->i_oparg;
4470 ext = arg >> 16;
4471 }
4472 if (i->i_lineno && !assemble_lnotab(a, i))
4473 return 0;
4474 if (a->a_offset + size >= len) {
4475 if (len > PY_SSIZE_T_MAX / 2)
4476 return 0;
4477 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4478 return 0;
4479 }
4480 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4481 a->a_offset += size;
4482 if (size == 6) {
4483 assert(i->i_hasarg);
4484 *code++ = (char)EXTENDED_ARG;
4485 *code++ = ext & 0xff;
4486 *code++ = ext >> 8;
4487 arg &= 0xffff;
4488 }
4489 *code++ = i->i_opcode;
4490 if (i->i_hasarg) {
4491 assert(size == 3 || size == 6);
4492 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004493 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 }
4495 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004496}
4497
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004498static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004499assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004501 basicblock *b;
4502 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
4503 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00004504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 /* Compute the size of each block and fixup jump args.
4506 Replace block pointer with position in bytecode. */
4507 do {
4508 totsize = 0;
4509 for (i = a->a_nblocks - 1; i >= 0; i--) {
4510 b = a->a_postorder[i];
4511 bsize = blocksize(b);
4512 b->b_offset = totsize;
4513 totsize += bsize;
4514 }
4515 last_extended_arg_count = extended_arg_count;
4516 extended_arg_count = 0;
4517 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4518 bsize = b->b_offset;
4519 for (i = 0; i < b->b_iused; i++) {
4520 struct instr *instr = &b->b_instr[i];
4521 /* Relative jumps are computed relative to
4522 the instruction pointer after fetching
4523 the jump instruction.
4524 */
4525 bsize += instrsize(instr);
4526 if (instr->i_jabs)
4527 instr->i_oparg = instr->i_target->b_offset;
4528 else if (instr->i_jrel) {
4529 int delta = instr->i_target->b_offset - bsize;
4530 instr->i_oparg = delta;
4531 }
4532 else
4533 continue;
4534 if (instr->i_oparg > 0xffff)
4535 extended_arg_count++;
4536 }
4537 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 /* XXX: This is an awful hack that could hurt performance, but
4540 on the bright side it should work until we come up
4541 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 The issue is that in the first loop blocksize() is called
4544 which calls instrsize() which requires i_oparg be set
4545 appropriately. There is a bootstrap problem because
4546 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 So we loop until we stop seeing new EXTENDED_ARGs.
4549 The only EXTENDED_ARGs that could be popping up are
4550 ones in jump instructions. So this should converge
4551 fairly quickly.
4552 */
4553 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004554}
4555
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004556static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01004557dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 PyObject *tuple, *k, *v;
4560 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 tuple = PyTuple_New(size);
4563 if (tuple == NULL)
4564 return NULL;
4565 while (PyDict_Next(dict, &pos, &k, &v)) {
4566 i = PyLong_AS_LONG(v);
4567 /* The keys of the dictionary are tuples. (see compiler_add_o)
4568 The object we want is always first, though. */
4569 k = PyTuple_GET_ITEM(k, 0);
4570 Py_INCREF(k);
4571 assert((i - offset) < size);
4572 assert((i - offset) >= 0);
4573 PyTuple_SET_ITEM(tuple, i - offset, k);
4574 }
4575 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004576}
4577
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004578static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004579compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004582 int flags = 0;
4583 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04004585 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 if (ste->ste_nested)
4587 flags |= CO_NESTED;
4588 if (ste->ste_generator)
4589 flags |= CO_GENERATOR;
4590 if (ste->ste_varargs)
4591 flags |= CO_VARARGS;
4592 if (ste->ste_varkeywords)
4593 flags |= CO_VARKEYWORDS;
4594 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 /* (Only) inherit compilerflags in PyCF_MASK */
4597 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 n = PyDict_Size(c->u->u_freevars);
4600 if (n < 0)
4601 return -1;
4602 if (n == 0) {
4603 n = PyDict_Size(c->u->u_cellvars);
4604 if (n < 0)
Victor Stinnerad9a0662013-11-19 22:23:20 +01004605 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 if (n == 0) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004607 flags |= CO_NOFREE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 }
4609 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004612}
4613
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004614static PyCodeObject *
4615makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 PyObject *tmp;
4618 PyCodeObject *co = NULL;
4619 PyObject *consts = NULL;
4620 PyObject *names = NULL;
4621 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 PyObject *name = NULL;
4623 PyObject *freevars = NULL;
4624 PyObject *cellvars = NULL;
4625 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004626 Py_ssize_t nlocals;
4627 int nlocals_int;
4628 int flags;
Victor Stinnerf8e32212013-11-19 23:56:34 +01004629 int argcount, kwonlyargcount;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 tmp = dict_keys_inorder(c->u->u_consts, 0);
4632 if (!tmp)
4633 goto error;
4634 consts = PySequence_List(tmp); /* optimize_code requires a list */
4635 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 names = dict_keys_inorder(c->u->u_names, 0);
4638 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4639 if (!consts || !names || !varnames)
4640 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4643 if (!cellvars)
4644 goto error;
4645 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4646 if (!freevars)
4647 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 nlocals = PyDict_Size(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01004650 assert(nlocals < INT_MAX);
4651 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
4652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 flags = compute_code_flags(c);
4654 if (flags < 0)
4655 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4658 if (!bytecode)
4659 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4662 if (!tmp)
4663 goto error;
4664 Py_DECREF(consts);
4665 consts = tmp;
4666
Victor Stinnerf8e32212013-11-19 23:56:34 +01004667 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
4668 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
4669 co = PyCode_New(argcount, kwonlyargcount,
Victor Stinnerad9a0662013-11-19 22:23:20 +01004670 nlocals_int, stackdepth(c), flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 bytecode, consts, names, varnames,
4672 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02004673 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 c->u->u_firstlineno,
4675 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004676 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 Py_XDECREF(consts);
4678 Py_XDECREF(names);
4679 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 Py_XDECREF(name);
4681 Py_XDECREF(freevars);
4682 Py_XDECREF(cellvars);
4683 Py_XDECREF(bytecode);
4684 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004685}
4686
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004687
4688/* For debugging purposes only */
4689#if 0
4690static void
4691dump_instr(const struct instr *i)
4692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 const char *jrel = i->i_jrel ? "jrel " : "";
4694 const char *jabs = i->i_jabs ? "jabs " : "";
4695 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 *arg = '\0';
4698 if (i->i_hasarg)
4699 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4702 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004703}
4704
4705static void
4706dump_basicblock(const basicblock *b)
4707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 const char *seen = b->b_seen ? "seen " : "";
4709 const char *b_return = b->b_return ? "return " : "";
4710 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4711 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4712 if (b->b_instr) {
4713 int i;
4714 for (i = 0; i < b->b_iused; i++) {
4715 fprintf(stderr, " [%02d] ", i);
4716 dump_instr(b->b_instr + i);
4717 }
4718 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004719}
4720#endif
4721
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004722static PyCodeObject *
4723assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004725 basicblock *b, *entryblock;
4726 struct assembler a;
4727 int i, j, nblocks;
4728 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 /* Make sure every block that falls off the end returns None.
4731 XXX NEXT_BLOCK() isn't quite right, because if the last
4732 block ends with a jump or return b_next shouldn't set.
4733 */
4734 if (!c->u->u_curblock->b_return) {
4735 NEXT_BLOCK(c);
4736 if (addNone)
4737 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4738 ADDOP(c, RETURN_VALUE);
4739 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 nblocks = 0;
4742 entryblock = NULL;
4743 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4744 nblocks++;
4745 entryblock = b;
4746 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004748 /* Set firstlineno if it wasn't explicitly set. */
4749 if (!c->u->u_firstlineno) {
4750 if (entryblock && entryblock->b_instr)
4751 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4752 else
4753 c->u->u_firstlineno = 1;
4754 }
4755 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4756 goto error;
4757 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 /* Can't modify the bytecode after computing jump offsets. */
4760 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 /* Emit code in reverse postorder from dfs. */
4763 for (i = a.a_nblocks - 1; i >= 0; i--) {
4764 b = a.a_postorder[i];
4765 for (j = 0; j < b->b_iused; j++)
4766 if (!assemble_emit(&a, &b->b_instr[j]))
4767 goto error;
4768 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4771 goto error;
4772 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4773 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004776 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 assemble_free(&a);
4778 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004779}
Georg Brandl8334fd92010-12-04 10:26:46 +00004780
4781#undef PyAST_Compile
4782PyAPI_FUNC(PyCodeObject *)
4783PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4784 PyArena *arena)
4785{
4786 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4787}
4788