blob: ce510aa731223058e6b4b3f498ffcd8e6c46ef2b [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);
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100396 k = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 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++;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100459 tuple = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 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) {
Martin Panter7462b6492015-11-02 03:37:02 +0000551 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500552 _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 }
Victor Stinner3cdd5fb2016-01-22 12:33:12 +0100562 tuple = _PyCode_ConstantKey(name);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500563 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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 case DELETE_SUBSCR:
907 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 case BINARY_LSHIFT:
910 case BINARY_RSHIFT:
911 case BINARY_AND:
912 case BINARY_XOR:
913 case BINARY_OR:
914 return -1;
915 case INPLACE_POWER:
916 return -1;
917 case GET_ITER:
918 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 case PRINT_EXPR:
921 return -1;
922 case LOAD_BUILD_CLASS:
923 return 1;
924 case INPLACE_LSHIFT:
925 case INPLACE_RSHIFT:
926 case INPLACE_AND:
927 case INPLACE_XOR:
928 case INPLACE_OR:
929 return -1;
930 case BREAK_LOOP:
931 return 0;
932 case SETUP_WITH:
933 return 7;
Yury Selivanov75445082015-05-11 22:57:16 -0400934 case WITH_CLEANUP_START:
935 return 1;
936 case WITH_CLEANUP_FINISH:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 return -1; /* XXX Sometimes more */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000938 case RETURN_VALUE:
939 return -1;
940 case IMPORT_STAR:
941 return -1;
942 case YIELD_VALUE:
943 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500944 case YIELD_FROM:
945 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 case POP_BLOCK:
947 return 0;
948 case POP_EXCEPT:
949 return 0; /* -3 except if bad bytecode */
950 case END_FINALLY:
951 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 case STORE_NAME:
954 return -1;
955 case DELETE_NAME:
956 return 0;
957 case UNPACK_SEQUENCE:
958 return oparg-1;
959 case UNPACK_EX:
960 return (oparg&0xFF) + (oparg>>8);
961 case FOR_ITER:
962 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 case STORE_ATTR:
965 return -2;
966 case DELETE_ATTR:
967 return -1;
968 case STORE_GLOBAL:
969 return -1;
970 case DELETE_GLOBAL:
971 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 case LOAD_CONST:
973 return 1;
974 case LOAD_NAME:
975 return 1;
976 case BUILD_TUPLE:
977 case BUILD_LIST:
978 case BUILD_SET:
979 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400980 case BUILD_LIST_UNPACK:
981 case BUILD_TUPLE_UNPACK:
982 case BUILD_SET_UNPACK:
983 case BUILD_MAP_UNPACK:
984 return 1 - oparg;
985 case BUILD_MAP_UNPACK_WITH_CALL:
986 return 1 - (oparg & 0xFF);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -0700988 return 1 - 2*oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 case LOAD_ATTR:
990 return 0;
991 case COMPARE_OP:
992 return -1;
993 case IMPORT_NAME:
994 return -1;
995 case IMPORT_FROM:
996 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 case JUMP_FORWARD:
999 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
1000 case JUMP_IF_FALSE_OR_POP: /* "" */
1001 case JUMP_ABSOLUTE:
1002 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 case POP_JUMP_IF_FALSE:
1005 case POP_JUMP_IF_TRUE:
1006 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 case LOAD_GLOBAL:
1009 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 case CONTINUE_LOOP:
1012 return 0;
1013 case SETUP_LOOP:
1014 return 0;
1015 case SETUP_EXCEPT:
1016 case SETUP_FINALLY:
1017 return 6; /* can push 3 values for the new exception
1018 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 case LOAD_FAST:
1021 return 1;
1022 case STORE_FAST:
1023 return -1;
1024 case DELETE_FAST:
1025 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case RAISE_VARARGS:
1028 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +00001029#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 case CALL_FUNCTION:
1031 return -NARGS(oparg);
1032 case CALL_FUNCTION_VAR:
1033 case CALL_FUNCTION_KW:
1034 return -NARGS(oparg)-1;
1035 case CALL_FUNCTION_VAR_KW:
1036 return -NARGS(oparg)-2;
1037 case MAKE_FUNCTION:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001038 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 case MAKE_CLOSURE:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001040 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001041#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 case BUILD_SLICE:
1043 if (oparg == 3)
1044 return -2;
1045 else
1046 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 case LOAD_CLOSURE:
1049 return 1;
1050 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001051 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 return 1;
1053 case STORE_DEREF:
1054 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001055 case DELETE_DEREF:
1056 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001057 case GET_AWAITABLE:
1058 return 0;
1059 case SETUP_ASYNC_WITH:
1060 return 6;
1061 case BEFORE_ASYNC_WITH:
1062 return 1;
1063 case GET_AITER:
1064 return 0;
1065 case GET_ANEXT:
1066 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001067 case GET_YIELD_FROM_ITER:
1068 return 0;
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;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103
Victor Stinner3cdd5fb2016-01-22 12:33:12 +01001104 t = _PyCode_ConstantKey(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 if (t == NULL)
1106 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 v = PyDict_GetItem(dict, t);
1109 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001110 if (PyErr_Occurred()) {
1111 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 arg = PyDict_Size(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001115 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 if (!v) {
1117 Py_DECREF(t);
1118 return -1;
1119 }
1120 if (PyDict_SetItem(dict, t, v) < 0) {
1121 Py_DECREF(t);
1122 Py_DECREF(v);
1123 return -1;
1124 }
1125 Py_DECREF(v);
1126 }
1127 else
1128 arg = PyLong_AsLong(v);
1129 Py_DECREF(t);
1130 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131}
1132
1133static int
1134compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001136{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001137 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001139 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140 return compiler_addop_i(c, opcode, arg);
1141}
1142
1143static int
1144compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001147 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001148 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1149 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001150 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151 arg = compiler_add_o(c, dict, mangled);
1152 Py_DECREF(mangled);
1153 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001154 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001155 return compiler_addop_i(c, opcode, arg);
1156}
1157
1158/* Add an opcode with an integer argument.
1159 Returns 0 on failure, 1 on success.
1160*/
1161
1162static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001163compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 struct instr *i;
1166 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001167
1168 /* Integer arguments are limit to 16-bit. There is an extension for 32-bit
1169 integer arguments. */
Victor Stinnerf8e32212013-11-19 23:56:34 +01001170 assert((-2147483647-1) <= oparg);
1171 assert(oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 off = compiler_next_instr(c, c->u->u_curblock);
1174 if (off < 0)
1175 return 0;
1176 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001177 i->i_opcode = opcode;
1178 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 i->i_hasarg = 1;
1180 compiler_set_lineno(c, off);
1181 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001182}
1183
1184static int
1185compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 struct instr *i;
1188 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 assert(b != NULL);
1191 off = compiler_next_instr(c, c->u->u_curblock);
1192 if (off < 0)
1193 return 0;
1194 i = &c->u->u_curblock->b_instr[off];
1195 i->i_opcode = opcode;
1196 i->i_target = b;
1197 i->i_hasarg = 1;
1198 if (absolute)
1199 i->i_jabs = 1;
1200 else
1201 i->i_jrel = 1;
1202 compiler_set_lineno(c, off);
1203 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001204}
1205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206/* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd
1207 like to find better names.) NEW_BLOCK() creates a new block and sets
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001208 it as the current block. NEXT_BLOCK() also creates an implicit jump
1209 from the current block to the new block.
1210*/
1211
Thomas Wouters89f507f2006-12-13 04:49:30 +00001212/* The returns inside these macros make it impossible to decref objects
1213 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001214*/
1215
1216
1217#define NEW_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (compiler_use_new_block((C)) == NULL) \
1219 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001220}
1221
1222#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (compiler_next_block((C)) == NULL) \
1224 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001225}
1226
1227#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 if (!compiler_addop((C), (OP))) \
1229 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001230}
1231
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001232#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (!compiler_addop((C), (OP))) { \
1234 compiler_exit_scope(c); \
1235 return 0; \
1236 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001237}
1238
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001239#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1241 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001242}
1243
1244#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1246 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001247}
1248
1249#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (!compiler_addop_i((C), (OP), (O))) \
1251 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001252}
1253
1254#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (!compiler_addop_j((C), (OP), (O), 1)) \
1256 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001257}
1258
1259#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (!compiler_addop_j((C), (OP), (O), 0)) \
1261 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001262}
1263
1264/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1265 the ASDL name to synthesize the name of the C type and the visit function.
1266*/
1267
1268#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 if (!compiler_visit_ ## TYPE((C), (V))) \
1270 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001271}
1272
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001273#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (!compiler_visit_ ## TYPE((C), (V))) { \
1275 compiler_exit_scope(c); \
1276 return 0; \
1277 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001278}
1279
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001280#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (!compiler_visit_slice((C), (V), (CTX))) \
1282 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001283}
1284
1285#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 int _i; \
1287 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1288 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1289 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1290 if (!compiler_visit_ ## TYPE((C), elt)) \
1291 return 0; \
1292 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001293}
1294
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001295#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 int _i; \
1297 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1298 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1299 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1300 if (!compiler_visit_ ## TYPE((C), elt)) { \
1301 compiler_exit_scope(c); \
1302 return 0; \
1303 } \
1304 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001305}
1306
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001307static int
1308compiler_isdocstring(stmt_ty s)
1309{
1310 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001311 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001312 return s->v.Expr.value->kind == Str_kind;
1313}
1314
1315/* Compile a sequence of statements, checking for a docstring. */
1316
1317static int
1318compiler_body(struct compiler *c, asdl_seq *stmts)
1319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 int i = 0;
1321 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 if (!asdl_seq_LEN(stmts))
1324 return 1;
1325 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001326 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* don't generate docstrings if -OO */
1328 i = 1;
1329 VISIT(c, expr, st->v.Expr.value);
1330 if (!compiler_nameop(c, __doc__, Store))
1331 return 0;
1332 }
1333 for (; i < asdl_seq_LEN(stmts); i++)
1334 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1335 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001336}
1337
1338static PyCodeObject *
1339compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 PyCodeObject *co;
1342 int addNone = 1;
1343 static PyObject *module;
1344 if (!module) {
1345 module = PyUnicode_InternFromString("<module>");
1346 if (!module)
1347 return NULL;
1348 }
1349 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001350 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 return NULL;
1352 switch (mod->kind) {
1353 case Module_kind:
1354 if (!compiler_body(c, mod->v.Module.body)) {
1355 compiler_exit_scope(c);
1356 return 0;
1357 }
1358 break;
1359 case Interactive_kind:
1360 c->c_interactive = 1;
1361 VISIT_SEQ_IN_SCOPE(c, stmt,
1362 mod->v.Interactive.body);
1363 break;
1364 case Expression_kind:
1365 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1366 addNone = 0;
1367 break;
1368 case Suite_kind:
1369 PyErr_SetString(PyExc_SystemError,
1370 "suite should not be possible");
1371 return 0;
1372 default:
1373 PyErr_Format(PyExc_SystemError,
1374 "module kind %d should not be possible",
1375 mod->kind);
1376 return 0;
1377 }
1378 co = assemble(c, addNone);
1379 compiler_exit_scope(c);
1380 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001381}
1382
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001383/* The test for LOCAL must come before the test for FREE in order to
1384 handle classes where name is both local and free. The local var is
1385 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001386*/
1387
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001388static int
1389get_ref_type(struct compiler *c, PyObject *name)
1390{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001391 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001392 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1393 !PyUnicode_CompareWithASCIIString(name, "__class__"))
1394 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001395 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (scope == 0) {
1397 char buf[350];
1398 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001399 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001401 PyUnicode_AsUTF8(name),
1402 PyUnicode_AsUTF8(c->u->u_name),
1403 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1404 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1405 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1406 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 );
1408 Py_FatalError(buf);
1409 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001412}
1413
1414static int
1415compiler_lookup_arg(PyObject *dict, PyObject *name)
1416{
1417 PyObject *k, *v;
Victor Stinner3cdd5fb2016-01-22 12:33:12 +01001418 k = _PyCode_ConstantKey(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001419 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001420 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001421 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001422 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001424 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001425 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001426}
1427
1428static int
Victor Stinnerad9a0662013-11-19 22:23:20 +01001429compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001431 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001432 if (qualname == NULL)
1433 qualname = co->co_name;
1434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 if (free == 0) {
1436 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001437 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 ADDOP_I(c, MAKE_FUNCTION, args);
1439 return 1;
1440 }
1441 for (i = 0; i < free; ++i) {
1442 /* Bypass com_addop_varname because it will generate
1443 LOAD_DEREF but LOAD_CLOSURE is needed.
1444 */
1445 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1446 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 /* Special case: If a class contains a method with a
1449 free variable that has the same name as a method,
1450 the name will be considered free *and* local in the
1451 class. It should be handled by the closure, as
1452 well as by the normal name loookup logic.
1453 */
1454 reftype = get_ref_type(c, name);
1455 if (reftype == CELL)
1456 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1457 else /* (reftype == FREE) */
1458 arg = compiler_lookup_arg(c->u->u_freevars, name);
1459 if (arg == -1) {
1460 fprintf(stderr,
1461 "lookup %s in %s %d %d\n"
1462 "freevars of %s: %s\n",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001463 PyUnicode_AsUTF8(PyObject_Repr(name)),
1464 PyUnicode_AsUTF8(c->u->u_name),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 reftype, arg,
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001466 PyUnicode_AsUTF8(co->co_name),
1467 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001468 Py_FatalError("compiler_make_closure()");
1469 }
1470 ADDOP_I(c, LOAD_CLOSURE, arg);
1471 }
1472 ADDOP_I(c, BUILD_TUPLE, free);
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_CLOSURE, args);
1476 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001477}
1478
1479static int
1480compiler_decorators(struct compiler *c, asdl_seq* decos)
1481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 if (!decos)
1485 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1488 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1489 }
1490 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001491}
1492
1493static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001494compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001496{
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001497 /* Return the number of defaults + 1.
1498 Returns 0 on error.
1499 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 int i, default_count = 0;
1501 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1502 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1503 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1504 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001505 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1506 if (!mangled)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001507 return 0;
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001508 ADDOP_O(c, LOAD_CONST, mangled, consts);
1509 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (!compiler_visit_expr(c, default_)) {
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001511 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 }
1513 default_count++;
1514 }
1515 }
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001516 return default_count + 1;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001517}
1518
1519static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001520compiler_visit_argannotation(struct compiler *c, identifier id,
1521 expr_ty annotation, PyObject *names)
1522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001524 PyObject *mangled;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 VISIT(c, expr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01001526 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001527 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001528 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001529 if (PyList_Append(names, mangled) < 0) {
1530 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001531 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001532 }
1533 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001535 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001536}
1537
1538static int
1539compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1540 PyObject *names)
1541{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001542 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 for (i = 0; i < asdl_seq_LEN(args); i++) {
1544 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001545 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 c,
1547 arg->arg,
1548 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001549 names))
1550 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001552 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001553}
1554
1555static int
1556compiler_visit_annotations(struct compiler *c, arguments_ty args,
1557 expr_ty returns)
1558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 /* Push arg annotations and a list of the argument names. Return the #
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001560 of items pushed + 1. The expressions are evaluated out-of-order wrt the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001562
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001563 More than 2^16-1 annotations is a SyntaxError. Returns 0 on error.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 */
1565 static identifier return_str;
1566 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001567 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 names = PyList_New(0);
1569 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001570 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001571
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001572 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001574 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001575 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001576 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001578 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001580 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001581 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001582 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 if (!return_str) {
1586 return_str = PyUnicode_InternFromString("return");
1587 if (!return_str)
1588 goto error;
1589 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001590 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 goto error;
1592 }
1593
1594 len = PyList_GET_SIZE(names);
1595 if (len > 65534) {
1596 /* len must fit in 16 bits, and len is incremented below */
1597 PyErr_SetString(PyExc_SyntaxError,
1598 "too many annotations");
1599 goto error;
1600 }
1601 if (len) {
1602 /* convert names to a tuple and place on stack */
1603 PyObject *elt;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001604 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 PyObject *s = PyTuple_New(len);
1606 if (!s)
1607 goto error;
1608 for (i = 0; i < len; i++) {
1609 elt = PyList_GET_ITEM(names, i);
1610 Py_INCREF(elt);
1611 PyTuple_SET_ITEM(s, i, elt);
1612 }
1613 ADDOP_O(c, LOAD_CONST, s, consts);
1614 Py_DECREF(s);
1615 len++; /* include the just-pushed tuple */
1616 }
1617 Py_DECREF(names);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001618
1619 /* We just checked that len <= 65535, see above */
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001620 return Py_SAFE_DOWNCAST(len + 1, Py_ssize_t, int);
Neal Norwitzc1505362006-12-28 06:47:50 +00001621
1622error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001624 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00001625}
1626
1627static int
Yury Selivanov75445082015-05-11 22:57:16 -04001628compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001631 PyObject *qualname, *first_const = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001632 arguments_ty args;
1633 expr_ty returns;
1634 identifier name;
1635 asdl_seq* decos;
1636 asdl_seq *body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 stmt_ty st;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001638 Py_ssize_t i, n, arglength;
1639 int docstring, kw_default_count = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 int num_annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001641 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001642
Yury Selivanov75445082015-05-11 22:57:16 -04001643
1644 if (is_async) {
1645 assert(s->kind == AsyncFunctionDef_kind);
1646
1647 args = s->v.AsyncFunctionDef.args;
1648 returns = s->v.AsyncFunctionDef.returns;
1649 decos = s->v.AsyncFunctionDef.decorator_list;
1650 name = s->v.AsyncFunctionDef.name;
1651 body = s->v.AsyncFunctionDef.body;
1652
1653 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1654 } else {
1655 assert(s->kind == FunctionDef_kind);
1656
1657 args = s->v.FunctionDef.args;
1658 returns = s->v.FunctionDef.returns;
1659 decos = s->v.FunctionDef.decorator_list;
1660 name = s->v.FunctionDef.name;
1661 body = s->v.FunctionDef.body;
1662
1663 scope_type = COMPILER_SCOPE_FUNCTION;
1664 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 if (!compiler_decorators(c, decos))
1667 return 0;
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05001668 if (args->defaults)
1669 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 if (args->kwonlyargs) {
1671 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1672 args->kw_defaults);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001673 if (res == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 return 0;
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001675 kw_default_count = res - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 num_annotations = compiler_visit_annotations(c, args, returns);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001678 if (num_annotations == 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 return 0;
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001680 num_annotations--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001682
Yury Selivanov75445082015-05-11 22:57:16 -04001683 if (!compiler_enter_scope(c, name,
1684 scope_type, (void *)s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 s->lineno))
1686 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001687
Yury Selivanov75445082015-05-11 22:57:16 -04001688 st = (stmt_ty)asdl_seq_GET(body, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 docstring = compiler_isdocstring(st);
Georg Brandl8334fd92010-12-04 10:26:46 +00001690 if (docstring && c->c_optimize < 2)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 first_const = st->v.Expr.value->v.Str.s;
1692 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1693 compiler_exit_scope(c);
1694 return 0;
1695 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 c->u->u_argcount = asdl_seq_LEN(args->args);
1698 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Yury Selivanov75445082015-05-11 22:57:16 -04001699 n = asdl_seq_LEN(body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 /* if there was a docstring, we need to skip the first statement */
1701 for (i = docstring; i < n; i++) {
Yury Selivanov75445082015-05-11 22:57:16 -04001702 st = (stmt_ty)asdl_seq_GET(body, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 VISIT_IN_SCOPE(c, stmt, st);
1704 }
1705 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001706 qualname = c->u->u_qualname;
1707 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001709 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001710 Py_XDECREF(qualname);
1711 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001713 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 arglength = asdl_seq_LEN(args->defaults);
1716 arglength |= kw_default_count << 8;
1717 arglength |= num_annotations << 16;
Yury Selivanovb7666a32015-07-22 14:48:57 +03001718 if (is_async)
1719 co->co_flags |= CO_COROUTINE;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001720 compiler_make_closure(c, co, arglength, qualname);
1721 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 /* decorators */
1725 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1726 ADDOP_I(c, CALL_FUNCTION, 1);
1727 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001728
Yury Selivanov75445082015-05-11 22:57:16 -04001729 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001730}
1731
1732static int
1733compiler_class(struct compiler *c, stmt_ty s)
1734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 PyCodeObject *co;
1736 PyObject *str;
1737 int i;
1738 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 if (!compiler_decorators(c, decos))
1741 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* ultimately generate code for:
1744 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1745 where:
1746 <func> is a function/closure created from the class body;
1747 it has a single argument (__locals__) where the dict
1748 (or MutableSequence) representing the locals is passed
1749 <name> is the class name
1750 <bases> is the positional arguments and *varargs argument
1751 <keywords> is the keyword arguments and **kwds argument
1752 This borrows from compiler_call.
1753 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001756 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1757 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 return 0;
1759 /* this block represents what we do in the new scope */
1760 {
1761 /* use the class name for name mangling */
1762 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001763 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 /* load (global) __name__ ... */
1765 str = PyUnicode_InternFromString("__name__");
1766 if (!str || !compiler_nameop(c, str, Load)) {
1767 Py_XDECREF(str);
1768 compiler_exit_scope(c);
1769 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001770 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 Py_DECREF(str);
1772 /* ... and store it as __module__ */
1773 str = PyUnicode_InternFromString("__module__");
1774 if (!str || !compiler_nameop(c, str, Store)) {
1775 Py_XDECREF(str);
1776 compiler_exit_scope(c);
1777 return 0;
1778 }
1779 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001780 assert(c->u->u_qualname);
1781 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001782 str = PyUnicode_InternFromString("__qualname__");
1783 if (!str || !compiler_nameop(c, str, Store)) {
1784 Py_XDECREF(str);
1785 compiler_exit_scope(c);
1786 return 0;
1787 }
1788 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 /* compile the body proper */
1790 if (!compiler_body(c, s->v.ClassDef.body)) {
1791 compiler_exit_scope(c);
1792 return 0;
1793 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001794 if (c->u->u_ste->ste_needs_class_closure) {
1795 /* return the (empty) __class__ cell */
1796 str = PyUnicode_InternFromString("__class__");
1797 if (str == NULL) {
1798 compiler_exit_scope(c);
1799 return 0;
1800 }
1801 i = compiler_lookup_arg(c->u->u_cellvars, str);
1802 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01001803 if (i < 0) {
1804 compiler_exit_scope(c);
1805 return 0;
1806 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001807 assert(i == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001808 /* Return the cell where to store __class__ */
1809 ADDOP_I(c, LOAD_CLOSURE, i);
1810 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001811 else {
1812 assert(PyDict_Size(c->u->u_cellvars) == 0);
1813 /* This happens when nobody references the cell. Return None. */
1814 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1815 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1817 /* create the code object */
1818 co = assemble(c, 1);
1819 }
1820 /* leave the new scope */
1821 compiler_exit_scope(c);
1822 if (co == NULL)
1823 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 /* 2. load the 'build_class' function */
1826 ADDOP(c, LOAD_BUILD_CLASS);
1827
1828 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001829 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 Py_DECREF(co);
1831
1832 /* 4. load class name */
1833 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1834
1835 /* 5. generate the rest of the code for the call */
1836 if (!compiler_call_helper(c, 2,
1837 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001838 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 return 0;
1840
1841 /* 6. apply decorators */
1842 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1843 ADDOP_I(c, CALL_FUNCTION, 1);
1844 }
1845
1846 /* 7. store into <name> */
1847 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1848 return 0;
1849 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850}
1851
1852static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001853compiler_ifexp(struct compiler *c, expr_ty e)
1854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 basicblock *end, *next;
1856
1857 assert(e->kind == IfExp_kind);
1858 end = compiler_new_block(c);
1859 if (end == NULL)
1860 return 0;
1861 next = compiler_new_block(c);
1862 if (next == NULL)
1863 return 0;
1864 VISIT(c, expr, e->v.IfExp.test);
1865 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1866 VISIT(c, expr, e->v.IfExp.body);
1867 ADDOP_JREL(c, JUMP_FORWARD, end);
1868 compiler_use_next_block(c, next);
1869 VISIT(c, expr, e->v.IfExp.orelse);
1870 compiler_use_next_block(c, end);
1871 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001872}
1873
1874static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875compiler_lambda(struct compiler *c, expr_ty e)
1876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001878 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 static identifier name;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001880 int kw_default_count = 0;
1881 Py_ssize_t arglength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 arguments_ty args = e->v.Lambda.args;
1883 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 if (!name) {
1886 name = PyUnicode_InternFromString("<lambda>");
1887 if (!name)
1888 return 0;
1889 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001890
Benjamin Peterson419d9a82013-02-10 09:48:22 -05001891 if (args->defaults)
1892 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 if (args->kwonlyargs) {
1894 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1895 args->kw_defaults);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03001896 if (res == 0) return 0;
1897 kw_default_count = res - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001899 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001900 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 /* Make None the first constant, so the lambda can't have a
1904 docstring. */
1905 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1906 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908 c->u->u_argcount = asdl_seq_LEN(args->args);
1909 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1910 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1911 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001912 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 }
1914 else {
1915 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001916 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001918 qualname = c->u->u_qualname;
1919 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001921 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 arglength = asdl_seq_LEN(args->defaults);
1925 arglength |= kw_default_count << 8;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001926 compiler_make_closure(c, co, arglength, qualname);
1927 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 Py_DECREF(co);
1929
1930 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931}
1932
1933static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001934compiler_if(struct compiler *c, stmt_ty s)
1935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 basicblock *end, *next;
1937 int constant;
1938 assert(s->kind == If_kind);
1939 end = compiler_new_block(c);
1940 if (end == NULL)
1941 return 0;
1942
Georg Brandl8334fd92010-12-04 10:26:46 +00001943 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 /* constant = 0: "if 0"
1945 * constant = 1: "if 1", "if 2", ...
1946 * constant = -1: rest */
1947 if (constant == 0) {
1948 if (s->v.If.orelse)
1949 VISIT_SEQ(c, stmt, s->v.If.orelse);
1950 } else if (constant == 1) {
1951 VISIT_SEQ(c, stmt, s->v.If.body);
1952 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001953 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 next = compiler_new_block(c);
1955 if (next == NULL)
1956 return 0;
1957 }
1958 else
1959 next = end;
1960 VISIT(c, expr, s->v.If.test);
1961 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1962 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001963 if (asdl_seq_LEN(s->v.If.orelse)) {
1964 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 compiler_use_next_block(c, next);
1966 VISIT_SEQ(c, stmt, s->v.If.orelse);
1967 }
1968 }
1969 compiler_use_next_block(c, end);
1970 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001971}
1972
1973static int
1974compiler_for(struct compiler *c, stmt_ty s)
1975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 start = compiler_new_block(c);
1979 cleanup = compiler_new_block(c);
1980 end = compiler_new_block(c);
1981 if (start == NULL || end == NULL || cleanup == NULL)
1982 return 0;
1983 ADDOP_JREL(c, SETUP_LOOP, end);
1984 if (!compiler_push_fblock(c, LOOP, start))
1985 return 0;
1986 VISIT(c, expr, s->v.For.iter);
1987 ADDOP(c, GET_ITER);
1988 compiler_use_next_block(c, start);
1989 ADDOP_JREL(c, FOR_ITER, cleanup);
1990 VISIT(c, expr, s->v.For.target);
1991 VISIT_SEQ(c, stmt, s->v.For.body);
1992 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1993 compiler_use_next_block(c, cleanup);
1994 ADDOP(c, POP_BLOCK);
1995 compiler_pop_fblock(c, LOOP, start);
1996 VISIT_SEQ(c, stmt, s->v.For.orelse);
1997 compiler_use_next_block(c, end);
1998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001999}
2000
Yury Selivanov75445082015-05-11 22:57:16 -04002001
2002static int
2003compiler_async_for(struct compiler *c, stmt_ty s)
2004{
2005 static PyObject *stopiter_error = NULL;
2006 basicblock *try, *except, *end, *after_try, *try_cleanup,
2007 *after_loop, *after_loop_else;
2008
2009 if (stopiter_error == NULL) {
2010 stopiter_error = PyUnicode_InternFromString("StopAsyncIteration");
2011 if (stopiter_error == NULL)
2012 return 0;
2013 }
2014
2015 try = compiler_new_block(c);
2016 except = compiler_new_block(c);
2017 end = compiler_new_block(c);
2018 after_try = compiler_new_block(c);
2019 try_cleanup = compiler_new_block(c);
2020 after_loop = compiler_new_block(c);
2021 after_loop_else = compiler_new_block(c);
2022
2023 if (try == NULL || except == NULL || end == NULL
2024 || after_try == NULL || try_cleanup == NULL)
2025 return 0;
2026
2027 ADDOP_JREL(c, SETUP_LOOP, after_loop);
2028 if (!compiler_push_fblock(c, LOOP, try))
2029 return 0;
2030
2031 VISIT(c, expr, s->v.AsyncFor.iter);
2032 ADDOP(c, GET_AITER);
2033 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2034 ADDOP(c, YIELD_FROM);
2035
2036 compiler_use_next_block(c, try);
2037
2038
2039 ADDOP_JREL(c, SETUP_EXCEPT, except);
2040 if (!compiler_push_fblock(c, EXCEPT, try))
2041 return 0;
2042
2043 ADDOP(c, GET_ANEXT);
2044 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2045 ADDOP(c, YIELD_FROM);
2046 VISIT(c, expr, s->v.AsyncFor.target);
2047 ADDOP(c, POP_BLOCK);
2048 compiler_pop_fblock(c, EXCEPT, try);
2049 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2050
2051
2052 compiler_use_next_block(c, except);
2053 ADDOP(c, DUP_TOP);
2054 ADDOP_O(c, LOAD_GLOBAL, stopiter_error, names);
2055 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2056 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2057
2058 ADDOP(c, POP_TOP);
2059 ADDOP(c, POP_TOP);
2060 ADDOP(c, POP_TOP);
2061 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2062 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2063 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2064
2065
2066 compiler_use_next_block(c, try_cleanup);
2067 ADDOP(c, END_FINALLY);
2068
2069 compiler_use_next_block(c, after_try);
2070 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2071 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2072
2073 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2074 compiler_pop_fblock(c, LOOP, try);
2075
2076 compiler_use_next_block(c, after_loop);
2077 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2078
2079 compiler_use_next_block(c, after_loop_else);
2080 VISIT_SEQ(c, stmt, s->v.For.orelse);
2081
2082 compiler_use_next_block(c, end);
2083
2084 return 1;
2085}
2086
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002087static int
2088compiler_while(struct compiler *c, stmt_ty s)
2089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00002091 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 if (constant == 0) {
2094 if (s->v.While.orelse)
2095 VISIT_SEQ(c, stmt, s->v.While.orelse);
2096 return 1;
2097 }
2098 loop = compiler_new_block(c);
2099 end = compiler_new_block(c);
2100 if (constant == -1) {
2101 anchor = compiler_new_block(c);
2102 if (anchor == NULL)
2103 return 0;
2104 }
2105 if (loop == NULL || end == NULL)
2106 return 0;
2107 if (s->v.While.orelse) {
2108 orelse = compiler_new_block(c);
2109 if (orelse == NULL)
2110 return 0;
2111 }
2112 else
2113 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 ADDOP_JREL(c, SETUP_LOOP, end);
2116 compiler_use_next_block(c, loop);
2117 if (!compiler_push_fblock(c, LOOP, loop))
2118 return 0;
2119 if (constant == -1) {
2120 VISIT(c, expr, s->v.While.test);
2121 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
2122 }
2123 VISIT_SEQ(c, stmt, s->v.While.body);
2124 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 /* XXX should the two POP instructions be in a separate block
2127 if there is no else clause ?
2128 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002130 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 compiler_use_next_block(c, anchor);
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002132 ADDOP(c, POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 compiler_pop_fblock(c, LOOP, loop);
2134 if (orelse != NULL) /* what if orelse is just pass? */
2135 VISIT_SEQ(c, stmt, s->v.While.orelse);
2136 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002139}
2140
2141static int
2142compiler_continue(struct compiler *c)
2143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2145 static const char IN_FINALLY_ERROR_MSG[] =
2146 "'continue' not supported inside 'finally' clause";
2147 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 if (!c->u->u_nfblocks)
2150 return compiler_error(c, LOOP_ERROR_MSG);
2151 i = c->u->u_nfblocks - 1;
2152 switch (c->u->u_fblock[i].fb_type) {
2153 case LOOP:
2154 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2155 break;
2156 case EXCEPT:
2157 case FINALLY_TRY:
2158 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2159 /* Prevent continue anywhere under a finally
2160 even if hidden in a sub-try or except. */
2161 if (c->u->u_fblock[i].fb_type == FINALLY_END)
2162 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2163 }
2164 if (i == -1)
2165 return compiler_error(c, LOOP_ERROR_MSG);
2166 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2167 break;
2168 case FINALLY_END:
2169 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2170 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002173}
2174
2175/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176
2177 SETUP_FINALLY L
2178 <code for body>
2179 POP_BLOCK
2180 LOAD_CONST <None>
2181 L: <code for finalbody>
2182 END_FINALLY
2183
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002184 The special instructions use the block stack. Each block
2185 stack entry contains the instruction that created it (here
2186 SETUP_FINALLY), the level of the value stack at the time the
2187 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Pushes the current value stack level and the label
2191 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002192 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 Pops en entry from the block stack, and pops the value
2194 stack until its level is the same as indicated on the
2195 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002196 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 Pops a variable number of entries from the *value* stack
2198 and re-raises the exception they specify. The number of
2199 entries popped depends on the (pseudo) exception type.
2200
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002201 The block stack is unwound when an exception is raised:
2202 when a SETUP_FINALLY entry is found, the exception is pushed
2203 onto the value stack (and the exception condition is cleared),
2204 and the interpreter jumps to the label gotten from the block
2205 stack.
2206*/
2207
2208static int
2209compiler_try_finally(struct compiler *c, stmt_ty s)
2210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 basicblock *body, *end;
2212 body = compiler_new_block(c);
2213 end = compiler_new_block(c);
2214 if (body == NULL || end == NULL)
2215 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 ADDOP_JREL(c, SETUP_FINALLY, end);
2218 compiler_use_next_block(c, body);
2219 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2220 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002221 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2222 if (!compiler_try_except(c, s))
2223 return 0;
2224 }
2225 else {
2226 VISIT_SEQ(c, stmt, s->v.Try.body);
2227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 ADDOP(c, POP_BLOCK);
2229 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2232 compiler_use_next_block(c, end);
2233 if (!compiler_push_fblock(c, FINALLY_END, end))
2234 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002235 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 ADDOP(c, END_FINALLY);
2237 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002240}
2241
2242/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002243 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002244 (The contents of the value stack is shown in [], with the top
2245 at the right; 'tb' is trace-back info, 'val' the exception's
2246 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247
2248 Value stack Label Instruction Argument
2249 [] SETUP_EXCEPT L1
2250 [] <code for S>
2251 [] POP_BLOCK
2252 [] JUMP_FORWARD L0
2253
2254 [tb, val, exc] L1: DUP )
2255 [tb, val, exc, exc] <evaluate E1> )
2256 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2257 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2258 [tb, val, exc] POP
2259 [tb, val] <assign to V1> (or POP if no V1)
2260 [tb] POP
2261 [] <code for S1>
2262 JUMP_FORWARD L0
2263
2264 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002265 .............................etc.......................
2266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2268
2269 [] L0: <next statement>
2270
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002271 Of course, parts are not generated if Vi or Ei is not present.
2272*/
2273static int
2274compiler_try_except(struct compiler *c, stmt_ty s)
2275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002277 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 body = compiler_new_block(c);
2280 except = compiler_new_block(c);
2281 orelse = compiler_new_block(c);
2282 end = compiler_new_block(c);
2283 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2284 return 0;
2285 ADDOP_JREL(c, SETUP_EXCEPT, except);
2286 compiler_use_next_block(c, body);
2287 if (!compiler_push_fblock(c, EXCEPT, body))
2288 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002289 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 ADDOP(c, POP_BLOCK);
2291 compiler_pop_fblock(c, EXCEPT, body);
2292 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002293 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 compiler_use_next_block(c, except);
2295 for (i = 0; i < n; i++) {
2296 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002297 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 if (!handler->v.ExceptHandler.type && i < n-1)
2299 return compiler_error(c, "default 'except:' must be last");
2300 c->u->u_lineno_set = 0;
2301 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002302 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 except = compiler_new_block(c);
2304 if (except == NULL)
2305 return 0;
2306 if (handler->v.ExceptHandler.type) {
2307 ADDOP(c, DUP_TOP);
2308 VISIT(c, expr, handler->v.ExceptHandler.type);
2309 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2310 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2311 }
2312 ADDOP(c, POP_TOP);
2313 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002314 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002315
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002316 cleanup_end = compiler_new_block(c);
2317 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002318 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002319 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002320
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002321 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2322 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002324 /*
2325 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002326 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002327 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002328 try:
2329 # body
2330 finally:
2331 name = None
2332 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002333 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002335 /* second try: */
2336 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2337 compiler_use_next_block(c, cleanup_body);
2338 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2339 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002341 /* second # body */
2342 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2343 ADDOP(c, POP_BLOCK);
2344 ADDOP(c, POP_EXCEPT);
2345 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002347 /* finally: */
2348 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2349 compiler_use_next_block(c, cleanup_end);
2350 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2351 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002353 /* name = None */
2354 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2355 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002357 /* del name */
2358 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002360 ADDOP(c, END_FINALLY);
2361 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 }
2363 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002364 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002366 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002367 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002368 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369
Guido van Rossumb940e112007-01-10 16:19:56 +00002370 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002371 ADDOP(c, POP_TOP);
2372 compiler_use_next_block(c, cleanup_body);
2373 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2374 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002376 ADDOP(c, POP_EXCEPT);
2377 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 }
2379 ADDOP_JREL(c, JUMP_FORWARD, end);
2380 compiler_use_next_block(c, except);
2381 }
2382 ADDOP(c, END_FINALLY);
2383 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002384 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 compiler_use_next_block(c, end);
2386 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002387}
2388
2389static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002390compiler_try(struct compiler *c, stmt_ty s) {
2391 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2392 return compiler_try_finally(c, s);
2393 else
2394 return compiler_try_except(c, s);
2395}
2396
2397
2398static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002399compiler_import_as(struct compiler *c, identifier name, identifier asname)
2400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* The IMPORT_NAME opcode was already generated. This function
2402 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 If there is a dot in name, we need to split it and emit a
2405 LOAD_ATTR for each name.
2406 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002407 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2408 PyUnicode_GET_LENGTH(name), 1);
2409 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002410 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002411 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 /* Consume the base module name to get the first attribute */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002413 Py_ssize_t pos = dot + 1;
2414 while (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PyObject *attr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002416 dot = PyUnicode_FindChar(name, '.', pos,
2417 PyUnicode_GET_LENGTH(name), 1);
2418 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002419 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002420 attr = PyUnicode_Substring(name, pos,
2421 (dot != -1) ? dot :
2422 PyUnicode_GET_LENGTH(name));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002423 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002424 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 ADDOP_O(c, LOAD_ATTR, attr, names);
2426 Py_DECREF(attr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002427 pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 }
2429 }
2430 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002431}
2432
2433static int
2434compiler_import(struct compiler *c, stmt_ty s)
2435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 /* The Import node stores a module name like a.b.c as a single
2437 string. This is convenient for all cases except
2438 import a.b.c as d
2439 where we need to parse that string to extract the individual
2440 module names.
2441 XXX Perhaps change the representation to make this case simpler?
2442 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002443 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 for (i = 0; i < n; i++) {
2446 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2447 int r;
2448 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002450 level = PyLong_FromLong(0);
2451 if (level == NULL)
2452 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 ADDOP_O(c, LOAD_CONST, level, consts);
2455 Py_DECREF(level);
2456 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2457 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 if (alias->asname) {
2460 r = compiler_import_as(c, alias->name, alias->asname);
2461 if (!r)
2462 return r;
2463 }
2464 else {
2465 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002466 Py_ssize_t dot = PyUnicode_FindChar(
2467 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002468 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002469 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002470 if (tmp == NULL)
2471 return 0;
2472 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002474 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 Py_DECREF(tmp);
2476 }
2477 if (!r)
2478 return r;
2479 }
2480 }
2481 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002482}
2483
2484static int
2485compiler_from_import(struct compiler *c, stmt_ty s)
2486{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002487 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 PyObject *names = PyTuple_New(n);
2490 PyObject *level;
2491 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 if (!empty_string) {
2494 empty_string = PyUnicode_FromString("");
2495 if (!empty_string)
2496 return 0;
2497 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 if (!names)
2500 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 level = PyLong_FromLong(s->v.ImportFrom.level);
2503 if (!level) {
2504 Py_DECREF(names);
2505 return 0;
2506 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 /* build up the names */
2509 for (i = 0; i < n; i++) {
2510 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2511 Py_INCREF(alias->name);
2512 PyTuple_SET_ITEM(names, i, alias->name);
2513 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2516 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2517 Py_DECREF(level);
2518 Py_DECREF(names);
2519 return compiler_error(c, "from __future__ imports must occur "
2520 "at the beginning of the file");
2521 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 ADDOP_O(c, LOAD_CONST, level, consts);
2524 Py_DECREF(level);
2525 ADDOP_O(c, LOAD_CONST, names, consts);
2526 Py_DECREF(names);
2527 if (s->v.ImportFrom.module) {
2528 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2529 }
2530 else {
2531 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2532 }
2533 for (i = 0; i < n; i++) {
2534 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2535 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002536
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002537 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 assert(n == 1);
2539 ADDOP(c, IMPORT_STAR);
2540 return 1;
2541 }
2542
2543 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2544 store_name = alias->name;
2545 if (alias->asname)
2546 store_name = alias->asname;
2547
2548 if (!compiler_nameop(c, store_name, Store)) {
2549 Py_DECREF(names);
2550 return 0;
2551 }
2552 }
2553 /* remove imported module */
2554 ADDOP(c, POP_TOP);
2555 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002556}
2557
2558static int
2559compiler_assert(struct compiler *c, stmt_ty s)
2560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 static PyObject *assertion_error = NULL;
2562 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02002563 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002564
Georg Brandl8334fd92010-12-04 10:26:46 +00002565 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 return 1;
2567 if (assertion_error == NULL) {
2568 assertion_error = PyUnicode_InternFromString("AssertionError");
2569 if (assertion_error == NULL)
2570 return 0;
2571 }
2572 if (s->v.Assert.test->kind == Tuple_kind &&
2573 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02002574 msg = PyUnicode_FromString("assertion is always true, "
2575 "perhaps remove parentheses?");
2576 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002578 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2579 c->c_filename, c->u->u_lineno,
2580 NULL, NULL) == -1) {
2581 Py_DECREF(msg);
2582 return 0;
2583 }
2584 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 }
2586 VISIT(c, expr, s->v.Assert.test);
2587 end = compiler_new_block(c);
2588 if (end == NULL)
2589 return 0;
2590 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2591 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2592 if (s->v.Assert.msg) {
2593 VISIT(c, expr, s->v.Assert.msg);
2594 ADDOP_I(c, CALL_FUNCTION, 1);
2595 }
2596 ADDOP_I(c, RAISE_VARARGS, 1);
2597 compiler_use_next_block(c, end);
2598 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002599}
2600
2601static int
2602compiler_visit_stmt(struct compiler *c, stmt_ty s)
2603{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002604 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 /* Always assign a lineno to the next instruction for a stmt. */
2607 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002608 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 switch (s->kind) {
2612 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04002613 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 case ClassDef_kind:
2615 return compiler_class(c, s);
2616 case Return_kind:
2617 if (c->u->u_ste->ste_type != FunctionBlock)
2618 return compiler_error(c, "'return' outside function");
2619 if (s->v.Return.value) {
2620 VISIT(c, expr, s->v.Return.value);
2621 }
2622 else
2623 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2624 ADDOP(c, RETURN_VALUE);
2625 break;
2626 case Delete_kind:
2627 VISIT_SEQ(c, expr, s->v.Delete.targets)
2628 break;
2629 case Assign_kind:
2630 n = asdl_seq_LEN(s->v.Assign.targets);
2631 VISIT(c, expr, s->v.Assign.value);
2632 for (i = 0; i < n; i++) {
2633 if (i < n - 1)
2634 ADDOP(c, DUP_TOP);
2635 VISIT(c, expr,
2636 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2637 }
2638 break;
2639 case AugAssign_kind:
2640 return compiler_augassign(c, s);
2641 case For_kind:
2642 return compiler_for(c, s);
2643 case While_kind:
2644 return compiler_while(c, s);
2645 case If_kind:
2646 return compiler_if(c, s);
2647 case Raise_kind:
2648 n = 0;
2649 if (s->v.Raise.exc) {
2650 VISIT(c, expr, s->v.Raise.exc);
2651 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002652 if (s->v.Raise.cause) {
2653 VISIT(c, expr, s->v.Raise.cause);
2654 n++;
2655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01002657 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002659 case Try_kind:
2660 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 case Assert_kind:
2662 return compiler_assert(c, s);
2663 case Import_kind:
2664 return compiler_import(c, s);
2665 case ImportFrom_kind:
2666 return compiler_from_import(c, s);
2667 case Global_kind:
2668 case Nonlocal_kind:
2669 break;
2670 case Expr_kind:
2671 if (c->c_interactive && c->c_nestlevel <= 1) {
2672 VISIT(c, expr, s->v.Expr.value);
2673 ADDOP(c, PRINT_EXPR);
2674 }
2675 else if (s->v.Expr.value->kind != Str_kind &&
2676 s->v.Expr.value->kind != Num_kind) {
2677 VISIT(c, expr, s->v.Expr.value);
2678 ADDOP(c, POP_TOP);
2679 }
2680 break;
2681 case Pass_kind:
2682 break;
2683 case Break_kind:
2684 if (!compiler_in_loop(c))
2685 return compiler_error(c, "'break' outside loop");
2686 ADDOP(c, BREAK_LOOP);
2687 break;
2688 case Continue_kind:
2689 return compiler_continue(c);
2690 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002691 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04002692 case AsyncFunctionDef_kind:
2693 return compiler_function(c, s, 1);
2694 case AsyncWith_kind:
2695 return compiler_async_with(c, s, 0);
2696 case AsyncFor_kind:
2697 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 }
Yury Selivanov75445082015-05-11 22:57:16 -04002699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002701}
2702
2703static int
2704unaryop(unaryop_ty op)
2705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 switch (op) {
2707 case Invert:
2708 return UNARY_INVERT;
2709 case Not:
2710 return UNARY_NOT;
2711 case UAdd:
2712 return UNARY_POSITIVE;
2713 case USub:
2714 return UNARY_NEGATIVE;
2715 default:
2716 PyErr_Format(PyExc_SystemError,
2717 "unary op %d should not be possible", op);
2718 return 0;
2719 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002720}
2721
2722static int
2723binop(struct compiler *c, operator_ty op)
2724{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 switch (op) {
2726 case Add:
2727 return BINARY_ADD;
2728 case Sub:
2729 return BINARY_SUBTRACT;
2730 case Mult:
2731 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002732 case MatMult:
2733 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 case Div:
2735 return BINARY_TRUE_DIVIDE;
2736 case Mod:
2737 return BINARY_MODULO;
2738 case Pow:
2739 return BINARY_POWER;
2740 case LShift:
2741 return BINARY_LSHIFT;
2742 case RShift:
2743 return BINARY_RSHIFT;
2744 case BitOr:
2745 return BINARY_OR;
2746 case BitXor:
2747 return BINARY_XOR;
2748 case BitAnd:
2749 return BINARY_AND;
2750 case FloorDiv:
2751 return BINARY_FLOOR_DIVIDE;
2752 default:
2753 PyErr_Format(PyExc_SystemError,
2754 "binary op %d should not be possible", op);
2755 return 0;
2756 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002757}
2758
2759static int
2760cmpop(cmpop_ty op)
2761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 switch (op) {
2763 case Eq:
2764 return PyCmp_EQ;
2765 case NotEq:
2766 return PyCmp_NE;
2767 case Lt:
2768 return PyCmp_LT;
2769 case LtE:
2770 return PyCmp_LE;
2771 case Gt:
2772 return PyCmp_GT;
2773 case GtE:
2774 return PyCmp_GE;
2775 case Is:
2776 return PyCmp_IS;
2777 case IsNot:
2778 return PyCmp_IS_NOT;
2779 case In:
2780 return PyCmp_IN;
2781 case NotIn:
2782 return PyCmp_NOT_IN;
2783 default:
2784 return PyCmp_BAD;
2785 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002786}
2787
2788static int
2789inplace_binop(struct compiler *c, operator_ty op)
2790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 switch (op) {
2792 case Add:
2793 return INPLACE_ADD;
2794 case Sub:
2795 return INPLACE_SUBTRACT;
2796 case Mult:
2797 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002798 case MatMult:
2799 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 case Div:
2801 return INPLACE_TRUE_DIVIDE;
2802 case Mod:
2803 return INPLACE_MODULO;
2804 case Pow:
2805 return INPLACE_POWER;
2806 case LShift:
2807 return INPLACE_LSHIFT;
2808 case RShift:
2809 return INPLACE_RSHIFT;
2810 case BitOr:
2811 return INPLACE_OR;
2812 case BitXor:
2813 return INPLACE_XOR;
2814 case BitAnd:
2815 return INPLACE_AND;
2816 case FloorDiv:
2817 return INPLACE_FLOOR_DIVIDE;
2818 default:
2819 PyErr_Format(PyExc_SystemError,
2820 "inplace binary op %d should not be possible", op);
2821 return 0;
2822 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002823}
2824
2825static int
2826compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2827{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002828 int op, scope;
2829 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 PyObject *dict = c->u->u_names;
2833 PyObject *mangled;
2834 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 mangled = _Py_Mangle(c->u->u_private, name);
2837 if (!mangled)
2838 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002839
Benjamin Peterson70b224d2012-12-06 17:49:58 -05002840 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2841 PyUnicode_CompareWithASCIIString(name, "True") &&
2842 PyUnicode_CompareWithASCIIString(name, "False"));
2843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 op = 0;
2845 optype = OP_NAME;
2846 scope = PyST_GetScope(c->u->u_ste, mangled);
2847 switch (scope) {
2848 case FREE:
2849 dict = c->u->u_freevars;
2850 optype = OP_DEREF;
2851 break;
2852 case CELL:
2853 dict = c->u->u_cellvars;
2854 optype = OP_DEREF;
2855 break;
2856 case LOCAL:
2857 if (c->u->u_ste->ste_type == FunctionBlock)
2858 optype = OP_FAST;
2859 break;
2860 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04002861 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 optype = OP_GLOBAL;
2863 break;
2864 case GLOBAL_EXPLICIT:
2865 optype = OP_GLOBAL;
2866 break;
2867 default:
2868 /* scope can be 0 */
2869 break;
2870 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002873 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 switch (optype) {
2876 case OP_DEREF:
2877 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002878 case Load:
2879 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
2880 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 case Store: op = STORE_DEREF; break;
2882 case AugLoad:
2883 case AugStore:
2884 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002885 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 case Param:
2887 default:
2888 PyErr_SetString(PyExc_SystemError,
2889 "param invalid for deref variable");
2890 return 0;
2891 }
2892 break;
2893 case OP_FAST:
2894 switch (ctx) {
2895 case Load: op = LOAD_FAST; break;
2896 case Store: op = STORE_FAST; break;
2897 case Del: op = DELETE_FAST; break;
2898 case AugLoad:
2899 case AugStore:
2900 break;
2901 case Param:
2902 default:
2903 PyErr_SetString(PyExc_SystemError,
2904 "param invalid for local variable");
2905 return 0;
2906 }
2907 ADDOP_O(c, op, mangled, varnames);
2908 Py_DECREF(mangled);
2909 return 1;
2910 case OP_GLOBAL:
2911 switch (ctx) {
2912 case Load: op = LOAD_GLOBAL; break;
2913 case Store: op = STORE_GLOBAL; break;
2914 case Del: op = DELETE_GLOBAL; break;
2915 case AugLoad:
2916 case AugStore:
2917 break;
2918 case Param:
2919 default:
2920 PyErr_SetString(PyExc_SystemError,
2921 "param invalid for global variable");
2922 return 0;
2923 }
2924 break;
2925 case OP_NAME:
2926 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002927 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 case Store: op = STORE_NAME; break;
2929 case Del: op = DELETE_NAME; break;
2930 case AugLoad:
2931 case AugStore:
2932 break;
2933 case Param:
2934 default:
2935 PyErr_SetString(PyExc_SystemError,
2936 "param invalid for name variable");
2937 return 0;
2938 }
2939 break;
2940 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 assert(op);
2943 arg = compiler_add_o(c, dict, mangled);
2944 Py_DECREF(mangled);
2945 if (arg < 0)
2946 return 0;
2947 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002948}
2949
2950static int
2951compiler_boolop(struct compiler *c, expr_ty e)
2952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002954 int jumpi;
2955 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 assert(e->kind == BoolOp_kind);
2959 if (e->v.BoolOp.op == And)
2960 jumpi = JUMP_IF_FALSE_OR_POP;
2961 else
2962 jumpi = JUMP_IF_TRUE_OR_POP;
2963 end = compiler_new_block(c);
2964 if (end == NULL)
2965 return 0;
2966 s = e->v.BoolOp.values;
2967 n = asdl_seq_LEN(s) - 1;
2968 assert(n >= 0);
2969 for (i = 0; i < n; ++i) {
2970 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2971 ADDOP_JABS(c, jumpi, end);
2972 }
2973 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2974 compiler_use_next_block(c, end);
2975 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002976}
2977
2978static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002979starunpack_helper(struct compiler *c, asdl_seq *elts,
2980 int single_op, int inner_op, int outer_op)
2981{
2982 Py_ssize_t n = asdl_seq_LEN(elts);
2983 Py_ssize_t i, nsubitems = 0, nseen = 0;
2984 for (i = 0; i < n; i++) {
2985 expr_ty elt = asdl_seq_GET(elts, i);
2986 if (elt->kind == Starred_kind) {
2987 if (nseen) {
2988 ADDOP_I(c, inner_op, nseen);
2989 nseen = 0;
2990 nsubitems++;
2991 }
2992 VISIT(c, expr, elt->v.Starred.value);
2993 nsubitems++;
2994 }
2995 else {
2996 VISIT(c, expr, elt);
2997 nseen++;
2998 }
2999 }
3000 if (nsubitems) {
3001 if (nseen) {
3002 ADDOP_I(c, inner_op, nseen);
3003 nsubitems++;
3004 }
3005 ADDOP_I(c, outer_op, nsubitems);
3006 }
3007 else
3008 ADDOP_I(c, single_op, nseen);
3009 return 1;
3010}
3011
3012static int
3013assignment_helper(struct compiler *c, asdl_seq *elts)
3014{
3015 Py_ssize_t n = asdl_seq_LEN(elts);
3016 Py_ssize_t i;
3017 int seen_star = 0;
3018 for (i = 0; i < n; i++) {
3019 expr_ty elt = asdl_seq_GET(elts, i);
3020 if (elt->kind == Starred_kind && !seen_star) {
3021 if ((i >= (1 << 8)) ||
3022 (n-i-1 >= (INT_MAX >> 8)))
3023 return compiler_error(c,
3024 "too many expressions in "
3025 "star-unpacking assignment");
3026 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3027 seen_star = 1;
3028 asdl_seq_SET(elts, i, elt->v.Starred.value);
3029 }
3030 else if (elt->kind == Starred_kind) {
3031 return compiler_error(c,
3032 "two starred expressions in assignment");
3033 }
3034 }
3035 if (!seen_star) {
3036 ADDOP_I(c, UNPACK_SEQUENCE, n);
3037 }
3038 VISIT_SEQ(c, expr, elts);
3039 return 1;
3040}
3041
3042static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003043compiler_list(struct compiler *c, expr_ty e)
3044{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003045 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003047 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003049 else if (e->v.List.ctx == Load) {
3050 return starunpack_helper(c, elts,
3051 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003053 else
3054 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003056}
3057
3058static int
3059compiler_tuple(struct compiler *c, expr_ty e)
3060{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003061 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003063 return assignment_helper(c, elts);
3064 }
3065 else if (e->v.Tuple.ctx == Load) {
3066 return starunpack_helper(c, elts,
3067 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3068 }
3069 else
3070 VISIT_SEQ(c, expr, elts);
3071 return 1;
3072}
3073
3074static int
3075compiler_set(struct compiler *c, expr_ty e)
3076{
3077 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3078 BUILD_SET, BUILD_SET_UNPACK);
3079}
3080
3081static int
3082compiler_dict(struct compiler *c, expr_ty e)
3083{
3084 Py_ssize_t i, n, containers, elements;
3085 int is_unpacking = 0;
3086 n = asdl_seq_LEN(e->v.Dict.values);
3087 containers = 0;
3088 elements = 0;
3089 for (i = 0; i < n; i++) {
3090 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3091 if (elements == 0xFFFF || (elements && is_unpacking)) {
3092 ADDOP_I(c, BUILD_MAP, elements);
3093 containers++;
3094 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003096 if (is_unpacking) {
3097 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3098 containers++;
3099 }
3100 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003101 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Benjamin Petersonee853392015-05-28 14:30:26 -05003102 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003103 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 }
3105 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003106 if (elements || containers == 0) {
3107 ADDOP_I(c, BUILD_MAP, elements);
3108 containers++;
3109 }
3110 /* If there is more than one dict, they need to be merged into a new
3111 * dict. If there is one dict and it's an unpacking, then it needs
3112 * to be copied into a new dict." */
3113 while (containers > 1 || is_unpacking) {
3114 int oparg = containers < 255 ? containers : 255;
3115 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3116 containers -= (oparg - 1);
3117 is_unpacking = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 }
3119 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003120}
3121
3122static int
3123compiler_compare(struct compiler *c, expr_ty e)
3124{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003125 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
3129 VISIT(c, expr, e->v.Compare.left);
3130 n = asdl_seq_LEN(e->v.Compare.ops);
3131 assert(n > 0);
3132 if (n > 1) {
3133 cleanup = compiler_new_block(c);
3134 if (cleanup == NULL)
3135 return 0;
3136 VISIT(c, expr,
3137 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3138 }
3139 for (i = 1; i < n; i++) {
3140 ADDOP(c, DUP_TOP);
3141 ADDOP(c, ROT_THREE);
3142 ADDOP_I(c, COMPARE_OP,
3143 cmpop((cmpop_ty)(asdl_seq_GET(
3144 e->v.Compare.ops, i - 1))));
3145 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3146 NEXT_BLOCK(c);
3147 if (i < (n - 1))
3148 VISIT(c, expr,
3149 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
3150 }
3151 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
3152 ADDOP_I(c, COMPARE_OP,
3153 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
3154 if (n > 1) {
3155 basicblock *end = compiler_new_block(c);
3156 if (end == NULL)
3157 return 0;
3158 ADDOP_JREL(c, JUMP_FORWARD, end);
3159 compiler_use_next_block(c, cleanup);
3160 ADDOP(c, ROT_TWO);
3161 ADDOP(c, POP_TOP);
3162 compiler_use_next_block(c, end);
3163 }
3164 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003165}
3166
3167static int
3168compiler_call(struct compiler *c, expr_ty e)
3169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003170 VISIT(c, expr, e->v.Call.func);
3171 return compiler_call_helper(c, 0,
3172 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003173 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003174}
3175
3176/* shared code between compiler_call and compiler_class */
3177static int
3178compiler_call_helper(struct compiler *c,
Victor Stinnerad9a0662013-11-19 22:23:20 +01003179 Py_ssize_t n, /* Args already pushed */
3180 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003181 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 int code = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003184 Py_ssize_t nelts, i, nseen, nkw;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003185
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003186 /* the number of tuples and dictionaries on the stack */
3187 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3188
3189 nkw = 0;
3190 nseen = 0; /* the number of positional arguments on the stack */
3191 nelts = asdl_seq_LEN(args);
3192 for (i = 0; i < nelts; i++) {
3193 expr_ty elt = asdl_seq_GET(args, i);
3194 if (elt->kind == Starred_kind) {
3195 /* A star-arg. If we've seen positional arguments,
3196 pack the positional arguments into a
3197 tuple. */
3198 if (nseen) {
3199 ADDOP_I(c, BUILD_TUPLE, nseen);
3200 nseen = 0;
3201 nsubargs++;
3202 }
3203 VISIT(c, expr, elt->v.Starred.value);
3204 nsubargs++;
3205 }
3206 else if (nsubargs) {
3207 /* We've seen star-args already, so we
3208 count towards items-to-pack-into-tuple. */
3209 VISIT(c, expr, elt);
3210 nseen++;
3211 }
3212 else {
3213 /* Positional arguments before star-arguments
3214 are left on the stack. */
3215 VISIT(c, expr, elt);
3216 n++;
3217 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003219 if (nseen) {
3220 /* Pack up any trailing positional arguments. */
3221 ADDOP_I(c, BUILD_TUPLE, nseen);
3222 nsubargs++;
3223 }
3224 if (nsubargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 code |= 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003226 if (nsubargs > 1) {
3227 /* If we ended up with more than one stararg, we need
3228 to concatenate them into a single sequence. */
3229 ADDOP_I(c, BUILD_LIST_UNPACK, nsubargs);
3230 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003232
3233 /* Same dance again for keyword arguments */
3234 nseen = 0; /* the number of keyword arguments on the stack following */
3235 nelts = asdl_seq_LEN(keywords);
3236 for (i = 0; i < nelts; i++) {
3237 keyword_ty kw = asdl_seq_GET(keywords, i);
3238 if (kw->arg == NULL) {
3239 /* A keyword argument unpacking. */
3240 if (nseen) {
3241 ADDOP_I(c, BUILD_MAP, nseen);
3242 nseen = 0;
3243 nsubkwargs++;
3244 }
3245 VISIT(c, expr, kw->value);
3246 nsubkwargs++;
3247 }
3248 else if (nsubkwargs) {
3249 /* A keyword argument and we already have a dict. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003250 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
Benjamin Petersonee853392015-05-28 14:30:26 -05003251 VISIT(c, expr, kw->value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003252 nseen++;
3253 }
3254 else {
3255 /* keyword argument */
3256 VISIT(c, keyword, kw)
3257 nkw++;
3258 }
3259 }
3260 if (nseen) {
3261 /* Pack up any trailing keyword arguments. */
3262 ADDOP_I(c, BUILD_MAP, nseen);
3263 nsubkwargs++;
3264 }
3265 if (nsubkwargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 code |= 2;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003267 if (nsubkwargs > 1) {
3268 /* Pack it all up */
Serhiy Storchaka3c317e72016-06-12 09:22:01 +03003269 int function_pos = n + (code & 1) + 2 * nkw + 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003270 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8));
3271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003272 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003273 assert(n < 1<<8);
3274 assert(nkw < 1<<24);
3275 n |= nkw << 8;
3276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 switch (code) {
3278 case 0:
3279 ADDOP_I(c, CALL_FUNCTION, n);
3280 break;
3281 case 1:
3282 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3283 break;
3284 case 2:
3285 ADDOP_I(c, CALL_FUNCTION_KW, n);
3286 break;
3287 case 3:
3288 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3289 break;
3290 }
3291 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003292}
3293
Nick Coghlan650f0d02007-04-15 12:05:43 +00003294
3295/* List and set comprehensions and generator expressions work by creating a
3296 nested function to perform the actual iteration. This means that the
3297 iteration variables don't leak into the current scope.
3298 The defined function is called immediately following its definition, with the
3299 result of that call being the result of the expression.
3300 The LC/SC version returns the populated container, while the GE version is
3301 flagged in symtable.c as a generator, so it returns the generator object
3302 when the function is called.
3303 This code *knows* that the loop cannot contain break, continue, or return,
3304 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3305
3306 Possible cleanups:
3307 - iterate over the generator sequence instead of using recursion
3308*/
3309
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003310static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311compiler_comprehension_generator(struct compiler *c,
3312 asdl_seq *generators, int gen_index,
3313 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 /* generate code for the iterator, then each of the ifs,
3316 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 comprehension_ty gen;
3319 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003320 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 start = compiler_new_block(c);
3323 skip = compiler_new_block(c);
3324 if_cleanup = compiler_new_block(c);
3325 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3328 anchor == NULL)
3329 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003331 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 if (gen_index == 0) {
3334 /* Receive outermost iter as an implicit argument */
3335 c->u->u_argcount = 1;
3336 ADDOP_I(c, LOAD_FAST, 0);
3337 }
3338 else {
3339 /* Sub-iter - calculate on the fly */
3340 VISIT(c, expr, gen->iter);
3341 ADDOP(c, GET_ITER);
3342 }
3343 compiler_use_next_block(c, start);
3344 ADDOP_JREL(c, FOR_ITER, anchor);
3345 NEXT_BLOCK(c);
3346 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 /* XXX this needs to be cleaned up...a lot! */
3349 n = asdl_seq_LEN(gen->ifs);
3350 for (i = 0; i < n; i++) {
3351 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3352 VISIT(c, expr, e);
3353 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3354 NEXT_BLOCK(c);
3355 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 if (++gen_index < asdl_seq_LEN(generators))
3358 if (!compiler_comprehension_generator(c,
3359 generators, gen_index,
3360 elt, val, type))
3361 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 /* only append after the last for generator */
3364 if (gen_index >= asdl_seq_LEN(generators)) {
3365 /* comprehension specific code */
3366 switch (type) {
3367 case COMP_GENEXP:
3368 VISIT(c, expr, elt);
3369 ADDOP(c, YIELD_VALUE);
3370 ADDOP(c, POP_TOP);
3371 break;
3372 case COMP_LISTCOMP:
3373 VISIT(c, expr, elt);
3374 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3375 break;
3376 case COMP_SETCOMP:
3377 VISIT(c, expr, elt);
3378 ADDOP_I(c, SET_ADD, gen_index + 1);
3379 break;
3380 case COMP_DICTCOMP:
3381 /* With 'd[k] = v', v is evaluated before k, so we do
3382 the same. */
3383 VISIT(c, expr, val);
3384 VISIT(c, expr, elt);
3385 ADDOP_I(c, MAP_ADD, gen_index + 1);
3386 break;
3387 default:
3388 return 0;
3389 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 compiler_use_next_block(c, skip);
3392 }
3393 compiler_use_next_block(c, if_cleanup);
3394 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3395 compiler_use_next_block(c, anchor);
3396
3397 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003398}
3399
3400static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003401compiler_comprehension(struct compiler *c, expr_ty e, int type,
3402 identifier name, asdl_seq *generators, expr_ty elt,
3403 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003404{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 PyCodeObject *co = NULL;
3406 expr_ty outermost_iter;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003407 PyObject *qualname = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 outermost_iter = ((comprehension_ty)
3410 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003411
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003412 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3413 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 if (type != COMP_GENEXP) {
3417 int op;
3418 switch (type) {
3419 case COMP_LISTCOMP:
3420 op = BUILD_LIST;
3421 break;
3422 case COMP_SETCOMP:
3423 op = BUILD_SET;
3424 break;
3425 case COMP_DICTCOMP:
3426 op = BUILD_MAP;
3427 break;
3428 default:
3429 PyErr_Format(PyExc_SystemError,
3430 "unknown comprehension type %d", type);
3431 goto error_in_scope;
3432 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 ADDOP_I(c, op, 0);
3435 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 if (!compiler_comprehension_generator(c, generators, 0, elt,
3438 val, type))
3439 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 if (type != COMP_GENEXP) {
3442 ADDOP(c, RETURN_VALUE);
3443 }
3444
3445 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003446 qualname = c->u->u_qualname;
3447 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003449 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 goto error;
3451
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003452 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003454 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 Py_DECREF(co);
3456
3457 VISIT(c, expr, outermost_iter);
3458 ADDOP(c, GET_ITER);
3459 ADDOP_I(c, CALL_FUNCTION, 1);
3460 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003461error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003463error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003464 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 Py_XDECREF(co);
3466 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003467}
3468
3469static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003470compiler_genexp(struct compiler *c, expr_ty e)
3471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 static identifier name;
3473 if (!name) {
3474 name = PyUnicode_FromString("<genexpr>");
3475 if (!name)
3476 return 0;
3477 }
3478 assert(e->kind == GeneratorExp_kind);
3479 return compiler_comprehension(c, e, COMP_GENEXP, name,
3480 e->v.GeneratorExp.generators,
3481 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003482}
3483
3484static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003485compiler_listcomp(struct compiler *c, expr_ty e)
3486{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 static identifier name;
3488 if (!name) {
3489 name = PyUnicode_FromString("<listcomp>");
3490 if (!name)
3491 return 0;
3492 }
3493 assert(e->kind == ListComp_kind);
3494 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3495 e->v.ListComp.generators,
3496 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003497}
3498
3499static int
3500compiler_setcomp(struct compiler *c, expr_ty e)
3501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 static identifier name;
3503 if (!name) {
3504 name = PyUnicode_FromString("<setcomp>");
3505 if (!name)
3506 return 0;
3507 }
3508 assert(e->kind == SetComp_kind);
3509 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3510 e->v.SetComp.generators,
3511 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003512}
3513
3514
3515static int
3516compiler_dictcomp(struct compiler *c, expr_ty e)
3517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 static identifier name;
3519 if (!name) {
3520 name = PyUnicode_FromString("<dictcomp>");
3521 if (!name)
3522 return 0;
3523 }
3524 assert(e->kind == DictComp_kind);
3525 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3526 e->v.DictComp.generators,
3527 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003528}
3529
3530
3531static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532compiler_visit_keyword(struct compiler *c, keyword_ty k)
3533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3535 VISIT(c, expr, k->value);
3536 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003537}
3538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003539/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003540 whether they are true or false.
3541
3542 Return values: 1 for true, 0 for false, -1 for non-constant.
3543 */
3544
3545static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003546expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 char *id;
3549 switch (e->kind) {
3550 case Ellipsis_kind:
3551 return 1;
3552 case Num_kind:
3553 return PyObject_IsTrue(e->v.Num.n);
3554 case Str_kind:
3555 return PyObject_IsTrue(e->v.Str.s);
3556 case Name_kind:
3557 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003558 id = PyUnicode_AsUTF8(e->v.Name.id);
Benjamin Peterson442f2092012-12-06 17:41:04 -05003559 if (id && strcmp(id, "__debug__") == 0)
3560 return !c->c_optimize;
3561 return -1;
3562 case NameConstant_kind: {
3563 PyObject *o = e->v.NameConstant.value;
3564 if (o == Py_None)
3565 return 0;
3566 else if (o == Py_True)
3567 return 1;
3568 else if (o == Py_False)
3569 return 0;
3570 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 default:
3572 return -1;
3573 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003574}
3575
Yury Selivanov75445082015-05-11 22:57:16 -04003576
3577/*
3578 Implements the async with statement.
3579
3580 The semantics outlined in that PEP are as follows:
3581
3582 async with EXPR as VAR:
3583 BLOCK
3584
3585 It is implemented roughly as:
3586
3587 context = EXPR
3588 exit = context.__aexit__ # not calling it
3589 value = await context.__aenter__()
3590 try:
3591 VAR = value # if VAR present in the syntax
3592 BLOCK
3593 finally:
3594 if an exception was raised:
3595 exc = copy of (exception, instance, traceback)
3596 else:
3597 exc = (None, None, None)
3598 if not (await exit(*exc)):
3599 raise
3600 */
3601static int
3602compiler_async_with(struct compiler *c, stmt_ty s, int pos)
3603{
3604 basicblock *block, *finally;
3605 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
3606
3607 assert(s->kind == AsyncWith_kind);
3608
3609 block = compiler_new_block(c);
3610 finally = compiler_new_block(c);
3611 if (!block || !finally)
3612 return 0;
3613
3614 /* Evaluate EXPR */
3615 VISIT(c, expr, item->context_expr);
3616
3617 ADDOP(c, BEFORE_ASYNC_WITH);
3618 ADDOP(c, GET_AWAITABLE);
3619 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3620 ADDOP(c, YIELD_FROM);
3621
3622 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
3623
3624 /* SETUP_ASYNC_WITH pushes a finally block. */
3625 compiler_use_next_block(c, block);
3626 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3627 return 0;
3628 }
3629
3630 if (item->optional_vars) {
3631 VISIT(c, expr, item->optional_vars);
3632 }
3633 else {
3634 /* Discard result from context.__aenter__() */
3635 ADDOP(c, POP_TOP);
3636 }
3637
3638 pos++;
3639 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
3640 /* BLOCK code */
3641 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
3642 else if (!compiler_async_with(c, s, pos))
3643 return 0;
3644
3645 /* End of try block; start the finally block */
3646 ADDOP(c, POP_BLOCK);
3647 compiler_pop_fblock(c, FINALLY_TRY, block);
3648
3649 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3650 compiler_use_next_block(c, finally);
3651 if (!compiler_push_fblock(c, FINALLY_END, finally))
3652 return 0;
3653
3654 /* Finally block starts; context.__exit__ is on the stack under
3655 the exception or return information. Just issue our magic
3656 opcode. */
3657 ADDOP(c, WITH_CLEANUP_START);
3658
3659 ADDOP(c, GET_AWAITABLE);
3660 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3661 ADDOP(c, YIELD_FROM);
3662
3663 ADDOP(c, WITH_CLEANUP_FINISH);
3664
3665 /* Finally block ends. */
3666 ADDOP(c, END_FINALLY);
3667 compiler_pop_fblock(c, FINALLY_END, finally);
3668 return 1;
3669}
3670
3671
Guido van Rossumc2e20742006-02-27 22:32:47 +00003672/*
3673 Implements the with statement from PEP 343.
3674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003676
3677 with EXPR as VAR:
3678 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679
Guido van Rossumc2e20742006-02-27 22:32:47 +00003680 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681
Thomas Wouters477c8d52006-05-27 19:21:47 +00003682 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003683 exit = context.__exit__ # not calling it
3684 value = context.__enter__()
3685 try:
3686 VAR = value # if VAR present in the syntax
3687 BLOCK
3688 finally:
3689 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03003690 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003691 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03003692 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003693 exit(*exc)
3694 */
3695static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003696compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003697{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003698 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003699 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003700
3701 assert(s->kind == With_kind);
3702
Guido van Rossumc2e20742006-02-27 22:32:47 +00003703 block = compiler_new_block(c);
3704 finally = compiler_new_block(c);
3705 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003706 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003707
Thomas Wouters477c8d52006-05-27 19:21:47 +00003708 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003709 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003710 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003711
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003712 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003713 compiler_use_next_block(c, block);
3714 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003715 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003716 }
3717
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003718 if (item->optional_vars) {
3719 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003720 }
3721 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003723 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003724 }
3725
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003726 pos++;
3727 if (pos == asdl_seq_LEN(s->v.With.items))
3728 /* BLOCK code */
3729 VISIT_SEQ(c, stmt, s->v.With.body)
3730 else if (!compiler_with(c, s, pos))
3731 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003732
3733 /* End of try block; start the finally block */
3734 ADDOP(c, POP_BLOCK);
3735 compiler_pop_fblock(c, FINALLY_TRY, block);
3736
3737 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3738 compiler_use_next_block(c, finally);
3739 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003740 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003741
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003742 /* Finally block starts; context.__exit__ is on the stack under
3743 the exception or return information. Just issue our magic
3744 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04003745 ADDOP(c, WITH_CLEANUP_START);
3746 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003747
3748 /* Finally block ends. */
3749 ADDOP(c, END_FINALLY);
3750 compiler_pop_fblock(c, FINALLY_END, finally);
3751 return 1;
3752}
3753
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003754static int
3755compiler_visit_expr(struct compiler *c, expr_ty e)
3756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 /* If expr e has a different line number than the last expr/stmt,
3758 set a new line number for the next instruction.
3759 */
3760 if (e->lineno > c->u->u_lineno) {
3761 c->u->u_lineno = e->lineno;
3762 c->u->u_lineno_set = 0;
3763 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003764 /* Updating the column offset is always harmless. */
3765 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 switch (e->kind) {
3767 case BoolOp_kind:
3768 return compiler_boolop(c, e);
3769 case BinOp_kind:
3770 VISIT(c, expr, e->v.BinOp.left);
3771 VISIT(c, expr, e->v.BinOp.right);
3772 ADDOP(c, binop(c, e->v.BinOp.op));
3773 break;
3774 case UnaryOp_kind:
3775 VISIT(c, expr, e->v.UnaryOp.operand);
3776 ADDOP(c, unaryop(e->v.UnaryOp.op));
3777 break;
3778 case Lambda_kind:
3779 return compiler_lambda(c, e);
3780 case IfExp_kind:
3781 return compiler_ifexp(c, e);
3782 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003783 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003785 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 case GeneratorExp_kind:
3787 return compiler_genexp(c, e);
3788 case ListComp_kind:
3789 return compiler_listcomp(c, e);
3790 case SetComp_kind:
3791 return compiler_setcomp(c, e);
3792 case DictComp_kind:
3793 return compiler_dictcomp(c, e);
3794 case Yield_kind:
3795 if (c->u->u_ste->ste_type != FunctionBlock)
3796 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003797 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3798 return compiler_error(c, "'yield' inside async function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003799 if (e->v.Yield.value) {
3800 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 }
3802 else {
3803 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3804 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003805 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003807 case YieldFrom_kind:
3808 if (c->u->u_ste->ste_type != FunctionBlock)
3809 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003810
3811 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3812 return compiler_error(c, "'yield from' inside async function");
3813
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003814 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003815 ADDOP(c, GET_YIELD_FROM_ITER);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003816 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3817 ADDOP(c, YIELD_FROM);
3818 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003819 case Await_kind:
3820 if (c->u->u_ste->ste_type != FunctionBlock)
3821 return compiler_error(c, "'await' outside function");
3822
Yury Selivanov9dec0352015-06-30 12:49:04 -04003823 if (c->u->u_scope_type == COMPILER_SCOPE_COMPREHENSION)
3824 return compiler_error(
3825 c, "'await' expressions in comprehensions are not supported");
3826
Yury Selivanov75445082015-05-11 22:57:16 -04003827 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
3828 return compiler_error(c, "'await' outside async function");
3829
3830 VISIT(c, expr, e->v.Await.value);
3831 ADDOP(c, GET_AWAITABLE);
3832 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3833 ADDOP(c, YIELD_FROM);
3834 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 case Compare_kind:
3836 return compiler_compare(c, e);
3837 case Call_kind:
3838 return compiler_call(c, e);
3839 case Num_kind:
3840 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3841 break;
3842 case Str_kind:
3843 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3844 break;
3845 case Bytes_kind:
3846 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3847 break;
3848 case Ellipsis_kind:
3849 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3850 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05003851 case NameConstant_kind:
3852 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3853 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 /* The following exprs can be assignment targets. */
3855 case Attribute_kind:
3856 if (e->v.Attribute.ctx != AugStore)
3857 VISIT(c, expr, e->v.Attribute.value);
3858 switch (e->v.Attribute.ctx) {
3859 case AugLoad:
3860 ADDOP(c, DUP_TOP);
3861 /* Fall through to load */
3862 case Load:
3863 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3864 break;
3865 case AugStore:
3866 ADDOP(c, ROT_TWO);
3867 /* Fall through to save */
3868 case Store:
3869 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3870 break;
3871 case Del:
3872 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3873 break;
3874 case Param:
3875 default:
3876 PyErr_SetString(PyExc_SystemError,
3877 "param invalid in attribute expression");
3878 return 0;
3879 }
3880 break;
3881 case Subscript_kind:
3882 switch (e->v.Subscript.ctx) {
3883 case AugLoad:
3884 VISIT(c, expr, e->v.Subscript.value);
3885 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3886 break;
3887 case Load:
3888 VISIT(c, expr, e->v.Subscript.value);
3889 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3890 break;
3891 case AugStore:
3892 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3893 break;
3894 case Store:
3895 VISIT(c, expr, e->v.Subscript.value);
3896 VISIT_SLICE(c, e->v.Subscript.slice, Store);
3897 break;
3898 case Del:
3899 VISIT(c, expr, e->v.Subscript.value);
3900 VISIT_SLICE(c, e->v.Subscript.slice, Del);
3901 break;
3902 case Param:
3903 default:
3904 PyErr_SetString(PyExc_SystemError,
3905 "param invalid in subscript expression");
3906 return 0;
3907 }
3908 break;
3909 case Starred_kind:
3910 switch (e->v.Starred.ctx) {
3911 case Store:
3912 /* In all legitimate cases, the Starred node was already replaced
3913 * by compiler_list/compiler_tuple. XXX: is that okay? */
3914 return compiler_error(c,
3915 "starred assignment target must be in a list or tuple");
3916 default:
3917 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003918 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 }
3920 break;
3921 case Name_kind:
3922 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3923 /* child nodes of List and Tuple will have expr_context set */
3924 case List_kind:
3925 return compiler_list(c, e);
3926 case Tuple_kind:
3927 return compiler_tuple(c, e);
3928 }
3929 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003930}
3931
3932static int
3933compiler_augassign(struct compiler *c, stmt_ty s)
3934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 expr_ty e = s->v.AugAssign.target;
3936 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 switch (e->kind) {
3941 case Attribute_kind:
3942 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
3943 AugLoad, e->lineno, e->col_offset, c->c_arena);
3944 if (auge == NULL)
3945 return 0;
3946 VISIT(c, expr, auge);
3947 VISIT(c, expr, s->v.AugAssign.value);
3948 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3949 auge->v.Attribute.ctx = AugStore;
3950 VISIT(c, expr, auge);
3951 break;
3952 case Subscript_kind:
3953 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
3954 AugLoad, e->lineno, e->col_offset, c->c_arena);
3955 if (auge == NULL)
3956 return 0;
3957 VISIT(c, expr, auge);
3958 VISIT(c, expr, s->v.AugAssign.value);
3959 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3960 auge->v.Subscript.ctx = AugStore;
3961 VISIT(c, expr, auge);
3962 break;
3963 case Name_kind:
3964 if (!compiler_nameop(c, e->v.Name.id, Load))
3965 return 0;
3966 VISIT(c, expr, s->v.AugAssign.value);
3967 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
3968 return compiler_nameop(c, e->v.Name.id, Store);
3969 default:
3970 PyErr_Format(PyExc_SystemError,
3971 "invalid node type (%d) for augmented assignment",
3972 e->kind);
3973 return 0;
3974 }
3975 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003976}
3977
3978static int
3979compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 struct fblockinfo *f;
3982 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3983 PyErr_SetString(PyExc_SystemError,
3984 "too many statically nested blocks");
3985 return 0;
3986 }
3987 f = &c->u->u_fblock[c->u->u_nfblocks++];
3988 f->fb_type = t;
3989 f->fb_block = b;
3990 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003991}
3992
3993static void
3994compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 struct compiler_unit *u = c->u;
3997 assert(u->u_nfblocks > 0);
3998 u->u_nfblocks--;
3999 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4000 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004001}
4002
Thomas Wouters89f507f2006-12-13 04:49:30 +00004003static int
4004compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 int i;
4006 struct compiler_unit *u = c->u;
4007 for (i = 0; i < u->u_nfblocks; ++i) {
4008 if (u->u_fblock[i].fb_type == LOOP)
4009 return 1;
4010 }
4011 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004012}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004013/* Raises a SyntaxError and returns 0.
4014 If something goes wrong, a different exception may be raised.
4015*/
4016
4017static int
4018compiler_error(struct compiler *c, const char *errstr)
4019{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004020 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004022
Victor Stinner14e461d2013-08-26 22:28:21 +02004023 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 if (!loc) {
4025 Py_INCREF(Py_None);
4026 loc = Py_None;
4027 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004028 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004029 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 if (!u)
4031 goto exit;
4032 v = Py_BuildValue("(zO)", errstr, u);
4033 if (!v)
4034 goto exit;
4035 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004036 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 Py_DECREF(loc);
4038 Py_XDECREF(u);
4039 Py_XDECREF(v);
4040 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004041}
4042
4043static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044compiler_handle_subscr(struct compiler *c, const char *kind,
4045 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004046{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 /* XXX this code is duplicated */
4050 switch (ctx) {
4051 case AugLoad: /* fall through to Load */
4052 case Load: op = BINARY_SUBSCR; break;
4053 case AugStore:/* fall through to Store */
4054 case Store: op = STORE_SUBSCR; break;
4055 case Del: op = DELETE_SUBSCR; break;
4056 case Param:
4057 PyErr_Format(PyExc_SystemError,
4058 "invalid %s kind %d in subscript\n",
4059 kind, ctx);
4060 return 0;
4061 }
4062 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004063 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 }
4065 else if (ctx == AugStore) {
4066 ADDOP(c, ROT_THREE);
4067 }
4068 ADDOP(c, op);
4069 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004070}
4071
4072static int
4073compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 int n = 2;
4076 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 /* only handles the cases where BUILD_SLICE is emitted */
4079 if (s->v.Slice.lower) {
4080 VISIT(c, expr, s->v.Slice.lower);
4081 }
4082 else {
4083 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4084 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 if (s->v.Slice.upper) {
4087 VISIT(c, expr, s->v.Slice.upper);
4088 }
4089 else {
4090 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4091 }
4092
4093 if (s->v.Slice.step) {
4094 n++;
4095 VISIT(c, expr, s->v.Slice.step);
4096 }
4097 ADDOP_I(c, BUILD_SLICE, n);
4098 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004099}
4100
4101static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4103 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 switch (s->kind) {
4106 case Slice_kind:
4107 return compiler_slice(c, s, ctx);
4108 case Index_kind:
4109 VISIT(c, expr, s->v.Index.value);
4110 break;
4111 case ExtSlice_kind:
4112 default:
4113 PyErr_SetString(PyExc_SystemError,
4114 "extended slice invalid in nested slice");
4115 return 0;
4116 }
4117 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004118}
4119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004120static int
4121compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4122{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004123 char * kindname = NULL;
4124 switch (s->kind) {
4125 case Index_kind:
4126 kindname = "index";
4127 if (ctx != AugStore) {
4128 VISIT(c, expr, s->v.Index.value);
4129 }
4130 break;
4131 case Slice_kind:
4132 kindname = "slice";
4133 if (ctx != AugStore) {
4134 if (!compiler_slice(c, s, ctx))
4135 return 0;
4136 }
4137 break;
4138 case ExtSlice_kind:
4139 kindname = "extended slice";
4140 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004141 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 for (i = 0; i < n; i++) {
4143 slice_ty sub = (slice_ty)asdl_seq_GET(
4144 s->v.ExtSlice.dims, i);
4145 if (!compiler_visit_nested_slice(c, sub, ctx))
4146 return 0;
4147 }
4148 ADDOP_I(c, BUILD_TUPLE, n);
4149 }
4150 break;
4151 default:
4152 PyErr_Format(PyExc_SystemError,
4153 "invalid subscript kind %d", s->kind);
4154 return 0;
4155 }
4156 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004157}
4158
Thomas Wouters89f507f2006-12-13 04:49:30 +00004159/* End of the compiler section, beginning of the assembler section */
4160
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004161/* do depth-first search of basic block graph, starting with block.
4162 post records the block indices in post-order.
4163
4164 XXX must handle implicit jumps from one block to next
4165*/
4166
Thomas Wouters89f507f2006-12-13 04:49:30 +00004167struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 PyObject *a_bytecode; /* string containing bytecode */
4169 int a_offset; /* offset into bytecode */
4170 int a_nblocks; /* number of reachable blocks */
4171 basicblock **a_postorder; /* list of blocks in dfs postorder */
4172 PyObject *a_lnotab; /* string containing lnotab */
4173 int a_lnotab_off; /* offset into lnotab */
4174 int a_lineno; /* last lineno of emitted instruction */
4175 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004176};
4177
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004178static void
4179dfs(struct compiler *c, basicblock *b, struct assembler *a)
4180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 int i;
4182 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 if (b->b_seen)
4185 return;
4186 b->b_seen = 1;
4187 if (b->b_next != NULL)
4188 dfs(c, b->b_next, a);
4189 for (i = 0; i < b->b_iused; i++) {
4190 instr = &b->b_instr[i];
4191 if (instr->i_jrel || instr->i_jabs)
4192 dfs(c, instr->i_target, a);
4193 }
4194 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004195}
4196
Neal Norwitz2744c6c2005-11-13 01:08:38 +00004197static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004198stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
4199{
Larry Hastings3a907972013-11-23 14:49:22 -08004200 int i, target_depth, effect;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 struct instr *instr;
4202 if (b->b_seen || b->b_startdepth >= depth)
4203 return maxdepth;
4204 b->b_seen = 1;
4205 b->b_startdepth = depth;
4206 for (i = 0; i < b->b_iused; i++) {
4207 instr = &b->b_instr[i];
Larry Hastings3a907972013-11-23 14:49:22 -08004208 effect = PyCompile_OpcodeStackEffect(instr->i_opcode, instr->i_oparg);
4209 if (effect == PY_INVALID_STACK_EFFECT) {
4210 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
4211 Py_FatalError("PyCompile_OpcodeStackEffect()");
4212 }
4213 depth += effect;
4214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 if (depth > maxdepth)
4216 maxdepth = depth;
4217 assert(depth >= 0); /* invalid code or bug in stackdepth() */
4218 if (instr->i_jrel || instr->i_jabs) {
4219 target_depth = depth;
4220 if (instr->i_opcode == FOR_ITER) {
4221 target_depth = depth-2;
Antoine Pitrou99614052014-05-23 11:46:03 +02004222 }
4223 else if (instr->i_opcode == SETUP_FINALLY ||
4224 instr->i_opcode == SETUP_EXCEPT) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 target_depth = depth+3;
4226 if (target_depth > maxdepth)
4227 maxdepth = target_depth;
4228 }
Antoine Pitrou99614052014-05-23 11:46:03 +02004229 else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||
4230 instr->i_opcode == JUMP_IF_FALSE_OR_POP)
4231 depth = depth - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 maxdepth = stackdepth_walk(c, instr->i_target,
4233 target_depth, maxdepth);
4234 if (instr->i_opcode == JUMP_ABSOLUTE ||
4235 instr->i_opcode == JUMP_FORWARD) {
4236 goto out; /* remaining code is dead */
4237 }
4238 }
4239 }
4240 if (b->b_next)
4241 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004242out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 b->b_seen = 0;
4244 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004245}
4246
4247/* Find the flow path that needs the largest stack. We assume that
4248 * cycles in the flow graph have no net effect on the stack depth.
4249 */
4250static int
4251stackdepth(struct compiler *c)
4252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 basicblock *b, *entryblock;
4254 entryblock = NULL;
4255 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4256 b->b_seen = 0;
4257 b->b_startdepth = INT_MIN;
4258 entryblock = b;
4259 }
4260 if (!entryblock)
4261 return 0;
4262 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004263}
4264
4265static int
4266assemble_init(struct assembler *a, int nblocks, int firstlineno)
4267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 memset(a, 0, sizeof(struct assembler));
4269 a->a_lineno = firstlineno;
4270 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4271 if (!a->a_bytecode)
4272 return 0;
4273 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4274 if (!a->a_lnotab)
4275 return 0;
Christian Heimes724b8282013-12-04 08:42:46 +01004276 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 PyErr_NoMemory();
4278 return 0;
4279 }
4280 a->a_postorder = (basicblock **)PyObject_Malloc(
4281 sizeof(basicblock *) * nblocks);
4282 if (!a->a_postorder) {
4283 PyErr_NoMemory();
4284 return 0;
4285 }
4286 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004287}
4288
4289static void
4290assemble_free(struct assembler *a)
4291{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004292 Py_XDECREF(a->a_bytecode);
4293 Py_XDECREF(a->a_lnotab);
4294 if (a->a_postorder)
4295 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004296}
4297
4298/* Return the size of a basic block in bytes. */
4299
4300static int
4301instrsize(struct instr *instr)
4302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 if (!instr->i_hasarg)
4304 return 1; /* 1 byte for the opcode*/
4305 if (instr->i_oparg > 0xffff)
4306 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (oparg) + 2(oparg extended) */
4307 return 3; /* 1 (opcode) + 2 (oparg) */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004308}
4309
4310static int
4311blocksize(basicblock *b)
4312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 int i;
4314 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 for (i = 0; i < b->b_iused; i++)
4317 size += instrsize(&b->b_instr[i]);
4318 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004319}
4320
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004321/* Appends a pair to the end of the line number table, a_lnotab, representing
4322 the instruction's bytecode offset and line number. See
4323 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00004324
Guido van Rossumf68d8e52001-04-14 17:55:09 +00004325static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004326assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004329 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 d_bytecode = a->a_offset - a->a_lineno_off;
4333 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 assert(d_bytecode >= 0);
4336 assert(d_lineno >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 if(d_bytecode == 0 && d_lineno == 0)
4339 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00004340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 if (d_bytecode > 255) {
4342 int j, nbytes, ncodes = d_bytecode / 255;
4343 nbytes = a->a_lnotab_off + 2 * ncodes;
4344 len = PyBytes_GET_SIZE(a->a_lnotab);
4345 if (nbytes >= len) {
4346 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
4347 len = nbytes;
4348 else if (len <= INT_MAX / 2)
4349 len *= 2;
4350 else {
4351 PyErr_NoMemory();
4352 return 0;
4353 }
4354 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4355 return 0;
4356 }
4357 lnotab = (unsigned char *)
4358 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4359 for (j = 0; j < ncodes; j++) {
4360 *lnotab++ = 255;
4361 *lnotab++ = 0;
4362 }
4363 d_bytecode -= ncodes * 255;
4364 a->a_lnotab_off += ncodes * 2;
4365 }
4366 assert(d_bytecode <= 255);
4367 if (d_lineno > 255) {
4368 int j, nbytes, ncodes = d_lineno / 255;
4369 nbytes = a->a_lnotab_off + 2 * ncodes;
4370 len = PyBytes_GET_SIZE(a->a_lnotab);
4371 if (nbytes >= len) {
4372 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
4373 len = nbytes;
4374 else if (len <= INT_MAX / 2)
4375 len *= 2;
4376 else {
4377 PyErr_NoMemory();
4378 return 0;
4379 }
4380 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4381 return 0;
4382 }
4383 lnotab = (unsigned char *)
4384 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4385 *lnotab++ = d_bytecode;
4386 *lnotab++ = 255;
4387 d_bytecode = 0;
4388 for (j = 1; j < ncodes; j++) {
4389 *lnotab++ = 0;
4390 *lnotab++ = 255;
4391 }
4392 d_lineno -= ncodes * 255;
4393 a->a_lnotab_off += ncodes * 2;
4394 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 len = PyBytes_GET_SIZE(a->a_lnotab);
4397 if (a->a_lnotab_off + 2 >= len) {
4398 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
4399 return 0;
4400 }
4401 lnotab = (unsigned char *)
4402 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 a->a_lnotab_off += 2;
4405 if (d_bytecode) {
4406 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004407 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 }
4409 else { /* First line of a block; def stmt, etc. */
4410 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004411 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 }
4413 a->a_lineno = i->i_lineno;
4414 a->a_lineno_off = a->a_offset;
4415 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004416}
4417
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004418/* assemble_emit()
4419 Extend the bytecode with a new instruction.
4420 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00004421*/
4422
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004423static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004424assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 int size, arg = 0, ext = 0;
4427 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4428 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 size = instrsize(i);
4431 if (i->i_hasarg) {
4432 arg = i->i_oparg;
4433 ext = arg >> 16;
4434 }
4435 if (i->i_lineno && !assemble_lnotab(a, i))
4436 return 0;
4437 if (a->a_offset + size >= len) {
4438 if (len > PY_SSIZE_T_MAX / 2)
4439 return 0;
4440 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4441 return 0;
4442 }
4443 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4444 a->a_offset += size;
4445 if (size == 6) {
4446 assert(i->i_hasarg);
4447 *code++ = (char)EXTENDED_ARG;
4448 *code++ = ext & 0xff;
4449 *code++ = ext >> 8;
4450 arg &= 0xffff;
4451 }
4452 *code++ = i->i_opcode;
4453 if (i->i_hasarg) {
4454 assert(size == 3 || size == 6);
4455 *code++ = arg & 0xff;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004456 *code++ = arg >> 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 }
4458 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004459}
4460
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004461static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004462assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 basicblock *b;
4465 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count;
4466 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00004467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 /* Compute the size of each block and fixup jump args.
4469 Replace block pointer with position in bytecode. */
4470 do {
4471 totsize = 0;
4472 for (i = a->a_nblocks - 1; i >= 0; i--) {
4473 b = a->a_postorder[i];
4474 bsize = blocksize(b);
4475 b->b_offset = totsize;
4476 totsize += bsize;
4477 }
4478 last_extended_arg_count = extended_arg_count;
4479 extended_arg_count = 0;
4480 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4481 bsize = b->b_offset;
4482 for (i = 0; i < b->b_iused; i++) {
4483 struct instr *instr = &b->b_instr[i];
4484 /* Relative jumps are computed relative to
4485 the instruction pointer after fetching
4486 the jump instruction.
4487 */
4488 bsize += instrsize(instr);
4489 if (instr->i_jabs)
4490 instr->i_oparg = instr->i_target->b_offset;
4491 else if (instr->i_jrel) {
4492 int delta = instr->i_target->b_offset - bsize;
4493 instr->i_oparg = delta;
4494 }
4495 else
4496 continue;
4497 if (instr->i_oparg > 0xffff)
4498 extended_arg_count++;
4499 }
4500 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 /* XXX: This is an awful hack that could hurt performance, but
4503 on the bright side it should work until we come up
4504 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 The issue is that in the first loop blocksize() is called
4507 which calls instrsize() which requires i_oparg be set
4508 appropriately. There is a bootstrap problem because
4509 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 So we loop until we stop seeing new EXTENDED_ARGs.
4512 The only EXTENDED_ARGs that could be popping up are
4513 ones in jump instructions. So this should converge
4514 fairly quickly.
4515 */
4516 } while (last_extended_arg_count != extended_arg_count);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004517}
4518
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004519static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01004520dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 PyObject *tuple, *k, *v;
4523 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 tuple = PyTuple_New(size);
4526 if (tuple == NULL)
4527 return NULL;
4528 while (PyDict_Next(dict, &pos, &k, &v)) {
4529 i = PyLong_AS_LONG(v);
Victor Stinner3cdd5fb2016-01-22 12:33:12 +01004530 /* The keys of the dictionary are tuples. (see compiler_add_o
4531 * and _PyCode_ConstantKey). The object we want is always second,
4532 * though. */
4533 k = PyTuple_GET_ITEM(k, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 Py_INCREF(k);
4535 assert((i - offset) < size);
4536 assert((i - offset) >= 0);
4537 PyTuple_SET_ITEM(tuple, i - offset, k);
4538 }
4539 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004540}
4541
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004542static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004543compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004546 int flags = 0;
4547 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04004549 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 if (ste->ste_nested)
4551 flags |= CO_NESTED;
4552 if (ste->ste_generator)
4553 flags |= CO_GENERATOR;
4554 if (ste->ste_varargs)
4555 flags |= CO_VARARGS;
4556 if (ste->ste_varkeywords)
4557 flags |= CO_VARKEYWORDS;
4558 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 /* (Only) inherit compilerflags in PyCF_MASK */
4561 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 n = PyDict_Size(c->u->u_freevars);
4564 if (n < 0)
4565 return -1;
4566 if (n == 0) {
4567 n = PyDict_Size(c->u->u_cellvars);
4568 if (n < 0)
Victor Stinnerad9a0662013-11-19 22:23:20 +01004569 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 if (n == 0) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004571 flags |= CO_NOFREE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 }
4573 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004576}
4577
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004578static PyCodeObject *
4579makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 PyObject *tmp;
4582 PyCodeObject *co = NULL;
4583 PyObject *consts = NULL;
4584 PyObject *names = NULL;
4585 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 PyObject *name = NULL;
4587 PyObject *freevars = NULL;
4588 PyObject *cellvars = NULL;
4589 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004590 Py_ssize_t nlocals;
4591 int nlocals_int;
4592 int flags;
Victor Stinnerf8e32212013-11-19 23:56:34 +01004593 int argcount, kwonlyargcount;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 tmp = dict_keys_inorder(c->u->u_consts, 0);
4596 if (!tmp)
4597 goto error;
4598 consts = PySequence_List(tmp); /* optimize_code requires a list */
4599 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 names = dict_keys_inorder(c->u->u_names, 0);
4602 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4603 if (!consts || !names || !varnames)
4604 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4607 if (!cellvars)
4608 goto error;
4609 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4610 if (!freevars)
4611 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 nlocals = PyDict_Size(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01004614 assert(nlocals < INT_MAX);
4615 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
4616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 flags = compute_code_flags(c);
4618 if (flags < 0)
4619 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4622 if (!bytecode)
4623 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4626 if (!tmp)
4627 goto error;
4628 Py_DECREF(consts);
4629 consts = tmp;
4630
Victor Stinnerf8e32212013-11-19 23:56:34 +01004631 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
4632 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
4633 co = PyCode_New(argcount, kwonlyargcount,
Victor Stinnerad9a0662013-11-19 22:23:20 +01004634 nlocals_int, stackdepth(c), flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 bytecode, consts, names, varnames,
4636 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02004637 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 c->u->u_firstlineno,
4639 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004640 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 Py_XDECREF(consts);
4642 Py_XDECREF(names);
4643 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 Py_XDECREF(name);
4645 Py_XDECREF(freevars);
4646 Py_XDECREF(cellvars);
4647 Py_XDECREF(bytecode);
4648 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004649}
4650
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004651
4652/* For debugging purposes only */
4653#if 0
4654static void
4655dump_instr(const struct instr *i)
4656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 const char *jrel = i->i_jrel ? "jrel " : "";
4658 const char *jabs = i->i_jabs ? "jabs " : "";
4659 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 *arg = '\0';
4662 if (i->i_hasarg)
4663 sprintf(arg, "arg: %d ", i->i_oparg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4666 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004667}
4668
4669static void
4670dump_basicblock(const basicblock *b)
4671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 const char *seen = b->b_seen ? "seen " : "";
4673 const char *b_return = b->b_return ? "return " : "";
4674 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4675 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4676 if (b->b_instr) {
4677 int i;
4678 for (i = 0; i < b->b_iused; i++) {
4679 fprintf(stderr, " [%02d] ", i);
4680 dump_instr(b->b_instr + i);
4681 }
4682 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004683}
4684#endif
4685
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004686static PyCodeObject *
4687assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 basicblock *b, *entryblock;
4690 struct assembler a;
4691 int i, j, nblocks;
4692 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 /* Make sure every block that falls off the end returns None.
4695 XXX NEXT_BLOCK() isn't quite right, because if the last
4696 block ends with a jump or return b_next shouldn't set.
4697 */
4698 if (!c->u->u_curblock->b_return) {
4699 NEXT_BLOCK(c);
4700 if (addNone)
4701 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4702 ADDOP(c, RETURN_VALUE);
4703 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 nblocks = 0;
4706 entryblock = NULL;
4707 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4708 nblocks++;
4709 entryblock = b;
4710 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004712 /* Set firstlineno if it wasn't explicitly set. */
4713 if (!c->u->u_firstlineno) {
4714 if (entryblock && entryblock->b_instr)
4715 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4716 else
4717 c->u->u_firstlineno = 1;
4718 }
4719 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4720 goto error;
4721 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 /* Can't modify the bytecode after computing jump offsets. */
4724 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 /* Emit code in reverse postorder from dfs. */
4727 for (i = a.a_nblocks - 1; i >= 0; i--) {
4728 b = a.a_postorder[i];
4729 for (j = 0; j < b->b_iused; j++)
4730 if (!assemble_emit(&a, &b->b_instr[j]))
4731 goto error;
4732 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4735 goto error;
4736 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4737 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004740 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 assemble_free(&a);
4742 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004743}
Georg Brandl8334fd92010-12-04 10:26:46 +00004744
4745#undef PyAST_Compile
4746PyAPI_FUNC(PyCodeObject *)
4747PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4748 PyArena *arena)
4749{
4750 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4751}
4752