blob: ffde903d651c670dee77452412efc848fa79de75 [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"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030032#include "wordcode_helpers.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034#define DEFAULT_BLOCK_SIZE 16
35#define DEFAULT_BLOCKS 8
36#define DEFAULT_CODE_SIZE 128
37#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000038
Nick Coghlan650f0d02007-04-15 12:05:43 +000039#define COMP_GENEXP 0
40#define COMP_LISTCOMP 1
41#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000042#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000043
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000044struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 unsigned i_jabs : 1;
46 unsigned i_jrel : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 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 int compiler_error(struct compiler *, const char *);
175static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
176
177static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
178static int compiler_visit_stmt(struct compiler *, stmt_ty);
179static int compiler_visit_keyword(struct compiler *, keyword_ty);
180static int compiler_visit_expr(struct compiler *, expr_ty);
181static int compiler_augassign(struct compiler *, stmt_ty);
182static int compiler_visit_slice(struct compiler *, slice_ty,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 expr_context_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184
185static int compiler_push_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000186 basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187static void compiler_pop_fblock(struct compiler *, enum fblocktype,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 basicblock *);
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189/* Returns true if there is a loop on the fblock stack. */
190static int compiler_in_loop(struct compiler *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000191
192static int inplace_binop(struct compiler *, operator_ty);
Georg Brandl8334fd92010-12-04 10:26:46 +0000193static int expr_constant(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500195static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400196static int compiler_async_with(struct compiler *, stmt_ty, int);
197static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100198static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400200 asdl_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500201static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400202static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000203
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204static PyCodeObject *assemble(struct compiler *, int addNone);
205static PyObject *__doc__;
206
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400207#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000208
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000210_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 /* Name mangling: __private becomes _classname__private.
213 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200214 PyObject *result;
215 size_t nlen, plen, ipriv;
216 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200218 PyUnicode_READ_CHAR(ident, 0) != '_' ||
219 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 Py_INCREF(ident);
221 return ident;
222 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200223 nlen = PyUnicode_GET_LENGTH(ident);
224 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 The only time a name with a dot can occur is when
228 we are compiling an import statement that has a
229 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 TODO(jhylton): Decide whether we want to support
232 mangling of the module name, e.g. __M.X.
233 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200234 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
235 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
236 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 Py_INCREF(ident);
238 return ident; /* Don't mangle __whatever__ */
239 }
240 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200241 ipriv = 0;
242 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
243 ipriv++;
244 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 Py_INCREF(ident);
246 return ident; /* Don't mangle if class is just underscores */
247 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200248 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000249
Antoine Pitrou55bff892013-04-06 21:21:04 +0200250 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
251 PyErr_SetString(PyExc_OverflowError,
252 "private identifier too large to be mangled");
253 return NULL;
254 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000255
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200256 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
257 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
258 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
259
260 result = PyUnicode_New(1 + nlen + plen, maxchar);
261 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200263 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
264 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200265 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
266 Py_DECREF(result);
267 return NULL;
268 }
269 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
270 Py_DECREF(result);
271 return NULL;
272 }
Victor Stinner8f825062012-04-27 13:55:39 +0200273 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200274 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000275}
276
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277static int
278compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 c->c_stack = PyList_New(0);
283 if (!c->c_stack)
284 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000287}
288
289PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200290PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
291 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 struct compiler c;
294 PyCodeObject *co = NULL;
295 PyCompilerFlags local_flags;
296 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 if (!__doc__) {
299 __doc__ = PyUnicode_InternFromString("__doc__");
300 if (!__doc__)
301 return NULL;
302 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 if (!compiler_init(&c))
305 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200306 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 c.c_filename = filename;
308 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200309 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 if (c.c_future == NULL)
311 goto finally;
312 if (!flags) {
313 local_flags.cf_flags = 0;
314 flags = &local_flags;
315 }
316 merged = c.c_future->ff_features | flags->cf_flags;
317 c.c_future->ff_features = merged;
318 flags->cf_flags = merged;
319 c.c_flags = flags;
Georg Brandl8334fd92010-12-04 10:26:46 +0000320 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 c.c_nestlevel = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322
Victor Stinner14e461d2013-08-26 22:28:21 +0200323 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 if (c.c_st == NULL) {
325 if (!PyErr_Occurred())
326 PyErr_SetString(PyExc_SystemError, "no symtable");
327 goto finally;
328 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000331
Thomas Wouters1175c432006-02-27 22:49:54 +0000332 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 compiler_free(&c);
334 assert(co || PyErr_Occurred());
335 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000336}
337
338PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200339PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
340 int optimize, PyArena *arena)
341{
342 PyObject *filename;
343 PyCodeObject *co;
344 filename = PyUnicode_DecodeFSDefault(filename_str);
345 if (filename == NULL)
346 return NULL;
347 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
348 Py_DECREF(filename);
349 return co;
350
351}
352
353PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354PyNode_Compile(struct _node *n, const char *filename)
355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 PyCodeObject *co = NULL;
357 mod_ty mod;
358 PyArena *arena = PyArena_New();
359 if (!arena)
360 return NULL;
361 mod = PyAST_FromNode(n, NULL, filename, arena);
362 if (mod)
363 co = PyAST_Compile(mod, filename, NULL, arena);
364 PyArena_Free(arena);
365 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000366}
367
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000368static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (c->c_st)
372 PySymtable_Free(c->c_st);
373 if (c->c_future)
374 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200375 Py_XDECREF(c->c_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000377}
378
Guido van Rossum79f25d91997-04-29 20:08:16 +0000379static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000381{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 Py_ssize_t i, n;
383 PyObject *v, *k;
384 PyObject *dict = PyDict_New();
385 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 n = PyList_Size(list);
388 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100389 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 if (!v) {
391 Py_DECREF(dict);
392 return NULL;
393 }
394 k = PyList_GET_ITEM(list, i);
Victor Stinnerefb24132016-01-22 12:33:12 +0100395 k = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
397 Py_XDECREF(k);
398 Py_DECREF(v);
399 Py_DECREF(dict);
400 return NULL;
401 }
402 Py_DECREF(k);
403 Py_DECREF(v);
404 }
405 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406}
407
408/* Return new dict containing names from src that match scope(s).
409
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000410src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000412values are integers, starting at offset and increasing by one for
413each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414*/
415
416static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100417dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700419 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500421 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 assert(offset >= 0);
424 if (dest == NULL)
425 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000426
Meador Inge2ca63152012-07-18 14:20:11 -0500427 /* Sort the keys so that we have a deterministic order on the indexes
428 saved in the returned dictionary. These indexes are used as indexes
429 into the free and cell var storage. Therefore if they aren't
430 deterministic, then the generated bytecode is not deterministic.
431 */
432 sorted_keys = PyDict_Keys(src);
433 if (sorted_keys == NULL)
434 return NULL;
435 if (PyList_Sort(sorted_keys) != 0) {
436 Py_DECREF(sorted_keys);
437 return NULL;
438 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500439 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500440
441 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 /* XXX this should probably be a macro in symtable.h */
443 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500444 k = PyList_GET_ITEM(sorted_keys, key_i);
445 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 assert(PyLong_Check(v));
447 vi = PyLong_AS_LONG(v);
448 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (scope == scope_type || vi & flag) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100451 PyObject *tuple, *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500453 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 Py_DECREF(dest);
455 return NULL;
456 }
457 i++;
Victor Stinnerefb24132016-01-22 12:33:12 +0100458 tuple = _PyCode_ConstantKey(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500460 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 Py_DECREF(item);
462 Py_DECREF(dest);
463 Py_XDECREF(tuple);
464 return NULL;
465 }
466 Py_DECREF(item);
467 Py_DECREF(tuple);
468 }
469 }
Meador Inge2ca63152012-07-18 14:20:11 -0500470 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000472}
473
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474static void
475compiler_unit_check(struct compiler_unit *u)
476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 basicblock *block;
478 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Victor Stinner976bb402016-03-23 11:36:19 +0100479 assert((Py_uintptr_t)block != 0xcbcbcbcbU);
480 assert((Py_uintptr_t)block != 0xfbfbfbfbU);
481 assert((Py_uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (block->b_instr != NULL) {
483 assert(block->b_ialloc > 0);
484 assert(block->b_iused > 0);
485 assert(block->b_ialloc >= block->b_iused);
486 }
487 else {
488 assert (block->b_iused == 0);
489 assert (block->b_ialloc == 0);
490 }
491 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492}
493
494static void
495compiler_unit_free(struct compiler_unit *u)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 compiler_unit_check(u);
500 b = u->u_blocks;
501 while (b != NULL) {
502 if (b->b_instr)
503 PyObject_Free((void *)b->b_instr);
504 next = b->b_list;
505 PyObject_Free((void *)b);
506 b = next;
507 }
508 Py_CLEAR(u->u_ste);
509 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400510 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 Py_CLEAR(u->u_consts);
512 Py_CLEAR(u->u_names);
513 Py_CLEAR(u->u_varnames);
514 Py_CLEAR(u->u_freevars);
515 Py_CLEAR(u->u_cellvars);
516 Py_CLEAR(u->u_private);
517 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518}
519
520static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100521compiler_enter_scope(struct compiler *c, identifier name,
522 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100525 basicblock *block;
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 Stinnerefb24132016-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++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100623
624 block = compiler_new_block(c);
625 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100627 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400629 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
630 if (!compiler_set_qualname(c))
631 return 0;
632 }
633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635}
636
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000637static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638compiler_exit_scope(struct compiler *c)
639{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100640 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 c->c_nestlevel--;
644 compiler_unit_free(c->u);
645 /* Restore c->u to the parent unit. */
646 n = PyList_GET_SIZE(c->c_stack) - 1;
647 if (n >= 0) {
648 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400649 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 assert(c->u);
651 /* we are deleting from a list so this really shouldn't fail */
652 if (PySequence_DelItem(c->c_stack, n) < 0)
653 Py_FatalError("compiler_exit_scope()");
654 compiler_unit_check(c->u);
655 }
656 else
657 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659}
660
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400661static int
662compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100663{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100664 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400665 _Py_static_string(dot_locals, ".<locals>");
666 Py_ssize_t stack_size;
667 struct compiler_unit *u = c->u;
668 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100669
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400670 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100671 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400672 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400673 if (stack_size > 1) {
674 int scope, force_global = 0;
675 struct compiler_unit *parent;
676 PyObject *mangled, *capsule;
677
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400678 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400679 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400680 assert(parent);
681
Yury Selivanov75445082015-05-11 22:57:16 -0400682 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
683 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
684 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400685 assert(u->u_name);
686 mangled = _Py_Mangle(parent->u_private, u->u_name);
687 if (!mangled)
688 return 0;
689 scope = PyST_GetScope(parent->u_ste, mangled);
690 Py_DECREF(mangled);
691 assert(scope != GLOBAL_IMPLICIT);
692 if (scope == GLOBAL_EXPLICIT)
693 force_global = 1;
694 }
695
696 if (!force_global) {
697 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400698 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400699 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
700 dot_locals_str = _PyUnicode_FromId(&dot_locals);
701 if (dot_locals_str == NULL)
702 return 0;
703 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
704 if (base == NULL)
705 return 0;
706 }
707 else {
708 Py_INCREF(parent->u_qualname);
709 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400710 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100711 }
712 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400713
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400714 if (base != NULL) {
715 dot_str = _PyUnicode_FromId(&dot);
716 if (dot_str == NULL) {
717 Py_DECREF(base);
718 return 0;
719 }
720 name = PyUnicode_Concat(base, dot_str);
721 Py_DECREF(base);
722 if (name == NULL)
723 return 0;
724 PyUnicode_Append(&name, u->u_name);
725 if (name == NULL)
726 return 0;
727 }
728 else {
729 Py_INCREF(u->u_name);
730 name = u->u_name;
731 }
732 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100733
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400734 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100735}
736
Eric V. Smith235a6f02015-09-19 14:51:32 -0400737
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000738/* Allocate a new block and return a pointer to it.
739 Returns NULL on error.
740*/
741
742static basicblock *
743compiler_new_block(struct compiler *c)
744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 basicblock *b;
746 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 u = c->u;
749 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
750 if (b == NULL) {
751 PyErr_NoMemory();
752 return NULL;
753 }
754 memset((void *)b, 0, sizeof(basicblock));
755 /* Extend the singly linked list of blocks with new block. */
756 b->b_list = u->u_blocks;
757 u->u_blocks = b;
758 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759}
760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000762compiler_next_block(struct compiler *c)
763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 basicblock *block = compiler_new_block(c);
765 if (block == NULL)
766 return NULL;
767 c->u->u_curblock->b_next = block;
768 c->u->u_curblock = block;
769 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000770}
771
772static basicblock *
773compiler_use_next_block(struct compiler *c, basicblock *block)
774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 assert(block != NULL);
776 c->u->u_curblock->b_next = block;
777 c->u->u_curblock = block;
778 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779}
780
781/* Returns the offset of the next instruction in the current block's
782 b_instr array. Resizes the b_instr as necessary.
783 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000784*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
786static int
787compiler_next_instr(struct compiler *c, basicblock *b)
788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 assert(b != NULL);
790 if (b->b_instr == NULL) {
791 b->b_instr = (struct instr *)PyObject_Malloc(
792 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
793 if (b->b_instr == NULL) {
794 PyErr_NoMemory();
795 return -1;
796 }
797 b->b_ialloc = DEFAULT_BLOCK_SIZE;
798 memset((char *)b->b_instr, 0,
799 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
800 }
801 else if (b->b_iused == b->b_ialloc) {
802 struct instr *tmp;
803 size_t oldsize, newsize;
804 oldsize = b->b_ialloc * sizeof(struct instr);
805 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 if (oldsize > (PY_SIZE_MAX >> 1)) {
808 PyErr_NoMemory();
809 return -1;
810 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 if (newsize == 0) {
813 PyErr_NoMemory();
814 return -1;
815 }
816 b->b_ialloc <<= 1;
817 tmp = (struct instr *)PyObject_Realloc(
818 (void *)b->b_instr, newsize);
819 if (tmp == NULL) {
820 PyErr_NoMemory();
821 return -1;
822 }
823 b->b_instr = tmp;
824 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
825 }
826 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000827}
828
Christian Heimes2202f872008-02-06 14:31:34 +0000829/* Set the i_lineno member of the instruction at offset off if the
830 line number for the current expression/statement has not
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000831 already been set. If it has been set, the call has no effect.
832
Christian Heimes2202f872008-02-06 14:31:34 +0000833 The line number is reset in the following cases:
834 - when entering a new scope
835 - on each statement
836 - on each expression that start a new line
837 - before the "except" clause
838 - before the "for" and "while" expressions
Thomas Wouters89f507f2006-12-13 04:49:30 +0000839*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841static void
842compiler_set_lineno(struct compiler *c, int off)
843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 basicblock *b;
845 if (c->u->u_lineno_set)
846 return;
847 c->u->u_lineno_set = 1;
848 b = c->u->u_curblock;
849 b->b_instr[off].i_lineno = c->u->u_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850}
851
Larry Hastings3a907972013-11-23 14:49:22 -0800852int
853PyCompile_OpcodeStackEffect(int opcode, int oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 switch (opcode) {
856 case POP_TOP:
857 return -1;
858 case ROT_TWO:
859 case ROT_THREE:
860 return 0;
861 case DUP_TOP:
862 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000863 case DUP_TOP_TWO:
864 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 case UNARY_POSITIVE:
867 case UNARY_NEGATIVE:
868 case UNARY_NOT:
869 case UNARY_INVERT:
870 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 case SET_ADD:
873 case LIST_APPEND:
874 return -1;
875 case MAP_ADD:
876 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 case BINARY_POWER:
879 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400880 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 case BINARY_MODULO:
882 case BINARY_ADD:
883 case BINARY_SUBTRACT:
884 case BINARY_SUBSCR:
885 case BINARY_FLOOR_DIVIDE:
886 case BINARY_TRUE_DIVIDE:
887 return -1;
888 case INPLACE_FLOOR_DIVIDE:
889 case INPLACE_TRUE_DIVIDE:
890 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 case INPLACE_ADD:
893 case INPLACE_SUBTRACT:
894 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400895 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 case INPLACE_MODULO:
897 return -1;
898 case STORE_SUBSCR:
899 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 case DELETE_SUBSCR:
901 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 case BINARY_LSHIFT:
904 case BINARY_RSHIFT:
905 case BINARY_AND:
906 case BINARY_XOR:
907 case BINARY_OR:
908 return -1;
909 case INPLACE_POWER:
910 return -1;
911 case GET_ITER:
912 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 case PRINT_EXPR:
915 return -1;
916 case LOAD_BUILD_CLASS:
917 return 1;
918 case INPLACE_LSHIFT:
919 case INPLACE_RSHIFT:
920 case INPLACE_AND:
921 case INPLACE_XOR:
922 case INPLACE_OR:
923 return -1;
924 case BREAK_LOOP:
925 return 0;
926 case SETUP_WITH:
927 return 7;
Yury Selivanov75445082015-05-11 22:57:16 -0400928 case WITH_CLEANUP_START:
929 return 1;
930 case WITH_CLEANUP_FINISH:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 return -1; /* XXX Sometimes more */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 case RETURN_VALUE:
933 return -1;
934 case IMPORT_STAR:
935 return -1;
936 case YIELD_VALUE:
937 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500938 case YIELD_FROM:
939 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 case POP_BLOCK:
941 return 0;
942 case POP_EXCEPT:
943 return 0; /* -3 except if bad bytecode */
944 case END_FINALLY:
945 return -1; /* or -2 or -3 if exception occurred */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000947 case STORE_NAME:
948 return -1;
949 case DELETE_NAME:
950 return 0;
951 case UNPACK_SEQUENCE:
952 return oparg-1;
953 case UNPACK_EX:
954 return (oparg&0xFF) + (oparg>>8);
955 case FOR_ITER:
956 return 1; /* or -1, at end of iterator */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 case STORE_ATTR:
959 return -2;
960 case DELETE_ATTR:
961 return -1;
962 case STORE_GLOBAL:
963 return -1;
964 case DELETE_GLOBAL:
965 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 case LOAD_CONST:
967 return 1;
968 case LOAD_NAME:
969 return 1;
970 case BUILD_TUPLE:
971 case BUILD_LIST:
972 case BUILD_SET:
973 return 1-oparg;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400974 case BUILD_LIST_UNPACK:
975 case BUILD_TUPLE_UNPACK:
976 case BUILD_SET_UNPACK:
977 case BUILD_MAP_UNPACK:
978 return 1 - oparg;
979 case BUILD_MAP_UNPACK_WITH_CALL:
980 return 1 - (oparg & 0xFF);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -0700982 return 1 - 2*oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 case LOAD_ATTR:
984 return 0;
985 case COMPARE_OP:
986 return -1;
987 case IMPORT_NAME:
988 return -1;
989 case IMPORT_FROM:
990 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 case JUMP_FORWARD:
993 case JUMP_IF_TRUE_OR_POP: /* -1 if jump not taken */
994 case JUMP_IF_FALSE_OR_POP: /* "" */
995 case JUMP_ABSOLUTE:
996 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 case POP_JUMP_IF_FALSE:
999 case POP_JUMP_IF_TRUE:
1000 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 case LOAD_GLOBAL:
1003 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 case CONTINUE_LOOP:
1006 return 0;
1007 case SETUP_LOOP:
1008 return 0;
1009 case SETUP_EXCEPT:
1010 case SETUP_FINALLY:
1011 return 6; /* can push 3 values for the new exception
1012 + 3 others for the previous exception state */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 case LOAD_FAST:
1015 return 1;
1016 case STORE_FAST:
1017 return -1;
1018 case DELETE_FAST:
1019 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 case RAISE_VARARGS:
1022 return -oparg;
Neal Norwitzc1505362006-12-28 06:47:50 +00001023#define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 case CALL_FUNCTION:
1025 return -NARGS(oparg);
1026 case CALL_FUNCTION_VAR:
1027 case CALL_FUNCTION_KW:
1028 return -NARGS(oparg)-1;
1029 case CALL_FUNCTION_VAR_KW:
1030 return -NARGS(oparg)-2;
1031 case MAKE_FUNCTION:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001032 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 case MAKE_CLOSURE:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001034 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001035#undef NARGS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case BUILD_SLICE:
1037 if (oparg == 3)
1038 return -2;
1039 else
1040 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 case LOAD_CLOSURE:
1043 return 1;
1044 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001045 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 return 1;
1047 case STORE_DEREF:
1048 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001049 case DELETE_DEREF:
1050 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001051 case GET_AWAITABLE:
1052 return 0;
1053 case SETUP_ASYNC_WITH:
1054 return 6;
1055 case BEFORE_ASYNC_WITH:
1056 return 1;
1057 case GET_AITER:
1058 return 0;
1059 case GET_ANEXT:
1060 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001061 case GET_YIELD_FROM_ITER:
1062 return 0;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001063 case FORMAT_VALUE:
1064 /* If there's a fmt_spec on the stack, we go from 2->1,
1065 else 1->1. */
1066 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001068 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 }
Larry Hastings3a907972013-11-23 14:49:22 -08001070 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071}
1072
1073/* Add an opcode with no argument.
1074 Returns 0 on failure, 1 on success.
1075*/
1076
1077static int
1078compiler_addop(struct compiler *c, int opcode)
1079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 basicblock *b;
1081 struct instr *i;
1082 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001083 assert(!HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 off = compiler_next_instr(c, c->u->u_curblock);
1085 if (off < 0)
1086 return 0;
1087 b = c->u->u_curblock;
1088 i = &b->b_instr[off];
1089 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001090 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (opcode == RETURN_VALUE)
1092 b->b_return = 1;
1093 compiler_set_lineno(c, off);
1094 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095}
1096
Victor Stinnerf8e32212013-11-19 23:56:34 +01001097static Py_ssize_t
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 PyObject *t, *v;
1101 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102
Victor Stinnerefb24132016-01-22 12:33:12 +01001103 t = _PyCode_ConstantKey(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 if (t == NULL)
1105 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 v = PyDict_GetItem(dict, t);
1108 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001109 if (PyErr_Occurred()) {
1110 Py_DECREF(t);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001112 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 arg = PyDict_Size(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001114 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 if (!v) {
1116 Py_DECREF(t);
1117 return -1;
1118 }
1119 if (PyDict_SetItem(dict, t, v) < 0) {
1120 Py_DECREF(t);
1121 Py_DECREF(v);
1122 return -1;
1123 }
1124 Py_DECREF(v);
1125 }
1126 else
1127 arg = PyLong_AsLong(v);
1128 Py_DECREF(t);
1129 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
1131
1132static int
1133compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001135{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001136 Py_ssize_t arg = compiler_add_o(c, dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001138 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001139 return compiler_addop_i(c, opcode, arg);
1140}
1141
1142static int
1143compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001145{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001146 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001147 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1148 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001149 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001150 arg = compiler_add_o(c, dict, mangled);
1151 Py_DECREF(mangled);
1152 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001153 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154 return compiler_addop_i(c, opcode, arg);
1155}
1156
1157/* Add an opcode with an integer argument.
1158 Returns 0 on failure, 1 on success.
1159*/
1160
1161static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001162compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001163{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 struct instr *i;
1165 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001166
Victor Stinner2ad474b2016-03-01 23:34:47 +01001167 /* oparg value is unsigned, but a signed C int is usually used to store
1168 it in the C code (like Python/ceval.c).
1169
1170 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1171
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001172 The argument of a concrete bytecode instruction is limited to 8-bit.
1173 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1174 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001175 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 off = compiler_next_instr(c, c->u->u_curblock);
1178 if (off < 0)
1179 return 0;
1180 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001181 i->i_opcode = opcode;
1182 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 compiler_set_lineno(c, off);
1184 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001185}
1186
1187static int
1188compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 struct instr *i;
1191 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001192
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001193 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 assert(b != NULL);
1195 off = compiler_next_instr(c, c->u->u_curblock);
1196 if (off < 0)
1197 return 0;
1198 i = &c->u->u_curblock->b_instr[off];
1199 i->i_opcode = opcode;
1200 i->i_target = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (absolute)
1202 i->i_jabs = 1;
1203 else
1204 i->i_jrel = 1;
1205 compiler_set_lineno(c, off);
1206 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001207}
1208
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001209/* NEXT_BLOCK() creates an implicit jump from the current block
1210 to the new block.
1211
1212 The returns inside this macro 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*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001215#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (compiler_next_block((C)) == NULL) \
1217 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001218}
1219
1220#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (!compiler_addop((C), (OP))) \
1222 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001223}
1224
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001225#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 if (!compiler_addop((C), (OP))) { \
1227 compiler_exit_scope(c); \
1228 return 0; \
1229 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001230}
1231
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001232#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1234 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001235}
1236
1237#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1239 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001240}
1241
1242#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 if (!compiler_addop_i((C), (OP), (O))) \
1244 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001245}
1246
1247#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 if (!compiler_addop_j((C), (OP), (O), 1)) \
1249 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001250}
1251
1252#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (!compiler_addop_j((C), (OP), (O), 0)) \
1254 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001255}
1256
1257/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1258 the ASDL name to synthesize the name of the C type and the visit function.
1259*/
1260
1261#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (!compiler_visit_ ## TYPE((C), (V))) \
1263 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001264}
1265
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001266#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 if (!compiler_visit_ ## TYPE((C), (V))) { \
1268 compiler_exit_scope(c); \
1269 return 0; \
1270 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001271}
1272
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001273#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (!compiler_visit_slice((C), (V), (CTX))) \
1275 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001276}
1277
1278#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 int _i; \
1280 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1281 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1282 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1283 if (!compiler_visit_ ## TYPE((C), elt)) \
1284 return 0; \
1285 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001286}
1287
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001288#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 int _i; \
1290 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1291 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1292 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1293 if (!compiler_visit_ ## TYPE((C), elt)) { \
1294 compiler_exit_scope(c); \
1295 return 0; \
1296 } \
1297 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001298}
1299
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001300static int
1301compiler_isdocstring(stmt_ty s)
1302{
1303 if (s->kind != Expr_kind)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001304 return 0;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001305 if (s->v.Expr.value->kind == Str_kind)
1306 return 1;
1307 if (s->v.Expr.value->kind == Constant_kind)
1308 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1309 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001310}
1311
1312/* Compile a sequence of statements, checking for a docstring. */
1313
1314static int
1315compiler_body(struct compiler *c, asdl_seq *stmts)
1316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 int i = 0;
1318 stmt_ty st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (!asdl_seq_LEN(stmts))
1321 return 1;
1322 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Georg Brandl8334fd92010-12-04 10:26:46 +00001323 if (compiler_isdocstring(st) && c->c_optimize < 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 /* don't generate docstrings if -OO */
1325 i = 1;
1326 VISIT(c, expr, st->v.Expr.value);
1327 if (!compiler_nameop(c, __doc__, Store))
1328 return 0;
1329 }
1330 for (; i < asdl_seq_LEN(stmts); i++)
1331 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1332 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001333}
1334
1335static PyCodeObject *
1336compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 PyCodeObject *co;
1339 int addNone = 1;
1340 static PyObject *module;
1341 if (!module) {
1342 module = PyUnicode_InternFromString("<module>");
1343 if (!module)
1344 return NULL;
1345 }
1346 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001347 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 return NULL;
1349 switch (mod->kind) {
1350 case Module_kind:
1351 if (!compiler_body(c, mod->v.Module.body)) {
1352 compiler_exit_scope(c);
1353 return 0;
1354 }
1355 break;
1356 case Interactive_kind:
1357 c->c_interactive = 1;
1358 VISIT_SEQ_IN_SCOPE(c, stmt,
1359 mod->v.Interactive.body);
1360 break;
1361 case Expression_kind:
1362 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1363 addNone = 0;
1364 break;
1365 case Suite_kind:
1366 PyErr_SetString(PyExc_SystemError,
1367 "suite should not be possible");
1368 return 0;
1369 default:
1370 PyErr_Format(PyExc_SystemError,
1371 "module kind %d should not be possible",
1372 mod->kind);
1373 return 0;
1374 }
1375 co = assemble(c, addNone);
1376 compiler_exit_scope(c);
1377 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001378}
1379
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380/* The test for LOCAL must come before the test for FREE in order to
1381 handle classes where name is both local and free. The local var is
1382 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001383*/
1384
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001385static int
1386get_ref_type(struct compiler *c, PyObject *name)
1387{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001388 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001389 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1390 !PyUnicode_CompareWithASCIIString(name, "__class__"))
1391 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001392 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (scope == 0) {
1394 char buf[350];
1395 PyOS_snprintf(buf, sizeof(buf),
Victor Stinner14e461d2013-08-26 22:28:21 +02001396 "unknown scope for %.100s in %.100s(%s)\n"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 "symbols: %s\nlocals: %s\nglobals: %s",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001398 PyUnicode_AsUTF8(name),
1399 PyUnicode_AsUTF8(c->u->u_name),
1400 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1401 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1402 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1403 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 );
1405 Py_FatalError(buf);
1406 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001409}
1410
1411static int
1412compiler_lookup_arg(PyObject *dict, PyObject *name)
1413{
1414 PyObject *k, *v;
Victor Stinnerefb24132016-01-22 12:33:12 +01001415 k = _PyCode_ConstantKey(name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001416 if (k == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001417 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001418 v = PyDict_GetItem(dict, k);
Neal Norwitz3715c3e2005-11-24 22:09:18 +00001419 Py_DECREF(k);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001420 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001421 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001422 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423}
1424
1425static int
Victor Stinnerad9a0662013-11-19 22:23:20 +01001426compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001428 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001429 if (qualname == NULL)
1430 qualname = co->co_name;
1431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (free == 0) {
1433 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001434 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 ADDOP_I(c, MAKE_FUNCTION, args);
1436 return 1;
1437 }
1438 for (i = 0; i < free; ++i) {
1439 /* Bypass com_addop_varname because it will generate
1440 LOAD_DEREF but LOAD_CLOSURE is needed.
1441 */
1442 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1443 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 /* Special case: If a class contains a method with a
1446 free variable that has the same name as a method,
1447 the name will be considered free *and* local in the
1448 class. It should be handled by the closure, as
1449 well as by the normal name loookup logic.
1450 */
1451 reftype = get_ref_type(c, name);
1452 if (reftype == CELL)
1453 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1454 else /* (reftype == FREE) */
1455 arg = compiler_lookup_arg(c->u->u_freevars, name);
1456 if (arg == -1) {
1457 fprintf(stderr,
1458 "lookup %s in %s %d %d\n"
1459 "freevars of %s: %s\n",
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001460 PyUnicode_AsUTF8(PyObject_Repr(name)),
1461 PyUnicode_AsUTF8(c->u->u_name),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 reftype, arg,
Serhiy Storchakadf4518c2014-11-18 23:34:33 +02001463 PyUnicode_AsUTF8(co->co_name),
1464 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 Py_FatalError("compiler_make_closure()");
1466 }
1467 ADDOP_I(c, LOAD_CLOSURE, arg);
1468 }
1469 ADDOP_I(c, BUILD_TUPLE, free);
1470 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001471 ADDOP_O(c, LOAD_CONST, qualname, consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 ADDOP_I(c, MAKE_CLOSURE, args);
1473 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
1476static int
1477compiler_decorators(struct compiler *c, asdl_seq* decos)
1478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (!decos)
1482 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1485 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1486 }
1487 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001491compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001494 int i, default_count = 0;
1495 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1496 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1497 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1498 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001499 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1500 if (!mangled)
1501 return -1;
1502 ADDOP_O(c, LOAD_CONST, mangled, consts);
1503 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 if (!compiler_visit_expr(c, default_)) {
1505 return -1;
1506 }
1507 default_count++;
1508 }
1509 }
1510 return default_count;
Guido van Rossum4f72a782006-10-27 23:31:49 +00001511}
1512
1513static int
Neal Norwitzc1505362006-12-28 06:47:50 +00001514compiler_visit_argannotation(struct compiler *c, identifier id,
1515 expr_ty annotation, PyObject *names)
1516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01001518 PyObject *mangled;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 VISIT(c, expr, annotation);
Victor Stinner065efc32014-02-18 22:07:56 +01001520 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001521 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001522 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001523 if (PyList_Append(names, mangled) < 0) {
1524 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001525 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05001526 }
1527 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001529 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001530}
1531
1532static int
1533compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1534 PyObject *names)
1535{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001536 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 for (i = 0; i < asdl_seq_LEN(args); i++) {
1538 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001539 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 c,
1541 arg->arg,
1542 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001543 names))
1544 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001546 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001547}
1548
1549static int
1550compiler_visit_annotations(struct compiler *c, arguments_ty args,
1551 expr_ty returns)
1552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 /* Push arg annotations and a list of the argument names. Return the #
1554 of items pushed. The expressions are evaluated out-of-order wrt the
1555 source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00001556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1558 */
1559 static identifier return_str;
1560 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001561 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 names = PyList_New(0);
1563 if (!names)
1564 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001565
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001566 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001568 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001569 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001570 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001572 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001574 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001575 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001576 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 if (!return_str) {
1580 return_str = PyUnicode_InternFromString("return");
1581 if (!return_str)
1582 goto error;
1583 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03001584 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 goto error;
1586 }
1587
1588 len = PyList_GET_SIZE(names);
1589 if (len > 65534) {
1590 /* len must fit in 16 bits, and len is incremented below */
1591 PyErr_SetString(PyExc_SyntaxError,
1592 "too many annotations");
1593 goto error;
1594 }
1595 if (len) {
1596 /* convert names to a tuple and place on stack */
1597 PyObject *elt;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001598 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 PyObject *s = PyTuple_New(len);
1600 if (!s)
1601 goto error;
1602 for (i = 0; i < len; i++) {
1603 elt = PyList_GET_ITEM(names, i);
1604 Py_INCREF(elt);
1605 PyTuple_SET_ITEM(s, i, elt);
1606 }
1607 ADDOP_O(c, LOAD_CONST, s, consts);
1608 Py_DECREF(s);
1609 len++; /* include the just-pushed tuple */
1610 }
1611 Py_DECREF(names);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001612
1613 /* We just checked that len <= 65535, see above */
1614 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
Neal Norwitzc1505362006-12-28 06:47:50 +00001615
1616error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 Py_DECREF(names);
1618 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001619}
1620
1621static int
Yury Selivanov75445082015-05-11 22:57:16 -04001622compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001625 PyObject *qualname, *first_const = Py_None;
Yury Selivanov75445082015-05-11 22:57:16 -04001626 arguments_ty args;
1627 expr_ty returns;
1628 identifier name;
1629 asdl_seq* decos;
1630 asdl_seq *body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 stmt_ty st;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001632 Py_ssize_t i, n, arglength;
1633 int docstring, kw_default_count = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 int num_annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04001635 int scope_type;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001636
Yury Selivanov75445082015-05-11 22:57:16 -04001637
1638 if (is_async) {
1639 assert(s->kind == AsyncFunctionDef_kind);
1640
1641 args = s->v.AsyncFunctionDef.args;
1642 returns = s->v.AsyncFunctionDef.returns;
1643 decos = s->v.AsyncFunctionDef.decorator_list;
1644 name = s->v.AsyncFunctionDef.name;
1645 body = s->v.AsyncFunctionDef.body;
1646
1647 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1648 } else {
1649 assert(s->kind == FunctionDef_kind);
1650
1651 args = s->v.FunctionDef.args;
1652 returns = s->v.FunctionDef.returns;
1653 decos = s->v.FunctionDef.decorator_list;
1654 name = s->v.FunctionDef.name;
1655 body = s->v.FunctionDef.body;
1656
1657 scope_type = COMPILER_SCOPE_FUNCTION;
1658 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 if (!compiler_decorators(c, decos))
1661 return 0;
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05001662 if (args->defaults)
1663 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (args->kwonlyargs) {
1665 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1666 args->kw_defaults);
1667 if (res < 0)
1668 return 0;
1669 kw_default_count = res;
1670 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 num_annotations = compiler_visit_annotations(c, args, returns);
1672 if (num_annotations < 0)
1673 return 0;
1674 assert((num_annotations & 0xFFFF) == num_annotations);
Guido van Rossum4f72a782006-10-27 23:31:49 +00001675
Yury Selivanov75445082015-05-11 22:57:16 -04001676 if (!compiler_enter_scope(c, name,
1677 scope_type, (void *)s,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 s->lineno))
1679 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680
Yury Selivanov75445082015-05-11 22:57:16 -04001681 st = (stmt_ty)asdl_seq_GET(body, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 docstring = compiler_isdocstring(st);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001683 if (docstring && c->c_optimize < 2) {
1684 if (st->v.Expr.value->kind == Constant_kind)
1685 first_const = st->v.Expr.value->v.Constant.value;
1686 else
1687 first_const = st->v.Expr.value->v.Str.s;
1688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1690 compiler_exit_scope(c);
1691 return 0;
1692 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 c->u->u_argcount = asdl_seq_LEN(args->args);
1695 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
Yury Selivanov75445082015-05-11 22:57:16 -04001696 n = asdl_seq_LEN(body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 /* if there was a docstring, we need to skip the first statement */
1698 for (i = docstring; i < n; i++) {
Yury Selivanov75445082015-05-11 22:57:16 -04001699 st = (stmt_ty)asdl_seq_GET(body, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 VISIT_IN_SCOPE(c, stmt, st);
1701 }
1702 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001703 qualname = c->u->u_qualname;
1704 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001706 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001707 Py_XDECREF(qualname);
1708 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001710 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 arglength = asdl_seq_LEN(args->defaults);
1713 arglength |= kw_default_count << 8;
1714 arglength |= num_annotations << 16;
Yury Selivanovb7666a32015-07-22 14:48:57 +03001715 if (is_async)
1716 co->co_flags |= CO_COROUTINE;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001717 compiler_make_closure(c, co, arglength, qualname);
1718 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 /* decorators */
1722 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1723 ADDOP_I(c, CALL_FUNCTION, 1);
1724 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001725
Yury Selivanov75445082015-05-11 22:57:16 -04001726 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727}
1728
1729static int
1730compiler_class(struct compiler *c, stmt_ty s)
1731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 PyCodeObject *co;
1733 PyObject *str;
1734 int i;
1735 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (!compiler_decorators(c, decos))
1738 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 /* ultimately generate code for:
1741 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
1742 where:
1743 <func> is a function/closure created from the class body;
1744 it has a single argument (__locals__) where the dict
1745 (or MutableSequence) representing the locals is passed
1746 <name> is the class name
1747 <bases> is the positional arguments and *varargs argument
1748 <keywords> is the keyword arguments and **kwds argument
1749 This borrows from compiler_call.
1750 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001753 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1754 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 return 0;
1756 /* this block represents what we do in the new scope */
1757 {
1758 /* use the class name for name mangling */
1759 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03001760 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 /* load (global) __name__ ... */
1762 str = PyUnicode_InternFromString("__name__");
1763 if (!str || !compiler_nameop(c, str, Load)) {
1764 Py_XDECREF(str);
1765 compiler_exit_scope(c);
1766 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 Py_DECREF(str);
1769 /* ... and store it as __module__ */
1770 str = PyUnicode_InternFromString("__module__");
1771 if (!str || !compiler_nameop(c, str, Store)) {
1772 Py_XDECREF(str);
1773 compiler_exit_scope(c);
1774 return 0;
1775 }
1776 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001777 assert(c->u->u_qualname);
1778 ADDOP_O(c, LOAD_CONST, c->u->u_qualname, consts);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001779 str = PyUnicode_InternFromString("__qualname__");
1780 if (!str || !compiler_nameop(c, str, Store)) {
1781 Py_XDECREF(str);
1782 compiler_exit_scope(c);
1783 return 0;
1784 }
1785 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 /* compile the body proper */
1787 if (!compiler_body(c, s->v.ClassDef.body)) {
1788 compiler_exit_scope(c);
1789 return 0;
1790 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001791 if (c->u->u_ste->ste_needs_class_closure) {
1792 /* return the (empty) __class__ cell */
1793 str = PyUnicode_InternFromString("__class__");
1794 if (str == NULL) {
1795 compiler_exit_scope(c);
1796 return 0;
1797 }
1798 i = compiler_lookup_arg(c->u->u_cellvars, str);
1799 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01001800 if (i < 0) {
1801 compiler_exit_scope(c);
1802 return 0;
1803 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001804 assert(i == 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 /* Return the cell where to store __class__ */
1806 ADDOP_I(c, LOAD_CLOSURE, i);
1807 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05001808 else {
1809 assert(PyDict_Size(c->u->u_cellvars) == 0);
1810 /* This happens when nobody references the cell. Return None. */
1811 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1812 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1814 /* create the code object */
1815 co = assemble(c, 1);
1816 }
1817 /* leave the new scope */
1818 compiler_exit_scope(c);
1819 if (co == NULL)
1820 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00001821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 /* 2. load the 'build_class' function */
1823 ADDOP(c, LOAD_BUILD_CLASS);
1824
1825 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001826 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827 Py_DECREF(co);
1828
1829 /* 4. load class name */
1830 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1831
1832 /* 5. generate the rest of the code for the call */
1833 if (!compiler_call_helper(c, 2,
1834 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001835 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 return 0;
1837
1838 /* 6. apply decorators */
1839 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1840 ADDOP_I(c, CALL_FUNCTION, 1);
1841 }
1842
1843 /* 7. store into <name> */
1844 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1845 return 0;
1846 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847}
1848
1849static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001850compiler_ifexp(struct compiler *c, expr_ty e)
1851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 basicblock *end, *next;
1853
1854 assert(e->kind == IfExp_kind);
1855 end = compiler_new_block(c);
1856 if (end == NULL)
1857 return 0;
1858 next = compiler_new_block(c);
1859 if (next == NULL)
1860 return 0;
1861 VISIT(c, expr, e->v.IfExp.test);
1862 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1863 VISIT(c, expr, e->v.IfExp.body);
1864 ADDOP_JREL(c, JUMP_FORWARD, end);
1865 compiler_use_next_block(c, next);
1866 VISIT(c, expr, e->v.IfExp.orelse);
1867 compiler_use_next_block(c, end);
1868 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00001869}
1870
1871static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001872compiler_lambda(struct compiler *c, expr_ty e)
1873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001875 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 static identifier name;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001877 int kw_default_count = 0;
1878 Py_ssize_t arglength;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 arguments_ty args = e->v.Lambda.args;
1880 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 if (!name) {
1883 name = PyUnicode_InternFromString("<lambda>");
1884 if (!name)
1885 return 0;
1886 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887
Benjamin Peterson419d9a82013-02-10 09:48:22 -05001888 if (args->defaults)
1889 VISIT_SEQ(c, expr, args->defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 if (args->kwonlyargs) {
1891 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1892 args->kw_defaults);
1893 if (res < 0) return 0;
1894 kw_default_count = res;
1895 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001896 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001897 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 /* Make None the first constant, so the lambda can't have a
1901 docstring. */
1902 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1903 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 c->u->u_argcount = asdl_seq_LEN(args->args);
1906 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1907 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1908 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001909 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 }
1911 else {
1912 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02001913 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001915 qualname = c->u->u_qualname;
1916 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04001918 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 arglength = asdl_seq_LEN(args->defaults);
1922 arglength |= kw_default_count << 8;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001923 compiler_make_closure(c, co, arglength, qualname);
1924 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 Py_DECREF(co);
1926
1927 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928}
1929
1930static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001931compiler_if(struct compiler *c, stmt_ty s)
1932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 basicblock *end, *next;
1934 int constant;
1935 assert(s->kind == If_kind);
1936 end = compiler_new_block(c);
1937 if (end == NULL)
1938 return 0;
1939
Georg Brandl8334fd92010-12-04 10:26:46 +00001940 constant = expr_constant(c, s->v.If.test);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 /* constant = 0: "if 0"
1942 * constant = 1: "if 1", "if 2", ...
1943 * constant = -1: rest */
1944 if (constant == 0) {
1945 if (s->v.If.orelse)
1946 VISIT_SEQ(c, stmt, s->v.If.orelse);
1947 } else if (constant == 1) {
1948 VISIT_SEQ(c, stmt, s->v.If.body);
1949 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001950 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 next = compiler_new_block(c);
1952 if (next == NULL)
1953 return 0;
1954 }
1955 else
1956 next = end;
1957 VISIT(c, expr, s->v.If.test);
1958 ADDOP_JABS(c, POP_JUMP_IF_FALSE, next);
1959 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02001960 if (asdl_seq_LEN(s->v.If.orelse)) {
1961 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 compiler_use_next_block(c, next);
1963 VISIT_SEQ(c, stmt, s->v.If.orelse);
1964 }
1965 }
1966 compiler_use_next_block(c, end);
1967 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001968}
1969
1970static int
1971compiler_for(struct compiler *c, stmt_ty s)
1972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 start = compiler_new_block(c);
1976 cleanup = compiler_new_block(c);
1977 end = compiler_new_block(c);
1978 if (start == NULL || end == NULL || cleanup == NULL)
1979 return 0;
1980 ADDOP_JREL(c, SETUP_LOOP, end);
1981 if (!compiler_push_fblock(c, LOOP, start))
1982 return 0;
1983 VISIT(c, expr, s->v.For.iter);
1984 ADDOP(c, GET_ITER);
1985 compiler_use_next_block(c, start);
1986 ADDOP_JREL(c, FOR_ITER, cleanup);
1987 VISIT(c, expr, s->v.For.target);
1988 VISIT_SEQ(c, stmt, s->v.For.body);
1989 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1990 compiler_use_next_block(c, cleanup);
1991 ADDOP(c, POP_BLOCK);
1992 compiler_pop_fblock(c, LOOP, start);
1993 VISIT_SEQ(c, stmt, s->v.For.orelse);
1994 compiler_use_next_block(c, end);
1995 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001996}
1997
Yury Selivanov75445082015-05-11 22:57:16 -04001998
1999static int
2000compiler_async_for(struct compiler *c, stmt_ty s)
2001{
2002 static PyObject *stopiter_error = NULL;
2003 basicblock *try, *except, *end, *after_try, *try_cleanup,
2004 *after_loop, *after_loop_else;
2005
2006 if (stopiter_error == NULL) {
2007 stopiter_error = PyUnicode_InternFromString("StopAsyncIteration");
2008 if (stopiter_error == NULL)
2009 return 0;
2010 }
2011
2012 try = compiler_new_block(c);
2013 except = compiler_new_block(c);
2014 end = compiler_new_block(c);
2015 after_try = compiler_new_block(c);
2016 try_cleanup = compiler_new_block(c);
2017 after_loop = compiler_new_block(c);
2018 after_loop_else = compiler_new_block(c);
2019
2020 if (try == NULL || except == NULL || end == NULL
2021 || after_try == NULL || try_cleanup == NULL)
2022 return 0;
2023
2024 ADDOP_JREL(c, SETUP_LOOP, after_loop);
2025 if (!compiler_push_fblock(c, LOOP, try))
2026 return 0;
2027
2028 VISIT(c, expr, s->v.AsyncFor.iter);
2029 ADDOP(c, GET_AITER);
2030 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2031 ADDOP(c, YIELD_FROM);
2032
2033 compiler_use_next_block(c, try);
2034
2035
2036 ADDOP_JREL(c, SETUP_EXCEPT, except);
2037 if (!compiler_push_fblock(c, EXCEPT, try))
2038 return 0;
2039
2040 ADDOP(c, GET_ANEXT);
2041 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2042 ADDOP(c, YIELD_FROM);
2043 VISIT(c, expr, s->v.AsyncFor.target);
2044 ADDOP(c, POP_BLOCK);
2045 compiler_pop_fblock(c, EXCEPT, try);
2046 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2047
2048
2049 compiler_use_next_block(c, except);
2050 ADDOP(c, DUP_TOP);
2051 ADDOP_O(c, LOAD_GLOBAL, stopiter_error, names);
2052 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2053 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2054
2055 ADDOP(c, POP_TOP);
2056 ADDOP(c, POP_TOP);
2057 ADDOP(c, POP_TOP);
2058 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2059 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2060 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2061
2062
2063 compiler_use_next_block(c, try_cleanup);
2064 ADDOP(c, END_FINALLY);
2065
2066 compiler_use_next_block(c, after_try);
2067 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2068 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2069
2070 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2071 compiler_pop_fblock(c, LOOP, try);
2072
2073 compiler_use_next_block(c, after_loop);
2074 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2075
2076 compiler_use_next_block(c, after_loop_else);
2077 VISIT_SEQ(c, stmt, s->v.For.orelse);
2078
2079 compiler_use_next_block(c, end);
2080
2081 return 1;
2082}
2083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002084static int
2085compiler_while(struct compiler *c, stmt_ty s)
2086{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 basicblock *loop, *orelse, *end, *anchor = NULL;
Georg Brandl8334fd92010-12-04 10:26:46 +00002088 int constant = expr_constant(c, s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 if (constant == 0) {
2091 if (s->v.While.orelse)
2092 VISIT_SEQ(c, stmt, s->v.While.orelse);
2093 return 1;
2094 }
2095 loop = compiler_new_block(c);
2096 end = compiler_new_block(c);
2097 if (constant == -1) {
2098 anchor = compiler_new_block(c);
2099 if (anchor == NULL)
2100 return 0;
2101 }
2102 if (loop == NULL || end == NULL)
2103 return 0;
2104 if (s->v.While.orelse) {
2105 orelse = compiler_new_block(c);
2106 if (orelse == NULL)
2107 return 0;
2108 }
2109 else
2110 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 ADDOP_JREL(c, SETUP_LOOP, end);
2113 compiler_use_next_block(c, loop);
2114 if (!compiler_push_fblock(c, LOOP, loop))
2115 return 0;
2116 if (constant == -1) {
2117 VISIT(c, expr, s->v.While.test);
2118 ADDOP_JABS(c, POP_JUMP_IF_FALSE, anchor);
2119 }
2120 VISIT_SEQ(c, stmt, s->v.While.body);
2121 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002123 /* XXX should the two POP instructions be in a separate block
2124 if there is no else clause ?
2125 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002126
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002127 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 compiler_use_next_block(c, anchor);
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002129 ADDOP(c, POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002130 compiler_pop_fblock(c, LOOP, loop);
2131 if (orelse != NULL) /* what if orelse is just pass? */
2132 VISIT_SEQ(c, stmt, s->v.While.orelse);
2133 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002136}
2137
2138static int
2139compiler_continue(struct compiler *c)
2140{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 static const char LOOP_ERROR_MSG[] = "'continue' not properly in loop";
2142 static const char IN_FINALLY_ERROR_MSG[] =
2143 "'continue' not supported inside 'finally' clause";
2144 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 if (!c->u->u_nfblocks)
2147 return compiler_error(c, LOOP_ERROR_MSG);
2148 i = c->u->u_nfblocks - 1;
2149 switch (c->u->u_fblock[i].fb_type) {
2150 case LOOP:
2151 ADDOP_JABS(c, JUMP_ABSOLUTE, c->u->u_fblock[i].fb_block);
2152 break;
2153 case EXCEPT:
2154 case FINALLY_TRY:
2155 while (--i >= 0 && c->u->u_fblock[i].fb_type != LOOP) {
2156 /* Prevent continue anywhere under a finally
2157 even if hidden in a sub-try or except. */
2158 if (c->u->u_fblock[i].fb_type == FINALLY_END)
2159 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2160 }
2161 if (i == -1)
2162 return compiler_error(c, LOOP_ERROR_MSG);
2163 ADDOP_JABS(c, CONTINUE_LOOP, c->u->u_fblock[i].fb_block);
2164 break;
2165 case FINALLY_END:
2166 return compiler_error(c, IN_FINALLY_ERROR_MSG);
2167 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002170}
2171
2172/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173
2174 SETUP_FINALLY L
2175 <code for body>
2176 POP_BLOCK
2177 LOAD_CONST <None>
2178 L: <code for finalbody>
2179 END_FINALLY
2180
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002181 The special instructions use the block stack. Each block
2182 stack entry contains the instruction that created it (here
2183 SETUP_FINALLY), the level of the value stack at the time the
2184 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002186 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002187 Pushes the current value stack level and the label
2188 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002189 POP_BLOCK:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 Pops en entry from the block stack, and pops the value
2191 stack until its level is the same as indicated on the
2192 block stack. (The label is ignored.)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002193 END_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 Pops a variable number of entries from the *value* stack
2195 and re-raises the exception they specify. The number of
2196 entries popped depends on the (pseudo) exception type.
2197
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002198 The block stack is unwound when an exception is raised:
2199 when a SETUP_FINALLY entry is found, the exception is pushed
2200 onto the value stack (and the exception condition is cleared),
2201 and the interpreter jumps to the label gotten from the block
2202 stack.
2203*/
2204
2205static int
2206compiler_try_finally(struct compiler *c, stmt_ty s)
2207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002208 basicblock *body, *end;
2209 body = compiler_new_block(c);
2210 end = compiler_new_block(c);
2211 if (body == NULL || end == NULL)
2212 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 ADDOP_JREL(c, SETUP_FINALLY, end);
2215 compiler_use_next_block(c, body);
2216 if (!compiler_push_fblock(c, FINALLY_TRY, body))
2217 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002218 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2219 if (!compiler_try_except(c, s))
2220 return 0;
2221 }
2222 else {
2223 VISIT_SEQ(c, stmt, s->v.Try.body);
2224 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 ADDOP(c, POP_BLOCK);
2226 compiler_pop_fblock(c, FINALLY_TRY, body);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2229 compiler_use_next_block(c, end);
2230 if (!compiler_push_fblock(c, FINALLY_END, end))
2231 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002232 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 ADDOP(c, END_FINALLY);
2234 compiler_pop_fblock(c, FINALLY_END, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002237}
2238
2239/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002240 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002241 (The contents of the value stack is shown in [], with the top
2242 at the right; 'tb' is trace-back info, 'val' the exception's
2243 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244
2245 Value stack Label Instruction Argument
2246 [] SETUP_EXCEPT L1
2247 [] <code for S>
2248 [] POP_BLOCK
2249 [] JUMP_FORWARD L0
2250
2251 [tb, val, exc] L1: DUP )
2252 [tb, val, exc, exc] <evaluate E1> )
2253 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
2254 [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 )
2255 [tb, val, exc] POP
2256 [tb, val] <assign to V1> (or POP if no V1)
2257 [tb] POP
2258 [] <code for S1>
2259 JUMP_FORWARD L0
2260
2261 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002262 .............................etc.......................
2263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 [tb, val, exc] Ln+1: END_FINALLY # re-raise exception
2265
2266 [] L0: <next statement>
2267
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002268 Of course, parts are not generated if Vi or Ei is not present.
2269*/
2270static int
2271compiler_try_except(struct compiler *c, stmt_ty s)
2272{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002274 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 body = compiler_new_block(c);
2277 except = compiler_new_block(c);
2278 orelse = compiler_new_block(c);
2279 end = compiler_new_block(c);
2280 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
2281 return 0;
2282 ADDOP_JREL(c, SETUP_EXCEPT, except);
2283 compiler_use_next_block(c, body);
2284 if (!compiler_push_fblock(c, EXCEPT, body))
2285 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002286 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 ADDOP(c, POP_BLOCK);
2288 compiler_pop_fblock(c, EXCEPT, body);
2289 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002290 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002291 compiler_use_next_block(c, except);
2292 for (i = 0; i < n; i++) {
2293 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002294 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 if (!handler->v.ExceptHandler.type && i < n-1)
2296 return compiler_error(c, "default 'except:' must be last");
2297 c->u->u_lineno_set = 0;
2298 c->u->u_lineno = handler->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002299 c->u->u_col_offset = handler->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 except = compiler_new_block(c);
2301 if (except == NULL)
2302 return 0;
2303 if (handler->v.ExceptHandler.type) {
2304 ADDOP(c, DUP_TOP);
2305 VISIT(c, expr, handler->v.ExceptHandler.type);
2306 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2307 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
2308 }
2309 ADDOP(c, POP_TOP);
2310 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002311 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00002312
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002313 cleanup_end = compiler_new_block(c);
2314 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002315 if (!(cleanup_end || cleanup_body))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002316 return 0;
Guido van Rossumb940e112007-01-10 16:19:56 +00002317
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002318 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2319 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002321 /*
2322 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002323 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002324 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03002325 try:
2326 # body
2327 finally:
2328 name = None
2329 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002330 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002332 /* second try: */
2333 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2334 compiler_use_next_block(c, cleanup_body);
2335 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2336 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002337
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002338 /* second # body */
2339 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2340 ADDOP(c, POP_BLOCK);
2341 ADDOP(c, POP_EXCEPT);
2342 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002344 /* finally: */
2345 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2346 compiler_use_next_block(c, cleanup_end);
2347 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2348 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002350 /* name = None */
2351 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2352 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002354 /* del name */
2355 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002357 ADDOP(c, END_FINALLY);
2358 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 }
2360 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002361 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002363 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05002364 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002365 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366
Guido van Rossumb940e112007-01-10 16:19:56 +00002367 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002368 ADDOP(c, POP_TOP);
2369 compiler_use_next_block(c, cleanup_body);
2370 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2371 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05002373 ADDOP(c, POP_EXCEPT);
2374 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 }
2376 ADDOP_JREL(c, JUMP_FORWARD, end);
2377 compiler_use_next_block(c, except);
2378 }
2379 ADDOP(c, END_FINALLY);
2380 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002381 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 compiler_use_next_block(c, end);
2383 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002384}
2385
2386static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002387compiler_try(struct compiler *c, stmt_ty s) {
2388 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2389 return compiler_try_finally(c, s);
2390 else
2391 return compiler_try_except(c, s);
2392}
2393
2394
2395static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002396compiler_import_as(struct compiler *c, identifier name, identifier asname)
2397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 /* The IMPORT_NAME opcode was already generated. This function
2399 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 If there is a dot in name, we need to split it and emit a
2402 LOAD_ATTR for each name.
2403 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002404 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2405 PyUnicode_GET_LENGTH(name), 1);
2406 if (dot == -2)
2407 return -1;
2408 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* Consume the base module name to get the first attribute */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002410 Py_ssize_t pos = dot + 1;
2411 while (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 PyObject *attr;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002413 dot = PyUnicode_FindChar(name, '.', pos,
2414 PyUnicode_GET_LENGTH(name), 1);
2415 if (dot == -2)
2416 return -1;
2417 attr = PyUnicode_Substring(name, pos,
2418 (dot != -1) ? dot :
2419 PyUnicode_GET_LENGTH(name));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002420 if (!attr)
2421 return -1;
2422 ADDOP_O(c, LOAD_ATTR, attr, names);
2423 Py_DECREF(attr);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002424 pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 }
2426 }
2427 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002428}
2429
2430static int
2431compiler_import(struct compiler *c, stmt_ty s)
2432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 /* The Import node stores a module name like a.b.c as a single
2434 string. This is convenient for all cases except
2435 import a.b.c as d
2436 where we need to parse that string to extract the individual
2437 module names.
2438 XXX Perhaps change the representation to make this case simpler?
2439 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01002440 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 for (i = 0; i < n; i++) {
2443 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
2444 int r;
2445 PyObject *level;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 level = PyLong_FromLong(0);
2448 if (level == NULL)
2449 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 ADDOP_O(c, LOAD_CONST, level, consts);
2452 Py_DECREF(level);
2453 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2454 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002456 if (alias->asname) {
2457 r = compiler_import_as(c, alias->name, alias->asname);
2458 if (!r)
2459 return r;
2460 }
2461 else {
2462 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002463 Py_ssize_t dot = PyUnicode_FindChar(
2464 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02002465 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002466 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02002467 if (tmp == NULL)
2468 return 0;
2469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002471 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 Py_DECREF(tmp);
2473 }
2474 if (!r)
2475 return r;
2476 }
2477 }
2478 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002479}
2480
2481static int
2482compiler_from_import(struct compiler *c, stmt_ty s)
2483{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002484 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 PyObject *names = PyTuple_New(n);
2487 PyObject *level;
2488 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00002489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 if (!empty_string) {
2491 empty_string = PyUnicode_FromString("");
2492 if (!empty_string)
2493 return 0;
2494 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 if (!names)
2497 return 0;
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 level = PyLong_FromLong(s->v.ImportFrom.level);
2500 if (!level) {
2501 Py_DECREF(names);
2502 return 0;
2503 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 /* build up the names */
2506 for (i = 0; i < n; i++) {
2507 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2508 Py_INCREF(alias->name);
2509 PyTuple_SET_ITEM(names, i, alias->name);
2510 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2513 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) {
2514 Py_DECREF(level);
2515 Py_DECREF(names);
2516 return compiler_error(c, "from __future__ imports must occur "
2517 "at the beginning of the file");
2518 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 ADDOP_O(c, LOAD_CONST, level, consts);
2521 Py_DECREF(level);
2522 ADDOP_O(c, LOAD_CONST, names, consts);
2523 Py_DECREF(names);
2524 if (s->v.ImportFrom.module) {
2525 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2526 }
2527 else {
2528 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2529 }
2530 for (i = 0; i < n; i++) {
2531 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2532 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002533
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002534 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 assert(n == 1);
2536 ADDOP(c, IMPORT_STAR);
2537 return 1;
2538 }
2539
2540 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2541 store_name = alias->name;
2542 if (alias->asname)
2543 store_name = alias->asname;
2544
2545 if (!compiler_nameop(c, store_name, Store)) {
2546 Py_DECREF(names);
2547 return 0;
2548 }
2549 }
2550 /* remove imported module */
2551 ADDOP(c, POP_TOP);
2552 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002553}
2554
2555static int
2556compiler_assert(struct compiler *c, stmt_ty s)
2557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 static PyObject *assertion_error = NULL;
2559 basicblock *end;
Victor Stinner14e461d2013-08-26 22:28:21 +02002560 PyObject* msg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002561
Georg Brandl8334fd92010-12-04 10:26:46 +00002562 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 return 1;
2564 if (assertion_error == NULL) {
2565 assertion_error = PyUnicode_InternFromString("AssertionError");
2566 if (assertion_error == NULL)
2567 return 0;
2568 }
2569 if (s->v.Assert.test->kind == Tuple_kind &&
2570 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
Victor Stinner14e461d2013-08-26 22:28:21 +02002571 msg = PyUnicode_FromString("assertion is always true, "
2572 "perhaps remove parentheses?");
2573 if (msg == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 return 0;
Victor Stinner14e461d2013-08-26 22:28:21 +02002575 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2576 c->c_filename, c->u->u_lineno,
2577 NULL, NULL) == -1) {
2578 Py_DECREF(msg);
2579 return 0;
2580 }
2581 Py_DECREF(msg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 }
2583 VISIT(c, expr, s->v.Assert.test);
2584 end = compiler_new_block(c);
2585 if (end == NULL)
2586 return 0;
2587 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2588 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2589 if (s->v.Assert.msg) {
2590 VISIT(c, expr, s->v.Assert.msg);
2591 ADDOP_I(c, CALL_FUNCTION, 1);
2592 }
2593 ADDOP_I(c, RAISE_VARARGS, 1);
2594 compiler_use_next_block(c, end);
2595 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002596}
2597
2598static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002599compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2600{
2601 if (c->c_interactive && c->c_nestlevel <= 1) {
2602 VISIT(c, expr, value);
2603 ADDOP(c, PRINT_EXPR);
2604 return 1;
2605 }
2606
Victor Stinnera2724092016-02-08 18:17:58 +01002607 switch (value->kind)
2608 {
2609 case Str_kind:
Victor Stinnera2724092016-02-08 18:17:58 +01002610 case Num_kind:
Victor Stinner15a30952016-02-08 22:45:06 +01002611 case Ellipsis_kind:
2612 case Bytes_kind:
Victor Stinnera2724092016-02-08 18:17:58 +01002613 case NameConstant_kind:
2614 case Constant_kind:
Victor Stinner15a30952016-02-08 22:45:06 +01002615 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002616 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002617
Victor Stinnera2724092016-02-08 18:17:58 +01002618 default:
2619 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002620 }
2621
2622 VISIT(c, expr, value);
2623 ADDOP(c, POP_TOP);
2624 return 1;
2625}
2626
2627static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002628compiler_visit_stmt(struct compiler *c, stmt_ty s)
2629{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002630 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 /* Always assign a lineno to the next instruction for a stmt. */
2633 c->u->u_lineno = s->lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00002634 c->u->u_col_offset = s->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 c->u->u_lineno_set = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 switch (s->kind) {
2638 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04002639 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 case ClassDef_kind:
2641 return compiler_class(c, s);
2642 case Return_kind:
2643 if (c->u->u_ste->ste_type != FunctionBlock)
2644 return compiler_error(c, "'return' outside function");
2645 if (s->v.Return.value) {
2646 VISIT(c, expr, s->v.Return.value);
2647 }
2648 else
2649 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2650 ADDOP(c, RETURN_VALUE);
2651 break;
2652 case Delete_kind:
2653 VISIT_SEQ(c, expr, s->v.Delete.targets)
2654 break;
2655 case Assign_kind:
2656 n = asdl_seq_LEN(s->v.Assign.targets);
2657 VISIT(c, expr, s->v.Assign.value);
2658 for (i = 0; i < n; i++) {
2659 if (i < n - 1)
2660 ADDOP(c, DUP_TOP);
2661 VISIT(c, expr,
2662 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2663 }
2664 break;
2665 case AugAssign_kind:
2666 return compiler_augassign(c, s);
2667 case For_kind:
2668 return compiler_for(c, s);
2669 case While_kind:
2670 return compiler_while(c, s);
2671 case If_kind:
2672 return compiler_if(c, s);
2673 case Raise_kind:
2674 n = 0;
2675 if (s->v.Raise.exc) {
2676 VISIT(c, expr, s->v.Raise.exc);
2677 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002678 if (s->v.Raise.cause) {
2679 VISIT(c, expr, s->v.Raise.cause);
2680 n++;
2681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01002683 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002685 case Try_kind:
2686 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 case Assert_kind:
2688 return compiler_assert(c, s);
2689 case Import_kind:
2690 return compiler_import(c, s);
2691 case ImportFrom_kind:
2692 return compiler_from_import(c, s);
2693 case Global_kind:
2694 case Nonlocal_kind:
2695 break;
2696 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01002697 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 case Pass_kind:
2699 break;
2700 case Break_kind:
2701 if (!compiler_in_loop(c))
2702 return compiler_error(c, "'break' outside loop");
2703 ADDOP(c, BREAK_LOOP);
2704 break;
2705 case Continue_kind:
2706 return compiler_continue(c);
2707 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05002708 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04002709 case AsyncFunctionDef_kind:
2710 return compiler_function(c, s, 1);
2711 case AsyncWith_kind:
2712 return compiler_async_with(c, s, 0);
2713 case AsyncFor_kind:
2714 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 }
Yury Selivanov75445082015-05-11 22:57:16 -04002716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002718}
2719
2720static int
2721unaryop(unaryop_ty op)
2722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 switch (op) {
2724 case Invert:
2725 return UNARY_INVERT;
2726 case Not:
2727 return UNARY_NOT;
2728 case UAdd:
2729 return UNARY_POSITIVE;
2730 case USub:
2731 return UNARY_NEGATIVE;
2732 default:
2733 PyErr_Format(PyExc_SystemError,
2734 "unary op %d should not be possible", op);
2735 return 0;
2736 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002737}
2738
2739static int
2740binop(struct compiler *c, operator_ty op)
2741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 switch (op) {
2743 case Add:
2744 return BINARY_ADD;
2745 case Sub:
2746 return BINARY_SUBTRACT;
2747 case Mult:
2748 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002749 case MatMult:
2750 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 case Div:
2752 return BINARY_TRUE_DIVIDE;
2753 case Mod:
2754 return BINARY_MODULO;
2755 case Pow:
2756 return BINARY_POWER;
2757 case LShift:
2758 return BINARY_LSHIFT;
2759 case RShift:
2760 return BINARY_RSHIFT;
2761 case BitOr:
2762 return BINARY_OR;
2763 case BitXor:
2764 return BINARY_XOR;
2765 case BitAnd:
2766 return BINARY_AND;
2767 case FloorDiv:
2768 return BINARY_FLOOR_DIVIDE;
2769 default:
2770 PyErr_Format(PyExc_SystemError,
2771 "binary op %d should not be possible", op);
2772 return 0;
2773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002774}
2775
2776static int
2777cmpop(cmpop_ty op)
2778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 switch (op) {
2780 case Eq:
2781 return PyCmp_EQ;
2782 case NotEq:
2783 return PyCmp_NE;
2784 case Lt:
2785 return PyCmp_LT;
2786 case LtE:
2787 return PyCmp_LE;
2788 case Gt:
2789 return PyCmp_GT;
2790 case GtE:
2791 return PyCmp_GE;
2792 case Is:
2793 return PyCmp_IS;
2794 case IsNot:
2795 return PyCmp_IS_NOT;
2796 case In:
2797 return PyCmp_IN;
2798 case NotIn:
2799 return PyCmp_NOT_IN;
2800 default:
2801 return PyCmp_BAD;
2802 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002803}
2804
2805static int
2806inplace_binop(struct compiler *c, operator_ty op)
2807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 switch (op) {
2809 case Add:
2810 return INPLACE_ADD;
2811 case Sub:
2812 return INPLACE_SUBTRACT;
2813 case Mult:
2814 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04002815 case MatMult:
2816 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 case Div:
2818 return INPLACE_TRUE_DIVIDE;
2819 case Mod:
2820 return INPLACE_MODULO;
2821 case Pow:
2822 return INPLACE_POWER;
2823 case LShift:
2824 return INPLACE_LSHIFT;
2825 case RShift:
2826 return INPLACE_RSHIFT;
2827 case BitOr:
2828 return INPLACE_OR;
2829 case BitXor:
2830 return INPLACE_XOR;
2831 case BitAnd:
2832 return INPLACE_AND;
2833 case FloorDiv:
2834 return INPLACE_FLOOR_DIVIDE;
2835 default:
2836 PyErr_Format(PyExc_SystemError,
2837 "inplace binary op %d should not be possible", op);
2838 return 0;
2839 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002840}
2841
2842static int
2843compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
2844{
Victor Stinnerad9a0662013-11-19 22:23:20 +01002845 int op, scope;
2846 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 PyObject *dict = c->u->u_names;
2850 PyObject *mangled;
2851 /* XXX AugStore isn't used anywhere! */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 mangled = _Py_Mangle(c->u->u_private, name);
2854 if (!mangled)
2855 return 0;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00002856
Benjamin Peterson70b224d2012-12-06 17:49:58 -05002857 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2858 PyUnicode_CompareWithASCIIString(name, "True") &&
2859 PyUnicode_CompareWithASCIIString(name, "False"));
2860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 op = 0;
2862 optype = OP_NAME;
2863 scope = PyST_GetScope(c->u->u_ste, mangled);
2864 switch (scope) {
2865 case FREE:
2866 dict = c->u->u_freevars;
2867 optype = OP_DEREF;
2868 break;
2869 case CELL:
2870 dict = c->u->u_cellvars;
2871 optype = OP_DEREF;
2872 break;
2873 case LOCAL:
2874 if (c->u->u_ste->ste_type == FunctionBlock)
2875 optype = OP_FAST;
2876 break;
2877 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04002878 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 optype = OP_GLOBAL;
2880 break;
2881 case GLOBAL_EXPLICIT:
2882 optype = OP_GLOBAL;
2883 break;
2884 default:
2885 /* scope can be 0 */
2886 break;
2887 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002890 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 switch (optype) {
2893 case OP_DEREF:
2894 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002895 case Load:
2896 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
2897 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 case Store: op = STORE_DEREF; break;
2899 case AugLoad:
2900 case AugStore:
2901 break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002902 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 case Param:
2904 default:
2905 PyErr_SetString(PyExc_SystemError,
2906 "param invalid for deref variable");
2907 return 0;
2908 }
2909 break;
2910 case OP_FAST:
2911 switch (ctx) {
2912 case Load: op = LOAD_FAST; break;
2913 case Store: op = STORE_FAST; break;
2914 case Del: op = DELETE_FAST; break;
2915 case AugLoad:
2916 case AugStore:
2917 break;
2918 case Param:
2919 default:
2920 PyErr_SetString(PyExc_SystemError,
2921 "param invalid for local variable");
2922 return 0;
2923 }
2924 ADDOP_O(c, op, mangled, varnames);
2925 Py_DECREF(mangled);
2926 return 1;
2927 case OP_GLOBAL:
2928 switch (ctx) {
2929 case Load: op = LOAD_GLOBAL; break;
2930 case Store: op = STORE_GLOBAL; break;
2931 case Del: op = DELETE_GLOBAL; break;
2932 case AugLoad:
2933 case AugStore:
2934 break;
2935 case Param:
2936 default:
2937 PyErr_SetString(PyExc_SystemError,
2938 "param invalid for global variable");
2939 return 0;
2940 }
2941 break;
2942 case OP_NAME:
2943 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002944 case Load: op = LOAD_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 case Store: op = STORE_NAME; break;
2946 case Del: op = DELETE_NAME; break;
2947 case AugLoad:
2948 case AugStore:
2949 break;
2950 case Param:
2951 default:
2952 PyErr_SetString(PyExc_SystemError,
2953 "param invalid for name variable");
2954 return 0;
2955 }
2956 break;
2957 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 assert(op);
2960 arg = compiler_add_o(c, dict, mangled);
2961 Py_DECREF(mangled);
2962 if (arg < 0)
2963 return 0;
2964 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002965}
2966
2967static int
2968compiler_boolop(struct compiler *c, expr_ty e)
2969{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002971 int jumpi;
2972 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 assert(e->kind == BoolOp_kind);
2976 if (e->v.BoolOp.op == And)
2977 jumpi = JUMP_IF_FALSE_OR_POP;
2978 else
2979 jumpi = JUMP_IF_TRUE_OR_POP;
2980 end = compiler_new_block(c);
2981 if (end == NULL)
2982 return 0;
2983 s = e->v.BoolOp.values;
2984 n = asdl_seq_LEN(s) - 1;
2985 assert(n >= 0);
2986 for (i = 0; i < n; ++i) {
2987 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2988 ADDOP_JABS(c, jumpi, end);
2989 }
2990 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2991 compiler_use_next_block(c, end);
2992 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002993}
2994
2995static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002996starunpack_helper(struct compiler *c, asdl_seq *elts,
2997 int single_op, int inner_op, int outer_op)
2998{
2999 Py_ssize_t n = asdl_seq_LEN(elts);
3000 Py_ssize_t i, nsubitems = 0, nseen = 0;
3001 for (i = 0; i < n; i++) {
3002 expr_ty elt = asdl_seq_GET(elts, i);
3003 if (elt->kind == Starred_kind) {
3004 if (nseen) {
3005 ADDOP_I(c, inner_op, nseen);
3006 nseen = 0;
3007 nsubitems++;
3008 }
3009 VISIT(c, expr, elt->v.Starred.value);
3010 nsubitems++;
3011 }
3012 else {
3013 VISIT(c, expr, elt);
3014 nseen++;
3015 }
3016 }
3017 if (nsubitems) {
3018 if (nseen) {
3019 ADDOP_I(c, inner_op, nseen);
3020 nsubitems++;
3021 }
3022 ADDOP_I(c, outer_op, nsubitems);
3023 }
3024 else
3025 ADDOP_I(c, single_op, nseen);
3026 return 1;
3027}
3028
3029static int
3030assignment_helper(struct compiler *c, asdl_seq *elts)
3031{
3032 Py_ssize_t n = asdl_seq_LEN(elts);
3033 Py_ssize_t i;
3034 int seen_star = 0;
3035 for (i = 0; i < n; i++) {
3036 expr_ty elt = asdl_seq_GET(elts, i);
3037 if (elt->kind == Starred_kind && !seen_star) {
3038 if ((i >= (1 << 8)) ||
3039 (n-i-1 >= (INT_MAX >> 8)))
3040 return compiler_error(c,
3041 "too many expressions in "
3042 "star-unpacking assignment");
3043 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3044 seen_star = 1;
3045 asdl_seq_SET(elts, i, elt->v.Starred.value);
3046 }
3047 else if (elt->kind == Starred_kind) {
3048 return compiler_error(c,
3049 "two starred expressions in assignment");
3050 }
3051 }
3052 if (!seen_star) {
3053 ADDOP_I(c, UNPACK_SEQUENCE, n);
3054 }
3055 VISIT_SEQ(c, expr, elts);
3056 return 1;
3057}
3058
3059static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003060compiler_list(struct compiler *c, expr_ty e)
3061{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003062 asdl_seq *elts = e->v.List.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003064 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003066 else if (e->v.List.ctx == Load) {
3067 return starunpack_helper(c, elts,
3068 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003070 else
3071 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003073}
3074
3075static int
3076compiler_tuple(struct compiler *c, expr_ty e)
3077{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003078 asdl_seq *elts = e->v.Tuple.elts;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003080 return assignment_helper(c, elts);
3081 }
3082 else if (e->v.Tuple.ctx == Load) {
3083 return starunpack_helper(c, elts,
3084 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
3085 }
3086 else
3087 VISIT_SEQ(c, expr, elts);
3088 return 1;
3089}
3090
3091static int
3092compiler_set(struct compiler *c, expr_ty e)
3093{
3094 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
3095 BUILD_SET, BUILD_SET_UNPACK);
3096}
3097
3098static int
3099compiler_dict(struct compiler *c, expr_ty e)
3100{
Victor Stinner976bb402016-03-23 11:36:19 +01003101 Py_ssize_t i, n, elements;
3102 int containers;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003103 int is_unpacking = 0;
3104 n = asdl_seq_LEN(e->v.Dict.values);
3105 containers = 0;
3106 elements = 0;
3107 for (i = 0; i < n; i++) {
3108 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3109 if (elements == 0xFFFF || (elements && is_unpacking)) {
3110 ADDOP_I(c, BUILD_MAP, elements);
3111 containers++;
3112 elements = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003114 if (is_unpacking) {
3115 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3116 containers++;
3117 }
3118 else {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003119 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Benjamin Petersonee853392015-05-28 14:30:26 -05003120 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003121 elements++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 }
3123 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003124 if (elements || containers == 0) {
3125 ADDOP_I(c, BUILD_MAP, elements);
3126 containers++;
3127 }
3128 /* If there is more than one dict, they need to be merged into a new
3129 * dict. If there is one dict and it's an unpacking, then it needs
3130 * to be copied into a new dict." */
3131 while (containers > 1 || is_unpacking) {
3132 int oparg = containers < 255 ? containers : 255;
3133 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3134 containers -= (oparg - 1);
3135 is_unpacking = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 }
3137 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003138}
3139
3140static int
3141compiler_compare(struct compiler *c, expr_ty e)
3142{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003143 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 basicblock *cleanup = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 /* XXX the logic can be cleaned up for 1 or multiple comparisons */
3147 VISIT(c, expr, e->v.Compare.left);
3148 n = asdl_seq_LEN(e->v.Compare.ops);
3149 assert(n > 0);
3150 if (n > 1) {
3151 cleanup = compiler_new_block(c);
3152 if (cleanup == NULL)
3153 return 0;
3154 VISIT(c, expr,
3155 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
3156 }
3157 for (i = 1; i < n; i++) {
3158 ADDOP(c, DUP_TOP);
3159 ADDOP(c, ROT_THREE);
3160 ADDOP_I(c, COMPARE_OP,
3161 cmpop((cmpop_ty)(asdl_seq_GET(
3162 e->v.Compare.ops, i - 1))));
3163 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3164 NEXT_BLOCK(c);
3165 if (i < (n - 1))
3166 VISIT(c, expr,
3167 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
3168 }
3169 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n - 1));
3170 ADDOP_I(c, COMPARE_OP,
3171 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n - 1))));
3172 if (n > 1) {
3173 basicblock *end = compiler_new_block(c);
3174 if (end == NULL)
3175 return 0;
3176 ADDOP_JREL(c, JUMP_FORWARD, end);
3177 compiler_use_next_block(c, cleanup);
3178 ADDOP(c, ROT_TWO);
3179 ADDOP(c, POP_TOP);
3180 compiler_use_next_block(c, end);
3181 }
3182 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183}
3184
3185static int
3186compiler_call(struct compiler *c, expr_ty e)
3187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 VISIT(c, expr, e->v.Call.func);
3189 return compiler_call_helper(c, 0,
3190 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003191 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003192}
3193
Eric V. Smith235a6f02015-09-19 14:51:32 -04003194static int
3195compiler_joined_str(struct compiler *c, expr_ty e)
3196{
3197 /* Concatenate parts of a string using ''.join(parts). There are
3198 probably better ways of doing this.
3199
3200 This is used for constructs like "'x=' f'{42}'", which have to
3201 be evaluated at compile time. */
3202
3203 static PyObject *empty_string;
3204 static PyObject *join_string;
3205
3206 if (!empty_string) {
3207 empty_string = PyUnicode_FromString("");
3208 if (!empty_string)
3209 return 0;
3210 }
3211 if (!join_string) {
3212 join_string = PyUnicode_FromString("join");
3213 if (!join_string)
3214 return 0;
3215 }
3216
3217 ADDOP_O(c, LOAD_CONST, empty_string, consts);
3218 ADDOP_NAME(c, LOAD_ATTR, join_string, names);
3219 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3220 ADDOP_I(c, BUILD_LIST, asdl_seq_LEN(e->v.JoinedStr.values));
3221 ADDOP_I(c, CALL_FUNCTION, 1);
3222 return 1;
3223}
3224
Eric V. Smitha78c7952015-11-03 12:45:05 -05003225/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003226static int
3227compiler_formatted_value(struct compiler *c, expr_ty e)
3228{
Eric V. Smitha78c7952015-11-03 12:45:05 -05003229 /* Our oparg encodes 2 pieces of information: the conversion
3230 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04003231
Eric V. Smitha78c7952015-11-03 12:45:05 -05003232 Convert the conversion char to 2 bits:
3233 None: 000 0x0 FVC_NONE
3234 !s : 001 0x1 FVC_STR
3235 !r : 010 0x2 FVC_REPR
3236 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04003237
Eric V. Smitha78c7952015-11-03 12:45:05 -05003238 next bit is whether or not we have a format spec:
3239 yes : 100 0x4
3240 no : 000 0x0
3241 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003242
Eric V. Smitha78c7952015-11-03 12:45:05 -05003243 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003244
Eric V. Smitha78c7952015-11-03 12:45:05 -05003245 /* Evaluate the expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003246 VISIT(c, expr, e->v.FormattedValue.value);
3247
Eric V. Smitha78c7952015-11-03 12:45:05 -05003248 switch (e->v.FormattedValue.conversion) {
3249 case 's': oparg = FVC_STR; break;
3250 case 'r': oparg = FVC_REPR; break;
3251 case 'a': oparg = FVC_ASCII; break;
3252 case -1: oparg = FVC_NONE; break;
3253 default:
3254 PyErr_SetString(PyExc_SystemError,
3255 "Unrecognized conversion character");
3256 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003257 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04003258 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003259 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04003260 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003261 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003262 }
3263
Eric V. Smitha78c7952015-11-03 12:45:05 -05003264 /* And push our opcode and oparg */
3265 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith235a6f02015-09-19 14:51:32 -04003266 return 1;
3267}
3268
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003269/* shared code between compiler_call and compiler_class */
3270static int
3271compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01003272 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003273 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003274 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 int code = 0;
Victor Stinner976bb402016-03-23 11:36:19 +01003277 Py_ssize_t nelts, i, nseen;
3278 int nkw;
Guido van Rossum52cc1d82007-03-18 15:41:51 +00003279
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003280 /* the number of tuples and dictionaries on the stack */
3281 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3282
3283 nkw = 0;
3284 nseen = 0; /* the number of positional arguments on the stack */
3285 nelts = asdl_seq_LEN(args);
3286 for (i = 0; i < nelts; i++) {
3287 expr_ty elt = asdl_seq_GET(args, i);
3288 if (elt->kind == Starred_kind) {
3289 /* A star-arg. If we've seen positional arguments,
3290 pack the positional arguments into a
3291 tuple. */
3292 if (nseen) {
3293 ADDOP_I(c, BUILD_TUPLE, nseen);
3294 nseen = 0;
3295 nsubargs++;
3296 }
3297 VISIT(c, expr, elt->v.Starred.value);
3298 nsubargs++;
3299 }
3300 else if (nsubargs) {
3301 /* We've seen star-args already, so we
3302 count towards items-to-pack-into-tuple. */
3303 VISIT(c, expr, elt);
3304 nseen++;
3305 }
3306 else {
3307 /* Positional arguments before star-arguments
3308 are left on the stack. */
3309 VISIT(c, expr, elt);
3310 n++;
3311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003313 if (nseen) {
3314 /* Pack up any trailing positional arguments. */
3315 ADDOP_I(c, BUILD_TUPLE, nseen);
3316 nsubargs++;
3317 }
3318 if (nsubargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 code |= 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003320 if (nsubargs > 1) {
3321 /* If we ended up with more than one stararg, we need
3322 to concatenate them into a single sequence. */
3323 ADDOP_I(c, BUILD_LIST_UNPACK, nsubargs);
3324 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003326
3327 /* Same dance again for keyword arguments */
3328 nseen = 0; /* the number of keyword arguments on the stack following */
3329 nelts = asdl_seq_LEN(keywords);
3330 for (i = 0; i < nelts; i++) {
3331 keyword_ty kw = asdl_seq_GET(keywords, i);
3332 if (kw->arg == NULL) {
3333 /* A keyword argument unpacking. */
3334 if (nseen) {
3335 ADDOP_I(c, BUILD_MAP, nseen);
3336 nseen = 0;
3337 nsubkwargs++;
3338 }
3339 VISIT(c, expr, kw->value);
3340 nsubkwargs++;
3341 }
3342 else if (nsubkwargs) {
3343 /* A keyword argument and we already have a dict. */
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003344 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
Benjamin Petersonee853392015-05-28 14:30:26 -05003345 VISIT(c, expr, kw->value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003346 nseen++;
3347 }
3348 else {
3349 /* keyword argument */
3350 VISIT(c, keyword, kw)
3351 nkw++;
3352 }
3353 }
3354 if (nseen) {
3355 /* Pack up any trailing keyword arguments. */
3356 ADDOP_I(c, BUILD_MAP, nseen);
3357 nsubkwargs++;
3358 }
3359 if (nsubkwargs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 code |= 2;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003361 if (nsubkwargs > 1) {
3362 /* Pack it all up */
3363 int function_pos = n + (code & 1) + nkw + 1;
3364 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs | (function_pos << 8));
3365 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003367 assert(n < 1<<8);
3368 assert(nkw < 1<<24);
3369 n |= nkw << 8;
3370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 switch (code) {
3372 case 0:
3373 ADDOP_I(c, CALL_FUNCTION, n);
3374 break;
3375 case 1:
3376 ADDOP_I(c, CALL_FUNCTION_VAR, n);
3377 break;
3378 case 2:
3379 ADDOP_I(c, CALL_FUNCTION_KW, n);
3380 break;
3381 case 3:
3382 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3383 break;
3384 }
3385 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003386}
3387
Nick Coghlan650f0d02007-04-15 12:05:43 +00003388
3389/* List and set comprehensions and generator expressions work by creating a
3390 nested function to perform the actual iteration. This means that the
3391 iteration variables don't leak into the current scope.
3392 The defined function is called immediately following its definition, with the
3393 result of that call being the result of the expression.
3394 The LC/SC version returns the populated container, while the GE version is
3395 flagged in symtable.c as a generator, so it returns the generator object
3396 when the function is called.
3397 This code *knows* that the loop cannot contain break, continue, or return,
3398 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3399
3400 Possible cleanups:
3401 - iterate over the generator sequence instead of using recursion
3402*/
3403
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003404static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405compiler_comprehension_generator(struct compiler *c,
3406 asdl_seq *generators, int gen_index,
3407 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 /* generate code for the iterator, then each of the ifs,
3410 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 comprehension_ty gen;
3413 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003414 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 start = compiler_new_block(c);
3417 skip = compiler_new_block(c);
3418 if_cleanup = compiler_new_block(c);
3419 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 if (start == NULL || skip == NULL || if_cleanup == NULL ||
3422 anchor == NULL)
3423 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 if (gen_index == 0) {
3428 /* Receive outermost iter as an implicit argument */
3429 c->u->u_argcount = 1;
3430 ADDOP_I(c, LOAD_FAST, 0);
3431 }
3432 else {
3433 /* Sub-iter - calculate on the fly */
3434 VISIT(c, expr, gen->iter);
3435 ADDOP(c, GET_ITER);
3436 }
3437 compiler_use_next_block(c, start);
3438 ADDOP_JREL(c, FOR_ITER, anchor);
3439 NEXT_BLOCK(c);
3440 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 /* XXX this needs to be cleaned up...a lot! */
3443 n = asdl_seq_LEN(gen->ifs);
3444 for (i = 0; i < n; i++) {
3445 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3446 VISIT(c, expr, e);
3447 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3448 NEXT_BLOCK(c);
3449 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 if (++gen_index < asdl_seq_LEN(generators))
3452 if (!compiler_comprehension_generator(c,
3453 generators, gen_index,
3454 elt, val, type))
3455 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 /* only append after the last for generator */
3458 if (gen_index >= asdl_seq_LEN(generators)) {
3459 /* comprehension specific code */
3460 switch (type) {
3461 case COMP_GENEXP:
3462 VISIT(c, expr, elt);
3463 ADDOP(c, YIELD_VALUE);
3464 ADDOP(c, POP_TOP);
3465 break;
3466 case COMP_LISTCOMP:
3467 VISIT(c, expr, elt);
3468 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3469 break;
3470 case COMP_SETCOMP:
3471 VISIT(c, expr, elt);
3472 ADDOP_I(c, SET_ADD, gen_index + 1);
3473 break;
3474 case COMP_DICTCOMP:
3475 /* With 'd[k] = v', v is evaluated before k, so we do
3476 the same. */
3477 VISIT(c, expr, val);
3478 VISIT(c, expr, elt);
3479 ADDOP_I(c, MAP_ADD, gen_index + 1);
3480 break;
3481 default:
3482 return 0;
3483 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 compiler_use_next_block(c, skip);
3486 }
3487 compiler_use_next_block(c, if_cleanup);
3488 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3489 compiler_use_next_block(c, anchor);
3490
3491 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003492}
3493
3494static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003495compiler_comprehension(struct compiler *c, expr_ty e, int type,
3496 identifier name, asdl_seq *generators, expr_ty elt,
3497 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00003498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 PyCodeObject *co = NULL;
3500 expr_ty outermost_iter;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003501 PyObject *qualname = NULL;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 outermost_iter = ((comprehension_ty)
3504 asdl_seq_GET(generators, 0))->iter;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003505
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003506 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3507 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 goto error;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 if (type != COMP_GENEXP) {
3511 int op;
3512 switch (type) {
3513 case COMP_LISTCOMP:
3514 op = BUILD_LIST;
3515 break;
3516 case COMP_SETCOMP:
3517 op = BUILD_SET;
3518 break;
3519 case COMP_DICTCOMP:
3520 op = BUILD_MAP;
3521 break;
3522 default:
3523 PyErr_Format(PyExc_SystemError,
3524 "unknown comprehension type %d", type);
3525 goto error_in_scope;
3526 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 ADDOP_I(c, op, 0);
3529 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00003530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003531 if (!compiler_comprehension_generator(c, generators, 0, elt,
3532 val, type))
3533 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 if (type != COMP_GENEXP) {
3536 ADDOP(c, RETURN_VALUE);
3537 }
3538
3539 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003540 qualname = c->u->u_qualname;
3541 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04003543 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 goto error;
3545
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003546 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003548 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 Py_DECREF(co);
3550
3551 VISIT(c, expr, outermost_iter);
3552 ADDOP(c, GET_ITER);
3553 ADDOP_I(c, CALL_FUNCTION, 1);
3554 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003555error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003557error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01003558 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 Py_XDECREF(co);
3560 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00003561}
3562
3563static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003564compiler_genexp(struct compiler *c, expr_ty e)
3565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 static identifier name;
3567 if (!name) {
3568 name = PyUnicode_FromString("<genexpr>");
3569 if (!name)
3570 return 0;
3571 }
3572 assert(e->kind == GeneratorExp_kind);
3573 return compiler_comprehension(c, e, COMP_GENEXP, name,
3574 e->v.GeneratorExp.generators,
3575 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003576}
3577
3578static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00003579compiler_listcomp(struct compiler *c, expr_ty e)
3580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 static identifier name;
3582 if (!name) {
3583 name = PyUnicode_FromString("<listcomp>");
3584 if (!name)
3585 return 0;
3586 }
3587 assert(e->kind == ListComp_kind);
3588 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
3589 e->v.ListComp.generators,
3590 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003591}
3592
3593static int
3594compiler_setcomp(struct compiler *c, expr_ty e)
3595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 static identifier name;
3597 if (!name) {
3598 name = PyUnicode_FromString("<setcomp>");
3599 if (!name)
3600 return 0;
3601 }
3602 assert(e->kind == SetComp_kind);
3603 return compiler_comprehension(c, e, COMP_SETCOMP, name,
3604 e->v.SetComp.generators,
3605 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00003606}
3607
3608
3609static int
3610compiler_dictcomp(struct compiler *c, expr_ty e)
3611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 static identifier name;
3613 if (!name) {
3614 name = PyUnicode_FromString("<dictcomp>");
3615 if (!name)
3616 return 0;
3617 }
3618 assert(e->kind == DictComp_kind);
3619 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3620 e->v.DictComp.generators,
3621 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00003622}
3623
3624
3625static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003626compiler_visit_keyword(struct compiler *c, keyword_ty k)
3627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3629 VISIT(c, expr, k->value);
3630 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003631}
3632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003634 whether they are true or false.
3635
3636 Return values: 1 for true, 0 for false, -1 for non-constant.
3637 */
3638
3639static int
Georg Brandl8334fd92010-12-04 10:26:46 +00003640expr_constant(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 char *id;
3643 switch (e->kind) {
3644 case Ellipsis_kind:
3645 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003646 case Constant_kind:
3647 return PyObject_IsTrue(e->v.Constant.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 case Num_kind:
3649 return PyObject_IsTrue(e->v.Num.n);
3650 case Str_kind:
3651 return PyObject_IsTrue(e->v.Str.s);
3652 case Name_kind:
3653 /* optimize away names that can't be reassigned */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003654 id = PyUnicode_AsUTF8(e->v.Name.id);
Benjamin Peterson442f2092012-12-06 17:41:04 -05003655 if (id && strcmp(id, "__debug__") == 0)
3656 return !c->c_optimize;
3657 return -1;
3658 case NameConstant_kind: {
3659 PyObject *o = e->v.NameConstant.value;
3660 if (o == Py_None)
3661 return 0;
3662 else if (o == Py_True)
3663 return 1;
3664 else if (o == Py_False)
3665 return 0;
3666 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 default:
3668 return -1;
3669 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670}
3671
Yury Selivanov75445082015-05-11 22:57:16 -04003672
3673/*
3674 Implements the async with statement.
3675
3676 The semantics outlined in that PEP are as follows:
3677
3678 async with EXPR as VAR:
3679 BLOCK
3680
3681 It is implemented roughly as:
3682
3683 context = EXPR
3684 exit = context.__aexit__ # not calling it
3685 value = await context.__aenter__()
3686 try:
3687 VAR = value # if VAR present in the syntax
3688 BLOCK
3689 finally:
3690 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03003691 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04003692 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03003693 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04003694 if not (await exit(*exc)):
3695 raise
3696 */
3697static int
3698compiler_async_with(struct compiler *c, stmt_ty s, int pos)
3699{
3700 basicblock *block, *finally;
3701 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
3702
3703 assert(s->kind == AsyncWith_kind);
3704
3705 block = compiler_new_block(c);
3706 finally = compiler_new_block(c);
3707 if (!block || !finally)
3708 return 0;
3709
3710 /* Evaluate EXPR */
3711 VISIT(c, expr, item->context_expr);
3712
3713 ADDOP(c, BEFORE_ASYNC_WITH);
3714 ADDOP(c, GET_AWAITABLE);
3715 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3716 ADDOP(c, YIELD_FROM);
3717
3718 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
3719
3720 /* SETUP_ASYNC_WITH pushes a finally block. */
3721 compiler_use_next_block(c, block);
3722 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3723 return 0;
3724 }
3725
3726 if (item->optional_vars) {
3727 VISIT(c, expr, item->optional_vars);
3728 }
3729 else {
3730 /* Discard result from context.__aenter__() */
3731 ADDOP(c, POP_TOP);
3732 }
3733
3734 pos++;
3735 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
3736 /* BLOCK code */
3737 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
3738 else if (!compiler_async_with(c, s, pos))
3739 return 0;
3740
3741 /* End of try block; start the finally block */
3742 ADDOP(c, POP_BLOCK);
3743 compiler_pop_fblock(c, FINALLY_TRY, block);
3744
3745 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3746 compiler_use_next_block(c, finally);
3747 if (!compiler_push_fblock(c, FINALLY_END, finally))
3748 return 0;
3749
3750 /* Finally block starts; context.__exit__ is on the stack under
3751 the exception or return information. Just issue our magic
3752 opcode. */
3753 ADDOP(c, WITH_CLEANUP_START);
3754
3755 ADDOP(c, GET_AWAITABLE);
3756 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3757 ADDOP(c, YIELD_FROM);
3758
3759 ADDOP(c, WITH_CLEANUP_FINISH);
3760
3761 /* Finally block ends. */
3762 ADDOP(c, END_FINALLY);
3763 compiler_pop_fblock(c, FINALLY_END, finally);
3764 return 1;
3765}
3766
3767
Guido van Rossumc2e20742006-02-27 22:32:47 +00003768/*
3769 Implements the with statement from PEP 343.
3770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 The semantics outlined in that PEP are as follows:
Guido van Rossumc2e20742006-02-27 22:32:47 +00003772
3773 with EXPR as VAR:
3774 BLOCK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775
Guido van Rossumc2e20742006-02-27 22:32:47 +00003776 It is implemented roughly as:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777
Thomas Wouters477c8d52006-05-27 19:21:47 +00003778 context = EXPR
Guido van Rossumc2e20742006-02-27 22:32:47 +00003779 exit = context.__exit__ # not calling it
3780 value = context.__enter__()
3781 try:
3782 VAR = value # if VAR present in the syntax
3783 BLOCK
3784 finally:
3785 if an exception was raised:
Serhiy Storchakad741a882015-06-11 00:06:39 +03003786 exc = copy of (exception, instance, traceback)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003787 else:
Serhiy Storchakad741a882015-06-11 00:06:39 +03003788 exc = (None, None, None)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003789 exit(*exc)
3790 */
3791static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003792compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00003793{
Guido van Rossumc2e20742006-02-27 22:32:47 +00003794 basicblock *block, *finally;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003795 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003796
3797 assert(s->kind == With_kind);
3798
Guido van Rossumc2e20742006-02-27 22:32:47 +00003799 block = compiler_new_block(c);
3800 finally = compiler_new_block(c);
3801 if (!block || !finally)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003802 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003803
Thomas Wouters477c8d52006-05-27 19:21:47 +00003804 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003805 VISIT(c, expr, item->context_expr);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003806 ADDOP_JREL(c, SETUP_WITH, finally);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003807
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003808 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00003809 compiler_use_next_block(c, block);
3810 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003811 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003812 }
3813
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003814 if (item->optional_vars) {
3815 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003816 }
3817 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003819 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003820 }
3821
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003822 pos++;
3823 if (pos == asdl_seq_LEN(s->v.With.items))
3824 /* BLOCK code */
3825 VISIT_SEQ(c, stmt, s->v.With.body)
3826 else if (!compiler_with(c, s, pos))
3827 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003828
3829 /* End of try block; start the finally block */
3830 ADDOP(c, POP_BLOCK);
3831 compiler_pop_fblock(c, FINALLY_TRY, block);
3832
3833 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3834 compiler_use_next_block(c, finally);
3835 if (!compiler_push_fblock(c, FINALLY_END, finally))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00003836 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00003837
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003838 /* Finally block starts; context.__exit__ is on the stack under
3839 the exception or return information. Just issue our magic
3840 opcode. */
Yury Selivanov75445082015-05-11 22:57:16 -04003841 ADDOP(c, WITH_CLEANUP_START);
3842 ADDOP(c, WITH_CLEANUP_FINISH);
Guido van Rossumc2e20742006-02-27 22:32:47 +00003843
3844 /* Finally block ends. */
3845 ADDOP(c, END_FINALLY);
3846 compiler_pop_fblock(c, FINALLY_END, finally);
3847 return 1;
3848}
3849
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003850static int
3851compiler_visit_expr(struct compiler *c, expr_ty e)
3852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 /* If expr e has a different line number than the last expr/stmt,
3854 set a new line number for the next instruction.
3855 */
3856 if (e->lineno > c->u->u_lineno) {
3857 c->u->u_lineno = e->lineno;
3858 c->u->u_lineno_set = 0;
3859 }
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00003860 /* Updating the column offset is always harmless. */
3861 c->u->u_col_offset = e->col_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 switch (e->kind) {
3863 case BoolOp_kind:
3864 return compiler_boolop(c, e);
3865 case BinOp_kind:
3866 VISIT(c, expr, e->v.BinOp.left);
3867 VISIT(c, expr, e->v.BinOp.right);
3868 ADDOP(c, binop(c, e->v.BinOp.op));
3869 break;
3870 case UnaryOp_kind:
3871 VISIT(c, expr, e->v.UnaryOp.operand);
3872 ADDOP(c, unaryop(e->v.UnaryOp.op));
3873 break;
3874 case Lambda_kind:
3875 return compiler_lambda(c, e);
3876 case IfExp_kind:
3877 return compiler_ifexp(c, e);
3878 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003879 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003881 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 case GeneratorExp_kind:
3883 return compiler_genexp(c, e);
3884 case ListComp_kind:
3885 return compiler_listcomp(c, e);
3886 case SetComp_kind:
3887 return compiler_setcomp(c, e);
3888 case DictComp_kind:
3889 return compiler_dictcomp(c, e);
3890 case Yield_kind:
3891 if (c->u->u_ste->ste_type != FunctionBlock)
3892 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003893 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3894 return compiler_error(c, "'yield' inside async function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003895 if (e->v.Yield.value) {
3896 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 }
3898 else {
3899 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3900 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003901 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003903 case YieldFrom_kind:
3904 if (c->u->u_ste->ste_type != FunctionBlock)
3905 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04003906
3907 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
3908 return compiler_error(c, "'yield from' inside async function");
3909
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003910 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003911 ADDOP(c, GET_YIELD_FROM_ITER);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00003912 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3913 ADDOP(c, YIELD_FROM);
3914 break;
Yury Selivanov75445082015-05-11 22:57:16 -04003915 case Await_kind:
3916 if (c->u->u_ste->ste_type != FunctionBlock)
3917 return compiler_error(c, "'await' outside function");
3918
Yury Selivanov9dec0352015-06-30 12:49:04 -04003919 if (c->u->u_scope_type == COMPILER_SCOPE_COMPREHENSION)
3920 return compiler_error(
3921 c, "'await' expressions in comprehensions are not supported");
3922
Yury Selivanov75445082015-05-11 22:57:16 -04003923 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION)
3924 return compiler_error(c, "'await' outside async function");
3925
3926 VISIT(c, expr, e->v.Await.value);
3927 ADDOP(c, GET_AWAITABLE);
3928 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3929 ADDOP(c, YIELD_FROM);
3930 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 case Compare_kind:
3932 return compiler_compare(c, e);
3933 case Call_kind:
3934 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003935 case Constant_kind:
3936 ADDOP_O(c, LOAD_CONST, e->v.Constant.value, consts);
3937 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 case Num_kind:
3939 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3940 break;
3941 case Str_kind:
3942 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3943 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04003944 case JoinedStr_kind:
3945 return compiler_joined_str(c, e);
3946 case FormattedValue_kind:
3947 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 case Bytes_kind:
3949 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3950 break;
3951 case Ellipsis_kind:
3952 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3953 break;
Benjamin Peterson442f2092012-12-06 17:41:04 -05003954 case NameConstant_kind:
3955 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3956 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 /* The following exprs can be assignment targets. */
3958 case Attribute_kind:
3959 if (e->v.Attribute.ctx != AugStore)
3960 VISIT(c, expr, e->v.Attribute.value);
3961 switch (e->v.Attribute.ctx) {
3962 case AugLoad:
3963 ADDOP(c, DUP_TOP);
3964 /* Fall through to load */
3965 case Load:
3966 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
3967 break;
3968 case AugStore:
3969 ADDOP(c, ROT_TWO);
3970 /* Fall through to save */
3971 case Store:
3972 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
3973 break;
3974 case Del:
3975 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
3976 break;
3977 case Param:
3978 default:
3979 PyErr_SetString(PyExc_SystemError,
3980 "param invalid in attribute expression");
3981 return 0;
3982 }
3983 break;
3984 case Subscript_kind:
3985 switch (e->v.Subscript.ctx) {
3986 case AugLoad:
3987 VISIT(c, expr, e->v.Subscript.value);
3988 VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);
3989 break;
3990 case Load:
3991 VISIT(c, expr, e->v.Subscript.value);
3992 VISIT_SLICE(c, e->v.Subscript.slice, Load);
3993 break;
3994 case AugStore:
3995 VISIT_SLICE(c, e->v.Subscript.slice, AugStore);
3996 break;
3997 case Store:
3998 VISIT(c, expr, e->v.Subscript.value);
3999 VISIT_SLICE(c, e->v.Subscript.slice, Store);
4000 break;
4001 case Del:
4002 VISIT(c, expr, e->v.Subscript.value);
4003 VISIT_SLICE(c, e->v.Subscript.slice, Del);
4004 break;
4005 case Param:
4006 default:
4007 PyErr_SetString(PyExc_SystemError,
4008 "param invalid in subscript expression");
4009 return 0;
4010 }
4011 break;
4012 case Starred_kind:
4013 switch (e->v.Starred.ctx) {
4014 case Store:
4015 /* In all legitimate cases, the Starred node was already replaced
4016 * by compiler_list/compiler_tuple. XXX: is that okay? */
4017 return compiler_error(c,
4018 "starred assignment target must be in a list or tuple");
4019 default:
4020 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004021 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 }
4023 break;
4024 case Name_kind:
4025 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
4026 /* child nodes of List and Tuple will have expr_context set */
4027 case List_kind:
4028 return compiler_list(c, e);
4029 case Tuple_kind:
4030 return compiler_tuple(c, e);
4031 }
4032 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004033}
4034
4035static int
4036compiler_augassign(struct compiler *c, stmt_ty s)
4037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004038 expr_ty e = s->v.AugAssign.target;
4039 expr_ty auge;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 assert(s->kind == AugAssign_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 switch (e->kind) {
4044 case Attribute_kind:
4045 auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,
4046 AugLoad, e->lineno, e->col_offset, c->c_arena);
4047 if (auge == NULL)
4048 return 0;
4049 VISIT(c, expr, auge);
4050 VISIT(c, expr, s->v.AugAssign.value);
4051 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4052 auge->v.Attribute.ctx = AugStore;
4053 VISIT(c, expr, auge);
4054 break;
4055 case Subscript_kind:
4056 auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,
4057 AugLoad, e->lineno, e->col_offset, c->c_arena);
4058 if (auge == NULL)
4059 return 0;
4060 VISIT(c, expr, auge);
4061 VISIT(c, expr, s->v.AugAssign.value);
4062 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4063 auge->v.Subscript.ctx = AugStore;
4064 VISIT(c, expr, auge);
4065 break;
4066 case Name_kind:
4067 if (!compiler_nameop(c, e->v.Name.id, Load))
4068 return 0;
4069 VISIT(c, expr, s->v.AugAssign.value);
4070 ADDOP(c, inplace_binop(c, s->v.AugAssign.op));
4071 return compiler_nameop(c, e->v.Name.id, Store);
4072 default:
4073 PyErr_Format(PyExc_SystemError,
4074 "invalid node type (%d) for augmented assignment",
4075 e->kind);
4076 return 0;
4077 }
4078 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079}
4080
4081static int
4082compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 struct fblockinfo *f;
4085 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
4086 PyErr_SetString(PyExc_SystemError,
4087 "too many statically nested blocks");
4088 return 0;
4089 }
4090 f = &c->u->u_fblock[c->u->u_nfblocks++];
4091 f->fb_type = t;
4092 f->fb_block = b;
4093 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004094}
4095
4096static void
4097compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
4098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 struct compiler_unit *u = c->u;
4100 assert(u->u_nfblocks > 0);
4101 u->u_nfblocks--;
4102 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
4103 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004104}
4105
Thomas Wouters89f507f2006-12-13 04:49:30 +00004106static int
4107compiler_in_loop(struct compiler *c) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 int i;
4109 struct compiler_unit *u = c->u;
4110 for (i = 0; i < u->u_nfblocks; ++i) {
4111 if (u->u_fblock[i].fb_type == LOOP)
4112 return 1;
4113 }
4114 return 0;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004115}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004116/* Raises a SyntaxError and returns 0.
4117 If something goes wrong, a different exception may be raised.
4118*/
4119
4120static int
4121compiler_error(struct compiler *c, const char *errstr)
4122{
Benjamin Peterson43b06862011-05-27 09:08:01 -05004123 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004125
Victor Stinner14e461d2013-08-26 22:28:21 +02004126 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004127 if (!loc) {
4128 Py_INCREF(Py_None);
4129 loc = Py_None;
4130 }
Victor Stinner14e461d2013-08-26 22:28:21 +02004131 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00004132 c->u->u_col_offset, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 if (!u)
4134 goto exit;
4135 v = Py_BuildValue("(zO)", errstr, u);
4136 if (!v)
4137 goto exit;
4138 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 Py_DECREF(loc);
4141 Py_XDECREF(u);
4142 Py_XDECREF(v);
4143 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004144}
4145
4146static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147compiler_handle_subscr(struct compiler *c, const char *kind,
4148 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 /* XXX this code is duplicated */
4153 switch (ctx) {
4154 case AugLoad: /* fall through to Load */
4155 case Load: op = BINARY_SUBSCR; break;
4156 case AugStore:/* fall through to Store */
4157 case Store: op = STORE_SUBSCR; break;
4158 case Del: op = DELETE_SUBSCR; break;
4159 case Param:
4160 PyErr_Format(PyExc_SystemError,
4161 "invalid %s kind %d in subscript\n",
4162 kind, ctx);
4163 return 0;
4164 }
4165 if (ctx == AugLoad) {
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00004166 ADDOP(c, DUP_TOP_TWO);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 }
4168 else if (ctx == AugStore) {
4169 ADDOP(c, ROT_THREE);
4170 }
4171 ADDOP(c, op);
4172 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004173}
4174
4175static int
4176compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4177{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 int n = 2;
4179 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 /* only handles the cases where BUILD_SLICE is emitted */
4182 if (s->v.Slice.lower) {
4183 VISIT(c, expr, s->v.Slice.lower);
4184 }
4185 else {
4186 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4187 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 if (s->v.Slice.upper) {
4190 VISIT(c, expr, s->v.Slice.upper);
4191 }
4192 else {
4193 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4194 }
4195
4196 if (s->v.Slice.step) {
4197 n++;
4198 VISIT(c, expr, s->v.Slice.step);
4199 }
4200 ADDOP_I(c, BUILD_SLICE, n);
4201 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004202}
4203
4204static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205compiler_visit_nested_slice(struct compiler *c, slice_ty s,
4206 expr_context_ty ctx)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 switch (s->kind) {
4209 case Slice_kind:
4210 return compiler_slice(c, s, ctx);
4211 case Index_kind:
4212 VISIT(c, expr, s->v.Index.value);
4213 break;
4214 case ExtSlice_kind:
4215 default:
4216 PyErr_SetString(PyExc_SystemError,
4217 "extended slice invalid in nested slice");
4218 return 0;
4219 }
4220 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004221}
4222
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004223static int
4224compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)
4225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 char * kindname = NULL;
4227 switch (s->kind) {
4228 case Index_kind:
4229 kindname = "index";
4230 if (ctx != AugStore) {
4231 VISIT(c, expr, s->v.Index.value);
4232 }
4233 break;
4234 case Slice_kind:
4235 kindname = "slice";
4236 if (ctx != AugStore) {
4237 if (!compiler_slice(c, s, ctx))
4238 return 0;
4239 }
4240 break;
4241 case ExtSlice_kind:
4242 kindname = "extended slice";
4243 if (ctx != AugStore) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004244 Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004245 for (i = 0; i < n; i++) {
4246 slice_ty sub = (slice_ty)asdl_seq_GET(
4247 s->v.ExtSlice.dims, i);
4248 if (!compiler_visit_nested_slice(c, sub, ctx))
4249 return 0;
4250 }
4251 ADDOP_I(c, BUILD_TUPLE, n);
4252 }
4253 break;
4254 default:
4255 PyErr_Format(PyExc_SystemError,
4256 "invalid subscript kind %d", s->kind);
4257 return 0;
4258 }
4259 return compiler_handle_subscr(c, kindname, ctx);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004260}
4261
Thomas Wouters89f507f2006-12-13 04:49:30 +00004262/* End of the compiler section, beginning of the assembler section */
4263
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004264/* do depth-first search of basic block graph, starting with block.
4265 post records the block indices in post-order.
4266
4267 XXX must handle implicit jumps from one block to next
4268*/
4269
Thomas Wouters89f507f2006-12-13 04:49:30 +00004270struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 PyObject *a_bytecode; /* string containing bytecode */
4272 int a_offset; /* offset into bytecode */
4273 int a_nblocks; /* number of reachable blocks */
4274 basicblock **a_postorder; /* list of blocks in dfs postorder */
4275 PyObject *a_lnotab; /* string containing lnotab */
4276 int a_lnotab_off; /* offset into lnotab */
4277 int a_lineno; /* last lineno of emitted instruction */
4278 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00004279};
4280
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004281static void
4282dfs(struct compiler *c, basicblock *b, struct assembler *a)
4283{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 int i;
4285 struct instr *instr = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 if (b->b_seen)
4288 return;
4289 b->b_seen = 1;
4290 if (b->b_next != NULL)
4291 dfs(c, b->b_next, a);
4292 for (i = 0; i < b->b_iused; i++) {
4293 instr = &b->b_instr[i];
4294 if (instr->i_jrel || instr->i_jabs)
4295 dfs(c, instr->i_target, a);
4296 }
4297 a->a_postorder[a->a_nblocks++] = b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004298}
4299
Neal Norwitz2744c6c2005-11-13 01:08:38 +00004300static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004301stackdepth_walk(struct compiler *c, basicblock *b, int depth, int maxdepth)
4302{
Larry Hastings3a907972013-11-23 14:49:22 -08004303 int i, target_depth, effect;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 struct instr *instr;
4305 if (b->b_seen || b->b_startdepth >= depth)
4306 return maxdepth;
4307 b->b_seen = 1;
4308 b->b_startdepth = depth;
4309 for (i = 0; i < b->b_iused; i++) {
4310 instr = &b->b_instr[i];
Larry Hastings3a907972013-11-23 14:49:22 -08004311 effect = PyCompile_OpcodeStackEffect(instr->i_opcode, instr->i_oparg);
4312 if (effect == PY_INVALID_STACK_EFFECT) {
4313 fprintf(stderr, "opcode = %d\n", instr->i_opcode);
4314 Py_FatalError("PyCompile_OpcodeStackEffect()");
4315 }
4316 depth += effect;
4317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 if (depth > maxdepth)
4319 maxdepth = depth;
4320 assert(depth >= 0); /* invalid code or bug in stackdepth() */
4321 if (instr->i_jrel || instr->i_jabs) {
4322 target_depth = depth;
4323 if (instr->i_opcode == FOR_ITER) {
4324 target_depth = depth-2;
Antoine Pitrou99614052014-05-23 11:46:03 +02004325 }
4326 else if (instr->i_opcode == SETUP_FINALLY ||
4327 instr->i_opcode == SETUP_EXCEPT) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 target_depth = depth+3;
4329 if (target_depth > maxdepth)
4330 maxdepth = target_depth;
4331 }
Antoine Pitrou99614052014-05-23 11:46:03 +02004332 else if (instr->i_opcode == JUMP_IF_TRUE_OR_POP ||
4333 instr->i_opcode == JUMP_IF_FALSE_OR_POP)
4334 depth = depth - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 maxdepth = stackdepth_walk(c, instr->i_target,
4336 target_depth, maxdepth);
4337 if (instr->i_opcode == JUMP_ABSOLUTE ||
4338 instr->i_opcode == JUMP_FORWARD) {
4339 goto out; /* remaining code is dead */
4340 }
4341 }
4342 }
4343 if (b->b_next)
4344 maxdepth = stackdepth_walk(c, b->b_next, depth, maxdepth);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004345out:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 b->b_seen = 0;
4347 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004348}
4349
4350/* Find the flow path that needs the largest stack. We assume that
4351 * cycles in the flow graph have no net effect on the stack depth.
4352 */
4353static int
4354stackdepth(struct compiler *c)
4355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 basicblock *b, *entryblock;
4357 entryblock = NULL;
4358 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4359 b->b_seen = 0;
4360 b->b_startdepth = INT_MIN;
4361 entryblock = b;
4362 }
4363 if (!entryblock)
4364 return 0;
4365 return stackdepth_walk(c, entryblock, 0, 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004366}
4367
4368static int
4369assemble_init(struct assembler *a, int nblocks, int firstlineno)
4370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 memset(a, 0, sizeof(struct assembler));
4372 a->a_lineno = firstlineno;
4373 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
4374 if (!a->a_bytecode)
4375 return 0;
4376 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
4377 if (!a->a_lnotab)
4378 return 0;
Christian Heimes724b8282013-12-04 08:42:46 +01004379 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 PyErr_NoMemory();
4381 return 0;
4382 }
4383 a->a_postorder = (basicblock **)PyObject_Malloc(
4384 sizeof(basicblock *) * nblocks);
4385 if (!a->a_postorder) {
4386 PyErr_NoMemory();
4387 return 0;
4388 }
4389 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004390}
4391
4392static void
4393assemble_free(struct assembler *a)
4394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 Py_XDECREF(a->a_bytecode);
4396 Py_XDECREF(a->a_lnotab);
4397 if (a->a_postorder)
4398 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004399}
4400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004401static int
4402blocksize(basicblock *b)
4403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 int i;
4405 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004408 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004410}
4411
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004412/* Appends a pair to the end of the line number table, a_lnotab, representing
4413 the instruction's bytecode offset and line number. See
4414 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00004415
Guido van Rossumf68d8e52001-04-14 17:55:09 +00004416static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004417assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004420 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004423 d_bytecode = a->a_offset - a->a_lineno_off;
4424 d_lineno = i->i_lineno - a->a_lineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 assert(d_bytecode >= 0);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if(d_bytecode == 0 && d_lineno == 0)
4429 return 1;
Guido van Rossum4bad92c1991-07-27 21:34:52 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 if (d_bytecode > 255) {
4432 int j, nbytes, ncodes = d_bytecode / 255;
4433 nbytes = a->a_lnotab_off + 2 * ncodes;
4434 len = PyBytes_GET_SIZE(a->a_lnotab);
4435 if (nbytes >= len) {
4436 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
4437 len = nbytes;
4438 else if (len <= INT_MAX / 2)
4439 len *= 2;
4440 else {
4441 PyErr_NoMemory();
4442 return 0;
4443 }
4444 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4445 return 0;
4446 }
4447 lnotab = (unsigned char *)
4448 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4449 for (j = 0; j < ncodes; j++) {
4450 *lnotab++ = 255;
4451 *lnotab++ = 0;
4452 }
4453 d_bytecode -= ncodes * 255;
4454 a->a_lnotab_off += ncodes * 2;
4455 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01004456 assert(0 <= d_bytecode && d_bytecode <= 255);
4457
4458 if (d_lineno < -128 || 127 < d_lineno) {
4459 int j, nbytes, ncodes, k;
4460 if (d_lineno < 0) {
4461 k = -128;
4462 /* use division on positive numbers */
4463 ncodes = (-d_lineno) / 128;
4464 }
4465 else {
4466 k = 127;
4467 ncodes = d_lineno / 127;
4468 }
4469 d_lineno -= ncodes * k;
4470 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 nbytes = a->a_lnotab_off + 2 * ncodes;
4472 len = PyBytes_GET_SIZE(a->a_lnotab);
4473 if (nbytes >= len) {
4474 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
4475 len = nbytes;
4476 else if (len <= INT_MAX / 2)
4477 len *= 2;
4478 else {
4479 PyErr_NoMemory();
4480 return 0;
4481 }
4482 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4483 return 0;
4484 }
4485 lnotab = (unsigned char *)
4486 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4487 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01004488 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 d_bytecode = 0;
4490 for (j = 1; j < ncodes; j++) {
4491 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01004492 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 a->a_lnotab_off += ncodes * 2;
4495 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01004496 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 len = PyBytes_GET_SIZE(a->a_lnotab);
4499 if (a->a_lnotab_off + 2 >= len) {
4500 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
4501 return 0;
4502 }
4503 lnotab = (unsigned char *)
4504 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00004505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 a->a_lnotab_off += 2;
4507 if (d_bytecode) {
4508 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004509 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 }
4511 else { /* First line of a block; def stmt, etc. */
4512 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02004513 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 }
4515 a->a_lineno = i->i_lineno;
4516 a->a_lineno_off = a->a_offset;
4517 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004518}
4519
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004520/* assemble_emit()
4521 Extend the bytecode with a new instruction.
4522 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00004523*/
4524
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004525static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004526assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00004527{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004528 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4530 char *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004531
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004532 arg = i->i_oparg;
4533 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 if (i->i_lineno && !assemble_lnotab(a, i))
4535 return 0;
4536 if (a->a_offset + size >= len) {
4537 if (len > PY_SSIZE_T_MAX / 2)
4538 return 0;
4539 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4540 return 0;
4541 }
4542 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4543 a->a_offset += size;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004544 write_op_arg((unsigned char*)code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004546}
4547
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00004548static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004549assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00004550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004552 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00004554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 /* Compute the size of each block and fixup jump args.
4556 Replace block pointer with position in bytecode. */
4557 do {
4558 totsize = 0;
4559 for (i = a->a_nblocks - 1; i >= 0; i--) {
4560 b = a->a_postorder[i];
4561 bsize = blocksize(b);
4562 b->b_offset = totsize;
4563 totsize += bsize;
4564 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004565 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4567 bsize = b->b_offset;
4568 for (i = 0; i < b->b_iused; i++) {
4569 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004570 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 /* Relative jumps are computed relative to
4572 the instruction pointer after fetching
4573 the jump instruction.
4574 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004575 bsize += isize;
4576 if (instr->i_jabs || instr->i_jrel) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 instr->i_oparg = instr->i_target->b_offset;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004578 if (instr->i_jrel) {
4579 instr->i_oparg -= bsize;
4580 }
4581 if (instrsize(instr->i_oparg) != isize) {
4582 extended_arg_recompile = 1;
4583 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 }
4586 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00004587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 /* XXX: This is an awful hack that could hurt performance, but
4589 on the bright side it should work until we come up
4590 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 The issue is that in the first loop blocksize() is called
4593 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004594 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00004596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 So we loop until we stop seeing new EXTENDED_ARGs.
4598 The only EXTENDED_ARGs that could be popping up are
4599 ones in jump instructions. So this should converge
4600 fairly quickly.
4601 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004602 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004603}
4604
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004605static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01004606dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 PyObject *tuple, *k, *v;
4609 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 tuple = PyTuple_New(size);
4612 if (tuple == NULL)
4613 return NULL;
4614 while (PyDict_Next(dict, &pos, &k, &v)) {
4615 i = PyLong_AS_LONG(v);
Victor Stinnerefb24132016-01-22 12:33:12 +01004616 /* The keys of the dictionary are tuples. (see compiler_add_o
4617 * and _PyCode_ConstantKey). The object we want is always second,
4618 * though. */
4619 k = PyTuple_GET_ITEM(k, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 Py_INCREF(k);
4621 assert((i - offset) < size);
4622 assert((i - offset) >= 0);
4623 PyTuple_SET_ITEM(tuple, i - offset, k);
4624 }
4625 return tuple;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004626}
4627
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004628static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004629compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004632 int flags = 0;
4633 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04004635 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 if (ste->ste_nested)
4637 flags |= CO_NESTED;
4638 if (ste->ste_generator)
4639 flags |= CO_GENERATOR;
4640 if (ste->ste_varargs)
4641 flags |= CO_VARARGS;
4642 if (ste->ste_varkeywords)
4643 flags |= CO_VARKEYWORDS;
4644 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 /* (Only) inherit compilerflags in PyCF_MASK */
4647 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00004648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 n = PyDict_Size(c->u->u_freevars);
4650 if (n < 0)
4651 return -1;
4652 if (n == 0) {
4653 n = PyDict_Size(c->u->u_cellvars);
4654 if (n < 0)
Victor Stinnerad9a0662013-11-19 22:23:20 +01004655 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 if (n == 0) {
Victor Stinnerad9a0662013-11-19 22:23:20 +01004657 flags |= CO_NOFREE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 }
4659 }
Jeremy Hyltond7f393e2001-02-12 16:01:03 +00004660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00004662}
4663
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004664static PyCodeObject *
4665makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 PyObject *tmp;
4668 PyCodeObject *co = NULL;
4669 PyObject *consts = NULL;
4670 PyObject *names = NULL;
4671 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyObject *name = NULL;
4673 PyObject *freevars = NULL;
4674 PyObject *cellvars = NULL;
4675 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004676 Py_ssize_t nlocals;
4677 int nlocals_int;
4678 int flags;
Victor Stinnerf8e32212013-11-19 23:56:34 +01004679 int argcount, kwonlyargcount;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 tmp = dict_keys_inorder(c->u->u_consts, 0);
4682 if (!tmp)
4683 goto error;
4684 consts = PySequence_List(tmp); /* optimize_code requires a list */
4685 Py_DECREF(tmp);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004687 names = dict_keys_inorder(c->u->u_names, 0);
4688 varnames = dict_keys_inorder(c->u->u_varnames, 0);
4689 if (!consts || !names || !varnames)
4690 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
4693 if (!cellvars)
4694 goto error;
4695 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
4696 if (!freevars)
4697 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 nlocals = PyDict_Size(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01004700 assert(nlocals < INT_MAX);
4701 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
4702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 flags = compute_code_flags(c);
4704 if (flags < 0)
4705 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004707 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
4708 if (!bytecode)
4709 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4712 if (!tmp)
4713 goto error;
4714 Py_DECREF(consts);
4715 consts = tmp;
4716
Victor Stinnerf8e32212013-11-19 23:56:34 +01004717 argcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
4718 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
4719 co = PyCode_New(argcount, kwonlyargcount,
Victor Stinnerad9a0662013-11-19 22:23:20 +01004720 nlocals_int, stackdepth(c), flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 bytecode, consts, names, varnames,
4722 freevars, cellvars,
Victor Stinner14e461d2013-08-26 22:28:21 +02004723 c->c_filename, c->u->u_name,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 c->u->u_firstlineno,
4725 a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004726 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 Py_XDECREF(consts);
4728 Py_XDECREF(names);
4729 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 Py_XDECREF(name);
4731 Py_XDECREF(freevars);
4732 Py_XDECREF(cellvars);
4733 Py_XDECREF(bytecode);
4734 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004735}
4736
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004737
4738/* For debugging purposes only */
4739#if 0
4740static void
4741dump_instr(const struct instr *i)
4742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 const char *jrel = i->i_jrel ? "jrel " : "";
4744 const char *jabs = i->i_jabs ? "jabs " : "";
4745 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004748 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03004750 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4752 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004753}
4754
4755static void
4756dump_basicblock(const basicblock *b)
4757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 const char *seen = b->b_seen ? "seen " : "";
4759 const char *b_return = b->b_return ? "return " : "";
4760 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
4761 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
4762 if (b->b_instr) {
4763 int i;
4764 for (i = 0; i < b->b_iused; i++) {
4765 fprintf(stderr, " [%02d] ", i);
4766 dump_instr(b->b_instr + i);
4767 }
4768 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004769}
4770#endif
4771
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004772static PyCodeObject *
4773assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 basicblock *b, *entryblock;
4776 struct assembler a;
4777 int i, j, nblocks;
4778 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00004779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 /* Make sure every block that falls off the end returns None.
4781 XXX NEXT_BLOCK() isn't quite right, because if the last
4782 block ends with a jump or return b_next shouldn't set.
4783 */
4784 if (!c->u->u_curblock->b_return) {
4785 NEXT_BLOCK(c);
4786 if (addNone)
4787 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4788 ADDOP(c, RETURN_VALUE);
4789 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 nblocks = 0;
4792 entryblock = NULL;
4793 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4794 nblocks++;
4795 entryblock = b;
4796 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 /* Set firstlineno if it wasn't explicitly set. */
4799 if (!c->u->u_firstlineno) {
4800 if (entryblock && entryblock->b_instr)
4801 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4802 else
4803 c->u->u_firstlineno = 1;
4804 }
4805 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4806 goto error;
4807 dfs(c, entryblock, &a);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 /* Can't modify the bytecode after computing jump offsets. */
4810 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00004811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 /* Emit code in reverse postorder from dfs. */
4813 for (i = a.a_nblocks - 1; i >= 0; i--) {
4814 b = a.a_postorder[i];
4815 for (j = 0; j < b->b_iused; j++)
4816 if (!assemble_emit(&a, &b->b_instr[j]))
4817 goto error;
4818 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00004819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4821 goto error;
4822 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0)
4823 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004826 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 assemble_free(&a);
4828 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00004829}
Georg Brandl8334fd92010-12-04 10:26:46 +00004830
4831#undef PyAST_Compile
4832PyAPI_FUNC(PyCodeObject *)
4833PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4834 PyArena *arena)
4835{
4836 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4837}